]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
factor: tweak prime_p zero counting
authorPaul Eggert <eggert@cs.ucla.edu>
Thu, 26 Sep 2024 00:55:00 +0000 (17:55 -0700)
committerPaul Eggert <eggert@cs.ucla.edu>
Sat, 28 Sep 2024 00:42:58 +0000 (17:42 -0700)
* src/factor.c (prime_p): Use stdc_trailing_zero here too.
This doesn’t make much of a performance difference but
we might as well be consistent.

src/factor.c

index 081bb0d57cdf223144fb051501e90b3b1dd53c3d..80757c083705df4022185d2106d02e69503372ad 100644 (file)
@@ -1166,7 +1166,6 @@ mp_millerrabin (mpz_srcptr n, mpz_srcptr nm1, mpz_ptr x, mpz_ptr y,
 static bool ATTRIBUTE_PURE
 prime_p (uintmax_t n)
 {
-  mp_bitcnt_t k;
   bool is_prime;
   uintmax_t a_prim, one, ni;
   struct factors factors;
@@ -1179,9 +1178,8 @@ prime_p (uintmax_t n)
     return true;
 
   /* Precomputation for Miller-Rabin.  */
-  uintmax_t q = n - 1;
-  for (k = 0; (q & 1) == 0; k++)
-    q >>= 1;
+  int k = stdc_trailing_zeros (n - 1);
+  uintmax_t q = (n - 1) >> k;
 
   uintmax_t a = 2;
   binv (ni, n);                 /* ni <- 1/n mod B */