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