File: xor_test.go

package info (click to toggle)
golang-github-templexxx-xor 0.1.2-5~bpo9+1
  • links: PTS, VCS
  • area: main
  • in suites: stretch-backports
  • size: 108 kB
  • sloc: asm: 834; makefile: 2
file content (299 lines) | stat: -rw-r--r-- 6,508 bytes parent folder | download | duplicates (4)
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
package xor

import (
	"bytes"
	"math/rand"
	"testing"
)

func TestVerifyBytesNoSIMD(t *testing.T) {
	for i := 1; i <= unitSize+16+2; i++ {
		if !verifyBytesNoSIMD(i) {
			t.Fatal("xor fault ", "size:", i)
		}
	}
}

func verifyBytesNoSIMD(size int) bool {
	dst := make([]byte, size)
	src0 := make([]byte, size)
	src1 := make([]byte, size)
	expect := make([]byte, size)
	rand.Seed(7)
	fillRandom(src0)
	rand.Seed(8)
	fillRandom(src1)
	for i := 0; i < size; i++ {
		expect[i] = src0[i] ^ src1[i]
	}
	xorBytes(dst, src0, src1, size)
	return bytes.Equal(expect, dst)
}

func TestVerifyBytes(t *testing.T) {
	for i := 1; i <= unitSize+16+2; i++ {
		if !verifyBytes(i) {
			t.Fatal("xor fault ", "size:", i)
		}
	}
}

func verifyBytes(size int) bool {
	dst := make([]byte, size)
	src0 := make([]byte, size)
	src1 := make([]byte, size)
	expect := make([]byte, size)
	rand.Seed(7)
	fillRandom(src0)
	rand.Seed(8)
	fillRandom(src1)
	for i := 0; i < size; i++ {
		expect[i] = src0[i] ^ src1[i]
	}
	xorBytes(dst, src0, src1, size)
	return bytes.Equal(expect, dst)
}

func TestVerifyBytesSrc1(t *testing.T) {
	for i := 1; i <= unitSize+16+2; i++ {
		if !verifyBytesSrc1(i) {
			t.Fatal("xor fault ", "size:", i)
		}
	}
}
func verifyBytesSrc1(size int) bool {
	dst := make([]byte, size)
	src0 := make([]byte, size)
	src1 := make([]byte, size)
	expect := make([]byte, size)
	rand.Seed(7)
	fillRandom(src0)
	rand.Seed(8)
	fillRandom(src1)
	for i := 0; i < size; i++ {
		expect[i] = src0[i] ^ src1[i]
	}
	xorSrc0(dst, src0, src1)
	return bytes.Equal(expect, dst)
}

func TestVerifyMatrixNoSIMD(t *testing.T) {
	for i := 1; i <= unitSize+16+2; i++ {
		if !verifyMatrixNoSIMD(i) {
			t.Fatal("xor fault ", "size:", i)
		}
	}
}

func verifyMatrixNoSIMD(size int) bool {
	numSRC := 3
	dst := make([]byte, size)
	expect := make([]byte, size)
	src := make([][]byte, numSRC)
	for i := 0; i < numSRC; i++ {
		src[i] = make([]byte, size)
		rand.Seed(int64(i))
		fillRandom(src[i])
	}
	for i := 0; i < size; i++ {
		expect[i] = src[0][i] ^ src[1][i]
	}
	for i := 2; i < numSRC; i++ {
		for j := 0; j < size; j++ {
			expect[j] ^= src[i][j]
		}
	}
	matrixNoSIMD(dst, src)
	return bytes.Equal(expect, dst)
}

func TestVerifyMatrix(t *testing.T) {
	for i := 1; i <= unitSize+16+2; i++ {
		if !verifyMatrix(i) {
			t.Fatal("xor fault ", "size:", i)
		}
	}
}

func verifyMatrix(size int) bool {
	numSRC := 3
	dst := make([]byte, size)
	expect := make([]byte, size)
	src := make([][]byte, numSRC)
	for i := 0; i < numSRC; i++ {
		src[i] = make([]byte, size)
		rand.Seed(int64(i))
		fillRandom(src[i])
	}
	for i := 0; i < size; i++ {
		expect[i] = src[0][i] ^ src[1][i]
	}
	for i := 2; i < numSRC; i++ {
		for j := 0; j < size; j++ {
			expect[j] ^= src[i][j]
		}
	}
	xorMatrix(dst, src)
	return bytes.Equal(expect, dst)
}

