|
| 1 | +// Copyright 2018 The Go Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | +// inspired by: https://github.com/golang/go/blob/4a3cef2036097d323b6cc0bbe90fc4d8c7588660/src/crypto/internal/fips140/subtle/xor_amd64.s |
| 5 | + |
| 6 | +//go:build !purego |
| 7 | + |
| 8 | +#include "textflag.h" |
| 9 | + |
| 10 | +// func testBytesASM(p *byte, n int) bool |
| 11 | +TEXT ·testBytesASM(SB), NOSPLIT, $0 |
| 12 | + MOVQ p+0(FP), SI |
| 13 | + MOVQ n+8(FP), DX |
| 14 | + TESTQ DX, DX // if len is 0, return false |
| 15 | + JZ not_found |
| 16 | + TESTQ $15, DX // AND 15 & len, if not zero jump to not_aligned. |
| 17 | + JNZ not_aligned |
| 18 | + |
| 19 | +aligned: |
| 20 | + MOVQ $0, AX // position in slice |
| 21 | + |
| 22 | + PCALIGN $16 |
| 23 | +loop16b: |
| 24 | + MOVOU (SI)(AX*1), X0 // Load 16 bytes |
| 25 | + PTEST X0, X0 // Test if all bits are zero (ZF=1 if all zero) |
| 26 | + JNZ found // If any bit is set (ZF=0), jump to found |
| 27 | + ADDQ $16, AX |
| 28 | + CMPQ DX, AX |
| 29 | + JNE loop16b |
| 30 | + JMP not_found |
| 31 | + |
| 32 | + PCALIGN $16 |
| 33 | +loop_1b: |
| 34 | + SUBQ $1, DX // Test 1 byte backwards. |
| 35 | + MOVB (SI)(DX*1), DI |
| 36 | + TESTB DI, DI // Test if byte is non-zero |
| 37 | + JNZ found |
| 38 | + TESTQ $7, DX // AND 7 & len, if not zero jump to loop_1b. |
| 39 | + JNZ loop_1b |
| 40 | + CMPQ DX, $0 // if len is 0, ret. |
| 41 | + JE not_found |
| 42 | + TESTQ $15, DX // AND 15 & len, if zero jump to aligned. |
| 43 | + JZ aligned |
| 44 | + |
| 45 | +not_aligned: |
| 46 | + TESTQ $7, DX // AND $7 & len, if not zero jump to loop_1b. |
| 47 | + JNE loop_1b |
| 48 | + SUBQ $8, DX // Test 8 bytes backwards. |
| 49 | + MOVQ (SI)(DX*1), DI |
| 50 | + TESTQ DI, DI // Test if 8 bytes are non-zero |
| 51 | + JNZ found |
| 52 | + CMPQ DX, $16 // if len is greater or equal 16 here, it must be aligned. |
| 53 | + JGE aligned |
| 54 | + JMP not_found |
| 55 | + |
| 56 | +not_found: |
| 57 | + MOVB $0, ret+16(FP) |
| 58 | + RET |
| 59 | + |
| 60 | +found: |
| 61 | + MOVB $1, ret+16(FP) |
| 62 | + RET |
| 63 | + |
0 commit comments