Debian Codegraph @

internal/caseconv/swar.go

raw symbols SCIP
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 // WordLen is the SWAR word width in bytes, re-exported for the case
21 // conversion callers in the strings and bytes packages: they route inputs
22 // shorter than WordLen to byte-wise paths and align offsets they pass (such
23 // as ToLowerCopy's from) with WordLen-derived masks.
24 const WordLen = swar.WordLen
25
26 // FirstUpperIndex returns the index of the first ASCII uppercase byte in b,
27 // or -1 if b contains none. b is only ever read; it may be an unsafe view
28 // over immutable string memory.
29 func FirstUpperIndex(b []byte) int {
30 n := len(b)
31 i := 0
32 if n >= 32 {
33 // Check the first word alone: canonical mixed-case values
34 // ("Content-Type") change case right at the start, and that path
35 // must stay one mask.
36 if m := swar.MatchRangeMask(binary.LittleEndian.Uint64(b[0:8]), 'A', 'Z'); m != 0 {
37 return swar.FirstLane(m)
38 }
39 // Then four words per branch: the masks compute independently, so
40 // the common no-uppercase case pays one test per 32 bytes. The
41 // MatchRangeMask computations stay inline so their constant ranges
42 // fold; only the cold hit-resolution ladder is shared, and it
43 // inlines (the no-match loop never reaches it).
44 for i = 8; i+32 <= n; i += 32 {
45 m0 := swar.MatchRangeMask(binary.LittleEndian.Uint64(b[i:i+8]), 'A', 'Z')
46 m1 := swar.MatchRangeMask(binary.LittleEndian.Uint64(b[i+8:i+16]), 'A', 'Z')
47 m2 := swar.MatchRangeMask(binary.LittleEndian.Uint64(b[i+16:i+24]), 'A', 'Z')
48 m3 := swar.MatchRangeMask(binary.LittleEndian.Uint64(b[i+24:i+32]), 'A', 'Z')
49 if m0|m1|m2|m3 != 0 {
50 return firstSetLane32(i, m0, m1, m2, m3)
51 }
52 }
53 if i == n {
54 return -1
55 }
56 // Finish with one overlapping block at n-32. MatchRangeMask is
57 // per-lane exact, so re-scanned lanes are known non-matching and the
58 // first set lane always falls in the new bytes.
59 i = n - 32
60 m0 := swar.MatchRangeMask(binary.LittleEndian.Uint64(b[i:i+8]), 'A', 'Z')
61 m1 := swar.MatchRangeMask(binary.LittleEndian.Uint64(b[i+8:i+16]), 'A', 'Z')
62 m2 := swar.MatchRangeMask(binary.LittleEndian.Uint64(b[i+16:i+24]), 'A', 'Z')
63 m3 := swar.MatchRangeMask(binary.LittleEndian.Uint64(b[i+24:i+32]), 'A', 'Z')
64 if m0|m1|m2|m3 != 0 {
65 return firstSetLane32(i, m0, m1, m2, m3)
66 }
67 return -1
68 }
69 for ; i+8 <= n; i += 8 {
70 if m := swar.MatchRangeMask(binary.LittleEndian.Uint64(b[i:i+8]), 'A', 'Z'); m != 0 {
71 return i + swar.FirstLane(m)
72 }
73 }
74 if i == n {
75 return -1
76 }
77 if n >= 8 {
78 // Scan the tail with one overlapping word; lanes that were already
79 // scanned are known to be zero, so FirstLane lands in the new bytes.
80 if m := swar.MatchRangeMask(binary.LittleEndian.Uint64(b[n-8:n]), 'A', 'Z'); m != 0 {
81 return n - 8 + swar.FirstLane(m)
82 }
83 return -1
84 }
85 for ; i < n; i++ {
86 if c := b[i]; c >= 'A' && c <= 'Z' {
87 return i
88 }
89 }
90 return -1
91 }
92
93 // FirstLowerIndex returns the index of the first ASCII lowercase byte in b,
94 // or -1 if b contains none. b is only ever read; it may be an unsafe view
95 // over immutable string memory.
96 // firstSetLane32 resolves a hit inside a 32-byte block: given the block's
97 // four per-word masks, it returns the index of the first marked byte
98 // relative to base, the block's starting offset. It runs at most once per
99 // scan — only after a block's combined mask tested non-zero — and is small
100 // enough to inline, so the hot no-match loops pay nothing for the sharing.
101 func firstSetLane32(base int, m0, m1, m2, m3 uint64) int {
102 if m0 != 0 {
103 return base + swar.FirstLane(m0)
104 }
105 if m1 != 0 {
106 return base + 8 + swar.FirstLane(m1)
107 }
108 if m2 != 0 {
109 return base + 16 + swar.FirstLane(m2)
110 }
111 return base + 24 + swar.FirstLane(m3)
112 }
113
114 func FirstLowerIndex(b []byte) int {
115 n := len(b)
116 i := 0
117 if n >= 32 {
118 // Structure and rationale mirror FirstUpperIndex.
119 if m := swar.MatchRangeMask(binary.LittleEndian.Uint64(b[0:8]), 'a', 'z'); m != 0 {
120 return swar.FirstLane(m)
121 }
122 for i = 8; i+32 <= n; i += 32 {
123 m0 := swar.MatchRangeMask(binary.LittleEndian.Uint64(b[i:i+8]), 'a', 'z')
124 m1 := swar.MatchRangeMask(binary.LittleEndian.Uint64(b[i+8:i+16]), 'a', 'z')
125 m2 := swar.MatchRangeMask(binary.LittleEndian.Uint64(b[i+16:i+24]), 'a', 'z')
126 m3 := swar.MatchRangeMask(binary.LittleEndian.Uint64(b[i+24:i+32]), 'a', 'z')
127 if m0|m1|m2|m3 != 0 {
128 return firstSetLane32(i, m0, m1, m2, m3)
129 }
130 }
131 if i == n {
132 return -1
133 }
134 i = n - 32
135 m0 := swar.MatchRangeMask(binary.LittleEndian.Uint64(b[i:i+8]), 'a', 'z')
136 m1 := swar.MatchRangeMask(binary.LittleEndian.Uint64(b[i+8:i+16]), 'a', 'z')
137 m2 := swar.MatchRangeMask(binary.LittleEndian.Uint64(b[i+16:i+24]), 'a', 'z')
138 m3 := swar.MatchRangeMask(binary.LittleEndian.Uint64(b[i+24:i+32]), 'a', 'z')
139 if m0|m1|m2|m3 != 0 {
140 return firstSetLane32(i, m0, m1, m2, m3)
141 }
142 return -1
143 }
144 for ; i+8 <= n; i += 8 {
145 if m := swar.MatchRangeMask(binary.LittleEndian.Uint64(b[i:i+8]), 'a', 'z'); m != 0 {
146 return i + swar.FirstLane(m)
147 }
148 }
149 if i == n {
150 return -1
151 }
152 if n >= 8 {
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 // ToLowerInPlace lower-cases every ASCII uppercase byte of b in place.
167 func ToLowerInPlace(b []byte) {
168 n := len(b)
169 i := 0
170 for ; i+8 <= n; i += 8 {
171 binary.LittleEndian.PutUint64(b[i:i+8], swar.ToLowerWord(binary.LittleEndian.Uint64(b[i:i+8])))
172 }
173 // Finish byte-wise: an overlapping word here would partially overlap the
174 // previous 8-byte store and stall on store-to-load forwarding.
175 for ; i < n; i++ {
176 b[i] = ToLowerTable[b[i]]
177 }
178 }
179
180 // ToUpperInPlace upper-cases every ASCII lowercase byte of b in place.
181 func ToUpperInPlace(b []byte) {
182 n := len(b)
183 i := 0
184 for ; i+8 <= n; i += 8 {
185 binary.LittleEndian.PutUint64(b[i:i+8], swar.ToUpperWord(binary.LittleEndian.Uint64(b[i:i+8])))
186 }
187 for ; i < n; i++ {
188 b[i] = ToUpperTable[b[i]]
189 }
190 }
191
192 // ToLowerCopy writes the lower-cased content of src into dst starting at
193 // byte offset from. Bytes before from must already be present in dst, and
194 // len(dst) must equal len(src). src is only ever read — the strings package
195 // passes an unsafe view over a string's immutable backing memory — so no
196 // code path here may write through src.
197 //
198 // from must be a multiple of WordLen and at or below the index of the first
199 // byte that lower-casing changes: when len(src) >= WordLen the overlapping
200 // tail store rewrites dst[n-WordLen:from) with case-converted bytes, which is
201 // only a no-op under that precondition.
202 //
203 // Callers currently invoke this only with len(src) >= WordLen; the byte-wise
204 // tail below keeps the helper correct standalone for shorter inputs.
205 func ToLowerCopy(dst, src []byte, from int) {
206 n := len(src)
207 i := from
208 for ; i+8 <= n; i += 8 {
209 binary.LittleEndian.PutUint64(dst[i:i+8], swar.ToLowerWord(binary.LittleEndian.Uint64(src[i:i+8])))
210 }
211 if i == n {
212 return
213 }
214 if n >= 8 {
215 binary.LittleEndian.PutUint64(dst[n-8:n], swar.ToLowerWord(binary.LittleEndian.Uint64(src[n-8:n])))
216 return
217 }
218 for ; i < n; i++ {
219 dst[i] = ToLowerTable[src[i]]
220 }
221 }
222
223 // ToUpperCopy writes the upper-cased content of src into dst starting at
224 // byte offset from. Bytes before from must already be present in dst, and
225 // len(dst) must equal len(src).
226 //
227 // from must be a multiple of WordLen and at or below the index of the first
228 // byte that upper-casing changes; see ToLowerCopy for why, including the
229 // note on the byte-wise tail and the requirement that src is never written.
230 func ToUpperCopy(dst, src []byte, from int) {
231 n := len(src)
232 i := from
233 for ; i+8 <= n; i += 8 {
234 binary.LittleEndian.PutUint64(dst[i:i+8], swar.ToUpperWord(binary.LittleEndian.Uint64(src[i:i+8])))
235 }
236 if i == n {
237 return
238 }
239 if n >= 8 {
240 binary.LittleEndian.PutUint64(dst[n-8:n], swar.ToUpperWord(binary.LittleEndian.Uint64(src[n-8:n])))
241 return
242 }
243 for ; i < n; i++ {
244 dst[i] = ToUpperTable[src[i]]
245 }
246 }