Debian Codegraph @

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