Debian Codegraph @

internal/bytealg/count_go122_amd64.s

raw
1 //go:build amd64 && go1.22
2 // +build amd64,go1.22
3
4 // Copyright 2018 The Go Authors. All rights reserved.
5 // Use of this source code is governed by a BSD-style
6 // license that can be found in the LICENSE file.
7
8 #include "go_asm.h"
9 #include "asm_amd64.h"
10 #include "textflag.h"
11
12 TEXT ·Count(SB), NOSPLIT, $0-40
13 #ifndef hasPOPCNT
14 CMPB golang·org∕x∕sys∕cpu·X86+const_offsetX86HasPOPCNT(SB), $1
15 JEQ 2(PC)
16 JMP ·countGeneric(SB)
17
18 #endif
19 MOVQ b_base+0(FP), SI
20 MOVQ b_len+8(FP), BX
21 MOVB c+24(FP), AL
22 LEAQ ret+32(FP), R8
23
24 LEAL -65(AX), CX // Check if the byte is a ASCII letter
25 CMPB CL, $25
26 JLS count_case // Byte sought is a ASCII letter
27 ADDL $-97, AX
28 CMPB AL, $25
29 JHI count
30
31 count_case:
32 MOVB c+24(FP), AL
33 JMP countbodyCase<>(SB)
34
35 count:
36 MOVB c+24(FP), AL
37 JMP countbody<>(SB)
38
39 TEXT ·CountString(SB), NOSPLIT, $0-32
40 #ifndef hasPOPCNT
41 CMPB golang·org∕x∕sys∕cpu·X86+const_offsetX86HasPOPCNT(SB), $1
42 JEQ 2(PC)
43 JMP ·countGenericString(SB)
44
45 #endif
46 MOVQ s_base+0(FP), SI
47 MOVQ s_len+8(FP), BX
48 MOVB c+16(FP), AL
49 LEAQ ret+24(FP), R8
50
51 LEAL -65(AX), CX // Check if the byte is a ASCII letter
52 CMPB CL, $25
53 JLS count_case // Byte sought is a ASCII letter
54 ADDL $-97, AX
55 CMPB AL, $25
56 JHI count
57
58 count_case:
59 MOVB c+16(FP), AL
60 JMP countbodyCase<>(SB)
61
62 count:
63 MOVB c+16(FP), AL
64 JMP countbody<>(SB)
65
66 // input:
67 // SI: data
68 // BX: data len
69 // AL: byte sought
70 // R8: address to put result
71 // This function requires the POPCNT instruction.
72 TEXT countbodyCase<>(SB), NOSPLIT, $0
73 // Shuffle X0 around so that each byte contains
74 // the character we're looking for.
75 ORL $32, AX // Convert byte to lowercase
76 MOVD AX, X0
77 PUNPCKLBW X0, X0
78 PUNPCKLBW X0, X0
79 PSHUFL $0, X0, X0
80
81 // Add space (' ') mask to X2
82 MOVQ $32, CX
83 MOVQ CX, X2
84 PUNPCKLBW X2, X2
85 PUNPCKLBW X2, X2
86 PSHUFL $0, X2, X2
87
88 CMPQ BX, $16
89 JLT small
90
91 MOVQ $0, R12 // Accumulator
92
93 MOVQ SI, DI
94
95 CMPQ BX, $64
96 JA avx2
97
98 sse:
99 LEAQ -16(SI)(BX*1), AX // AX = address of last 16 bytes
100 JMP sseloopentry
101
102 PCALIGN $16
103
104 sseloop:
105 // Move the next 16-byte chunk of the data into X1.
106 MOVOU (DI), X1
107
108 // Logical OR to convert data to lowercase
109 POR X2, X1
110
111 // Compare bytes in X0 to X1.
112 PCMPEQB X0, X1
113
114 // Take the top bit of each byte in X1 and put the result in DX.
115 PMOVMSKB X1, DX
116
117 // Count number of matching bytes
118 POPCNTL DX, DX
119
120 // Accumulate into R12
121 ADDQ DX, R12
122
123 // Advance to next block.
124 ADDQ $16, DI
125
126 sseloopentry:
127 CMPQ DI, AX
128 JBE sseloop
129
130 // Get the number of bytes to consider in the last 16 bytes
131 ANDQ $15, BX
132 JZ end
133
134 // Create mask to ignore overlap between previous 16 byte block
135 // and the next.
136 MOVQ $16, CX
137 SUBQ BX, CX
138 MOVQ $0xFFFF, R10
139 SARQ CL, R10
140 SALQ CL, R10
141
142 // Process the last 16-byte chunk. This chunk may overlap with the
143 // chunks we've already searched so we need to mask part of it.
144 MOVOU (AX), X1
145 POR X2, X1 // Convert data to lowercase
146 PCMPEQB X0, X1
147 PMOVMSKB X1, DX
148
149 // Apply mask
150 ANDQ R10, DX
151 POPCNTL DX, DX
152 ADDQ DX, R12
153
154 end:
155 MOVQ R12, (R8)
156 RET
157
158 // handle for lengths < 16
159 small:
160 TESTQ BX, BX
161 JEQ endzero
162
163 // Check if we'll load across a page boundary.
164 LEAQ 16(SI), AX
165 TESTW $0xff0, AX
166 JEQ endofpage
167
168 // We must ignore high bytes as they aren't part of our slice.
169 // Create mask.
170 MOVB BX, CX
171 MOVQ $1, R10
172 SALQ CL, R10
173 SUBQ $1, R10
174
175 // Load data
176 MOVOU (SI), X1
177
178 // Convert data to lowercase
179 POR X2, X1
180
181 // Compare target byte with each byte in data.
182 PCMPEQB X0, X1
183
184 // Move result bits to integer register.
185 PMOVMSKB X1, DX
186
187 // Apply mask
188 ANDQ R10, DX
189 POPCNTL DX, DX
190
191 // Directly return DX, we don't need to accumulate
192 // since we have <16 bytes.
193 MOVQ DX, (R8)
194 RET
195
196 endzero:
197 MOVQ $0, (R8)
198 RET
199
200 endofpage:
201 // We must ignore low bytes as they aren't part of our slice.
202 MOVQ $16, CX
203 SUBQ BX, CX
204 MOVQ $0xFFFF, R10
205 SARQ CL, R10
206 SALQ CL, R10
207
208 // Load data into the high end of X1.
209 MOVOU -16(SI)(BX*1), X1
210
211 // Convert data to lowercase
212 POR X2, X1
213
214 // Compare target byte with each byte in data.
215 PCMPEQB X0, X1
216
217 // Move result bits to integer register.
218 PMOVMSKB X1, DX
219
220 // Apply mask
221 ANDQ R10, DX
222
223 // Directly return DX, we don't need to accumulate
224 // since we have <16 bytes.
225 POPCNTL DX, DX
226 MOVQ DX, (R8)
227 RET
228
229 avx2:
230 #ifndef hasAVX2
231 CMPB golang·org∕x∕sys∕cpu·X86+const_offsetX86HasAVX2(SB), $1
232 JNE sse
233
234 #endif
235 // Create a mask in Y6 that converts text to upper case.
236 VPBROADCASTB X2, Y6
237 MOVD AX, X0
238 LEAQ -64(SI)(BX*1), R11
239 LEAQ (SI)(BX*1), R13
240 VPBROADCASTB X0, Y1
241 PCALIGN $32
242
243 avx2_loop:
244 VMOVDQU (DI), Y2
245 VMOVDQU 32(DI), Y4
246 VPOR Y6, Y2, Y2 // Convert data to lowercase
247 VPOR Y6, Y4, Y4 // Convert data to lowercase
248 VPCMPEQB Y1, Y2, Y3
249 VPCMPEQB Y1, Y4, Y5
250 VPMOVMSKB Y3, DX
251 VPMOVMSKB Y5, CX
252 POPCNTL DX, DX
253 POPCNTL CX, CX
254 ADDQ DX, R12
255 ADDQ CX, R12
256 ADDQ $64, DI
257 CMPQ DI, R11
258 JLE avx2_loop
259
260 // If last block is already processed,
261 // skip to the end.
262 //
263 // This check is NOT an optimization; if the input length is a
264 // multiple of 64, we must not go through the last leg of the
265 // function because the bit shift count passed to SALQ below would
266 // be 64, which is outside of the 0-63 range supported by those
267 // instructions.
268 //
269 // Tests in the bytes and strings packages with input lengths that
270 // are multiples of 64 will break if this condition were removed.
271 CMPQ DI, R13
272 JEQ endavx
273
274 // Load address of the last 64 bytes.
275 // There is an overlap with the previous block.
276 MOVQ R11, DI
277 VMOVDQU (DI), Y2
278 VMOVDQU 32(DI), Y4
279 VPOR Y6, Y2, Y2 // Convert data to lowercase
280 VPOR Y6, Y4, Y4 // Convert data to lowercase
281 VPCMPEQB Y1, Y2, Y3
282 VPCMPEQB Y1, Y4, Y5
283 VPMOVMSKB Y3, DX
284 VPMOVMSKB Y5, CX
285
286 // Exit AVX mode.
287 VZEROUPPER
288 SALQ $32, CX
289 ORQ CX, DX
290
291 // Create mask to ignore overlap between previous 64 byte block
292 // and the next.
293 ANDQ $63, BX
294 MOVQ $64, CX
295 SUBQ BX, CX
296 MOVQ $0xFFFFFFFFFFFFFFFF, R10
297 SALQ CL, R10
298
299 // Apply mask
300 ANDQ R10, DX
301 POPCNTQ DX, DX
302 ADDQ DX, R12
303 MOVQ R12, (R8)
304 RET
305
306 endavx:
307 // Exit AVX mode.
308 VZEROUPPER
309 MOVQ R12, (R8)
310 RET
311
312 // input:
313 // SI: data
314 // BX: data len
315 // AL: byte sought
316 // R8: address to put result
317 // This function requires the POPCNT instruction.
318 TEXT countbody<>(SB), NOSPLIT, $0
319 // Shuffle X0 around so that each byte contains
320 // the character we're looking for.
321 MOVD AX, X0
322 PUNPCKLBW X0, X0
323 PUNPCKLBW X0, X0
324 PSHUFL $0, X0, X0
325
326 CMPQ BX, $16
327 JLT small
328
329 MOVQ $0, R12 // Accumulator
330
331 MOVQ SI, DI
332
333 CMPQ BX, $64
334 JAE avx2
335
336 sse:
337 LEAQ -16(SI)(BX*1), AX // AX = address of last 16 bytes
338 JMP sseloopentry
339
340 PCALIGN $16
341
342 sseloop:
343 // Move the next 16-byte chunk of the data into X1.
344 MOVOU (DI), X1
345
346 // Compare bytes in X0 to X1.
347 PCMPEQB X0, X1
348
349 // Take the top bit of each byte in X1 and put the result in DX.
350 PMOVMSKB X1, DX
351
352 // Count number of matching bytes
353 POPCNTL DX, DX
354
355 // Accumulate into R12
356 ADDQ DX, R12
357
358 // Advance to next block.
359 ADDQ $16, DI
360
361 sseloopentry:
362 CMPQ DI, AX
363 JBE sseloop
364
365 // Get the number of bytes to consider in the last 16 bytes
366 ANDQ $15, BX
367 JZ end
368
369 // Create mask to ignore overlap between previous 16 byte block
370 // and the next.
371 MOVQ $16, CX
372 SUBQ BX, CX
373 MOVQ $0xFFFF, R10
374 SARQ CL, R10
375 SALQ CL, R10
376
377 // Process the last 16-byte chunk. This chunk may overlap with the
378 // chunks we've already searched so we need to mask part of it.
379 MOVOU (AX), X1
380 PCMPEQB X0, X1
381 PMOVMSKB X1, DX
382
383 // Apply mask
384 ANDQ R10, DX
385 POPCNTL DX, DX
386 ADDQ DX, R12
387
388 end:
389 MOVQ R12, (R8)
390 RET
391
392 // handle for lengths < 16
393 small:
394 TESTQ BX, BX
395 JEQ endzero
396
397 // Check if we'll load across a page boundary.
398 LEAQ 16(SI), AX
399 TESTW $0xff0, AX
400 JEQ endofpage
401
402 // We must ignore high bytes as they aren't part of our slice.
403 // Create mask.
404 MOVB BX, CX
405 MOVQ $1, R10
406 SALQ CL, R10
407 SUBQ $1, R10
408
409 // Load data
410 MOVOU (SI), X1
411
412 // Compare target byte with each byte in data.
413 PCMPEQB X0, X1
414
415 // Move result bits to integer register.
416 PMOVMSKB X1, DX
417
418 // Apply mask
419 ANDQ R10, DX
420 POPCNTL DX, DX
421
422 // Directly return DX, we don't need to accumulate
423 // since we have <16 bytes.
424 MOVQ DX, (R8)
425 RET
426
427 endzero:
428 MOVQ $0, (R8)
429 RET
430
431 endofpage:
432 // We must ignore low bytes as they aren't part of our slice.
433 MOVQ $16, CX
434 SUBQ BX, CX
435 MOVQ $0xFFFF, R10
436 SARQ CL, R10
437 SALQ CL, R10
438
439 // Load data into the high end of X1.
440 MOVOU -16(SI)(BX*1), X1
441
442 // Compare target byte with each byte in data.
443 PCMPEQB X0, X1
444
445 // Move result bits to integer register.
446 PMOVMSKB X1, DX
447
448 // Apply mask
449 ANDQ R10, DX
450
451 // Directly return DX, we don't need to accumulate
452 // since we have <16 bytes.
453 POPCNTL DX, DX
454 MOVQ DX, (R8)
455 RET
456
457 avx2:
458 #ifndef hasAVX2
459 CMPB golang·org∕x∕sys∕cpu·X86+const_offsetX86HasAVX2(SB), $1
460 JNE sse
461
462 #endif
463 MOVD AX, X0
464 LEAQ -64(SI)(BX*1), R11
465 LEAQ (SI)(BX*1), R13
466 VPBROADCASTB X0, Y1
467 PCALIGN $32
468
469 avx2_loop:
470 VMOVDQU (DI), Y2
471 VMOVDQU 32(DI), Y4
472 VPCMPEQB Y1, Y2, Y3
473 VPCMPEQB Y1, Y4, Y5
474 VPMOVMSKB Y3, DX
475 VPMOVMSKB Y5, CX
476 POPCNTL DX, DX
477 POPCNTL CX, CX
478 ADDQ DX, R12
479 ADDQ CX, R12
480 ADDQ $64, DI
481 CMPQ DI, R11
482 JLE avx2_loop
483
484 // If last block is already processed,
485 // skip to the end.
486 //
487 // This check is NOT an optimization; if the input length is a
488 // multiple of 64, we must not go through the last leg of the
489 // function because the bit shift count passed to SALQ below would
490 // be 64, which is outside of the 0-63 range supported by those
491 // instructions.
492 //
493 // Tests in the bytes and strings packages with input lengths that
494 // are multiples of 64 will break if this condition were removed.
495 CMPQ DI, R13
496 JEQ endavx
497
498 // Load address of the last 64 bytes.
499 // There is an overlap with the previous block.
500 MOVQ R11, DI
501 VMOVDQU (DI), Y2
502 VMOVDQU 32(DI), Y4
503 VPCMPEQB Y1, Y2, Y3
504 VPCMPEQB Y1, Y4, Y5
505 VPMOVMSKB Y3, DX
506 VPMOVMSKB Y5, CX
507
508 // Exit AVX mode.
509 VZEROUPPER
510 SALQ $32, CX
511 ORQ CX, DX
512
513 // Create mask to ignore overlap between previous 64 byte block
514 // and the next.
515 ANDQ $63, BX
516 MOVQ $64, CX
517 SUBQ BX, CX
518 MOVQ $0xFFFFFFFFFFFFFFFF, R10
519 SALQ CL, R10
520
521 // Apply mask
522 ANDQ R10, DX
523 POPCNTQ DX, DX
524 ADDQ DX, R12
525 MOVQ R12, (R8)
526 RET
527
528 endavx:
529 // Exit AVX mode.
530 VZEROUPPER
531 MOVQ R12, (R8)
532 RET