func testChunkDiff(t *testing.T, a, b *chunk) { if a.ctx != b.ctx { panic("chunks with different contexts") } bitsDiff := 0 for i := uint32(0); i < a.ctx.chunkSize; i++ { bitsDiff += int(swar.Ones8(a.data[i] ^ b.data[i])) } if bitsDiff != 1 { t.Errorf("%#v and %#v differ by more than 1 bit (by %v)", a.data, b.data, bitsDiff) } }
// readBitMask xors the bits specified out of the byte B by applying the // mask and then stores the value in the atom a at the specified atom // bit index abi. func (c *chunk) readBitMask(a *atom, abi uint8, mask byte, B byte) { // First, extract the desired bits from the chunk byte by using // the mask. x := mask & B // We then want to XOR together the bits specified by the mask. // The key is to recognize that this is the same as taking the // 8-bit population count (ones count, or Hamming weight) and // then examining the parity. If even, then 0; if odd, then 1. x = swar.Ones8(x) % 2 // XOR the bit into the atom. a.xorBit(x, abi) // Done! }
func testBytesDiff(t *testing.T, a, b []byte, expectBits int) { var m int if len(a) < len(b) { m = len(a) } else { m = len(b) } bitsDiff := 0 for i := 0; i < m; i++ { bitsDiff += int(swar.Ones8(a[i] ^ b[i])) } if bitsDiff != expectBits { t.Errorf("not %v bit difference (is %v)", expectBits, bitsDiff) } }