Debian Codegraph @

internal/bytealg/count_go122_arm64.s

raw
1 //go:build arm64 && go1.22
2 // +build arm64,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 "textflag.h"
10
11 TEXT ·Count(SB), NOSPLIT, $0-40
12 MOVD b_base+0(FP), R0
13 MOVD b_len+8(FP), R2
14 MOVBU c+24(FP), R1
15 MOVD $ret+32(FP), R8
16
17 // Test if c is a letter.
18 SUB $65, R1, R3
19 CMPW $25, R3
20 BLS index_case // c is a letter
21 SUB $97, R1, R3
22 CMPW $25, R3
23 BHI index // c is a letter
24
25 index_case:
26 B countbytebodyCase<>(SB)
27
28 index:
29 B countbytebody<>(SB)
30
31 TEXT ·CountString(SB), NOSPLIT, $0-32
32 MOVD s_base+0(FP), R0
33 MOVD s_len+8(FP), R2
34 MOVBU c+16(FP), R1
35 MOVD $ret+24(FP), R8
36
37 // Test if c is a letter.
38 SUB $65, R1, R3
39 CMPW $25, R3
40 BLS index_case // c is a letter
41 SUB $97, R1, R3
42 CMPW $25, R3
43 BHI index // c is not a letter
44
45 index_case:
46 B countbytebodyCase<>(SB)
47
48 index:
49 B countbytebody<>(SB)
50
51 // input:
52 // R0: data
53 // R2: data len
54 // R1: byte to find
55 // R8: address to put result
56 TEXT countbytebodyCase<>(SB), NOSPLIT, $0
57 // R11 = count of byte to search
58 MOVD $0, R11
59
60 // short path to handle 0-byte case
61 CBZ R2, done
62 CMP $0x20, R2
63
64 // Convert byte being searched for to lowercase.
65 ORR $32, R1, R1
66
67 // Bit mask to convert upper-case chars to lower-case.
68 VMOVQ $0x2020202020202020, $0x2020202020202020, V9
69
70 // jump directly to tail if length < 32
71 BLO tail
72 ANDS $0x1f, R0, R9
73 BEQ chunk
74
75 // Work with not 32-byte aligned head
76 BIC $0x1f, R0, R3
77 ADD $0x20, R3
78 PCALIGN $16
79
80 head_loop:
81 MOVBU.P 1(R0), R5
82 CMP R5, R1
83 CINC EQ, R11, R11
84 SUB $1, R2, R2
85 CMP R0, R3
86 BNE head_loop
87
88 // Work with 32-byte aligned chunks
89 chunk:
90 BIC $0x1f, R2, R9
91
92 // The first chunk can also be the last
93 CBZ R9, tail
94
95 // R3 = end of 32-byte chunks
96 ADD R0, R9, R3
97 MOVD $1, R5
98 VMOV R5, V5.B16
99
100 // R2 = length of tail
101 SUB R9, R2, R2
102
103 // Duplicate R1 (byte to search) to 16 1-byte elements of V0
104 VMOV R1, V0.B16
105
106 // Clear the low 64-bit element of V7 and V8
107 VEOR V7.B8, V7.B8, V7.B8
108 VEOR V8.B8, V8.B8, V8.B8
109 PCALIGN $16
110
111 // Count the target byte in 32-byte chunk
112 chunk_loop:
113 VLD1.P (R0), [V1.B16, V2.B16]
114 VORR V9.B16, V1.B16, V1.B16 // Convert to lowercase
115 VORR V9.B16, V2.B16, V2.B16 // Convert to lowercase
116 CMP R0, R3
117 VCMEQ V0.B16, V1.B16, V3.B16
118 VCMEQ V0.B16, V2.B16, V4.B16
119
120 // Clear the higher 7 bits
121 VAND V5.B16, V3.B16, V3.B16
122 VAND V5.B16, V4.B16, V4.B16
123
124 // Count lanes match the requested byte
125 VADDP V4.B16, V3.B16, V6.B16 // 32B->16B
126 VUADDLV V6.B16, V7
127
128 // Accumulate the count in low 64-bit element of V8 when inside the loop
129 VADD V7, V8
130 BNE chunk_loop
131 VMOV V8.D[0], R6
132 ADD R6, R11, R11
133 CBZ R2, done
134
135 tail:
136 // Work with tail shorter than 32 bytes
137 MOVBU.P 1(R0), R5
138 ORR $32, R5, R5 // Convert to lowercase
139 SUB $1, R2, R2
140 CMP R5, R1
141 CINC EQ, R11, R11
142 CBNZ R2, tail
143
144 done:
145 MOVD R11, (R8)
146 RET
147
148 // input:
149 // R0: data
150 // R2: data len
151 // R1: byte to find
152 // R8: address to put result
153 TEXT countbytebody<>(SB), NOSPLIT, $0
154 // R11 = count of byte to search
155 MOVD $0, R11
156
157 // short path to handle 0-byte case
158 CBZ R2, done
159 CMP $0x20, R2
160
161 // jump directly to tail if length < 32
162 BLO tail
163 ANDS $0x1f, R0, R9
164 BEQ chunk
165
166 // Work with not 32-byte aligned head
167 BIC $0x1f, R0, R3
168 ADD $0x20, R3
169 PCALIGN $16
170
171 head_loop:
172 MOVBU.P 1(R0), R5
173 CMP R5, R1
174 CINC EQ, R11, R11
175 SUB $1, R2, R2
176 CMP R0, R3
177 BNE head_loop
178
179 // Work with 32-byte aligned chunks
180 chunk:
181 BIC $0x1f, R2, R9
182
183 // The first chunk can also be the last
184 CBZ R9, tail
185
186 // R3 = end of 32-byte chunks
187 ADD R0, R9, R3
188 MOVD $1, R5
189 VMOV R5, V5.B16
190
191 // R2 = length of tail
192 SUB R9, R2, R2
193
194 // Duplicate R1 (byte to search) to 16 1-byte elements of V0
195 VMOV R1, V0.B16
196
197 // Clear the low 64-bit element of V7 and V8
198 VEOR V7.B8, V7.B8, V7.B8
199 VEOR V8.B8, V8.B8, V8.B8
200 PCALIGN $16
201
202 // Count the target byte in 32-byte chunk
203 chunk_loop:
204 VLD1.P (R0), [V1.B16, V2.B16]
205 CMP R0, R3
206 VCMEQ V0.B16, V1.B16, V3.B16
207 VCMEQ V0.B16, V2.B16, V4.B16
208
209 // Clear the higher 7 bits
210 VAND V5.B16, V3.B16, V3.B16
211 VAND V5.B16, V4.B16, V4.B16
212
213 // Count lanes match the requested byte
214 VADDP V4.B16, V3.B16, V6.B16 // 32B->16B
215 VUADDLV V6.B16, V7
216
217 // Accumulate the count in low 64-bit element of V8 when inside the loop
218 VADD V7, V8
219 BNE chunk_loop
220 VMOV V8.D[0], R6
221 ADD R6, R11, R11
222 CBZ R2, done
223
224 tail:
225 // Work with tail shorter than 32 bytes
226 MOVBU.P 1(R0), R5
227 SUB $1, R2, R2
228 CMP R5, R1
229 CINC EQ, R11, R11
230 CBNZ R2, tail
231
232 done:
233 MOVD R11, (R8)
234 RET