Debian Codegraph @

internal/decoder/string_scan_amd64.s

raw
1 #include "textflag.h"
2
3 // SPECIAL computes into Yd the lanes of the bytes of Yx which end a run of the plain bytes of a string: a quote, a
4 // backslash or a control character ( 0x1f or less, which the unsigned minimum with 0x1f leaves unchanged ). Y8, Y9
5 // and Y10 have the quote, the backslash and 0x1f in every lane; Yt is a temporary.
6 //
7 // Only VEX instructions are used: a legacy SSE instruction mixed with them costs a transition.
8 #define SPECIAL(Yx, Yt, Yd) \
9 VPCMPEQB Y8, Yx, Yd \
10 VPCMPEQB Y9, Yx, Yt \
11 VPOR Yt, Yd, Yd \
12 VPMINUB Y10, Yx, Yt \
13 VPCMPEQB Yx, Yt, Yt \
14 VPOR Yt, Yd, Yd
15
16 // func indexStringSpecialAVX2(p unsafe.Pointer, n int) (index int, high uint64)
17 //
18 // It returns the index of the first byte of the n bytes at p which is a quote, a backslash or a control
19 // character, or n if there is none, and high, which is not zero if a byte before it is 0x80 or more. n is 32
20 // or more. The blocks of 32 bytes are looked at in order, and the last block overlaps the previous one.
21 TEXT ·indexStringSpecialAVX2(SB), NOSPLIT, $0-32
22 MOVQ p+0(FP), SI
23 MOVQ n+8(FP), CX
24
25 VPBROADCASTB cQuoteS<>(SB), Y8
26 VPBROADCASTB cBackslashS<>(SB), Y9
27 VPBROADCASTB c1f<>(SB), Y10
28
29 XORQ R8, R8 // the top bits of the bytes before the index
30 MOVQ SI, DI // the address of the block
31 LEAQ -32(SI)(CX*1), R11 // the address of the last block
32 loop:
33 CMPQ DI, R11
34 JGT last
35 VMOVDQU (DI), Y0
36 SPECIAL(Y0, Y1, Y2)
37 VPMOVMSKB Y2, DX
38 VPMOVMSKB Y0, BX
39 TESTL DX, DX
40 JNE found
41 ORL BX, R8
42 ADDQ $32, DI
43 JMP loop
44 last:
45 // the last block, which overlaps the previous one unless the blocks ended exactly at n.
46 LEAQ (SI)(CX*1), AX
47 CMPQ DI, AX
48 JEQ none
49 MOVQ R11, DI
50 VMOVDQU (DI), Y0
51 SPECIAL(Y0, Y1, Y2)
52 VPMOVMSKB Y2, DX
53 VPMOVMSKB Y0, BX
54 TESTL DX, DX
55 JNE found
56 ORL BX, R8
57 none:
58 VZEROUPPER
59 MOVQ CX, index+16(FP)
60 MOVQ R8, high+24(FP)
61 RET
62 found:
63 // the top bits of the bytes of the block before the special one: below the lowest bit of DX.
64 MOVL DX, AX
65 NEGL AX
66 ANDL DX, AX
67 DECL AX
68 ANDL AX, BX
69 ORL BX, R8
70 BSFL DX, DX
71 SUBQ SI, DI
72 ADDQ DX, DI
73 VZEROUPPER
74 MOVQ DI, index+16(FP)
75 MOVQ R8, high+24(FP)
76 RET
77
78 DATA cQuoteS<>+0(SB)/1, $0x22
79 GLOBL cQuoteS<>(SB), RODATA|NOPTR, $1
80 DATA cBackslashS<>+0(SB)/1, $0x5c
81 GLOBL cBackslashS<>(SB), RODATA|NOPTR, $1
82 DATA c1f<>+0(SB)/1, $0x1f
83 GLOBL c1f<>(SB), RODATA|NOPTR, $1