func BenchmarkBytesNoSIMDx12B(b *testing.B) {
	benchmarkBytesNoSIMD(b, 12)
}
func BenchmarkBytes12B(b *testing.B) {
	benchmarkBytesMini(b, 12)
}
func BenchmarkBytesNoSIMD16B(b *testing.B) {
	benchmarkBytesNoSIMD(b, 16)
}
func BenchmarkBytes16B(b *testing.B) {
	benchmarkBytesMini(b, 16)
}
func BenchmarkBytesNoSIMD24B(b *testing.B) {
	benchmarkBytesNoSIMD(b, 24)
}
func BenchmarkBytes24B(b *testing.B) {
	benchmarkBytesMini(b, 24)
}
func BenchmarkBytesNoSIMD32B(b *testing.B) {
	benchmarkBytesNoSIMD(b, 32)
}
func BenchmarkBytes32B(b *testing.B) {
	benchmarkBytesMini(b, 32)
}
func benchmarkBytesMini(b *testing.B, size int) {
	src0 := make([]byte, size)
	src1 := make([]byte, size)
	dst := make([]byte, size)
	rand.Seed(int64(0))
	fillRandom(src0)
	rand.Seed(int64(1))
	fillRandom(src1)
	BytesSrc1(dst, src0, src1)
	b.SetBytes(int64(size) * 2)
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		BytesSrc1(dst, src0, src1)
	}
}

func BenchmarkBytesNoSIMD1K(b *testing.B) {
	benchmarkBytesNoSIMD(b, 1024)
}
func BenchmarkBytesNoSIMD16K(b *testing.B) {
	benchmarkBytesNoSIMD(b, 16*1024)
}
func BenchmarkBytesNoSIMD16M(b *testing.B) {
	benchmarkBytesNoSIMD(b, 16*1024*1024)
}
func benchmarkBytesNoSIMD(b *testing.B, size int) {
	src1 := make([]byte, size)
	src2 := make([]byte, size)
	dst := make([]byte, size)
	rand.Seed(int64(0))
	fillRandom(src1)
	rand.Seed(int64(1))
	fillRandom(src2)
	bytesNoSIMD(dst, src1, src2, size)
	b.SetBytes(int64(size) * 2)
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		bytesNoSIMD(dst, src1, src2, size)
	}
}

func BenchmarkBytes1K(b *testing.B) {
	benchmarkBytes(b, 1024)
}
func BenchmarkBytes16K(b *testing.B) {
	benchmarkBytes(b, 16*1024)
}
func BenchmarkBytes16M(b *testing.B) {
	benchmarkBytes(b, 16*1024*1024)
}

// compare with bytes
func BenchmarkMatrix2x1K(b *testing.B) {
	benchmarkMatrix(b, 2, 1024)
}
func BenchmarkMatrix2x16K(b *testing.B) {
	benchmarkMatrix(b, 2, 16*1024)
}
func BenchmarkMatrix2x16M(b *testing.B) {
	benchmarkMatrix(b, 2, 16*1024*1024)
}
func benchmarkBytes(b *testing.B, size int) {
	src1 := make([]byte, size)
	src2 := make([]byte, size)
	dst := make([]byte, size)
	rand.Seed(int64(0))
	fillRandom(src1)
	rand.Seed(int64(1))
	fillRandom(src2)
	xorBytes(dst, src1, src2, size)
	b.SetBytes(int64(size) * 2)
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		xorBytes(dst, src1, src2, size)
	}
}

func BenchmarkMatrixNoSIMD5x1K(b *testing.B) {
	benchmarkMatrixNoSIMD(b, 5, 1024)
}
func BenchmarkMatrixNoSIMD5x16K(b *testing.B) {
	benchmarkMatrixNoSIMD(b, 5, 16*1024)
}
func BenchmarkMatrixNoSIMD5x16M(b *testing.B) {
	benchmarkMatrixNoSIMD(b, 5, 16*1024*1024)
}
func benchmarkMatrixNoSIMD(b *testing.B, numSRC, size int) {
	src := make([][]byte, numSRC)
	dst := make([]byte, size)
	for i := 0; i < numSRC; i++ {
		rand.Seed(int64(i))
		src[i] = make([]byte, size)
		fillRandom(src[i])
	}
	matrixNoSIMD(dst, src)
	b.SetBytes(int64(size * numSRC))
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		matrixNoSIMD(dst, src)
	}
}

func BenchmarkMatrix5x1K(b *testing.B) {
	benchmarkMatrix(b, 5, 1024)
}
func BenchmarkMatrix5x16K(b *testing.B) {
	benchmarkMatrix(b, 5, 16*1024)
}
func BenchmarkMatrix5x16M(b *testing.B) {
	benchmarkMatrix(b, 5, 16*1024*1024)
}
func benchmarkMatrix(b *testing.B, numSRC, size int) {
	src := make([][]byte, numSRC)
	dst := make([]byte, size)
	for i := 0; i < numSRC; i++ {
		rand.Seed(int64(i))
		src[i] = make([]byte, size)
		fillRandom(src[i])
	}
	xorMatrix(dst, src)
	b.SetBytes(int64(size * numSRC))
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		xorMatrix(dst, src)
	}
}

func fillRandom(p []byte) {
	for i := 0; i < len(p); i += 7 {
		val := rand.Int63()
		for j := 0; i+j < len(p) && j < 7; j++ {
			p[i+j] = byte(val)
			val >>= 8
		}
	}
}