[Gmp-commit] /var/hg/gmp: 2 new changesets

mercurial at gmplib.org mercurial at gmplib.org
Sun May 29 16:17:52 CEST 2022


details:   /var/hg/gmp/rev/c2299aa93a66
changeset: 18359:c2299aa93a66
user:      Marco Bodrato <bodrato at mail.dm.unipi.it>
date:      Sun May 29 16:13:56 2022 +0200
description:
tests/mpz/t-aorsmul.c (check_sqr): New function to test mpz_aorsmul (w,x,x)

details:   /var/hg/gmp/rev/61ef108d740c
changeset: 18360:61ef108d740c
user:      Marco Bodrato <bodrato at mail.dm.unipi.it>
date:      Sun May 29 16:16:47 2022 +0200
description:
mpz/aorsmul.c: New shortcuts for the case mpz_aorsmul(w,x,x). Tx: Fredrik Johansson

diffstat:

 mpz/aorsmul.c         |  26 +++++++++++++++++++++-----
 tests/mpz/t-aorsmul.c |  47 +++++++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 66 insertions(+), 7 deletions(-)

diffs (135 lines):

diff -r 0da884259303 -r 61ef108d740c mpz/aorsmul.c
--- a/mpz/aorsmul.c	Sun May 29 14:54:08 2022 +0200
+++ b/mpz/aorsmul.c	Sun May 29 16:16:47 2022 +0200
@@ -1,6 +1,6 @@
 /* mpz_addmul, mpz_submul -- add or subtract multiple.
 
-Copyright 2001, 2004, 2005, 2012 Free Software Foundation, Inc.
+Copyright 2001, 2004, 2005, 2012, 2022 Free Software Foundation, Inc.
 
 This file is part of the GNU MP Library.
 
@@ -52,7 +52,7 @@
 {
   mp_size_t  xsize, ysize, tsize, wsize, wsize_signed;
   mp_ptr     wp, tp;
-  mp_limb_t  c, high;
+  mp_limb_t  c;
   TMP_DECL;
 
   /* w unaffected if x==0 or y==0 */
@@ -90,9 +90,16 @@
 
   if (wsize_signed == 0)
     {
+      mp_limb_t  high;
       /* Nothing to add to, just set w=x*y.  No w==x or w==y overlap here,
 	 since we know x,y!=0 but w==0.  */
-      high = mpn_mul (wp, PTR(x),xsize, PTR(y),ysize);
+      if (x == y)
+	{
+	  mpn_sqr (wp, PTR(x),xsize);
+	  high = wp[tsize-1];
+	}
+      else
+	high = mpn_mul (wp, PTR(x),xsize, PTR(y),ysize);
       tsize -= (high == 0);
       SIZ(w) = (sub >= 0 ? tsize : -tsize);
       return;
@@ -101,8 +108,17 @@
   TMP_MARK;
   tp = TMP_ALLOC_LIMBS (tsize);
 
-  high = mpn_mul (tp, PTR(x),xsize, PTR(y),ysize);
-  tsize -= (high == 0);
+  if (x == y)
+    {
+      mpn_sqr (tp, PTR(x),xsize);
+      tsize -= (tp[tsize-1] == 0);
+    }
+  else
+    {
+      mp_limb_t high;
+      high = mpn_mul (tp, PTR(x),xsize, PTR(y),ysize);
+      tsize -= (high == 0);
+    }
   ASSERT (tp[tsize-1] != 0);
   if (sub >= 0)
     {
diff -r 0da884259303 -r 61ef108d740c tests/mpz/t-aorsmul.c
--- a/tests/mpz/t-aorsmul.c	Sun May 29 14:54:08 2022 +0200
+++ b/tests/mpz/t-aorsmul.c	Sun May 29 16:16:47 2022 +0200
@@ -1,6 +1,6 @@
 /* Test mpz_addmul, mpz_addmul_ui, mpz_submul, mpz_submul_ui.
 
-Copyright 2001, 2002 Free Software Foundation, Inc.
+Copyright 2001, 2002, 2022 Free Software Foundation, Inc.
 
 This file is part of the GNU MP Library test suite.
 
@@ -155,7 +155,8 @@
       abort ();
     }
 
-  mpz_mul (want, x, y);
+
+  mpz_sub (want, want, w);
   mpz_sub (want, w, want);
   mpz_set (got, w);
   mpz_submul (got, x, y);
@@ -171,6 +172,45 @@
 }
 
 void
+check_sqr (mpz_srcptr w, mpz_srcptr x)
+{
+  mpz_t  want, got;
+
+  mpz_init (want);
+  mpz_init (got);
+
+  mpz_mul (want, x, x);
+  mpz_add (want, w, want);
+  mpz_set (got, w);
+  mpz_addmul (got, x, x);
+  MPZ_CHECK_FORMAT (got);
+  if (mpz_cmp (want, got) != 0)
+    {
+      printf ("mpz_addmul xx fail\n");
+    sqrfail:
+      mpz_trace ("w", w);
+      mpz_trace ("x", x);
+      mpz_trace ("want", want);
+      mpz_trace ("got ", got);
+      abort ();
+    }
+
+  mpz_sub (want, want, w);
+  mpz_sub (want, w, want);
+  mpz_set (got, w);
+  mpz_submul (got, x, x);
+  MPZ_CHECK_FORMAT (got);
+  if (mpz_cmp (want, got) != 0)
+    {
+      printf ("mpz_submul xx fail\n");
+      goto sqrfail;
+    }
+
+  mpz_clear (want);
+  mpz_clear (got);
+}
+
+void
 check_one_ui (mpz_ptr w, mpz_ptr x, unsigned long y)
 {
   mpz_t  want, got;
@@ -235,6 +275,9 @@
 
                   mpz_neg (y, y);
                 }
+
+	      check_sqr (w, x);
+
               mpz_neg (x, x);
             }
           mpz_neg (w, w);


More information about the gmp-commit mailing list