[Gmp-commit] /var/hg/gmp: Improve method 4 and 5 by using the division free m...

mercurial at gmplib.org mercurial at gmplib.org
Mon Sep 23 14:55:22 UTC 2019


details:   /var/hg/gmp/rev/882413828d04
changeset: 17922:882413828d04
user:      Torbjorn Granlund <tg at gmplib.org>
date:      Mon Sep 23 16:55:18 2019 +0200
description:
Improve method 4 and 5 by using the division free methods optimistically, detecting errors.  Tweak table values.

diffstat:

 mpn/generic/hgcd2.c |  295 ++++++++++++++++++++++++++++-----------------------
 1 files changed, 159 insertions(+), 136 deletions(-)

diffs (truncated from 374 to 300 lines):

diff -r bb72edd77eaa -r 882413828d04 mpn/generic/hgcd2.c
--- a/mpn/generic/hgcd2.c	Mon Sep 23 00:20:59 2019 +0200
+++ b/mpn/generic/hgcd2.c	Mon Sep 23 16:55:18 2019 +0200
@@ -145,94 +145,117 @@
 
 /* Table quotients.  We extract the NBITS most significant bits of the
    numerator limb, and the corresponding bits from the divisor limb, and use
-   these to form an index into the table.  We only get NBITS/2 bits of
-   progress, and the table is very large.  The hit rate for the table with
-   NBITS = 6 is about 80%, meaning we get a lot of poorly predicated branches.
+   these to form an index into the table.  This method is probably only useful
+   for short pipelines with slow multiplication.
 
    Possible improvements:
 
-   * Don't identify the failure case early, by looking at the extracted divisor
-     bits.  Instead do the table thing, and only invoke the slow path if the
-     remainder is off after one conditional adjustment.  (This will require a
-     table where the 0 entries are properly filled in.)
-
    * Perhaps extract the highest NBITS of the divisor instead of the same bits
      as from the numerator.  That would require another count_leading_zeros,
      and a post-multiply shift of the quotient.
 
-   * Compress the table.  Its values are tiny now, and there are lots of zero
+   * Compress tables?  Their values are tiny, and there are lots of zero
      entries (which are never used).
 
    * Round the table entries more cleverly?
 */
 
-#define NBITS 6		/* Must be even. */
-static const unsigned char tab[1 << (2 * NBITS - 1)] = {
-0,0,0,0,0,0,0,0,3,3,2,2,2,2,2,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
-0,0,0,0,0,0,0,0,0,0,0,0,0,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1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0
+#ifndef NBITS
+#define NBITS 5
+#endif
+
+#if NBITS == 5
+/* This needs full division about 13.2% of the time. */
+static const unsigned char tab[512] = {
+17, 9, 5,4,3,2,2,2,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
+18, 9, 6,4,3,2,2,2,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
+19,10, 6,4,3,3,2,2,2,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,
+20,10, 6,5,3,3,2,2,2,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,
+21,11, 7,5,4,3,2,2,2,2,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,
+22,11, 7,5,4,3,3,2,2,2,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,
+23,12, 7,5,4,3,3,2,2,2,2,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,
+24,12, 8,6,4,3,3,2,2,2,2,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,
+25,13, 8,6,5,4,3,3,2,2,2,2,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,
+26,13, 8,6,5,4,3,3,2,2,2,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,
+27,14, 9,6,5,4,3,3,2,2,2,2,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,
+28,14, 9,7,5,4,3,3,3,2,2,2,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,
+29,15,10,7,5,4,4,3,3,2,2,2,2,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,
+30,15,10,7,6,5,4,3,3,2,2,2,2,2,1,1,1,1,1,1,1,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,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,
+61,31,21,15,12,10,8,7,6,6,5,5,4,4,4,3,3,3,3,3,2,2,2,2,2,2,2,2,2,2,1,1,
+ 1, 1, 1, 1, 1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,
+62,31,22,16,12,10,9,7,6,6,5,5,4,4,4,3,3,3,3,3,2,2,2,2,2,2,2,2,2,2,1,1,
+ 1, 1, 1, 1, 1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,
+63,32,22,16,13,10,9,7,7,6,5,5,4,4,4,3,3,3,3,3,2,2,2,2,2,2,2,2,2,2,2,1,
+ 1, 1, 1, 1, 1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,
+64,32,22,16,13,10,9,8,7,6,5,5,4,4,4,3,3,3,3,3,3,2,2,2,2,2,2,2,2,2,2,1,
+ 1, 1, 1, 1, 1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
+};
+#else
+#error No table for provided NBITS
+#endif
+
 static const unsigned char *tabp = tab - (1 << (NBITS - 1) << NBITS);
 
 mp_double_limb_t
@@ -242,116 +265,116 @@
   size_t nbi, dbi;
   mp_limb_t q0;
   mp_limb_t r0;
+  mp_limb_t mask;
   mp_double_limb_t res;
 
+  ASSERT (n0 >= d0);		/* Actually only msb position is critical. */
+
   count_leading_zeros (ncnt, n0);
-  nbi = n0 << ncnt >> (64 - NBITS);
-  dbi = d0 << ncnt >> (64 - NBITS);
+  nbi = n0 << ncnt >> (GMP_LIMB_BITS - NBITS);
+  dbi = d0 << ncnt >> (GMP_LIMB_BITS - NBITS);
 
-  if (UNLIKELY (dbi < (1 << NBITS/2)))
+  q0 = tabp[(nbi << NBITS) + dbi];
+  r0 = n0 - q0 * d0;
+  mask = -(mp_limb_t) (r0 >= d0);
+  q0 -= mask;
+  r0 -= d0 & mask;
+
+  if (UNLIKELY (r0 >= d0))
     {
       q0 = n0 / d0;
       r0 = n0 - q0 * d0;
     }
-  else
-    {
-      mp_limb_t mask;
-      q0 = tabp[(nbi << NBITS) + dbi];
-      r0 = n0 - q0 * d0;
-      mask = -(mp_limb_t) (r0 >= d0);
-      q0 -= mask;
-      r0 -= d0 & mask;
-    }
 
   res.d1 = q0;
   res.d0 = r0;
   return res;
 }
 
