Skip to content

Commit

Permalink
Speed up Blob XOR operations
Browse files Browse the repository at this point in the history
  • Loading branch information
fwojcik committed Aug 21, 2023
1 parent 3a9d37b commit 72e194c
Showing 1 changed file with 40 additions and 3 deletions.
43 changes: 40 additions & 3 deletions util/Blob.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,17 +141,54 @@ class Blob {
Blob operator ^ ( const Blob & k ) const {
Blob t;

for (size_t i = 0; i < _bytes; i++) {
size_t i = _bytes;
while (i >= 8) {
uint64_t a, b;
i -= 8;
memcpy(&a, &bytes[i], 8);
memcpy(&b, &k.bytes[i], 8);
a ^= b;
memcpy(&t.bytes[i], &a, 8);
}
while (i >= 4) {
uint32_t a, b;
i -= 4;
memcpy(&a, &bytes[i], 4);
memcpy(&b, &k.bytes[i], 4);
a ^= b;
memcpy(&t.bytes[i], &a, 4);
}
while (i >= 1) {
i -= 1;
t.bytes[i] = bytes[i] ^ k.bytes[i];
}

return t;
}

Blob & operator ^= ( const Blob & k ) {
for (size_t i = 0; i < _bytes; i++) {
bytes[i] ^= k.bytes[i];
size_t i = _bytes;
while (i >= 8) {
uint64_t a, b;
i -= 8;
memcpy(&a, &bytes[i], 8);
memcpy(&b, &k.bytes[i], 8);
a ^= b;
memcpy(&bytes[i], &a, 8);
}
while (i >= 4) {
uint32_t a, b;
i -= 4;
memcpy(&a, &bytes[i], 4);
memcpy(&b, &k.bytes[i], 4);
a ^= b;
memcpy(&bytes[i], &a, 4);
}
while (i >= 1) {
i -= 1;
bytes[i] = bytes[i] ^ k.bytes[i];
}

return *this;
}

Expand Down

0 comments on commit 72e194c

Please sign in to comment.