Improve bitset_at version by avoiding a redundant variable shift.

This commit is contained in:
Patrick Pelissier committed 2018-02-16 20:42:42 +01:00
1 parent 96178194b3
commit 1753ddc4d6
1 file changed
+5 -5
+5 -5
View File
@@ -145,11 +145,11 @@ bitset_set_at(bitset_t v, size_t i, bool x)
{
BITSETI_CONTRACT(v);
assert (i < v->size && v->ptr != NULL);
size_t offset = i / BITSET_LIMB_BIT;
size_t index = i % BITSET_LIMB_BIT;
// This is a branchless version as x can only be 0 or 1.
v->ptr[offset] &= ~(((bitset_limb)1)<<index);
v->ptr[offset] |= ((bitset_limb)x)<<index;
const size_t offset = i / BITSET_LIMB_BIT;
const size_t index = i % BITSET_LIMB_BIT;
// This is a branchless version as x can only be 0 or 1 with only one variable shift.
const size_t mask = (bitset_limb)1<<index;
v->ptr[offset] = (v->ptr[offset] & ~mask) | (mask & (-(bitset_limb)x));
BITSETI_CONTRACT (v);
}