| 1 |
//go:build arm64 && !go1.22 |
| 2 |
// +build arm64,!go1.22 |
| 3 |
|
| 4 |
// Copyright 2018 The Go Authors. All rights reserved. |
| 5 |
// Use of this source code is governed by a BSD-style |
| 6 |
// license that can be found in the LICENSE file. |
| 7 |
|
| 8 |
#include "go_asm.h" |
| 9 |
#include "textflag.h" |
| 10 |
|
| 11 |
TEXT ·Count(SB), NOSPLIT, $0-40 |
| 12 |
MOVD b_base+0(FP), R0 |
| 13 |
MOVD b_len+8(FP), R2 |
| 14 |
MOVBU c+24(FP), R1 |
| 15 |
MOVD $ret+32(FP), R8 |
| 16 |
|
| 17 |
// Test if c is a letter. |
| 18 |
SUB $65, R1, R3 |
| 19 |
CMPW $25, R3 |
| 20 |
BLS index_case // c is a letter |
| 21 |
SUB $97, R1, R3 |
| 22 |
CMPW $25, R3 |
| 23 |
BHI index // c is a letter |
| 24 |
|
| 25 |
index_case: |
| 26 |
B countbytebodyCase<>(SB) |
| 27 |
|
| 28 |
index: |
| 29 |
B countbytebody<>(SB) |
| 30 |
|
| 31 |
TEXT ·CountString(SB), NOSPLIT, $0-32 |
| 32 |
MOVD s_base+0(FP), R0 |
| 33 |
MOVD s_len+8(FP), R2 |
| 34 |
MOVBU c+16(FP), R1 |
| 35 |
MOVD $ret+24(FP), R8 |
| 36 |
|
| 37 |
// Test if c is a letter. |
| 38 |
SUB $65, R1, R3 |
| 39 |
CMPW $25, R3 |
| 40 |
BLS index_case // c is a letter |
| 41 |
SUB $97, R1, R3 |
| 42 |
CMPW $25, R3 |
| 43 |
BHI index // c is not a letter |
| 44 |
|
| 45 |
index_case: |
| 46 |
B countbytebodyCase<>(SB) |
| 47 |
|
| 48 |
index: |
| 49 |
B countbytebody<>(SB) |
| 50 |
|
| 51 |
// input: |
| 52 |
// R0: data |
| 53 |
// R2: data len |
| 54 |
// R1: byte to find |
| 55 |
// R8: address to put result |
| 56 |
TEXT countbytebodyCase<>(SB), NOSPLIT, $0 |
| 57 |
// R11 = count of byte to search |
| 58 |
MOVD $0, R11 |
| 59 |
|
| 60 |
// short path to handle 0-byte case |
| 61 |
CBZ R2, done |
| 62 |
CMP $0x20, R2 |
| 63 |
|
| 64 |
// Convert byte being searched for to lowercase. |
| 65 |
ORR $32, R1, R1 |
| 66 |
|
| 67 |
// Bit mask to convert upper-case chars to lower-case. |
| 68 |
VMOVQ $0x2020202020202020, $0x2020202020202020, V9 |
| 69 |
|
| 70 |
// jump directly to tail if length < 32 |
| 71 |
BLO tail |
| 72 |
ANDS $0x1f, R0, R9 |
| 73 |
BEQ chunk |
| 74 |
|
| 75 |
// Work with not 32-byte aligned head |
| 76 |
BIC $0x1f, R0, R3 |
| 77 |
ADD $0x20, R3 |
| 78 |
|
| 79 |
head_loop: |
| 80 |
MOVBU.P 1(R0), R5 |
| 81 |
CMP R5, R1 |
| 82 |
CINC EQ, R11, R11 |
| 83 |
SUB $1, R2, R2 |
| 84 |
CMP R0, R3 |
| 85 |
BNE head_loop |
| 86 |
|
| 87 |
// Work with 32-byte aligned chunks |
| 88 |
chunk: |
| 89 |
BIC $0x1f, R2, R9 |
| 90 |
|
| 91 |
// The first chunk can also be the last |
| 92 |
CBZ R9, tail |
| 93 |
|
| 94 |
// R3 = end of 32-byte chunks |
| 95 |
ADD R0, R9, R3 |
| 96 |
MOVD $1, R5 |
| 97 |
VMOV R5, V5.B16 |
| 98 |
|
| 99 |
// R2 = length of tail |
| 100 |
SUB R9, R2, R2 |
| 101 |
|
| 102 |
// Duplicate R1 (byte to search) to 16 1-byte elements of V0 |
| 103 |
VMOV R1, V0.B16 |
| 104 |
|
| 105 |
// Clear the low 64-bit element of V7 and V8 |
| 106 |
VEOR V7.B8, V7.B8, V7.B8 |
| 107 |
VEOR V8.B8, V8.B8, V8.B8 |
| 108 |
|
| 109 |
// Count the target byte in 32-byte chunk |
| 110 |
chunk_loop: |
| 111 |
VLD1.P (R0), [V1.B16, V2.B16] |
| 112 |
VORR V9.B16, V1.B16, V1.B16 // Convert to lowercase |
| 113 |
VORR V9.B16, V2.B16, V2.B16 // Convert to lowercase |
| 114 |
CMP R0, R3 |
| 115 |
VCMEQ V0.B16, V1.B16, V3.B16 |
| 116 |
VCMEQ V0.B16, V2.B16, V4.B16 |
| 117 |
|
| 118 |
// Clear the higher 7 bits |
| 119 |
VAND V5.B16, V3.B16, V3.B16 |
| 120 |
VAND V5.B16, V4.B16, V4.B16 |
| 121 |
|
| 122 |
// Count lanes match the requested byte |
| 123 |
VADDP V4.B16, V3.B16, V6.B16 // 32B->16B |
| 124 |
VUADDLV V6.B16, V7 |
| 125 |
|
| 126 |
// Accumulate the count in low 64-bit element of V8 when inside the loop |
| 127 |
VADD V7, V8 |
| 128 |
BNE chunk_loop |
| 129 |
VMOV V8.D[0], R6 |
| 130 |
ADD R6, R11, R11 |
| 131 |
CBZ R2, done |
| 132 |
|
| 133 |
tail: |
| 134 |
// Work with tail shorter than 32 bytes |
| 135 |
MOVBU.P 1(R0), R5 |
| 136 |
ORR $32, R5, R5 // Convert to lowercase |
| 137 |
SUB $1, R2, R2 |
| 138 |
CMP R5, R1 |
| 139 |
CINC EQ, R11, R11 |
| 140 |
CBNZ R2, tail |
| 141 |
|
| 142 |
done: |
| 143 |
MOVD R11, (R8) |
| 144 |
RET |
| 145 |
|
| 146 |
// input: |
| 147 |
// R0: data |
| 148 |
// R2: data len |
| 149 |
// R1: byte to find |
| 150 |
// R8: address to put result |
| 151 |
TEXT countbytebody<>(SB), NOSPLIT, $0 |
| 152 |
// R11 = count of byte to search |
| 153 |
MOVD $0, R11 |
| 154 |
|
| 155 |
// short path to handle 0-byte case |
| 156 |
CBZ R2, done |
| 157 |
CMP $0x20, R2 |
| 158 |
|
| 159 |
// jump directly to tail if length < 32 |
| 160 |
BLO tail |
| 161 |
ANDS $0x1f, R0, R9 |
| 162 |
BEQ chunk |
| 163 |
|
| 164 |
// Work with not 32-byte aligned head |
| 165 |
BIC $0x1f, R0, R3 |
| 166 |
ADD $0x20, R3 |
| 167 |
|
| 168 |
head_loop: |
| 169 |
MOVBU.P 1(R0), R5 |
| 170 |
CMP R5, R1 |
| 171 |
CINC EQ, R11, R11 |
| 172 |
SUB $1, R2, R2 |
| 173 |
CMP R0, R3 |
| 174 |
BNE head_loop |
| 175 |
|
| 176 |
// Work with 32-byte aligned chunks |
| 177 |
chunk: |
| 178 |
BIC $0x1f, R2, R9 |
| 179 |
|
| 180 |
// The first chunk can also be the last |
| 181 |
CBZ R9, tail |
| 182 |
|
| 183 |
// R3 = end of 32-byte chunks |
| 184 |
ADD R0, R9, R3 |
| 185 |
MOVD $1, R5 |
| 186 |
VMOV R5, V5.B16 |
| 187 |
|
| 188 |
// R2 = length of tail |
| 189 |
SUB R9, R2, R2 |
| 190 |
|
| 191 |
// Duplicate R1 (byte to search) to 16 1-byte elements of V0 |
| 192 |
VMOV R1, V0.B16 |
| 193 |
|
| 194 |
// Clear the low 64-bit element of V7 and V8 |
| 195 |
VEOR V7.B8, V7.B8, V7.B8 |
| 196 |
VEOR V8.B8, V8.B8, V8.B8 |
| 197 |
|
| 198 |
// Count the target byte in 32-byte chunk |
| 199 |
chunk_loop: |
| 200 |
VLD1.P (R0), [V1.B16, V2.B16] |
| 201 |
CMP R0, R3 |
| 202 |
VCMEQ V0.B16, V1.B16, V3.B16 |
| 203 |
VCMEQ V0.B16, V2.B16, V4.B16 |
| 204 |
|
| 205 |
// Clear the higher 7 bits |
| 206 |
VAND V5.B16, V3.B16, V3.B16 |
| 207 |
VAND V5.B16, V4.B16, V4.B16 |
| 208 |
|
| 209 |
// Count lanes match the requested byte |
| 210 |
VADDP V4.B16, V3.B16, V6.B16 // 32B->16B |
| 211 |
VUADDLV V6.B16, V7 |
| 212 |
|
| 213 |
// Accumulate the count in low 64-bit element of V8 when inside the loop |
| 214 |
VADD V7, V8 |
| 215 |
BNE chunk_loop |
| 216 |
VMOV V8.D[0], R6 |
| 217 |
ADD R6, R11, R11 |
| 218 |
CBZ R2, done |
| 219 |
|
| 220 |
tail: |
| 221 |
// Work with tail shorter than 32 bytes |
| 222 |
MOVBU.P 1(R0), R5 |
| 223 |
SUB $1, R2, R2 |
| 224 |
CMP R5, R1 |
| 225 |
CINC EQ, R11, R11 |
| 226 |
CBNZ R2, tail |
| 227 |
|
| 228 |
done: |
| 229 |
MOVD R11, (R8) |
| 230 |
RET |