1
2
3
4
5
6
7 package simd_test
8
9 import (
10 "fmt"
11 "math"
12 "simd/archsimd"
13 "testing"
14 "unsafe"
15 )
16
17
18
19 func sameBits[T any](a, b T) bool {
20 n := unsafe.Sizeof(a)
21 return string(unsafe.Slice((*byte)(unsafe.Pointer(&a)), n)) == string(unsafe.Slice((*byte)(unsafe.Pointer(&b)), n))
22 }
23
24
25
26
27
28 func testSVEElem[T any, V interface{ Len() int }](t *testing.T, name string, pool []T, load func([]T) V, get func(V, uint8) T) {
29 t.Helper()
30 var s [sveMaxBytes]T
31 var x V
32 for start := range pool {
33 for k := range s {
34 s[k] = pool[(start+k)%len(pool)]
35 }
36 x = load(s[:])
37 for i := range x.Len() {
38 if got := get(x, uint8(i)); !sameBits(got, s[i]) {
39 t.Errorf("%s(%v, %d) = %v, want %v", name, s[:x.Len()], i, got, s[i])
40 return
41 }
42 }
43 }
44 for _, i := range []int{x.Len(), x.Len() + 1, 255} {
45 mustPanic(t, fmt.Sprintf("%s(x, %d)", name, i), func() { get(x, uint8(i)) })
46 }
47 }
48
49 func TestElemSVE(t *testing.T) {
50 if !archsimd.ARM64.SVE() {
51 t.Skip("no SVE")
52 }
53 testSVEElem(t, "Int8s.Elem", append(int8s, math.MinInt8, math.MaxInt8), archsimd.LoadInt8s, archsimd.Int8s.Elem)
54 testSVEElem(t, "Uint8s.Elem", append(uint8s, math.MaxUint8), archsimd.LoadUint8s, archsimd.Uint8s.Elem)
55 testSVEElem(t, "Int16s.Elem", append(int16s, math.MinInt16, math.MaxInt16), archsimd.LoadInt16s, archsimd.Int16s.Elem)
56 testSVEElem(t, "Uint16s.Elem", append(uint16s, math.MaxUint16), archsimd.LoadUint16s, archsimd.Uint16s.Elem)
57 testSVEElem(t, "Int32s.Elem", append(int32s, math.MinInt32, math.MaxInt32), archsimd.LoadInt32s, archsimd.Int32s.Elem)
58 testSVEElem(t, "Uint32s.Elem", append(uint32s, math.MaxUint32), archsimd.LoadUint32s, archsimd.Uint32s.Elem)
59 testSVEElem(t, "Float32s.Elem", float32s, archsimd.LoadFloat32s, archsimd.Float32s.Elem)
60 testSVEElem(t, "Int64s.Elem", append(int64s, math.MinInt64, math.MaxInt64), archsimd.LoadInt64s, archsimd.Int64s.Elem)
61 testSVEElem(t, "Uint64s.Elem", append(uint64s, math.MaxUint64), archsimd.LoadUint64s, archsimd.Uint64s.Elem)
62 testSVEElem(t, "Float64s.Elem", float64s, archsimd.LoadFloat64s, archsimd.Float64s.Elem)
63 }
64
65
66
67
68 func TestElemConstSVE(t *testing.T) {
69 if !archsimd.ARM64.SVE() {
70 t.Skip("no SVE")
71 }
72 var s [sveMaxBytes]int8
73 for k := range s {
74 s[k] = int8(-1 - k)
75 }
76 x := archsimd.LoadInt8s(s[:])
77 if got := int64(x.Elem(0)); got != -1 {
78 t.Errorf("Int8s.Elem(0) = %d, want -1", got)
79 }
80 if got := int64(x.Elem(15)); got != -16 {
81 t.Errorf("Int8s.Elem(15) = %d, want -16", got)
82 }
83
84 if x.Len() > 31 {
85 if got := int64(x.Elem(31)); got != -32 {
86 t.Errorf("Int8s.Elem(31) = %d, want -32", got)
87 }
88 } else {
89 mustPanic(t, "Int8s.Elem(31)", func() { x.Elem(31) })
90 }
91 var s16 [sveMaxBytes]int16
92 s16[7] = math.MinInt16
93 if got := int64(archsimd.LoadInt16s(s16[:]).Elem(7)); got != math.MinInt16 {
94 t.Errorf("Int16s.Elem(7) = %d, want %d", got, math.MinInt16)
95 }
96 var s32 [sveMaxBytes]int32
97 s32[3] = -5
98 if got := int64(archsimd.LoadInt32s(s32[:]).Elem(3)); got != -5 {
99 t.Errorf("Int32s.Elem(3) = %d, want -5", got)
100 }
101 }
102
103
104
105
106
107 func testSVESetElem[T any, V interface{ Len() int }](t *testing.T, name string, pool []T, load func([]T) V, store func(V, []T), set func(V, uint8, T) V) {
108 t.Helper()
109 var s, got [sveMaxBytes]T
110 var x V
111 for start := range pool {
112 for k := range s {
113 s[k] = pool[(start+k)%len(pool)]
114 }
115 x = load(s[:])
116 y := pool[(start+len(s))%len(pool)]
117 for i := range x.Len() {
118 store(set(x, uint8(i), y), got[:])
119 for k := range x.Len() {
120 want := s[k]
121 if k == i {
122 want = y
123 }
124 if !sameBits(got[k], want) {
125 t.Errorf("%s(%v, %d, %v)[%d] = %v, want %v", name, s[:x.Len()], i, y, k, got[k], want)
126 return
127 }
128 }
129 }
130 }
131 for _, i := range []int{x.Len(), x.Len() + 1, 255} {
132 mustPanic(t, fmt.Sprintf("%s(x, %d, y)", name, i), func() { set(x, uint8(i), pool[0]) })
133 }
134 }
135
136 func TestSetElemSVE(t *testing.T) {
137 if !archsimd.ARM64.SVE() {
138 t.Skip("no SVE")
139 }
140 testSVESetElem(t, "Int8s.SetElem", append(int8s, math.MinInt8, math.MaxInt8), archsimd.LoadInt8s, archsimd.Int8s.Store, archsimd.Int8s.SetElem)
141 testSVESetElem(t, "Uint8s.SetElem", append(uint8s, math.MaxUint8), archsimd.LoadUint8s, archsimd.Uint8s.Store, archsimd.Uint8s.SetElem)
142 testSVESetElem(t, "Int16s.SetElem", append(int16s, math.MinInt16, math.MaxInt16), archsimd.LoadInt16s, archsimd.Int16s.Store, archsimd.Int16s.SetElem)
143 testSVESetElem(t, "Uint16s.SetElem", append(uint16s, math.MaxUint16), archsimd.LoadUint16s, archsimd.Uint16s.Store, archsimd.Uint16s.SetElem)
144 testSVESetElem(t, "Int32s.SetElem", append(int32s, math.MinInt32, math.MaxInt32), archsimd.LoadInt32s, archsimd.Int32s.Store, archsimd.Int32s.SetElem)
145 testSVESetElem(t, "Uint32s.SetElem", append(uint32s, math.MaxUint32), archsimd.LoadUint32s, archsimd.Uint32s.Store, archsimd.Uint32s.SetElem)
146 testSVESetElem(t, "Float32s.SetElem", float32s, archsimd.LoadFloat32s, archsimd.Float32s.Store, archsimd.Float32s.SetElem)
147 testSVESetElem(t, "Int64s.SetElem", append(int64s, math.MinInt64, math.MaxInt64), archsimd.LoadInt64s, archsimd.Int64s.Store, archsimd.Int64s.SetElem)
148 testSVESetElem(t, "Uint64s.SetElem", append(uint64s, math.MaxUint64), archsimd.LoadUint64s, archsimd.Uint64s.Store, archsimd.Uint64s.SetElem)
149 testSVESetElem(t, "Float64s.SetElem", float64s, archsimd.LoadFloat64s, archsimd.Float64s.Store, archsimd.Float64s.SetElem)
150 }
151
152
153
154 func TestSetElemConstSVE(t *testing.T) {
155 if !archsimd.ARM64.SVE() {
156 t.Skip("no SVE")
157 }
158 var s, got [sveMaxBytes]int16
159 for k := range s {
160 s[k] = int16(k + 1)
161 }
162 x := archsimd.LoadInt16s(s[:])
163 y := x.SetElem(0, -5).SetElem(7, 100)
164 y.Store(got[:])
165 x.Store(s[:])
166 for k := range x.Len() {
167 want := int16(k + 1)
168 if s[k] != want {
169 t.Errorf("x[%d] = %d after SetElem, want %d", k, s[k], want)
170 }
171 switch k {
172 case 0:
173 want = -5
174 case 7:
175 want = 100
176 }
177 if got[k] != want {
178 t.Errorf("x.SetElem(0, -5).SetElem(7, 100)[%d] = %d, want %d", k, got[k], want)
179 }
180 }
181
182 if x.Len() > 15 {
183 x.SetElem(15, 9).Store(got[:])
184 if got[15] != 9 {
185 t.Errorf("Int16s.SetElem(15, 9)[15] = %d, want 9", got[15])
186 }
187 } else {
188 mustPanic(t, "Int16s.SetElem(15, 9)", func() { x.SetElem(15, 9) })
189 }
190 }
191
View as plain text