123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299 |
- 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
- }
- }
- }
|