-
Notifications
You must be signed in to change notification settings - Fork 0
/
rec_sum.jtmpl
51 lines (37 loc) · 1.02 KB
/
rec_sum.jtmpl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// Same as
//
// sumWithAccumulator :: Num a => [a] -> a -> a
// sumWithAccumulator [] acc = acc
// sumWithAccumulator (x:xs) acc = sumWithAccumulator xs (acc + x)
// sumList :: Num a => [a] -> a
// sumList xs = sumWithAccumulator xs 0
// The base case is the array with 1 element
inline fn sum_array_1(reg ptr u64[1] a, reg u64 acc) -> reg u64 {
reg u64 t;
t = a[0];
acc = acc;
acc += t;
acc = acc;
return acc;
}
inline fn sum_array<INLEN>(reg ptr u64[INLEN] a, reg u64 acc) -> reg u64 {
reg u64 sum_tail;
reg ptr u64[INLEN - 1] tail;
sum_tail = sum_tail; acc = acc;
tail = a[1 : INLEN - 1];
sum_tail = sum_array<INLEN - 1>(tail, acc);
sum_tail = sum_tail; acc = acc;
acc += sum_tail;
return acc;
}//<>
export fn sum_rec(reg usize _a) -> reg u64 {
inline int i;
reg u64 t acc sum;
stack u64[10] a;
reg ptr u64[10] ap;
for i=0 to 10 { t = (u64) [_a + 8*i]; a[i] = t; }
?{}, acc = #set0();
ap = a;
sum = sum_array<10>(ap, acc);
return sum;
}