Skip to content
Myr

SIMD.md

This file

Path

src/algo/SIMD.md

99 lines · 4.3 KiB · Markdown

Last changed by

Clean up non-ascii charactersJunegunn Choibefore adoption8b66489

History of this file

Take it

The walk stopped after 200 commits: lines older than 76efddd point at it.

bash: shift-delete to delete history entries during CTRL-RJunegunn Choibefore adoption76efddd

lines 1–4why, one step back
1# SIMD byte search: `indexByteTwo` / `lastIndexByteTwo`
23## What these functions do4

Clean up non-ascii charactersJunegunn Choibefore adoption8b66489

line 5why, one step back
5`indexByteTwo(s []byte, b1, b2 byte) int` -- returns the index of the

bash: shift-delete to delete history entries during CTRL-RJunegunn Choibefore adoption76efddd

lines 6–7why, one step back
6**first** occurrence of `b1` or `b2` in `s`, or `-1`.
7

Clean up non-ascii charactersJunegunn Choibefore adoption8b66489

line 8why, one step back
8`lastIndexByteTwo(s []byte, b1, b2 byte) int` -- returns the index of the

bash: shift-delete to delete history entries during CTRL-RJunegunn Choibefore adoption76efddd

lines 9–93why, one step back
9**last** occurrence of `b1` or `b2` in `s`, or `-1`.
1011They are used by the fuzzy matching algorithm (`algo.go`) to skip ahead12during case-insensitive search. Instead of calling `bytes.IndexByte` twice13(once for lowercase, once for uppercase), a single SIMD pass finds both at14once.1516## File layout1718| 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 |2627## How the SIMD implementations work2829**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`.3940**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`.5556**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.6061## Running tests6263```bash64# Unit + exhaustive tests65go test ./src/algo/ -run 'TestIndexByteTwo|TestLastIndexByteTwo' -v6667# Fuzz tests (run for 10 seconds each)68go test ./src/algo/ -run '^$' -fuzz FuzzIndexByteTwo -fuzztime 10s69go test ./src/algo/ -run '^$' -fuzz FuzzLastIndexByteTwo -fuzztime 10s7071# Cross-architecture: test amd64 on an arm64 Mac (via Rosetta)72GOARCH=amd64 go test ./src/algo/ -run 'TestIndexByteTwo|TestLastIndexByteTwo' -v73GOARCH=amd64 go test ./src/algo/ -run '^$' -fuzz FuzzIndexByteTwo -fuzztime 10s74GOARCH=amd64 go test ./src/algo/ -run '^$' -fuzz FuzzLastIndexByteTwo -fuzztime 10s75```7677## Running micro-benchmarks7879```bash80# All indexByteTwo / lastIndexByteTwo benchmarks81go test ./src/algo/ -bench 'IndexByteTwo' -benchmem8283# Specific size84go test ./src/algo/ -bench 'IndexByteTwo_1000'85```8687Each benchmark compares the SIMD `asm` implementation against reference88implementations (`2xIndexByte` using `bytes.IndexByte`, and a simple `loop`).8990## Correctness verification9192The assembly is verified by three layers of testing:93

Clean up non-ascii charactersJunegunn Choibefore adoption8b66489

lines 94–95why, one step back
941. **Table-driven tests** -- known inputs with expected outputs.
952. **Exhaustive tests** -- all lengths 0–256, every match position, no-match

bash: shift-delete to delete history entries during CTRL-RJunegunn Choibefore adoption76efddd

lines 96–97why, one step back
96 cases, and both-bytes-present cases, compared against a simple loop
97 reference.

Clean up non-ascii charactersJunegunn Choibefore adoption8b66489

line 98why, one step back
983. **Fuzz tests** -- randomized inputs via `testing.F`, compared against the

bash: shift-delete to delete history entries during CTRL-RJunegunn Choibefore adoption76efddd

line 99why, one step back
99 same loop reference.