Debian Codegraph @

simd/ascii.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 "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 turns each 32-byte
44 // vector into a high-bit mask and adds its population count; elsewhere it
45 // counts SWAR-word-wise with a popcount per word.
46 func CountNonASCII(data []byte) int {
47 return countNonASCIIImpl(data)
48 }
49
50 // The unrolled loops below all pin their group of words with a
51 // three-index reslice before loading from it. swar.Load8's own reslice is
52 // bounds-checked against the backing array's capacity, which the loop
53 // condition (stated in terms of length) does not imply, so a variable
54 // index costs a compare, a branch and a four-instruction empty-slice
55 // pointer clamp per load. Pinning the window once pays that at group
56 // granularity and leaves the loads inside it at constant offsets, where
57 // the checks fold away entirely: measured -20% at 32B rising to -62% at
58 // 4KiB for the ASCII scan, and the same shape for the first-match scans.
59
60 // isASCIIGeneric is the portable SWAR implementation behind IsASCII. No
61 // index is needed, so detection can be deferred: whole groups of words are
62 // OR-ed into one accumulator and tested once, four words per branch and
63 // then two, which is what turns the per-word test into a per-32-byte one.
64 // Inputs of 8..15 bytes skip the loops entirely — two overlapping words
65 // already cover them — and every 8+ byte input finishes with one
66 // overlapping word at n-8.
67 func isASCIIGeneric(data []byte) bool {
68 n := len(data)
69 if n < swar.WordLen {
70 for i := range n {
71 if data[i] >= 0x80 {
72 return false
73 }
74 }
75 return true
76 }
77 if n < 2*swar.WordLen {
78 return (swar.Load8(data, 0)|swar.Load8(data, n-swar.WordLen))&swar.HighBits == 0
79 }
80 i := 0
81 for ; i+4*swar.WordLen <= n; i += 4 * swar.WordLen {
82 w := data[i : i+4*swar.WordLen : i+4*swar.WordLen]
83 acc := swar.Load8(w, 0) |
84 swar.Load8(w, swar.WordLen) |
85 swar.Load8(w, 2*swar.WordLen) |
86 swar.Load8(w, 3*swar.WordLen)
87 if acc&swar.HighBits != 0 {
88 return false
89 }
90 }
91 for ; i+2*swar.WordLen <= n; i += 2 * swar.WordLen {
92 w := data[i : i+2*swar.WordLen : i+2*swar.WordLen]
93 if (swar.Load8(w, 0)|swar.Load8(w, swar.WordLen))&swar.HighBits != 0 {
94 return false
95 }
96 }
97 for ; i+swar.WordLen <= n; i += swar.WordLen {
98 if swar.Load8(data, i)&swar.HighBits != 0 {
99 return false
100 }
101 }
102 if i == n {
103 return true
104 }
105 return swar.Load8(data, n-swar.WordLen)&swar.HighBits == 0
106 }
107
108 // firstNonASCIIGeneric is the portable SWAR implementation behind
109 // FirstNonASCII: two words per branch, then one overlapping word at n-8.
110 func firstNonASCIIGeneric(data []byte) int {
111 n := len(data)
112 if n < swar.WordLen {
113 for i := range n {
114 if data[i] >= 0x80 {
115 return i
116 }
117 }
118 return -1
119 }
120 i := 0
121 for ; i+2*swar.WordLen <= n; i += 2 * swar.WordLen {
122 w := data[i : i+2*swar.WordLen : i+2*swar.WordLen]
123 m0 := swar.Load8(w, 0) & swar.HighBits
124 m1 := swar.Load8(w, swar.WordLen) & swar.HighBits
125 if m0|m1 != 0 {
126 if m0 != 0 {
127 return i + swar.FirstLane(m0)
128 }
129 return i + swar.WordLen + swar.FirstLane(m1)
130 }
131 }
132 for ; i+swar.WordLen <= n; i += swar.WordLen {
133 if m := swar.Load8(data, i) & swar.HighBits; m != 0 {
134 return i + swar.FirstLane(m)
135 }
136 }
137 if i == n {
138 return -1
139 }
140 // Overlapping final word: lanes before i are known ASCII, so any set
141 // lane falls in the new bytes.
142 if m := swar.Load8(data, n-swar.WordLen) & swar.HighBits; m != 0 {
143 return n - swar.WordLen + swar.FirstLane(m)
144 }
145 return -1
146 }
147
148 // countNonASCIIGeneric is the portable SWAR implementation behind
149 // CountNonASCII: a popcount of the 0x80 lane bits per word, four words per
150 // pinned window. Every word has to be visited whatever happens, so unlike
151 // the first-match scans there is no branch to amortize — the win here is
152 // purely the bounds checks the window removes.
153 func countNonASCIIGeneric(data []byte) int {
154 count := 0
155 i := 0
156 n := len(data)
157 for ; i+4*swar.WordLen <= n; i += 4 * swar.WordLen {
158 w := data[i : i+4*swar.WordLen : i+4*swar.WordLen]
159 count += bits.OnesCount64(swar.Load8(w, 0)&swar.HighBits) +
160 bits.OnesCount64(swar.Load8(w, swar.WordLen)&swar.HighBits) +
161 bits.OnesCount64(swar.Load8(w, 2*swar.WordLen)&swar.HighBits) +
162 bits.OnesCount64(swar.Load8(w, 3*swar.WordLen)&swar.HighBits)
163 }
164 for ; i+swar.WordLen <= n; i += swar.WordLen {
165 count += bits.OnesCount64(swar.Load8(data, i) & swar.HighBits)
166 }
167 // No overlapping word here: it would double-count. The tail is at most
168 // seven bytes.
169 for ; i < n; i++ {
170 if data[i] >= 0x80 {
171 count++
172 }
173 }
174 return count
175 }