Add SIMD indexByteTwo/lastIndexByteTwo doc
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Junegunn Choi · committed by Junegunn Choi · parent 50be8bc · Browse files · 1 file·+99 added−0 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+99 added−0 removed
- other:+99 added−0 removed
Provenance
- Commit
96eb68c- Tree
82b5f52- Author
Junegunn Choi <junegunn.c@gmail.com>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent
50be8bc
1 file changed+99 added−0 removed
added:src/algo/SIMD.md+99 added
1+# SIMD byte search: `indexByteTwo` / `lastIndexByteTwo`2+3+## What these functions do4+5+`indexByteTwo(s []byte, b1, b2 byte) int` — returns the index of the6+**first** occurrence of `b1` or `b2` in `s`, or `-1`.7+8+`lastIndexByteTwo(s []byte, b1, b2 byte) int` — returns the index of the9+**last** occurrence of `b1` or `b2` in `s`, or `-1`.10+11+They are used by the fuzzy matching algorithm (`algo.go`) to skip ahead12+during case-insensitive search. Instead of calling `bytes.IndexByte` twice13+(once for lowercase, once for uppercase), a single SIMD pass finds both at14+once.15+16+## File layout17+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 work28+29+**ARM64 (NEON):**30+- Broadcasts both needle bytes into NEON registers (`VMOV`).31+- Processes 32-byte aligned chunks. For each chunk, compares all bytes32+ against both needles (`VCMEQ`), ORs the results (`VORR`), and builds a33+ 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 to36+ 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 YMM42+ 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`, then46+ `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-limiting58+ (search `b1` first, then limit the `b2` search to `s[:i1]`).59+- `lastIndexByteTwo` uses a simple backward for loop.60+61+## Running tests62+63+```bash64+# Unit + exhaustive tests65+go test ./src/algo/ -run 'TestIndexByteTwo|TestLastIndexByteTwo' -v66+67+# Fuzz tests (run for 10 seconds each)68+go test ./src/algo/ -run '^$' -fuzz FuzzIndexByteTwo -fuzztime 10s69+go test ./src/algo/ -run '^$' -fuzz FuzzLastIndexByteTwo -fuzztime 10s70+71+# Cross-architecture: test amd64 on an arm64 Mac (via Rosetta)72+GOARCH=amd64 go test ./src/algo/ -run 'TestIndexByteTwo|TestLastIndexByteTwo' -v73+GOARCH=amd64 go test ./src/algo/ -run '^$' -fuzz FuzzIndexByteTwo -fuzztime 10s74+GOARCH=amd64 go test ./src/algo/ -run '^$' -fuzz FuzzLastIndexByteTwo -fuzztime 10s75+```76+77+## Running micro-benchmarks78+79+```bash80+# All indexByteTwo / lastIndexByteTwo benchmarks81+go test ./src/algo/ -bench 'IndexByteTwo' -benchmem82+83+# Specific size84+go test ./src/algo/ -bench 'IndexByteTwo_1000'85+```86+87+Each benchmark compares the SIMD `asm` implementation against reference88+implementations (`2xIndexByte` using `bytes.IndexByte`, and a simple `loop`).89+90+## Correctness verification91+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-match96+ cases, and both-bytes-present cases, compared against a simple loop97+ reference.98+3. **Fuzz tests** — randomized inputs via `testing.F`, compared against the99+ same loop reference.