-
 #elif HGCD2_DIV1_METHOD == 5
 
 /* Table inverses of divisors.  We don't bother with suppressing the msb from
    the tables.  We index with the NBITS most significant divisor bits,
    including the always-set highest bit, but use addressing trickery via tabp
-   to suppress it.  */
+   to suppress it.
+
+   Possible improvements:
+
+   * Do first multiply using 32-bit operations on 64-bit computers.  At least
+     on most Arm64 cores, that uses 3 times less resources.  It also saves on
+     many x86-64 processors.
+*/
 
 #ifndef NBITS
-#define NBITS 8
+#define NBITS 7
 #endif
 
-#if NBITS == 7
+#if NBITS == 5
+/* This needs full division about 1.63% of the time. */
+static const unsigned char tab[16] = {
+ 63, 59, 55, 52, 50, 47, 45, 43, 41, 39, 38, 36, 35, 34, 33, 32
+};
+static const unsigned char *tabp = tab - (1 << (NBITS - 1));
+#elif NBITS == 6
+/* This needs full division about 0.93% of the time. */
+static const unsigned char tab[32] = {
+127,123,119,116,112,109,106,104,101, 98, 96, 94, 92, 90, 88, 86,
+ 84, 82, 80, 79, 77, 76, 74, 73, 72, 70, 69, 68, 67, 66, 65, 64
+};
+static const unsigned char *tabp = tab - (1 << (NBITS - 1));
+#elif NBITS == 7
+/* This needs full division about 0.49% of the time. */
 static const unsigned char tab[64] = {
-252,248,244,240,236,233,230,226,223,220,217,214,211,208,206,203,
-200,198,195,193,191,188,186,184,182,180,178,176,174,172,170,168,
-166,164,163,161,159,158,156,155,153,152,150,149,147,146,144,143,
-142,140,139,138,137,136,134,133,132,131,130,129,128,127,126,125
+255,251,247,243,239,236,233,229,226,223,220,217,214,211,209,206,
+203,201,198,196,194,191,189,187,185,183,181,179,177,175,173,171,
+169,167,166,164,162,161,159,158,156,155,153,152,150,149,147,146,
+145,143,142,141,140,139,137,136,135,134,133,132,131,130,129,128
 };
 static const unsigned char *tabp = tab - (1 << (NBITS - 1));
 #elif NBITS == 8
+/* This needs full division about 0.26% of the time. */
 static const unsigned short tab[128] = {
-508,504,500,496,492,488,485,481,477,474,470,467,464,460,457,454,
-451,447,444,441,438,435,432,430,427,424,421,418,416,413,410,408,
-405,403,400,398,395,393,390,388,386,383,381,379,377,374,372,370,
-368,366,364,362,360,358,356,354,352,350,348,346,344,342,340,339,
-337,335,333,332,330,328,326,325,323,322,320,318,317,315,314,312,
-311,309,308,306,305,303,302,300,299,298,296,295,293,292,291,289,
-288,287,285,284,283,282,280,279,278,277,276,274,273,272,271,270,
-269,267,266,265,264,263,262,261,260,259,258,257,256,255,254,253
-};
-static const unsigned short *tabp = tab - (1 << (NBITS - 1));
-#elif NBITS == 9


More information about the gmp-commit mailing list