Debian Codegraph @

internal/caseconv/swar.go

raw
1 package caseconv
2
3 import (
4 "encoding/binary"
5
6 "github.com/gofiber/utils/v2/swar"
7 )
8
9 // Word-at-a-time ASCII case conversion built on the public SWAR primitives
10 // in github.com/gofiber/utils/v2/swar. Byte order inside a word does not
11 // matter for the case folds themselves (every operation is per byte lane),
12 // so these helpers are correct on both little- and big-endian platforms.
13 //
14 // Words move through encoding/binary rather than swar.Load8/swar.Store8 on
15 // purpose: the binary.LittleEndian forms measure ~9% faster in the in-place
16 // word loops here (benchstat -count=10, Go 1.25, Apple M2 Pro). The lane
17 // order is identical, so swar words and binary.LittleEndian loads/stores
18 // can be mixed freely.
19 //
20 // The word loops pin each group of four words with a three-index reslice
21 // and address the words at constant offsets inside it. A two-index
22 // reslice at a variable offset costs a capacity check plus the
23 // four-instruction pointer clamp the compiler emits when the new
24 // capacity is not a known nonzero constant; the pinned form pays one
25 // check per group and no clamp, and the constant-offset accesses inside
26 // it are check-free. See swar.Load8 for the same note.
27
28 // WordLen is the SWAR word width in bytes, re-exported for the case
29 // conversion callers in the strings and bytes packages: they route inputs
30 // shorter than WordLen to byte-wise paths and align offsets they pass (such
31 // as ToLowerCopy's from) with WordLen-derived masks.
32 const WordLen = swar.WordLen
33
34 // FirstUpperIndex returns the index of the first ASCII uppercase byte in b,
35 // or -1 if b contains none. b is only ever read; it may be an unsafe view
36 // over immutable string memory.
37 func FirstUpperIndex(b []byte) int {
38 n := len(b)
39 i := 0
40 if n >= 32 {
41 // Check the first word alone: canonical mixed-case values
42 // ("Content-Type") change case right at the start, and that path
43 // must stay one mask.
44 if m := swar.MatchRangeMask(binary.LittleEndian.Uint64(b[0:8]), 'A', 'Z'); m != 0 {
45 return swar.FirstLane(m)
46 }
47 // Then four words per branch: the masks compute independently, so
48 // the common no-uppercase case pays one test per 32 bytes. The
49 // MatchRangeMask computations stay inline so their constant ranges
50 // fold; only the cold hit-resolution ladder is shared, and it
51 // inlines (the no-match loop never reaches it).
52 for i = 8; i+32 <= n; i += 32 {
53 w := b[i : i+32 : i+32]
54 m0 := swar.MatchRangeMask(binary.LittleEndian.Uint64(w[0:8]), 'A', 'Z')
55 m1 := swar.MatchRangeMask(binary.LittleEndian.Uint64(w[8:16]), 'A', 'Z')
56 m2 := swar.MatchRangeMask(binary.LittleEndian.Uint64(w[16:24]), 'A', 'Z')
57 m3 := swar.MatchRangeMask(binary.LittleEndian.Uint64(w[24:32]), 'A', 'Z')
58 if m0|m1|m2|m3 != 0 {
59 return firstSetLane32(i, m0, m1, m2, m3)
60 }
61 }
62 if i == n {
63 return -1
64 }
65 // Finish with one overlapping block at n-32. MatchRangeMask is
66 // per-lane exact, so re-scanned lanes are known non-matching and the
67 // first set lane always falls in the new bytes.
68 i = n - 32
69 w := b[i : i+32 : i+32]
70 m0 := swar.MatchRangeMask(binary.LittleEndian.Uint64(w[0:8]), 'A', 'Z')
71 m1 := swar.MatchRangeMask(binary.LittleEndian.Uint64(w[8:16]), 'A', 'Z')
72 m2 := swar.MatchRangeMask(binary.LittleEndian.Uint64(w[16:24]), 'A', 'Z')
73 m3 := swar.MatchRangeMask(binary.LittleEndian.Uint64(w[24:32]), 'A', 'Z')
74 if m0|m1|m2|m3 != 0 {
75 return firstSetLane32(i, m0, m1, m2, m3)
76 }
77 return -1
78 }
79 for ; i+8 <= n; i += 8 {
80 if m := swar.MatchRangeMask(binary.LittleEndian.Uint64(b[i:i+8]), 'A', 'Z'); m != 0 {
81 return i + swar.FirstLane(m)
82 }
83 }
84 if i == n {
85 return -1
86 }
87 if n >= 8 {
88 // Scan the tail with one overlapping word; lanes that were already
89 // scanned are known to be zero, so FirstLane lands in the new bytes.
90 if m := swar.MatchRangeMask(binary.LittleEndian.Uint64(b[n-8:n]), 'A', 'Z'); m != 0 {
91 return n - 8 + swar.FirstLane(m)
92 }
93 return -1
94 }
95 for ; i < n; i++ {
96 if c := b[i]; c >= 'A' && c <= 'Z' {
97 return i
98 }
99 }
100 return -1
101 }
102
103 // FirstLowerIndex returns the index of the first ASCII lowercase byte in b,
104 // or -1 if b contains none. b is only ever read; it may be an unsafe view
105 // over immutable string memory.
106 // firstSetLane32 resolves a hit inside a 32-byte block: given the block's
107 // four per-word masks, it returns the index of the first marked byte
108 // relative to base, the block's starting offset. It runs at most once per
109 // scan — only after a block's combined mask tested non-zero — and is small
110 // enough to inline, so the hot no-match loops pay nothing for the sharing.
111 func firstSetLane32(base int, m0, m1, m2, m3 uint64) int {
112 if m0 != 0 {
113 return base + swar.FirstLane(m0)
114 }
115 if m1 != 0 {
116 return base + 8 + swar.FirstLane(m1)
117 }
118 if m2 != 0 {
119 return base + 16 + swar.FirstLane(m2)
120 }
121 return base + 24 + swar.FirstLane(m3)
122 }
123
124 func FirstLowerIndex(b []byte) int {
125 n := len(b)
126 i := 0
127 if n >= 32 {
128 // Structure and rationale mirror FirstUpperIndex.
129 if m := swar.MatchRangeMask(binary.LittleEndian.Uint64(b[0:8]), 'a', 'z'); m != 0 {
130 return swar.FirstLane(m)
131 }
132 for i = 8; i+32 <= n; i += 32 {
133 w := b[i : i+32 : i+32]
134 m0 := swar.MatchRangeMask(binary.LittleEndian.Uint64(w[0:8]), 'a', 'z')
135 m1 := swar.MatchRangeMask(binary.LittleEndian.Uint64(w[8:16]), 'a', 'z')
136 m2 := swar.MatchRangeMask(binary.LittleEndian.Uint64(w[16:24]), 'a', 'z')
137 m3 := swar.MatchRangeMask(binary.LittleEndian.Uint64(w[24:32]), 'a', 'z')
138 if m0|m1|m2|m3 != 0 {
139 return firstSetLane32(i, m0, m1, m2, m3)
140 }
141 }
142 if i == n {
143 return -1
144 }
145 i = n - 32
146 w := b[i : i+32 : i+32]
147 m0 := swar.MatchRangeMask(binary.LittleEndian.Uint64(w[0:8]), 'a', 'z')
148 m1 := swar.MatchRangeMask(binary.LittleEndian.Uint64(w[8:16]), 'a', 'z')
149 m2 := swar.MatchRangeMask(binary.LittleEndian.Uint64(w[16:24]), 'a', 'z')
150 m3 := swar.MatchRangeMask(binary.LittleEndian.Uint64(w[24:32]), 'a', 'z')
151 if m0|m1|m2|m3 != 0 {
152 return firstSetLane32(i, m0, m1, m2, m3)
153 }
154 return -1
155 }
156 for ; i+8 <= n; i += 8 {
157 if m := swar.MatchRangeMask(binary.LittleEndian.Uint64(b[i:i+8]), 'a', 'z'); m != 0 {
158 return i + swar.FirstLane(m)
159 }
160 }
161 if i == n {
162 return -1
163 }
164 if n >= 8 {
165 if m := swar.MatchRangeMask(binary.LittleEndian.Uint64(b[n-8:n]), 'a', 'z'); m != 0 {
166 return n - 8 + swar.FirstLane(m)
167 }
168 return -1
169 }
170 for ; i < n; i++ {
171 if c := b[i]; c >= 'a' && c <= 'z' {
172 return i
173 }
174 }
175 return -1
176 }
177
178 // ToLowerInPlace lower-cases every ASCII uppercase byte of b in place:
179 // four words per pinned group, then single words, then bytes.
180 func ToLowerInPlace(b []byte) {
181 n := len(b)
182 i := 0
183 for ; i+32 <= n; i += 32 {
184 w := b[i : i+32 : i+32]
185 binary.LittleEndian.PutUint64(w[0:8], swar.ToLowerWord(binary.LittleEndian.Uint64(w[0:8])))
186 binary.LittleEndian.PutUint64(w[8:16], swar.ToLowerWord(binary.LittleEndian.Uint64(w[8:16])))
187 binary.LittleEndian.PutUint64(w[16:24], swar.ToLowerWord(binary.LittleEndian.Uint64(w[16:24])))
188 binary.LittleEndian.PutUint64(w[24:32], swar.ToLowerWord(binary.LittleEndian.Uint64(w[24:32])))
189 }
190 for ; i+8 <= n; i += 8 {
191 w := b[i : i+8 : i+8]
192 binary.LittleEndian.PutUint64(w, swar.ToLowerWord(binary.LittleEndian.Uint64(w)))
193 }
194 // Finish byte-wise: an overlapping word here would partially overlap the
195 // previous 8-byte store and stall on store-to-load forwarding.
196 for ; i < n; i++ {
197 b[i] = ToLowerTable[b[i]]
198 }
199 }
200
201 // ToUpperInPlace upper-cases every ASCII lowercase byte of b in place; the
202 // loop shape mirrors ToLowerInPlace.
203 func ToUpperInPlace(b []byte) {
204 n := len(b)
205 i := 0
206 for ; i+32 <= n; i += 32 {
207 w := b[i : i+32 : i+32]
208 binary.LittleEndian.PutUint64(w[0:8], swar.ToUpperWord(binary.LittleEndian.Uint64(w[0:8])))
209 binary.LittleEndian.PutUint64(w[8:16], swar.ToUpperWord(binary.LittleEndian.Uint64(w[8:16])))
210 binary.LittleEndian.PutUint64(w[16:24], swar.ToUpperWord(binary.LittleEndian.Uint64(w[16:24])))
211 binary.LittleEndian.PutUint64(w[24:32], swar.ToUpperWord(binary.LittleEndian.Uint64(w[24:32])))
212 }
213 for ; i+8 <= n; i += 8 {
214 w := b[i : i+8 : i+8]
215 binary.LittleEndian.PutUint64(w, swar.ToUpperWord(binary.LittleEndian.Uint64(w)))
216 }
217 for ; i < n; i++ {
218 b[i] = ToUpperTable[b[i]]
219 }
220 }
221
222 // ToLowerCopy writes the lower-cased content of src into dst starting at
223 // byte offset from. Bytes before from must already be present in dst, and
224 // len(dst) must equal len(src). src is only ever read — the strings package
225 // passes an unsafe view over a string's immutable backing memory — so no
226 // code path here may write through src.
227 //
228 // from must be a multiple of WordLen and at or below the index of the first
229 // byte that lower-casing changes: when len(src) >= WordLen the overlapping
230 // tail store rewrites dst[n-WordLen:from) with case-converted bytes, which is
231 // only a no-op under that precondition.
232 //
233 // Callers currently invoke this only with len(src) >= WordLen; the byte-wise
234 // tail below keeps the helper correct standalone for shorter inputs.
235 func ToLowerCopy(dst, src []byte, from int) {
236 n := len(src)
237 i := from
238 for ; i+32 <= n; i += 32 {
239 s := src[i : i+32 : i+32]
240 d := dst[i : i+32 : i+32]
241 binary.LittleEndian.PutUint64(d[0:8], swar.ToLowerWord(binary.LittleEndian.Uint64(s[0:8])))
242 binary.LittleEndian.PutUint64(d[8:16], swar.ToLowerWord(binary.LittleEndian.Uint64(s[8:16])))
243 binary.LittleEndian.PutUint64(d[16:24], swar.ToLowerWord(binary.LittleEndian.Uint64(s[16:24])))
244 binary.LittleEndian.PutUint64(d[24:32], swar.ToLowerWord(binary.LittleEndian.Uint64(s[24:32])))
245 }
246 for ; i+8 <= n; i += 8 {
247 binary.LittleEndian.PutUint64(dst[i:i+8:i+8], swar.ToLowerWord(binary.LittleEndian.Uint64(src[i:i+8:i+8])))
248 }
249 if i == n {
250 return
251 }
252 if n >= 8 {
253 binary.LittleEndian.PutUint64(dst[n-8:n:n], swar.ToLowerWord(binary.LittleEndian.Uint64(src[n-8:n:n])))
254 return
255 }
256 for ; i < n; i++ {
257 dst[i] = ToLowerTable[src[i]]
258 }
259 }
260
261 // ToUpperCopy writes the upper-cased content of src into dst starting at
262 // byte offset from. Bytes before from must already be present in dst, and
263 // len(dst) must equal len(src).
264 //
265 // from must be a multiple of WordLen and at or below the index of the first
266 // byte that upper-casing changes; see ToLowerCopy for why, including the
267 // note on the byte-wise tail and the requirement that src is never written.
268 func ToUpperCopy(dst, src []byte, from int) {
269 n := len(src)
270 i := from
271 for ; i+32 <= n; i += 32 {
272 s := src[i : i+32 : i+32]
273 d := dst[i : i+32 : i+32]
274 binary.LittleEndian.PutUint64(d[0:8], swar.ToUpperWord(binary.LittleEndian.Uint64(s[0:8])))
275 binary.LittleEndian.PutUint64(d[8:16], swar.ToUpperWord(binary.LittleEndian.Uint64(s[8:16])))
276 binary.LittleEndian.PutUint64(d[16:24], swar.ToUpperWord(binary.LittleEndian.Uint64(s[16:24])))
277 binary.LittleEndian.PutUint64(d[24:32], swar.ToUpperWord(binary.LittleEndian.Uint64(s[24:32])))
278 }
279 for ; i+8 <= n; i += 8 {
280 binary.LittleEndian.PutUint64(dst[i:i+8:i+8], swar.ToUpperWord(binary.LittleEndian.Uint64(src[i:i+8:i+8])))
281 }
282 if i == n {
283 return
284 }
285 if n >= 8 {
286 binary.LittleEndian.PutUint64(dst[n-8:n:n], swar.ToUpperWord(binary.LittleEndian.Uint64(src[n-8:n:n])))
287 return
288 }
289 for ; i < n; i++ {
290 dst[i] = ToUpperTable[src[i]]
291 }
292 }