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