]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
factor: prefer uuint to two words in a couple of places
authorPaul Eggert <eggert@cs.ucla.edu>
Sun, 1 Jun 2025 23:28:47 +0000 (16:28 -0700)
committerPaul Eggert <eggert@cs.ucla.edu>
Thu, 10 Jul 2025 00:12:39 +0000 (17:12 -0700)
This simplifies things slightly by using uuint for
some two-word integers.
* src/factor.c (strtouuint): Accept uuint *, not two mp_limb_t *.
All callers changed.
(print_factors_single): Accept uuint, not two limbs.
All callers changed.
(print_factors): Use simpler test for high bit,
one that need not worry about promoting to int.

src/factor.c

index 1b4a4463303570c70438f57cf9dfab6f19a24f1d..d89fecc833558f4bc6f60bb1ab8ca6187914d49e 100644 (file)
@@ -1796,7 +1796,7 @@ mp_factor (mpz_t t, struct mp_factors *factors)
 }
 
 static strtol_error
-strtouuint (mp_limb_t *hip, mp_limb_t *lop, char const *s)
+strtouuint (uuint *u, char const *s)
 {
   int lo_carry;
   mp_limb_t hi = 0, lo = 0;
@@ -1846,9 +1846,7 @@ strtouuint (mp_limb_t *hip, mp_limb_t *lop, char const *s)
         }
     }
 
-  *hip = hi;
-  *lop = lo;
-
+  *u = make_uuint (hi, lo);
   return err;
 }
 
@@ -2028,14 +2026,14 @@ lbuf_putmpz (mpz_t const i)
 
 /* Single-precision factoring */
 static void
-print_factors_single (mp_limb_t t1, mp_limb_t t0)
+print_factors_single (uuint t)
 {
   struct factors factors;
 
-  print_uuint (make_uuint (t1, t0));
+  print_uuint (t);
   lbuf_putc (':');
 
-  factor (t1, t0, &factors);
+  factor (hi (t), lo (t), &factors);
 
   for (int j = 0; j < factors.nfactors; j++)
     for (int k = 0; k < factors.e[j]; k++)
@@ -2073,21 +2071,20 @@ print_factors (char const *input)
     str++;
   str += *str == '+';
 
-  mp_limb_t t1, t0;
-
   /* Try converting the number to one or two words.  If it fails, use GMP or
      print an error message.  The 2nd condition checks that the most
      significant bit of the two-word number is clear, in a typesize neutral
      way.  */
-  strtol_error err = strtouuint (&t1, &t0, str);
+  uuint u;
+  strtol_error err = strtouuint (&u, str);
 
   switch (err)
     {
     case LONGINT_OK:
-      if (((t1 << 1) >> 1) == t1)
+      if (hi (u) >> (W_TYPE_SIZE - 1) == 0)
         {
           devmsg ("[using single-precision arithmetic] ");
-          print_factors_single (t1, t0);
+          print_factors_single (u);
           return true;
         }
       FALLTHROUGH;