1 // Copyright 2026 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 #include "go_asm.h"
6 #include "textflag.h"
7
8 // input:
9 // R4 = b_base
10 // R5 = b_len
11 // R6 = b_cap (unused)
12 // R7 = byte to find
13 TEXT ·LastIndexByte<ABIInternal>(SB),NOSPLIT,$0-40
14 AND $0xff, R7
15 JMP lastindexbytebody<>(SB)
16
17 // input:
18 // R4 = s_base
19 // R5 = s_len
20 // R6 = byte to find
21 TEXT ·LastIndexByteString<ABIInternal>(SB),NOSPLIT,$0-32
22 AND $0xff, R6, R7 // byte to find
23 JMP lastindexbytebody<>(SB)
24
25 // input:
26 // R4: b_base
27 // R5: len
28 // R7: byte to find
29 TEXT lastindexbytebody<>(SB),NOSPLIT,$0
30 BEQ R5, notfound // len == 0
31
32 MOVV R4, R6 // store base for later
33 ADDV R4, R5, R8 // R8 = current right end (exclusive)
34
35 MOVV $32, R9
36 BGE R5, R9, lasx
37 tail:
38 // Scalar backward scan, one 8-byte word at a time.
39 // Bytes are extracted from the highest byte down so that
40 // the last match in the word is found first.
41 MOVV $8, R9
42 BLT R5, R9, lt_8
43 generic8_loop:
44 ADDV $-8, R8
45 MOVV (R8), R10
46
47 BSTRPICKV $63, R10, $56, R11
48 BEQ R7, R11, found_add_7
49
50 BSTRPICKV $55, R10, $48, R11
51 BEQ R7, R11, found_add_6
52
53 BSTRPICKV $47, R10, $40, R11
54 BEQ R7, R11, found_add_5
55
56 BSTRPICKV $39, R10, $32, R11
57 BEQ R7, R11, found_add_4
58
59 BSTRPICKV $31, R10, $24, R11
60 BEQ R7, R11, found_add_3
61
62 BSTRPICKV $23, R10, $16, R11
63 BEQ R7, R11, found_add_2
64
65 BSTRPICKV $15, R10, $8, R11
66 BEQ R7, R11, found_add_1
67
68 AND $0xff, R10, R11
69 BEQ R7, R11, found_r8
70
71 ADDV $-8, R5
72 BGE R5, R9, generic8_loop
73
74 lt_8:
75 // Fewer than 8 bytes left (possibly 0 after generic8_loop):
76 // scan backward byte by byte.
77 BEQ R4, R8, notfound
78 ADDV $-1, R8
79 MOVBU (R8), R10
80 BEQ R7, R10, found_r8
81 JMP lt_8
82
83 found_add_7:
84 ADDV $7, R8
85 JMP found_r8
86 found_add_6:
87 ADDV $6, R8
88 JMP found_r8
89 found_add_5:
90 ADDV $5, R8
91 JMP found_r8
92 found_add_4:
93 ADDV $4, R8
94 JMP found_r8
95 found_add_3:
96 ADDV $3, R8
97 JMP found_r8
98 found_add_2:
99 ADDV $2, R8
100 JMP found_r8
101 found_add_1:
102 ADDV $1, R8
103 JMP found_r8
104
105 found_r8:
106 MOVV R8, R4 // R8 points at the matching byte
107 found:
108 SUBV R6, R4
109 RET
110
111 notfound:
112 MOVV $-1, R4
113 RET
114
115 lasx:
116 MOVBU internal∕cpu·Loong64+const_offsetLOONG64HasLASX(SB), R9
117 BEQ R9, lsx
118 XVMOVQ R7, X0.B32
119
120 MOVV $128, R9
121 BLT R5, R9, lasx32_entry
122 lasx128_loop:
123 ADDV $-128, R8
124 XVMOVQ 0(R8), X1
125 XVMOVQ 32(R8), X2
126 XVMOVQ 64(R8), X3
127 XVMOVQ 96(R8), X4
128
129 // Check higher offsets first: the last match wins.
130 XVSEQB X4, X0, X5
131 XVSETNEV X5, FCC0
132 BFPT lasx_found_add_96
133
134 XVSEQB X3, X0, X5
135 XVSETNEV X5, FCC0
136 BFPT lasx_found_add_64
137
138 XVSEQB X2, X0, X5
139 XVSETNEV X5, FCC0
140 BFPT lasx_found_add_32
141
142 XVSEQB X1, X0, X5
143 XVSETNEV X5, FCC0
144 BFPT lasx_found_add_0
145
146 ADDV $-128, R5
147 BGE R5, R9, lasx128_loop
148
149 BEQ R5, notfound // all bytes checked
150
151 // 0 < R5 < 128
152 lasx32_entry:
153 MOVV $32, R9
154 BLT R5, R9, tail
155 lasx32_loop:
156 ADDV $-32, R8
157 XVMOVQ 0(R8), X1
158
159 XVSEQB X1, X0, X5
160 XVSETNEV X5, FCC0
161 BFPT lasx_found_add_0
162
163 ADDV $-32, R5
164 BGE R5, R9, lasx32_loop
165
166 BEQ R5, notfound
167
168 JMP tail // 0 < R5 < 32: finish with the scalar tail
169
170 lasx_found_add_0:
171 MOVV R0, R11
172 JMP lasx_index_cal
173
174 lasx_found_add_32:
175 MOVV $32, R11
176 JMP lasx_index_cal
177
178 lasx_found_add_64:
179 MOVV $64, R11
180 JMP lasx_index_cal
181
182 lasx_found_add_96:
183 MOVV $96, R11
184 JMP lasx_index_cal
185
186 lasx_index_cal:
187 // X5 holds the compare result of the matching block.
188 // Find the last set byte: scan 64-bit lanes from high to
189 // low; a lane is non-zero iff its clz != 64. R11 = block
190 // offset; add the lane base (24/16/8/0) for the first
191 // non-zero lane. Byte index in lane = 7 - (clz>>3).
192 MOVV $64, R9
193 XVMOVQ X5.V[3], R10
194 CLZV R10, R10
195 BNE R10, R9, lasx_last_add_24
196
197 XVMOVQ X5.V[2], R10
198 CLZV R10, R10
199 BNE R10, R9, lasx_last_add_16
200
201 XVMOVQ X5.V[1], R10
202 CLZV R10, R10
203 BNE R10, R9, lasx_last_add_8
204
205 XVMOVQ X5.V[0], R10
206 CLZV R10, R10
207 JMP lasx_last_cal
208
209 lasx_last_add_24:
210 ADDV $24, R11
211 JMP lasx_last_cal
212 lasx_last_add_16:
213 ADDV $16, R11
214 JMP lasx_last_cal
215 lasx_last_add_8:
216 ADDV $8, R11
217
218 lasx_last_cal:
219 SRLV $3, R10
220 MOVV $7, R9
221 SUBV R10, R9 // byte index of last match in lane
222 ADDV R9, R11
223 ADDV R11, R8
224 MOVV R8, R4
225 JMP found
226
227 lsx:
228 MOVBU internal∕cpu·Loong64+const_offsetLOONG64HasLSX(SB), R9
229 BEQ R9, tail
230 VMOVQ R7, V0.B16
231
232 MOVV $64, R9
233 BLT R5, R9, lsx16_entry
234 lsx64_loop:
235 ADDV $-64, R8
236 VMOVQ 0(R8), V1
237 VMOVQ 16(R8), V2
238 VMOVQ 32(R8), V3
239 VMOVQ 48(R8), V4
240
241 // Check higher offsets first: the last match wins.
242 VSEQB V4, V0, V5
243 VSETNEV V5, FCC0
244 BFPT lsx_found_add_48
245
246 VSEQB V3, V0, V5
247 VSETNEV V5, FCC0
248 BFPT lsx_found_add_32
249
250 VSEQB V2, V0, V5
251 VSETNEV V5, FCC0
252 BFPT lsx_found_add_16
253
254 VSEQB V1, V0, V5
255 VSETNEV V5, FCC0
256 BFPT lsx_found_add_0
257
258 ADDV $-64, R5
259 BGE R5, R9, lsx64_loop
260
261 BEQ R5, notfound
262
263 // 0 < R5 < 64
264 lsx16_entry:
265 MOVV $16, R9
266 BLT R5, R9, tail
267 lsx16_loop:
268 ADDV $-16, R8
269 VMOVQ 0(R8), V1
270
271 VSEQB V1, V0, V5
272 VSETNEV V5, FCC0
273 BFPT lsx_found_add_0
274
275 ADDV $-16, R5
276 BGE R5, R9, lsx16_loop
277
278 BEQ R5, notfound
279
280 JMP tail // 0 < R5 < 16: finish with the scalar tail
281
282 lsx_found_add_0:
283 MOVV R0, R11
284 JMP lsx_index_cal
285
286 lsx_found_add_16:
287 MOVV $16, R11
288 JMP lsx_index_cal
289
290 lsx_found_add_32:
291 MOVV $32, R11
292 JMP lsx_index_cal
293
294 lsx_found_add_48:
295 MOVV $48, R11
296 JMP lsx_index_cal
297
298 lsx_index_cal:
299 // V5 holds the compare result of the matching block.
300 // Find the last set byte: scan 64-bit lanes from high to
301 // low; a lane is non-zero iff its clz != 64. R11 = block
302 // offset; add the lane base (8/0) for the first non-zero
303 // lane. Byte index in lane = 7 - (clz>>3).
304 MOVV $64, R9
305
306 VMOVQ V5.V[1], R10
307 CLZV R10, R10
308 BNE R10, R9, lsx_last_add_8
309
310 VMOVQ V5.V[0], R10
311 CLZV R10, R10
312 JMP lsx_last_cal
313
314 lsx_last_add_8:
315 ADDV $8, R11
316
317 lsx_last_cal:
318 SRLV $3, R10
319 MOVV $7, R9
320 SUBV R10, R9 // byte index of last match in lane
321 ADDV R9, R11
322 ADDV R11, R8
323 MOVV R8, R4
324 JMP found
325
View as plain text