Debian Codegraph @

internal/bytealg/indexbyte_go122_amd64.s

raw
1 //go:build amd64 && go1.22
2 // +build amd64,go1.22
3
4 // Copyright 2023 Charlie Vieth. All rights reserved.
5 // Use of this source code is governed by the MIT license.
6
7 // The following code is modified version of the standard library's
8 // internal/bytealg/indexbyte_amd64.s the Go LICENSE can be found in
9 // the go.LICENSE file.
10
11 #include "go_asm.h"
12 #include "textflag.h"
13
14 TEXT ·IndexByte(SB), NOSPLIT, $0-40
15 MOVQ b_base+0(FP), SI
16 MOVQ b_len+8(FP), BX
17 MOVB c+24(FP), AL
18 LEAQ ret+32(FP), R8
19
20 LEAL -65(AX), CX // Check if the byte is a ASCII letter
21 CMPB CL, $25
22 JLS index_case // Byte sought is a ASCII letter
23 ADDL $-97, AX
24 CMPB AL, $25
25 JHI index
26
27 index_case:
28 MOVB c+24(FP), AL
29 JMP indexbytebodyCase<>(SB)
30
31 index:
32 MOVB c+24(FP), AL
33 JMP indexbytebody<>(SB)
34
35 TEXT ·IndexByteString(SB), NOSPLIT, $0-32
36 MOVQ s_base+0(FP), SI
37 MOVQ s_len+8(FP), BX
38 MOVB c+16(FP), AL
39 LEAQ ret+24(FP), R8
40
41 LEAL -65(AX), CX // Check if the byte is a ASCII letter
42 CMPB CL, $25
43 JLS index_case // Byte sought is a ASCII letter
44 ADDL $-97, AX
45 CMPB AL, $25
46 JHI index
47
48 index_case:
49 MOVB c+16(FP), AL
50 JMP indexbytebodyCase<>(SB)
51
52 index:
53 MOVB c+16(FP), AL
54 JMP indexbytebody<>(SB)
55
56 // indexbytebodyCase is a case insensitive version indexbytebody
57 // the byte being sought *must* be an ASCII letter.
58 //
59 // input:
60 // SI: data
61 // BX: data len
62 // AL: byte sought
63 // R8: address to put result
64 TEXT indexbytebodyCase<>(SB), NOSPLIT, $0
65 // Shuffle X0 around so that each byte contains
66 // the character we're looking for.
67 ORL $32, AX // Convert byte to lowercase
68 MOVD AX, X0
69 PUNPCKLBW X0, X0
70 PUNPCKLBW X0, X0
71 PSHUFL $0, X0, X0
72
73 // Add space (' ') mask to X2
74 MOVQ $32, CX
75 MOVQ CX, X2
76 PUNPCKLBW X2, X2
77 PUNPCKLBW X2, X2
78 PSHUFL $0, X2, X2
79
80 CMPQ BX, $16
81 JLT small
82
83 MOVQ SI, DI
84
85 CMPQ BX, $32
86 JA avx2
87
88 sse:
89 LEAQ -16(SI)(BX*1), AX // AX = address of last 16 bytes
90 JMP sseloopentry
91
92 PCALIGN $16
93
94 sseloop:
95 // Move the next 16-byte chunk of the data into X1.
96 MOVOU (DI), X1
97
98 // Logical OR to convert data to lowercase
99 POR X2, X1
100
101 // Compare bytes in X0 to X1.
102 PCMPEQB X0, X1
103
104 // Take the top bit of each byte in X1 and put the result in DX.
105 PMOVMSKB X1, DX
106
107 // Find first set bit, if any.
108 BSFL DX, DX
109 JNZ ssesuccess
110
111 // Advance to next block.
112 ADDQ $16, DI
113
114 sseloopentry:
115 CMPQ DI, AX
116 JB sseloop
117
118 // Search the last 16-byte chunk. This chunk may overlap with the
119 // chunks we've already searched, but that's ok.
120 MOVQ AX, DI
121 MOVOU (AX), X1
122 POR X2, X1 // Convert data to lowercase
123 PCMPEQB X0, X1
124 PMOVMSKB X1, DX
125 BSFL DX, DX
126 JNZ ssesuccess
127
128 failure:
129 MOVQ $-1, (R8)
130 RET
131
132 // We've found a chunk containing the byte.
133 // The chunk was loaded from DI.
134 // The index of the matching byte in the chunk is DX.
135 // The start of the data is SI.
136 ssesuccess:
137 SUBQ SI, DI // Compute offset of chunk within data.
138 ADDQ DX, DI // Add offset of byte within chunk.
139 MOVQ DI, (R8)
140 RET
141
142 // handle for lengths < 16
143 small:
144 TESTQ BX, BX
145 JEQ failure
146
147 // Check if we'll load across a page boundary.
148 LEAQ 16(SI), AX
149 TESTW $0xff0, AX
150 JEQ endofpage
151
152 MOVOU (SI), X1 // Load data
153 POR X2, X1 // Convert data to lowercase
154 PCMPEQB X0, X1 // Compare target byte with each byte in data.
155 PMOVMSKB X1, DX // Move result bits to integer register.
156 BSFL DX, DX // Find first set bit.
157 JZ failure // No set bit, failure.
158 CMPL DX, BX
159 JAE failure // Match is past end of data.
160 MOVQ DX, (R8)
161 RET
162
163 endofpage:
164 MOVOU -16(SI)(BX*1), X1 // Load data into the high end of X1.
165 POR X2, X1 // Convert data to lowercase
166 PCMPEQB X0, X1 // Compare target byte with each byte in data.
167 PMOVMSKB X1, DX // Move result bits to integer register.
168 MOVL BX, CX
169 SHLL CX, DX
170 SHRL $16, DX // Shift desired bits down to bottom of register.
171 BSFL DX, DX // Find first set bit.
172 JZ failure // No set bit, failure.
173 MOVQ DX, (R8)
174 RET
175
176 avx2:
177 #ifndef hasAVX2
178 CMPB golang·org∕x∕sys∕cpu·X86+const_offsetX86HasAVX2(SB), $1
179 JNE sse
180
181 #endif
182 // Create a mask in Y4 that converts text to upper case.
183 VPBROADCASTB X2, Y4 // space ' ' already stored in X2
184
185 MOVD AX, X0
186 LEAQ -32(SI)(BX*1), R11
187 VPBROADCASTB X0, Y1
188
189 PCALIGN $32
190
191 avx2_loop:
192 VMOVDQU (DI), Y2
193 VPOR Y4, Y2, Y2 // Convert data to lowercase
194 VPCMPEQB Y1, Y2, Y3
195 VPTEST Y3, Y3
196 JNZ avx2success
197 ADDQ $32, DI
198 CMPQ DI, R11
199 JLT avx2_loop
200 MOVQ R11, DI
201 VMOVDQU (DI), Y2
202 VPOR Y4, Y2, Y2 // Convert data to lowercase
203 VPCMPEQB Y1, Y2, Y3
204 VPTEST Y3, Y3
205 JNZ avx2success
206 VZEROUPPER
207 MOVQ $-1, (R8)
208 RET
209
210 avx2success:
211 VPMOVMSKB Y3, DX
212 BSFL DX, DX
213 SUBQ SI, DI
214 ADDQ DI, DX
215 MOVQ DX, (R8)
216 VZEROUPPER
217 RET
218
219 // indexbytebody is the same as internal/bytealg/indexbyte_amd64.s (go1.20)
220 //
221 // input:
222 // SI: data
223 // BX: data len
224 // AL: byte sought
225 // R8: address to put result
226 TEXT indexbytebody<>(SB), NOSPLIT, $0
227 // Shuffle X0 around so that each byte contains
228 // the character we're looking for.
229 MOVD AX, X0
230 PUNPCKLBW X0, X0
231 PUNPCKLBW X0, X0
232 PSHUFL $0, X0, X0
233
234 CMPQ BX, $16
235 JLT small
236
237 MOVQ SI, DI
238
239 CMPQ BX, $32
240 JA avx2
241
242 sse:
243 LEAQ -16(SI)(BX*1), AX // AX = address of last 16 bytes
244 JMP sseloopentry
245
246 PCALIGN $16
247
248 sseloop:
249 // Move the next 16-byte chunk of the data into X1.
250 MOVOU (DI), X1
251
252 // Compare bytes in X0 to X1.
253 PCMPEQB X0, X1
254
255 // Take the top bit of each byte in X1 and put the result in DX.
256 PMOVMSKB X1, DX
257
258 // Find first set bit, if any.
259 BSFL DX, DX
260 JNZ ssesuccess
261
262 // Advance to next block.
263 ADDQ $16, DI
264
265 sseloopentry:
266 CMPQ DI, AX
267 JB sseloop
268
269 // Search the last 16-byte chunk. This chunk may overlap with the
270 // chunks we've already searched, but that's ok.
271 MOVQ AX, DI
272 MOVOU (AX), X1
273 PCMPEQB X0, X1
274 PMOVMSKB X1, DX
275 BSFL DX, DX
276 JNZ ssesuccess
277
278 failure:
279 MOVQ $-1, (R8)
280 RET
281
282 // We've found a chunk containing the byte.
283 // The chunk was loaded from DI.
284 // The index of the matching byte in the chunk is DX.
285 // The start of the data is SI.
286 ssesuccess:
287 SUBQ SI, DI // Compute offset of chunk within data.
288 ADDQ DX, DI // Add offset of byte within chunk.
289 MOVQ DI, (R8)
290 RET
291
292 // handle for lengths < 16
293 small:
294 TESTQ BX, BX
295 JEQ failure
296
297 // Check if we'll load across a page boundary.
298 LEAQ 16(SI), AX
299 TESTW $0xff0, AX
300 JEQ endofpage
301
302 MOVOU (SI), X1 // Load data
303 PCMPEQB X0, X1 // Compare target byte with each byte in data.
304 PMOVMSKB X1, DX // Move result bits to integer register.
305 BSFL DX, DX // Find first set bit.
306 JZ failure // No set bit, failure.
307 CMPL DX, BX
308 JAE failure // Match is past end of data.
309 MOVQ DX, (R8)
310 RET
311
312 endofpage:
313 MOVOU -16(SI)(BX*1), X1 // Load data into the high end of X1.
314 PCMPEQB X0, X1 // Compare target byte with each byte in data.
315 PMOVMSKB X1, DX // Move result bits to integer register.
316 MOVL BX, CX
317 SHLL CX, DX
318 SHRL $16, DX // Shift desired bits down to bottom of register.
319 BSFL DX, DX // Find first set bit.
320 JZ failure // No set bit, failure.
321 MOVQ DX, (R8)
322 RET
323
324 avx2:
325 #ifndef hasAVX2
326 CMPB golang·org∕x∕sys∕cpu·X86+const_offsetX86HasAVX2(SB), $1
327 JNE sse
328
329 #endif
330 MOVD AX, X0
331 LEAQ -32(SI)(BX*1), R11
332 VPBROADCASTB X0, Y1
333
334 PCALIGN $32
335
336 avx2_loop:
337 VMOVDQU (DI), Y2
338 VPCMPEQB Y1, Y2, Y3
339 VPTEST Y3, Y3
340 JNZ avx2success
341 ADDQ $32, DI
342 CMPQ DI, R11
343 JLT avx2_loop
344 MOVQ R11, DI
345 VMOVDQU (DI), Y2
346 VPCMPEQB Y1, Y2, Y3
347 VPTEST Y3, Y3
348 JNZ avx2success
349 VZEROUPPER
350 MOVQ $-1, (R8)
351 RET
352
353 avx2success:
354 VPMOVMSKB Y3, DX
355 BSFL DX, DX
356 SUBQ SI, DI
357 ADDQ DI, DX
358 MOVQ DX, (R8)
359 VZEROUPPER
360 RET