Use unsigned compare trick to check in bounds

This commit is contained in:
2024-06-26 19:41:25 -07:00
parent 08f2998a85
commit 789ecc29b3

View File

@@ -1675,6 +1675,12 @@ bool checkMaxBetweenExclusive(Node *n, int begin, int end,
}
}
// [begin, end) is now the half-open interval of children we're interested in.
const unsigned shiftUpperBound = end - begin;
const unsigned shiftAmount = begin;
auto inBounds = [&](unsigned c) { return c - shiftAmount < shiftUpperBound; };
switch (n->getType()) {
case Type_Node0: // GCOVR_EXCL_LINE
// We would have returned above, after not finding a child
@@ -1684,7 +1690,7 @@ bool checkMaxBetweenExclusive(Node *n, int begin, int end,
bool result = true;
for (int i = 0; i < 3; ++i) {
result &= !((self->children[i].childMaxVersion > readVersion) &
(begin <= self->index[i]) & (self->index[i] < end));
inBounds(self->index[i]));
}
return result;
} break;
@@ -1693,7 +1699,7 @@ bool checkMaxBetweenExclusive(Node *n, int begin, int end,
bool result = true;
for (int i = 0; i < 16; ++i) {
result &= !((self->children[i].childMaxVersion > readVersion) &
(begin <= self->index[i]) & (self->index[i] < end));
inBounds(self->index[i]));
}
return result;
} break;
@@ -1704,9 +1710,8 @@ bool checkMaxBetweenExclusive(Node *n, int begin, int end,
assume(self->numChildren <= Node48::kMaxNodes);
for (int i = 0; i < self->numChildren; ++i) {
assert(self->index[self->reverseIndex[i]] == i);
conflict[i] = (begin <= self->reverseIndex[i]) &
(self->reverseIndex[i] < end) &
(self->children[i].childMaxVersion > readVersion);
conflict[i] = (self->children[i].childMaxVersion > readVersion) &
inBounds(self->reverseIndex[i]);
}
bool result = true;
for (auto c : conflict) {
@@ -1721,8 +1726,8 @@ bool checkMaxBetweenExclusive(Node *n, int begin, int end,
bool result = true;
for (int i = 0; i < Node256::kMaxOfMaxPageSize; ++i) {
int j = (begin & ~(Node256::kMaxOfMaxPageSize - 1)) + i;
result &= !((self->children[j].childMaxVersion > readVersion) &
(begin <= j) & (j < end));
result &=
!((self->children[j].childMaxVersion > readVersion) & inBounds(j));
}
if (!result) {
return result;