| 1 | //go:build amd64 && linux |
| 2 | |
| 3 | package simd |
| 4 | |
| 5 | import ( |
| 6 | "syscall" |
| 7 | "testing" |
| 8 | |
| 9 | "github.com/stretchr/testify/require" |
| 10 | ) |
| 11 | |
| 12 | // Every kernel in this package is self-bounding: it clamps its own vector |
| 13 | // loops and finishes short remainders itself, so it may be called directly |
| 14 | // with any length. That is not testable with ordinary slices — Go rounds |
| 15 | // allocations up to a size class, so a read past the end lands on |
| 16 | // zero-filled slack and the wrong answer never materializes. These tests |
| 17 | // place the last byte of the input flush against an unmapped page, turning |
| 18 | // any over-read into a fault instead of a silent success. |
| 19 | // |
| 20 | // countNonASCIIAVX2 is the reason this file exists: it is the one kernel |
| 21 | // whose result depends on the trailing bytes, so it cannot use the |
| 22 | // overlapping-window epilogue its siblings use, and an earlier version |
| 23 | // looped while base < end and read whole vectors past the slice. |
| 24 | |
| 25 | // guardedTail returns a slice of n bytes whose final byte is the last |
| 26 | // readable byte before an unmapped page, plus a cleanup function. Reading |
| 27 | // even one byte past the end faults. |
| 28 | func guardedTail(t *testing.T, n int) []byte { |
| 29 | t.Helper() |
| 30 | pageSize := syscall.Getpagesize() |
| 31 | require.LessOrEqual(t, n, pageSize, "input must fit in one page") |
| 32 | |
| 33 | // Two pages, then drop read access to the second one. |
| 34 | region, err := syscall.Mmap(-1, 0, 2*pageSize, |
| 35 | syscall.PROT_READ|syscall.PROT_WRITE, |
| 36 | syscall.MAP_PRIVATE|syscall.MAP_ANON) |
| 37 | require.NoError(t, err) |
| 38 | t.Cleanup(func() { |
| 39 | require.NoError(t, syscall.Munmap(region)) |
| 40 | }) |
| 41 | require.NoError(t, syscall.Mprotect(region[pageSize:], syscall.PROT_NONE)) |
| 42 | |
| 43 | return region[pageSize-n : pageSize : pageSize] |
| 44 | } |
| 45 | |
| 46 | func Test_KernelsAreSelfBounding(t *testing.T) { |
| 47 | if !Accelerated() { |
| 48 | t.Skip("AVX2 kernels not active on this CPU") |
| 49 | } |
| 50 | // Lengths that leave every possible sub-vector and sub-block |
| 51 | // remainder, including the 1..31 that has no whole vector at all. |
| 52 | for _, n := range []int{ |
| 53 | 1, 2, 7, 8, 15, 16, 31, 32, 33, 63, 64, 65, 95, |
| 54 | 96, 127, 128, 129, 159, 160, 161, 200, 255, 256, 257, 384, 385, |
| 55 | } { |
| 56 | buf := guardedTail(t, n) |
| 57 | for i := range buf { |
| 58 | buf[i] = byte('a' + i%23) |
| 59 | } |
| 60 | // Each kernel is called directly, bypassing the *Impl dispatch, so |
| 61 | // the guard is on the kernel itself and not on a wrapper. |
| 62 | require.True(t, isASCIIAVX2(buf), "isASCIIAVX2 len %d", n) |
| 63 | require.Equal(t, -1, firstNonASCIIAVX2(buf), "firstNonASCIIAVX2 len %d", n) |
| 64 | if hasPOPCNT { |
| 65 | require.Equal(t, 0, countNonASCIIAVX2(buf), "countNonASCIIAVX2 len %d", n) |
| 66 | } |
| 67 | require.Equal(t, -1, memchr2AVX2(buf, 'z', 'y'), "memchr2AVX2 len %d", n) |
| 68 | require.Equal(t, -1, memchr3AVX2(buf, 'z', 'y', 'x'), "memchr3AVX2 len %d", n) |
| 69 | require.Equal(t, -1, memchrDigitAVX2(buf), "memchrDigitAVX2 len %d", n) |
| 70 | require.Equal(t, -1, memchrNotWordAVX2(buf), "memchrNotWordAVX2 len %d", n) |
| 71 | for _, offset := range []int{1, 2, 8, 31, 32, 33} { |
| 72 | if offset >= n { |
| 73 | continue |
| 74 | } |
| 75 | require.Equal(t, -1, memchrPairAVX2(buf, 'z', 'y', offset), |
| 76 | "memchrPairAVX2 len %d offset %d", n, offset) |
| 77 | } |
| 78 | |
| 79 | // The word scan needs a haystack with no word characters, so it |
| 80 | // too has to walk to the end. |
| 81 | for i := range buf { |
| 82 | buf[i] = '.' |
| 83 | } |
| 84 | require.Equal(t, -1, memchrWordAVX2(buf), "memchrWordAVX2 len %d", n) |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | // Test_CountNonASCIIAVX2_CountsExactly pins the counting kernel's tail |
| 89 | // handling: an over-read that happened to land on non-ASCII bytes would |
| 90 | // inflate the count rather than fault, which the guard test alone cannot |
| 91 | // distinguish from a correct scan of zero-filled slack. |
| 92 | func Test_CountNonASCIIAVX2_CountsExactly(t *testing.T) { |
| 93 | if !Accelerated() || !hasPOPCNT { |
| 94 | t.Skip("AVX2+POPCNT kernels not active on this CPU") |
| 95 | } |
| 96 | for n := range 400 { |
| 97 | buf := guardedTail(t, max(n, 1))[:n] |
| 98 | want := 0 |
| 99 | for i := range buf { |
| 100 | buf[i] = byte(i * 7) |
| 101 | if buf[i] >= 0x80 { |
| 102 | want++ |
| 103 | } |
| 104 | } |
| 105 | require.Equal(t, want, countNonASCIIAVX2(buf), "len %d", n) |
| 106 | require.Equal(t, want, CountNonASCII(buf), "dispatched, len %d", n) |
| 107 | } |
| 108 | } |