Debian Codegraph @

simd/ascii_amd64.s

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 // Modified: legacy-SSE MOVD/MOVQ register moves replaced with VEX-encoded
5 // VMOVD/VMOVQ to avoid AVX-SSE transition penalties on Intel CPUs, the
6 // scalar tail loop replaced with one overlapping vector at the buffer end
7 // for the MinLen+ inputs the Go dispatch guarantees, the 32-byte main loop
8 // replaced with a 4x unrolled 128-byte block, and firstNonASCIIAVX2 /
9 // countNonASCIIAVX2 added so the locating and counting scans no longer
10 // have to fall back to SWAR words on amd64.
11
12 //go:build amd64
13
14 #include "textflag.h"
15
16 // All three kernels share one observation: a byte is non-ASCII exactly when
17 // its high bit is set, and VPMOVMSKB gathers those 32 high bits into a
18 // scalar register in one instruction. OR-ing four vectors before the
19 // extraction therefore preserves "some byte in these 128 was non-ASCII"
20 // exactly, which is what lets the block loop test 128 bytes with a single
21 // port-0 VPMOVMSKB. Both loops test their bound at the bottom against a
22 // precomputed limit pointer.
23
24 // func isASCIIAVX2(data []byte) bool
25 //
26 // AVX2 implementation of ASCII detection that checks if all bytes are < 0x80.
27 //
28 // Algorithm:
29 // 1. Block loop: OR four 32-byte vectors, extract the high bits with
30 // VPMOVMSKB, and return false if any is set. The loads fold into the
31 // VPOR operands, so a block costs four memory ops and one extraction.
32 // 2. 32-byte loop for the remainder, then one overlapping 32-byte window
33 // ending at the buffer end (rescanning bytes already proven ASCII
34 // cannot change the outcome). A scalar loop serves only sub-32 direct
35 // calls, unreachable through the Go dispatch.
36 // 3. Always call VZEROUPPER before returning (critical for performance)
37 //
38 // Parameters (FP offsets):
39 // data_base+0(FP) - pointer to data (8 bytes)
40 // data_len+8(FP) - data length (8 bytes)
41 // data_cap+16(FP) - data capacity (8 bytes, unused but part of slice)
42 // ret+24(FP) - return value: bool (1 byte)
43 //
44 // Total argument frame size: 25 bytes (8+8+8+1)
45 TEXT ·isASCIIAVX2(SB), NOSPLIT, $0-25
46 // Load parameters
47 MOVQ data_base+0(FP), SI // SI = data pointer
48 MOVQ data_len+8(FP), DX // DX = data length
49
50 // Empty data check
51 TESTQ DX, DX
52 JZ all_ascii
53
54 // Calculate end pointer
55 LEAQ (SI)(DX*1), R8 // R8 = SI + length (end pointer)
56
57 CMPQ DX, $32
58 JB tail_loop // sub-32 direct call: scalar
59
60 LEAQ -32(R8), R12 // last valid 32-byte window base
61 LEAQ -128(R8), R11 // last valid 128-byte block base
62 CMPQ SI, R11
63 JA loop32_entry
64
65 loop128:
66 VMOVDQU (SI), Y0
67 VPOR 32(SI), Y0, Y0
68 VPOR 64(SI), Y0, Y0
69 VPOR 96(SI), Y0, Y0
70 VPMOVMSKB Y0, AX
71 TESTL AX, AX
72 JNZ found_non_ascii
73
74 ADDQ $128, SI
75 CMPQ SI, R11
76 JBE loop128
77
78 loop32_entry:
79 CMPQ SI, R12
80 JA last32
81
82 loop32:
83 // Load 32 bytes (unaligned load is safe and fast with AVX2) and
84 // extract the high bit of each byte into a 32-bit mask.
85 VMOVDQU (SI), Y0
86 VPMOVMSKB Y0, AX
87 TESTL AX, AX
88 JNZ found_non_ascii
89
90 ADDQ $32, SI
91 CMPQ SI, R12
92 JBE loop32
93
94 last32:
95 // Fewer than 32 bytes remain past SI; retest the final window.
96 CMPQ SI, R8
97 JAE all_ascii
98 VMOVDQU (R12), Y0
99 VPMOVMSKB Y0, AX
100 TESTL AX, AX
101 JNZ found_non_ascii
102 JMP all_ascii
103
104 tail_loop:
105 // Load one byte and check high bit
106 MOVBLZX (SI), AX // AX = data[SI] (zero-extended)
107 TESTB $0x80, AL // Check if high bit is set
108 JNZ found_non_ascii // If set, non-ASCII found
109
110 // Advance to next byte
111 INCQ SI
112 CMPQ SI, R8 // Check if reached end
113 JB tail_loop // Continue if SI < end
114
115 all_ascii:
116 // All bytes are ASCII
117 MOVB $1, ret+24(FP) // Return true
118 VZEROUPPER // Clear upper YMM bits (CRITICAL!)
119 RET
120
121 found_non_ascii:
122 MOVB $0, ret+24(FP) // Return false
123 VZEROUPPER // Clear upper YMM bits
124 RET
125
126 // func firstNonASCIIAVX2(data []byte) int
127 //
128 // AVX2 kernel behind FirstNonASCII: the same scan as isASCIIAVX2, but the
129 // block loop keeps the four source vectors live so the hit path can
130 // re-extract them in address order and BSF the first non-ASCII byte.
131 //
132 // Parameters (FP offsets):
133 // data_base+0(FP) - pointer to data (8 bytes)
134 // data_len+8(FP) - data length (8 bytes)
135 // data_cap+16(FP) - data capacity (8 bytes, unused)
136 // ret+24(FP) - return value: index or -1 (8 bytes)
137 TEXT ·firstNonASCIIAVX2(SB), NOSPLIT, $0-32
138 MOVQ data_base+0(FP), SI
139 MOVQ data_len+8(FP), DX
140
141 TESTQ DX, DX
142 JZ fna_not_found
143
144 MOVQ SI, DI // DI = data start (preserved)
145 LEAQ (SI)(DX*1), R8
146
147 CMPQ DX, $32
148 JB fna_tail_loop // sub-32 direct call: scalar
149
150 LEAQ -32(R8), R12
151 LEAQ -128(R8), R11
152 CMPQ SI, R11
153 JA fna_loop32_entry
154
155 fna_loop128:
156 VMOVDQU (SI), Y0
157 VMOVDQU 32(SI), Y1
158 VMOVDQU 64(SI), Y2
159 VMOVDQU 96(SI), Y3
160 VPOR Y0, Y1, Y4
161 VPOR Y2, Y3, Y5
162 VPOR Y4, Y5, Y4
163 VPMOVMSKB Y4, CX
164 TESTL CX, CX
165 JNZ fna_found_in_block
166
167 ADDQ $128, SI
168 CMPQ SI, R11
169 JBE fna_loop128
170
171 fna_loop32_entry:
172 CMPQ SI, R12
173 JA fna_last32
174
175 fna_loop32:
176 VMOVDQU (SI), Y0
177 VPMOVMSKB Y0, CX
178 TESTL CX, CX
179 JNZ fna_found_in_vector
180
181 ADDQ $32, SI
182 CMPQ SI, R12
183 JBE fna_loop32
184
185 fna_last32:
186 // Fewer than 32 bytes remain past SI; rescan the final window. Lanes
187 // before SI are known ASCII, so the first set lane falls in the new
188 // bytes.
189 CMPQ SI, R8
190 JAE fna_not_found
191 MOVQ R12, SI
192 VMOVDQU (SI), Y0
193 VPMOVMSKB Y0, CX
194 TESTL CX, CX
195 JNZ fna_found_in_vector
196 JMP fna_not_found
197
198 fna_tail_loop:
199 MOVBLZX (SI), AX
200 TESTB $0x80, AL
201 JNZ fna_found_scalar
202 INCQ SI
203 CMPQ SI, R8
204 JB fna_tail_loop
205
206 fna_not_found:
207 MOVQ $-1, AX
208 MOVQ AX, ret+24(FP)
209 VZEROUPPER
210 RET
211
212 fna_found_in_block:
213 VPMOVMSKB Y0, CX
214 TESTL CX, CX
215 JNZ fna_found_in_vector
216 ADDQ $32, SI
217 VPMOVMSKB Y1, CX
218 TESTL CX, CX
219 JNZ fna_found_in_vector
220 ADDQ $32, SI
221 VPMOVMSKB Y2, CX
222 TESTL CX, CX
223 JNZ fna_found_in_vector
224 ADDQ $32, SI
225 VPMOVMSKB Y3, CX
226
227 fna_found_in_vector:
228 BSFL CX, CX
229 SUBQ DI, SI
230 ADDQ SI, CX
231 MOVQ CX, ret+24(FP)
232 VZEROUPPER
233 RET
234
235 fna_found_scalar:
236 SUBQ DI, SI
237 MOVQ SI, ret+24(FP)
238 VZEROUPPER
239 RET
240
241 // func countNonASCIIAVX2(data []byte) int
242 //
243 // AVX2 kernel behind CountNonASCII. Counting is the one scan here that
244 // cannot finish with the overlapping final window the first-match kernels
245 // use — rescanning bytes would count them twice — so instead the vector
246 // loops stop at the last whole vector and a branch-free byte loop takes
247 // the remaining 0-31 bytes. That keeps the kernel self-bounding for any
248 // length, like every other kernel in this package: it never reads past
249 // data[len(data)-1], so no caller-side length preconditioning stands
250 // between it and an out-of-bounds read.
251 //
252 // Each vector's high bits become a 32-bit mask and POPCNT turns it into a
253 // lane count. Four masks are extracted before the four POPCNTs so the
254 // port-0 extractions and the port-1 population counts overlap; each POPCNT
255 // writes the register its own VPMOVMSKB just produced, so the destination
256 // false dependency Intel CPUs carry on POPCNT is already satisfied.
257 //
258 // POPCNT is a separate CPUID feature from AVX2, so countNonASCIIImpl gates
259 // on both; see cpu_amd64.go.
260 //
261 // Parameters (FP offsets):
262 // data_base+0(FP) - pointer to data (8 bytes)
263 // data_len+8(FP) - data length (8 bytes)
264 // data_cap+16(FP) - data capacity (8 bytes, unused)
265 // ret+24(FP) - number of bytes >= 0x80 (8 bytes)
266 TEXT ·countNonASCIIAVX2(SB), NOSPLIT, $0-32
267 MOVQ data_base+0(FP), SI
268 MOVQ data_len+8(FP), DX
269
270 XORQ R9, R9 // R9 = running count
271 XORQ R13, R13 // R13 = second accumulator
272
273 TESTQ DX, DX
274 JZ cna_done
275
276 LEAQ (SI)(DX*1), R8 // R8 = end pointer
277 CMPQ DX, $32
278 JB cna_tail // no whole vector: byte loop only
279
280 LEAQ -32(R8), R12 // last valid 32-byte window base
281 LEAQ -128(R8), R11 // last valid 128-byte block base
282 CMPQ SI, R11
283 JA cna_loop32_entry
284
285 cna_loop128:
286 VMOVDQU (SI), Y0
287 VMOVDQU 32(SI), Y1
288 VMOVDQU 64(SI), Y2
289 VMOVDQU 96(SI), Y3
290 VPMOVMSKB Y0, AX
291 VPMOVMSKB Y1, BX
292 VPMOVMSKB Y2, CX
293 VPMOVMSKB Y3, R10
294 POPCNTL AX, AX
295 POPCNTL BX, BX
296 POPCNTL CX, CX
297 POPCNTL R10, R10
298 ADDQ AX, R9
299 ADDQ BX, R13
300 ADDQ CX, R9
301 ADDQ R10, R13
302
303 ADDQ $128, SI
304 CMPQ SI, R11
305 JBE cna_loop128
306
307 cna_loop32_entry:
308 CMPQ SI, R12
309 JA cna_tail
310
311 cna_loop32:
312 VMOVDQU (SI), Y0
313 VPMOVMSKB Y0, AX
314 POPCNTL AX, AX
315 ADDQ AX, R9
316 ADDQ $32, SI
317 CMPQ SI, R12
318 JBE cna_loop32
319
320 cna_tail:
321 // 0-31 bytes left. Shifting the byte down to its high bit turns the
322 // test into an add, so the loop carries no data-dependent branch.
323 CMPQ SI, R8
324 JAE cna_done
325 MOVBLZX (SI), AX
326 SHRL $7, AX
327 ADDQ AX, R9
328 INCQ SI
329 JMP cna_tail
330
331 cna_done:
332 ADDQ R13, R9
333 MOVQ R9, ret+24(FP)
334 VZEROUPPER
335 RET