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 "textflag.h"
6
7 // func LastIndexByte(b []byte, c byte) int
8 // input:
9 // R0: b ptr
10 // R1: b len
11 // R2: b cap (unused)
12 // R3: c byte to search
13 // return
14 // R0: result
15 TEXT ·LastIndexByte<ABIInternal>(SB),NOSPLIT,$0-40
16 MOVD R3, R2
17 B ·LastIndexByteString<ABIInternal>(SB)
18
19 // func LastIndexByteString(s string, c byte) int
20 // input:
21 // R0: s ptr
22 // R1: s len
23 // R2: c byte to search
24 // return
25 // R0: result
26 TEXT ·LastIndexByteString<ABIInternal>(SB),NOSPLIT,$0-32
27 // Core algorithm uses the same chunked NEON comparison as
28 // IndexByteString (see indexbyte_arm64.s): we load 16-byte chunks,
29 // compare against the target byte, and narrow the per-byte
30 // comparison mask down to one nibble per byte in a general purpose
31 // register. Unlike IndexByteString, we scan from the end of the
32 // string towards the start, and within a chunk we pick the
33 // highest-indexed match (the last matching nibble) instead of the
34 // lowest.
35
36 #define PTR R0
37 #define LEN R1
38 #define TARGET R2
39 #define WIN R3
40 #define MASK R4
41 #define VTARGET V0.B16
42
43 // Length 0, nothing to do.
44 CBZ LEN, fail
45
46 // Make vector containing the byte we're searching for in each lane.
47 VMOV TARGET, VTARGET // [c c c ... c c c]
48
49 // Small, need to be extra careful about out of bounds.
50 CMP $16, LEN
51 BLT small
52
53 // WIN = address of the rightmost 16-byte window.
54 ADD PTR, LEN, WIN
55 SUB $16, WIN
56
57 loop:
58 VLD1 (WIN), [V2.B16] // load data
59 VCMEQ VTARGET, V2.B16, V2.B16 // compare each byte against the target byte
60 VSHRN $4, V2.H8, V2.B8 // compact to one nibble per byte
61 VMOV V2.D[0], MASK // move to general purpose register
62 CBNZ MASK, found
63 SUB $16, WIN // move the window to the left
64 CMP PTR, WIN
65 BHI loop
66
67 // Check the leftmost 16-byte window, starting exactly at PTR. This
68 // window may overlap with the windows we've already checked, but
69 // that's ok.
70 MOVD PTR, WIN
71 VLD1 (WIN), [V2.B16]
72 VCMEQ VTARGET, V2.B16, V2.B16
73 VSHRN $4, V2.H8, V2.B8
74 VMOV V2.D[0], MASK
75 CBZ MASK, fail
76
77 // MASK contains a nonzero nibble bitmask of matches within the 16-byte
78 // window loaded from WIN; find the highest (rightmost) one.
79 found:
80 CLZ MASK, R9
81 MOVD $63, R10
82 SUB R9, R10, R9 // R9 = index of the highest set bit
83 LSR $2, R9, R9 // R9 = nibble index of the rightmost match in the window
84 SUB PTR, WIN, R10 // R10 = offset of the window start from the string start
85 ADD R9, R10, R0
86 RET
87
88 fail:
89 MOVD $-1, R0
90 RET
91
92 // 1-15 bytes
93 PCALIGN $16
94 small:
95 AND $0xf, PTR, R8 // R8 = offset of start in 16-byte region
96 ADD R8, LEN, R9 // R9 = offset of end (from start's 16-byte region boundary)
97 MOVD $0, R10 // R10 = low bits of match data to throw away
98 CMP $16, R9
99 BGT smallNoAdjust // straddles two 16-byte regions - safe to load directly
100
101 // data is all within a single 16-byte region
102 BIC $0xf, PTR, PTR // round down to start of 16-byte region
103 LSL $2, R8, R10 // throw away the match bits below original start
104
105 smallNoAdjust:
106 VLD1 (PTR), [V2.B16]
107 VCMEQ VTARGET, V2.B16, V2.B16
108 VSHRN $4, V2.H8, V2.B8 // compact to one nibble per byte
109 VMOV V2.D[0], MASK // move to general purpose register
110 LSR R10, MASK, MASK // discard matches before string start
111
112 // Discard matches at or beyond the end of the string.
113 MOVD $1, R9
114 LSL $2, LEN, R11
115 LSL R11, R9, R9
116 SUB $1, R9, R9
117 AND R9, MASK, MASK
118 CBZ MASK, fail
119
120 CLZ MASK, R9
121 MOVD $63, R11
122 SUB R9, R11, R9
123 LSR $2, R9, R0
124 RET
125
View as plain text