Debian Codegraph @

utils/xor/xor_arm.go

raw symbols SCIP
1 // Copyright 2022 The Pion 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
5 // Package xor provides utility functions used by other Pion
6 // packages. ARM arch.
7 package xor
8
9 import (
10 "golang.org/x/sys/cpu"
11 "unsafe"
12 )
13
14 const wordSize = int(unsafe.Sizeof(uintptr(0))) // nolint:gosec
15 var hasNEON = cpu.ARM.HasNEON // nolint:gochecknoglobals
16
17 func isAligned(a *byte) bool {
18 return uintptr(unsafe.Pointer(a))%uintptr(wordSize) == 0
19 }
20
21 // XorBytes xors the bytes in a and b. The destination should have enough
22 // space, otherwise xorBytes will panic. Returns the number of bytes xor'd.
23 //revive:disable-next-line
24 func XorBytes(dst, a, b []byte) int {
25 n := len(a)
26 if len(b) < n {
27 n = len(b)
28 }
29 if n == 0 {
30 return 0
31 }
32 // make sure dst has enough space
33 _ = dst[n-1]
34
35 if hasNEON {
36 xorBytesNEON32(&dst[0], &a[0], &b[0], n)
37 } else if isAligned(&dst[0]) && isAligned(&a[0]) && isAligned(&b[0]) {
38 xorBytesARM32(&dst[0], &a[0], &b[0], n)
39 } else {
40 safeXORBytes(dst, a, b, n)
41 }
42 return n
43 }
44
45 // n needs to be smaller or equal than the length of a and b.
46 func safeXORBytes(dst, a, b []byte, n int) {
47 for i := 0; i < n; i++ {
48 dst[i] = a[i] ^ b[i]
49 }
50 }
51
52 //go:noescape
53 func xorBytesARM32(dst, a, b *byte, n int)
54
55 //go:noescape
56 func xorBytesNEON32(dst, a, b *byte, n int)