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

mercurial at gmplib.org mercurial at gmplib.org
Wed Oct 28 21:05:48 UTC 2015


details:   /var/hg/gmp/rev/75e07996a590
changeset: 16906:75e07996a590
user:      Torbjorn Granlund <torbjorng at google.com>
date:      Wed Oct 28 21:33:53 2015 +0100
description:
Rewrite for accuracy and performance.

details:   /var/hg/gmp/rev/584e21d6a613
changeset: 16907:584e21d6a613
user:      Torbjorn Granlund <torbjorng at google.com>
date:      Wed Oct 28 22:03:17 2015 +0100
description:
Amend last change.

details:   /var/hg/gmp/rev/d10b11a9618c
changeset: 16908:d10b11a9618c
user:      Torbjorn Granlund <torbjorng at google.com>
date:      Wed Oct 28 22:04:21 2015 +0100
description:
Add elementary testing of mpf_pow_ui.

details:   /var/hg/gmp/rev/cf915e53225d
changeset: 16909:cf915e53225d
user:      Torbjorn Granlund <torbjorng at google.com>
date:      Wed Oct 28 22:05:17 2015 +0100
description:
ChangeLog

diffstat:

 ChangeLog             |   7 +++++
 mpf/pow_ui.c          |  48 ++++++++++++++++++++++++++---------
 tests/mpf/Makefile.am |   4 +-
 tests/mpf/t-pow_ui.c  |  68 +++++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 112 insertions(+), 15 deletions(-)

diffs (170 lines):

diff -r ba0bde86d614 -r cf915e53225d ChangeLog
--- a/ChangeLog	Mon Oct 26 21:56:06 2015 +0100
+++ b/ChangeLog	Wed Oct 28 22:05:17 2015 +0100
@@ -1,3 +1,10 @@
+2015-10-28  Torbjörn Granlund  <torbjorng at google.com>
+
+	* tests/mpf/t-pow_ui.c: New file.
+	* tests/mpf/Makefile.am (check_PROGRAMS): Compile it.
+
+	* mpf/pow_ui.c: Rewrite for accuracy and performance.
+
 2015-10-26 Marco Bodrato <bodrato at mail.dm.unipi.it>
 
 	* configfsf.guess: Updated to version 2015-10-21, for a typo.
diff -r ba0bde86d614 -r cf915e53225d mpf/pow_ui.c
--- a/mpf/pow_ui.c	Mon Oct 26 21:56:06 2015 +0100
+++ b/mpf/pow_ui.c	Wed Oct 28 22:05:17 2015 +0100
@@ -30,25 +30,47 @@
 
 #include "gmp.h"
 #include "gmp-impl.h"
+#include "longlong.h"
 
 void
 mpf_pow_ui (mpf_ptr r, mpf_srcptr b, unsigned long int e)
 {
-  mpf_t b2;
+  mpf_t t;
+  int cnt;
 
-  mpf_init2 (b2, mpf_get_prec (r) + 1);
-  mpf_set (b2, b);
-
-  if ((e & 1) != 0)
-    mpf_set (r, b);
-  else
-    mpf_set_ui (r, 1);
-  while (e >>= 1)
+  if (e <= 1)
     {
-      mpf_mul (b2, b2, b2);
-      if ((e & 1) != 0)
-	mpf_mul (r, r, b2);
+      if (e == 0)
+	mpf_set_ui (r, 1);
+      else
+	mpf_set (r, b);
+      return;
     }
 
-  mpf_clear (b2);
+  count_leading_zeros (cnt, (mp_limb_t) e);
+  cnt = GMP_LIMB_BITS - 1 - cnt;
+
+  /* Use a temp for all intermediate result.  We might want to add an exponent
+     derived # of bits here too, e.g.  ((cnt >> GMP_LIMB_BITS/2) != 0).  */
+  mpf_init2 (t, mpf_get_prec (r) + GMP_LIMB_BITS);
+
+  mpf_set (t, b);		/* consume most significant bit */
+  while (--cnt > 0)
+    {
+      mpf_mul (t, t, t);
+      if ((e >> cnt) & 1)
+	mpf_mul (t, t, b);
+    }
+
+  if (e & 1)
+    {
+      mpf_mul (t, t, t);
+      mpf_mul (r, t, b);
+    }
+  else
+    {
+      mpf_mul (r, t, t);
+    }
+
+  mpf_clear (t);
 }
diff -r ba0bde86d614 -r cf915e53225d tests/mpf/Makefile.am
--- a/tests/mpf/Makefile.am	Mon Oct 26 21:56:06 2015 +0100
+++ b/tests/mpf/Makefile.am	Wed Oct 28 22:05:17 2015 +0100
@@ -1,6 +1,6 @@
 ## Process this file with automake to generate Makefile.in
 
-# Copyright 1996, 1999-2004 Free Software Foundation, Inc.
+# Copyright 1996, 1999-2004, 2015 Free Software Foundation, Inc.
 #
 # This file is part of the GNU MP Library test suite.
 #
@@ -24,7 +24,7 @@
 check_PROGRAMS = t-dm2exp t-conv t-add t-sub t-sqrt t-sqrt_ui t-muldiv reuse \
   t-cmp_d t-cmp_si t-div t-fits t-get_d t-get_d_2exp \
   t-get_si t-get_ui t-gsprec t-inp_str t-int_p t-mul_ui \
-  t-set t-set_q t-set_si t-set_ui t-trunc t-ui_div t-eq
+  t-set t-set_q t-set_si t-set_ui t-trunc t-ui_div t-eq t-pow_ui
 TESTS = $(check_PROGRAMS)
 
 $(top_builddir)/tests/libtests.la:
diff -r ba0bde86d614 -r cf915e53225d tests/mpf/t-pow_ui.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/mpf/t-pow_ui.c	Wed Oct 28 22:05:17 2015 +0100
@@ -0,0 +1,68 @@
+/* Test mpf_pow_ui
+
+Copyright 2015 Free Software Foundation, Inc.
+
+This file is part of the GNU MP Library test suite.
+
+The GNU MP Library test suite is free software; you can redistribute it
+and/or modify it under the terms of the GNU General Public License as
+published by the Free Software Foundation; either version 3 of the License,
+or (at your option) any later version.
+
+The GNU MP Library test suite is distributed in the hope that it will be
+useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
+Public License for more details.
+
+You should have received a copy of the GNU General Public License along with
+the GNU MP Library test suite.  If not, see https://www.gnu.org/licenses/.  */
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "gmp.h"
+#include "gmp-impl.h"
+#include "tests.h"
+
+void
+check_data (void)
+{
+  unsigned int b, e;
+  mpf_t b1, r, r2;
+
+  mpf_inits (b1, r, r2, NULL);
+
+  /* This test just test integers with results that fit in a single
+     limb.  This avoid any rounding.  */
+
+  for (b = 0; b <= 400; b++)
+    {
+      mpf_set_ui (b1, b);
+      mpf_set_ui (r2, 1);
+      for (e = 0; e <= GMP_LIMB_BITS; e++)
+	{
+	  mpf_pow_ui (r, b1, e);
+
+	  if (mpf_cmp_ui (r, ~CNST_LIMB(0)) > 0)
+	    break;
+
+	  if (mpf_cmp (r, r2))
+	    abort ();
+
+	  mpf_mul_ui (r2, r2, b);
+	}
+    }
+
+  mpf_clears (b1, r, r2, NULL);
+}
+
+int
+main (int argc, char **argv)
+{
+  tests_start ();
+
+  check_data ();
+
+  tests_end ();
+  exit (0);
+}


More information about the gmp-commit mailing list