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

mercurial at gmplib.org mercurial at gmplib.org
Mon Apr 20 15:25:53 UTC 2020


details:   /var/hg/gmp/rev/b6bbbc44bdc9
changeset: 18066:b6bbbc44bdc9
user:      Niels M?ller <nisse at lysator.liu.se>
date:      Mon Apr 20 17:10:38 2020 +0200
description:
mini-gmp: Simplify mpz_fits_slong_p.

details:   /var/hg/gmp/rev/f4c89b9840ba
changeset: 18067:f4c89b9840ba
user:      Niels M?ller <nisse at lysator.liu.se>
date:      Mon Apr 20 17:25:22 2020 +0200
description:
mini-gmp: More functions mpz_fits_*_p.

New functions mpz_fits_sint_p, mpz_fits_uint_p, mpz_fits_sshort_p, and
mpz_fits_ushort_p. New tests, contributed by Marco.

diffstat:

 mini-gmp/ChangeLog        |    9 ++
 mini-gmp/mini-gmp.c       |   27 +++++++-
 mini-gmp/mini-gmp.h       |    4 +
 mini-gmp/tests/t-signed.c |  145 +++++++++++++++++++++++++++++++++++++++++++++-
 4 files changed, 182 insertions(+), 3 deletions(-)

diffs (238 lines):

diff -r 805304ca965a -r f4c89b9840ba mini-gmp/ChangeLog
--- a/mini-gmp/ChangeLog	Tue Mar 24 23:13:28 2020 +0100
+++ b/mini-gmp/ChangeLog	Mon Apr 20 17:25:22 2020 +0200
@@ -1,3 +1,12 @@
+2020-04-20  Niels Möller  <nisse at lysator.liu.se>
+
+	* mini-gmp.c (mpz_fits_slong_p): Simplify, by using mpz_cmp_si.
+	(mpz_fits_sint_p, mpz_fits_uint_p, mpz_fits_sshort_p)
+	(mpz_fits_ushort_p): New functions.
+	* mini-gmp.h: Declare new functions.
+	* tests/t-signed.c (try_fits_utype_p, try_fits_sint_p)
+	(try_fits_sshort_p): New tests.
+
 2020-02-02 Marco Bodrato <bodrato at mail.dm.unipi.it>
 
 	* mini-gmp/mini-gmp.c (gmp_jacobi_coprime): Exit condition on a.
diff -r 805304ca965a -r f4c89b9840ba mini-gmp/mini-gmp.c
--- a/mini-gmp/mini-gmp.c	Tue Mar 24 23:13:28 2020 +0100
+++ b/mini-gmp/mini-gmp.c	Mon Apr 20 17:25:22 2020 +0200
@@ -1541,8 +1541,7 @@
 int
 mpz_fits_slong_p (const mpz_t u)
 {
-  return (LONG_MAX + LONG_MIN == 0 || mpz_cmp_ui (u, LONG_MAX) <= 0) &&
-    mpz_cmpabs_ui (u, GMP_NEG_CAST (unsigned long int, LONG_MIN)) <= 0;
+  return mpz_cmp_si (u, LONG_MAX) <= 0 && mpz_cmp_si (u, LONG_MIN) >= 0;
 }
 
 static int
@@ -1565,6 +1564,30 @@
   return us >= 0 && mpn_absfits_ulong_p (u->_mp_d, us);
 }
 
