-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatmul4_loop.mapal
More file actions
51 lines (48 loc) · 1.49 KB
/
Copy pathmatmul4_loop.mapal
File metadata and controls
51 lines (48 loc) · 1.49 KB
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
// matmul4 — the ADR-0021 motivating program: 4×4 matrix multiply as ONE flattened
// loop, building the result cell-by-cell via `c[t] <- v` (array element update).
// The `cell` fn's inner k-loop computes one dot product; the two loops live in
// two fns (the canonical-loop rule is per-fn). Oracle contract for the generated
// arrays below: prints -275 then 3748.
// Perf-scale versions (N=16..128) + cross-language harness: benches/matmul/.
fn cell(a: [f32; 16], b: [f32; 16], i: i32, j: i32) -> f32 {
mut k: i32 <- 0;
mut acc: f32 <- 0.0;
loop {
(k < 4) -> {
-true-> {
acc + a[i * 4 + k] * b[k * 4 + j] -> acc;
k + 1 -> k;
-> loop;
}
-false-> acc -> ret;
}
}
}
fn matmul(a: [f32; 16], b: [f32; 16]) -> [f32; 16] {
mut c: [f32; 16] <- b;
mut t: i32 <- 0;
loop {
(t < 16) -> {
-true-> {
t / 4 -> i;
t % 4 -> j;
(a, b, i, j) -> cell -> v;
c[t] <- v;
t + 1 -> t;
-> loop;
}
-false-> c -> ret;
}
}
}
fn main() {
[
-37.0, -30.0, -23.0, -16.0, -9.0, -2.0, 5.0, 12.0,
19.0, 26.0, 33.0, 40.0, 47.0, -47.0, -40.0, -33.0] -> a;
[
7.0, 14.0, 21.0, 28.0, 35.0, 42.0, 49.0, -45.0,
-38.0, -31.0, -24.0, -17.0, -10.0, -3.0, 4.0, 11.0] -> b;
(a, b) -> matmul -> c;
c[0] -> println;
c[15] -> println;
}