-
Notifications
You must be signed in to change notification settings - Fork 10
/
xor_test.go
480 lines (392 loc) · 8.45 KB
/
xor_test.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
// Copyright (c) 2019. Temple3x ([email protected])
//
// Use of this source code is governed by the MIT License
// that can be found in the LICENSE file.
//
// Copyright 2013 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.
//
// TestEncodeBytes is copied from Go Standard lib:
// crypto/cipher/xor_test.go
package xorsimd
import (
"bytes"
"fmt"
"math/rand"
"testing"
"time"
"unsafe"
)
const (
kb = 1024
mb = 1024 * 1024
testSize = kb
)
func TestBytes8(t *testing.T) {
rand.Seed(time.Now().UnixNano())
for j := 0; j < 1024; j++ {
a := make([]byte, 8)
b := make([]byte, 8)
fillRandom(a)
fillRandom(b)
dst0 := make([]byte, 8)
Bytes8(dst0, a, b)
dst1 := make([]byte, 8)
for i := 0; i < 8; i++ {
dst1[i] = a[i] ^ b[i]
}
if !bytes.Equal(dst0, dst1) {
t.Fatal("not equal", a, b, dst0, dst1)
}
}
}
func TestBytes16(t *testing.T) {
rand.Seed(time.Now().UnixNano())
for j := 0; j < 1024; j++ {
a := make([]byte, 16)
b := make([]byte, 16)
fillRandom(a)
fillRandom(b)
dst0 := make([]byte, 16)
Bytes16(dst0, a, b)
dst1 := make([]byte, 16)
for i := 0; i < 16; i++ {
dst1[i] = a[i] ^ b[i]
}
if !bytes.Equal(dst0, dst1) {
t.Fatal("not equal", dst0, dst1, a, b)
}
}
}
const wordSize = int(unsafe.Sizeof(uintptr(0)))
func TestBytes8Align(t *testing.T) {
rand.Seed(time.Now().UnixNano())
for j := 0; j < 1024; j++ {
a := make([]byte, 8+wordSize)
b := make([]byte, 8+wordSize)
dst0 := make([]byte, 8+wordSize)
dst1 := make([]byte, 8+wordSize)
al := alignment(a)
offset := 0
if al != 0 {
offset = wordSize - al
}
a = a[offset : offset+8]
al = alignment(b)
offset = 0
if al != 0 {
offset = wordSize - al
}
b = b[offset : offset+8]
al = alignment(dst0)
offset = 0
if al != 0 {
offset = wordSize - al
}
dst0 = dst0[offset : offset+8]
al = alignment(dst1)
offset = 0
if al != 0 {
offset = wordSize - al
}
dst1 = dst1[offset : offset+8]
fillRandom(a)
fillRandom(b)
Bytes8Align(dst0, a, b)
for i := 0; i < 8; i++ {
dst1[i] = a[i] ^ b[i]
}
if !bytes.Equal(dst0, dst1) {
t.Fatal("not equal", a, b, dst0, dst1)
}
}
}
func alignment(s []byte) int {
return int(uintptr(unsafe.Pointer(&s[0])) & uintptr(wordSize-1))
}
func TestBytes16Align(t *testing.T) {
rand.Seed(time.Now().UnixNano())
for j := 0; j < 1024; j++ {
a := make([]byte, 16+wordSize)
b := make([]byte, 16+wordSize)
dst0 := make([]byte, 16+wordSize)
dst1 := make([]byte, 16+wordSize)
al := alignment(a)
offset := 0
if al != 0 {
offset = wordSize - al
}
a = a[offset : offset+16]
al = alignment(b)
offset = 0
if al != 0 {
offset = wordSize - al
}
b = b[offset : offset+16]
al = alignment(dst0)
offset = 0
if al != 0 {
offset = wordSize - al
}
dst0 = dst0[offset : offset+16]
al = alignment(dst1)
offset = 0
if al != 0 {
offset = wordSize - al
}
dst1 = dst1[offset : offset+16]
fillRandom(a)
fillRandom(b)
Bytes16Align(dst0, a, b)
for i := 0; i < 16; i++ {
dst1[i] = a[i] ^ b[i]
}
if !bytes.Equal(dst0, dst1) {
t.Fatal("not equal", a, b, dst0, dst1)
}
}
}
func TestBytesA(t *testing.T) {
rand.Seed(time.Now().UnixNano())
for j := 2; j <= 1024; j++ {
for alignP := 0; alignP < 2; alignP++ {
p := make([]byte, j)[alignP:]
q := make([]byte, j)
d1 := make([]byte, j)
d2 := make([]byte, j)
fillRandom(p)
fillRandom(q)
BytesA(d1, p, q)
for i := 0; i < j-alignP; i++ {
d2[i] = p[i] ^ q[i]
}
if !bytes.Equal(d1, d2) {
t.Fatal("not equal")
}
}
}
}
func TestBytesB(t *testing.T) {
rand.Seed(time.Now().UnixNano())
for j := 2; j <= 1024; j++ {
for alignQ := 0; alignQ < 2; alignQ++ {
p := make([]byte, j)
q := make([]byte, j)[alignQ:]
d1 := make([]byte, j)
d2 := make([]byte, j)
fillRandom(p)
fillRandom(q)
BytesB(d1, p, q)
for i := 0; i < j-alignQ; i++ {
d2[i] = p[i] ^ q[i]
}
if !bytes.Equal(d1, d2) {
t.Fatal("not equal")
}
}
}
}
func TestBytes(t *testing.T) {
rand.Seed(time.Now().UnixNano())
for j := 1; j <= 1024; j++ {
for alignP := 0; alignP < 2; alignP++ {
for alignQ := 0; alignQ < 2; alignQ++ {
for alignD := 0; alignD < 2; alignD++ {
p := make([]byte, j)[alignP:]
q := make([]byte, j)[alignQ:]
d1 := make([]byte, j)[alignD:]
d2 := make([]byte, j)[alignD:]
fillRandom(p)
fillRandom(q)
Bytes(d1, p, q)
n := min(p, q, d1)
for i := 0; i < n; i++ {
d2[i] = p[i] ^ q[i]
}
if !bytes.Equal(d1, d2) {
t.Fatal("not equal")
}
}
}
}
}
}
func min(a, b, c []byte) int {
n := len(a)
if len(b) < n {
n = len(b)
}
if len(c) < n {
n = len(c)
}
return n
}
func TestEncodeWithFeature(t *testing.T) {
max := testSize
switch getCPUFeature() {
case avx512:
testEncode(t, max, sse2, -1)
testEncode(t, max, avx2, sse2)
testEncode(t, max, avx512, avx2)
case avx2:
testEncode(t, max, sse2, -1)
testEncode(t, max, avx2, sse2)
case sse2:
testEncode(t, max, sse2, -1)
case generic:
testEncode(t, max, generic, -1)
}
}
func testEncode(t *testing.T, maxSize, feat, cmpFeat int) {
rand.Seed(time.Now().UnixNano())
srcN := randIntn(10, 2) // Cannot be 1, see func encode(dst []byte, src [][]byte, feature int).
fs := featToStr(feat)
for size := 1; size <= maxSize; size++ {
exp := make([]byte, size)
src := make([][]byte, srcN)
for j := 0; j < srcN; j++ {
src[j] = make([]byte, size)
fillRandom(src[j])
}
if cmpFeat < 0 {
encodeTested(exp, src)
} else {
cpuFeature = cmpFeat
Encode(exp, src)
}
act := make([]byte, size)
cpuFeature = feat
Encode(act, src)
if !bytes.Equal(exp, act) {
t.Fatalf("%s mismatched with %s, src_num: %d, size: %d",
fs, featToStr(cmpFeat), srcN, size)
}
}
t.Logf("%s pass src_num:%d, max_size: %d",
fs, srcN, maxSize)
}
func featToStr(f int) string {
switch f {
case avx512:
return "AVX512"
case avx2:
return "AVX2"
case sse2:
return "SSE2"
case generic:
return "Generic"
default:
return "Tested"
}
}
func encodeTested(dst []byte, src [][]byte) {
n := len(dst)
for i := 0; i < n; i++ {
s := src[0][i]
for j := 1; j < len(src); j++ {
s ^= src[j][i]
}
dst[i] = s
}
}
// randIntn returns, as an int, a non-negative pseudo-random number in [min,n)
// from the default Source.
func randIntn(n, min int) int {
m := rand.Intn(n)
if m < min {
m = min
}
return m
}
func BenchmarkBytes8(b *testing.B) {
s0 := make([]byte, 8)
s1 := make([]byte, 8)
fillRandom(s0)
fillRandom(s1)
dst0 := make([]byte, 8)
b.ResetTimer()
b.SetBytes(8)
for i := 0; i < b.N; i++ {
Bytes8(dst0, s0, s1)
}
}
func BenchmarkBytes16(b *testing.B) {
s0 := make([]byte, 16)
s1 := make([]byte, 16)
fillRandom(s0)
fillRandom(s1)
dst0 := make([]byte, 16)
b.ResetTimer()
b.SetBytes(16)
for i := 0; i < b.N; i++ {
Bytes16(dst0, s0, s1)
}
}
func BenchmarkBytesN_16Bytes(b *testing.B) {
s0 := make([]byte, 16)
s1 := make([]byte, 16)
fillRandom(s0)
fillRandom(s1)
dst0 := make([]byte, 16)
b.ResetTimer()
b.SetBytes(16)
for i := 0; i < b.N; i++ {
BytesA(dst0, s0, s1)
}
}
func BenchmarkEncode(b *testing.B) {
sizes := []int{4 * kb, mb, 8 * mb}
srcNums := []int{5, 10}
var feats []int
switch getCPUFeature() {
case avx512:
feats = append(feats, avx512)
feats = append(feats, avx2)
feats = append(feats, sse2)
case avx2:
feats = append(feats, avx2)
feats = append(feats, sse2)
case sse2:
feats = append(feats, sse2)
default:
feats = append(feats, generic)
}
b.Run("", benchEncRun(benchEnc, srcNums, sizes, feats))
}
func benchEncRun(f func(*testing.B, int, int, int), srcNums, sizes, feats []int) func(*testing.B) {
return func(b *testing.B) {
for _, feat := range feats {
for _, srcNum := range srcNums {
for _, size := range sizes {
b.Run(fmt.Sprintf("(%d+1)-%s-%s", srcNum, byteToStr(size), featToStr(feat)), func(b *testing.B) {
f(b, srcNum, size, feat)
})
}
}
}
}
}
func benchEnc(b *testing.B, srcNum, size, feat int) {
dst := make([]byte, size)
src := make([][]byte, srcNum)
for i := 0; i < srcNum; i++ {
src[i] = make([]byte, size)
fillRandom(src[i])
}
cpuFeature = feat
b.SetBytes(int64((srcNum + 1) * size))
b.ResetTimer()
for i := 0; i < b.N; i++ {
encode(dst, src)
}
}
func fillRandom(p []byte) {
rand.Read(p)
}
func byteToStr(n int) string {
if n >= mb {
return fmt.Sprintf("%dMB", n/mb)
}
return fmt.Sprintf("%dKB", n/kb)
}