Skip to content

Instantly share code, notes, and snippets.

@jiayihu
Last active April 11, 2021 12:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jiayihu/cbb64ba1284fcda7e41c5106e133e242 to your computer and use it in GitHub Desktop.
Save jiayihu/cbb64ba1284fcda7e41c5106e133e242 to your computer and use it in GitHub Desktop.
#![allow(non_snake_case)]
fn init_array<const M: usize, const N: usize>(
A: &mut [[f32; N]; M],
r: &mut [f32; M],
p: &mut [f32; N],
) {
for i in 0..N {
p[i] = (i % N) as f32 / N as f32;
}
for i in 0..M {
r[i] = (i % M) as f32 / M as f32;
for j in 0..N {
A[i][j] = (i * (j + 1) % M) as f32 / M as f32;
}
}
}
fn kernel_bicg<const M: usize, const N: usize>(
A: &[[f32; N]; M],
s: &mut [f32; N],
q: &mut [f32; M],
p: &[f32; N],
r: &[f32; M],
) {
for i in 0..N {
s[i] = 0.0;
}
for i in 0..M {
q[i] = 0.0;
for j in 0..N {
s[j] = s[j] + r[i] * A[i][j];
q[i] = q[i] + A[i][j] * p[j];
}
}
}
#[no_mangle]
pub extern "C" fn bench() {
const M: usize = 1;
const N: usize = 1;
let mut A = [[0_f32; N]; M];
let mut s = [0_f32; N];
let mut q = [0_f32; M];
let mut p = [0_f32; N];
let mut r = [0_f32; M];
init_array(&mut A, &mut r, &mut p);
kernel_bicg(&A, &mut s, &mut q, &p, &r);
}
(module
(type $t0 (func))
(func $bench (type $t0)
(local $l0 i32) (local $l1 i32) (local $l2 i32) (local $l3 f32)
global.get $g0
i32.const 32
i32.sub
local.set $l0
i32.const 0
local.set $l1
block $B0
loop $L1
local.get $l1
i32.const 4
i32.eq
br_if $B0
local.get $l0
i32.const 28
i32.add
local.get $l1
i32.add
i32.const 0
i32.store
local.get $l1
i32.const 4
i32.add
local.set $l1
br $L1
end
end
i32.const 0
local.set $l1
local.get $l0
i32.load offset=28
local.set $l2
block $B2
loop $L3
local.get $l1
i32.const 4
i32.eq
br_if $B2
local.get $l0
i32.const 12
i32.add
local.get $l1
i32.add
local.get $l2
i32.store
local.get $l1
i32.const 4
i32.add
local.set $l1
br $L3
end
end
i32.const 0
local.set $l1
block $B4
loop $L5
local.get $l1
i32.const 4
i32.eq
br_if $B4
local.get $l0
i32.const 16
i32.add
local.get $l1
i32.add
i32.const 0
i32.store
local.get $l1
i32.const 4
i32.add
local.set $l1
br $L5
end
end
i32.const 0
local.set $l1
block $B6
loop $L7
local.get $l1
i32.const 4
i32.eq
br_if $B6
local.get $l0
i32.const 20
i32.add
local.get $l1
i32.add
i32.const 0
i32.store
local.get $l1
i32.const 4
i32.add
local.set $l1
br $L7
end
end
i32.const 0
local.set $l1
block $B8
loop $L9
local.get $l1
i32.const 4
i32.eq
br_if $B8
local.get $l0
i32.const 24
i32.add
local.get $l1
i32.add
i32.const 0
i32.store
local.get $l1
i32.const 4
i32.add
local.set $l1
br $L9
end
end
i32.const 0
local.set $l1
block $B10
loop $L11
local.get $l1
i32.const 4
i32.eq
br_if $B10
local.get $l0
i32.const 28
i32.add
local.get $l1
i32.add
i32.const 0
i32.store
local.get $l1
i32.const 4
i32.add
local.set $l1
br $L11
end
end
local.get $l0
i32.const 0
i32.store offset=28
local.get $l0
i32.const 0
i32.store offset=24
local.get $l0
i32.const 0
i32.store offset=12
local.get $l0
i32.const 0
i32.store offset=16
f32.const 0x0p+0 (;=0;)
local.set $l3
i32.const 1
local.set $l1
block $B12
loop $L13
local.get $l1
i32.const 1
i32.and
i32.eqz
br_if $B12
local.get $l0
f32.const 0x0p+0 (;=0;)
local.get $l3
f32.add
local.tee $l3
f32.store offset=20
local.get $l0
f32.const 0x0p+0 (;=0;)
local.get $l0
f32.load offset=16
f32.add
f32.store offset=16
i32.const 0
local.set $l1
br $L13
end
end)
(memory $memory 1)
(global $g0 (mut i32) (i32.const 16))
(global $__data_end i32 (i32.const 16))
(global $__heap_base i32 (i32.const 16))
(export "memory" (memory 0))
(export "bench" (func $bench))
(export "__data_end" (global 1))
(export "__heap_base" (global 2)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment