diff --git a/src/util/bitfield.h b/src/util/bitfield.h index 208c2ef5..a71c1f88 100644 --- a/src/util/bitfield.h +++ b/src/util/bitfield.h @@ -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]; } }