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
* modification, are permitted provided that the following conditions are met:
@ -187,8 +187,15 @@ public:
size_t count() const {
static_assert(block_size == 64, "adjust popcount for block_type");
size_t sum = 0;
for (const auto &e : bits) {
sum += popcount64(e);
size_t i = 0;
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());
return sum;
@ -298,49 +305,61 @@ public:
}
/// Bitwise OR.
bitfield operator|(const bitfield &a) const {
bitfield cr;
for (size_t i = 0; i < bits.size(); i++) {
cr.bits[i] = bits[i] | a.bits[i];
}
return cr;
bitfield operator|(bitfield a) const {
a |= *this;
return a;
}
/// Bitwise OR-equals.
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];
}
}
/// Bitwise AND.
bitfield operator&(const bitfield &a) const {
bitfield cr;
for (size_t i = 0; i < bits.size(); i++) {
cr.bits[i] = bits[i] & a.bits[i];
}
return cr;
bitfield operator&(bitfield a) const {
a &= *this;
return a;
}
/// Bitwise AND-equals.
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];
}
}
/// Bitwise XOR.
bitfield operator^(const bitfield &a) const {
bitfield cr;
for (size_t i = 0; i < bits.size(); i++) {
cr.bits[i] = bits[i] ^ a.bits[i];
}
return cr;
bitfield operator^(bitfield a) const {
a ^= *this;
return a;
}
/// Bitwise XOR-equals.
void operator^=(const bitfield &a) {
for (size_t i = 0; i < bits.size(); i++) {
void operator^=(bitfield a) {
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];
}
}