-
Notifications
You must be signed in to change notification settings - Fork 18.4k
crypto/cipher: add optimized assembly xorBytes for ARM (NEON + non-NEON) #53154
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
+180
−9
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
src/crypto/cipher/xor_arm.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// Copyright 2022 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package cipher | ||
|
||
import ( | ||
"internal/cpu" | ||
"unsafe" | ||
) | ||
|
||
const wordSize = int(unsafe.Sizeof(uintptr(0))) | ||
|
||
var hasNEON = cpu.HWCap&(1<<12) != 0 | ||
|
||
func isAligned(a *byte) bool { | ||
return uintptr(unsafe.Pointer(a))%uintptr(wordSize) == 0 | ||
} | ||
|
||
// xorBytes xors the bytes in a and b. The destination should have enough | ||
// space, otherwise xorBytes will panic. Returns the number of bytes xor'd. | ||
func xorBytes(dst, a, b []byte) int { | ||
n := len(a) | ||
if len(b) < n { | ||
n = len(b) | ||
} | ||
if n == 0 { | ||
return 0 | ||
} | ||
// make sure dst has enough space | ||
_ = dst[n-1] | ||
|
||
if hasNEON { | ||
xorBytesNEON32(&dst[0], &a[0], &b[0], n) | ||
} else if isAligned(&dst[0]) && isAligned(&a[0]) && isAligned(&b[0]) { | ||
xorBytesARM32(&dst[0], &a[0], &b[0], n) | ||
} else { | ||
safeXORBytes(dst, a, b, n) | ||
} | ||
return n | ||
} | ||
|
||
// n needs to be smaller or equal than the length of a and b. | ||
func safeXORBytes(dst, a, b []byte, n int) { | ||
for i := 0; i < n; i++ { | ||
dst[i] = a[i] ^ b[i] | ||
} | ||
} | ||
|
||
func xorWords(dst, a, b []byte) { | ||
xorBytes(dst, a, b) | ||
} | ||
|
||
//go:noescape | ||
func xorBytesARM32(dst, a, b *byte, n int) | ||
|
||
//go:noescape | ||
func xorBytesNEON32(dst, a, b *byte, n int) |
114 changes: 114 additions & 0 deletions
src/crypto/cipher/xor_arm.s
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
// Copyright 2022 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
#include "textflag.h" | ||
|
||
// func xorBytesARM32(dst, a, b *byte, n int) | ||
TEXT ·xorBytesARM32(SB), NOSPLIT|NOFRAME, 0ドル | ||
MOVW dst+0(FP), R0 | ||
MOVW a+4(FP), R1 | ||
MOVW b+8(FP), R2 | ||
MOVW n+12(FP), R3 | ||
CMP 4,ドル R3 | ||
BLT less_than4 | ||
|
||
loop_4: | ||
MOVW.P 4(R1), R4 | ||
MOVW.P 4(R2), R5 | ||
EOR R4, R5, R5 | ||
MOVW.P R5, 4(R0) | ||
|
||
SUB 4,ドル R3 | ||
CMP 4,ドル R3 | ||
BGE loop_4 | ||
|
||
less_than4: | ||
CMP 2,ドル R3 | ||
BLT less_than2 | ||
MOVH.P 2(R1), R4 | ||
MOVH.P 2(R2), R5 | ||
EOR R4, R5, R5 | ||
MOVH.P R5, 2(R0) | ||
|
||
SUB 2,ドル R3 | ||
|
||
less_than2: | ||
CMP 0,ドル R3 | ||
BEQ end | ||
MOVB (R1), R4 | ||
MOVB (R2), R5 | ||
EOR R4, R5, R5 | ||
MOVB R5, (R0) | ||
end: | ||
RET | ||
|
||
// func xorBytesNEON32(dst, a, b *byte, n int) | ||
TEXT ·xorBytesNEON32(SB), NOSPLIT|NOFRAME, 0ドル | ||
MOVW dst+0(FP), R0 | ||
MOVW a+4(FP), R1 | ||
MOVW b+8(FP), R2 | ||
MOVW n+12(FP), R3 | ||
CMP 32,ドル R3 | ||
BLT less_than32 | ||
|
||
loop_32: | ||
WORD 0ドルxF421020D // vld1.u8 {q0, q1}, [r1]! | ||
WORD 0ドルxF422420D // vld1.u8 {q2, q3}, [r2]! | ||
WORD 0ドルxF3004154 // veor q2, q0, q2 | ||
WORD 0ドルxF3026156 // veor q3, q1, q3 | ||
WORD 0ドルxF400420D // vst1.u8 {q2, q3}, [r0]! | ||
|
||
SUB 32,ドル R3 | ||
CMP 32,ドル R3 | ||
BGE loop_32 | ||
|
||
less_than32: | ||
CMP 16,ドル R3 | ||
BLT less_than16 | ||
WORD 0ドルxF4210A0D // vld1.u8 q0, [r1]! | ||
WORD 0ドルxF4222A0D // vld1.u8 q1, [r2]! | ||
WORD 0ドルxF3002152 // veor q1, q0, q1 | ||
WORD 0ドルxF4002A0D // vst1.u8 {q1}, [r0]! | ||
|
||
SUB 16,ドル R3 | ||
|
||
less_than16: | ||
CMP 8,ドル R3 | ||
BLT less_than8 | ||
WORD 0ドルxF421070D // vld1.u8 d0, [r1]! | ||
WORD 0ドルxF422170D // vld1.u8 d1, [r2]! | ||
WORD 0ドルxF3001111 // veor d1, d0, d1 | ||
WORD 0ドルxF400170D // vst1.u8 {d1}, [r0]! | ||
|
||
SUB 8,ドル R3 | ||
|
||
less_than8: | ||
CMP 4,ドル R3 | ||
BLT less_than4 | ||
MOVW.P 4(R1), R4 | ||
MOVW.P 4(R2), R5 | ||
EOR R4, R5, R5 | ||
MOVW.P R5, 4(R0) | ||
|
||
SUB 4,ドル R3 | ||
|
||
less_than4: | ||
CMP 2,ドル R3 | ||
BLT less_than2 | ||
MOVH.P 2(R1), R4 | ||
MOVH.P 2(R2), R5 | ||
EOR R4, R5, R5 | ||
MOVH.P R5, 2(R0) | ||
|
||
SUB 2,ドル R3 | ||
|
||
less_than2: | ||
CMP 0,ドル R3 | ||
BEQ end | ||
MOVB (R1), R4 | ||
MOVB (R2), R5 | ||
EOR R4, R5, R5 | ||
MOVB R5, (R0) | ||
end: | ||
RET |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.