-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathxor_amd64.go
115 lines (98 loc) · 2.21 KB
/
xor_amd64.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// +build go1.7,amd64,!gccgo,!appengine,!nacl
package fastxor
import (
"unsafe"
"golang.org/x/sys/cpu"
)
//go:noescape
func xorBytesSSE(dst, a, b []byte, n int)
//go:noescape
func xorBytesAVX2(dst, a, b []byte, n int)
func min(a, b, c int) int {
if a < b {
b = a
}
if b < c {
c = b
}
return c
}
// Bytes stores (a xor b) in dst, stopping when the end of any slice is
// reached. It returns the number of bytes xor'd.
func Bytes(dst, a, b []byte) int {
n := min(len(dst), len(a), len(b))
if n == 0 {
return 0
}
switch {
case cpu.X86.HasAVX2:
xorBytesAVX2(dst, a, b, n)
case cpu.X86.HasSSE2:
xorBytesSSE(dst, a, b, n)
default:
xorBytesGeneric(dst, a, b, n)
}
return n
}
const wordSize = int(unsafe.Sizeof(uintptr(0)))
func xorBytesGeneric(dst, a, b []byte, n int) {
// Assert dst has enough space
_ = dst[n-1]
w := n / wordSize
if w > 0 {
dw := *(*[]uintptr)(unsafe.Pointer(&dst))
aw := *(*[]uintptr)(unsafe.Pointer(&a))
bw := *(*[]uintptr)(unsafe.Pointer(&b))
_ = aw[w-1]
_ = bw[w-1]
_ = dw[w-1]
for i := 0; i < w; i++ {
dw[i] = aw[i] ^ bw[i]
}
}
_ = dst[n-1]
_ = a[n-1]
_ = b[n-1]
for i := (n - n%wordSize); i < n; i++ {
dst[i] = a[i] ^ b[i]
}
}
// Byte xors each byte in a with b and stores the result in dst, stopping when
// the end of either dst or a is reached. It returns the number of bytes
// xor'd.
func Byte(dst, a []byte, b byte) int {
n := len(a)
if len(dst) < n {
n = len(dst)
}
var bw uintptr
for i := 0; i < wordSize; i += 1 {
bw |= uintptr(b) << uint(i*8)
}
w := n / wordSize
if w > 0 {
dw := *(*[]uintptr)(unsafe.Pointer(&dst))
aw := *(*[]uintptr)(unsafe.Pointer(&a))
_ = aw[w-1]
_ = dw[w-1]
for i := 0; i < w; i++ {
dw[i] = aw[i] ^ bw
}
}
_ = dst[n-1]
_ = a[n-1]
for i := (n - n%wordSize); i < n; i++ {
dst[i] = a[i] ^ b
}
return n
}
// Block stores (a xor b) in dst, where a, b, and dst all have length 16.
func Block(dst, a, b []byte) {
// profiling indicates that for 16-byte blocks, the cost of a function
// call outweighs the SSE/AVX speedup
dw := (*[2]uintptr)(unsafe.Pointer(&dst[0]))
aw := (*[2]uintptr)(unsafe.Pointer(&a[0]))
bw := (*[2]uintptr)(unsafe.Pointer(&b[0]))
dw[0] = aw[0] ^ bw[0]
dw[1] = aw[1] ^ bw[1]
}