| 1 |
//go:build ignore |
| 2 |
// +build ignore |
| 3 |
|
| 4 |
package main |
| 5 |
|
| 6 |
import ( |
| 7 |
"bytes" |
| 8 |
|
| 9 |
. "github.com/mmcloughlin/avo/build" |
| 10 |
. "github.com/mmcloughlin/avo/operand" |
| 11 |
. "github.com/mmcloughlin/avo/reg" |
| 12 |
. "github.com/segmentio/asm/build/internal/asm" |
| 13 |
) |
| 14 |
|
| 15 |
func init() { |
| 16 |
ConstraintExpr("!purego") |
| 17 |
} |
| 18 |
|
| 19 |
func incompleteMaskData() []byte { |
| 20 |
// The incomplete mask is used on every block to flag the bytes that are |
| 21 |
// incomplete if this is the last block (for example a byte that starts |
| 22 |
// a 4 byte character only 3 bytes before the end). |
| 23 |
any := byte(0xFF) |
| 24 |
needs4 := byte(0b11110000) - 1 |
| 25 |
needs3 := byte(0b11100000) - 1 |
| 26 |
needs2 := byte(0b11000000) - 1 |
| 27 |
b := [32]byte{ |
| 28 |
any, any, any, any, any, any, any, any, |
| 29 |
any, any, any, any, any, any, any, any, |
| 30 |
any, any, any, any, any, any, any, any, |
| 31 |
any, any, any, any, any, needs4, needs3, needs2, |
| 32 |
} |
| 33 |
return b[:] |
| 34 |
} |
| 35 |
|
| 36 |
func continuationMaskData(pattern byte) []byte { |
| 37 |
// Pattern is something like 0b11100000 to accept all bytes of the form |
| 38 |
// 111xxxxx. |
| 39 |
v := pattern - 1 |
| 40 |
return bytes.Repeat([]byte{v}, 32) |
| 41 |
} |
| 42 |
|
| 43 |
func nibbleMasksData() (nib1, nib2, nib3 []byte) { |
| 44 |
const ( |
| 45 |
TooShort = 1 << 0 |
| 46 |
TooLong = 1 << 1 |
| 47 |
Overlong3 = 1 << 2 |
| 48 |
Surrogate = 1 << 4 |
| 49 |
Overlong2 = 1 << 5 |
| 50 |
TwoConts = 1 << 7 |
| 51 |
TooLarge = 1 << 3 |
| 52 |
TooLarge1000 = 1 << 6 |
| 53 |
Overlong4 = 1 << 6 |
| 54 |
Carry = TooShort | TooLong | TwoConts |
| 55 |
) |
| 56 |
|
| 57 |
fullMask := func(b [16]byte) []byte { |
| 58 |
m := make([]byte, 32) |
| 59 |
copy(m, b[:]) |
| 60 |
copy(m[16:], b[:]) |
| 61 |
return m |
| 62 |
} |
| 63 |
|
| 64 |
nib1 = fullMask([16]byte{ |
| 65 |
// 0_______ ________ <ASCII in byte 1> |
| 66 |
TooLong, TooLong, TooLong, TooLong, |
| 67 |
TooLong, TooLong, TooLong, TooLong, |
| 68 |
// 10______ ________ <continuation in byte 1> |
| 69 |
TwoConts, TwoConts, TwoConts, TwoConts, |
| 70 |
// 1100____ ________ <two byte lead in byte 1> |
| 71 |
TooShort | Overlong2, |
| 72 |
// 1101____ ________ <two byte lead in byte 1> |
| 73 |
TooShort, |
| 74 |
// 1110____ ________ <three byte lead in byte 1> |
| 75 |
TooShort | Overlong3 | Surrogate, |
| 76 |
// 1111____ ________ <four+ byte lead in byte 1> |
| 77 |
TooShort | TooLarge | TooLarge1000 | Overlong4, |
| 78 |
}) |
| 79 |
|
| 80 |
nib2 = fullMask([16]byte{ |
| 81 |
// ____0000 ________ |
| 82 |
Carry | Overlong3 | Overlong2 | Overlong4, |
| 83 |
// ____0001 ________ |
| 84 |
Carry | Overlong2, |
| 85 |
// ____001_ ________ |
| 86 |
Carry, |
| 87 |
Carry, |
| 88 |
|
| 89 |
// ____0100 ________ |
| 90 |
Carry | TooLarge, |
| 91 |
// ____0101 ________ |
| 92 |
Carry | TooLarge | TooLarge1000, |
| 93 |
// ____011_ ________ |
| 94 |
Carry | TooLarge | TooLarge1000, |
| 95 |
Carry | TooLarge | TooLarge1000, |
| 96 |
|
| 97 |
// ____1___ ________ |
| 98 |
Carry | TooLarge | TooLarge1000, |
| 99 |
Carry | TooLarge | TooLarge1000, |
| 100 |
Carry | TooLarge | TooLarge1000, |
| 101 |
Carry | TooLarge | TooLarge1000, |
| 102 |
Carry | TooLarge | TooLarge1000, |
| 103 |
// ____1101 ________ |
| 104 |
Carry | TooLarge | TooLarge1000 | Surrogate, |
| 105 |
Carry | TooLarge | TooLarge1000, |
| 106 |
Carry | TooLarge | TooLarge1000, |
| 107 |
}) |
| 108 |
|
| 109 |
nib3 = fullMask([16]byte{ |
| 110 |
// ________ 0_______ <ASCII in byte 2> |
| 111 |
TooShort, TooShort, TooShort, TooShort, |
| 112 |
TooShort, TooShort, TooShort, TooShort, |
| 113 |
|
| 114 |
// ________ 1000____ |
| 115 |
TooLong | Overlong2 | TwoConts | Overlong3 | TooLarge1000 | Overlong4, |
| 116 |
// ________ 1001____ |
| 117 |
TooLong | Overlong2 | TwoConts | Overlong3 | TooLarge, |
| 118 |
// ________ 101_____ |
| 119 |
TooLong | Overlong2 | TwoConts | Surrogate | TooLarge, |
| 120 |
TooLong | Overlong2 | TwoConts | Surrogate | TooLarge, |
| 121 |
|
| 122 |
// ________ 11______ |
| 123 |
TooShort, TooShort, TooShort, TooShort, |
| 124 |
}) |
| 125 |
|
| 126 |
return |
| 127 |
} |
| 128 |
|
| 129 |
func main() { |
| 130 |
TEXT("validateAvx", NOSPLIT, "func(p []byte) byte") |
| 131 |
Doc("Optimized version of Validate for inputs of more than 32B.") |
| 132 |
|
| 133 |
ret, err := ReturnIndex(0).Resolve() |
| 134 |
if err != nil { |
| 135 |
panic(err) |
| 136 |
} |
| 137 |
|
| 138 |
d := Load(Param("p").Base(), GP64()) |
| 139 |
n := Load(Param("p").Len(), GP64()) |
| 140 |
|
| 141 |
isAscii := GP8() |
| 142 |
MOVB(Imm(1), isAscii) |
| 143 |
|
| 144 |
Comment("Prepare the constant masks") |
| 145 |
|
| 146 |
incompleteMask := ConstBytes("incomplete_mask", incompleteMaskData()) |
| 147 |
incompleteMaskY := YMM() |
| 148 |
VMOVDQU(incompleteMask, incompleteMaskY) |
| 149 |
|
| 150 |
continuation4Bytes := ConstBytes("cont4_vec", continuationMaskData(0b11110000)) |
| 151 |
continuation4BytesY := YMM() |
| 152 |
VMOVDQU(continuation4Bytes, continuation4BytesY) |
| 153 |
|
| 154 |
continuation3Bytes := ConstBytes("cont3_vec", continuationMaskData(0b11100000)) |
| 155 |
continuation3BytesY := YMM() |
| 156 |
VMOVDQU(continuation3Bytes, continuation3BytesY) |
| 157 |
|
| 158 |
nib1Data, nib2Data, nib3Data := nibbleMasksData() |
| 159 |
|
| 160 |
Comment("High nibble of current byte") |
| 161 |
nibble1Errors := ConstBytes("nibble1_errors", nib1Data) |
| 162 |
nibble1Y := YMM() |
| 163 |
VMOVDQU(nibble1Errors, nibble1Y) |
| 164 |
|
| 165 |
Comment("Low nibble of current byte") |
| 166 |
nibble2Errors := ConstBytes("nibble2_errors", nib2Data) |
| 167 |
nibble2Y := YMM() |
| 168 |
VMOVDQU(nibble2Errors, nibble2Y) |
| 169 |
|
| 170 |
Comment("High nibble of the next byte") |
| 171 |
nibble3Errors := ConstBytes("nibble3_errors", nib3Data) |
| 172 |
nibble3Y := YMM() |
| 173 |
VMOVDQU(nibble3Errors, nibble3Y) |
| 174 |
|
| 175 |
Comment("Nibble mask") |
| 176 |
lowerNibbleMask := ConstArray64("nibble_mask", |
| 177 |
0x0F0F0F0F0F0F0F0F, |
| 178 |
0x0F0F0F0F0F0F0F0F, |
| 179 |
0x0F0F0F0F0F0F0F0F, |
| 180 |
0x0F0F0F0F0F0F0F0F, |
| 181 |
) |
| 182 |
|
| 183 |
nibbleMaskY := YMM() |
| 184 |
VMOVDQU(lowerNibbleMask, nibbleMaskY) |
| 185 |
|
| 186 |
Comment("MSB mask") |
| 187 |
msbMask := ConstArray64("msb_mask", |
| 188 |
0x8080808080808080, |
| 189 |
0x8080808080808080, |
| 190 |
0x8080808080808080, |
| 191 |
0x8080808080808080, |
| 192 |
) |
| 193 |
|
| 194 |
msbMaskY := YMM() |
| 195 |
VMOVDQU(msbMask, msbMaskY) |
| 196 |
|
| 197 |
Comment("For the first pass, set the previous block as zero.") |
| 198 |
previousBlockY := YMM() |
| 199 |
zeroOutVector(previousBlockY) |
| 200 |
|
| 201 |
Comment("Zeroes the error vector.") |
| 202 |
errorY := YMM() |
| 203 |
zeroOutVector(errorY) |
| 204 |
|
| 205 |
Comment(`Zeroes the "previous block was incomplete" vector.`) |
| 206 |
incompletePreviousBlockY := YMM() |
| 207 |
zeroOutVector(incompletePreviousBlockY) |
| 208 |
|
| 209 |
Comment("Top of the loop.") |
| 210 |
Label("check_input") |
| 211 |
|
| 212 |
currentBlockY := YMM() |
| 213 |
|
| 214 |
Comment("if bytes left >= 32") |
| 215 |
CMPQ(n, U8(32)) |
| 216 |
Comment("go process the next block") |
| 217 |
JGE(LabelRef("process")) |
| 218 |
|
| 219 |
Comment("If < 32 bytes left") |
| 220 |
|
| 221 |
Comment("Fast exit if done") |
| 222 |
CMPQ(n, U8(0)) |
| 223 |
JE(LabelRef("end")) |
| 224 |
|
| 225 |
// At this point we know we need to load up to 32 bytes of input to |
| 226 |
// finish the validation and pad the rest of the input vector with |
| 227 |
// zeroes. |
| 228 |
// |
| 229 |
// This code assumes that the remainder of the input data ends right |
| 230 |
// before a page boundary. As a result, we need to take special care to |
| 231 |
// avoid a page fault. |
| 232 |
// |
| 233 |
// At a high level: |
| 234 |
// |
| 235 |
// 1. Move back the data pointer so that the 32 bytes load ends exactly |
| 236 |
// where the input does. |
| 237 |
// |
| 238 |
// 2. Shift right the loaded input so that the remaining input starts at |
| 239 |
// the beginning of the vector. |
| 240 |
// |
| 241 |
// 3. Pad the rest of the vector with zeroes. |
| 242 |
// |
| 243 |
// Because AVX2 32 bytes vectors are really two 16 bytes vector, we need |
| 244 |
// to jump through hoops to perform the shift operation accross |
| 245 |
// lates. This code has two versions, one for inputs of less than 16 |
| 246 |
// bytes, and one for larger inputs. Though the latter as more steps, |
| 247 |
// they work using a shuffle mask to shift the bytes in the vector, and |
| 248 |
// a blend operation to stich together the various pieces of the |
| 249 |
// resulting vector. |
| 250 |
// |
| 251 |
// TODO: faster load code when not near a page boundary. |
| 252 |
|
| 253 |
Comment("If 0 < bytes left < 32") |
| 254 |
|
| 255 |
zeroes := YMM() |
| 256 |
VPXOR(zeroes, zeroes, zeroes) |
| 257 |
|
| 258 |
shuffleMaskBytes := make([]byte, 3*16) |
| 259 |
for i := byte(0); i < 16; i++ { |
| 260 |
shuffleMaskBytes[i] = i |
| 261 |
shuffleMaskBytes[i+16] = i |
| 262 |
shuffleMaskBytes[i+32] = i |
| 263 |
} |
| 264 |
shuffleMask := ConstBytes("shuffle_mask", shuffleMaskBytes) |
| 265 |
|
| 266 |
shuffleClearMaskBytes := make([]byte, 3*16) |
| 267 |
for i := byte(0); i < 16; i++ { |
| 268 |
shuffleClearMaskBytes[i] = i |
| 269 |
shuffleClearMaskBytes[i+16] = 0xFF |
| 270 |
shuffleClearMaskBytes[i+32] = 0xFF |
| 271 |
} |
| 272 |
shuffleClearMask := ConstBytes("shuffle_clear_mask", shuffleClearMaskBytes) |
| 273 |
|
| 274 |
offset := GP64() |
| 275 |
shuffleMaskPtr := GP64() |
| 276 |
shuffle := YMM() |
| 277 |
tmp1 := YMM() |
| 278 |
|
| 279 |
MOVQ(U64(32), offset) |
| 280 |
SUBQ(n, offset) |
| 281 |
|
| 282 |
SUBQ(offset, d) |
| 283 |
|
| 284 |
VMOVDQU(Mem{Base: d}, currentBlockY) |
| 285 |
|
| 286 |
CMPQ(n, U8(16)) |
| 287 |
JA(LabelRef("tail_load_large")) |
| 288 |
|
| 289 |
Comment("Shift right that works if remaining bytes <= 16, safe next to a page boundary") |
| 290 |
|
| 291 |
VPERM2I128(Imm(3), currentBlockY, zeroes, currentBlockY) |
| 292 |
|
| 293 |
LEAQ(shuffleClearMask.Offset(16), shuffleMaskPtr) |
| 294 |
ADDQ(n, offset) |
| 295 |
ADDQ(n, offset) |
| 296 |
SUBQ(Imm(32), offset) |
| 297 |
SUBQ(offset, shuffleMaskPtr) |
| 298 |
VMOVDQU(Mem{Base: shuffleMaskPtr}, shuffle) |
| 299 |
|
| 300 |
VPSHUFB(shuffle, currentBlockY, currentBlockY) |
| 301 |
|
| 302 |
XORQ(n, n) |
| 303 |
JMP(LabelRef("loaded")) |
| 304 |
|
| 305 |
Comment("Shift right that works if remaining bytes >= 16, safe next to a page boundary") |
| 306 |
Label("tail_load_large") |
| 307 |
|
| 308 |
ADDQ(n, offset) |
| 309 |
ADDQ(n, offset) |
| 310 |
SUBQ(Imm(48), offset) |
| 311 |
|
| 312 |
LEAQ(shuffleMask.Offset(16), shuffleMaskPtr) |
| 313 |
SUBQ(offset, shuffleMaskPtr) |
| 314 |
VMOVDQU(Mem{Base: shuffleMaskPtr}, shuffle) |
| 315 |
|
| 316 |
VPSHUFB(shuffle, currentBlockY, tmp1) |
| 317 |
|
| 318 |
tmp2 := YMM() |
| 319 |
VPERM2I128(Imm(3), currentBlockY, zeroes, tmp2) |
| 320 |
|
| 321 |
VPSHUFB(shuffle, tmp2, tmp2) |
| 322 |
|
| 323 |
blendMaskBytes := make([]byte, 3*16) |
| 324 |
for i := byte(0); i < 16; i++ { |
| 325 |
blendMaskBytes[i] = 0xFF |
| 326 |
blendMaskBytes[i+16] = 0x00 |
| 327 |
blendMaskBytes[i+32] = 0xFF |
| 328 |
} |
| 329 |
blendMask := ConstBytes("blend_mask", blendMaskBytes) |
| 330 |
|
| 331 |
blendMaskStartPtr := GP64() |
| 332 |
LEAQ(blendMask.Offset(16), blendMaskStartPtr) |
| 333 |
SUBQ(offset, blendMaskStartPtr) |
| 334 |
|
| 335 |
blend := YMM() |
| 336 |
VBROADCASTF128(Mem{Base: blendMaskStartPtr}, blend) |
| 337 |
VPBLENDVB(blend, tmp1, tmp2, currentBlockY) |
| 338 |
|
| 339 |
XORQ(n, n) |
| 340 |
JMP(LabelRef("loaded")) |
| 341 |
|
| 342 |
Comment("Process one 32B block of data") |
| 343 |
Label("process") |
| 344 |
|
| 345 |
Comment("Load the next block of bytes") |
| 346 |
VMOVDQU(Mem{Base: d}, currentBlockY) |
| 347 |
SUBQ(U8(32), n) |
| 348 |
ADDQ(U8(32), d) |
| 349 |
|
| 350 |
Label("loaded") |
| 351 |
|
| 352 |
Comment("Fast check to see if ASCII") |
| 353 |
tmp := GP32() |
| 354 |
VPMOVMSKB(currentBlockY, tmp) |
| 355 |
CMPL(tmp, Imm(0)) |
| 356 |
JNZ(LabelRef("non_ascii")) |
| 357 |
|
| 358 |
Comment("If this whole block is ASCII, there is nothing to do, and it is an error if any of the previous code point was incomplete.") |
| 359 |
VPOR(errorY, incompletePreviousBlockY, errorY) |
| 360 |
JMP(LabelRef("check_input")) |
| 361 |
|
| 362 |
Label("non_ascii") |
| 363 |
XORB(isAscii, isAscii) |
| 364 |
|
| 365 |
Comment("Prepare intermediate vector for push operations") |
| 366 |
vp := YMM() |
| 367 |
VPERM2I128(Imm(3), previousBlockY, currentBlockY, vp) |
| 368 |
|
| 369 |
Comment("Check errors on the high nibble of the previous byte") |
| 370 |
previousY := YMM() |
| 371 |
VPALIGNR(Imm(15), vp, currentBlockY, previousY) |
| 372 |
|
| 373 |
highPrev := highNibbles(previousY, nibbleMaskY) |
| 374 |
VPSHUFB(highPrev, nibble1Y, highPrev) |
| 375 |
|
| 376 |
Comment("Check errors on the low nibble of the previous byte") |
| 377 |
lowPrev := lowNibbles(previousY, nibbleMaskY) |
| 378 |
VPSHUFB(lowPrev, nibble2Y, lowPrev) |
| 379 |
VPAND(lowPrev, highPrev, highPrev) |
| 380 |
|
| 381 |
Comment("Check errors on the high nibble on the current byte") |
| 382 |
highCurr := highNibbles(currentBlockY, nibbleMaskY) |
| 383 |
VPSHUFB(highCurr, nibble3Y, highCurr) |
| 384 |
VPAND(highCurr, highPrev, highPrev) |
| 385 |
|
| 386 |
Comment("Find 3 bytes continuations") |
| 387 |
off2 := YMM() |
| 388 |
VPALIGNR(Imm(14), vp, currentBlockY, off2) |
| 389 |
VPSUBUSB(continuation3BytesY, off2, off2) |
| 390 |
|
| 391 |
Comment("Find 4 bytes continuations") |
| 392 |
off3 := YMM() |
| 393 |
VPALIGNR(Imm(13), vp, currentBlockY, off3) |
| 394 |
|
| 395 |
VPSUBUSB(continuation4BytesY, off3, off3) |
| 396 |
|
| 397 |
Comment("Combine them to have all continuations") |
| 398 |
continuationBitsY := YMM() |
| 399 |
VPOR(off2, off3, continuationBitsY) |
| 400 |
|
| 401 |
Comment("Perform a byte-sized signed comparison with zero to turn any non-zero bytes into 0xFF.") |
| 402 |
tmpY := zeroOutVector(YMM()) |
| 403 |
VPCMPGTB(tmpY, continuationBitsY, continuationBitsY) |
| 404 |
|
| 405 |
Comment("Find bytes that are continuations by looking at their most significant bit.") |
| 406 |
VPAND(msbMaskY, continuationBitsY, continuationBitsY) |
| 407 |
|
| 408 |
Comment("Find mismatches between expected and actual continuation bytes") |
| 409 |
VPXOR(continuationBitsY, highPrev, continuationBitsY) |
| 410 |
|
| 411 |
Comment("Store result in sticky error") |
| 412 |
VPOR(errorY, continuationBitsY, errorY) |
| 413 |
|
| 414 |
Comment("Prepare for next iteration") |
| 415 |
VPSUBUSB(incompleteMaskY, currentBlockY, incompletePreviousBlockY) |
| 416 |
VMOVDQU(currentBlockY, previousBlockY) |
| 417 |
|
| 418 |
Comment("End of loop") |
| 419 |
JMP(LabelRef("check_input")) |
| 420 |
|
| 421 |
Label("end") |
| 422 |
|
| 423 |
Comment("If the previous block was incomplete, this is an error.") |
| 424 |
VPOR(incompletePreviousBlockY, errorY, errorY) |
| 425 |
|
| 426 |
Comment("Return whether any error bit was set") |
| 427 |
VPTEST(errorY, errorY) |
| 428 |
out := GP8() |
| 429 |
SETEQ(out) |
| 430 |
|
| 431 |
Comment("Bit 0 tells if the input is valid utf8, bit 1 tells if it's valid ascii") |
| 432 |
ANDB(out, isAscii) |
| 433 |
SHLB(Imm(1), isAscii) |
| 434 |
ORB(isAscii, out) |
| 435 |
|
| 436 |
MOVB(out, ret.Addr) |
| 437 |
VZEROUPPER() |
| 438 |
RET() |
| 439 |
|
| 440 |
Generate() |
| 441 |
} |
| 442 |
|
| 443 |
func lowNibbles(a VecVirtual, nibbleMask VecVirtual) VecVirtual { |
| 444 |
out := YMM() |
| 445 |
VPAND(a, nibbleMask, out) |
| 446 |
return out |
| 447 |
} |
| 448 |
|
| 449 |
func highNibbles(a VecVirtual, nibbleMask VecVirtual) VecVirtual { |
| 450 |
out := YMM() |
| 451 |
VPSRLW(Imm(4), a, out) |
| 452 |
VPAND(out, nibbleMask, out) |
| 453 |
return out |
| 454 |
} |
| 455 |
|
| 456 |
func zeroOutVector(y VecVirtual) VecVirtual { |
| 457 |
VXORPS(y, y, y) |
| 458 |
return y |
| 459 |
} |