// da.h
static float sumAcross32(const float s, const long n, const float *x) {
float ret = s;
for (long i = 0; i < n; i++) {
ret += x[i];
}
return ret;
}
static double sumAcross64(const double s, const long n, const double *x) {
double ret = s;
for (long i = 0; i < n; i++) {
ret += x[i];
}
return ret;
}
require "da.h"; // or name it on command line
use CPtr;
use SysCTypes;
extern proc sumAcross32(s: real(32), n: c_long, x: c_ptr(real(32))):real(32);
extern proc sumAcross64(s: real(64), n: c_long, x: c_ptr(real(64))):real(64);
proc sumAcross(s: ?t, arr: [] t) {
// Note, this will not work for non-contiguous array storage:
// * multidimensional arrays might or might not work the way you want
// * arrays with strided domains probably don't do what you expect
if t == real(32) {
return sumAcross32(s:real(32), arr.size, c_ptrTo(arr));
} else if t == real(64) {
return sumAcross64(s:real(64), arr.size, c_ptrTo(arr));
} else {
compilerError("sumAcross not implemented yet for this type");
}
}
var x : [1..10] real(64);
var sumX = sumAcross(1.0, x);
writeln(sumX);
var u : [20..40] real(32);
var sumU = sumAcross(1.0:real(32), u);
writeln(sumU);