| 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 it |
| 15 | // extracts and population-counts all four (and, unlike the others, gates |
| 16 | // on POPCNT as well as AVX2). |
| 17 | // Memmem prefilters 128+ byte haystacks on amd64 — needles of 2-6 bytes |
| 18 | // containing two distinct values through the MemchrPair kernel, longer or |
| 19 | // single-valued needles through a single rare-byte bytes.IndexByte scan — |
| 20 | // and delegates to bytes.Index for shorter haystacks or without AVX2. |
| 21 | // MemchrInTable/MemchrNotInTable are plain scalar loops on every |
| 22 | // architecture, since an arbitrary 256-entry membership test has no cheap |
| 23 | // vector form. Accelerated reports whether the AVX2 tier is active. |
| 24 | // Single-byte search is deliberately absent: bytes.IndexByte is already |
| 25 | // vector-accelerated by the Go runtime on all major architectures. |
| 26 | // |
| 27 | // Unlike the top-level utils helpers, this package operates on []byte only — |
| 28 | // it is the raw engine underneath the generic helpers. Callers holding a |
| 29 | // string can use the top-level generic functions instead of converting. |
| 30 | // |
| 31 | // Adapted from the coregex project, https://github.com/coregx/coregex |
| 32 | // (package simd), Copyright (c) 2025 Andrey Kolkov and contributors, |
| 33 | // MIT License. See the LICENSE file in this directory for the full text. |
| 34 | package simd |
| 35 | |
| 36 | // MinLen is the input length from which the AVX2 kernels engage on amd64: |
| 37 | // they consume whole 32-byte vectors, and below one vector their setup cost |
| 38 | // outweighs the win, so shorter inputs take the SWAR fallbacks. Callers |
| 39 | // that route between their own word loops and this package (as the |
| 40 | // top-level utils helpers do) should gate on this constant rather than |
| 41 | // hard-coding 32, so a retune here propagates everywhere. |
| 42 | const MinLen = 32 |
| 43 | |
| 44 | // Accelerated reports whether the AVX2 assembly kernels are in use on this |
| 45 | // CPU. When false, all functions use portable fallback implementations. |
| 46 | func Accelerated() bool { |
| 47 | return hasAVX2 |
| 48 | } |