| 1 |
# SIMD byte search: `indexByteTwo` / `lastIndexByteTwo` |
| 2 |
|
| 3 |
## What these functions do |
| 4 |
|
| 5 |
`indexByteTwo(s []byte, b1, b2 byte) int` -- returns the index of the |
| 6 |
**first** occurrence of `b1` or `b2` in `s`, or `-1`. |
| 7 |
|
| 8 |
`lastIndexByteTwo(s []byte, b1, b2 byte) int` -- returns the index of the |
| 9 |
**last** occurrence of `b1` or `b2` in `s`, or `-1`. |
| 10 |
|
| 11 |
They are used by the fuzzy matching algorithm (`algo.go`) to skip ahead |
| 12 |
during case-insensitive search. Instead of calling `bytes.IndexByte` twice |
| 13 |
(once for lowercase, once for uppercase), a single SIMD pass finds both at |
| 14 |
once. |
| 15 |
|
| 16 |
## File layout |
| 17 |
|
| 18 |
| File | Purpose | |
| 19 |
| ------ | --------- | |
| 20 |
| `indexbyte2_arm64.go` | Go declarations (`//go:noescape`) for ARM64 | |
| 21 |
| `indexbyte2_arm64.s` | ARM64 NEON assembly (32-byte aligned blocks, syndrome extraction) | |
| 22 |
| `indexbyte2_amd64.go` | Go declarations + AVX2 runtime detection for AMD64 | |
| 23 |
| `indexbyte2_amd64.s` | AMD64 AVX2/SSE2 assembly with CPUID dispatch | |
| 24 |
| `indexbyte2_other.go` | Pure Go fallback for all other architectures | |
| 25 |
| `indexbyte2_test.go` | Unit tests, exhaustive tests, fuzz tests, and benchmarks | |
| 26 |
|
| 27 |
## How the SIMD implementations work |
| 28 |
|
| 29 |
**ARM64 (NEON):** |
| 30 |
- Broadcasts both needle bytes into NEON registers (`VMOV`). |
| 31 |
- Processes 32-byte aligned chunks. For each chunk, compares all bytes |
| 32 |
against both needles (`VCMEQ`), ORs the results (`VORR`), and builds a |
| 33 |
64-bit syndrome with 2 bits per byte. |
| 34 |
- `indexByteTwo` uses `RBIT` + `CLZ` to find the lowest set bit (first match). |
| 35 |
- `lastIndexByteTwo` scans backward and uses `CLZ` on the raw syndrome to |
| 36 |
find the highest set bit (last match). |
| 37 |
- Handles alignment and partial first/last blocks with bit masking. |
| 38 |
- Adapted from Go's `internal/bytealg/indexbyte_arm64.s`. |
| 39 |
|
| 40 |
**AMD64 (AVX2 with SSE2 fallback):** |
| 41 |
- At init time, `cpuHasAVX2()` checks CPUID + XGETBV for AVX2 and OS YMM |
| 42 |
support. The result is cached in `_useAVX2`. |
| 43 |
- **AVX2 path** (inputs >= 32 bytes, when available): |
| 44 |
- Broadcasts both needles via `VPBROADCASTB`. |
| 45 |
- Processes 32-byte blocks: `VPCMPEQB` against both needles, `VPOR`, then |
| 46 |
`VPMOVMSKB` to get a 32-bit mask. |
| 47 |
- 5 instructions per loop iteration (vs 7 for SSE2) at 2x the throughput. |
| 48 |
- `VZEROUPPER` before every return to avoid SSE/AVX transition penalties. |
| 49 |
- **SSE2 fallback** (inputs < 32 bytes, or CPUs without AVX2): |
| 50 |
- Broadcasts via `PUNPCKLBW` + `PSHUFL`. |
| 51 |
- Processes 16-byte blocks: `PCMPEQB`, `POR`, `PMOVMSKB`. |
| 52 |
- Small inputs (<16 bytes) are handled with page-boundary-safe loads. |
| 53 |
- Both paths use `BSFL` (forward) / `BSRL` (reverse) for bit scanning. |
| 54 |
- Adapted from Go's `internal/bytealg/indexbyte_amd64.s`. |
| 55 |
|
| 56 |
**Fallback (other platforms):** |
| 57 |
- `indexByteTwo` uses two `bytes.IndexByte` calls with scope-limiting |
| 58 |
(search `b1` first, then limit the `b2` search to `s[:i1]`). |
| 59 |
- `lastIndexByteTwo` uses a simple backward for loop. |
| 60 |
|
| 61 |
## Running tests |
| 62 |
|
| 63 |
```bash |
| 64 |
# Unit + exhaustive tests |
| 65 |
go test ./src/algo/ -run 'TestIndexByteTwo|TestLastIndexByteTwo' -v |
| 66 |
|
| 67 |
# Fuzz tests (run for 10 seconds each) |
| 68 |
go test ./src/algo/ -run '^$' -fuzz FuzzIndexByteTwo -fuzztime 10s |
| 69 |
go test ./src/algo/ -run '^$' -fuzz FuzzLastIndexByteTwo -fuzztime 10s |
| 70 |
|
| 71 |
# Cross-architecture: test amd64 on an arm64 Mac (via Rosetta) |
| 72 |
GOARCH=amd64 go test ./src/algo/ -run 'TestIndexByteTwo|TestLastIndexByteTwo' -v |
| 73 |
GOARCH=amd64 go test ./src/algo/ -run '^$' -fuzz FuzzIndexByteTwo -fuzztime 10s |
| 74 |
GOARCH=amd64 go test ./src/algo/ -run '^$' -fuzz FuzzLastIndexByteTwo -fuzztime 10s |
| 75 |
``` |
| 76 |
|
| 77 |
## Running micro-benchmarks |
| 78 |
|
| 79 |
```bash |
| 80 |
# All indexByteTwo / lastIndexByteTwo benchmarks |
| 81 |
go test ./src/algo/ -bench 'IndexByteTwo' -benchmem |
| 82 |
|
| 83 |
# Specific size |
| 84 |
go test ./src/algo/ -bench 'IndexByteTwo_1000' |
| 85 |
``` |
| 86 |
|
| 87 |
Each benchmark compares the SIMD `asm` implementation against reference |
| 88 |
implementations (`2xIndexByte` using `bytes.IndexByte`, and a simple `loop`). |
| 89 |
|
| 90 |
## Correctness verification |
| 91 |
|
| 92 |
The assembly is verified by three layers of testing: |
| 93 |
|
| 94 |
1. **Table-driven tests** -- known inputs with expected outputs. |
| 95 |
2. **Exhaustive tests** -- all lengths 0–256, every match position, no-match |
| 96 |
cases, and both-bytes-present cases, compared against a simple loop |
| 97 |
reference. |
| 98 |
3. **Fuzz tests** -- randomized inputs via `testing.F`, compared against the |
| 99 |
same loop reference. |