Debian Codegraph @

internal/encoder/string_scan_amd64.s

raw
1 #include "textflag.h"
2
3 // MASK computes into Yd the mask of the bytes of Yx which may need an escape, using Yt as a temporary:
4 // the lane of such a byte is not zero. See nibbleTables: Y8 is the table of the low nibbles in both halves,
5 // Y9 the one of the high nibbles and Y10 has 0x0f in every lane.
6 //
7 // Only VEX instructions are used: a legacy SSE instruction, such as a move from a general register to an XMM
8 // register, mixed with them cost about 500 ns per call.
9 #define MASK(Yx, Yt, Yd) \
10 VPAND Y10, Yx, Yt \
11 VPSRLW $4, Yx, Yd \
12 VPAND Y10, Yd, Yd \
13 VPSHUFB Yt, Y8, Yt \
14 VPSHUFB Yd, Y9, Yd \
15 VPAND Yt, Yd, Yd
16
17 // func scanStringAVX2(p unsafe.Pointer, n int, tables *nibbleTables) int
18 //
19 // It returns 1 if a byte of the n bytes at p may need an escape by the tables, or 0. n is 32 or more.
20 // The blocks of 128 bytes are looked at first, then the blocks of 32 bytes, and the last block overlaps the
21 // previous one.
22 TEXT ·scanStringAVX2(SB), NOSPLIT, $0-32
23 MOVQ p+0(FP), SI
24 MOVQ n+8(FP), CX
25 MOVQ tables+16(FP), AX
26
27 VBROADCASTI128 (AX), Y8
28 VBROADCASTI128 16(AX), Y9
29 VPBROADCASTB c0f<>(SB), Y10
30
31 MOVQ SI, DI // the address of the block
32 LEAQ -128(SI)(CX*1), R11 // the address of the last block of 128 bytes
33 CMPQ DI, R11
34 JGT small
35 loop128:
36 VMOVDQU (DI), Y0
37 VMOVDQU 32(DI), Y1
38 VMOVDQU 64(DI), Y2
39 VMOVDQU 96(DI), Y3
40 MASK(Y0, Y4, Y5)
41 MASK(Y1, Y4, Y6)
42 VPOR Y6, Y5, Y5
43 MASK(Y2, Y4, Y6)
44 VPOR Y6, Y5, Y5
45 MASK(Y3, Y4, Y6)
46 VPOR Y6, Y5, Y5
47 VPTEST Y5, Y5
48 JNE found
49 ADDQ $128, DI
50 CMPQ DI, R11
51 JLE loop128
52 small:
53 LEAQ -32(SI)(CX*1), R11 // the address of the last block of 32 bytes
54 CMPQ DI, R11
55 JGT last
56 loop32:
57 VMOVDQU (DI), Y0
58 MASK(Y0, Y4, Y5)
59 VPTEST Y5, Y5
60 JNE found
61 ADDQ $32, DI
62 CMPQ DI, R11
63 JLE loop32
64 last:
65 // the last block, which overlaps the previous one unless the blocks ended exactly at n.
66 LEAQ (SI)(CX*1), DX
67 CMPQ DI, DX
68 JEQ none
69 VMOVDQU (R11), Y0
70 MASK(Y0, Y4, Y5)
71 VPTEST Y5, Y5
72 JNE found
73 none:
74 VZEROUPPER
75 MOVQ $0, ret+24(FP)
76 RET
77 found:
78 VZEROUPPER
79 MOVQ $1, ret+24(FP)
80 RET
81
82 // func escapeStringAVX2(dst, src unsafe.Pointer, n int, tables *nibbleTables, seqs *[256]uint64) (consumed, written int)
83 //
84 // It appends the n bytes at src to dst, escaped, 32 bytes at a time: a block is stored to dst as it is, and the
85 // bytes of it which may need an escape by the tables are found by one mask. For each of them in order, dst is
86 // advanced to it, its escape, the sequence of seqs of the byte ( see escapeSequences ), is stored, and the rest of
87 // the block is stored again after it from src, 32 bytes, which is why that is done only while 32 bytes of src
88 // remain from there. It stops at a byte whose sequence is 0, which the caller escapes, or when fewer than 32 bytes
89 // remain, and returns the numbers of the bytes it read and wrote. dst has room for 6*n+32 bytes: a byte is
90 // escaped in 6 bytes at most, and 32 bytes are stored at a time.
91 TEXT ·escapeStringAVX2(SB), NOSPLIT, $0-56
92 MOVQ dst+0(FP), DI
93 MOVQ src+8(FP), SI
94 MOVQ n+16(FP), CX
95 MOVQ tables+24(FP), AX
96 MOVQ seqs+32(FP), R9
97
98 VBROADCASTI128 (AX), Y8
99 VBROADCASTI128 16(AX), Y9
100 VPBROADCASTB c0f<>(SB), Y10
101 VPXOR Y11, Y11, Y11
102
103 MOVQ SI, R12 // the start of src
104 MOVQ DI, R13 // the start of dst
105 LEAQ (SI)(CX*1), R11 // the end of src
106 eloop:
107 LEAQ 32(SI), R10 // the end of the block
108 CMPQ R10, R11
109 JGT edone
110 VMOVDQU (SI), Y0
111 VMOVDQU Y0, (DI)
112 MASK(Y0, Y4, Y5)
113 // the lanes which are zero are the ones of the bytes which need no escape.
114 VPCMPEQB Y11, Y5, Y5
115 VPMOVMSKB Y5, DX
116 NOTL DX
117 TESTL DX, DX
118 JNE eescape
119 MOVQ R10, SI
120 ADDQ $32, DI
121 JMP eloop
122 eescape:
123 MOVQ SI, R14 // the address of the bit 0 of the mask
124 ebyte:
125 // dst is advanced over the bytes before the one to escape, which are stored already.
126 BSFL DX, CX
127 LEAQ (R14)(CX*1), AX
128 MOVQ AX, BX
129 SUBQ SI, BX
130 ADDQ BX, DI
131 MOVQ AX, SI
132 MOVBLZX (SI), AX
133 MOVQ (R9)(AX*8), BX
134 TESTQ BX, BX
135 JEQ edone
136 // the sequence, whose length is in the top byte: the bytes after it are written over.
137 MOVQ BX, (DI)
138 SHRQ $56, BX
139 ADDQ BX, DI
140 INCQ SI
141 // the rest of the block is stored again after the escape, from src, if 32 bytes of src remain.
142 LEAQ 32(SI), AX
143 CMPQ AX, R11
144 JGT edone
145 VMOVDQU (SI), Y1
146 VMOVDQU Y1, (DI)
147 LEAL -1(DX), AX
148 ANDL AX, DX
149 JNE ebyte
150 // the rest of the block needs no escape.
151 MOVQ R10, BX
152 SUBQ SI, BX
153 ADDQ BX, DI
154 MOVQ R10, SI
155 JMP eloop
156 edone:
157 VZEROUPPER
158 SUBQ R12, SI
159 MOVQ SI, consumed+40(FP)
160 SUBQ R13, DI
161 MOVQ DI, written+48(FP)
162 RET
163
164 // func escapeUTF8AVX2(dst, src unsafe.Pointer, n int, tables *nibbleTables, seqs *[256]uint64, utf8 *utf8Tables) (consumed, written int)
165 //
166 // It is escapeStringAVX2 for the options which normalize UTF-8: the tables are the ones of the bytes of ASCII to
167 // escape, and a block with a byte which is not ASCII is validated as UTF-8 by the tables of utf8 ( see utf8Tables ).
168 //
169 // The blocks follow each other by 32 bytes, and the bytes before each byte of a block are taken from the previous
170 // block ( Y11 ), so that a character which goes on after a block is validated with the next block. A block of
171 // ASCII is taken as zeros, which are validated as it is: the loop of the blocks after a block of ASCII ( uloop )
172 // neither keeps the previous block nor looks at R8, which is 0 there, as the loop of escapeStringAVX2 does, and the
173 // one after a block with a byte which is not ASCII ( unext ) keeps it and has R8 = 1. Then the number of the bytes
174 // of the character which that block leaves to the next one, 0 to 3, is counted from its last bytes where it is
175 // needed, which is rarely:
176 // - a block with invalid UTF-8, or with U+2028 or U+2029, which are escaped, stops the loop at the start of that
177 // character, or else at its first byte which is not ASCII, the start of a character, which the caller escapes;
178 // - a block of ASCII after such a character stops the loop at the start of the character, which is invalid;
179 // - the loop ends before such a character in its last block.
180 // So the loop always stops at the start of a character, and the bytes after it which are in dst are written over
181 // by the caller.
182 TEXT ·escapeUTF8AVX2(SB), NOSPLIT, $0-64
183 MOVQ dst+0(FP), DI
184 MOVQ src+8(FP), SI
185 MOVQ n+16(FP), CX
186 MOVQ tables+24(FP), AX
187 MOVQ seqs+32(FP), R9
188 MOVQ utf8+40(FP), R8
189
190 VBROADCASTI128 (AX), Y8
191 VBROADCASTI128 16(AX), Y9
192 VPBROADCASTB c0f<>(SB), Y10
193 VBROADCASTI128 (R8), Y12 // the errors by the high nibble of the first byte
194 VBROADCASTI128 16(R8), Y13 // by its low nibble
195 VBROADCASTI128 32(R8), Y14 // by the high nibble of the second byte
196 VPXOR Y15, Y15, Y15
197 XORL R8, R8
198
199 MOVQ SI, R12 // the start of src
200 MOVQ DI, R13 // the start of dst
201 LEAQ (SI)(CX*1), R11 // the end of src
202 uloop:
203 LEAQ 32(SI), R10 // the end of the block
204 CMPQ R10, R11
205 JGT uend
206 VMOVDQU (SI), Y0
207 VMOVDQU Y0, (DI)
208 MASK(Y0, Y4, Y5)
209 VPCMPEQB Y15, Y5, Y5
210 VPMOVMSKB Y5, DX
211 NOTL DX // the bytes of ASCII to escape
212 VPMOVMSKB Y0, BX // the bytes which are not ASCII
213 TESTL BX, BX
214 JNE uutf8first
215 ublock:
216 TESTL DX, DX
217 JNE uescape
218 ADDQ $32, SI
219 ADDQ $32, DI
220 JMP uloop
221 uutf8first:
222 // the previous block, if there is one, is of ASCII, which is validated as zeros are.
223 VPXOR Y11, Y11, Y11
224 JMP uutf8
225 uascii:
226 // a block of ASCII after a block with a byte which is not ASCII: a character which that block leaves to this
227 // one is invalid.
228 XORL R8, R8
229 MOVBLZX -1(SI), AX
230 CMPL AX, $0xc0
231 JAE uasciileft1
232 MOVBLZX -2(SI), AX
233 CMPL AX, $0xe0
234 JAE uasciileft2
235 MOVBLZX -3(SI), AX
236 CMPL AX, $0xf0
237 JB uasciiok
238 INCQ R8
239 uasciileft2:
240 INCQ R8
241 uasciileft1:
242 INCQ R8
243 JMP uincomplete
244 uasciiok:
245 JMP ublock
246 uutf8:
247 // the bytes one, two and three before every byte, with the previous block before the block.
248 VPERM2I128 $0x03, Y11, Y0, Y1
249 VPALIGNR $15, Y1, Y0, Y2
250 VPALIGNR $14, Y1, Y0, Y3
251 VPALIGNR $13, Y1, Y0, Y6
252 // the errors of every two bytes in a row.
253 VPSRLW $4, Y2, Y7
254 VPAND Y10, Y7, Y7
255 VPSHUFB Y7, Y12, Y7
256 VPAND Y10, Y2, Y4
257 VPSHUFB Y4, Y13, Y4
258 VPAND Y4, Y7, Y7
259 VPSRLW $4, Y0, Y4
260 VPAND Y10, Y4, Y4
261 VPSHUFB Y4, Y14, Y4
262 VPAND Y4, Y7, Y7
263 // the third and the fourth bytes of a character must be continuation bytes, which the lookups take as errors
264 // of two continuation bytes in a row: the bit of that error is flipped there.
265 // The constants are operands in memory, of 32 bytes: a broadcast would take the port of the shuffles, which
266 // the lookups above need.
267 VPSUBUSB c60x32<>(SB), Y3, Y4 // 0x80 or more if the byte two before is 111_____
268 VPSUBUSB c70x32<>(SB), Y6, Y6 // 0x80 or more if the byte three before is 1111____
269 VPOR Y6, Y4, Y4
270 VPAND c80x32<>(SB), Y4, Y4
271 VPXOR Y4, Y7, Y7
272 // U+2028 and U+2029: E2 80 A8 and E2 80 A9, looked for only if a byte two before a byte is E2, as few are.
273 VPCMPEQB cE2x32<>(SB), Y3, Y4
274 VPMOVMSKB Y4, AX
275 TESTL AX, AX
276 JEQ uvalid
277 VPCMPEQB c80x32<>(SB), Y2, Y6
278 VPAND Y6, Y4, Y4
279 VPAND cFEx32<>(SB), Y0, Y6
280 VPCMPEQB cA8x32<>(SB), Y6, Y6
281 VPAND Y6, Y4, Y4
282 VPOR Y4, Y7, Y7
283 uvalid:
284 VPTEST Y7, Y7
285 JNE uproblem
286 MOVL $1, R8
287 TESTL DX, DX
288 JNE uescape8
289 // the next block, which is likely to have bytes which are not ASCII too: the loop goes on from here, with one
290 // jump for a block.
291 ADDQ $32, SI
292 ADDQ $32, DI
293 unext:
294 LEAQ 32(SI), R10
295 CMPQ R10, R11
296 JGT uend
297 VMOVDQA Y0, Y11
298 VMOVDQU (SI), Y0
299 VMOVDQU Y0, (DI)
300 MASK(Y0, Y4, Y5)
301 VPCMPEQB Y15, Y5, Y5
302 VPMOVMSKB Y5, DX
303 NOTL DX
304 VPMOVMSKB Y0, BX
305 TESTL BX, BX
306 JNE uutf8
307 JMP uascii
308 uproblem:
309 // the loop stops at the start of the character which the previous block leaves to this one, if there is one,
310 // or else at the first byte which is not ASCII, as at a byte to escape whose sequence is 0.
311 TESTQ R8, R8
312 JEQ ufirst
313 XORL R8, R8
314 MOVBLZX -1(SI), AX
315 CMPL AX, $0xc0
316 JAE uproblemleft1
317 MOVBLZX -2(SI), AX
318 CMPL AX, $0xe0
319 JAE uproblemleft2
320 MOVBLZX -3(SI), AX
321 CMPL AX, $0xf0
322 JB ufirst
323 INCQ R8
324 uproblemleft2:
325 INCQ R8
326 uproblemleft1:
327 INCQ R8
328 JMP uincomplete
329 ufirst:
330 BSFL BX, CX
331 MOVL $1, AX
332 SHLL CX, AX
333 ORL AX, DX
334 JMP ublock
335 uescape:
336 MOVQ SI, R14 // the address of the bit 0 of the mask
337 ubyte:
338 // dst is advanced over the bytes before the one to escape, which are stored already.
339 BSFL DX, CX
340 LEAQ (R14)(CX*1), AX
341 MOVQ AX, BX
342 SUBQ SI, BX
343 ADDQ BX, DI
344 MOVQ AX, SI
345 MOVBLZX (SI), AX
346 MOVQ (R9)(AX*8), BX
347 TESTQ BX, BX
348 JEQ udone
349 // the sequence, whose length is in the top byte: the bytes after it are written over.
350 MOVQ BX, (DI)
351 SHRQ $56, BX
352 ADDQ BX, DI
353 INCQ SI
354 // the rest of the block is stored again after the escape, from src, if 32 bytes of src remain.
355 LEAQ 32(SI), AX
356 CMPQ AX, R11
357 JGT udone
358 VMOVDQU (SI), Y1
359 VMOVDQU Y1, (DI)
360 LEAL -1(DX), AX
361 ANDL AX, DX
362 JNE ubyte
363 // the rest of the block needs no escape.
364 MOVQ R10, BX
365 SUBQ SI, BX
366 ADDQ BX, DI
367 MOVQ R10, SI
368 JMP uloop
369 uescape8:
370 // the escapes of a block of the loop after a block with a byte which is not ASCII, which goes on in that loop.
371 MOVQ SI, R14 // the address of the bit 0 of the mask
372 ubyte8:
373 // dst is advanced over the bytes before the one to escape, which are stored already.
374 BSFL DX, CX
375 LEAQ (R14)(CX*1), AX
376 MOVQ AX, BX
377 SUBQ SI, BX
378 ADDQ BX, DI
379 MOVQ AX, SI
380 MOVBLZX (SI), AX
381 MOVQ (R9)(AX*8), BX
382 TESTQ BX, BX
383 JEQ udone
384 // the sequence, whose length is in the top byte: the bytes after it are written over.
385 MOVQ BX, (DI)
386 SHRQ $56, BX
387 ADDQ BX, DI
388 INCQ SI
389 // the rest of the block is stored again after the escape, from src, if 32 bytes of src remain.
390 LEAQ 32(SI), AX
391 CMPQ AX, R11
392 JGT udone
393 VMOVDQU (SI), Y1
394 VMOVDQU Y1, (DI)
395 LEAL -1(DX), AX
396 ANDL AX, DX
397 JNE ubyte8
398 // the rest of the block needs no escape.
399 MOVQ R10, BX
400 SUBQ SI, BX
401 ADDQ BX, DI
402 MOVQ R10, SI
403 JMP unext
404 uend:
405 // the loop ends before the character which the last block leaves to the next one, which the caller validates.
406 TESTQ R8, R8
407 JEQ udone
408 XORL R8, R8
409 MOVBLZX -1(SI), AX
410 CMPL AX, $0xc0
411 JAE uendleft1
412 MOVBLZX -2(SI), AX
413 CMPL AX, $0xe0
414 JAE uendleft2
415 MOVBLZX -3(SI), AX
416 CMPL AX, $0xf0
417 JB udone
418 INCQ R8
419 uendleft2:
420 INCQ R8
421 uendleft1:
422 INCQ R8
423 uincomplete:
424 SUBQ R8, SI
425 SUBQ R8, DI
426 udone:
427 VZEROUPPER
428 SUBQ R12, SI
429 MOVQ SI, consumed+48(FP)
430 SUBQ R13, DI
431 MOVQ DI, written+56(FP)
432 RET
433
434 DATA c0f<>+0(SB)/1, $0x0f
435 GLOBL c0f<>(SB), RODATA|NOPTR, $1
436
437 // the constants of the check of UTF-8, repeated in the 32 bytes of a register.
438 DATA c60x32<>+0(SB)/8, $0x6060606060606060
439 DATA c60x32<>+8(SB)/8, $0x6060606060606060
440 DATA c60x32<>+16(SB)/8, $0x6060606060606060
441 DATA c60x32<>+24(SB)/8, $0x6060606060606060
442 GLOBL c60x32<>(SB), RODATA|NOPTR, $32
443 DATA c70x32<>+0(SB)/8, $0x7070707070707070
444 DATA c70x32<>+8(SB)/8, $0x7070707070707070
445 DATA c70x32<>+16(SB)/8, $0x7070707070707070
446 DATA c70x32<>+24(SB)/8, $0x7070707070707070
447 GLOBL c70x32<>(SB), RODATA|NOPTR, $32
448 DATA c80x32<>+0(SB)/8, $0x8080808080808080
449 DATA c80x32<>+8(SB)/8, $0x8080808080808080
450 DATA c80x32<>+16(SB)/8, $0x8080808080808080
451 DATA c80x32<>+24(SB)/8, $0x8080808080808080
452 GLOBL c80x32<>(SB), RODATA|NOPTR, $32
453 DATA cE2x32<>+0(SB)/8, $0xe2e2e2e2e2e2e2e2
454 DATA cE2x32<>+8(SB)/8, $0xe2e2e2e2e2e2e2e2
455 DATA cE2x32<>+16(SB)/8, $0xe2e2e2e2e2e2e2e2
456 DATA cE2x32<>+24(SB)/8, $0xe2e2e2e2e2e2e2e2
457 GLOBL cE2x32<>(SB), RODATA|NOPTR, $32
458 DATA cFEx32<>+0(SB)/8, $0xfefefefefefefefe
459 DATA cFEx32<>+8(SB)/8, $0xfefefefefefefefe
460 DATA cFEx32<>+16(SB)/8, $0xfefefefefefefefe
461 DATA cFEx32<>+24(SB)/8, $0xfefefefefefefefe
462 GLOBL cFEx32<>(SB), RODATA|NOPTR, $32
463 DATA cA8x32<>+0(SB)/8, $0xa8a8a8a8a8a8a8a8
464 DATA cA8x32<>+8(SB)/8, $0xa8a8a8a8a8a8a8a8
465 DATA cA8x32<>+16(SB)/8, $0xa8a8a8a8a8a8a8a8
466 DATA cA8x32<>+24(SB)/8, $0xa8a8a8a8a8a8a8a8
467 GLOBL cA8x32<>(SB), RODATA|NOPTR, $32