Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

htable: use precalculated list of primes #3739

Merged
merged 2 commits into from
May 31, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 13 additions & 12 deletions main/htable.c
Original file line number Diff line number Diff line change
Expand Up @@ -258,24 +258,25 @@ static void hashTablePutItem00 (hashTable *htable, void *key, void *val
htable->count++;
}

/* TODO: A pre-calculated array can be used instead of
* finding a new one at runtume. */
/* "doubling" primes smaller than 2^32 */
static const unsigned int primes[] = {
3, 7, 17, 37, 79, 163, 331, 673, 1361, 2729, 5471, 10949, 21911,
43853, 87719, 175447, 350899, 701819, 1403641, 2807303, 5614657,
11229331, 22458671, 44917381, 89834777, 179669557, 359339171,
718678369, 1437356741, 2874713497,
};

static unsigned int
prime_double(unsigned int i)
{
unsigned int j;

Assert (i > 2);
Assert (i % 2);

for (unsigned int c = 2 * i + 1; ; c += 2)
{
for (unsigned int i0 = 3; i0 < i; i0 += 2)
{
if ((c % i0) == 0)
goto next;
}
return c;
next:;
}
for (j = 0; j < sizeof(primes) / sizeof(primes[0]); ++j)
if (primes[j] > i)
return primes[j];

return i;
}
Expand Down