| 1 |
//go:build amd64 && !go1.22 |
| 2 |
// +build amd64,!go1.22 |
| 3 |
|
| 4 |
// Copyright 2023 Charlie Vieth. All rights reserved. |
| 5 |
// Use of this source code is governed by the MIT license. |
| 6 |
|
| 7 |
// The following code is modified version of the standard library's |
| 8 |
// internal/bytealg/indexbyte_amd64.s the Go LICENSE can be found in |
| 9 |
// the go.LICENSE file. |
| 10 |
|
| 11 |
#include "go_asm.h" |
| 12 |
#include "textflag.h" |
| 13 |
|
| 14 |
TEXT ·IndexByte(SB), NOSPLIT, $0-40 |
| 15 |
MOVQ b_base+0(FP), SI |
| 16 |
MOVQ b_len+8(FP), BX |
| 17 |
MOVB c+24(FP), AL |
| 18 |
LEAQ ret+32(FP), R8 |
| 19 |
|
| 20 |
LEAL -65(AX), CX // Check if the byte is a ASCII letter |
| 21 |
CMPB CL, $25 |
| 22 |
JLS index_case // Byte sought is a ASCII letter |
| 23 |
ADDL $-97, AX |
| 24 |
CMPB AL, $25 |
| 25 |
JHI index |
| 26 |
|
| 27 |
index_case: |
| 28 |
MOVB c+24(FP), AL |
| 29 |
JMP indexbytebodyCase<>(SB) |
| 30 |
|
| 31 |
index: |
| 32 |
MOVB c+24(FP), AL |
| 33 |
JMP indexbytebody<>(SB) |
| 34 |
|
| 35 |
TEXT ·IndexByteString(SB), NOSPLIT, $0-32 |
| 36 |
MOVQ s_base+0(FP), SI |
| 37 |
MOVQ s_len+8(FP), BX |
| 38 |
MOVB c+16(FP), AL |
| 39 |
LEAQ ret+24(FP), R8 |
| 40 |
|
| 41 |
LEAL -65(AX), CX // Check if the byte is a ASCII letter |
| 42 |
CMPB CL, $25 |
| 43 |
JLS index_case // Byte sought is a ASCII letter |
| 44 |
ADDL $-97, AX |
| 45 |
CMPB AL, $25 |
| 46 |
JHI index |
| 47 |
|
| 48 |
index_case: |
| 49 |
MOVB c+16(FP), AL |
| 50 |
JMP indexbytebodyCase<>(SB) |
| 51 |
|
| 52 |
index: |
| 53 |
MOVB c+16(FP), AL |
| 54 |
JMP indexbytebody<>(SB) |
| 55 |
|
| 56 |
// indexbytebodyCase is a case insensitive version indexbytebody |
| 57 |
// the byte being sought *must* be an ASCII letter. |
| 58 |
// |
| 59 |
// input: |
| 60 |
// SI: data |
| 61 |
// BX: data len |
| 62 |
// AL: byte sought |
| 63 |
// R8: address to put result |
| 64 |
TEXT indexbytebodyCase<>(SB), NOSPLIT, $0 |
| 65 |
// Shuffle X0 around so that each byte contains |
| 66 |
// the character we're looking for. |
| 67 |
ORL $32, AX // Convert byte to lowercase |
| 68 |
MOVD AX, X0 |
| 69 |
PUNPCKLBW X0, X0 |
| 70 |
PUNPCKLBW X0, X0 |
| 71 |
PSHUFL $0, X0, X0 |
| 72 |
|
| 73 |
// Add space (' ') mask to X2 |
| 74 |
MOVQ $32, CX |
| 75 |
MOVQ CX, X2 |
| 76 |
PUNPCKLBW X2, X2 |
| 77 |
PUNPCKLBW X2, X2 |
| 78 |
PSHUFL $0, X2, X2 |
| 79 |
|
| 80 |
CMPQ BX, $16 |
| 81 |
JLT small |
| 82 |
|
| 83 |
MOVQ SI, DI |
| 84 |
|
| 85 |
CMPQ BX, $32 |
| 86 |
JA avx2 |
| 87 |
|
| 88 |
sse: |
| 89 |
LEAQ -16(SI)(BX*1), AX // AX = address of last 16 bytes |
| 90 |
JMP sseloopentry |
| 91 |
|
| 92 |
sseloop: |
| 93 |
// Move the next 16-byte chunk of the data into X1. |
| 94 |
MOVOU (DI), X1 |
| 95 |
|
| 96 |
// Logical OR to convert data to lowercase |
| 97 |
POR X2, X1 |
| 98 |
|
| 99 |
// Compare bytes in X0 to X1. |
| 100 |
PCMPEQB X0, X1 |
| 101 |
|
| 102 |
// Take the top bit of each byte in X1 and put the result in DX. |
| 103 |
PMOVMSKB X1, DX |
| 104 |
|
| 105 |
// Find first set bit, if any. |
| 106 |
BSFL DX, DX |
| 107 |
JNZ ssesuccess |
| 108 |
|
| 109 |
// Advance to next block. |
| 110 |
ADDQ $16, DI |
| 111 |
|
| 112 |
sseloopentry: |
| 113 |
CMPQ DI, AX |
| 114 |
JB sseloop |
| 115 |
|
| 116 |
// Search the last 16-byte chunk. This chunk may overlap with the |
| 117 |
// chunks we've already searched, but that's ok. |
| 118 |
MOVQ AX, DI |
| 119 |
MOVOU (AX), X1 |
| 120 |
POR X2, X1 // Convert data to lowercase |
| 121 |
PCMPEQB X0, X1 |
| 122 |
PMOVMSKB X1, DX |
| 123 |
BSFL DX, DX |
| 124 |
JNZ ssesuccess |
| 125 |
|
| 126 |
failure: |
| 127 |
MOVQ $-1, (R8) |
| 128 |
RET |
| 129 |
|
| 130 |
// We've found a chunk containing the byte. |
| 131 |
// The chunk was loaded from DI. |
| 132 |
// The index of the matching byte in the chunk is DX. |
| 133 |
// The start of the data is SI. |
| 134 |
ssesuccess: |
| 135 |
SUBQ SI, DI // Compute offset of chunk within data. |
| 136 |
ADDQ DX, DI // Add offset of byte within chunk. |
| 137 |
MOVQ DI, (R8) |
| 138 |
RET |
| 139 |
|
| 140 |
// handle for lengths < 16 |
| 141 |
small: |
| 142 |
TESTQ BX, BX |
| 143 |
JEQ failure |
| 144 |
|
| 145 |
// Check if we'll load across a page boundary. |
| 146 |
LEAQ 16(SI), AX |
| 147 |
TESTW $0xff0, AX |
| 148 |
JEQ endofpage |
| 149 |
|
| 150 |
MOVOU (SI), X1 // Load data |
| 151 |
POR X2, X1 // Convert data to lowercase |
| 152 |
PCMPEQB X0, X1 // Compare target byte with each byte in data. |
| 153 |
PMOVMSKB X1, DX // Move result bits to integer register. |
| 154 |
BSFL DX, DX // Find first set bit. |
| 155 |
JZ failure // No set bit, failure. |
| 156 |
CMPL DX, BX |
| 157 |
JAE failure // Match is past end of data. |
| 158 |
MOVQ DX, (R8) |
| 159 |
RET |
| 160 |
|
| 161 |
endofpage: |
| 162 |
MOVOU -16(SI)(BX*1), X1 // Load data into the high end of X1. |
| 163 |
POR X2, X1 // Convert data to lowercase |
| 164 |
PCMPEQB X0, X1 // Compare target byte with each byte in data. |
| 165 |
PMOVMSKB X1, DX // Move result bits to integer register. |
| 166 |
MOVL BX, CX |
| 167 |
SHLL CX, DX |
| 168 |
SHRL $16, DX // Shift desired bits down to bottom of register. |
| 169 |
BSFL DX, DX // Find first set bit. |
| 170 |
JZ failure // No set bit, failure. |
| 171 |
MOVQ DX, (R8) |
| 172 |
RET |
| 173 |
|
| 174 |
avx2: |
| 175 |
#ifndef hasAVX2 |
| 176 |
CMPB golang·org∕x∕sys∕cpu·X86+const_offsetX86HasAVX2(SB), $1 |
| 177 |
JNE sse |
| 178 |
|
| 179 |
#endif |
| 180 |
// Create a mask in Y4 that converts text to upper case. |
| 181 |
VPBROADCASTB X2, Y4 // space ' ' already stored in X2 |
| 182 |
|
| 183 |
MOVD AX, X0 |
| 184 |
LEAQ -32(SI)(BX*1), R11 |
| 185 |
VPBROADCASTB X0, Y1 |
| 186 |
|
| 187 |
avx2_loop: |
| 188 |
VMOVDQU (DI), Y2 |
| 189 |
VPOR Y4, Y2, Y2 // Convert data to lowercase |
| 190 |
VPCMPEQB Y1, Y2, Y3 |
| 191 |
VPTEST Y3, Y3 |
| 192 |
JNZ avx2success |
| 193 |
ADDQ $32, DI |
| 194 |
CMPQ DI, R11 |
| 195 |
JLT avx2_loop |
| 196 |
MOVQ R11, DI |
| 197 |
VMOVDQU (DI), Y2 |
| 198 |
VPOR Y4, Y2, Y2 // Convert data to lowercase |
| 199 |
VPCMPEQB Y1, Y2, Y3 |
| 200 |
VPTEST Y3, Y3 |
| 201 |
JNZ avx2success |
| 202 |
VZEROUPPER |
| 203 |
MOVQ $-1, (R8) |
| 204 |
RET |
| 205 |
|
| 206 |
avx2success: |
| 207 |
VPMOVMSKB Y3, DX |
| 208 |
BSFL DX, DX |
| 209 |
SUBQ SI, DI |
| 210 |
ADDQ DI, DX |
| 211 |
MOVQ DX, (R8) |
| 212 |
VZEROUPPER |
| 213 |
RET |
| 214 |
|
| 215 |
// indexbytebody is the same as internal/bytealg/indexbyte_amd64.s (go1.20) |
| 216 |
// |
| 217 |
// input: |
| 218 |
// SI: data |
| 219 |
// BX: data len |
| 220 |
// AL: byte sought |
| 221 |
// R8: address to put result |
| 222 |
TEXT indexbytebody<>(SB), NOSPLIT, $0 |
| 223 |
// Shuffle X0 around so that each byte contains |
| 224 |
// the character we're looking for. |
| 225 |
MOVD AX, X0 |
| 226 |
PUNPCKLBW X0, X0 |
| 227 |
PUNPCKLBW X0, X0 |
| 228 |
PSHUFL $0, X0, X0 |
| 229 |
|
| 230 |
CMPQ BX, $16 |
| 231 |
JLT small |
| 232 |
|
| 233 |
MOVQ SI, DI |
| 234 |
|
| 235 |
CMPQ BX, $32 |
| 236 |
JA avx2 |
| 237 |
|
| 238 |
sse: |
| 239 |
LEAQ -16(SI)(BX*1), AX // AX = address of last 16 bytes |
| 240 |
JMP sseloopentry |
| 241 |
|
| 242 |
sseloop: |
| 243 |
// Move the next 16-byte chunk of the data into X1. |
| 244 |
MOVOU (DI), X1 |
| 245 |
|
| 246 |
// Compare bytes in X0 to X1. |
| 247 |
PCMPEQB X0, X1 |
| 248 |
|
| 249 |
// Take the top bit of each byte in X1 and put the result in DX. |
| 250 |
PMOVMSKB X1, DX |
| 251 |
|
| 252 |
// Find first set bit, if any. |
| 253 |
BSFL DX, DX |
| 254 |
JNZ ssesuccess |
| 255 |
|
| 256 |
// Advance to next block. |
| 257 |
ADDQ $16, DI |
| 258 |
|
| 259 |
sseloopentry: |
| 260 |
CMPQ DI, AX |
| 261 |
JB sseloop |
| 262 |
|
| 263 |
// Search the last 16-byte chunk. This chunk may overlap with the |
| 264 |
// chunks we've already searched, but that's ok. |
| 265 |
MOVQ AX, DI |
| 266 |
MOVOU (AX), X1 |
| 267 |
PCMPEQB X0, X1 |
| 268 |
PMOVMSKB X1, DX |
| 269 |
BSFL DX, DX |
| 270 |
JNZ ssesuccess |
| 271 |
|
| 272 |
failure: |
| 273 |
MOVQ $-1, (R8) |
| 274 |
RET |
| 275 |
|
| 276 |
// We've found a chunk containing the byte. |
| 277 |
// The chunk was loaded from DI. |
| 278 |
// The index of the matching byte in the chunk is DX. |
| 279 |
// The start of the data is SI. |
| 280 |
ssesuccess: |
| 281 |
SUBQ SI, DI // Compute offset of chunk within data. |
| 282 |
ADDQ DX, DI // Add offset of byte within chunk. |
| 283 |
MOVQ DI, (R8) |
| 284 |
RET |
| 285 |
|
| 286 |
// handle for lengths < 16 |
| 287 |
small: |
| 288 |
TESTQ BX, BX |
| 289 |
JEQ failure |
| 290 |
|
| 291 |
// Check if we'll load across a page boundary. |
| 292 |
LEAQ 16(SI), AX |
| 293 |
TESTW $0xff0, AX |
| 294 |
JEQ endofpage |
| 295 |
|
| 296 |
MOVOU (SI), X1 // Load data |
| 297 |
PCMPEQB X0, X1 // Compare target byte with each byte in data. |
| 298 |
PMOVMSKB X1, DX // Move result bits to integer register. |
| 299 |
BSFL DX, DX // Find first set bit. |
| 300 |
JZ failure // No set bit, failure. |
| 301 |
CMPL DX, BX |
| 302 |
JAE failure // Match is past end of data. |
| 303 |
MOVQ DX, (R8) |
| 304 |
RET |
| 305 |
|
| 306 |
endofpage: |
| 307 |
MOVOU -16(SI)(BX*1), X1 // Load data into the high end of X1. |
| 308 |
PCMPEQB X0, X1 // Compare target byte with each byte in data. |
| 309 |
PMOVMSKB X1, DX // Move result bits to integer register. |
| 310 |
MOVL BX, CX |
| 311 |
SHLL CX, DX |
| 312 |
SHRL $16, DX // Shift desired bits down to bottom of register. |
| 313 |
BSFL DX, DX // Find first set bit. |
| 314 |
JZ failure // No set bit, failure. |
| 315 |
MOVQ DX, (R8) |
| 316 |
RET |
| 317 |
|
| 318 |
avx2: |
| 319 |
#ifndef hasAVX2 |
| 320 |
CMPB golang·org∕x∕sys∕cpu·X86+const_offsetX86HasAVX2(SB), $1 |
| 321 |
JNE sse |
| 322 |
|
| 323 |
#endif |
| 324 |
MOVD AX, X0 |
| 325 |
LEAQ -32(SI)(BX*1), R11 |
| 326 |
VPBROADCASTB X0, Y1 |
| 327 |
|
| 328 |
avx2_loop: |
| 329 |
VMOVDQU (DI), Y2 |
| 330 |
VPCMPEQB Y1, Y2, Y3 |
| 331 |
VPTEST Y3, Y3 |
| 332 |
JNZ avx2success |
| 333 |
ADDQ $32, DI |
| 334 |
CMPQ DI, R11 |
| 335 |
JLT avx2_loop |
| 336 |
MOVQ R11, DI |
| 337 |
VMOVDQU (DI), Y2 |
| 338 |
VPCMPEQB Y1, Y2, Y3 |
| 339 |
VPTEST Y3, Y3 |
| 340 |
JNZ avx2success |
| 341 |
VZEROUPPER |
| 342 |
MOVQ $-1, (R8) |
| 343 |
RET |
| 344 |
|
| 345 |
avx2success: |
| 346 |
VPMOVMSKB Y3, DX |
| 347 |
BSFL DX, DX |
| 348 |
SUBQ SI, DI |
| 349 |
ADDQ DI, DX |
| 350 |
MOVQ DX, (R8) |
| 351 |
VZEROUPPER |
| 352 |
RET |