Skip to content

Commit 4acef15

Browse files
committed
Remove all architecture-dependent conditional branching in nextPrime
This shortens the list of available primes to what is available on different system architectures based on the maximum amount of memory addressable for the program using pointers.
1 parent 523600b commit 4acef15

1 file changed

Lines changed: 11 additions & 5 deletions

File tree

Hashtable.c

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,18 +91,24 @@ size_t Hashtable_count(const Hashtable* this) {
9191
/* https://oeis.org/A014234 */
9292
static const uint64_t OEISprimes[] = {
9393
7, 13, 31, 61, 127, 251, 509, 1021, 2039, 4093, 8191,
94-
16381, 32749, 65521, 131071, 262139, 524287, 1048573,
94+
16381, 32749, 65521,
95+
#if SIZE_MAX > UINT16_MAX
96+
131071, 262139, 524287, 1048573,
9597
2097143, 4194301, 8388593, 16777213, 33554393,
9698
67108859, 134217689, 268435399, 536870909, 1073741789,
97-
2147483647, 4294967291, 8589934583, 17179869143,
98-
34359738337, 68719476731, 137438953447
99+
2147483647, 4294967291,
100+
#endif
101+
#if SIZE_MAX > UINT32_MAX
102+
8589934583, 17179869143, 34359738337, 68719476731, 137438953447,
103+
#endif
99104
};
100105

101106
static size_t nextPrime(size_t n) {
102107
/* 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])
108+
for (size_t i = 0; i < ARRAYSIZE(OEISprimes); i++) {
109+
if (n <= OEISprimes[i]) {
105110
return OEISprimes[i];
111+
}
106112
}
107113

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

0 commit comments

Comments
 (0)