[Gmp-commit] /var/hg/gmp: mini-gmp/tests/t-double.c: Tests the exact number o...

mercurial at gmplib.org mercurial at gmplib.org
Sun May 20 22:15:49 UTC 2018


details:   /var/hg/gmp/rev/aab8a010d10f
changeset: 17630:aab8a010d10f
user:      Marco Bodrato <bodrato at mail.dm.unipi.it>
date:      Mon May 21 00:13:27 2018 +0200
description:
mini-gmp/tests/t-double.c: Tests the exact number of converted bits

diffstat:

 bootstrap.c               |   2 +-
 mini-gmp/ChangeLog        |   2 +
 mini-gmp/mini-gmp.c       |   2 +-
 mini-gmp/tests/t-double.c |  92 ++++++++++++++++++++++++++++++++++++++++++++++-
 4 files changed, 95 insertions(+), 3 deletions(-)

diffs (156 lines):

diff -r 8e0a2e94ae5a -r aab8a010d10f bootstrap.c
--- a/bootstrap.c	Sun May 20 13:24:44 2018 +0200
+++ b/bootstrap.c	Mon May 21 00:13:27 2018 +0200
@@ -29,7 +29,7 @@
 see https://www.gnu.org/licenses/.  */
 
 
-#define DONT_USE_FLOAT_H 1
+#define MINI_GMP_DONT_USE_FLOAT_H 1
 #include "mini-gmp/mini-gmp.c"
 
 #define MIN(l,o) ((l) < (o) ? (l) : (o))
diff -r 8e0a2e94ae5a -r aab8a010d10f mini-gmp/ChangeLog
--- a/mini-gmp/ChangeLog	Sun May 20 13:24:44 2018 +0200
+++ b/mini-gmp/ChangeLog	Mon May 21 00:13:27 2018 +0200
@@ -5,6 +5,8 @@
 
 	* mini-mpq.c (mpq_mul): Use mpq_nan_init.
 
+	* tests/t-double.c (test_matissa): Tests converted bits.
+
 2018-04-26 Marco Bodrato <bodrato at mail.dm.unipi.it>
 
 	* mini-mpq.c: New file, mini-implementation of mpq_t.
diff -r 8e0a2e94ae5a -r aab8a010d10f mini-gmp/mini-gmp.c
--- a/mini-gmp/mini-gmp.c	Sun May 20 13:24:44 2018 +0200
+++ b/mini-gmp/mini-gmp.c	Mon May 21 00:13:27 2018 +0200
@@ -50,7 +50,7 @@
 
 #include "mini-gmp.h"
 
-#if !defined(DONT_USE_FLOAT_H)
+#if !defined(MINI_GMP_DONT_USE_FLOAT_H)
 #include <float.h>
 #endif
 
diff -r 8e0a2e94ae5a -r aab8a010d10f mini-gmp/tests/t-double.c
--- a/mini-gmp/tests/t-double.c	Sun May 20 13:24:44 2018 +0200
+++ b/mini-gmp/tests/t-double.c	Mon May 21 00:13:27 2018 +0200
@@ -1,6 +1,6 @@
 /*
 
-Copyright 2012, 2013 Free Software Foundation, Inc.
+Copyright 2012, 2013, 2018 Free Software Foundation, Inc.
 
 This file is part of the GNU MP Library test suite.
 
@@ -22,13 +22,102 @@
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
+#include <float.h>
 
 #include "testutils.h"
 
 #define GMP_LIMB_BITS (sizeof(mp_limb_t) * CHAR_BIT)
 
+mp_bitcnt_t
+mpz_mantissasizeinbits (const mpz_t z)
+{
+  return ! mpz_cmp_ui (z, 0) ? 0 :
+    mpz_sizeinbase (z, 2) - mpz_scan1 (z, 0);
+}
+
+#if defined(DBL_MANT_DIG) && FLT_RADIX == 2
+int
+mpz_get_d_exact_p (const mpz_t z)
+{
+  return mpz_mantissasizeinbits (z) <= DBL_MANT_DIG;
+}
+#define HAVE_EXACT_P 1
+#endif
+
 #define COUNT 10000
 
+void
+test_matissa (void)
+{
+  mpz_t x, y;
+  int i, c;
+
+  mpz_init (x);
+  mpz_init (y);
+
+  mini_urandomb (y, 4);
+  c = i = mpz_get_ui (y);
+
+  do {
+    double d;
+    int cmp;
+
+    mpz_setbit (x, c);
+    d = mpz_get_d (x);
+    mpz_set_d (y, d);
+    if (mpz_cmp_d (y, d) != 0)
+      {
+	fprintf (stderr, "mpz_cmp_d (y, d) failed:\n"
+		 "d = %.20g\n"
+		 "i = %i\n"
+		 "c = %i\n",
+		 d, i, c);
+	abort ();
+      }
+
+    cmp = mpz_cmp (x, y);
+
+#if defined(HAVE_EXACT_P)
+    if ((mpz_get_d_exact_p (x) != 0) != (cmp == 0))
+      {
+	fprintf (stderr, "Not all bits converted:\n"
+		 "d = %.20g\n"
+		 "i = %i\n"
+		 "c = %i\n",
+		 d, i, c);
+	abort ();
+      }
+#endif
+
+    if (cmp < 0)
+      {
+	fprintf (stderr, "mpz_get_d failed:\n"
+		 "d = %.20g\n"
+		 "i = %i\n"
+		 "c = %i\n",
+		 d, i, c);
+	abort ();
+      }
+    else if (cmp > 0)
+      {
+	if (mpz_cmp_d (x, d) <= 0)
+	  {
+	    fprintf (stderr, "mpz_cmp_d (x, d) failed:\n"
+		     "d = %.20g\n"
+		     "i = %i\n"
+		     "c = %i\n",
+		     d, i, c);
+	    abort ();
+	  }
+	break;
+      }
+    ++c;
+  } while (1);
+
+  mpz_clear (x);
+  mpz_clear (y);
+}
+
 static const struct
 {
   double d;
@@ -135,4 +224,5 @@
     }
 
   mpz_clear (x);
+  test_matissa();
 }


More information about the gmp-commit mailing list