| 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 ·IndexNonASCII(SB), NOSPLIT, $0-24 |
| 11 |
MOVD s_base+0(FP), R0 |
| 12 |
MOVD s_len+8(FP), R2 |
| 13 |
MOVD $ret+16(FP), R8 |
| 14 |
|
| 15 |
B indexByteBodyNonASCII<>(SB) |
| 16 |
|
| 17 |
TEXT ·IndexByteNonASCII(SB), NOSPLIT, $0-32 |
| 18 |
MOVD b_base+0(FP), R0 |
| 19 |
MOVD b_len+8(FP), R2 |
| 20 |
MOVD $ret+24(FP), R8 |
| 21 |
|
| 22 |
B indexByteBodyNonASCII<>(SB) |
| 23 |
|
| 24 |
// input: |
| 25 |
// R0: data |
| 26 |
// R2: data len |
| 27 |
// R8: address to put result |
| 28 |
TEXT indexByteBodyNonASCII<>(SB), NOSPLIT, $0 |
| 29 |
CBZ R2, fail |
| 30 |
MOVD R0, R11 |
| 31 |
|
| 32 |
// Magic constant 0x40100401 allows us to identify |
| 33 |
// which lane matches the requested byte. |
| 34 |
// 0x40100401 = ((1<<0) + (4<<8) + (16<<16) + (64<<24)) |
| 35 |
// Different bytes have different bit masks (i.e: 1, 4, 16, 64) |
| 36 |
MOVD $0x40100401, R5 |
| 37 |
|
| 38 |
// Bit mask for RuneSelf (0x80) |
| 39 |
VMOVQ $0x8080808080808080, $0x8080808080808080, V0 |
| 40 |
|
| 41 |
// Work with aligned 32-byte chunks |
| 42 |
BIC $0x1f, R0, R3 |
| 43 |
VMOV R5, V5.S4 |
| 44 |
ANDS $0x1f, R0, R9 |
| 45 |
AND $0x1f, R2, R10 |
| 46 |
BEQ loop |
| 47 |
|
| 48 |
// Input string is not 32-byte aligned. We calculate the |
| 49 |
// syndrome value for the aligned 32 bytes block containing |
| 50 |
// the first bytes and mask off the irrelevant part. |
| 51 |
VLD1.P (R3), [V1.B16, V2.B16] |
| 52 |
VAND V0.B16, V1.B16, V1.B16 // Check for RuneSelf |
| 53 |
VAND V0.B16, V2.B16, V2.B16 // Check for RuneSelf |
| 54 |
SUB $0x20, R9, R4 |
| 55 |
ADDS R4, R2, R2 |
| 56 |
VCMEQ V0.B16, V1.B16, V3.B16 |
| 57 |
VCMEQ V0.B16, V2.B16, V4.B16 |
| 58 |
VAND V5.B16, V3.B16, V3.B16 |
| 59 |
VAND V5.B16, V4.B16, V4.B16 |
| 60 |
VADDP V4.B16, V3.B16, V6.B16 // 256->128 |
| 61 |
VADDP V6.B16, V6.B16, V6.B16 // 128->64 |
| 62 |
VMOV V6.D[0], R6 |
| 63 |
|
| 64 |
// Clear the irrelevant lower bits |
| 65 |
LSL $1, R9, R4 |
| 66 |
LSR R4, R6, R6 |
| 67 |
LSL R4, R6, R6 |
| 68 |
|
| 69 |
// The first block can also be the last |
| 70 |
BLS masklast |
| 71 |
|
| 72 |
// Have we found something already? |
| 73 |
CBNZ R6, tail |
| 74 |
|
| 75 |
loop: |
| 76 |
VLD1.P (R3), [V1.B16, V2.B16] |
| 77 |
SUBS $0x20, R2, R2 |
| 78 |
VAND V0.B16, V1.B16, V1.B16 // Check for RuneSelf |
| 79 |
VAND V0.B16, V2.B16, V2.B16 // Check for RuneSelf |
| 80 |
VCMEQ V0.B16, V1.B16, V3.B16 |
| 81 |
VCMEQ V0.B16, V2.B16, V4.B16 |
| 82 |
|
| 83 |
// If we're out of data we finish regardless of the result |
| 84 |
BLS end |
| 85 |
|
| 86 |
// Use a fast check for the termination condition |
| 87 |
VORR V4.B16, V3.B16, V6.B16 |
| 88 |
VADDP V6.D2, V6.D2, V6.D2 |
| 89 |
VMOV V6.D[0], R6 |
| 90 |
|
| 91 |
// We're not out of data, loop if we haven't found the character |
| 92 |
CBZ R6, loop |
| 93 |
|
| 94 |
end: |
| 95 |
// Termination condition found, let's calculate the syndrome value |
| 96 |
VAND V5.B16, V3.B16, V3.B16 |
| 97 |
VAND V5.B16, V4.B16, V4.B16 |
| 98 |
VADDP V4.B16, V3.B16, V6.B16 |
| 99 |
VADDP V6.B16, V6.B16, V6.B16 |
| 100 |
VMOV V6.D[0], R6 |
| 101 |
|
| 102 |
// Only do the clear for the last possible block with less than 32 bytes |
| 103 |
// Condition flags come from SUBS in the loop |
| 104 |
BHS tail |
| 105 |
|
| 106 |
masklast: |
| 107 |
// Clear the irrelevant upper bits |
| 108 |
ADD R9, R10, R4 |
| 109 |
AND $0x1f, R4, R4 |
| 110 |
SUB $0x20, R4, R4 |
| 111 |
NEG R4<<1, R4 |
| 112 |
LSL R4, R6, R6 |
| 113 |
LSR R4, R6, R6 |
| 114 |
|
| 115 |
tail: |
| 116 |
// Check that we have found a character |
| 117 |
CBZ R6, fail |
| 118 |
|
| 119 |
// Count the trailing zeros using bit reversing |
| 120 |
RBIT R6, R6 |
| 121 |
|
| 122 |
// Compensate the last post-increment |
| 123 |
SUB $0x20, R3, R3 |
| 124 |
|
| 125 |
// And count the leading zeros |
| 126 |
CLZ R6, R6 |
| 127 |
|
| 128 |
// R6 is twice the offset into the fragment |
| 129 |
ADD R6>>1, R3, R0 |
| 130 |
|
| 131 |
// Compute the offset result |
| 132 |
SUB R11, R0, R0 |
| 133 |
MOVD R0, (R8) |
| 134 |
RET |
| 135 |
|
| 136 |
fail: |
| 137 |
MOVD $-1, R0 |
| 138 |
MOVD R0, (R8) |
| 139 |
RET |