]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
factor: tweak gcd2_odd performance
authorPaul Eggert <eggert@cs.ucla.edu>
Thu, 26 Sep 2024 00:50:09 +0000 (17:50 -0700)
committerPaul Eggert <eggert@cs.ucla.edu>
Sat, 28 Sep 2024 00:42:58 +0000 (17:42 -0700)
* src/factor.c (gcd2_odd): Use stdc_trailing_zeros here too.

src/factor.c

index 13fb92f8de4dd5d6f63bb40e0cb9b3725dae3cc7..081bb0d57cdf223144fb051501e90b3b1dd53c3d 100644 (file)
@@ -441,9 +441,12 @@ gcd2_odd (uintmax_t *r1, uintmax_t a1, uintmax_t a0, uintmax_t b1, uintmax_t b0)
       *r1 = b1;
       return b0;
     }
-
-  while ((a0 & 1) == 0)
-    rsh2 (a1, a0, a1, a0, 1);
+  if (!a0)
+    a0 = a1, a1 = 0;
+  assume (a0);
+  int ctz = stdc_trailing_zeros (a0);
+  if (ctz)
+    rsh2 (a1, a0, a1, a0, ctz);
 
   for (;;)
     {
@@ -456,16 +459,22 @@ gcd2_odd (uintmax_t *r1, uintmax_t a1, uintmax_t a0, uintmax_t b1, uintmax_t b0)
       if (gt2 (a1, a0, b1, b0))
         {
           sub_ddmmss (a1, a0, a1, a0, b1, b0);
-          do
-            rsh2 (a1, a0, a1, a0, 1);
-          while ((a0 & 1) == 0);
+          if (!a0)
+            a0 = a1, a1 = 0;
+          assume (a0);
+          ctz = stdc_trailing_zeros (a0);
+          if (ctz)
+            rsh2 (a1, a0, a1, a0, ctz);
         }
       else if (gt2 (b1, b0, a1, a0))
         {
           sub_ddmmss (b1, b0, b1, b0, a1, a0);
-          do
-            rsh2 (b1, b0, b1, b0, 1);
-          while ((b0 & 1) == 0);
+          if (!b0)
+            b0 = b1, b1 = 0;
+          assume (b0);
+          ctz = stdc_trailing_zeros (b0);
+          if (ctz)
+            rsh2 (b1, b0, b1, b0, ctz);
         }
       else
         break;