| 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 one overlapping vector at the buffer end |
| 7 |
// for the MinLen+ inputs the Go dispatch guarantees, the per-range |
| 8 |
// clamp-and-compare classifier replaced with a nibble table lookup, and |
| 9 |
// the 32-byte main loops replaced with 4x unrolled 128-byte blocks. |
| 10 |
|
| 11 |
//go:build amd64 |
| 12 |
|
| 13 |
#include "textflag.h" |
| 14 |
|
| 15 |
// Classifying \w = [A-Za-z0-9_] with unsigned clamp-and-compare costs three |
| 16 |
// VPMINUB/VPMAXUB/VPCMPEQB triples plus a VPCMPEQB for '_' and three VPORs: |
| 17 |
// thirteen vector ops per 32 bytes, ten of them on the two general vector |
| 18 |
// ALU ports. A nibble table does the same job in six, and the two VPSHUFBs |
| 19 |
// it adds run on the otherwise idle shuffle port. |
| 20 |
// |
| 21 |
// The table splits each byte into its high and low nibble. Every high |
| 22 |
// nibble that contains word characters gets one bit: |
| 23 |
// |
| 24 |
// 0x3_ -> 0x01 0x4_ -> 0x02 0x5_ -> 0x04 0x6_ -> 0x08 0x7_ -> 0x10 |
| 25 |
// |
| 26 |
// and each low-nibble entry carries the set of high-nibble rows in which |
| 27 |
// that low nibble is a word character, so a byte is a word character |
| 28 |
// exactly when lowTable[lo] & highTable[hi] != 0: |
| 29 |
// |
| 30 |
// lo 0x0 -> 0x15 ('0', 'P', 'p') |
| 31 |
// lo 0x1..0x9 -> 0x1F (digits and letters in every row) |
| 32 |
// lo 0xA -> 0x1E ('J', 'Z', 'j', 'z' but not ':') |
| 33 |
// lo 0xB..0xE -> 0x0A ('K'..'N', 'k'..'n' only) |
| 34 |
// lo 0xF -> 0x0E ('O', '_', 'o') |
| 35 |
// |
| 36 |
// Bytes >= 0x80 need no separate guard: VPSHUFB zeroes any lane whose |
| 37 |
// index byte has bit 7 set, so the low-nibble lookup already returns 0 for |
| 38 |
// them and they classify as non-word, matching the SWAR fallback. |
| 39 |
// |
| 40 |
// Both kernels compute the same "not a word character" mask (0xFF per |
| 41 |
// non-word lane): the low and high lookups are AND-ed and compared against |
| 42 |
// zero, which is the complement the class test needs anyway. |
| 43 |
// memchrNotWord ORs those masks together across a block and scans the |
| 44 |
// extracted bits directly; memchrWord ANDs them (a block holds a word |
| 45 |
// character exactly when some lane's non-word mask is clear) and inverts |
| 46 |
// the extracted bits with a single NOTL. |
| 47 |
|
| 48 |
// NOTWORD leaves in dst a mask with 0xFF in every lane of src that is NOT |
| 49 |
// a word character. t0 and t1 are scratch; dst may alias src. |
| 50 |
#define NOTWORD(src, dst, t0, t1) \ |
| 51 |
VPSRLW $4, src, t0; \ |
| 52 |
VPAND Y2, t0, t0; \ |
| 53 |
VPSHUFB t0, Y1, t0; \ |
| 54 |
VPSHUFB src, Y0, t1; \ |
| 55 |
VPAND t0, t1, t0; \ |
| 56 |
VPCMPEQB Y3, t0, dst |
| 57 |
|
| 58 |
// SETUPTABLES loads the nibble tables, the low-nibble mask and a zero |
| 59 |
// vector into Y0-Y3. The tables are built from immediates rather than a |
| 60 |
// RODATA section so the kernels stay position-independent and free of |
| 61 |
// relocations. |
| 62 |
#define SETUPTABLES \ |
| 63 |
MOVQ $0x1F1F1F1F1F1F1F15, AX; \ |
| 64 |
VMOVQ AX, X0; \ |
| 65 |
MOVQ $0x0E0A0A0A0A1E1F1F, AX; \ |
| 66 |
VPINSRQ $1, AX, X0, X0; \ |
| 67 |
VINSERTI128 $1, X0, Y0, Y0; \ |
| 68 |
MOVQ $0x1008040201000000, AX; \ |
| 69 |
VMOVQ AX, X1; \ |
| 70 |
VINSERTI128 $1, X1, Y1, Y1; \ |
| 71 |
MOVQ $0x0F0F0F0F0F0F0F0F, AX; \ |
| 72 |
VMOVQ AX, X2; \ |
| 73 |
VPBROADCASTQ X2, Y2; \ |
| 74 |
VPXOR Y3, Y3, Y3 |
| 75 |
|
| 76 |
// func memchrWordAVX2(haystack []byte) int |
| 77 |
// |
| 78 |
// AVX2 implementation to find the first word character [A-Za-z0-9_]. |
| 79 |
// |
| 80 |
// Parameters: |
| 81 |
// haystack_base+0(FP) - pointer to haystack (8 bytes) |
| 82 |
// haystack_len+8(FP) - length (8 bytes) |
| 83 |
// haystack_cap+16(FP) - capacity (8 bytes, unused) |
| 84 |
// ret+24(FP) - return value (8 bytes) |
| 85 |
// |
| 86 |
TEXT ·memchrWordAVX2(SB), NOSPLIT, $0-32 |
| 87 |
// Load parameters |
| 88 |
MOVQ haystack_base+0(FP), SI // SI = haystack pointer |
| 89 |
MOVQ haystack_len+8(FP), DX // DX = length |
| 90 |
|
| 91 |
// Empty check |
| 92 |
TESTQ DX, DX |
| 93 |
JZ word_not_found |
| 94 |
|
| 95 |
// Save start for offset calculation and compute the end pointer |
| 96 |
MOVQ SI, DI // DI = start pointer |
| 97 |
LEAQ (SI)(DX*1), R8 // R8 = end pointer |
| 98 |
|
| 99 |
CMPQ DX, $32 |
| 100 |
JB word_tail_loop // sub-32 direct call: scalar |
| 101 |
|
| 102 |
SETUPTABLES |
| 103 |
|
| 104 |
LEAQ -32(R8), R12 // last valid 32-byte window base |
| 105 |
LEAQ -128(R8), R11 // last valid 128-byte block base |
| 106 |
CMPQ SI, R11 |
| 107 |
JA word_loop32_entry |
| 108 |
|
| 109 |
word_loop128: |
| 110 |
VMOVDQU (SI), Y4 |
| 111 |
VMOVDQU 32(SI), Y5 |
| 112 |
VMOVDQU 64(SI), Y6 |
| 113 |
VMOVDQU 96(SI), Y7 |
| 114 |
NOTWORD(Y4, Y4, Y8, Y9) |
| 115 |
NOTWORD(Y5, Y5, Y8, Y9) |
| 116 |
NOTWORD(Y6, Y6, Y8, Y9) |
| 117 |
NOTWORD(Y7, Y7, Y8, Y9) |
| 118 |
|
| 119 |
// A word character exists in the block iff some lane's non-word mask |
| 120 |
// is clear, so AND the four masks and look for a zero lane. |
| 121 |
VPAND Y4, Y5, Y8 |
| 122 |
VPAND Y6, Y7, Y9 |
| 123 |
VPAND Y8, Y9, Y8 |
| 124 |
VPMOVMSKB Y8, CX |
| 125 |
NOTL CX |
| 126 |
TESTL CX, CX |
| 127 |
JNZ word_found_in_block |
| 128 |
|
| 129 |
ADDQ $128, SI |
| 130 |
CMPQ SI, R11 |
| 131 |
JBE word_loop128 |
| 132 |
|
| 133 |
word_loop32_entry: |
| 134 |
CMPQ SI, R12 |
| 135 |
JA word_last32 |
| 136 |
|
| 137 |
word_loop32: |
| 138 |
VMOVDQU (SI), Y4 |
| 139 |
NOTWORD(Y4, Y4, Y8, Y9) |
| 140 |
VPMOVMSKB Y4, CX |
| 141 |
NOTL CX |
| 142 |
TESTL CX, CX |
| 143 |
JNZ word_found_in_vector |
| 144 |
|
| 145 |
ADDQ $32, SI |
| 146 |
CMPQ SI, R12 |
| 147 |
JBE word_loop32 |
| 148 |
|
| 149 |
word_last32: |
| 150 |
// Fewer than 32 bytes remain past SI; rescan the final window. Lanes |
| 151 |
// before SI were already resolved as non-matching, so BSF stays exact. |
| 152 |
CMPQ SI, R8 |
| 153 |
JAE word_not_found |
| 154 |
MOVQ R12, SI |
| 155 |
VMOVDQU (SI), Y4 |
| 156 |
NOTWORD(Y4, Y4, Y8, Y9) |
| 157 |
VPMOVMSKB Y4, CX |
| 158 |
NOTL CX |
| 159 |
TESTL CX, CX |
| 160 |
JNZ word_found_in_vector |
| 161 |
JMP word_not_found |
| 162 |
|
| 163 |
word_tail_loop: |
| 164 |
MOVBLZX (SI), AX // AX = byte |
| 165 |
|
| 166 |
// Check [A-Z]: 65 <= b <= 90 |
| 167 |
CMPB AL, $65 |
| 168 |
JB word_check_lower |
| 169 |
CMPB AL, $90 |
| 170 |
JBE word_found_scalar |
| 171 |
|
| 172 |
word_check_lower: |
| 173 |
// Check [a-z]: 97 <= b <= 122 |
| 174 |
CMPB AL, $97 |
| 175 |
JB word_check_digit |
| 176 |
CMPB AL, $122 |
| 177 |
JBE word_found_scalar |
| 178 |
|
| 179 |
word_check_digit: |
| 180 |
// Check [0-9]: 48 <= b <= 57 |
| 181 |
CMPB AL, $48 |
| 182 |
JB word_check_underscore |
| 183 |
CMPB AL, $57 |
| 184 |
JBE word_found_scalar |
| 185 |
|
| 186 |
word_check_underscore: |
| 187 |
// Check '_': 95 |
| 188 |
CMPB AL, $95 |
| 189 |
JE word_found_scalar |
| 190 |
|
| 191 |
// Not a word char, continue |
| 192 |
INCQ SI |
| 193 |
CMPQ SI, R8 |
| 194 |
JB word_tail_loop |
| 195 |
|
| 196 |
word_not_found: |
| 197 |
MOVQ $-1, AX |
| 198 |
MOVQ AX, ret+24(FP) |
| 199 |
VZEROUPPER |
| 200 |
RET |
| 201 |
|
| 202 |
word_found_in_block: |
| 203 |
// Locate the first hit by re-extracting the per-vector masks in |
| 204 |
// address order; only one of the four can hold the lowest match. |
| 205 |
VPMOVMSKB Y4, CX |
| 206 |
NOTL CX |
| 207 |
TESTL CX, CX |
| 208 |
JNZ word_found_in_vector |
| 209 |
ADDQ $32, SI |
| 210 |
VPMOVMSKB Y5, CX |
| 211 |
NOTL CX |
| 212 |
TESTL CX, CX |
| 213 |
JNZ word_found_in_vector |
| 214 |
ADDQ $32, SI |
| 215 |
VPMOVMSKB Y6, CX |
| 216 |
NOTL CX |
| 217 |
TESTL CX, CX |
| 218 |
JNZ word_found_in_vector |
| 219 |
ADDQ $32, SI |
| 220 |
VPMOVMSKB Y7, CX |
| 221 |
NOTL CX |
| 222 |
|
| 223 |
word_found_in_vector: |
| 224 |
BSFL CX, BX |
| 225 |
SUBQ DI, SI // SI = offset to chunk start |
| 226 |
ADDQ SI, BX // BX = absolute position |
| 227 |
MOVQ BX, ret+24(FP) |
| 228 |
VZEROUPPER |
| 229 |
RET |
| 230 |
|
| 231 |
word_found_scalar: |
| 232 |
SUBQ DI, SI |
| 233 |
MOVQ SI, ret+24(FP) |
| 234 |
VZEROUPPER |
| 235 |
RET |
| 236 |
|
| 237 |
// func memchrNotWordAVX2(haystack []byte) int |
| 238 |
// |
| 239 |
// AVX2 implementation to find the first non-word character, the complement |
| 240 |
// of memchrWordAVX2. The nibble classifier already produces the non-word |
| 241 |
// mask directly, so this kernel skips the NOTL memchrWordAVX2 needs. |
| 242 |
// |
| 243 |
TEXT ·memchrNotWordAVX2(SB), NOSPLIT, $0-32 |
| 244 |
// Load parameters |
| 245 |
MOVQ haystack_base+0(FP), SI |
| 246 |
MOVQ haystack_len+8(FP), DX |
| 247 |
|
| 248 |
// Empty check |
| 249 |
TESTQ DX, DX |
| 250 |
JZ notword_not_found |
| 251 |
|
| 252 |
// Save start for offset calculation and compute the end pointer |
| 253 |
MOVQ SI, DI |
| 254 |
LEAQ (SI)(DX*1), R8 |
| 255 |
|
| 256 |
CMPQ DX, $32 |
| 257 |
JB notword_tail_loop // sub-32 direct call: scalar |
| 258 |
|
| 259 |
SETUPTABLES |
| 260 |
|
| 261 |
LEAQ -32(R8), R12 |
| 262 |
LEAQ -128(R8), R11 |
| 263 |
CMPQ SI, R11 |
| 264 |
JA notword_loop32_entry |
| 265 |
|
| 266 |
notword_loop128: |
| 267 |
VMOVDQU (SI), Y4 |
| 268 |
VMOVDQU 32(SI), Y5 |
| 269 |
VMOVDQU 64(SI), Y6 |
| 270 |
VMOVDQU 96(SI), Y7 |
| 271 |
NOTWORD(Y4, Y4, Y8, Y9) |
| 272 |
NOTWORD(Y5, Y5, Y8, Y9) |
| 273 |
NOTWORD(Y6, Y6, Y8, Y9) |
| 274 |
NOTWORD(Y7, Y7, Y8, Y9) |
| 275 |
|
| 276 |
VPOR Y4, Y5, Y8 |
| 277 |
VPOR Y6, Y7, Y9 |
| 278 |
VPOR Y8, Y9, Y8 |
| 279 |
VPMOVMSKB Y8, CX |
| 280 |
TESTL CX, CX |
| 281 |
JNZ notword_found_in_block |
| 282 |
|
| 283 |
ADDQ $128, SI |
| 284 |
CMPQ SI, R11 |
| 285 |
JBE notword_loop128 |
| 286 |
|
| 287 |
notword_loop32_entry: |
| 288 |
CMPQ SI, R12 |
| 289 |
JA notword_last32 |
| 290 |
|
| 291 |
notword_loop32: |
| 292 |
VMOVDQU (SI), Y4 |
| 293 |
NOTWORD(Y4, Y4, Y8, Y9) |
| 294 |
VPMOVMSKB Y4, CX |
| 295 |
TESTL CX, CX |
| 296 |
JNZ notword_found_in_vector |
| 297 |
|
| 298 |
ADDQ $32, SI |
| 299 |
CMPQ SI, R12 |
| 300 |
JBE notword_loop32 |
| 301 |
|
| 302 |
notword_last32: |
| 303 |
CMPQ SI, R8 |
| 304 |
JAE notword_not_found |
| 305 |
MOVQ R12, SI |
| 306 |
VMOVDQU (SI), Y4 |
| 307 |
NOTWORD(Y4, Y4, Y8, Y9) |
| 308 |
VPMOVMSKB Y4, CX |
| 309 |
TESTL CX, CX |
| 310 |
JNZ notword_found_in_vector |
| 311 |
JMP notword_not_found |
| 312 |
|
| 313 |
notword_tail_loop: |
| 314 |
MOVBLZX (SI), AX |
| 315 |
|
| 316 |
// Check if NOT a word char (inverse of word_tail_loop logic) |
| 317 |
// Word chars: [A-Z], [a-z], [0-9], '_' |
| 318 |
// If it's a word char, skip; if not, found |
| 319 |
|
| 320 |
// Check [A-Z]: 65 <= b <= 90 |
| 321 |
CMPB AL, $65 |
| 322 |
JB notword_check_lower_gap |
| 323 |
CMPB AL, $90 |
| 324 |
JBE notword_is_word // It's [A-Z], skip |
| 325 |
|
| 326 |
notword_check_lower_gap: |
| 327 |
// Check [a-z]: 97 <= b <= 122 |
| 328 |
CMPB AL, $97 |
| 329 |
JB notword_check_digit_gap |
| 330 |
CMPB AL, $122 |
| 331 |
JBE notword_is_word // It's [a-z], skip |
| 332 |
|
| 333 |
notword_check_digit_gap: |
| 334 |
// Check [0-9]: 48 <= b <= 57 |
| 335 |
CMPB AL, $48 |
| 336 |
JB notword_check_underscore_gap |
| 337 |
CMPB AL, $57 |
| 338 |
JBE notword_is_word // It's [0-9], skip |
| 339 |
|
| 340 |
notword_check_underscore_gap: |
| 341 |
// Check '_': 95 |
| 342 |
CMPB AL, $95 |
| 343 |
JE notword_is_word // It's '_', skip |
| 344 |
|
| 345 |
// Not a word char - found! |
| 346 |
JMP notword_found_scalar |
| 347 |
|
| 348 |
notword_is_word: |
| 349 |
// It's a word char, continue searching |
| 350 |
INCQ SI |
| 351 |
CMPQ SI, R8 |
| 352 |
JB notword_tail_loop |
| 353 |
|
| 354 |
notword_not_found: |
| 355 |
MOVQ $-1, AX |
| 356 |
MOVQ AX, ret+24(FP) |
| 357 |
VZEROUPPER |
| 358 |
RET |
| 359 |
|
| 360 |
notword_found_in_block: |
| 361 |
VPMOVMSKB Y4, CX |
| 362 |
TESTL CX, CX |
| 363 |
JNZ notword_found_in_vector |
| 364 |
ADDQ $32, SI |
| 365 |
VPMOVMSKB Y5, CX |
| 366 |
TESTL CX, CX |
| 367 |
JNZ notword_found_in_vector |
| 368 |
ADDQ $32, SI |
| 369 |
VPMOVMSKB Y6, CX |
| 370 |
TESTL CX, CX |
| 371 |
JNZ notword_found_in_vector |
| 372 |
ADDQ $32, SI |
| 373 |
VPMOVMSKB Y7, CX |
| 374 |
|
| 375 |
notword_found_in_vector: |
| 376 |
BSFL CX, BX |
| 377 |
SUBQ DI, SI |
| 378 |
ADDQ SI, BX |
| 379 |
MOVQ BX, ret+24(FP) |
| 380 |
VZEROUPPER |
| 381 |
RET |
| 382 |
|
| 383 |
notword_found_scalar: |
| 384 |
SUBQ DI, SI |
| 385 |
MOVQ SI, ret+24(FP) |
| 386 |
VZEROUPPER |
| 387 |
RET |