Branchless version of _set_at

This commit is contained in:
Patrick Pelissier committed 2017-08-14 19:19:23 +02:00
1 parent f539cd877f
commit 0e232d146b
1 file changed
+6 -5
+6 -5
View File
@@ -149,13 +149,14 @@ bitset_set_at(bitset_t v, size_t i, bool x)
{
BITSETI_CONTRACT(v);
assert (i < v->size && v->ptr != NULL);
M_ASSUME (x == false || x == true);
size_t offset = i / BITSET_LIMB_BIT;
size_t index = i % BITSET_LIMB_BIT;
if (x == true){
v->ptr[offset] |= 1U<<index;
} else {
v->ptr[offset] &= ~(1U<<index);
}
// This is a branchless version as x can only be 0 or 1.
v->ptr[offset] &= ~(1U<<index);
v->ptr[offset] |= x<<index;
BITSETI_CONTRACT (v);
}