Debian Codegraph @

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