Debian Codegraph @

xor_generic.go

raw symbols SCIP
1 // Copyright (c) 2019. Temple3x ([email protected])
2 //
3 // Use of this source code is governed by the MIT License
4 // that can be found in the LICENSE file.
5 //
6 // Copyright 2013 The Go Authors. All rights reserved.
7 // Use of this source code is governed by a BSD-style
8 // license that can be found in the LICENSE file.
9
10 //go:build !amd64
11
12 package xorsimd
13
14 import (
15 "runtime"
16 "unsafe"
17 )
18
19 const supportsUnaligned = runtime.GOARCH == "386" || runtime.GOARCH == "ppc64" || runtime.GOARCH == "ppc64le" || runtime.GOARCH == "s390x"
20
21 func getCPUFeature() int {
22 return generic
23 }
24
25 func encode(dst []byte, src [][]byte) {
26 if supportsUnaligned {
27 fastEncode(dst, src, len(dst))
28 } else {
29 // TODO(hanwen): if (dst, a, b) have common alignment
30 // we could still try fastEncode. It is not clear
31 // how often this happens, and it's only worth it if
32 // the block encryption itself is hardware
33 // accelerated.
34 safeEncode(dst, src, len(dst))
35 }
36
37 }
38
39 // fastEncode XORs in bulk. It only works on architectures that
40 // support unaligned read/writes.
41 func fastEncode(dst []byte, src [][]byte, n int) {
42 w := n / wordSize
43 if w > 0 {
44 wordBytes := w * wordSize
45
46 wordAlignSrc := make([][]byte, len(src))
47 for i := range src {
48 wordAlignSrc[i] = src[i][:wordBytes]
49 }
50 fastEnc(dst[:wordBytes], wordAlignSrc)
51 }
52
53 for i := n - n%wordSize; i < n; i++ {
54 s := src[0][i]
55 for j := 1; j < len(src); j++ {
56 s ^= src[j][i]
57 }
58 dst[i] = s
59 }
60 }
61
62 func fastEnc(dst []byte, src [][]byte) {
63 dw := *(*[]uintptr)(unsafe.Pointer(&dst))
64 sw := make([][]uintptr, len(src))
65 for i := range src {
66 sw[i] = *(*[]uintptr)(unsafe.Pointer(&src[i]))
67 }
68
69 n := len(dst) / wordSize
70 for i := 0; i < n; i++ {
71 s := sw[0][i]
72 for j := 1; j < len(sw); j++ {
73 s ^= sw[j][i]
74 }
75 dw[i] = s
76 }
77 }
78
79 func safeEncode(dst []byte, src [][]byte, n int) {
80 for i := 0; i < n; i++ {
81 s := src[0][i]
82 for j := 1; j < len(src); j++ {
83 s ^= src[j][i]
84 }
85 dst[i] = s
86 }
87 }
88
89 // Bytes8 XORs exactly 8 bytes from a and b into dst.
90 // Each slice must have length >= 8, otherwise it panics.
91 func Bytes8(dst, a, b []byte) {
92
93 bytesWords(dst[:8], a[:8], b[:8])
94 }
95
96 // Bytes16 XORs exactly 16 bytes from a and b into dst.
97 // Each slice must have length >= 16, otherwise it panics.
98 func Bytes16(dst, a, b []byte) {
99
100 bytesWords(dst[:16], a[:16], b[:16])
101 }
102
103 // bytesWords XORs multiples of 4 or 8 bytes (depending on architecture).
104 // The slice arguments a and b are assumed to be of equal length.
105 func bytesWords(dst, a, b []byte) {
106 if supportsUnaligned {
107 dw := *(*[]uintptr)(unsafe.Pointer(&dst))
108 aw := *(*[]uintptr)(unsafe.Pointer(&a))
109 bw := *(*[]uintptr)(unsafe.Pointer(&b))
110 n := len(b) / wordSize
111 for i := 0; i < n; i++ {
112 dw[i] = aw[i] ^ bw[i]
113 }
114 } else {
115 n := len(b)
116 for i := 0; i < n; i++ {
117 dst[i] = a[i] ^ b[i]
118 }
119 }
120 }
121
122 // Bytes8Align XORs exactly 8 bytes from a and b into dst.
123 // Each slice must have length >= 8, otherwise it panics.
124 //
125 // All byte slices must be aligned to wordSize.
126 func Bytes8Align(dst, a, b []byte) {
127
128 bytesWordsAlign(dst[:8], a[:8], b[:8])
129 }
130
131 // Bytes16Align XORs exactly 16 bytes from a and b into dst.
132 // Each slice must have length >= 16, otherwise it panics.
133 //
134 // All byte slices must be aligned to wordSize.
135 func Bytes16Align(dst, a, b []byte) {
136
137 bytesWordsAlign(dst[:16], a[:16], b[:16])
138 }
139
140 // bytesWordsAlign XORs multiples of 4 or 8 bytes (depending on architecture).
141 // The slice arguments a and b are assumed to be of equal length.
142 //
143 // All byte slices must be aligned to wordSize.
144 func bytesWordsAlign(dst, a, b []byte) {
145 dw := *(*[]uintptr)(unsafe.Pointer(&dst))
146 aw := *(*[]uintptr)(unsafe.Pointer(&a))
147 bw := *(*[]uintptr)(unsafe.Pointer(&b))
148 n := len(b) / wordSize
149 for i := 0; i < n; i++ {
150 dw[i] = aw[i] ^ bw[i]
151 }
152 }
153
154 // BytesA XORs len(a) bytes from a and b into dst.
155 // Callers must ensure len(dst) >= len(a) and len(b) >= len(a).
156 //
157 // This helper is intended for small slices where setup overhead dominates.
158 // For larger slices, Bytes is usually faster.
159 func BytesA(dst, a, b []byte) {
160
161 n := len(a)
162 bytesN(dst[:n], a[:n], b[:n], n)
163 }
164
165 // BytesB XORs len(b) bytes from a and b into dst.
166 // Callers must ensure len(dst) >= len(b) and len(a) >= len(b).
167 //
168 // This helper is intended for small slices where setup overhead dominates.
169 // For larger slices, Bytes is usually faster.
170 func BytesB(dst, a, b []byte) {
171
172 n := len(b)
173 bytesN(dst[:n], a[:n], b[:n], n)
174 }
175
176 func bytesN(dst, a, b []byte, n int) {
177
178 switch {
179 case supportsUnaligned:
180 w := n / wordSize
181 if w > 0 {
182 dw := *(*[]uintptr)(unsafe.Pointer(&dst))
183 aw := *(*[]uintptr)(unsafe.Pointer(&a))
184 bw := *(*[]uintptr)(unsafe.Pointer(&b))
185 for i := 0; i < w; i++ {
186 dw[i] = aw[i] ^ bw[i]
187 }
188 }
189
190 for i := (n - n%wordSize); i < n; i++ {
191 dst[i] = a[i] ^ b[i]
192 }
193 default:
194 for i := 0; i < n; i++ {
195 dst[i] = a[i] ^ b[i]
196 }
197 }
198 }