Debian Codegraph @

simd/simd.go

raw
1 // Package simd provides SIMD-accelerated byte searching and validation
2 // primitives for the Fiber ecosystem: multi-needle scans, character-class
3 // scans, paired-byte scans, substring search, and ASCII validation.
4 //
5 // Acceleration comes in two tiers. The scan kernels — Memchr2, Memchr3,
6 // MemchrPair, MemchrDigit, MemchrWord, MemchrNotWord, IsASCII,
7 // FirstNonASCII, and CountNonASCII — dispatch to AVX2 assembly on amd64
8 // CPUs (for inputs of MinLen+ bytes) and fall back to portable SWAR loops
9 // built on the swar package everywhere else. The kernels consume four
10 // 32-byte vectors per iteration. The first-match scans and IsASCII combine
11 // the per-vector masks, so a 128-byte block costs one mask extraction and
12 // one branch, and the scans that report a position then re-extract the
13 // four masks in address order to locate the hit. CountNonASCII is the
14 // exception: a count cannot be recovered from a combined mask, so its
15 // block loop accumulates per-lane counts in vector byte lanes and reduces
16 // them once at the end, and the remaining whole vectors are extracted and
17 // population-counted (which is why it, unlike the others, gates on POPCNT
18 // as well as AVX2).
19 // Memmem prefilters on amd64 — needles of 2-6 bytes containing two
20 // distinct values through the MemchrPair kernel from 65-byte haystacks,
21 // longer or single-valued needles through a single rare-byte
22 // bytes.IndexByte scan from 128 bytes — and delegates to bytes.Index for
23 // shorter haystacks or without AVX2.
24 // MemchrInTable/MemchrNotInTable are plain scalar loops on every
25 // architecture, since an arbitrary 256-entry membership test has no cheap
26 // vector form. Accelerated reports whether the AVX2 tier is active.
27 // Single-byte search is deliberately absent: bytes.IndexByte is already
28 // vector-accelerated by the Go runtime on all major architectures.
29 //
30 // Unlike the top-level utils helpers, this package operates on []byte only —
31 // it is the raw engine underneath the generic helpers. Callers holding a
32 // string can use the top-level generic functions instead of converting.
33 //
34 // Adapted from the coregex project, https://github.com/coregx/coregex
35 // (package simd), Copyright (c) 2025 Andrey Kolkov and contributors,
36 // MIT License. See the LICENSE file in this directory for the full text.
37 package simd
38
39 // MinLen is the input length from which the AVX2 kernels engage on amd64:
40 // they consume whole 32-byte vectors, and below one vector their setup cost
41 // outweighs the win, so shorter inputs take the SWAR fallbacks. Callers
42 // that route between their own word loops and this package (as the
43 // top-level utils helpers do) should gate on this constant rather than
44 // hard-coding 32, so a retune here propagates everywhere.
45 const MinLen = 32
46
47 // Accelerated reports whether the AVX2 assembly kernels are in use on this
48 // CPU. When false, all functions use portable fallback implementations.
49 func Accelerated() bool {
50 return hasAVX2
51 }