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, and the length from which these
29 // helpers are worth a call: below it the conversion is a table lookup per byte,
30 // cheaper inline. The case packages gate on it; the From converters require it.
31 const WordLen = swar.WordLen
32
33 // ToLowerFrom returns a lower-cased copy of src. first must be FirstUpperIndex's
34 // own result, with 0 <= first < len(src) and len(src) >= WordLen: a larger first
35 // silently mis-converts, so it must not be cached or derived elsewhere.
36 //
37 // src is only read, so it may be an unsafe view over immutable string memory;
38 // the result is freshly allocated and unaliased. strings.ToLower needs both.
39 func ToLowerFrom(src []byte, first int) []byte {
40 n := len(src)
41 dst := make([]byte, n)
42
43 // Copy verbatim up to the word holding first, then convert from there.
44 i := first &^ (WordLen - 1)
45 copy(dst, src[:i])
46
47 for ; i+4*WordLen <= n; i += 4 * WordLen {
48 sw := src[i : i+4*WordLen : i+4*WordLen]
49 d := dst[i : i+4*WordLen : i+4*WordLen]
50 binary.LittleEndian.PutUint64(d[0:WordLen], swar.ToLowerWord(binary.LittleEndian.Uint64(sw[0:WordLen])))
51 binary.LittleEndian.PutUint64(d[WordLen:2*WordLen], swar.ToLowerWord(binary.LittleEndian.Uint64(sw[WordLen:2*WordLen])))
52 binary.LittleEndian.PutUint64(d[2*WordLen:3*WordLen], swar.ToLowerWord(binary.LittleEndian.Uint64(sw[2*WordLen:3*WordLen])))
53 binary.LittleEndian.PutUint64(d[3*WordLen:4*WordLen], swar.ToLowerWord(binary.LittleEndian.Uint64(sw[3*WordLen:4*WordLen])))
54 }
55 for ; i+WordLen <= n; i += WordLen {
56 binary.LittleEndian.PutUint64(dst[i:i+WordLen:i+WordLen], swar.ToLowerWord(binary.LittleEndian.Uint64(src[i:i+WordLen:i+WordLen])))
57 }
58 if i == n {
59 return dst
60 }
61 // One overlapping word finishes the tail. It re-reads src, so above the
62 // aligned start it recomputes what the loops wrote, and below it folds over
63 // the verbatim prefix -- harmless only because every byte under first is
64 // unchanged by the fold, which is what makes first's upper bound matter.
65 binary.LittleEndian.PutUint64(dst[n-WordLen:n:n], swar.ToLowerWord(binary.LittleEndian.Uint64(src[n-WordLen:n:n])))
66 return dst
67 }
68
69 // ToUpperFrom mirrors ToLowerFrom, with first from FirstLowerIndex.
70 func ToUpperFrom(src []byte, first int) []byte {
71 n := len(src)
72 dst := make([]byte, n)
73
74 // Copy verbatim up to the word holding first, then convert from there.
75 i := first &^ (WordLen - 1)
76 copy(dst, src[:i])
77
78 for ; i+4*WordLen <= n; i += 4 * WordLen {
79 sw := src[i : i+4*WordLen : i+4*WordLen]
80 d := dst[i : i+4*WordLen : i+4*WordLen]
81 binary.LittleEndian.PutUint64(d[0:WordLen], swar.ToUpperWord(binary.LittleEndian.Uint64(sw[0:WordLen])))
82 binary.LittleEndian.PutUint64(d[WordLen:2*WordLen], swar.ToUpperWord(binary.LittleEndian.Uint64(sw[WordLen:2*WordLen])))
83 binary.LittleEndian.PutUint64(d[2*WordLen:3*WordLen], swar.ToUpperWord(binary.LittleEndian.Uint64(sw[2*WordLen:3*WordLen])))
84 binary.LittleEndian.PutUint64(d[3*WordLen:4*WordLen], swar.ToUpperWord(binary.LittleEndian.Uint64(sw[3*WordLen:4*WordLen])))
85 }
86 for ; i+WordLen <= n; i += WordLen {
87 binary.LittleEndian.PutUint64(dst[i:i+WordLen:i+WordLen], swar.ToUpperWord(binary.LittleEndian.Uint64(src[i:i+WordLen:i+WordLen])))
88 }
89 if i == n {
90 return dst
91 }
92 // See ToLowerFrom for why the overlapping store is harmless.
93 binary.LittleEndian.PutUint64(dst[n-WordLen:n:n], swar.ToUpperWord(binary.LittleEndian.Uint64(src[n-WordLen:n:n])))
94 return dst
95 }
96
97 // FirstUpperIndex returns the index of the first ASCII uppercase byte in b,
98 // or -1 if b contains none. b is only ever read; it may be an unsafe view
99 // over immutable string memory.
100 func FirstUpperIndex(b []byte) int {
101 n := len(b)
102 i := 0
103 if n >= 32 {
104 // Check the first word alone: canonical mixed-case values
105 // ("Content-Type") change case right at the start, and that path
106 // must stay one mask.
107 if m := swar.MatchRangeMask(binary.LittleEndian.Uint64(b[0:8]), 'A', 'Z'); m != 0 {
108 return swar.FirstLane(m)
109 }
110 // Then four words per branch: the masks compute independently, so
111 // the common no-uppercase case pays one test per 32 bytes. The
112 // MatchRangeMask computations stay inline so their constant ranges
113 // fold; only the cold hit-resolution ladder is shared, and it
114 // inlines (the no-match loop never reaches it).
115 for i = 8; i+32 <= n; i += 32 {
116 w := b[i : i+32 : i+32]
117 m0 := swar.MatchRangeMask(binary.LittleEndian.Uint64(w[0:8]), 'A', 'Z')
118 m1 := swar.MatchRangeMask(binary.LittleEndian.Uint64(w[8:16]), 'A', 'Z')
119 m2 := swar.MatchRangeMask(binary.LittleEndian.Uint64(w[16:24]), 'A', 'Z')
120 m3 := swar.MatchRangeMask(binary.LittleEndian.Uint64(w[24:32]), 'A', 'Z')
121 if m0|m1|m2|m3 != 0 {
122 return firstSetLane32(i, m0, m1, m2, m3)
123 }
124 }
125 if i == n {
126 return -1
127 }
128 // Finish with one overlapping block at n-32. MatchRangeMask is
129 // per-lane exact, so re-scanned lanes are known non-matching and the
130 // first set lane always falls in the new bytes.
131 i = n - 32
132 w := b[i : i+32 : i+32]
133 m0 := swar.MatchRangeMask(binary.LittleEndian.Uint64(w[0:8]), 'A', 'Z')
134 m1 := swar.MatchRangeMask(binary.LittleEndian.Uint64(w[8:16]), 'A', 'Z')
135 m2 := swar.MatchRangeMask(binary.LittleEndian.Uint64(w[16:24]), 'A', 'Z')
136 m3 := swar.MatchRangeMask(binary.LittleEndian.Uint64(w[24:32]), 'A', 'Z')
137 if m0|m1|m2|m3 != 0 {
138 return firstSetLane32(i, m0, m1, m2, m3)
139 }
140 return -1
141 }
142 for ; i+8 <= n; i += 8 {
143 if m := swar.MatchRangeMask(binary.LittleEndian.Uint64(b[i:i+8]), 'A', 'Z'); m != 0 {
144 return i + swar.FirstLane(m)
145 }
146 }
147 if i == n {
148 return -1
149 }
150 if n >= 8 {
151 // Scan the tail with one overlapping word; lanes that were already
152 // scanned are known to be zero, so FirstLane lands in the new bytes.
153 if m := swar.MatchRangeMask(binary.LittleEndian.Uint64(b[n-8:n]), 'A', 'Z'); m != 0 {
154 return n - 8 + swar.FirstLane(m)
155 }
156 return -1
157 }
158 for ; i < n; i++ {
159 if c := b[i]; c >= 'A' && c <= 'Z' {
160 return i
161 }
162 }
163 return -1
164 }
165
166 // firstSetLane32 resolves a hit inside a 32-byte block: given the block's
167 // four per-word masks, it returns the index of the first marked byte
168 // relative to base, the block's starting offset. It runs at most once per
169 // scan — only after a block's combined mask tested non-zero — and is small
170 // enough to inline, so the hot no-match loops pay nothing for the sharing.
171 func firstSetLane32(base int, m0, m1, m2, m3 uint64) int {
172 if m0 != 0 {
173 return base + swar.FirstLane(m0)
174 }
175 if m1 != 0 {
176 return base + 8 + swar.FirstLane(m1)
177 }
178 if m2 != 0 {
179 return base + 16 + swar.FirstLane(m2)
180 }
181 return base + 24 + swar.FirstLane(m3)
182 }
183
184 // FirstLowerIndex returns the index of the first ASCII lowercase byte in b,
185 // or -1 if b contains none. b is only ever read; it may be an unsafe view
186 // over immutable string memory.
187 func FirstLowerIndex(b []byte) int {
188 n := len(b)
189 i := 0
190 if n >= 32 {
191 // Structure and rationale mirror FirstUpperIndex.
192 if m := swar.MatchRangeMask(binary.LittleEndian.Uint64(b[0:8]), 'a', 'z'); m != 0 {
193 return swar.FirstLane(m)
194 }
195 for i = 8; i+32 <= n; i += 32 {
196 w := b[i : i+32 : i+32]
197 m0 := swar.MatchRangeMask(binary.LittleEndian.Uint64(w[0:8]), 'a', 'z')
198 m1 := swar.MatchRangeMask(binary.LittleEndian.Uint64(w[8:16]), 'a', 'z')
199 m2 := swar.MatchRangeMask(binary.LittleEndian.Uint64(w[16:24]), 'a', 'z')
200 m3 := swar.MatchRangeMask(binary.LittleEndian.Uint64(w[24:32]), 'a', 'z')
201 if m0|m1|m2|m3 != 0 {
202 return firstSetLane32(i, m0, m1, m2, m3)
203 }
204 }
205 if i == n {
206 return -1
207 }
208 i = n - 32
209 w := b[i : i+32 : i+32]
210 m0 := swar.MatchRangeMask(binary.LittleEndian.Uint64(w[0:8]), 'a', 'z')
211 m1 := swar.MatchRangeMask(binary.LittleEndian.Uint64(w[8:16]), 'a', 'z')
212 m2 := swar.MatchRangeMask(binary.LittleEndian.Uint64(w[16:24]), 'a', 'z')
213 m3 := swar.MatchRangeMask(binary.LittleEndian.Uint64(w[24:32]), 'a', 'z')
214 if m0|m1|m2|m3 != 0 {
215 return firstSetLane32(i, m0, m1, m2, m3)
216 }
217 return -1
218 }
219 for ; i+8 <= n; i += 8 {
220 if m := swar.MatchRangeMask(binary.LittleEndian.Uint64(b[i:i+8]), 'a', 'z'); m != 0 {
221 return i + swar.FirstLane(m)
222 }
223 }
224 if i == n {
225 return -1
226 }
227 if n >= 8 {
228 if m := swar.MatchRangeMask(binary.LittleEndian.Uint64(b[n-8:n]), 'a', 'z'); m != 0 {
229 return n - 8 + swar.FirstLane(m)
230 }
231 return -1
232 }
233 for ; i < n; i++ {
234 if c := b[i]; c >= 'a' && c <= 'z' {
235 return i
236 }
237 }
238 return -1
239 }
240
241 // ToLowerInPlace lower-cases every ASCII uppercase byte of b in place:
242 // four words per pinned group, then single words, then bytes.
243 func ToLowerInPlace(b []byte) {
244 n := len(b)
245 i := 0
246 for ; i+32 <= n; i += 32 {
247 w := b[i : i+32 : i+32]
248 binary.LittleEndian.PutUint64(w[0:8], swar.ToLowerWord(binary.LittleEndian.Uint64(w[0:8])))
249 binary.LittleEndian.PutUint64(w[8:16], swar.ToLowerWord(binary.LittleEndian.Uint64(w[8:16])))
250 binary.LittleEndian.PutUint64(w[16:24], swar.ToLowerWord(binary.LittleEndian.Uint64(w[16:24])))
251 binary.LittleEndian.PutUint64(w[24:32], swar.ToLowerWord(binary.LittleEndian.Uint64(w[24:32])))
252 }
253 for ; i+8 <= n; i += 8 {
254 w := b[i : i+8 : i+8]
255 binary.LittleEndian.PutUint64(w, swar.ToLowerWord(binary.LittleEndian.Uint64(w)))
256 }
257 // Finish byte-wise: an overlapping word here would partially overlap the
258 // previous 8-byte store and stall on store-to-load forwarding.
259 for ; i < n; i++ {
260 b[i] = ToLowerTable[b[i]]
261 }
262 }
263
264 // ToUpperInPlace upper-cases every ASCII lowercase byte of b in place; the
265 // loop shape mirrors ToLowerInPlace.
266 func ToUpperInPlace(b []byte) {
267 n := len(b)
268 i := 0
269 for ; i+32 <= n; i += 32 {
270 w := b[i : i+32 : i+32]
271 binary.LittleEndian.PutUint64(w[0:8], swar.ToUpperWord(binary.LittleEndian.Uint64(w[0:8])))
272 binary.LittleEndian.PutUint64(w[8:16], swar.ToUpperWord(binary.LittleEndian.Uint64(w[8:16])))
273 binary.LittleEndian.PutUint64(w[16:24], swar.ToUpperWord(binary.LittleEndian.Uint64(w[16:24])))
274 binary.LittleEndian.PutUint64(w[24:32], swar.ToUpperWord(binary.LittleEndian.Uint64(w[24:32])))
275 }
276 for ; i+8 <= n; i += 8 {
277 w := b[i : i+8 : i+8]
278 binary.LittleEndian.PutUint64(w, swar.ToUpperWord(binary.LittleEndian.Uint64(w)))
279 }
280 for ; i < n; i++ {
281 b[i] = ToUpperTable[b[i]]
282 }
283 }