-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvec_add_int_list.ex
49 lines (43 loc) · 1.47 KB
/
vec_add_int_list.ex
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
defmodule AddTwoIntVec do
@moduledoc false
use Charms
alias Charms.{SIMD, Term, Pointer}
defm load_list(env, l :: Term.t()) :: SIMD.t(i32(), 8) do
i_ptr = Pointer.allocate(i32())
zero = const 0 :: Pointer.element_type(i_ptr)
Pointer.store(zero, i_ptr)
init = SIMD.new(SIMD.t(i32(), 8), [0, 0, 0, 0, 0, 0, 0, 0])
Enum.reduce(l, init, fn x, acc ->
v_ptr = Pointer.allocate(i32())
enif_get_int(env, x, v_ptr)
i = Pointer.load(i_ptr)
Pointer.store(i + 1, i_ptr)
Pointer.load(v_ptr) |> vector.insertelement(acc, i)
end)
end
defm add(env, a, b, error) :: Term.t() do
v1 = load_list(env, a)
v2 = load_list(env, b)
v = arith.addi(v1, v2)
start = const 0 :: i32()
enif_make_list8(
env,
enif_make_int(env, vector.extractelement(v, start)),
enif_make_int(env, vector.extractelement(v, start + 1)),
enif_make_int(env, vector.extractelement(v, start + 2)),
enif_make_int(env, vector.extractelement(v, start + 3)),
enif_make_int(env, vector.extractelement(v, start + 4)),
enif_make_int(env, vector.extractelement(v, start + 5)),
enif_make_int(env, vector.extractelement(v, start + 6)),
enif_make_int(env, vector.extractelement(v, start + 7))
)
end
defm dummy_load_no_make(env, a, b, error) :: Term.t() do
v1 = load_list(env, a)
v2 = load_list(env, b)
func.return(a)
end
defm dummy_return(env, a, b, error) :: Term.t() do
func.return(a)
end
end