| 1 |
// Adapted from the coregex project, https://github.com/coregx/coregex |
| 2 |
// (package simd), Copyright (c) 2025 Andrey Kolkov and contributors, |
| 3 |
// MIT License. See the LICENSE file in this directory for the full text. |
| 4 |
|
| 5 |
package simd |
| 6 |
|
| 7 |
import ( |
| 8 |
"bytes" |
| 9 |
|
| 10 |
"github.com/gofiber/utils/v2/swar" |
| 11 |
) |
| 12 |
|
| 13 |
// The portable fallbacks below are composed from the swar package's |
| 14 |
// primitives rather than re-deriving the bit tricks; see swar's package |
| 15 |
// documentation for the ZeroLanes contract the first-match scans rely on |
| 16 |
// (false positives only above the first true match, so the first set lane |
| 17 |
// is always exact). |
| 18 |
|
| 19 |
// Memchr2 returns the index of the first occurrence in haystack of either |
| 20 |
// needle1 or needle2, or -1 if neither is present. Both needles are checked |
| 21 |
// in the same pass, so it costs the same as a single-byte scan. |
| 22 |
// |
| 23 |
// There is deliberately no single-needle Memchr: use bytes.IndexByte, which |
| 24 |
// the Go runtime already accelerates on all major architectures. |
| 25 |
func Memchr2(haystack []byte, needle1, needle2 byte) int { |
| 26 |
if len(haystack) == 0 { |
| 27 |
return -1 |
| 28 |
} |
| 29 |
return memchr2Impl(haystack, needle1, needle2) |
| 30 |
} |
| 31 |
|
| 32 |
// Memchr3 returns the index of the first occurrence in haystack of needle1, |
| 33 |
// needle2, or needle3, or -1 if none is present. All three needles are |
| 34 |
// checked in the same pass. |
| 35 |
func Memchr3(haystack []byte, needle1, needle2, needle3 byte) int { |
| 36 |
if len(haystack) == 0 { |
| 37 |
return -1 |
| 38 |
} |
| 39 |
return memchr3Impl(haystack, needle1, needle2, needle3) |
| 40 |
} |
| 41 |
|
| 42 |
// MemchrPair returns the first index i such that haystack[i] == byte1 and |
| 43 |
// haystack[i+offset] == byte2, or -1 if no such position exists. offset must |
| 44 |
// be >= 0; a negative offset returns -1. |
| 45 |
// |
| 46 |
// Requiring two bytes at a fixed distance makes the scan far more selective |
| 47 |
// than a single-byte search, which is what makes it a good substring |
| 48 |
// prefilter: false candidates need both bytes at exactly the right distance. |
| 49 |
// Memmem uses it for short needles. |
| 50 |
func MemchrPair(haystack []byte, byte1, byte2 byte, offset int) int { |
| 51 |
if offset < 0 || len(haystack) <= offset { |
| 52 |
return -1 |
| 53 |
} |
| 54 |
if offset == 0 { |
| 55 |
// Both bytes would occupy the same position, so they must be equal. |
| 56 |
if byte1 != byte2 { |
| 57 |
return -1 |
| 58 |
} |
| 59 |
return bytes.IndexByte(haystack, byte1) |
| 60 |
} |
| 61 |
return memchrPairImpl(haystack, byte1, byte2, offset) |
| 62 |
} |
| 63 |
|
| 64 |
// memchr2Generic is the portable SWAR fallback for Memchr2: a ZeroLanes |
| 65 |
// first-match scan over both needle broadcasts, finishing 8+ byte inputs |
| 66 |
// with one overlapping word at n-8 (already-scanned lanes are known |
| 67 |
// non-matching, so the first set lane falls in the new bytes). |
| 68 |
func memchr2Generic(haystack []byte, needle1, needle2 byte) int { |
| 69 |
n := len(haystack) |
| 70 |
if n < swar.WordLen { |
| 71 |
for i := range n { |
| 72 |
if haystack[i] == needle1 || haystack[i] == needle2 { |
| 73 |
return i |
| 74 |
} |
| 75 |
} |
| 76 |
return -1 |
| 77 |
} |
| 78 |
|
| 79 |
bc1 := swar.Broadcast(needle1) |
| 80 |
bc2 := swar.Broadcast(needle2) |
| 81 |
i := 0 |
| 82 |
for ; i+2*swar.WordLen <= n; i += 2 * swar.WordLen { |
| 83 |
// Pinned window: see the note above isASCIIGeneric in ascii.go for |
| 84 |
// why the loads use constant offsets into it. |
| 85 |
win := haystack[i : i+2*swar.WordLen : i+2*swar.WordLen] |
| 86 |
w0, w1 := swar.Load8(win, 0), swar.Load8(win, swar.WordLen) |
| 87 |
m0 := swar.ZeroLanes(w0^bc1) | swar.ZeroLanes(w0^bc2) |
| 88 |
m1 := swar.ZeroLanes(w1^bc1) | swar.ZeroLanes(w1^bc2) |
| 89 |
if m0|m1 != 0 { |
| 90 |
if m0 != 0 { |
| 91 |
return i + swar.FirstLane(m0) |
| 92 |
} |
| 93 |
return i + swar.WordLen + swar.FirstLane(m1) |
| 94 |
} |
| 95 |
} |
| 96 |
for ; i+swar.WordLen <= n; i += swar.WordLen { |
| 97 |
w := swar.Load8(haystack, i) |
| 98 |
if m := swar.ZeroLanes(w^bc1) | swar.ZeroLanes(w^bc2); m != 0 { |
| 99 |
return i + swar.FirstLane(m) |
| 100 |
} |
| 101 |
} |
| 102 |
if i == n { |
| 103 |
return -1 |
| 104 |
} |
| 105 |
w := swar.Load8(haystack, n-swar.WordLen) |
| 106 |
if m := swar.ZeroLanes(w^bc1) | swar.ZeroLanes(w^bc2); m != 0 { |
| 107 |
return n - swar.WordLen + swar.FirstLane(m) |
| 108 |
} |
| 109 |
return -1 |
| 110 |
} |
| 111 |
|
| 112 |
// memchr3Generic is the portable SWAR fallback for Memchr3; see |
| 113 |
// memchr2Generic for the scan shape. |
| 114 |
func memchr3Generic(haystack []byte, needle1, needle2, needle3 byte) int { |
| 115 |
n := len(haystack) |
| 116 |
if n < swar.WordLen { |
| 117 |
for i := range n { |
| 118 |
if haystack[i] == needle1 || haystack[i] == needle2 || haystack[i] == needle3 { |
| 119 |
return i |
| 120 |
} |
| 121 |
} |
| 122 |
return -1 |
| 123 |
} |
| 124 |
|
| 125 |
bc1 := swar.Broadcast(needle1) |
| 126 |
bc2 := swar.Broadcast(needle2) |
| 127 |
bc3 := swar.Broadcast(needle3) |
| 128 |
i := 0 |
| 129 |
for ; i+2*swar.WordLen <= n; i += 2 * swar.WordLen { |
| 130 |
win := haystack[i : i+2*swar.WordLen : i+2*swar.WordLen] |
| 131 |
w0, w1 := swar.Load8(win, 0), swar.Load8(win, swar.WordLen) |
| 132 |
m0 := swar.ZeroLanes(w0^bc1) | swar.ZeroLanes(w0^bc2) | swar.ZeroLanes(w0^bc3) |
| 133 |
m1 := swar.ZeroLanes(w1^bc1) | swar.ZeroLanes(w1^bc2) | swar.ZeroLanes(w1^bc3) |
| 134 |
if m0|m1 != 0 { |
| 135 |
if m0 != 0 { |
| 136 |
return i + swar.FirstLane(m0) |
| 137 |
} |
| 138 |
return i + swar.WordLen + swar.FirstLane(m1) |
| 139 |
} |
| 140 |
} |
| 141 |
for ; i+swar.WordLen <= n; i += swar.WordLen { |
| 142 |
w := swar.Load8(haystack, i) |
| 143 |
if m := swar.ZeroLanes(w^bc1) | swar.ZeroLanes(w^bc2) | swar.ZeroLanes(w^bc3); m != 0 { |
| 144 |
return i + swar.FirstLane(m) |
| 145 |
} |
| 146 |
} |
| 147 |
if i == n { |
| 148 |
return -1 |
| 149 |
} |
| 150 |
w := swar.Load8(haystack, n-swar.WordLen) |
| 151 |
if m := swar.ZeroLanes(w^bc1) | swar.ZeroLanes(w^bc2) | swar.ZeroLanes(w^bc3); m != 0 { |
| 152 |
return n - swar.WordLen + swar.FirstLane(m) |
| 153 |
} |
| 154 |
return -1 |
| 155 |
} |
| 156 |
|
| 157 |
// memchrPairGeneric is the portable SWAR fallback for MemchrPair. It leans |
| 158 |
// on ZeroLanes flagging every true zero lane (part of its contract), so |
| 159 |
// ANDing the two per-position masks can never drop a real pair; what the |
| 160 |
// AND does lose is lane exactness — a flagged lane may be a false |
| 161 |
// candidate sitting strictly below the first true pair — so every |
| 162 |
// candidate is re-verified scalar and refuted ones are stepped past. |
| 163 |
// ZeroLanes plus this rare verify step beats a pair of exact |
| 164 |
// swar.MatchByteMask masks on the no-candidate fast path, which is the |
| 165 |
// common case. The caller guarantees offset >= 1 and |
| 166 |
// len(haystack) > offset. |
| 167 |
// |
| 168 |
// The scan runs over the candidate positions 0..len-offset-1: two words |
| 169 |
// per branch through pinned windows (see ascii.go), then one word, then |
| 170 |
// one overlapping word ending at the last candidate. Positions below the |
| 171 |
// overlap were either never flagged or refuted by the verify step, so |
| 172 |
// both are known non-pairs and any candidate the overlap flags there |
| 173 |
// fails verification harmlessly; the first candidate to verify is the |
| 174 |
// first pair. An earlier version loaded both words of every step through |
| 175 |
// swar.Load8 at variable offsets and finished the last 1-7 positions |
| 176 |
// scalar, which at 32 bytes ran no faster than a plain scalar loop. |
| 177 |
func memchrPairGeneric(haystack []byte, byte1, byte2 byte, offset int) int { |
| 178 |
n := len(haystack) |
| 179 |
if n < swar.WordLen+offset { |
| 180 |
for i := 0; i+offset < n; i++ { |
| 181 |
if haystack[i] == byte1 && haystack[i+offset] == byte2 { |
| 182 |
return i |
| 183 |
} |
| 184 |
} |
| 185 |
return -1 |
| 186 |
} |
| 187 |
|
| 188 |
bc1 := swar.Broadcast(byte1) |
| 189 |
bc2 := swar.Broadcast(byte2) |
| 190 |
last := n - offset // candidate positions are 0..last-1 |
| 191 |
i := 0 |
| 192 |
for ; i+2*swar.WordLen <= last; i += 2 * swar.WordLen { |
| 193 |
a := haystack[i : i+2*swar.WordLen : i+2*swar.WordLen] |
| 194 |
b := haystack[i+offset : i+offset+2*swar.WordLen : i+offset+2*swar.WordLen] |
| 195 |
c0 := swar.ZeroLanes(swar.Load8(a, 0)^bc1) & swar.ZeroLanes(swar.Load8(b, 0)^bc2) |
| 196 |
c1 := swar.ZeroLanes(swar.Load8(a, swar.WordLen)^bc1) & swar.ZeroLanes(swar.Load8(b, swar.WordLen)^bc2) |
| 197 |
if c0|c1 != 0 { |
| 198 |
if pos := verifyPairCandidates(haystack, byte1, byte2, offset, i, c0); pos >= 0 { |
| 199 |
return pos |
| 200 |
} |
| 201 |
if pos := verifyPairCandidates(haystack, byte1, byte2, offset, i+swar.WordLen, c1); pos >= 0 { |
| 202 |
return pos |
| 203 |
} |
| 204 |
} |
| 205 |
} |
| 206 |
for ; i+swar.WordLen <= last; i += swar.WordLen { |
| 207 |
a := haystack[i : i+swar.WordLen : i+swar.WordLen] |
| 208 |
b := haystack[i+offset : i+offset+swar.WordLen : i+offset+swar.WordLen] |
| 209 |
c := swar.ZeroLanes(swar.Load8(a, 0)^bc1) & swar.ZeroLanes(swar.Load8(b, 0)^bc2) |
| 210 |
if c != 0 { |
| 211 |
if pos := verifyPairCandidates(haystack, byte1, byte2, offset, i, c); pos >= 0 { |
| 212 |
return pos |
| 213 |
} |
| 214 |
} |
| 215 |
} |
| 216 |
if i == last { |
| 217 |
return -1 |
| 218 |
} |
| 219 |
i = last - swar.WordLen |
| 220 |
a := haystack[i : i+swar.WordLen : i+swar.WordLen] |
| 221 |
b := haystack[i+offset : i+offset+swar.WordLen : i+offset+swar.WordLen] |
| 222 |
c := swar.ZeroLanes(swar.Load8(a, 0)^bc1) & swar.ZeroLanes(swar.Load8(b, 0)^bc2) |
| 223 |
if c != 0 { |
| 224 |
return verifyPairCandidates(haystack, byte1, byte2, offset, i, c) |
| 225 |
} |
| 226 |
return -1 |
| 227 |
} |
| 228 |
|
| 229 |
// verifyPairCandidates checks the candidate lanes of cand, a ZeroLanes AND |
| 230 |
// mask for the word at base, in address order and returns the first |
| 231 |
// position that really holds byte1 and byte2 at distance offset, or -1. |
| 232 |
// It runs only on the rare candidate path, so the scalar re-reads are |
| 233 |
// never on the no-match loop. |
| 234 |
func verifyPairCandidates(haystack []byte, byte1, byte2 byte, offset, base int, cand uint64) int { |
| 235 |
for ; cand != 0; cand &= cand - 1 { |
| 236 |
pos := base + swar.FirstLane(cand) |
| 237 |
if haystack[pos] == byte1 && haystack[pos+offset] == byte2 { |
| 238 |
return pos |
| 239 |
} |
| 240 |
} |
| 241 |
return -1 |
| 242 |
} |