Improve bitset_at version by avoiding a redundant variable shift.
This commit is contained in:
1 parent
96178194b3
commit
1753ddc4d6
1 file changed
+5
-5
+5
-5
@@ -145,11 +145,11 @@ bitset_set_at(bitset_t v, size_t i, bool x)
|
|||||||
{
|
{
|
||||||
BITSETI_CONTRACT(v);
|
BITSETI_CONTRACT(v);
|
||||||
assert (i < v->size && v->ptr != NULL);
|
assert (i < v->size && v->ptr != NULL);
|
||||||
size_t offset = i / BITSET_LIMB_BIT;
|
const size_t offset = i / BITSET_LIMB_BIT;
|
||||||
size_t index = 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.
|
// This is a branchless version as x can only be 0 or 1 with only one variable shift.
|
||||||
v->ptr[offset] &= ~(((bitset_limb)1)<<index);
|
const size_t mask = (bitset_limb)1<<index;
|
||||||
v->ptr[offset] |= ((bitset_limb)x)<<index;
|
v->ptr[offset] = (v->ptr[offset] & ~mask) | (mask & (-(bitset_limb)x));
|
||||||
BITSETI_CONTRACT (v);
|
BITSETI_CONTRACT (v);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user