| 1 |
// Copyright 2023 Charlie Vieth. All rights reserved. |
| 2 |
// Use of this source code is governed by the MIT license. |
| 3 |
|
| 4 |
// The following code is modified version of the standard library's |
| 5 |
// internal/bytealg/indexbyte_armd64.s the Go LICENSE can be found in |
| 6 |
// the go.LICENSE file. |
| 7 |
|
| 8 |
#include "textflag.h" |
| 9 |
|
| 10 |
TEXT ·IndexByte(SB), NOSPLIT, $0-40 |
| 11 |
MOVD b_base+0(FP), R0 |
| 12 |
MOVD b_len+8(FP), R2 |
| 13 |
MOVBU c+24(FP), R1 |
| 14 |
MOVD $ret+32(FP), R8 |
| 15 |
|
| 16 |
// Test if c is a letter. |
| 17 |
SUB $65, R1, R3 |
| 18 |
CMPW $25, R3 |
| 19 |
BLS index_case // c is a letter |
| 20 |
SUB $97, R1, R3 |
| 21 |
CMPW $25, R3 |
| 22 |
BHI index // c is not a letter |
| 23 |
|
| 24 |
index_case: |
| 25 |
B indexbytebodyCase<>(SB) |
| 26 |
|
| 27 |
index: |
| 28 |
B indexbytebody<>(SB) |
| 29 |
|
| 30 |
TEXT ·IndexByteString(SB), NOSPLIT, $0-32 |
| 31 |
MOVD s_base+0(FP), R0 |
| 32 |
MOVD s_len+8(FP), R2 |
| 33 |
MOVBU c+16(FP), R1 |
| 34 |
MOVD $ret+24(FP), R8 |
| 35 |
|
| 36 |
// Test if c is a letter. |
| 37 |
SUB $65, R1, R3 |
| 38 |
CMPW $25, R3 |
| 39 |
BLS index_case // c is a letter |
| 40 |
SUB $97, R1, R3 |
| 41 |
CMPW $25, R3 |
| 42 |
BHI index // c is not a letter |
| 43 |
|
| 44 |
index_case: |
| 45 |
B indexbytebodyCase<>(SB) |
| 46 |
|
| 47 |
index: |
| 48 |
B indexbytebody<>(SB) |
| 49 |
|
| 50 |
// indexbytebodyCase is a case insensitive version indexbytebody |
| 51 |
// the byte being sought *must* be an ASCII letter. |
| 52 |
// |
| 53 |
// input: |
| 54 |
// R0: data |
| 55 |
// R1: byte to search |
| 56 |
// R2: data len |
| 57 |
// R8: address to put result |
| 58 |
TEXT indexbytebodyCase<>(SB), NOSPLIT, $0 |
| 59 |
// Core algorithm: |
| 60 |
// For each 32-byte chunk we calculate a 64-bit syndrome value, |
| 61 |
// with two bits per byte. For each tuple, bit 0 is set if the |
| 62 |
// relevant byte matched the requested character and bit 1 is |
| 63 |
// not used (faster than using a 32bit syndrome). Since the bits |
| 64 |
// in the syndrome reflect exactly the order in which things occur |
| 65 |
// in the original string, counting trailing zeros allows to |
| 66 |
// identify exactly which byte has matched. |
| 67 |
|
| 68 |
CBZ R2, fail |
| 69 |
MOVD R0, R11 |
| 70 |
|
| 71 |
// Magic constant 0x40100401 allows us to identify |
| 72 |
// which lane matches the requested byte. |
| 73 |
// 0x40100401 = ((1<<0) + (4<<8) + (16<<16) + (64<<24)) |
| 74 |
// Different bytes have different bit masks (i.e: 1, 4, 16, 64) |
| 75 |
MOVD $0x40100401, R5 |
| 76 |
|
| 77 |
// Convert byte being searched for to lowercase. |
| 78 |
ORR $32, R1, R1 |
| 79 |
VMOV R1, V0.B16 |
| 80 |
|
| 81 |
// Bit mask to convert upper-case chars to lower-case. |
| 82 |
VMOVQ $0x2020202020202020, $0x2020202020202020, V7 |
| 83 |
|
| 84 |
// Work with aligned 32-byte chunks |
| 85 |
BIC $0x1f, R0, R3 |
| 86 |
VMOV R5, V5.S4 |
| 87 |
ANDS $0x1f, R0, R9 |
| 88 |
AND $0x1f, R2, R10 |
| 89 |
BEQ loop |
| 90 |
|
| 91 |
// Input string is not 32-byte aligned. We calculate the |
| 92 |
// syndrome value for the aligned 32 bytes block containing |
| 93 |
// the first bytes and mask off the irrelevant part. |
| 94 |
VLD1.P (R3), [V1.B16, V2.B16] |
| 95 |
VORR V7.B16, V1.B16, V1.B16 // Convert to lowercase |
| 96 |
VORR V7.B16, V2.B16, V2.B16 // Convert to lowercase |
| 97 |
SUB $0x20, R9, R4 |
| 98 |
ADDS R4, R2, R2 |
| 99 |
VCMEQ V0.B16, V1.B16, V3.B16 |
| 100 |
VCMEQ V0.B16, V2.B16, V4.B16 |
| 101 |
VAND V5.B16, V3.B16, V3.B16 |
| 102 |
VAND V5.B16, V4.B16, V4.B16 |
| 103 |
VADDP V4.B16, V3.B16, V6.B16 // 256->128 |
| 104 |
VADDP V6.B16, V6.B16, V6.B16 // 128->64 |
| 105 |
VMOV V6.D[0], R6 |
| 106 |
|
| 107 |
// Clear the irrelevant lower bits |
| 108 |
LSL $1, R9, R4 |
| 109 |
LSR R4, R6, R6 |
| 110 |
LSL R4, R6, R6 |
| 111 |
|
| 112 |
// The first block can also be the last |
| 113 |
BLS masklast |
| 114 |
|
| 115 |
// Have we found something already? |
| 116 |
CBNZ R6, tail |
| 117 |
|
| 118 |
loop: |
| 119 |
VLD1.P (R3), [V1.B16, V2.B16] |
| 120 |
SUBS $0x20, R2, R2 |
| 121 |
VORR V7.B16, V1.B16, V1.B16 // Convert to lowercase |
| 122 |
VORR V7.B16, V2.B16, V2.B16 // Convert to lowercase |
| 123 |
VCMEQ V0.B16, V1.B16, V3.B16 |
| 124 |
VCMEQ V0.B16, V2.B16, V4.B16 |
| 125 |
|
| 126 |
// If we're out of data we finish regardless of the result |
| 127 |
BLS end |
| 128 |
|
| 129 |
// Use a fast check for the termination condition |
| 130 |
VORR V4.B16, V3.B16, V6.B16 |
| 131 |
VADDP V6.D2, V6.D2, V6.D2 |
| 132 |
VMOV V6.D[0], R6 |
| 133 |
|
| 134 |
// We're not out of data, loop if we haven't found the character |
| 135 |
CBZ R6, loop |
| 136 |
|
| 137 |
end: |
| 138 |
// Termination condition found, let's calculate the syndrome value |
| 139 |
VAND V5.B16, V3.B16, V3.B16 |
| 140 |
VAND V5.B16, V4.B16, V4.B16 |
| 141 |
VADDP V4.B16, V3.B16, V6.B16 |
| 142 |
VADDP V6.B16, V6.B16, V6.B16 |
| 143 |
VMOV V6.D[0], R6 |
| 144 |
|
| 145 |
// Only do the clear for the last possible block with less than 32 bytes |
| 146 |
// Condition flags come from SUBS in the loop |
| 147 |
BHS tail |
| 148 |
|
| 149 |
masklast: |
| 150 |
// Clear the irrelevant upper bits |
| 151 |
ADD R9, R10, R4 |
| 152 |
AND $0x1f, R4, R4 |
| 153 |
SUB $0x20, R4, R4 |
| 154 |
NEG R4<<1, R4 |
| 155 |
LSL R4, R6, R6 |
| 156 |
LSR R4, R6, R6 |
| 157 |
|
| 158 |
tail: |
| 159 |
// Check that we have found a character |
| 160 |
CBZ R6, fail |
| 161 |
|
| 162 |
// Count the trailing zeros using bit reversing |
| 163 |
RBIT R6, R6 |
| 164 |
|
| 165 |
// Compensate the last post-increment |
| 166 |
SUB $0x20, R3, R3 |
| 167 |
|
| 168 |
// And count the leading zeros |
| 169 |
CLZ R6, R6 |
| 170 |
|
| 171 |
// R6 is twice the offset into the fragment |
| 172 |
ADD R6>>1, R3, R0 |
| 173 |
|
| 174 |
// Compute the offset result |
| 175 |
SUB R11, R0, R0 |
| 176 |
MOVD R0, (R8) |
| 177 |
RET |
| 178 |
|
| 179 |
fail: |
| 180 |
MOVD $-1, R0 |
| 181 |
MOVD R0, (R8) |
| 182 |
RET |
| 183 |
|
| 184 |
// indexbytebody is the same as internal/bytealg/indexbyte_arm64.s (go1.20) |
| 185 |
// |
| 186 |
// input: |
| 187 |
// R0: data |
| 188 |
// R1: byte to search |
| 189 |
// R2: data len |
| 190 |
// R8: address to put result |
| 191 |
TEXT indexbytebody<>(SB), NOSPLIT, $0 |
| 192 |
// Core algorithm: |
| 193 |
// For each 32-byte chunk we calculate a 64-bit syndrome value, |
| 194 |
// with two bits per byte. For each tuple, bit 0 is set if the |
| 195 |
// relevant byte matched the requested character and bit 1 is |
| 196 |
// not used (faster than using a 32bit syndrome). Since the bits |
| 197 |
// in the syndrome reflect exactly the order in which things occur |
| 198 |
// in the original string, counting trailing zeros allows to |
| 199 |
// identify exactly which byte has matched. |
| 200 |
|
| 201 |
CBZ R2, fail |
| 202 |
MOVD R0, R11 |
| 203 |
|
| 204 |
// Magic constant 0x40100401 allows us to identify |
| 205 |
// which lane matches the requested byte. |
| 206 |
// 0x40100401 = ((1<<0) + (4<<8) + (16<<16) + (64<<24)) |
| 207 |
// Different bytes have different bit masks (i.e: 1, 4, 16, 64) |
| 208 |
MOVD $0x40100401, R5 |
| 209 |
VMOV R1, V0.B16 |
| 210 |
|
| 211 |
// Work with aligned 32-byte chunks |
| 212 |
BIC $0x1f, R0, R3 |
| 213 |
VMOV R5, V5.S4 |
| 214 |
ANDS $0x1f, R0, R9 |
| 215 |
AND $0x1f, R2, R10 |
| 216 |
BEQ loop |
| 217 |
|
| 218 |
// Input string is not 32-byte aligned. We calculate the |
| 219 |
// syndrome value for the aligned 32 bytes block containing |
| 220 |
// the first bytes and mask off the irrelevant part. |
| 221 |
VLD1.P (R3), [V1.B16, V2.B16] |
| 222 |
SUB $0x20, R9, R4 |
| 223 |
ADDS R4, R2, R2 |
| 224 |
VCMEQ V0.B16, V1.B16, V3.B16 |
| 225 |
VCMEQ V0.B16, V2.B16, V4.B16 |
| 226 |
VAND V5.B16, V3.B16, V3.B16 |
| 227 |
VAND V5.B16, V4.B16, V4.B16 |
| 228 |
VADDP V4.B16, V3.B16, V6.B16 // 256->128 |
| 229 |
VADDP V6.B16, V6.B16, V6.B16 // 128->64 |
| 230 |
VMOV V6.D[0], R6 |
| 231 |
|
| 232 |
// Clear the irrelevant lower bits |
| 233 |
LSL $1, R9, R4 |
| 234 |
LSR R4, R6, R6 |
| 235 |
LSL R4, R6, R6 |
| 236 |
|
| 237 |
// The first block can also be the last |
| 238 |
BLS masklast |
| 239 |
|
| 240 |
// Have we found something already? |
| 241 |
CBNZ R6, tail |
| 242 |
|
| 243 |
loop: |
| 244 |
VLD1.P (R3), [V1.B16, V2.B16] |
| 245 |
SUBS $0x20, R2, R2 |
| 246 |
VCMEQ V0.B16, V1.B16, V3.B16 |
| 247 |
VCMEQ V0.B16, V2.B16, V4.B16 |
| 248 |
|
| 249 |
// If we're out of data we finish regardless of the result |
| 250 |
BLS end |
| 251 |
|
| 252 |
// Use a fast check for the termination condition |
| 253 |
VORR V4.B16, V3.B16, V6.B16 |
| 254 |
VADDP V6.D2, V6.D2, V6.D2 |
| 255 |
VMOV V6.D[0], R6 |
| 256 |
|
| 257 |
// We're not out of data, loop if we haven't found the character |
| 258 |
CBZ R6, loop |
| 259 |
|
| 260 |
end: |
| 261 |
// Termination condition found, let's calculate the syndrome value |
| 262 |
VAND V5.B16, V3.B16, V3.B16 |
| 263 |
VAND V5.B16, V4.B16, V4.B16 |
| 264 |
VADDP V4.B16, V3.B16, V6.B16 |
| 265 |
VADDP V6.B16, V6.B16, V6.B16 |
| 266 |
VMOV V6.D[0], R6 |
| 267 |
|
| 268 |
// Only do the clear for the last possible block with less than 32 bytes |
| 269 |
// Condition flags come from SUBS in the loop |
| 270 |
BHS tail |
| 271 |
|
| 272 |
masklast: |
| 273 |
// Clear the irrelevant upper bits |
| 274 |
ADD R9, R10, R4 |
| 275 |
AND $0x1f, R4, R4 |
| 276 |
SUB $0x20, R4, R4 |
| 277 |
NEG R4<<1, R4 |
| 278 |
LSL R4, R6, R6 |
| 279 |
LSR R4, R6, R6 |
| 280 |
|
| 281 |
tail: |
| 282 |
// Check that we have found a character |
| 283 |
CBZ R6, fail |
| 284 |
|
| 285 |
// Count the trailing zeros using bit reversing |
| 286 |
RBIT R6, R6 |
| 287 |
|
| 288 |
// Compensate the last post-increment |
| 289 |
SUB $0x20, R3, R3 |
| 290 |
|
| 291 |
// And count the leading zeros |
| 292 |
CLZ R6, R6 |
| 293 |
|
| 294 |
// R6 is twice the offset into the fragment |
| 295 |
ADD R6>>1, R3, R0 |
| 296 |
|
| 297 |
// Compute the offset result |
| 298 |
SUB R11, R0, R0 |
| 299 |
MOVD R0, (R8) |
| 300 |
RET |
| 301 |
|
| 302 |
fail: |
| 303 |
MOVD $-1, R0 |
| 304 |
MOVD R0, (R8) |
| 305 |
RET |