+int
+mpz_fits_sint_p (const mpz_t u)
+{
+  return mpz_cmp_si (u, INT_MAX) <= 0 && mpz_cmp_si (u, INT_MIN) >= 0;
+}
+
+int
+mpz_fits_uint_p (const mpz_t u)
+{
+  return u->_mp_size >= 0 && mpz_cmpabs_ui (u, UINT_MAX) <= 0;
+}
+
+int
+mpz_fits_sshort_p (const mpz_t u)
+{
+  return mpz_cmp_si (u, SHRT_MAX) <= 0 && mpz_cmp_si (u, SHRT_MIN) >= 0;
+}
+
+int
+mpz_fits_ushort_p (const mpz_t u)
+{
+  return u->_mp_size >= 0 && mpz_cmpabs_ui (u, USHRT_MAX) <= 0;
+}
+
 long int
 mpz_get_si (const mpz_t u)
 {
diff -r 805304ca965a -r f4c89b9840ba mini-gmp/mini-gmp.h
--- a/mini-gmp/mini-gmp.h	Tue Mar 24 23:13:28 2020 +0100
+++ b/mini-gmp/mini-gmp.h	Mon Apr 20 17:25:22 2020 +0200
@@ -244,6 +244,10 @@
 
 int mpz_fits_slong_p (const mpz_t);
 int mpz_fits_ulong_p (const mpz_t);
+int mpz_fits_sint_p (const mpz_t);
+int mpz_fits_uint_p (const mpz_t);
+int mpz_fits_sshort_p (const mpz_t);
+int mpz_fits_ushort_p (const mpz_t);
 long int mpz_get_si (const mpz_t);
 unsigned long int mpz_get_ui (const mpz_t);
 double mpz_get_d (const mpz_t);
diff -r 805304ca965a -r f4c89b9840ba mini-gmp/tests/t-signed.c
--- a/mini-gmp/tests/t-signed.c	Tue Mar 24 23:13:28 2020 +0100
+++ b/mini-gmp/tests/t-signed.c	Mon Apr 20 17:25:22 2020 +0200
@@ -1,6 +1,6 @@
 /* Exercise some mpz_..._si functions.
 
-Copyright 2013, 2016 Free Software Foundation, Inc.
+Copyright 2013, 2016, 2020 Free Software Foundation, Inc.
 
 This file is part of the GNU MP Library test suite.
 
@@ -197,9 +197,152 @@
 }
 
 void
+try_fits_utype_p (void)
+{
+  mpz_t x;
+  mpz_init (x);
+  if (!mpz_fits_ulong_p (x))
+    {
+      printf ("mpz_fits_ulong_p (0) false!\n");
+      abort ();
+    }
+  if (!mpz_fits_uint_p (x))
+    {
+      printf ("mpz_fits_uint_p (0) false!\n");
+      abort ();
+    }
+  if (!mpz_fits_ushort_p (x))
+    {
+      printf ("mpz_fits_udhort_p (0) false!\n");
+      abort ();
+    }
+  mpz_set_si (x, -1);
+  if (mpz_fits_ulong_p (x))
+    {
+      printf ("mpz_fits_ulong_p (- 1) true!\n");
+      abort ();
+    }
+  if (mpz_fits_uint_p (x))
+    {
+      printf ("mpz_fits_uint_p (- 1) true!\n");
+      abort ();
+    }
+  if (mpz_fits_ushort_p (x))
+    {
+      printf ("mpz_fits_ushort_p (- 1) true!\n");
+      abort ();
+    }
+  mpz_set_ui (x, ULONG_MAX);
+  if (!mpz_fits_ulong_p (x))
+    {
+      printf ("mpz_fits_ulong_p (ULONG_MAX) false!\n");
+      abort ();
+    }
+  mpz_add_ui (x, x, 1);
+  if (mpz_fits_ulong_p (x))
+    {
+      printf ("mpz_fits_ulong_p (ULONG_MAX + 1) true!\n");
+      abort ();
+    }
+  mpz_set_ui (x, UINT_MAX);
+  if (!mpz_fits_uint_p (x))
+    {
+      printf ("mpz_fits_uint_p (UINT_MAX) false!\n");
+      abort ();
+    }
+  mpz_add_ui (x, x, 1);
+  if (mpz_fits_uint_p (x))
+    {
+      printf ("mpz_fits_uint_p (UINT_MAX + 1) true!\n");
+      abort ();
+    }
+  mpz_set_ui (x, USHRT_MAX);
+  if (!mpz_fits_ushort_p (x))
+    {
+      printf ("mpz_fits_ushort_p (USHRT_MAX) false!\n");
+      abort ();
+    }
+  mpz_add_ui (x, x, 1);
+  if (mpz_fits_ushort_p (x))
+    {
+      printf ("mpz_fits_ushort_p (USHRT_MAX + 1) true!\n");
+      abort ();
+    }
+
+  mpz_clear (x);
+}
+
+void
+try_fits_sint_p (void)
+{
+  mpz_t x;
+  mpz_init_set_si (x, INT_MAX);
+  if (!mpz_fits_sint_p (x))
+    {
+      printf ("mpz_fits_sint_p (INT_MAX) false!\n");
+      abort ();
+    }
+  mpz_add_ui (x, x, 1);
+  if (mpz_fits_sint_p (x))
+    {
+      printf ("mpz_fits_sint_p (INT_MAX + 1) true!\n");
+      abort ();
+    }
+  mpz_set_si (x, INT_MIN);
+  if (!mpz_fits_sint_p (x))
+    {
+      printf ("mpz_fits_sint_p (INT_MIN) false!\n");
+      abort ();
+    }
+  mpz_sub_ui (x, x, 1);
+  if (mpz_fits_sint_p (x))
+    {
+      printf ("mpz_fits_sint_p (INT_MIN - 1) true!\n");
+      abort ();
+    }
+
+  mpz_clear (x);
+}
+
+void
+try_fits_sshort_p (void)
+{
+  mpz_t x;
+  mpz_init_set_si (x, SHRT_MAX);
+  if (!mpz_fits_sshort_p (x))
+    {
+      printf ("mpz_fits_sshort_p (SHRT_MAX) false!\n");
+      abort ();
+    }
+  mpz_add_ui (x, x, 1);
+  if (mpz_fits_sshort_p (x))
+    {
+      printf ("mpz_fits_sshort_p (SHRT_MAX + 1) true!\n");
+      abort ();
+    }
+  mpz_set_si (x, SHRT_MIN);
+  if (!mpz_fits_sshort_p (x))
+    {
+      printf ("mpz_fits_sshort_p (SHRT_MIN) false!\n");
+      abort ();
+    }
+  mpz_sub_ui (x, x, 1);
+  if (mpz_fits_sshort_p (x))
+    {
+      printf ("mpz_fits_sshort_p (SHRT_MIN - 1) true!\n");
+      abort ();
+    }
+
+  mpz_clear (x);
+}
+
+void
 testmain (int argc, char *argv[])
 {
   try_fits_slong_p ();
+  try_fits_sint_p ();
+  try_fits_sshort_p ();
+  try_fits_utype_p ();
   try_op_si (-1);
   try_op_si (1);
 }


More information about the gmp-commit mailing list