Debian Codegraph @

utf8/valid_amd64.s

raw
1 // Code generated by command: go run valid_asm.go -pkg utf8 -out ../utf8/valid_amd64.s -stubs ../utf8/valid_amd64.go. DO NOT EDIT.
2
3 //go:build !purego
4
5 #include "textflag.h"
6
7 // func validateAvx(p []byte) byte
8 // Requires: AVX, AVX2
9 TEXT ·validateAvx(SB), NOSPLIT, $0-25
10 MOVQ p_base+0(FP), AX
11 MOVQ p_len+8(FP), CX
12 MOVB $0x01, DL
13
14 // Prepare the constant masks
15 VMOVDQU incomplete_mask<>+0(SB), Y0
16 VMOVDQU cont4_vec<>+0(SB), Y1
17 VMOVDQU cont3_vec<>+0(SB), Y2
18
19 // High nibble of current byte
20 VMOVDQU nibble1_errors<>+0(SB), Y3
21
22 // Low nibble of current byte
23 VMOVDQU nibble2_errors<>+0(SB), Y4
24
25 // High nibble of the next byte
26 VMOVDQU nibble3_errors<>+0(SB), Y5
27
28 // Nibble mask
29 VMOVDQU nibble_mask<>+0(SB), Y6
30
31 // MSB mask
32 VMOVDQU msb_mask<>+0(SB), Y7
33
34 // For the first pass, set the previous block as zero.
35 VXORPS Y8, Y8, Y8
36
37 // Zeroes the error vector.
38 VXORPS Y9, Y9, Y9
39
40 // Zeroes the "previous block was incomplete" vector.
41 VXORPS Y10, Y10, Y10
42
43 // Top of the loop.
44 check_input:
45 // if bytes left >= 32
46 CMPQ CX, $0x20
47
48 // go process the next block
49 JGE process
50
51 // If < 32 bytes left
52 // Fast exit if done
53 CMPQ CX, $0x00
54 JE end
55
56 // If 0 < bytes left < 32
57 VPXOR Y12, Y12, Y12
58 MOVQ $0x0000000000000020, BX
59 SUBQ CX, BX
60 SUBQ BX, AX
61 VMOVDQU (AX), Y11
62 CMPQ CX, $0x10
63 JA tail_load_large
64
65 // Shift right that works if remaining bytes <= 16, safe next to a page boundary
66 VPERM2I128 $0x03, Y11, Y12, Y11
67 LEAQ shuffle_clear_mask<>+16(SB), SI
68 ADDQ CX, BX
69 ADDQ CX, BX
70 SUBQ $0x20, BX
71 SUBQ BX, SI
72 VMOVDQU (SI), Y13
73 VPSHUFB Y13, Y11, Y11
74 XORQ CX, CX
75 JMP loaded
76
77 // Shift right that works if remaining bytes >= 16, safe next to a page boundary
78 tail_load_large:
79 ADDQ CX, BX
80 ADDQ CX, BX
81 SUBQ $0x30, BX
82 LEAQ shuffle_mask<>+16(SB), SI
83 SUBQ BX, SI
84 VMOVDQU (SI), Y13
85 VPSHUFB Y13, Y11, Y14
86 VPERM2I128 $0x03, Y11, Y12, Y11
87 VPSHUFB Y13, Y11, Y11
88 LEAQ blend_mask<>+16(SB), CX
89 SUBQ BX, CX
90 VBROADCASTF128 (CX), Y12
91 VPBLENDVB Y12, Y14, Y11, Y11
92 XORQ CX, CX
93 JMP loaded
94
95 // Process one 32B block of data
96 process:
97 // Load the next block of bytes
98 VMOVDQU (AX), Y11
99 SUBQ $0x20, CX
100 ADDQ $0x20, AX
101
102 loaded:
103 // Fast check to see if ASCII
104 VPMOVMSKB Y11, BX
105 CMPL BX, $0x00
106 JNZ non_ascii
107
108 // 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.
109 VPOR Y9, Y10, Y9
110 JMP check_input
111
112 non_ascii:
113 XORB DL, DL
114
115 // Prepare intermediate vector for push operations
116 VPERM2I128 $0x03, Y8, Y11, Y8
117
118 // Check errors on the high nibble of the previous byte
119 VPALIGNR $0x0f, Y8, Y11, Y10
120 VPSRLW $0x04, Y10, Y12
121 VPAND Y12, Y6, Y12
122 VPSHUFB Y12, Y3, Y12
123
124 // Check errors on the low nibble of the previous byte
125 VPAND Y10, Y6, Y10
126 VPSHUFB Y10, Y4, Y10
127 VPAND Y10, Y12, Y12
128
129 // Check errors on the high nibble on the current byte
130 VPSRLW $0x04, Y11, Y10
131 VPAND Y10, Y6, Y10
132 VPSHUFB Y10, Y5, Y10
133 VPAND Y10, Y12, Y12
134
135 // Find 3 bytes continuations
136 VPALIGNR $0x0e, Y8, Y11, Y10
137 VPSUBUSB Y2, Y10, Y10
138
139 // Find 4 bytes continuations
140 VPALIGNR $0x0d, Y8, Y11, Y8
141 VPSUBUSB Y1, Y8, Y8
142
143 // Combine them to have all continuations
144 VPOR Y10, Y8, Y8
145
146 // Perform a byte-sized signed comparison with zero to turn any non-zero bytes into 0xFF.
147 VXORPS Y10, Y10, Y10
148 VPCMPGTB Y10, Y8, Y8
149
150 // Find bytes that are continuations by looking at their most significant bit.
151 VPAND Y7, Y8, Y8
152
153 // Find mismatches between expected and actual continuation bytes
154 VPXOR Y8, Y12, Y8
155
156 // Store result in sticky error
157 VPOR Y9, Y8, Y9
158
159 // Prepare for next iteration
160 VPSUBUSB Y0, Y11, Y10
161 VMOVDQU Y11, Y8
162
163 // End of loop
164 JMP check_input
165
166 end:
167 // If the previous block was incomplete, this is an error.
168 VPOR Y10, Y9, Y9
169
170 // Return whether any error bit was set
171 VPTEST Y9, Y9
172 SETEQ AL
173
174 // Bit 0 tells if the input is valid utf8, bit 1 tells if it's valid ascii
175 ANDB AL, DL
176 SHLB $0x01, DL
177 ORB DL, AL
178 MOVB AL, ret+24(FP)
179 VZEROUPPER
180 RET
181
182 DATA incomplete_mask<>+0(SB)/8, $0xffffffffffffffff
183 DATA incomplete_mask<>+8(SB)/8, $0xffffffffffffffff
184 DATA incomplete_mask<>+16(SB)/8, $0xffffffffffffffff
185 DATA incomplete_mask<>+24(SB)/8, $0xbfdfefffffffffff
186 GLOBL incomplete_mask<>(SB), RODATA|NOPTR, $32
187
188 DATA cont4_vec<>+0(SB)/8, $0xefefefefefefefef
189 DATA cont4_vec<>+8(SB)/8, $0xefefefefefefefef
190 DATA cont4_vec<>+16(SB)/8, $0xefefefefefefefef
191 DATA cont4_vec<>+24(SB)/8, $0xefefefefefefefef
192 GLOBL cont4_vec<>(SB), RODATA|NOPTR, $32
193
194 DATA cont3_vec<>+0(SB)/8, $0xdfdfdfdfdfdfdfdf
195 DATA cont3_vec<>+8(SB)/8, $0xdfdfdfdfdfdfdfdf
196 DATA cont3_vec<>+16(SB)/8, $0xdfdfdfdfdfdfdfdf
197 DATA cont3_vec<>+24(SB)/8, $0xdfdfdfdfdfdfdfdf
198 GLOBL cont3_vec<>(SB), RODATA|NOPTR, $32
199
200 DATA nibble1_errors<>+0(SB)/8, $0x0202020202020202
201 DATA nibble1_errors<>+8(SB)/8, $0x4915012180808080
202 DATA nibble1_errors<>+16(SB)/8, $0x0202020202020202
203 DATA nibble1_errors<>+24(SB)/8, $0x4915012180808080
204 GLOBL nibble1_errors<>(SB), RODATA|NOPTR, $32
205
206 DATA nibble2_errors<>+0(SB)/8, $0xcbcbcb8b8383a3e7
207 DATA nibble2_errors<>+8(SB)/8, $0xcbcbdbcbcbcbcbcb
208 DATA nibble2_errors<>+16(SB)/8, $0xcbcbcb8b8383a3e7
209 DATA nibble2_errors<>+24(SB)/8, $0xcbcbdbcbcbcbcbcb
210 GLOBL nibble2_errors<>(SB), RODATA|NOPTR, $32
211
212 DATA nibble3_errors<>+0(SB)/8, $0x0101010101010101
213 DATA nibble3_errors<>+8(SB)/8, $0x01010101babaaee6
214 DATA nibble3_errors<>+16(SB)/8, $0x0101010101010101
215 DATA nibble3_errors<>+24(SB)/8, $0x01010101babaaee6
216 GLOBL nibble3_errors<>(SB), RODATA|NOPTR, $32
217
218 DATA nibble_mask<>+0(SB)/8, $0x0f0f0f0f0f0f0f0f
219 DATA nibble_mask<>+8(SB)/8, $0x0f0f0f0f0f0f0f0f
220 DATA nibble_mask<>+16(SB)/8, $0x0f0f0f0f0f0f0f0f
221 DATA nibble_mask<>+24(SB)/8, $0x0f0f0f0f0f0f0f0f
222 GLOBL nibble_mask<>(SB), RODATA|NOPTR, $32
223
224 DATA msb_mask<>+0(SB)/8, $0x8080808080808080
225 DATA msb_mask<>+8(SB)/8, $0x8080808080808080
226 DATA msb_mask<>+16(SB)/8, $0x8080808080808080
227 DATA msb_mask<>+24(SB)/8, $0x8080808080808080
228 GLOBL msb_mask<>(SB), RODATA|NOPTR, $32
229
230 DATA shuffle_mask<>+0(SB)/8, $0x0706050403020100
231 DATA shuffle_mask<>+8(SB)/8, $0x0f0e0d0c0b0a0908
232 DATA shuffle_mask<>+16(SB)/8, $0x0706050403020100
233 DATA shuffle_mask<>+24(SB)/8, $0x0f0e0d0c0b0a0908
234 DATA shuffle_mask<>+32(SB)/8, $0x0706050403020100
235 DATA shuffle_mask<>+40(SB)/8, $0x0f0e0d0c0b0a0908
236 GLOBL shuffle_mask<>(SB), RODATA|NOPTR, $48
237
238 DATA shuffle_clear_mask<>+0(SB)/8, $0x0706050403020100
239 DATA shuffle_clear_mask<>+8(SB)/8, $0x0f0e0d0c0b0a0908
240 DATA shuffle_clear_mask<>+16(SB)/8, $0xffffffffffffffff
241 DATA shuffle_clear_mask<>+24(SB)/8, $0xffffffffffffffff
242 DATA shuffle_clear_mask<>+32(SB)/8, $0xffffffffffffffff
243 DATA shuffle_clear_mask<>+40(SB)/8, $0xffffffffffffffff
244 GLOBL shuffle_clear_mask<>(SB), RODATA|NOPTR, $48
245
246 DATA blend_mask<>+0(SB)/8, $0xffffffffffffffff
247 DATA blend_mask<>+8(SB)/8, $0xffffffffffffffff
248 DATA blend_mask<>+16(SB)/8, $0x0000000000000000
249 DATA blend_mask<>+24(SB)/8, $0x0000000000000000
250 DATA blend_mask<>+32(SB)/8, $0xffffffffffffffff
251 DATA blend_mask<>+40(SB)/8, $0xffffffffffffffff
252 GLOBL blend_mask<>(SB), RODATA|NOPTR, $48