Debian Codegraph @

internal/bytealg/index_non_ascii_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 sseloop:
48 // Move the next 16-byte chunk of the data into X1.
49 MOVOU (DI), X1
50
51 // Logical or to check for top-bit (RuneSelf)
52 PAND X1, X0
53
54 // Take the top bit of each byte in X1 and put the result in DX.
55 PMOVMSKB X1, DX
56
57 // Find first set bit, if any.
58 BSFL DX, DX
59 JNZ ssesuccess
60
61 // Advance to next block.
62 ADDQ $16, DI
63
64 sseloopentry:
65 CMPQ DI, AX
66 JB sseloop
67
68 // Search the last 16-byte chunk. This chunk may overlap with the
69 // chunks we've already searched, but that's ok.
70 MOVQ AX, DI
71 MOVOU (AX), X1
72 PAND X1, X0 // Check if RuneSelf is set
73 PMOVMSKB X1, DX
74 BSFL DX, DX
75 JNZ ssesuccess
76
77 failure:
78 MOVQ $-1, (R8)
79 RET
80
81 // We've found a chunk containing the byte.
82 // The chunk was loaded from DI.
83 // The index of the matching byte in the chunk is DX.
84 // The start of the data is SI.
85 ssesuccess:
86 SUBQ SI, DI // Compute offset of chunk within data.
87 ADDQ DX, DI // Add offset of byte within chunk.
88 MOVQ DI, (R8)
89 RET
90
91 // handle for lengths < 16
92 small:
93 TESTQ BX, BX
94 JEQ failure
95
96 // Check if we'll load across a page boundary.
97 LEAQ 16(SI), AX
98 TESTW $0xff0, AX
99 JEQ endofpage
100
101 MOVOU (SI), X1 // Load data
102 PAND X1, X0 // Check if RuneSelf is set
103 PMOVMSKB X1, DX // Move result bits to integer register.
104 BSFL DX, DX // Find first set bit.
105 JZ failure // No set bit, failure.
106 CMPL DX, BX
107 JAE failure // Match is past end of data.
108 MOVQ DX, (R8)
109 RET
110
111 endofpage:
112 MOVOU -16(SI)(BX*1), X1 // Load data into the high end of X1.
113 PAND X1, X0 // Check if RuneSelf is set
114 PMOVMSKB X1, DX // Move result bits to integer register.
115 MOVL BX, CX
116 SHLL CX, DX
117 SHRL $16, DX // Shift desired bits down to bottom of register.
118 BSFL DX, DX // Find first set bit.
119 JZ failure // No set bit, failure.
120 MOVQ DX, (R8)
121 RET
122
123 avx2:
124 #ifndef hasAVX2
125 CMPB golang·org∕x∕sys∕cpu·X86+const_offsetX86HasAVX2(SB), $1
126 JNE sse
127
128 #endif
129 // Create a mask in Y4 that to check for 0x80
130 MOVD AX, X0 // $0x80 already stored in AX
131 VPBROADCASTB X0, Y4
132
133 MOVD AX, X0
134 LEAQ -32(SI)(BX*1), R11
135 VPBROADCASTB X0, Y1
136
137 avx2_loop:
138 VMOVDQU (DI), Y2
139 VPAND Y4, Y2, Y2 // AND for top bit 0x80
140 VPCMPEQB Y1, Y2, Y3
141 VPTEST Y3, Y3
142 JNZ avx2success
143 ADDQ $32, DI
144 CMPQ DI, R11
145 JLT avx2_loop
146 MOVQ R11, DI
147 VMOVDQU (DI), Y2
148 VPAND Y4, Y2, Y2 // AND for top bit 0x80
149 VPCMPEQB Y1, Y2, Y3
150 VPTEST Y3, Y3
151 JNZ avx2success
152 VZEROUPPER
153 MOVQ $-1, (R8)
154 RET
155
156 avx2success:
157 VPMOVMSKB Y3, DX
158 BSFL DX, DX
159 SUBQ SI, DI
160 ADDQ DI, DX
161 MOVQ DX, (R8)
162 VZEROUPPER
163 RET