Debian Codegraph @

internal/bytealg/index_non_ascii_go122_amd64.s

raw
1 //go:build amd64 && go1.22
2 // +build amd64,go1.22
3
4 #include "go_asm.h"
5 #include "textflag.h"
6
7 // TODO: can we use popcnt for this ???
8
9 TEXT ·IndexByteNonASCII(SB), NOSPLIT, $0-32
10 MOVQ b_base+0(FP), SI
11 MOVQ b_len+8(FP), BX
12 LEAQ ret+24(FP), R8
13
14 JMP indexByteBodyNonASCII<>(SB)
15
16 TEXT ·IndexNonASCII(SB), NOSPLIT, $0-24
17 MOVQ s_base+0(FP), SI
18 MOVQ s_len+8(FP), BX
19 LEAQ ret+16(FP), R8
20
21 JMP indexByteBodyNonASCII<>(SB)
22
23 // input:
24 // SI: data
25 // R8: address to put result
26 TEXT indexByteBodyNonASCII<>(SB), NOSPLIT, $0
27 // Shuffle X0 around so that each byte contains
28 // the character we're looking for.
29 MOVQ $0x80, AX
30 MOVD AX, X0
31 PUNPCKLBW X0, X0
32 PUNPCKLBW X0, X0
33 PSHUFL $0, X0, X0
34
35 CMPQ BX, $16
36 JLT small
37
38 MOVQ SI, DI
39
40 CMPQ BX, $32
41 JA avx2
42
43 sse:
44 LEAQ -16(SI)(BX*1), AX // AX = address of last 16 bytes
45 JMP sseloopentry
46
47 PCALIGN $16
48
49 sseloop:
50 // Move the next 16-byte chunk of the data into X1.
51 MOVOU (DI), X1
52
53 // Logical or to check for top-bit (RuneSelf)
54 PAND X1, X0
55
56 // Take the top bit of each byte in X1 and put the result in DX.
57 PMOVMSKB X1, DX
58
59 // Find first set bit, if any.
60 BSFL DX, DX
61 JNZ ssesuccess
62
63 // Advance to next block.
64 ADDQ $16, DI
65
66 sseloopentry:
67 CMPQ DI, AX
68 JB sseloop
69
70 // Search the last 16-byte chunk. This chunk may overlap with the
71 // chunks we've already searched, but that's ok.
72 MOVQ AX, DI
73 MOVOU (AX), X1
74 PAND X1, X0 // Check if RuneSelf is set
75 PMOVMSKB X1, DX
76 BSFL DX, DX
77 JNZ ssesuccess
78
79 failure:
80 MOVQ $-1, (R8)
81 RET
82
83 // We've found a chunk containing the byte.
84 // The chunk was loaded from DI.
85 // The index of the matching byte in the chunk is DX.
86 // The start of the data is SI.
87 ssesuccess:
88 SUBQ SI, DI // Compute offset of chunk within data.
89 ADDQ DX, DI // Add offset of byte within chunk.
90 MOVQ DI, (R8)
91 RET
92
93 // handle for lengths < 16
94 small:
95 TESTQ BX, BX
96 JEQ failure
97
98 // Check if we'll load across a page boundary.
99 LEAQ 16(SI), AX
100 TESTW $0xff0, AX
101 JEQ endofpage
102
103 MOVOU (SI), X1 // Load data
104 PAND X1, X0 // Check if RuneSelf is set
105 PMOVMSKB X1, DX // Move result bits to integer register.
106 BSFL DX, DX // Find first set bit.
107 JZ failure // No set bit, failure.
108 CMPL DX, BX
109 JAE failure // Match is past end of data.
110 MOVQ DX, (R8)
111 RET
112
113 endofpage:
114 MOVOU -16(SI)(BX*1), X1 // Load data into the high end of X1.
115 PAND X1, X0 // Check if RuneSelf is set
116 PMOVMSKB X1, DX // Move result bits to integer register.
117 MOVL BX, CX
118 SHLL CX, DX
119 SHRL $16, DX // Shift desired bits down to bottom of register.
120 BSFL DX, DX // Find first set bit.
121 JZ failure // No set bit, failure.
122 MOVQ DX, (R8)
123 RET
124
125 avx2:
126 #ifndef hasAVX2
127 CMPB golang·org∕x∕sys∕cpu·X86+const_offsetX86HasAVX2(SB), $1
128 JNE sse
129
130 #endif
131 // Create a mask in Y4 that to check for 0x80
132 MOVD AX, X0 // $0x80 already stored in AX
133 VPBROADCASTB X0, Y4
134
135 MOVD AX, X0
136 LEAQ -32(SI)(BX*1), R11
137 VPBROADCASTB X0, Y1
138
139 PCALIGN $32
140
141 avx2_loop:
142 VMOVDQU (DI), Y2
143 VPAND Y4, Y2, Y2 // AND for top bit 0x80
144 VPCMPEQB Y1, Y2, Y3
145 VPTEST Y3, Y3
146 JNZ avx2success
147 ADDQ $32, DI
148 CMPQ DI, R11
149 JLT avx2_loop
150 MOVQ R11, DI
151 VMOVDQU (DI), Y2
152 VPAND Y4, Y2, Y2 // AND for top bit 0x80
153 VPCMPEQB Y1, Y2, Y3
154 VPTEST Y3, Y3
155 JNZ avx2success
156 VZEROUPPER
157 MOVQ $-1, (R8)
158 RET
159
160 avx2success:
161 VPMOVMSKB Y3, DX
162 BSFL DX, DX
163 SUBQ SI, DI
164 ADDQ DI, DX
165 MOVQ DX, (R8)
166 VZEROUPPER
167 RET