Debian Codegraph @

simd/memchr.go

raw symbols SCIP
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 func memchrPairGeneric(haystack []byte, byte1, byte2 byte, offset int) int {
168 n := len(haystack)
169 if n < swar.WordLen+offset {
170 for i := 0; i+offset < n; i++ {
171 if haystack[i] == byte1 && haystack[i+offset] == byte2 {
172 return i
173 }
174 }
175 return -1
176 }
177
178 bc1 := swar.Broadcast(byte1)
179 bc2 := swar.Broadcast(byte2)
180 i := 0
181 for ; i+swar.WordLen+offset <= n; i += swar.WordLen {
182 cand := swar.ZeroLanes(swar.Load8(haystack, i)^bc1) &
183 swar.ZeroLanes(swar.Load8(haystack, i+offset)^bc2)
184 for cand != 0 {
185 pos := i + swar.FirstLane(cand)
186 if haystack[pos] == byte1 && haystack[pos+offset] == byte2 {
187 return pos
188 }
189 cand &= cand - 1
190 }
191 }
192 for ; i+offset < n; i++ {
193 if haystack[i] == byte1 && haystack[i+offset] == byte2 {
194 return i
195 }
196 }
197 return -1
198 }