Debian Codegraph @

swar/swar_test.go

raw symbols SCIP
1 package swarscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/swar`/
2
3 import (
4 "encoding/binaryscip-go gomod github.com/golang/go/src go1.25.0 `encoding/binary`/"
5 "math/randscip-go gomod github.com/golang/go/src go1.25.0 `math/rand`/"
6 "testingscip-go gomod github.com/golang/go/src go1.25.0 testing/"
7
8 "github.com/stretchr/testify/requirescip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/require`/"
9 )
10
11 // wordFromLanes builds a word with the given 8 lane values, lane 0 first.
12 func wordFromLanesscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/swar`/wordFromLanes().(lanes [8]byte) uint64 {
13 return binaryscip-go gomod github.com/golang/go/src go1.25.0 `encoding/binary`/.LittleEndianscip-go gomod github.com/golang/go/src go1.25.0 `encoding/binary`/LittleEndian..Uint64scip-go gomod github.com/golang/go/src go1.25.0 `encoding/binary`/littleEndian#Uint64().(lanes[:])
14 }
15
16 func Test_Load8_LaneOrderscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/swar`/Test_Load8_LaneOrder().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) {
17 t.Parallelscip-go gomod github.com/golang/go/src go1.25.0 testing/T#Parallel().()
18 b := []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
19 want := uint64(0x0807060504030201)
20 requirescip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/require`/.Equalscip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/require`/Equal().(t, want, Load8scip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/swar`/Load8().(b, 0))
21 requirescip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/require`/.Equalscip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/require`/Equal().(t, want, Load8scip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/swar`/Load8().(string(b), 0))
22 requirescip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/require`/.Equalscip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/require`/Equal().(t, uint64(0x0908070605040302), Load8scip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/swar`/Load8().(b, 1))
23 requirescip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/require`/.Equalscip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/require`/Equal().(t, uint64(0x0A09080706050403), Load8scip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/swar`/Load8().(string(b), 2))
24 }
25
26 func Test_Store8_LaneOrder_RoundTripscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/swar`/Test_Store8_LaneOrder_RoundTrip().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) {
27 t.Parallelscip-go gomod github.com/golang/go/src go1.25.0 testing/T#Parallel().()
28 b := make([]byte, 12)
29 Store8scip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/swar`/Store8().(b, 2, 0x0807060504030201)
30 requirescip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/require`/.Equalscip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/require`/Equal().(t, []byte{0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 0, 0}, b)
31 // Load8/Store8 round trip at every offset, leaving neighbors untouched.
32 src := []byte{0xFF, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xAA, 0xBB}
33 for i := 0; i+8 <= len(src); i++ {
34 dst := make([]byte, len(src))
35 copy(dst, src)
36 Store8scip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/swar`/Store8().(dst, i, Load8scip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/swar`/Load8().(src, i))
37 requirescip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/require`/.Equalscip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/require`/Equal().(t, src, dst, "offset %d", i)
38 }
39 }
40
41 func Test_Broadcastscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/swar`/Test_Broadcast().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) {
42 t.Parallelscip-go gomod github.com/golang/go/src go1.25.0 testing/T#Parallel().()
43 for _, c := range []byte{0x00, 0x01, '0', 'a', 0x7F, 0x80, 0xFF} {
44 want := wordFromLanesscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/swar`/wordFromLanes().([8]byte{c, c, c, c, c, c, c, c})
45 requirescip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/require`/.Equalscip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/require`/Equal().(t, want, Broadcastscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/swar`/Broadcast().(c), "byte 0x%02x", c)
46 }
47 }
48
49 func Test_ToLowerWord_ToUpperWord_Exhaustivescip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/swar`/Test_ToLowerWord_ToUpperWord_Exhaustive().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) {
50 t.Parallelscip-go gomod github.com/golang/go/src go1.25.0 testing/T#Parallel().()
51 // Every byte value in every lane position, once with all other lanes
52 // zero and once with all other lanes 0xFF, so lane isolation is proven.
53 for c := range 256 {
54 wantLower := byte(c)
55 if wantLower >= 'A' && wantLower <= 'Z' {
56 wantLower += 'a' - 'A'
57 }
58 wantUpper := byte(c)
59 if wantUpper >= 'a' && wantUpper <= 'z' {
60 wantUpper -= 'a' - 'A'
61 }
62 for lane := range 8 {
63 for _, fill := range []byte{0x00, 0xFF, 'm', 'M'} {
64 var lanes [8]byte
65 for i := range lanes {
66 lanes[i] = fill
67 }
68 lanes[lane] = byte(c)
69 w := wordFromLanesscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/swar`/wordFromLanes().(lanes)
70
71 gotLower := ToLowerWordscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/swar`/ToLowerWord().(w)
72 gotUpper := ToUpperWordscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/swar`/ToUpperWord().(w)
73 requirescip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/require`/.Equalscip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/require`/Equal().(t, wantLower, byte(gotLower>>(8*lane)),
74 "ToLowerWord lane %d byte 0x%02x fill 0x%02x", lane, c, fill)
75 requirescip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/require`/.Equalscip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/require`/Equal().(t, wantUpper, byte(gotUpper>>(8*lane)),
76 "ToUpperWord lane %d byte 0x%02x fill 0x%02x", lane, c, fill)
77 }
78 }
79 }
80 }
81
82 func Test_MatchByteMask_Exhaustivescip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/swar`/Test_MatchByteMask_Exhaustive().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) {
83 t.Parallelscip-go gomod github.com/golang/go/src go1.25.0 testing/T#Parallel().()
84 // For every needle value and every lane value, the mask must be exact:
85 // 0x80 iff equal, 0 otherwise — including needle 0x00, needle >= 0x80,
86 // and adjacent-lane interference (the borrow-corruption trap).
87 // Plain checks keep the ~1M words fast, as in the range test below.
88 for needle := range 256 {
89 for c := range 256 {
90 for lane := range 8 {
91 // Fill other lanes with the needle itself (worst case for
92 // cross-lane borrows) and with needle^1 (no other matches).
93 for _, fill := range []byte{byte(needle), byte(needle) ^ 1} {
94 var lanes [8]byte
95 for i := range lanes {
96 lanes[i] = fill
97 }
98 lanes[lane] = byte(c)
99 m := MatchByteMaskscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/swar`/MatchByteMask().(wordFromLanesscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/swar`/wordFromLanes().(lanes), byte(needle))
100 for i := range lanes {
101 want := byte(0)
102 if lanes[i] == byte(needle) {
103 want = 0x80
104 }
105 if got := byte(m >> (8 * i)); got != want {
106 t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("needle 0x%02x lanes %v lane %d: got 0x%02x want 0x%02x",
107 needle, lanes, i, got, want)
108 }
109 }
110 }
111 }
112 }
113 }
114 }
115
116 func Test_MatchRangeMask_Exhaustivescip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/swar`/Test_MatchRangeMask_Exhaustive().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) {
117 t.Parallelscip-go gomod github.com/golang/go/src go1.25.0 testing/T#Parallel().()
118 // All valid (lo, hi) pairs, with every byte value rotating through the
119 // lanes. The neighbor lanes hold the range boundaries and both extremes
120 // rather than a constant fill: the carry-freedom claim is exactly about
121 // adjacent-lane interaction. Plain checks keep the ~2M words fast.
122 for lo := 0; lo <= 0x7F; lo++ {
123 for hi := lo; hi <= 0x7F; hi++ {
124 base := [8]byte{0x00, byte(lo), byte(hi), byte(lo) - 1, byte(hi) + 1, 0x7F, 0x80, 0xFF}
125 for c := range 256 {
126 lanes := base
127 lanes[c&7] = byte(c)
128 m := MatchRangeMaskscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/swar`/MatchRangeMask().(wordFromLanesscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/swar`/wordFromLanes().(lanes), byte(lo), byte(hi))
129 for i, v := range lanes {
130 want := uint64(0)
131 if v >= byte(lo) && v <= byte(hi) && v < 0x80 {
132 want = 0x80
133 }
134 if got := m >> (8 * i) & 0xFF; got != want {
135 t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("range [0x%02x,0x%02x] lanes %v lane %d: got 0x%02x want 0x%02x",
136 lo, hi, lanes, i, got, want)
137 }
138 }
139 }
140 }
141 }
142
143 // Random valid ranges over fully random lanes, as a second angle on
144 // mixed neighbor values.
145 rng := randscip-go gomod github.com/golang/go/src go1.25.0 `math/rand`/.Newscip-go gomod github.com/golang/go/src go1.25.0 `math/rand`/New().(randscip-go gomod github.com/golang/go/src go1.25.0 `math/rand`/.NewSourcescip-go gomod github.com/golang/go/src go1.25.0 `math/rand`/NewSource().(11)) //nolint:gosec // deterministic test data
146 for range 200000 {
147 lo := byte(rng.Intnscip-go gomod github.com/golang/go/src go1.25.0 `math/rand`/Rand#Intn().(0x80))
148 hi := lo + byte(rng.Intnscip-go gomod github.com/golang/go/src go1.25.0 `math/rand`/Rand#Intn().(0x80-int(lo)))
149 var lanes [8]byte
150 for i := range lanes {
151 lanes[i] = byte(rng.Intnscip-go gomod github.com/golang/go/src go1.25.0 `math/rand`/Rand#Intn().(256))
152 }
153 m := MatchRangeMaskscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/swar`/MatchRangeMask().(wordFromLanesscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/swar`/wordFromLanes().(lanes), lo, hi)
154 for i, v := range lanes {
155 want := uint64(0)
156 if v >= lo && v <= hi && v < 0x80 {
157 want = 0x80
158 }
159 if got := m >> (8 * i) & 0xFF; got != want {
160 t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("range [0x%02x,0x%02x] lanes %v lane %d: got 0x%02x want 0x%02x",
161 lo, hi, lanes, i, got, want)
162 }
163 }
164 }
165 }
166
167 func Test_FirstLane_LastLanescip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/swar`/Test_FirstLane_LastLane().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) {
168 t.Parallelscip-go gomod github.com/golang/go/src go1.25.0 testing/T#Parallel().()
169 requirescip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/require`/.Equalscip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/require`/Equal().(t, 8, FirstLanescip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/swar`/FirstLane().(0))
170 requirescip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/require`/.Equalscip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/require`/Equal().(t, -1, LastLanescip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/swar`/LastLane().(0))
171 for first := range 8 {
172 for last := first; last < 8; last++ {
173 mask := uint64(0x80)<<(8*first) | uint64(0x80)<<(8*last)
174 requirescip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/require`/.Equalscip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/require`/Equal().(t, first, FirstLanescip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/swar`/FirstLane().(mask))
175 requirescip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/require`/.Equalscip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/require`/Equal().(t, last, LastLanescip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/swar`/LastLane().(mask))
176 }
177 }
178 }
179
180 // Benchmark_Load8_Fusion guards the load-combining requirement: an unfused
181 // generic instantiation shows a ~4-8x slower word loop. If this benchmark
182 // ever collapses to per-byte speed, the reslice idiom in Load8 has stopped
183 // fusing and Load8 needs concrete string/[]byte helpers behind the generic
184 // front. The guard is advisory only, it cannot fail CI; eyeball it (or
185 // benchstat it against the previous toolchain) when touching Load8/Store8
186 // or bumping the Go version. The store leg covers Store8's write fusion.
187 func Benchmark_Load8_Fusionscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/swar`/Benchmark_Load8_Fusion().(b *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Bscip-go gomod github.com/golang/go/src go1.25.0 testing/B#) {
188 buf := make([]byte, 4096)
189 for i := range buf {
190 buf[i] = byte(i)
191 }
192 s := string(buf)
193 b.Runscip-go gomod github.com/golang/go/src go1.25.0 testing/B#Run().("bytes", func(b *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Bscip-go gomod github.com/golang/go/src go1.25.0 testing/B#) {
194 b.SetBytesscip-go gomod github.com/golang/go/src go1.25.0 testing/B#SetBytes().(int64(len(buf)))
195 var acc uint64
196 for b.Loopscip-go gomod github.com/golang/go/src go1.25.0 testing/B#Loop().() {
197 for i := 0; i+8 <= len(buf); i += 8 {
198 acc += Load8scip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/swar`/Load8().(buf, i)
199 }
200 }
201 _ = acc
202 })
203 b.Runscip-go gomod github.com/golang/go/src go1.25.0 testing/B#Run().("string", func(b *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Bscip-go gomod github.com/golang/go/src go1.25.0 testing/B#) {
204 b.SetBytesscip-go gomod github.com/golang/go/src go1.25.0 testing/B#SetBytes().(int64(len(s)))
205 var acc uint64
206 for b.Loopscip-go gomod github.com/golang/go/src go1.25.0 testing/B#Loop().() {
207 for i := 0; i+8 <= len(s); i += 8 {
208 acc += Load8scip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/swar`/Load8().(s, i)
209 }
210 }
211 _ = acc
212 })
213 b.Runscip-go gomod github.com/golang/go/src go1.25.0 testing/B#Run().("store", func(b *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Bscip-go gomod github.com/golang/go/src go1.25.0 testing/B#) {
214 b.SetBytesscip-go gomod github.com/golang/go/src go1.25.0 testing/B#SetBytes().(int64(len(buf)))
215 w := Broadcastscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/swar`/Broadcast().('x')
216 for b.Loopscip-go gomod github.com/golang/go/src go1.25.0 testing/B#Loop().() {
217 for i := 0; i+8 <= len(buf); i += 8 {
218 Store8scip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/swar`/Store8().(buf, i, w)
219 }
220 }
221 })
222 }
223
224 func Test_ZeroLanes_FirstMatchPropertyscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/swar`/Test_ZeroLanes_FirstMatchProperty().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) {
225 t.Parallelscip-go gomod github.com/golang/go/src go1.25.0 testing/T#Parallel().()
226 // For every word shape: the mask is zero iff no lane is zero, and the
227 // lowest set lane is always a true zero lane (false positives may only
228 // appear above it).
229 for zeroAt := range 8 {
230 for fill := range 256 {
231 var lanes [8]byte
232 for i := range lanes {
233 lanes[i] = byte(fill)
234 }
235 lanes[zeroAt] = 0
236 m := ZeroLanesscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/swar`/ZeroLanes().(wordFromLanesscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/swar`/wordFromLanes().(lanes))
237 requirescip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/require`/.NotZeroscip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/require`/NotZero().(t, m, "fill 0x%02x zero at %d", fill, zeroAt)
238 lo := FirstLanescip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/swar`/FirstLane().(m)
239 requirescip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/require`/.Zeroscip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/require`/Zero().(t, lanes[lo], "fill 0x%02x zero at %d: first lane %d", fill, zeroAt, lo)
240 if byte(fill) != 0 {
241 requirescip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/require`/.Equalscip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/require`/Equal().(t, zeroAt, lo)
242 }
243 }
244 }
245 for fill := 1; fill < 256; fill++ {
246 var lanes [8]byte
247 for i := range lanes {
248 lanes[i] = byte(fill)
249 }
250 requirescip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/require`/.Zeroscip-go gomod github.com/stretchr/testify v1.12.1 `github.com/stretchr/testify/require`/Zero().(t, ZeroLanesscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/swar`/ZeroLanes().(wordFromLanesscip-go gomod github.com/gofiber/utils/v2 . `github.com/gofiber/utils/v2/swar`/wordFromLanes().(lanes)), "fill 0x%02x", fill)
251 }
252 }