diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 09:51:24 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 09:51:24 +0000 |
commit | f7548d6d28c313cf80e6f3ef89aed16a19815df1 (patch) | |
tree | a3f6f2a3f247293bee59ecd28e8cd8ceb6ca064a /src/lib/primes.c | |
parent | Initial commit. (diff) | |
download | dovecot-f7548d6d28c313cf80e6f3ef89aed16a19815df1.tar.xz dovecot-f7548d6d28c313cf80e6f3ef89aed16a19815df1.zip |
Adding upstream version 1:2.3.19.1+dfsg1.upstream/1%2.3.19.1+dfsg1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/lib/primes.c')
-rw-r--r-- | src/lib/primes.c | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/src/lib/primes.c b/src/lib/primes.c new file mode 100644 index 0000000..cb4a5e2 --- /dev/null +++ b/src/lib/primes.c @@ -0,0 +1,48 @@ +/* Copyright (c) 2013-2018 Dovecot authors, see the included COPYING file */ + +#include "lib.h" +#include "primes.h" + +static const unsigned int primes[] = { +#define PRIME_SKIP_COUNT 3 + 17, + 37, + 67, + 131, + 257, /* next from 2^8 */ + 521, + 1031, + 2053, + 4099, + 8209, + 16411, + 32771, + 65537, /* next from 2^16 */ + 131101, + 262147, + 524309, + 1048583, + 2097169, + 4194319, + 8388617, + 16777259, /* next from 2^24 */ + 33554467, + 67108879, + 134217757, + 268435459, + 536870923, + 1073741827, + 2147483659U, + 4294967291U /* previous from 2^32 */ +}; + +unsigned int primes_closest(unsigned int num) +{ + unsigned int i; + + for (i = 31; i > PRIME_SKIP_COUNT; i--) { + if ((num & (1U << i)) != 0) + return primes[i - PRIME_SKIP_COUNT]; + } + return primes[0]; +} |