__udiv_qrnnd_c

paul zimmermann Paul.Zimmermann at inria.fr
Mon Jul 18 07:59:15 UTC 2016


       Hello,

with the patch below, I get a small speedup on __udiv_qrnnd_c.

Best regards,
Paul Zimmermann

--- longlong.h.orig	2016-07-18 09:47:05.832658464 +0200
+++ longlong.h	2016-07-18 09:57:21.832816211 +0200
@@ -2064,7 +2064,7 @@
 /* Define this unconditionally, so it can be used for debugging.  */
 #define __udiv_qrnnd_c(q, r, n1, n0, d) \
   do {									\
-    UWtype __d1, __d0, __q1, __q0, __r1, __r0, __m;			\
+    UWtype __d1, __d0, __q1, __q0, __r1, __r0;                          \
 									\
     ASSERT ((d) != 0);							\
     ASSERT ((n1) < (d));						\
@@ -2074,29 +2074,25 @@
 									\
     __q1 = (n1) / __d1;							\
     __r1 = (n1) - __q1 * __d1;						\
-    __m = __q1 * __d0;							\
-    __r1 = __r1 * __ll_B | __ll_highpart (n0);				\
-    if (__r1 < __m)							\
+    __r0 = __r1 * __ll_B | __ll_highpart (n0);				\
+    __r1 = __r0 - __q1 * __d0;                                          \
+    if (__r1 > __r0) /* borrow when subtracting q1*d0 */                \
       {									\
 	__q1--, __r1 += (d);						\
-	if (__r1 >= (d)) /* i.e. we didn't get carry when adding to __r1 */\
-	  if (__r1 < __m)						\
-	    __q1--, __r1 += (d);					\
+	if (__r1 > (d)) /* no carry when adding d */                    \
+          __q1--, __r1 += (d);                                          \
       }									\
-    __r1 -= __m;							\
 									\
     __q0 = __r1 / __d1;							\
     __r0 = __r1  - __q0 * __d1;						\
-    __m = __q0 * __d0;							\
-    __r0 = __r0 * __ll_B | __ll_lowpart (n0);				\
-    if (__r0 < __m)							\
+    __r1 = __r0 * __ll_B | __ll_lowpart (n0);				\
+    __r0 = __r1 - __q0 * __d0;                                          \
+    if (__r0 > __r1) /* borrow when subtracting q0*d0 */                \
       {									\
 	__q0--, __r0 += (d);						\
-	if (__r0 >= (d))						\
-	  if (__r0 < __m)						\
-	    __q0--, __r0 += (d);					\
+	if (__r0 > (d)) /* no carry when adding d */                    \
+          __q0--, __r0 += (d);                                          \
       }									\
-    __r0 -= __m;							\
 									\
     (q) = __q1 * __ll_B | __q0;						\
     (r) = __r0;								\


More information about the gmp-devel mailing list