mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-22 12:46:44 +00:00
common/bitutil: remove safe fallback
This commit is contained in:
parent
6646b42dd4
commit
b838c4c9f7
2 changed files with 14 additions and 15 deletions
|
|
@ -22,19 +22,6 @@ func XORBytes(dst, a, b []byte) int {
|
||||||
return subtle.XORBytes(dst, a, b)
|
return subtle.XORBytes(dst, a, b)
|
||||||
}
|
}
|
||||||
|
|
||||||
// safeXORBytes xors one by one. It works on all architectures, independent if
|
|
||||||
// it supports unaligned read/writes or not.
|
|
||||||
func safeXORBytes(dst, a, b []byte) int {
|
|
||||||
n := len(a)
|
|
||||||
if len(b) < n {
|
|
||||||
n = len(b)
|
|
||||||
}
|
|
||||||
for i := 0; i < n; i++ {
|
|
||||||
dst[i] = a[i] ^ b[i]
|
|
||||||
}
|
|
||||||
return n
|
|
||||||
}
|
|
||||||
|
|
||||||
// ANDBytes ands the bytes in a and b. The destination is assumed to have enough
|
// ANDBytes ands the bytes in a and b. The destination is assumed to have enough
|
||||||
// space. Returns the number of bytes and'd.
|
// space. Returns the number of bytes and'd.
|
||||||
func ANDBytes(dst, a, b []byte) int {
|
func ANDBytes(dst, a, b []byte) int {
|
||||||
|
|
|
||||||
|
|
@ -29,7 +29,7 @@ func TestXOR(t *testing.T) {
|
||||||
d2 := make([]byte, 1023+alignD)[alignD:]
|
d2 := make([]byte, 1023+alignD)[alignD:]
|
||||||
|
|
||||||
XORBytes(d1, p, q)
|
XORBytes(d1, p, q)
|
||||||
safeXORBytes(d2, p, q)
|
naiveXOR(d2, p, q)
|
||||||
if !bytes.Equal(d1, d2) {
|
if !bytes.Equal(d1, d2) {
|
||||||
t.Error("not equal", d1, d2)
|
t.Error("not equal", d1, d2)
|
||||||
}
|
}
|
||||||
|
|
@ -38,6 +38,18 @@ func TestXOR(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// naiveXOR xors bytes one by one.
|
||||||
|
func naiveXOR(dst, a, b []byte) int {
|
||||||
|
n := len(a)
|
||||||
|
if len(b) < n {
|
||||||
|
n = len(b)
|
||||||
|
}
|
||||||
|
for i := 0; i < n; i++ {
|
||||||
|
dst[i] = a[i] ^ b[i]
|
||||||
|
}
|
||||||
|
return n
|
||||||
|
}
|
||||||
|
|
||||||
// Tests that bitwise AND works for various alignments.
|
// Tests that bitwise AND works for various alignments.
|
||||||
func TestAND(t *testing.T) {
|
func TestAND(t *testing.T) {
|
||||||
for alignP := 0; alignP < 2; alignP++ {
|
for alignP := 0; alignP < 2; alignP++ {
|
||||||
|
|
@ -134,7 +146,7 @@ func benchmarkBaseXOR(b *testing.B, size int) {
|
||||||
p, q := make([]byte, size), make([]byte, size)
|
p, q := make([]byte, size), make([]byte, size)
|
||||||
|
|
||||||
for i := 0; i < b.N; i++ {
|
for i := 0; i < b.N; i++ {
|
||||||
safeXORBytes(p, p, q)
|
naiveXOR(p, p, q)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue