Skip to content

Commit 35e2adb

Browse files
committed
Allow for optimizing out bound check on 64 bit systems
This re-wording of the previous snippet hints to the compiler more directly, that this condition can be dropped if the type of size_t is narrower than the one used for OEISprimes (uint64_t). This optimization applies for 32 bit systems.
1 parent 523600b commit 35e2adb

1 file changed

Lines changed: 9 additions & 2 deletions

File tree

Hashtable.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,16 @@ static const uint64_t OEISprimes[] = {
100100

101101
static size_t nextPrime(size_t n) {
102102
/* on 32-bit make sure we do not return primes not fitting in size_t */
103-
for (size_t i = 0; i < ARRAYSIZE(OEISprimes) && OEISprimes[i] < SIZE_MAX; i++) {
104-
if (n <= OEISprimes[i])
103+
for (size_t i = 0; i < ARRAYSIZE(OEISprimes); i++) {
104+
#if SIZE_MAX < UINT64_MAX
105+
if (OEISprimes[i] > SIZE_MAX) {
106+
break;
107+
}
108+
#endif
109+
110+
if (n <= OEISprimes[i]) {
105111
return OEISprimes[i];
112+
}
106113
}
107114

108115
CRT_fatalError("Hashtable: no prime found");

0 commit comments

Comments
 (0)