| 1 |
#include "textflag.h" |
| 2 |
|
| 3 |
// func IndexByteTwo(s []byte, b1, b2 byte) int |
| 4 |
// |
| 5 |
// Returns the index of the first occurrence of b1 or b2 in s, or -1. |
| 6 |
// Uses ARM64 NEON to search for both bytes in a single pass over the data. |
| 7 |
// Adapted from Go's internal/bytealg/indexbyte_arm64.s (single-byte version). |
| 8 |
TEXT ·IndexByteTwo(SB),NOSPLIT,$0-40 |
| 9 |
MOVD s_base+0(FP), R0 |
| 10 |
MOVD s_len+8(FP), R2 |
| 11 |
MOVBU b1+24(FP), R1 |
| 12 |
MOVBU b2+25(FP), R7 |
| 13 |
MOVD $ret+32(FP), R8 |
| 14 |
|
| 15 |
// Core algorithm: |
| 16 |
// For each 32-byte chunk we calculate a 64-bit syndrome value, |
| 17 |
// with two bits per byte. We compare against both b1 and b2, |
| 18 |
// OR the results, then use the same syndrome extraction as |
| 19 |
// Go's IndexByte. |
| 20 |
|
| 21 |
CBZ R2, fail |
| 22 |
MOVD R0, R11 |
| 23 |
// Magic constant 0x40100401 allows us to identify which lane matches. |
| 24 |
// Each byte in the group of 4 gets a distinct bit: 1, 4, 16, 64. |
| 25 |
MOVD $0x40100401, R5 |
| 26 |
VMOV R1, V0.B16 // V0 = splat(b1) |
| 27 |
VMOV R7, V7.B16 // V7 = splat(b2) |
| 28 |
// Work with aligned 32-byte chunks |
| 29 |
BIC $0x1f, R0, R3 |
| 30 |
VMOV R5, V5.S4 |
| 31 |
ANDS $0x1f, R0, R9 |
| 32 |
AND $0x1f, R2, R10 |
| 33 |
BEQ loop |
| 34 |
|
| 35 |
// Input string is not 32-byte aligned. Process the first |
| 36 |
// aligned 32-byte block and mask off bytes before our start. |
| 37 |
VLD1.P (R3), [V1.B16, V2.B16] |
| 38 |
SUB $0x20, R9, R4 |
| 39 |
ADDS R4, R2, R2 |
| 40 |
// Compare against both needles |
| 41 |
VCMEQ V0.B16, V1.B16, V3.B16 // b1 vs first 16 bytes |
| 42 |
VCMEQ V7.B16, V1.B16, V8.B16 // b2 vs first 16 bytes |
| 43 |
VORR V8.B16, V3.B16, V3.B16 // combine |
| 44 |
VCMEQ V0.B16, V2.B16, V4.B16 // b1 vs second 16 bytes |
| 45 |
VCMEQ V7.B16, V2.B16, V9.B16 // b2 vs second 16 bytes |
| 46 |
VORR V9.B16, V4.B16, V4.B16 // combine |
| 47 |
// Build syndrome |
| 48 |
VAND V5.B16, V3.B16, V3.B16 |
| 49 |
VAND V5.B16, V4.B16, V4.B16 |
| 50 |
VADDP V4.B16, V3.B16, V6.B16 |
| 51 |
VADDP V6.B16, V6.B16, V6.B16 |
| 52 |
VMOV V6.D[0], R6 |
| 53 |
// Clear the irrelevant lower bits |
| 54 |
LSL $1, R9, R4 |
| 55 |
LSR R4, R6, R6 |
| 56 |
LSL R4, R6, R6 |
| 57 |
// The first block can also be the last |
| 58 |
BLS masklast |
| 59 |
// Have we found something already? |
| 60 |
CBNZ R6, tail |
| 61 |
|
| 62 |
loop: |
| 63 |
VLD1.P (R3), [V1.B16, V2.B16] |
| 64 |
SUBS $0x20, R2, R2 |
| 65 |
// Compare against both needles, OR results |
| 66 |
VCMEQ V0.B16, V1.B16, V3.B16 |
| 67 |
VCMEQ V7.B16, V1.B16, V8.B16 |
| 68 |
VORR V8.B16, V3.B16, V3.B16 |
| 69 |
VCMEQ V0.B16, V2.B16, V4.B16 |
| 70 |
VCMEQ V7.B16, V2.B16, V9.B16 |
| 71 |
VORR V9.B16, V4.B16, V4.B16 |
| 72 |
// If we're out of data we finish regardless of the result |
| 73 |
BLS end |
| 74 |
// Fast check: OR both halves and check for any match |
| 75 |
VORR V4.B16, V3.B16, V6.B16 |
| 76 |
VADDP V6.D2, V6.D2, V6.D2 |
| 77 |
VMOV V6.D[0], R6 |
| 78 |
CBZ R6, loop |
| 79 |
|
| 80 |
end: |
| 81 |
// Found something or out of data, build full syndrome |
| 82 |
VAND V5.B16, V3.B16, V3.B16 |
| 83 |
VAND V5.B16, V4.B16, V4.B16 |
| 84 |
VADDP V4.B16, V3.B16, V6.B16 |
| 85 |
VADDP V6.B16, V6.B16, V6.B16 |
| 86 |
VMOV V6.D[0], R6 |
| 87 |
// Only mask for the last block |
| 88 |
BHS tail |
| 89 |
|
| 90 |
masklast: |
| 91 |
// Clear irrelevant upper bits |
| 92 |
ADD R9, R10, R4 |
| 93 |
AND $0x1f, R4, R4 |
| 94 |
SUB $0x20, R4, R4 |
| 95 |
NEG R4<<1, R4 |
| 96 |
LSL R4, R6, R6 |
| 97 |
LSR R4, R6, R6 |
| 98 |
|
| 99 |
tail: |
| 100 |
CBZ R6, fail |
| 101 |
RBIT R6, R6 |
| 102 |
SUB $0x20, R3, R3 |
| 103 |
CLZ R6, R6 |
| 104 |
ADD R6>>1, R3, R0 |
| 105 |
SUB R11, R0, R0 |
| 106 |
MOVD R0, (R8) |
| 107 |
RET |
| 108 |
|
| 109 |
fail: |
| 110 |
MOVD $-1, R0 |
| 111 |
MOVD R0, (R8) |
| 112 |
RET |
| 113 |
|
| 114 |
// func lastIndexByteTwo(s []byte, b1, b2 byte) int |
| 115 |
// |
| 116 |
// Returns the index of the last occurrence of b1 or b2 in s, or -1. |
| 117 |
// Scans backward using ARM64 NEON. |
| 118 |
TEXT ·lastIndexByteTwo(SB),NOSPLIT,$0-40 |
| 119 |
MOVD s_base+0(FP), R0 |
| 120 |
MOVD s_len+8(FP), R2 |
| 121 |
MOVBU b1+24(FP), R1 |
| 122 |
MOVBU b2+25(FP), R7 |
| 123 |
MOVD $ret+32(FP), R8 |
| 124 |
|
| 125 |
CBZ R2, lfail |
| 126 |
MOVD R0, R11 // save base |
| 127 |
ADD R0, R2, R12 // R12 = end = base + len |
| 128 |
MOVD $0x40100401, R5 |
| 129 |
VMOV R1, V0.B16 // V0 = splat(b1) |
| 130 |
VMOV R7, V7.B16 // V7 = splat(b2) |
| 131 |
VMOV R5, V5.S4 |
| 132 |
|
| 133 |
// Align: find the aligned block containing the last byte |
| 134 |
SUB $1, R12, R3 |
| 135 |
BIC $0x1f, R3, R3 // R3 = start of aligned block containing last byte |
| 136 |
|
| 137 |
// --- Process tail block --- |
| 138 |
VLD1 (R3), [V1.B16, V2.B16] |
| 139 |
VCMEQ V0.B16, V1.B16, V3.B16 |
| 140 |
VCMEQ V7.B16, V1.B16, V8.B16 |
| 141 |
VORR V8.B16, V3.B16, V3.B16 |
| 142 |
VCMEQ V0.B16, V2.B16, V4.B16 |
| 143 |
VCMEQ V7.B16, V2.B16, V9.B16 |
| 144 |
VORR V9.B16, V4.B16, V4.B16 |
| 145 |
VAND V5.B16, V3.B16, V3.B16 |
| 146 |
VAND V5.B16, V4.B16, V4.B16 |
| 147 |
VADDP V4.B16, V3.B16, V6.B16 |
| 148 |
VADDP V6.B16, V6.B16, V6.B16 |
| 149 |
VMOV V6.D[0], R6 |
| 150 |
|
| 151 |
// Mask upper bits (bytes past end of slice) |
| 152 |
// tail_bytes = end - R3 (1..32) |
| 153 |
SUB R3, R12, R10 // R10 = tail_bytes |
| 154 |
MOVD $64, R4 |
| 155 |
SUB R10<<1, R4, R4 // R4 = 64 - 2*tail_bytes |
| 156 |
LSL R4, R6, R6 |
| 157 |
LSR R4, R6, R6 |
| 158 |
|
| 159 |
// Is this also the head block? |
| 160 |
CMP R11, R3 // R3 - R11 |
| 161 |
BLO lmaskfirst // R3 < base: head+tail in same block |
| 162 |
BEQ ltailonly // R3 == base: single aligned block |
| 163 |
|
| 164 |
// R3 > base: more blocks before this one |
| 165 |
CBNZ R6, llast |
| 166 |
B lbacksetup |
| 167 |
|
| 168 |
ltailonly: |
| 169 |
// Single block, already masked upper bits |
| 170 |
CBNZ R6, llast |
| 171 |
B lfail |
| 172 |
|
| 173 |
lmaskfirst: |
| 174 |
// Mask lower bits (bytes before start of slice) |
| 175 |
SUB R3, R11, R4 // R4 = base - R3 |
| 176 |
LSL $1, R4, R4 |
| 177 |
LSR R4, R6, R6 |
| 178 |
LSL R4, R6, R6 |
| 179 |
CBNZ R6, llast |
| 180 |
B lfail |
| 181 |
|
| 182 |
lbacksetup: |
| 183 |
SUB $0x20, R3 |
| 184 |
|
| 185 |
lbackloop: |
| 186 |
VLD1 (R3), [V1.B16, V2.B16] |
| 187 |
VCMEQ V0.B16, V1.B16, V3.B16 |
| 188 |
VCMEQ V7.B16, V1.B16, V8.B16 |
| 189 |
VORR V8.B16, V3.B16, V3.B16 |
| 190 |
VCMEQ V0.B16, V2.B16, V4.B16 |
| 191 |
VCMEQ V7.B16, V2.B16, V9.B16 |
| 192 |
VORR V9.B16, V4.B16, V4.B16 |
| 193 |
// Quick check: any match in this block? |
| 194 |
VORR V4.B16, V3.B16, V6.B16 |
| 195 |
VADDP V6.D2, V6.D2, V6.D2 |
| 196 |
VMOV V6.D[0], R6 |
| 197 |
|
| 198 |
// Is this a head block? (R3 < base) |
| 199 |
CMP R11, R3 |
| 200 |
BLO lheadblock |
| 201 |
|
| 202 |
// Full block (R3 >= base) |
| 203 |
CBNZ R6, lbackfound |
| 204 |
// More blocks? |
| 205 |
BEQ lfail // R3 == base, no more |
| 206 |
SUB $0x20, R3 |
| 207 |
B lbackloop |
| 208 |
|
| 209 |
lbackfound: |
| 210 |
// Build full syndrome |
| 211 |
VAND V5.B16, V3.B16, V3.B16 |
| 212 |
VAND V5.B16, V4.B16, V4.B16 |
| 213 |
VADDP V4.B16, V3.B16, V6.B16 |
| 214 |
VADDP V6.B16, V6.B16, V6.B16 |
| 215 |
VMOV V6.D[0], R6 |
| 216 |
B llast |
| 217 |
|
| 218 |
lheadblock: |
| 219 |
// R3 < base. Build full syndrome if quick check had a match. |
| 220 |
CBZ R6, lfail |
| 221 |
VAND V5.B16, V3.B16, V3.B16 |
| 222 |
VAND V5.B16, V4.B16, V4.B16 |
| 223 |
VADDP V4.B16, V3.B16, V6.B16 |
| 224 |
VADDP V6.B16, V6.B16, V6.B16 |
| 225 |
VMOV V6.D[0], R6 |
| 226 |
// Mask lower bits |
| 227 |
SUB R3, R11, R4 // R4 = base - R3 |
| 228 |
LSL $1, R4, R4 |
| 229 |
LSR R4, R6, R6 |
| 230 |
LSL R4, R6, R6 |
| 231 |
CBZ R6, lfail |
| 232 |
|
| 233 |
llast: |
| 234 |
// Find last match: highest set bit in syndrome |
| 235 |
// Syndrome has bit 2i set for matching byte i. |
| 236 |
// CLZ gives leading zeros; byte_offset = (63 - CLZ) / 2. |
| 237 |
CLZ R6, R6 |
| 238 |
MOVD $63, R4 |
| 239 |
SUB R6, R4, R6 // R6 = 63 - CLZ = bit position |
| 240 |
LSR $1, R6 // R6 = byte offset within block |
| 241 |
ADD R3, R6, R0 // R0 = absolute address |
| 242 |
SUB R11, R0, R0 // R0 = slice index |
| 243 |
MOVD R0, (R8) |
| 244 |
RET |
| 245 |
|
| 246 |
lfail: |
| 247 |
MOVD $-1, R0 |
| 248 |
MOVD R0, (R8) |
| 249 |
RET |