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

mercurial at gmplib.org mercurial at gmplib.org
Fri Feb 3 17:56:18 UTC 2017


details:   /var/hg/gmp/rev/7fd73a750834
changeset: 17222:7fd73a750834
user:      Marco Bodrato <bodrato at mail.dm.unipi.it>
date:      Fri Feb 03 18:54:12 2017 +0100
description:
* mini-gmp.c (mpz_sizeinbase, mpz_get_str, mpz_set_str): Support bases up to 62.

details:   /var/hg/gmp/rev/170b4abb7cc1
changeset: 17223:170b4abb7cc1
user:      Marco Bodrato <bodrato at mail.dm.unipi.it>
date:      Fri Feb 03 18:55:38 2017 +0100
description:
mini-gmp/tests/t-str.c: Test bases up to 62

details:   /var/hg/gmp/rev/5a6706c3817f
changeset: 17224:5a6706c3817f
user:      Marco Bodrato <bodrato at mail.dm.unipi.it>
date:      Fri Feb 03 18:56:09 2017 +0100
description:
Changelog

diffstat:

 mini-gmp/ChangeLog     |   8 ++++++++
 mini-gmp/README        |   2 --
 mini-gmp/mini-gmp.c    |  26 +++++++++++++++-----------
 mini-gmp/tests/t-str.c |  14 +++++++-------
 4 files changed, 30 insertions(+), 20 deletions(-)

diffs (163 lines):

diff -r e5173fed3ce9 -r 5a6706c3817f mini-gmp/ChangeLog
--- a/mini-gmp/ChangeLog	Fri Feb 03 14:29:31 2017 +0100
+++ b/mini-gmp/ChangeLog	Fri Feb 03 18:56:09 2017 +0100
@@ -1,7 +1,15 @@
+2017-02-03 Marco Bodrato <bodrato at mail.dm.unipi.it>
+
+	* mini-gmp.c (mpz_sizeinbase, mpz_get_str, mpz_set_str): Support
+	bases up to 62.
+	* tests/t-str.c: Update tests accordingly.
+
 2016-11-27 Marco Bodrato <bodrato at mail.dm.unipi.it>
 
 	* mini-gmp.c (mpz_get_si): fewer branches, safer forumla for negatives.
 
+	* tests/t-signed.c: Removed a typo
+
 2016-11-24  Niels Möller  <nisse at lysator.liu.se>
 
 	* tests/hex-random.c (mkseed): Produce a 48-bit mpz_t value,
diff -r e5173fed3ce9 -r 5a6706c3817f mini-gmp/README
--- a/mini-gmp/README	Fri Feb 03 14:29:31 2017 +0100
+++ b/mini-gmp/README	Fri Feb 03 18:56:09 2017 +0100
@@ -42,8 +42,6 @@
 functions are fully compatible with the corresponding GMP functions,
 as specified in the GMP manual, with a few exceptions:
 
-  mpz_set_str, mpz_init_set_str, mpz_get_str, mpz_out_str and
-  mpz_sizeinbase support only |base| <= 36;
   mpz_export and mpz_import support only NAILS = 0.
 
   The REALLOC_FUNC and FREE_FUNC registered with
diff -r e5173fed3ce9 -r 5a6706c3817f mini-gmp/mini-gmp.c
--- a/mini-gmp/mini-gmp.c	Fri Feb 03 14:29:31 2017 +0100
+++ b/mini-gmp/mini-gmp.c	Fri Feb 03 18:56:09 2017 +0100
@@ -4013,7 +4013,7 @@
   size_t ndigits;
 
   assert (base >= 2);
-  assert (base <= 36);
+  assert (base <= 62);
 
   un = GMP_ABS (u->_mp_size);
   if (un == 0)
@@ -4063,19 +4063,22 @@
   mp_size_t un;
   size_t i, sn;
 
-  if (base >= 0)
+  digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
+  if (base > 1)
     {
-      digits = "0123456789abcdefghijklmnopqrstuvwxyz";
+      if (base <= 36)
+	digits = "0123456789abcdefghijklmnopqrstuvwxyz";
+      else if (base > 62)
+	return NULL;
     }
