[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH v5 05/10] src/: ceil_prime(): Add function to get the lowest prim
From: |
Alejandro Colomar |
Subject: |
[PATCH v5 05/10] src/: ceil_prime(): Add function to get the lowest prime not less than n |
Date: |
Sat, 16 Mar 2024 13:34:53 +0100 |
And use it where the same logic was being open-coded.
While at it, fix the logic, which was incorrect in the open-coded call
sites, since for an input of 1, it produced 3, but the first prime is 2.
A recent commit started rejecting 1 earlier, so this bug was now
impossible to trigger, but remained there.
Also, since this is a library function, let's behave well for an input
of 0, which is mathematically fine, and return also the first prime, 2.
Fixes: 4c7a3396375b ("[libbib, libgroff, indxbib]: Slightly refactor.")
Link: <https://savannah.gnu.org/bugs/?65452>
Link: <https://lists.gnu.org/archive/html/groff/2024-03/msg00065.html>
Cc: "G. Branden Robinson" <branden@debian.org>
Cc: Dave Kemper <saint.snit@gmail.com>
Cc: "James K. Lowden" <jklowden@schemamania.org>
Cc: Colin Watson <cjwatson@debian.org>
Cc: Werner LEMBERG <wl@gnu.org>
Cc: James Clark <jjc@jclark.com>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
---
src/include/lib.h | 2 +-
src/libs/libbib/index.cpp | 4 +---
src/libs/libgroff/prime.cpp | 18 +++++++++++++++++-
src/utils/indxbib/indxbib.cpp | 6 +-----
4 files changed, 20 insertions(+), 10 deletions(-)
diff --git a/src/include/lib.h b/src/include/lib.h
index 0f4ed458e..b6c98caee 100644
--- a/src/include/lib.h
+++ b/src/include/lib.h
@@ -56,7 +56,7 @@ extern "C" {
#include <stdbool.h>
char *strsave(const char *s);
-bool is_prime(unsigned);
+unsigned ceil_prime(unsigned);
double groff_hypot(double, double);
#include <stdio.h>
diff --git a/src/libs/libbib/index.cpp b/src/libs/libbib/index.cpp
index 18701a157..53088397a 100644
--- a/src/libs/libbib/index.cpp
+++ b/src/libs/libbib/index.cpp
@@ -611,9 +611,7 @@ void index_search_item::read_common_words_file()
error("can't open '%1': %2", common_words_file, strerror(errno));
return;
}
- common_words_table_size = 2*header.common + 1;
- while (!is_prime(common_words_table_size))
- common_words_table_size += 2;
+ common_words_table_size = ceil_prime(2 * header.common);
common_words_table = new char *[common_words_table_size];
for (int i = 0; i < common_words_table_size; i++)
common_words_table[i] = 0;
diff --git a/src/libs/libgroff/prime.cpp b/src/libs/libgroff/prime.cpp
index b1e73470a..485f7b95d 100644
--- a/src/libs/libgroff/prime.cpp
+++ b/src/libs/libgroff/prime.cpp
@@ -22,7 +22,23 @@ internet at <http://www.gnu.org/licenses/gpl-2.0.txt>. */
#include <assert.h>
#include <math.h>
-bool is_prime(unsigned n)
+static bool is_prime(unsigned);
+
+unsigned ceil_prime(unsigned n)
+{
+ if (n <= 2)
+ return 2;
+
+ if (n % 2 == 0)
+ n++;
+
+ while (!is_prime(n))
+ n += 2;
+
+ return n;
+}
+
+static bool is_prime(unsigned n)
{
assert(n > 1);
if (n <= 3)
diff --git a/src/utils/indxbib/indxbib.cpp b/src/utils/indxbib/indxbib.cpp
index a3ac8eb6e..b15bd8187 100644
--- a/src/utils/indxbib/indxbib.cpp
+++ b/src/utils/indxbib/indxbib.cpp
@@ -148,11 +148,7 @@ int main(int argc, char **argv)
{
int requested_hash_table_size;
check_integer_arg('h', optarg, 2, &requested_hash_table_size);
- hash_table_size = requested_hash_table_size;
- if ((hash_table_size > 2) && (hash_table_size % 2) == 0)
- hash_table_size++;
- while (!is_prime(hash_table_size))
- hash_table_size += 2;
+ hash_table_size = ceil_prime(requested_hash_table_size);
if (hash_table_size != requested_hash_table_size)
warning("requested hash table size %1 is not prime: using %2"
" instead", optarg, hash_table_size);
--
2.43.0
signature.asc
Description: PGP signature
- [PATCH v4 07/10] [indxbib]: Clear errno before calling strtol(3), (continued)
- [PATCH v4 08/10] [indxbib]: Remove dead code, Alejandro Colomar, 2024/03/16
- [PATCH v4 09/10] [indxbib]: Collapse related tests, Alejandro Colomar, 2024/03/16
- [PATCH v5 00/10] strtol(3)-related fixes, Alejandro Colomar, 2024/03/16
- [PATCH v5 01/10] [libgroff]: Remove redundant checks., Alejandro Colomar, 2024/03/16
- [PATCH v5 02/10] [libgroff]: Remove dead code, Alejandro Colomar, 2024/03/16
- [PATCH v5 03/10] src/: Remove redundant checks after strtol(3)., Alejandro Colomar, 2024/03/16
- [PATCH v5 04/10] [grolbp]: Remove bogus (and redundant) check, Alejandro Colomar, 2024/03/16
- [PATCH v5 05/10] src/: ceil_prime(): Add function to get the lowest prime not less than n,
Alejandro Colomar <=
- [PATCH v5 06/10] [indxbib]: Don't else after [[noreturn]], Alejandro Colomar, 2024/03/16
- [PATCH v5 07/10] [indxbib]: Clear errno before calling strtol(3), Alejandro Colomar, 2024/03/16
- [PATCH v5 08/10] [indxbib]: Remove dead code, Alejandro Colomar, 2024/03/16
- [PATCH v5 09/10] [indxbib]: Collapse related tests, Alejandro Colomar, 2024/03/16
- [PATCH v5 10/10] [grolbp]: Fix range check after strtol(3), Alejandro Colomar, 2024/03/16
[PATCH v3 5/9] src/: ceil_prime(): Add function to get the lowest prime not smaller than n, Alejandro Colomar, 2024/03/15
[PATCH v3 1/9] [libgroff]: Remove redundant checks., Alejandro Colomar, 2024/03/15
[PATCH v3 7/9] [indxbib]: Clear errno before calling strtol(3), Alejandro Colomar, 2024/03/15
[PATCH v3 8/9] [indxbib]: Remove dead code, Alejandro Colomar, 2024/03/15
[PATCH v3 6/9] [indxbib]: Don't else after [[noreturn]], Alejandro Colomar, 2024/03/15