| 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, and the 32-byte main |
| 8 |
// loop replaced with a 4x unrolled 128-byte block. |
| 9 |
|
| 10 |
//go:build amd64 |
| 11 |
|
| 12 |
#include "textflag.h" |
| 13 |
|
| 14 |
// DIGITS leaves in dst a mask with 0xFF in every lane of src holding an |
| 15 |
// ASCII digit. t0 and t1 are scratch; dst may alias src. |
| 16 |
// |
| 17 |
// Y0 = [0x2F x 32] ('0' - 1) Y1 = [0x39 x 32] ('9') |
| 18 |
// |
| 19 |
// VPCMPGTB is a signed comparison, which is what makes the pair exact for |
| 20 |
// every byte: lanes >= 0x80 are negative and so fail "> 0x2F" outright. |
| 21 |
#define DIGITS(src, dst, t0, t1) \ |
| 22 |
VPCMPGTB Y0, src, t0; \ |
| 23 |
VPCMPGTB Y1, src, t1; \ |
| 24 |
VPANDN t0, t1, dst |
| 25 |
|
| 26 |
// func memchrDigitAVX2(haystack []byte) int |
| 27 |
// |
| 28 |
// AVX2 implementation of digit search that finds the first ASCII digit [0-9]. |
| 29 |
// |
| 30 |
// Algorithm for range check [0-9] (bytes 0x30-0x39): |
| 31 |
// 1. Broadcast 0x2F ('0'-1) and 0x39 ('9') to YMM registers |
| 32 |
// 2. Load 32 bytes from haystack |
| 33 |
// 3. VPCMPGTB: check if chunk > 0x2F (byte >= '0') |
| 34 |
// 4. VPCMPGTB: check if 0x39 < chunk, then invert (byte <= '9') |
| 35 |
// 5. VPANDN: combine masks to get bytes in range ['0'-'9'] |
| 36 |
// 6. VPMOVMSKB: extract 32-bit mask |
| 37 |
// 7. BSFL: find first set bit |
| 38 |
// |
| 39 |
// The block loop runs steps 2-5 over four vectors, ORs the four masks and |
| 40 |
// extracts once; the (rare) hit path re-extracts the per-vector masks in |
| 41 |
// address order to locate the first digit. |
| 42 |
// |
| 43 |
// Parameters (FP offsets): |
| 44 |
// haystack_base+0(FP) - pointer to haystack data (8 bytes) |
| 45 |
// haystack_len+8(FP) - haystack length (8 bytes) |
| 46 |
// haystack_cap+16(FP) - haystack capacity (8 bytes, unused but part of slice) |
| 47 |
// ret+24(FP) - return value: index or -1 (8 bytes) |
| 48 |
// |
| 49 |
// Total argument frame size: 32 bytes (8+8+8+8) |
| 50 |
TEXT ·memchrDigitAVX2(SB), NOSPLIT, $0-32 |
| 51 |
// Load parameters |
| 52 |
MOVQ haystack_base+0(FP), SI // SI = haystack pointer |
| 53 |
MOVQ haystack_len+8(FP), DX // DX = haystack length |
| 54 |
|
| 55 |
// Empty haystack check |
| 56 |
TESTQ DX, DX |
| 57 |
JZ not_found |
| 58 |
|
| 59 |
// Prepare constants for range check ['0'-'9'] = [0x30-0x39] |
| 60 |
// We check: byte > 0x2F AND byte <= 0x39 |
| 61 |
MOVQ $0x2F2F2F2F2F2F2F2F, AX |
| 62 |
VMOVQ AX, X0 |
| 63 |
VPBROADCASTQ X0, Y0 // Y0 = [0x2F x 32] |
| 64 |
|
| 65 |
MOVQ $0x3939393939393939, AX |
| 66 |
VMOVQ AX, X1 |
| 67 |
VPBROADCASTQ X1, Y1 // Y1 = [0x39 x 32] |
| 68 |
|
| 69 |
// Save start pointer for offset calculation, compute the end pointer |
| 70 |
MOVQ SI, DI // DI = haystack start (preserved) |
| 71 |
LEAQ (SI)(DX*1), R8 // R8 = SI + length (end pointer) |
| 72 |
|
| 73 |
CMPQ DX, $32 |
| 74 |
JB tail_loop // sub-32 direct call: scalar |
| 75 |
|
| 76 |
LEAQ -32(R8), R12 // last valid 32-byte window base |
| 77 |
LEAQ -128(R8), R11 // last valid 128-byte block base |
| 78 |
CMPQ SI, R11 |
| 79 |
JA loop32_entry |
| 80 |
|
| 81 |
loop128: |
| 82 |
VMOVDQU (SI), Y2 |
| 83 |
VMOVDQU 32(SI), Y3 |
| 84 |
VMOVDQU 64(SI), Y4 |
| 85 |
VMOVDQU 96(SI), Y5 |
| 86 |
DIGITS(Y2, Y2, Y6, Y7) |
| 87 |
DIGITS(Y3, Y3, Y6, Y7) |
| 88 |
DIGITS(Y4, Y4, Y6, Y7) |
| 89 |
DIGITS(Y5, Y5, Y6, Y7) |
| 90 |
|
| 91 |
VPOR Y2, Y3, Y6 |
| 92 |
VPOR Y4, Y5, Y7 |
| 93 |
VPOR Y6, Y7, Y6 |
| 94 |
VPMOVMSKB Y6, CX |
| 95 |
TESTL CX, CX |
| 96 |
JNZ found_in_block |
| 97 |
|
| 98 |
ADDQ $128, SI |
| 99 |
CMPQ SI, R11 |
| 100 |
JBE loop128 |
| 101 |
|
| 102 |
loop32_entry: |
| 103 |
CMPQ SI, R12 |
| 104 |
JA last32 |
| 105 |
|
| 106 |
loop32: |
| 107 |
VMOVDQU (SI), Y2 |
| 108 |
DIGITS(Y2, Y2, Y6, Y7) |
| 109 |
VPMOVMSKB Y2, CX |
| 110 |
TESTL CX, CX |
| 111 |
JNZ found_in_vector |
| 112 |
|
| 113 |
ADDQ $32, SI |
| 114 |
CMPQ SI, R12 |
| 115 |
JBE loop32 |
| 116 |
|
| 117 |
last32: |
| 118 |
// Fewer than 32 bytes remain past SI; rescan the final window. Lanes |
| 119 |
// before SI were already resolved as non-digits, so BSF stays exact. |
| 120 |
CMPQ SI, R8 |
| 121 |
JAE not_found |
| 122 |
MOVQ R12, SI |
| 123 |
VMOVDQU (SI), Y2 |
| 124 |
DIGITS(Y2, Y2, Y6, Y7) |
| 125 |
VPMOVMSKB Y2, CX |
| 126 |
TESTL CX, CX |
| 127 |
JNZ found_in_vector |
| 128 |
JMP not_found |
| 129 |
|
| 130 |
tail_loop: |
| 131 |
// Load one byte |
| 132 |
MOVBLZX (SI), BX // BX = haystack[SI] (zero-extended) |
| 133 |
|
| 134 |
// Check if byte is in range ['0'-'9'] (0x30-0x39) |
| 135 |
CMPB BL, $0x30 // Compare with '0' |
| 136 |
JB tail_next // If < '0', not a digit |
| 137 |
CMPB BL, $0x39 // Compare with '9' |
| 138 |
JA tail_next // If > '9', not a digit |
| 139 |
|
| 140 |
// Found digit! |
| 141 |
SUBQ DI, SI // SI = offset from start |
| 142 |
MOVQ SI, ret+24(FP) // Return index |
| 143 |
VZEROUPPER // Clear upper YMM bits (CRITICAL!) |
| 144 |
RET |
| 145 |
|
| 146 |
tail_next: |
| 147 |
// Advance to next byte |
| 148 |
INCQ SI |
| 149 |
CMPQ SI, R8 // Check if reached end |
| 150 |
JB tail_loop // Continue if SI < end |
| 151 |
|
| 152 |
not_found: |
| 153 |
// No digit found in entire haystack |
| 154 |
MOVQ $-1, AX // Return -1 |
| 155 |
MOVQ AX, ret+24(FP) |
| 156 |
VZEROUPPER // Clear upper YMM bits (CRITICAL!) |
| 157 |
RET |
| 158 |
|
| 159 |
found_in_block: |
| 160 |
// Locate the first digit by re-extracting the per-vector masks in |
| 161 |
// address order; only one of the four can hold the lowest match. |
| 162 |
VPMOVMSKB Y2, CX |
| 163 |
TESTL CX, CX |
| 164 |
JNZ found_in_vector |
| 165 |
ADDQ $32, SI |
| 166 |
VPMOVMSKB Y3, CX |
| 167 |
TESTL CX, CX |
| 168 |
JNZ found_in_vector |
| 169 |
ADDQ $32, SI |
| 170 |
VPMOVMSKB Y4, CX |
| 171 |
TESTL CX, CX |
| 172 |
JNZ found_in_vector |
| 173 |
ADDQ $32, SI |
| 174 |
VPMOVMSKB Y5, CX |
| 175 |
|
| 176 |
found_in_vector: |
| 177 |
// Digit found in vector! CX contains a 32-bit mask with set bits at |
| 178 |
// digit positions; BSFL finds the index of the first one. |
| 179 |
BSFL CX, BX // BX = index of first set bit (0-31) |
| 180 |
SUBQ DI, SI // SI = offset from start to current chunk |
| 181 |
ADDQ SI, BX // BX = absolute index |
| 182 |
MOVQ BX, ret+24(FP) // Return index |
| 183 |
VZEROUPPER // Clear upper YMM bits |
| 184 |
RET |