-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsimd.t
81 lines (70 loc) · 1.98 KB
/
simd.t
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
-- SPDX-FileCopyrightText: 2024 René Hiemstra <[email protected]>
-- SPDX-FileCopyrightText: 2024 Torsten Keßler <[email protected]>
--
-- SPDX-License-Identifier: MIT
local VectorFactory = terralib.memoize(function(T, N)
local SIMD = vector(T, N)
local struct vec {
data: SIMD
}
terra vec:load(data: &T)
escape
local arg = terralib.newlist()
for i = 0, SIMD.N - 1 do
arg:insert(`data[i])
end
emit quote self.data = vectorof(T, [arg]) end
end
end
vec.methods.load:setinlined(true)
terra vec:store(data: &T)
escape
for i = 0, SIMD.N - 1 do
emit quote data[i] = self.data[i] end
end
end
end
vec.methods.store:setinlined(true)
terra vec:hsum()
var res = [T](0)
escape
for j = 0, N - 1 do
emit quote res = res + self.data[j] end
end
end
return res
end
vec.methods.hsum:setinlined(true)
function vec.metamethods.__cast(from, to, exp)
if from == T or from == SIMD then
return `vec {[exp]}
elseif from == &T then
return quote
var v: vec
v:load([exp])
in
v
end
end
error("Cannot convert non vector type to vector")
end
terra vec.metamethods.__add(self: vec, other: vec)
return vec {self.data + other.data}
end
terra vec.metamethods.__sub(self: vec, other: vec)
return vec {self.data - other.data}
end
terra vec.metamethods.__ne(self: vec)
return vec {-self.data}
end
terra vec.metamethods.__mul(self: vec, other: vec)
return vec {self.data * other.data}
end
terra vec.metamethods.__div(self: vec, other: vec)
return vec {self.data / other.data}
end
return vec
end)
return {
VectorFactory = VectorFactory,
}