bitfield: unroll main operators

This commit is contained in:
Alex Coyte 2016-04-12 09:52:53 +10:00 committed by Matthew Barr
parent 938ac9fd38
commit 41751c4f3b

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2015, Intel Corporation * Copyright (c) 2015-2016, Intel Corporation
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met: * modification, are permitted provided that the following conditions are met:
@ -187,8 +187,15 @@ public:
size_t count() const { size_t count() const {
static_assert(block_size == 64, "adjust popcount for block_type"); static_assert(block_size == 64, "adjust popcount for block_type");
size_t sum = 0; size_t sum = 0;
for (const auto &e : bits) { size_t i = 0;
sum += popcount64(e); for (; i + 4 <= num_blocks; i += 4) {
sum += popcount64(bits[i]);
sum += popcount64(bits[i + 1]);
sum += popcount64(bits[i + 2]);
sum += popcount64(bits[i + 3]);
}
for (; i < num_blocks; i++) {
sum += popcount64(bits[i]);
} }
assert(sum <= size()); assert(sum <= size());
return sum; return sum;
@ -298,49 +305,61 @@ public:
} }
/// Bitwise OR. /// Bitwise OR.
bitfield operator|(const bitfield &a) const { bitfield operator|(bitfield a) const {
bitfield cr; a |= *this;
for (size_t i = 0; i < bits.size(); i++) { return a;
cr.bits[i] = bits[i] | a.bits[i];
}
return cr;
} }
/// Bitwise OR-equals. /// Bitwise OR-equals.
void operator|=(const bitfield &a) { void operator|=(const bitfield &a) {
for (size_t i = 0; i < bits.size(); i++) { size_t i = 0;
for (; i + 4 <= num_blocks; i += 4) {
bits[i] |= a.bits[i];
bits[i + 1] |= a.bits[i + 1];
bits[i + 2] |= a.bits[i + 2];
bits[i + 3] |= a.bits[i + 3];
}
for (; i < num_blocks; i++) {
bits[i] |= a.bits[i]; bits[i] |= a.bits[i];
} }
} }
/// Bitwise AND. /// Bitwise AND.
bitfield operator&(const bitfield &a) const { bitfield operator&(bitfield a) const {
bitfield cr; a &= *this;
for (size_t i = 0; i < bits.size(); i++) { return a;
cr.bits[i] = bits[i] & a.bits[i];
}
return cr;
} }
/// Bitwise AND-equals. /// Bitwise AND-equals.
void operator&=(const bitfield &a) { void operator&=(const bitfield &a) {
for (size_t i = 0; i < bits.size(); i++) { size_t i = 0;
for (; i + 4 <= num_blocks; i += 4) {
bits[i] &= a.bits[i];
bits[i + 1] &= a.bits[i + 1];
bits[i + 2] &= a.bits[i + 2];
bits[i + 3] &= a.bits[i + 3];
}
for (; i < num_blocks; i++) {
bits[i] &= a.bits[i]; bits[i] &= a.bits[i];
} }
} }
/// Bitwise XOR. /// Bitwise XOR.
bitfield operator^(const bitfield &a) const { bitfield operator^(bitfield a) const {
bitfield cr; a ^= *this;
for (size_t i = 0; i < bits.size(); i++) { return a;
cr.bits[i] = bits[i] ^ a.bits[i];
}
return cr;
} }
/// Bitwise XOR-equals. /// Bitwise XOR-equals.
void operator^=(const bitfield &a) { void operator^=(bitfield a) {
for (size_t i = 0; i < bits.size(); i++) { size_t i = 0;
for (; i + 4 <= num_blocks; i += 4) {
bits[i] ^= a.bits[i];
bits[i + 1] ^= a.bits[i + 1];
bits[i + 2] ^= a.bits[i + 2];
bits[i + 3] ^= a.bits[i + 3];
}
for (; i < num_blocks; i++) {
bits[i] ^= a.bits[i]; bits[i] ^= a.bits[i];
} }
} }