| 1 | package swar |
| 2 | |
| 3 | import ( |
| 4 | "encoding/binary" |
| 5 | "math/rand" |
| 6 | "testing" |
| 7 | |
| 8 | "github.com/stretchr/testify/require" |
| 9 | ) |
| 10 | |
| 11 | // wordFromLanes builds a word with the given 8 lane values, lane 0 first. |
| 12 | func wordFromLanes(lanes [8]byte) uint64 { |
| 13 | return binary.LittleEndian.Uint64(lanes[:]) |
| 14 | } |
| 15 | |
| 16 | func Test_Load8_LaneOrder(t *testing.T) { |
| 17 | t.Parallel() |
| 18 | b := []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10} |
| 19 | want := uint64(0x0807060504030201) |
| 20 | require.Equal(t, want, Load8(b, 0)) |
| 21 | require.Equal(t, want, Load8(string(b), 0)) |
| 22 | require.Equal(t, uint64(0x0908070605040302), Load8(b, 1)) |
| 23 | require.Equal(t, uint64(0x0A09080706050403), Load8(string(b), 2)) |
| 24 | } |
| 25 | |
| 26 | func Test_Store8_LaneOrder_RoundTrip(t *testing.T) { |
| 27 | t.Parallel() |
| 28 | b := make([]byte, 12) |
| 29 | Store8(b, 2, 0x0807060504030201) |
| 30 | 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 | Store8(dst, i, Load8(src, i)) |
| 37 | require.Equal(t, src, dst, "offset %d", i) |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | func Test_Broadcast(t *testing.T) { |
| 42 | t.Parallel() |
| 43 | for _, c := range []byte{0x00, 0x01, '0', 'a', 0x7F, 0x80, 0xFF} { |
| 44 | want := wordFromLanes([8]byte{c, c, c, c, c, c, c, c}) |
| 45 | require.Equal(t, want, Broadcast(c), "byte 0x%02x", c) |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | func Test_ToLowerWord_ToUpperWord_Exhaustive(t *testing.T) { |
| 50 | 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 := wordFromLanes(lanes) |
| 70 | |
| 71 | gotLower := ToLowerWord(w) |
| 72 | gotUpper := ToUpperWord(w) |
| 73 | require.Equal(t, wantLower, byte(gotLower>>(8*lane)), |
| 74 | "ToLowerWord lane %d byte 0x%02x fill 0x%02x", lane, c, fill) |
| 75 | 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_Exhaustive(t *testing.T) { |
| 83 | 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 := MatchByteMask(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.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_Exhaustive(t *testing.T) { |
| 117 | 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 := MatchRangeMask(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.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 := rand.New(rand.NewSource(11)) //nolint:gosec // deterministic test data |
| 146 | for range 200000 { |
| 147 | lo := byte(rng.Intn(0x80)) |
| 148 | hi := lo + byte(rng.Intn(0x80-int(lo))) |
| 149 | var lanes [8]byte |
| 150 | for i := range lanes { |
| 151 | lanes[i] = byte(rng.Intn(256)) |
| 152 | } |
| 153 | m := MatchRangeMask(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.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_LastLane(t *testing.T) { |
| 168 | t.Parallel() |
| 169 | require.Equal(t, 8, FirstLane(0)) |
| 170 | require.Equal(t, -1, 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 | require.Equal(t, first, FirstLane(mask)) |
| 175 | require.Equal(t, last, 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_Fusion(b *testing.B) { |
| 188 | buf := make([]byte, 4096) |
| 189 | for i := range buf { |
| 190 | buf[i] = byte(i) |
| 191 | } |
| 192 | s := string(buf) |
| 193 | b.Run("bytes", func(b *testing.B) { |
| 194 | b.SetBytes(int64(len(buf))) |
| 195 | var acc uint64 |
| 196 | for b.Loop() { |
| 197 | for i := 0; i+8 <= len(buf); i += 8 { |
| 198 | acc += Load8(buf, i) |
| 199 | } |
| 200 | } |
| 201 | _ = acc |
| 202 | }) |
| 203 | b.Run("string", func(b *testing.B) { |
| 204 | b.SetBytes(int64(len(s))) |
| 205 | var acc uint64 |
| 206 | for b.Loop() { |
| 207 | for i := 0; i+8 <= len(s); i += 8 { |
| 208 | acc += Load8(s, i) |
| 209 | } |
| 210 | } |
| 211 | _ = acc |
| 212 | }) |
| 213 | b.Run("store", func(b *testing.B) { |
| 214 | b.SetBytes(int64(len(buf))) |
| 215 | w := Broadcast('x') |
| 216 | for b.Loop() { |
| 217 | for i := 0; i+8 <= len(buf); i += 8 { |
| 218 | Store8(buf, i, w) |
| 219 | } |
| 220 | } |
| 221 | }) |
| 222 | } |
| 223 | |
| 224 | func Test_ZeroLanes_FirstMatchProperty(t *testing.T) { |
| 225 | 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 := ZeroLanes(wordFromLanes(lanes)) |
| 237 | require.NotZero(t, m, "fill 0x%02x zero at %d", fill, zeroAt) |
| 238 | lo := FirstLane(m) |
| 239 | require.Zero(t, lanes[lo], "fill 0x%02x zero at %d: first lane %d", fill, zeroAt, lo) |
| 240 | if byte(fill) != 0 { |
| 241 | 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 | require.Zero(t, ZeroLanes(wordFromLanes(lanes)), "fill 0x%02x", fill) |
| 251 | } |
| 252 | } |