+  else if (base >= -1)
+    base = 10;
   else
     {
       base = -base;
-      digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
+      if (base > 36)
+	return NULL;
     }
-  if (base <= 1)
-    base = 10;
-  if (base > 36)
-    return NULL;
 
   sn = 1 + mpz_sizeinbase (u, base);
   if (!sp)
@@ -4123,14 +4126,14 @@
 int
 mpz_set_str (mpz_t r, const char *sp, int base)
 {
-  unsigned bits;
+  unsigned bits, value_of_a;
   mp_size_t rn, alloc;
   mp_ptr rp;
   size_t dn;
   int sign;
   unsigned char *dp;
 
-  assert (base == 0 || (base >= 2 && base <= 36));
+  assert (base == 0 || (base >= 2 && base <= 62));
 
   while (isspace( (unsigned char) *sp))
     sp++;
@@ -4166,6 +4169,7 @@
     }
   dp = (unsigned char *) gmp_xalloc (strlen (sp));
 
+  value_of_a = (base > 36) ? 36 : 10;
   for (dn = 0; *sp; sp++)
     {
       unsigned digit;
@@ -4175,7 +4179,7 @@
       else if (*sp >= '0' && *sp <= '9')
 	digit = *sp - '0';
       else if (*sp >= 'a' && *sp <= 'z')
-	digit = *sp - 'a' + 10;
+	digit = *sp - 'a' + value_of_a;
       else if (*sp >= 'A' && *sp <= 'Z')
 	digit = *sp - 'A' + 10;
       else
diff -r e5173fed3ce9 -r 5a6706c3817f mini-gmp/tests/t-str.c
--- a/mini-gmp/tests/t-str.c	Fri Feb 03 14:29:31 2017 +0100
+++ b/mini-gmp/tests/t-str.c	Fri Feb 03 18:56:09 2017 +0100
@@ -159,9 +159,9 @@
   for (i = 0; i < COUNT; i++)
     {
       int base;
-      for (base = 0; base <= 36; base += 1 + (base == 0))
+      for (base = 0; base <= 62; base += 1 + (base == 0))
 	{
-	  hex_random_str_op (MAXBITS, i&1 ? base: -base, &ap, &rp);
+	  hex_random_str_op (MAXBITS, (i&1 || base > 36) ? base: -base, &ap, &rp);
 	  if (mpz_set_str (a, ap, 16) != 0)
 	    {
 	      fprintf (stderr, "mpz_set_str failed on input %s\n", ap);
@@ -181,7 +181,7 @@
 		       base, (unsigned) arn, (unsigned)bn);
 	      abort ();
 	    }
-	  bp = mpz_get_str (NULL, i&1 ? base: -base, a);
+	  bp = mpz_get_str (NULL, (i&1 || base > 36) ? base: -base, a);
 	  if (strcmp (bp, rp))
 	    {
 	      fprintf (stderr, "mpz_get_str failed:\n");
@@ -197,7 +197,7 @@
 	    {
 	      size_t tn;
 	      rewind (tmp);
-	      tn = mpz_out_str (tmp, i&1 ? base: -base, a);
+	      tn = mpz_out_str (tmp, (i&1 || base > 36) ? base: -base, a);
 	      if (tn != rn)
 		{
 		  fprintf (stderr, "mpz_out_str, bad return value:\n");
@@ -249,7 +249,7 @@
 	      size_t i;
 	      const char *absr;
 	      mp_limb_t t[MAXLIMBS];
-	      mp_size_t tn = mpz_size (a);
+	      size_t tn = mpz_size (a);
 
 	      assert (tn <= MAXLIMBS);
 	      mpn_copyi (t, a->_mp_d, tn);
@@ -272,11 +272,11 @@
 	      for (i = 0; i < bn; i++)
 		{
 		  unsigned char digit = absr[i];
-		  unsigned value;
+		  char value;
 		  if (digit >= '0' && digit <= '9')
 		    value = digit - '0';
 		  else if (digit >= 'a' && digit <= 'z')
-		    value = digit - 'a' + 10;
+		    value = digit - 'a' + ((base > 36) ? 36 : 10);
 		  else if (digit >= 'A' && digit <= 'Z')
 		    value = digit - 'A' + 10;
 		  else


More information about the gmp-commit mailing list