Debian Codegraph @

simd/ascii.go

raw
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 "math/bits"
9
10 "github.com/gofiber/utils/v2/swar"
11 )
12
13 // IsASCII reports whether every byte in data is ASCII (< 0x80). An empty
14 // slice is trivially ASCII.
15 //
16 // On amd64 with AVX2 and inputs of MinLen+ bytes it ORs four 32-byte
17 // vectors and tests all 128 bytes with a single VPMOVMSKB; elsewhere it
18 // uses a SWAR loop over 8-byte words.
19 func IsASCII(data []byte) bool {
20 if len(data) == 0 {
21 return true
22 }
23 return isASCIIImpl(data)
24 }
25
26 // FirstNonASCII returns the index of the first byte >= 0x80 in data, or -1
27 // if data is all ASCII. Use it to find where UTF-8 sequences begin.
28 //
29 // It reads the same signal as IsASCII — a non-ASCII byte is exactly a byte
30 // with its high bit set — so on amd64 with AVX2 and inputs of MinLen+
31 // bytes it also tests 128 bytes per VPMOVMSKB, keeping the four vectors
32 // live so the hit path can pinpoint the byte; elsewhere it falls back to
33 // SWAR words.
34 func FirstNonASCII(data []byte) int {
35 if len(data) == 0 {
36 return -1
37 }
38 return firstNonASCIIImpl(data)
39 }
40
41 // CountNonASCII returns the number of bytes >= 0x80 in data.
42 //
43 // On amd64 with AVX2 and inputs of MinLen+ bytes it accumulates a per-lane
44 // count of high-bit bytes in vector byte lanes, four 32-byte vectors per
45 // iteration, sums the lanes once at the end, and population-counts the
46 // high-bit masks of the remaining whole vectors; elsewhere it counts
47 // SWAR-word-wise with a popcount per word.
48 func CountNonASCII(data []byte) int {
49 return countNonASCIIImpl(data)
50 }
51
52 // The unrolled loops below all pin their group of words with a
53 // three-index reslice before loading from it. swar.Load8's own reslice is
54 // bounds-checked against the backing array's capacity, which the loop
55 // condition (stated in terms of length) does not imply, so a variable
56 // index costs a compare, a branch and a four-instruction empty-slice
57 // pointer clamp per load. Pinning the window once pays that at group
58 // granularity and leaves the loads inside it at constant offsets, where
59 // the checks fold away entirely: measured -20% at 32B rising to -62% at
60 // 4KiB for the ASCII scan, and the same shape for the first-match scans.
61
62 // isASCIIGeneric is the portable SWAR implementation behind IsASCII. No
63 // index is needed, so detection can be deferred: whole groups of words are
64 // OR-ed into one accumulator and tested once, four words per branch and
65 // then two, which is what turns the per-word test into a per-32-byte one.
66 // Inputs of 8..15 bytes skip the loops entirely — two overlapping words
67 // already cover them — and every 8+ byte input finishes with one
68 // overlapping word at n-8.
69 func isASCIIGeneric(data []byte) bool {
70 n := len(data)
71 if n < swar.WordLen {
72 for i := range n {
73 if data[i] >= 0x80 {
74 return false
75 }
76 }
77 return true
78 }
79 if n < 2*swar.WordLen {
80 return (swar.Load8(data, 0)|swar.Load8(data, n-swar.WordLen))&swar.HighBits == 0
81 }
82 i := 0
83 for ; i+4*swar.WordLen <= n; i += 4 * swar.WordLen {
84 w := data[i : i+4*swar.WordLen : i+4*swar.WordLen]
85 acc := swar.Load8(w, 0) |
86 swar.Load8(w, swar.WordLen) |
87 swar.Load8(w, 2*swar.WordLen) |
88 swar.Load8(w, 3*swar.WordLen)
89 if acc&swar.HighBits != 0 {
90 return false
91 }
92 }
93 for ; i+2*swar.WordLen <= n; i += 2 * swar.WordLen {
94 w := data[i : i+2*swar.WordLen : i+2*swar.WordLen]
95 if (swar.Load8(w, 0)|swar.Load8(w, swar.WordLen))&swar.HighBits != 0 {
96 return false
97 }
98 }
99 for ; i+swar.WordLen <= n; i += swar.WordLen {
100 if swar.Load8(data, i)&swar.HighBits != 0 {
101 return false
102 }
103 }
104 if i == n {
105 return true
106 }
107 return swar.Load8(data, n-swar.WordLen)&swar.HighBits == 0
108 }
109
110 // firstNonASCIIGeneric is the portable SWAR implementation behind
111 // FirstNonASCII: two words per branch, then one overlapping word at n-8.
112 func firstNonASCIIGeneric(data []byte) int {
113 n := len(data)
114 if n < swar.WordLen {
115 for i := range n {
116 if data[i] >= 0x80 {
117 return i
118 }
119 }
120 return -1
121 }
122 i := 0
123 for ; i+2*swar.WordLen <= n; i += 2 * swar.WordLen {
124 w := data[i : i+2*swar.WordLen : i+2*swar.WordLen]
125 m0 := swar.Load8(w, 0) & swar.HighBits
126 m1 := swar.Load8(w, swar.WordLen) & swar.HighBits
127 if m0|m1 != 0 {
128 if m0 != 0 {
129 return i + swar.FirstLane(m0)
130 }
131 return i + swar.WordLen + swar.FirstLane(m1)
132 }
133 }
134 for ; i+swar.WordLen <= n; i += swar.WordLen {
135 if m := swar.Load8(data, i) & swar.HighBits; m != 0 {
136 return i + swar.FirstLane(m)
137 }
138 }
139 if i == n {
140 return -1
141 }
142 // Overlapping final word: lanes before i are known ASCII, so any set
143 // lane falls in the new bytes.
144 if m := swar.Load8(data, n-swar.WordLen) & swar.HighBits; m != 0 {
145 return n - swar.WordLen + swar.FirstLane(m)
146 }
147 return -1
148 }
149
150 // countNonASCIIGeneric is the portable SWAR implementation behind
151 // CountNonASCII: a popcount of the 0x80 lane bits per word, four words per
152 // pinned window. Every word has to be visited whatever happens, so unlike
153 // the first-match scans there is no branch to amortize — the win here is
154 // purely the bounds checks the window removes.
155 func countNonASCIIGeneric(data []byte) int {
156 count := 0
157 i := 0
158 n := len(data)
159 for ; i+4*swar.WordLen <= n; i += 4 * swar.WordLen {
160 w := data[i : i+4*swar.WordLen : i+4*swar.WordLen]
161 count += bits.OnesCount64(swar.Load8(w, 0)&swar.HighBits) +
162 bits.OnesCount64(swar.Load8(w, swar.WordLen)&swar.HighBits) +
163 bits.OnesCount64(swar.Load8(w, 2*swar.WordLen)&swar.HighBits) +
164 bits.OnesCount64(swar.Load8(w, 3*swar.WordLen)&swar.HighBits)
165 }
166 for ; i+swar.WordLen <= n; i += swar.WordLen {
167 count += bits.OnesCount64(swar.Load8(data, i) & swar.HighBits)
168 }
169 // No overlapping word here: it would double-count. The tail is at most
170 // seven bytes.
171 for ; i < n; i++ {
172 if data[i] >= 0x80 {
173 count++
174 }
175 }
176 return count
177 }