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 (the counting kernel's block
11 // loop accumulating in vector byte lanes rather than extracting and
12 // population-counting every vector's mask).
13
14 //go:build amd64
15
16 #include "textflag.h"
17 #include "block_align_amd64.h"
18
19 // All three kernels share one observation: a byte is non-ASCII exactly when
20 // its high bit is set, and VPMOVMSKB gathers those 32 high bits into a
21 // scalar register in one instruction. OR-ing four vectors before the
22 // extraction therefore preserves "some byte in these 128 was non-ASCII"
23 // exactly, which is what lets the block loop test 128 bytes with a single
24 // port-0 VPMOVMSKB. Both loops test their bound at the bottom against a
25 // precomputed limit pointer.
26
27 // func isASCIIAVX2(data []byte) bool
28 //
29 // AVX2 implementation of ASCII detection that checks if all bytes are < 0x80.
30 //
31 // Algorithm:
32 // 1. Block loop: OR four 32-byte vectors, extract the high bits with
33 // VPMOVMSKB, and return false if any is set. The loads fold into the
34 // VPOR operands, so a block costs four memory ops and one extraction.
35 // 2. 32-byte loop for the remainder, then one overlapping 32-byte window
36 // ending at the buffer end (rescanning bytes already proven ASCII
37 // cannot change the outcome). A scalar loop serves only sub-32 direct
38 // calls, unreachable through the Go dispatch.
39 // 3. Always call VZEROUPPER before returning (critical for performance)
40 //
41 // Parameters (FP offsets):
42 // data_base+0(FP) - pointer to data (8 bytes)
43 // data_len+8(FP) - data length (8 bytes)
44 // data_cap+16(FP) - data capacity (8 bytes, unused but part of slice)
45 // ret+24(FP) - return value: bool (1 byte)
46 //
47 // Total argument frame size: 25 bytes (8+8+8+1)
48 TEXT ·isASCIIAVX2(SB), NOSPLIT, $0-25
49 // Load parameters
50 MOVQ data_base+0(FP), SI // SI = data pointer
51 MOVQ data_len+8(FP), DX // DX = data length
52
53 // Empty data check
54 TESTQ DX, DX
55 JZ all_ascii
56
57 // Calculate end pointer
58 LEAQ (SI)(DX*1), R8 // R8 = SI + length (end pointer)
59
60 CMPQ DX, $32
61 JB tail_loop // sub-32 direct call: scalar
62
63 LEAQ -32(R8), R12 // last valid 32-byte window base
64 LEAQ -128(R8), R11 // last valid 128-byte block base
65 CMPQ SI, R11
66 JA loop32_entry
67
68 BLOCKALIGN
69 loop128:
70 VMOVDQU (SI), Y0
71 VPOR 32(SI), Y0, Y0
72 VPOR 64(SI), Y0, Y0
73 VPOR 96(SI), Y0, Y0
74 VPMOVMSKB Y0, AX
75 TESTL AX, AX
76 JNZ found_non_ascii
77
78 ADDQ $128, SI
79 CMPQ SI, R11
80 JBE loop128
81
82 loop32_entry:
83 CMPQ SI, R12
84 JA last32
85
86 loop32:
87 // Load 32 bytes (unaligned load is safe and fast with AVX2) and
88 // extract the high bit of each byte into a 32-bit mask.
89 VMOVDQU (SI), Y0
90 VPMOVMSKB Y0, AX
91 TESTL AX, AX
92 JNZ found_non_ascii
93
94 ADDQ $32, SI
95 CMPQ SI, R12
96 JBE loop32
97
98 last32:
99 // Fewer than 32 bytes remain past SI; retest the final window.
100 CMPQ SI, R8
101 JAE all_ascii
102 VMOVDQU (R12), Y0
103 VPMOVMSKB Y0, AX
104 TESTL AX, AX
105 JNZ found_non_ascii
106 JMP all_ascii
107
108 tail_loop:
109 // Load one byte and check high bit
110 MOVBLZX (SI), AX // AX = data[SI] (zero-extended)
111 TESTB $0x80, AL // Check if high bit is set
112 JNZ found_non_ascii // If set, non-ASCII found
113
114 // Advance to next byte
115 INCQ SI
116 CMPQ SI, R8 // Check if reached end
117 JB tail_loop // Continue if SI < end
118
119 all_ascii:
120 // All bytes are ASCII
121 MOVB $1, ret+24(FP) // Return true
122 VZEROUPPER // Clear upper YMM bits (CRITICAL!)
123 RET
124
125 found_non_ascii:
126 MOVB $0, ret+24(FP) // Return false
127 VZEROUPPER // Clear upper YMM bits
128 RET
129
130 // func firstNonASCIIAVX2(data []byte) int
131 //
132 // AVX2 kernel behind FirstNonASCII: the same scan as isASCIIAVX2, but the
133 // block loop keeps the four source vectors live so the hit path can
134 // re-extract them in address order and BSF the first non-ASCII byte.
135 //
136 // Parameters (FP offsets):
137 // data_base+0(FP) - pointer to data (8 bytes)
138 // data_len+8(FP) - data length (8 bytes)
139 // data_cap+16(FP) - data capacity (8 bytes, unused)
140 // ret+24(FP) - return value: index or -1 (8 bytes)
141 TEXT ·firstNonASCIIAVX2(SB), NOSPLIT, $0-32
142 MOVQ data_base+0(FP), SI
143 MOVQ data_len+8(FP), DX
144
145 TESTQ DX, DX
146 JZ fna_not_found
147
148 MOVQ SI, DI // DI = data start (preserved)
149 LEAQ (SI)(DX*1), R8
150
151 CMPQ DX, $32
152 JB fna_tail_loop // sub-32 direct call: scalar
153
154 LEAQ -32(R8), R12
155 LEAQ -128(R8), R11
156 CMPQ SI, R11
157 JA fna_loop32_entry
158
159 BLOCKALIGN
160 fna_loop128:
161 VMOVDQU (SI), Y0
162 VMOVDQU 32(SI), Y1
163 VMOVDQU 64(SI), Y2
164 VMOVDQU 96(SI), Y3
165 VPOR Y0, Y1, Y4
166 VPOR Y2, Y3, Y5
167 VPOR Y4, Y5, Y4
168 VPMOVMSKB Y4, CX
169 TESTL CX, CX
170 JNZ fna_found_in_block
171
172 ADDQ $128, SI
173 CMPQ SI, R11
174 JBE fna_loop128
175
176 fna_loop32_entry:
177 CMPQ SI, R12
178 JA fna_last32
179
180 fna_loop32:
181 VMOVDQU (SI), Y0
182 VPMOVMSKB Y0, CX
183 TESTL CX, CX
184 JNZ fna_found_in_vector
185
186 ADDQ $32, SI
187 CMPQ SI, R12
188 JBE fna_loop32
189
190 fna_last32:
191 // Fewer than 32 bytes remain past SI; rescan the final window. Lanes
192 // before SI are known ASCII, so the first set lane falls in the new
193 // bytes.
194 CMPQ SI, R8
195 JAE fna_not_found
196 MOVQ R12, SI
197 VMOVDQU (SI), Y0
198 VPMOVMSKB Y0, CX
199 TESTL CX, CX
200 JNZ fna_found_in_vector
201 JMP fna_not_found
202
203 fna_tail_loop:
204 MOVBLZX (SI), AX
205 TESTB $0x80, AL
206 JNZ fna_found_scalar
207 INCQ SI
208 CMPQ SI, R8
209 JB fna_tail_loop
210
211 fna_not_found:
212 MOVQ $-1, AX
213 MOVQ AX, ret+24(FP)
214 VZEROUPPER
215 RET
216
217 fna_found_in_block:
218 VPMOVMSKB Y0, CX
219 TESTL CX, CX
220 JNZ fna_found_in_vector
221 ADDQ $32, SI
222 VPMOVMSKB Y1, CX
223 TESTL CX, CX
224 JNZ fna_found_in_vector
225 ADDQ $32, SI
226 VPMOVMSKB Y2, CX
227 TESTL CX, CX
228 JNZ fna_found_in_vector
229 ADDQ $32, SI
230 VPMOVMSKB Y3, CX
231
232 fna_found_in_vector:
233 BSFL CX, CX
234 SUBQ DI, SI
235 ADDQ SI, CX
236 MOVQ CX, ret+24(FP)
237 VZEROUPPER
238 RET
239
240 fna_found_scalar:
241 SUBQ DI, SI
242 MOVQ SI, ret+24(FP)
243 VZEROUPPER
244 RET
245
246 // func countNonASCIIAVX2(data []byte) int
247 //
248 // AVX2 kernel behind CountNonASCII. Counting is the one scan here that
249 // cannot finish with the overlapping final window the first-match kernels
250 // use — rescanning bytes would count them twice — so instead the vector
251 // loops stop at the last whole vector and a branch-free byte loop takes
252 // the remaining 0-31 bytes. That keeps the kernel self-bounding for any
253 // length, like every other kernel in this package: it never reads past
254 // data[len(data)-1], so no caller-side length preconditioning stands
255 // between it and an out-of-bounds read.
256 //
257 // The block loop accumulates in vector byte lanes rather than extracting
258 // a mask per vector. A signed compare against zero turns each non-ASCII
259 // byte (the only bytes that read as negative) into 0xFF, and subtracting
260 // that lane from a byte accumulator adds one to it; the compare takes its
261 // input straight from memory, so a vector costs two vector ops and no
262 // scalar work. An earlier version extracted every vector's high bits
263 // with VPMOVMSKB and population-counted them, four extractions per block
264 // on the one port that executes them, which held the block loop to about
265 // half the throughput of the ASCII scans. Two accumulators alternate so
266 // each carries a two-op dependency chain per block.
267 //
268 // A byte lane can hold 255 and gains at most two per block, so the block
269 // loop flushes both accumulators every 127 blocks: VPSADBW against zero
270 // sums each accumulator's bytes into four 64-bit lanes, which are added
271 // to a running vector total. After the last block the totals are reduced
272 // to one scalar. That reduction is a fixed ten-cycle latency chain, which
273 // the 1-3 vectors left after the block loop (or the 1-3 vectors of a
274 // sub-block input) could not amortize, so those are still extracted with
275 // VPMOVMSKB and population-counted, exactly as before; each POPCNT writes
276 // the register its own VPMOVMSKB just produced, so the destination false
277 // dependency Intel CPUs carry on POPCNT is already satisfied. Inputs
278 // below one block therefore never touch the vector accumulators.
279 //
280 // POPCNT is a separate CPUID feature from AVX2, so countNonASCIIImpl gates
281 // on both; see cpu_amd64.go.
282 //
283 // Parameters (FP offsets):
284 // data_base+0(FP) - pointer to data (8 bytes)
285 // data_len+8(FP) - data length (8 bytes)
286 // data_cap+16(FP) - data capacity (8 bytes, unused)
287 // ret+24(FP) - number of bytes >= 0x80 (8 bytes)
288
289 // FLUSH folds the two byte accumulators Y4 and Y5 into the 64-bit lane
290 // totals in Y8 and clears them. Y7 is the zero vector; Y0 and Y1 are
291 // scratch.
292 #define FLUSH \
293 VPSADBW Y7, Y4, Y0; \
294 VPSADBW Y7, Y5, Y1; \
295 VPADDQ Y0, Y8, Y8; \
296 VPADDQ Y1, Y8, Y8; \
297 VPXOR Y4, Y4, Y4; \
298 VPXOR Y5, Y5, Y5
299
300 TEXT ·countNonASCIIAVX2(SB), NOSPLIT, $0-32
301 MOVQ data_base+0(FP), SI
302 MOVQ data_len+8(FP), DX
303
304 XORQ R9, R9 // R9 = block loop total
305 XORQ R10, R10 // R10 = remainder total (kept apart so
306 // the remainder need not wait for the
307 // vector reduction)
308
309 TESTQ DX, DX
310 JZ cna_done
311
312 LEAQ (SI)(DX*1), R8 // R8 = end pointer
313 CMPQ DX, $32
314 JB cna_tail // no whole vector: byte loop only
315
316 LEAQ -32(R8), R12 // last valid 32-byte window base
317 LEAQ -128(R8), R11 // last valid 128-byte block base
318 CMPQ SI, R11
319 JA cna_loop32_entry // no whole block: extract and count
320
321 VPXOR Y7, Y7, Y7 // Y7 = zero (compare and SAD operand)
322 VPXOR Y4, Y4, Y4 // Y4 = byte accumulator A
323 VPXOR Y5, Y5, Y5 // Y5 = byte accumulator B
324 VPXOR Y8, Y8, Y8 // Y8 = 64-bit lane totals
325 MOVQ $127, R13 // blocks until a byte lane could overflow
326
327 // This loop carries a third fused pair, the DECQ/JZ flush countdown,
328 // which at BLOCKALIGN's offset would straddle a window boundary; see
329 // block_align_amd64.h.
330 BLOCKALIGN_LONG
331 cna_loop128:
332 // 0 > byte is exactly "high bit set"; the compares read their data
333 // operand from memory.
334 VPCMPGTB (SI), Y7, Y0
335 VPCMPGTB 32(SI), Y7, Y1
336 VPCMPGTB 64(SI), Y7, Y2
337 VPCMPGTB 96(SI), Y7, Y3
338 VPSUBB Y0, Y4, Y4 // lane -= 0xFF, i.e. += 1 where non-ASCII
339 VPSUBB Y1, Y5, Y5
340 VPSUBB Y2, Y4, Y4
341 VPSUBB Y3, Y5, Y5
342
343 ADDQ $128, SI
344 DECQ R13
345 JZ cna_flush
346
347 cna_loop128_next:
348 CMPQ SI, R11
349 JBE cna_loop128
350 JMP cna_block_done
351
352 cna_flush:
353 FLUSH
354 MOVQ $127, R13
355 JMP cna_loop128_next
356
357 cna_block_done:
358 // Fold the accumulators into the lane totals and the four totals into
359 // one scalar: high half onto low, then the high 64-bit lane onto the
360 // low one.
361 VPSADBW Y7, Y4, Y0
362 VPSADBW Y7, Y5, Y1
363 VPADDQ Y0, Y8, Y8
364 VPADDQ Y1, Y8, Y8
365 VEXTRACTI128 $1, Y8, X0
366 VPADDQ X0, X8, X8
367 VPSRLDQ $8, X8, X0
368 VPADDQ X0, X8, X8
369 VMOVQ X8, R9
370
371 cna_loop32_entry:
372 CMPQ SI, R12
373 JA cna_tail
374
375 cna_loop32:
376 VMOVDQU (SI), Y0
377 VPMOVMSKB Y0, AX
378 POPCNTL AX, AX
379 ADDQ AX, R10
380 ADDQ $32, SI
381 CMPQ SI, R12
382 JBE cna_loop32
383
384 cna_tail:
385 // 0-31 bytes left. Shifting the byte down to its high bit turns the
386 // test into an add, so the loop carries no data-dependent branch.
387 CMPQ SI, R8
388 JAE cna_done
389 MOVBLZX (SI), AX
390 SHRL $7, AX
391 ADDQ AX, R10
392 INCQ SI
393 JMP cna_tail
394
395 cna_done:
396 ADDQ R10, R9
397 MOVQ R9, ret+24(FP)
398 VZEROUPPER
399 RET