]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
factor: always use Baillie-PSW
authorPaul Eggert <eggert@cs.ucla.edu>
Sun, 15 Jun 2025 06:10:25 +0000 (23:10 -0700)
committerPaul Eggert <eggert@cs.ucla.edu>
Thu, 10 Jul 2025 00:12:39 +0000 (17:12 -0700)
* src/factor.c (USE_BAILLIE_PSW): New constant.
(prime_p, prime2_p): Use it, i.e., always use Baillie-PSW.
Do so by using mp_prime_p.  Do not tell GCC these functions
are pure; the pure mark was present only to pacify GCC
and is no longer needed now that thes functions call mp_prime_p.

src/factor.c

index 0542c0e6f1550b2dcea5428f8c258f6a43e575a9..82b283f6a211c13bf0238176bf00e0b193f0243f 100644 (file)
@@ -1261,9 +1261,18 @@ mp_millerrabin (mpz_srcptr n, mpz_srcptr nm1, mpz_ptr x, mpz_ptr y,
   return false;
 }
 
-/* Lucas' prime test.  The number of iterations vary greatly, up to a few dozen
-   have been observed.  The average seem to be about 2.  */
-static bool ATTRIBUTE_PURE
+static bool mp_prime_p (mpz_t);
+
+/* Use the Baillie-PSW primality test for all integers.
+   Code that is currently never executed because it is protected by
+   the equivalent of "if (!USE_BAILLIE_PSW)" dates back to when
+   factor.c used an inferior primality test.
+   FIXME: Either remove the !USE_BAILLIE_PSW code, or fix it to be
+   more efficient than the USE_BAILLIE_PSW code, by special casing
+   Baillie-PSW for single- and/or double-word args.  */
+enum { USE_BAILLIE_PSW = true };
+
+static bool
 prime_p (mp_limb_t n)
 {
   mp_limb_t a_prim, one, ni;
@@ -1276,6 +1285,12 @@ prime_p (mp_limb_t n)
   if (n < (mp_limb_t) FIRST_OMITTED_PRIME * FIRST_OMITTED_PRIME)
     return true;
 
+  if (USE_BAILLIE_PSW)
+    {
+      mpz_t mn = MPZ_ROINIT_N (&n, 1);
+      return mp_prime_p (mn);
+    }
+
   /* Precomputation for Miller-Rabin.  */
   int k = stdc_trailing_zeros (n - 1);
   mp_limb_t q = (n - 1) >> k;
@@ -1344,7 +1359,7 @@ prime_p (mp_limb_t n)
     }
 }
 
-static bool ATTRIBUTE_PURE
+static bool
 prime2_p (mp_limb_t n1, mp_limb_t n0)
 {
   mp_limb_t q[2], nm1[2];
@@ -1358,6 +1373,13 @@ prime2_p (mp_limb_t n1, mp_limb_t n0)
   if (n1 == 0)
     return prime_p (n0);
 
+  if (USE_BAILLIE_PSW)
+    {
+      uuint un = make_uuint (n1, n0);
+      mpz_t mn = MPZ_ROINIT_N (un.uu, 2);
+      return mp_prime_p (mn);
+    }
+
   nm1[1] = n1 - (n0 == 0);
   nm1[0] = n0 - 1;
   if (nm1[0] == 0)