Debian Codegraph @

simd/memchr_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 loops replaced with overlapping vector rescans of the final
7 // window (a vector pair in memchrPairAVX2) for the MinLen+ inputs the Go
8 // dispatch guarantees, and the 32-byte main loops replaced with 4x
9 // unrolled 128-byte blocks that OR the per-vector masks together and
10 // extract a single VPMOVMSKB per block.
11
12 //go:build amd64
13
14 #include "textflag.h"
15
16 // The kernels below share one loop skeleton:
17 //
18 // 1. A 128-byte block loop, entered only while base <= end-128. Four
19 // vectors are compared in parallel, their match masks OR-ed together,
20 // and a single VPMOVMSKB tests all 128 bytes at once. The per-vector
21 // masks stay live, so the (rare) hit path re-extracts them in address
22 // order to locate the first match. This keeps the port-0-only
23 // VPMOVMSKB and the loop bookkeeping off the critical path: the block
24 // loop retires four 32-byte compares for the branch overhead of one.
25 // 2. A 32-byte loop for the remainder, entered while base <= end-32.
26 // 3. One overlapping 32-byte window ending at the buffer end for the
27 // last 1-31 bytes. Lanes before the current base were already
28 // resolved as non-matching, so the first set lane of the overlap
29 // always falls in the new bytes and BSF stays exact.
30 //
31 // Both loops test their bound at the bottom against a precomputed limit
32 // pointer, so the steady state costs one ADDQ/CMPQ/JBE instead of the
33 // LEAQ/CMPQ/JA/ADDQ/JMP the original 32-byte loops carried. Inputs shorter
34 // than one vector — unreachable through the Go dispatch, which routes them
35 // to the SWAR fallbacks — take the scalar tail loops.
36
37 // func memchr2AVX2(haystack []byte, needle1, needle2 byte) int
38 //
39 // AVX2 kernel behind Memchr2 that searches for either of two bytes.
40 // Uses parallel comparison with two broadcast vectors and combines results
41 // with VPOR.
42 //
43 // Parameters (FP offsets, ABI0; the byte arguments are packed into
44 // adjacent 1-byte slots after the 24-byte slice header, then padded to
45 // align the return value):
46 // haystack_base+0(FP) - pointer (8 bytes)
47 // haystack_len+8(FP) - length (8 bytes)
48 // haystack_cap+16(FP) - capacity (8 bytes, unused)
49 // needle1+24(FP) - first needle (1 byte)
50 // needle2+25(FP) - second needle (1 byte, then 6 bytes padding)
51 // ret+32(FP) - return value (8 bytes)
52 //
53 // Total argument frame: 40 bytes (24 slice + 1 + 1 + 6 padding + 8 ret)
54 TEXT ·memchr2AVX2(SB), NOSPLIT, $0-40
55 // Load parameters
56 MOVQ haystack_base+0(FP), SI
57 MOVQ haystack_len+8(FP), DX
58 MOVBQZX needle1+24(FP), AX
59 MOVBQZX needle2+25(FP), BX
60
61 // Empty check
62 TESTQ DX, DX
63 JZ not_found2
64
65 // Broadcast both needles
66 VMOVD AX, X0
67 VPBROADCASTB X0, Y0 // Y0 = [needle1 × 32]
68 VMOVD BX, X1
69 VPBROADCASTB X1, Y1 // Y1 = [needle2 × 32]
70
71 // Save start pointer and compute the end
72 MOVQ SI, DI
73 LEAQ (SI)(DX*1), R8
74
75 CMPQ DX, $32
76 JB tail_loop2 // sub-32 direct call: scalar
77
78 LEAQ -32(R8), R12 // last valid 32-byte window base
79 LEAQ -128(R8), R11 // last valid 128-byte block base
80 CMPQ SI, R11
81 JA loop32_2_entry
82
83 loop128_2:
84 VMOVDQU (SI), Y2
85 VMOVDQU 32(SI), Y3
86 VMOVDQU 64(SI), Y4
87 VMOVDQU 96(SI), Y5
88
89 VPCMPEQB Y0, Y2, Y6
90 VPCMPEQB Y1, Y2, Y7
91 VPOR Y6, Y7, Y2 // Y2 = matches in bytes 0-31
92 VPCMPEQB Y0, Y3, Y6
93 VPCMPEQB Y1, Y3, Y7
94 VPOR Y6, Y7, Y3 // Y3 = matches in bytes 32-63
95 VPCMPEQB Y0, Y4, Y6
96 VPCMPEQB Y1, Y4, Y7
97 VPOR Y6, Y7, Y4 // Y4 = matches in bytes 64-95
98 VPCMPEQB Y0, Y5, Y6
99 VPCMPEQB Y1, Y5, Y7
100 VPOR Y6, Y7, Y5 // Y5 = matches in bytes 96-127
101
102 VPOR Y2, Y3, Y6
103 VPOR Y4, Y5, Y7
104 VPOR Y6, Y7, Y6 // Y6 = matches anywhere in the block
105 VPMOVMSKB Y6, CX
106 TESTL CX, CX
107 JNZ found_in_block2
108
109 ADDQ $128, SI
110 CMPQ SI, R11
111 JBE loop128_2
112
113 loop32_2_entry:
114 CMPQ SI, R12
115 JA last32_2
116
117 loop32_2:
118 VMOVDQU (SI), Y2
119 VPCMPEQB Y0, Y2, Y3
120 VPCMPEQB Y1, Y2, Y4
121 VPOR Y3, Y4, Y3
122 VPMOVMSKB Y3, CX
123 TESTL CX, CX
124 JNZ found_in_vector2
125
126 ADDQ $32, SI
127 CMPQ SI, R12
128 JBE loop32_2
129
130 last32_2:
131 // Fewer than 32 bytes remain past SI; rescan the final window.
132 CMPQ SI, R8
133 JAE not_found2
134 MOVQ R12, SI
135 VMOVDQU (SI), Y2
136 VPCMPEQB Y0, Y2, Y3
137 VPCMPEQB Y1, Y2, Y4
138 VPOR Y3, Y4, Y3
139 VPMOVMSKB Y3, CX
140 TESTL CX, CX
141 JNZ found_in_vector2
142 JMP not_found2
143
144 tail_loop2:
145 MOVBLZX (SI), CX
146 CMPB CL, AL // Compare with needle1
147 JE found_scalar2
148 CMPB CL, BL // Compare with needle2
149 JE found_scalar2
150
151 INCQ SI
152 CMPQ SI, R8
153 JB tail_loop2
154
155 not_found2:
156 MOVQ $-1, AX
157 MOVQ AX, ret+32(FP)
158 VZEROUPPER
159 RET
160
161 found_in_block2:
162 // Locate the first hit by re-extracting the per-vector masks in
163 // address order; only one of the four can hold the lowest match.
164 VPMOVMSKB Y2, CX
165 TESTL CX, CX
166 JNZ found_in_vector2
167 ADDQ $32, SI
168 VPMOVMSKB Y3, CX
169 TESTL CX, CX
170 JNZ found_in_vector2
171 ADDQ $32, SI
172 VPMOVMSKB Y4, CX
173 TESTL CX, CX
174 JNZ found_in_vector2
175 ADDQ $32, SI
176 VPMOVMSKB Y5, CX
177
178 found_in_vector2:
179 BSFL CX, CX
180 SUBQ DI, SI
181 ADDQ SI, CX
182 MOVQ CX, ret+32(FP)
183 VZEROUPPER
184 RET
185
186 found_scalar2:
187 SUBQ DI, SI
188 MOVQ SI, ret+32(FP)
189 VZEROUPPER
190 RET
191
192 // func memchr3AVX2(haystack []byte, needle1, needle2, needle3 byte) int
193 //
194 // AVX2 kernel behind Memchr3 that searches for any of three bytes. Same
195 // skeleton as memchr2AVX2 with a third broadcast/compare.
196 //
197 // Parameters (FP offsets, ABI0; byte arguments packed into adjacent 1-byte
198 // slots after the slice header, then padded):
199 // haystack_base+0(FP) - pointer (8 bytes)
200 // haystack_len+8(FP) - length (8 bytes)
201 // haystack_cap+16(FP) - capacity (8 bytes, unused)
202 // needle1+24(FP) - first needle (1 byte)
203 // needle2+25(FP) - second needle (1 byte)
204 // needle3+26(FP) - third needle (1 byte, then 5 bytes padding)
205 // ret+32(FP) - return value (8 bytes)
206 //
207 // Total argument frame: 40 bytes (24 slice + 3 + 5 padding + 8 ret)
208 TEXT ·memchr3AVX2(SB), NOSPLIT, $0-40
209 // Load parameters
210 MOVQ haystack_base+0(FP), SI
211 MOVQ haystack_len+8(FP), DX
212 MOVBQZX needle1+24(FP), AX
213 MOVBQZX needle2+25(FP), BX
214 MOVBQZX needle3+26(FP), R10
215
216 // Empty check
217 TESTQ DX, DX
218 JZ not_found3
219
220 // Broadcast all three needles
221 VMOVD AX, X0
222 VPBROADCASTB X0, Y0 // Y0 = [needle1 × 32]
223 VMOVD BX, X1
224 VPBROADCASTB X1, Y1 // Y1 = [needle2 × 32]
225 VMOVD R10, X2
226 VPBROADCASTB X2, Y2 // Y2 = [needle3 × 32]
227
228 // Save start pointer and compute the end
229 MOVQ SI, DI
230 LEAQ (SI)(DX*1), R8
231
232 CMPQ DX, $32
233 JB tail_loop3 // sub-32 direct call: scalar
234
235 LEAQ -32(R8), R12 // last valid 32-byte window base
236 LEAQ -128(R8), R11 // last valid 128-byte block base
237 CMPQ SI, R11
238 JA loop32_3_entry
239
240 loop128_3:
241 VMOVDQU (SI), Y3
242 VMOVDQU 32(SI), Y4
243 VMOVDQU 64(SI), Y5
244 VMOVDQU 96(SI), Y6
245
246 VPCMPEQB Y0, Y3, Y7
247 VPCMPEQB Y1, Y3, Y8
248 VPCMPEQB Y2, Y3, Y9
249 VPOR Y7, Y8, Y7
250 VPOR Y7, Y9, Y3 // Y3 = matches in bytes 0-31
251 VPCMPEQB Y0, Y4, Y7
252 VPCMPEQB Y1, Y4, Y8
253 VPCMPEQB Y2, Y4, Y9
254 VPOR Y7, Y8, Y7
255 VPOR Y7, Y9, Y4 // Y4 = matches in bytes 32-63
256 VPCMPEQB Y0, Y5, Y7
257 VPCMPEQB Y1, Y5, Y8
258 VPCMPEQB Y2, Y5, Y9
259 VPOR Y7, Y8, Y7
260 VPOR Y7, Y9, Y5 // Y5 = matches in bytes 64-95
261 VPCMPEQB Y0, Y6, Y7
262 VPCMPEQB Y1, Y6, Y8
263 VPCMPEQB Y2, Y6, Y9
264 VPOR Y7, Y8, Y7
265 VPOR Y7, Y9, Y6 // Y6 = matches in bytes 96-127
266
267 VPOR Y3, Y4, Y7
268 VPOR Y5, Y6, Y8
269 VPOR Y7, Y8, Y7 // Y7 = matches anywhere in the block
270 VPMOVMSKB Y7, CX
271 TESTL CX, CX
272 JNZ found_in_block3
273
274 ADDQ $128, SI
275 CMPQ SI, R11
276 JBE loop128_3
277
278 loop32_3_entry:
279 CMPQ SI, R12
280 JA last32_3
281
282 loop32_3:
283 VMOVDQU (SI), Y3
284 VPCMPEQB Y0, Y3, Y4
285 VPCMPEQB Y1, Y3, Y5
286 VPCMPEQB Y2, Y3, Y6
287 VPOR Y4, Y5, Y4
288 VPOR Y4, Y6, Y4
289 VPMOVMSKB Y4, CX
290 TESTL CX, CX
291 JNZ found_in_vector3
292
293 ADDQ $32, SI
294 CMPQ SI, R12
295 JBE loop32_3
296
297 last32_3:
298 CMPQ SI, R8
299 JAE not_found3
300 MOVQ R12, SI
301 VMOVDQU (SI), Y3
302 VPCMPEQB Y0, Y3, Y4
303 VPCMPEQB Y1, Y3, Y5
304 VPCMPEQB Y2, Y3, Y6
305 VPOR Y4, Y5, Y4
306 VPOR Y4, Y6, Y4
307 VPMOVMSKB Y4, CX
308 TESTL CX, CX
309 JNZ found_in_vector3
310 JMP not_found3
311
312 tail_loop3:
313 MOVBLZX (SI), CX
314 CMPB CL, AL // Compare with needle1
315 JE found_scalar3
316 CMPB CL, BL // Compare with needle2
317 JE found_scalar3
318 CMPB CL, R10B // Compare with needle3
319 JE found_scalar3
320
321 INCQ SI
322 CMPQ SI, R8
323 JB tail_loop3
324
325 not_found3:
326 MOVQ $-1, AX
327 MOVQ AX, ret+32(FP)
328 VZEROUPPER
329 RET
330
331 found_in_block3:
332 VPMOVMSKB Y3, CX
333 TESTL CX, CX
334 JNZ found_in_vector3
335 ADDQ $32, SI
336 VPMOVMSKB Y4, CX
337 TESTL CX, CX
338 JNZ found_in_vector3
339 ADDQ $32, SI
340 VPMOVMSKB Y5, CX
341 TESTL CX, CX
342 JNZ found_in_vector3
343 ADDQ $32, SI
344 VPMOVMSKB Y6, CX
345
346 found_in_vector3:
347 BSFL CX, CX
348 SUBQ DI, SI
349 ADDQ SI, CX
350 MOVQ CX, ret+32(FP)
351 VZEROUPPER
352 RET
353
354 found_scalar3:
355 SUBQ DI, SI
356 MOVQ SI, ret+32(FP)
357 VZEROUPPER
358 RET
359
360 // func memchrPairAVX2(haystack []byte, byte1, byte2 byte, offset int) int
361 //
362 // AVX2 implementation of paired-byte search. Finds positions where byte1
363 // appears at position i and byte2 appears at position i+offset.
364 //
365 // Algorithm:
366 // 1. Broadcast byte1 to YMM0, byte2 to YMM1
367 // 2. For each 32-byte chunk at position p:
368 // - Load haystack[p:p+32], compare with byte1 → mask1
369 // - Load haystack[p+offset:p+offset+32], compare with byte2 → mask2
370 // - AND mask1, mask2 → combined mask (both bytes at correct distance)
371 // 3. First set bit in combined mask is the answer
372 // 4. The block loop runs the same AND over four chunk pairs at a time.
373 // 5. The final 1-31 pair positions are rescanned with one overlapping
374 // vector pair whose second load ends exactly at the buffer end; pair
375 // positions before it are known non-matching, so BSF stays exact.
376 // Inputs with fewer than offset+32 bytes — unreachable through the Go
377 // dispatch — take a scalar loop instead.
378 //
379 // Parameters (FP offsets, ABI0; the byte arguments are packed into
380 // adjacent 1-byte slots, then padded to align the int argument):
381 // haystack_base+0(FP) - pointer (8 bytes)
382 // haystack_len+8(FP) - length (8 bytes)
383 // haystack_cap+16(FP) - capacity (8 bytes, unused)
384 // byte1+24(FP) - first byte to find (1 byte)
385 // byte2+25(FP) - second byte to find (1 byte, then 6 bytes padding)
386 // offset+32(FP) - distance between byte1 and byte2 (8 bytes)
387 // ret+40(FP) - return value (8 bytes)
388 //
389 // Total argument frame: 48 bytes (24 slice + 1 + 1 + 6 padding + 8 + 8 ret)
390 TEXT ·memchrPairAVX2(SB), NOSPLIT, $0-48
391 // Load parameters
392 MOVQ haystack_base+0(FP), SI // SI = haystack pointer
393 MOVQ haystack_len+8(FP), DX // DX = haystack length
394 MOVBQZX byte1+24(FP), AX // AX = byte1
395 MOVBQZX byte2+25(FP), BX // BX = byte2
396 MOVQ offset+32(FP), R10 // R10 = offset
397
398 // Empty check and bounds check
399 TESTQ DX, DX
400 JZ not_found_pair
401 CMPQ R10, DX // offset >= length?
402 JAE not_found_pair
403
404 // Broadcast both bytes
405 VMOVD AX, X0
406 VPBROADCASTB X0, Y0 // Y0 = [byte1 × 32]
407 VMOVD BX, X1
408 VPBROADCASTB X1, Y1 // Y1 = [byte2 × 32]
409
410 // Save start pointer
411 MOVQ SI, DI // DI = haystack start
412
413 // Calculate limits:
414 // - We need 32 bytes at position p (for byte1 check)
415 // - We need 32 bytes at position p+offset (for byte2 check)
416 // - So we need p + offset + 32 <= length
417 // - Which means p <= length - offset - 32
418 MOVQ DX, R12 // R12 = length
419 SUBQ R10, R12 // R12 = length - offset
420 SUBQ $32, R12 // R12 = length - offset - 32 (last valid p)
421 JL scalar_entry_pair // sub-(offset+32) direct call: scalar
422 ADDQ SI, R12 // R12 = absolute last valid base
423
424 LEAQ -96(R12), R11 // last valid 128-position block base
425 CMPQ SI, R11
426 JA loop32_pair_entry
427
428 loop128_pair:
429 // byte1 side: four consecutive 32-byte chunks
430 VMOVDQU (SI), Y2
431 VMOVDQU 32(SI), Y3
432 VMOVDQU 64(SI), Y4
433 VMOVDQU 96(SI), Y5
434 VPCMPEQB Y0, Y2, Y2
435 VPCMPEQB Y0, Y3, Y3
436 VPCMPEQB Y0, Y4, Y4
437 VPCMPEQB Y0, Y5, Y5
438
439 // byte2 side: the same chunks shifted by offset
440 VMOVDQU (SI)(R10*1), Y6
441 VMOVDQU 32(SI)(R10*1), Y7
442 VMOVDQU 64(SI)(R10*1), Y8
443 VMOVDQU 96(SI)(R10*1), Y9
444 VPCMPEQB Y1, Y6, Y6
445 VPCMPEQB Y1, Y7, Y7
446 VPCMPEQB Y1, Y8, Y8
447 VPCMPEQB Y1, Y9, Y9
448
449 VPAND Y2, Y6, Y2 // Y2 = pairs in positions 0-31
450 VPAND Y3, Y7, Y3 // Y3 = pairs in positions 32-63
451 VPAND Y4, Y8, Y4 // Y4 = pairs in positions 64-95
452 VPAND Y5, Y9, Y5 // Y5 = pairs in positions 96-127
453
454 VPOR Y2, Y3, Y6
455 VPOR Y4, Y5, Y7
456 VPOR Y6, Y7, Y6
457 VPMOVMSKB Y6, CX
458 TESTL CX, CX
459 JNZ found_in_block_pair
460
461 ADDQ $128, SI
462 CMPQ SI, R11
463 JBE loop128_pair
464
465 loop32_pair_entry:
466 CMPQ SI, R12
467 JA vector_tail_pair
468
469 loop32_pair:
470 // Load 32 bytes at position p (for byte1) and at p+offset (for byte2)
471 VMOVDQU (SI), Y2 // Y2 = haystack[p:p+32]
472 VMOVDQU (SI)(R10*1), Y3 // Y3 = haystack[p+offset:p+offset+32]
473 VPCMPEQB Y0, Y2, Y4 // Y4 = positions where haystack[p+k] == byte1
474 VPCMPEQB Y1, Y3, Y5 // Y5 = positions where haystack[p+offset+k] == byte2
475
476 // AND the results: bit k is set only if:
477 // - haystack[p+k] == byte1
478 // - haystack[p+offset+k] == byte2
479 VPAND Y4, Y5, Y4 // Y4 = combined mask
480 VPMOVMSKB Y4, CX
481 TESTL CX, CX
482 JNZ found_in_vector_pair
483
484 ADDQ $32, SI
485 CMPQ SI, R12
486 JBE loop32_pair
487
488 vector_tail_pair:
489 // Fewer than 32 pair positions remain past SI. If any do, rescan the
490 // final 32-position window based at R12 = base+len-offset-32: its second
491 // load ends exactly at the buffer end, and pair positions before SI are
492 // known non-matching.
493 LEAQ (DI)(DX*1), R9 // R9 = buffer end
494 SUBQ R10, R9 // R9 = end of valid pair positions
495 CMPQ SI, R9
496 JAE not_found_pair
497 MOVQ R12, SI // SI = base of the final window
498 VMOVDQU (SI), Y2
499 VMOVDQU (SI)(R10*1), Y3
500 VPCMPEQB Y0, Y2, Y4
501 VPCMPEQB Y1, Y3, Y5
502 VPAND Y4, Y5, Y4
503 VPMOVMSKB Y4, CX
504 TESTL CX, CX
505 JNZ found_in_vector_pair
506 JMP not_found_pair
507
508 scalar_entry_pair:
509 // Scalar path for direct calls with fewer than offset+32 bytes.
510 LEAQ (DI)(DX*1), R8 // R8 = end pointer
511
512 tail_loop_pair:
513 // Check if SI + offset < end
514 LEAQ (SI)(R10*1), R9 // R9 = SI + offset
515 CMPQ R9, R8
516 JAE not_found_pair // If SI + offset >= end, we're done
517
518 // Load and compare both bytes
519 MOVBLZX (SI), CX // CX = haystack[SI]
520 CMPB CL, AL // Compare with byte1
521 JNE tail_next_pair // Not byte1, skip
522
523 // byte1 matches, check byte2
524 MOVBLZX (SI)(R10*1), CX // CX = haystack[SI + offset]
525 CMPB CL, BL // Compare with byte2
526 JE found_scalar_pair // Both match!
527
528 tail_next_pair:
529 INCQ SI
530 JMP tail_loop_pair
531
532 not_found_pair:
533 MOVQ $-1, AX
534 MOVQ AX, ret+40(FP)
535 VZEROUPPER
536 RET
537
538 found_in_block_pair:
539 VPMOVMSKB Y2, CX
540 TESTL CX, CX
541 JNZ found_in_vector_pair
542 ADDQ $32, SI
543 VPMOVMSKB Y3, CX
544 TESTL CX, CX
545 JNZ found_in_vector_pair
546 ADDQ $32, SI
547 VPMOVMSKB Y4, CX
548 TESTL CX, CX
549 JNZ found_in_vector_pair
550 ADDQ $32, SI
551 VPMOVMSKB Y5, CX
552
553 found_in_vector_pair:
554 // Match found in vector! CX contains mask
555 BSFL CX, CX // CX = index of first set bit (0-31)
556 SUBQ DI, SI // SI = offset from start to current chunk
557 ADDQ SI, CX // CX = absolute index
558 MOVQ CX, ret+40(FP)
559 VZEROUPPER
560 RET
561
562 found_scalar_pair:
563 SUBQ DI, SI
564 MOVQ SI, ret+40(FP)
565 VZEROUPPER
566 RET