[Gmp-commit] /var/hg/gmp: Don't strip leading zeros since current mpn_get_str...

mercurial at gmplib.org mercurial at gmplib.org
Fri Jun 1 18:30:14 CEST 2012


details:   /var/hg/gmp/rev/ac9e31000e89
changeset: 15039:ac9e31000e89
user:      Torbjorn Granlund <tege at gmplib.org>
date:      Fri Jun 01 18:30:10 2012 +0200
description:
Don't strip leading zeros since current mpn_get_str won't generate any.
Misc streamlining.

diffstat:

 ChangeLog     |   4 ++++
 mpz/get_str.c |  19 ++++---------------
 mpz/out_str.c |  29 +++++++++--------------------
 3 files changed, 17 insertions(+), 35 deletions(-)

diffs (119 lines):

diff -r 0bac450f0eaf -r ac9e31000e89 ChangeLog
--- a/ChangeLog	Fri Jun 01 13:20:07 2012 +0200
+++ b/ChangeLog	Fri Jun 01 18:30:10 2012 +0200
@@ -1,5 +1,9 @@
 2012-06-01  Torbjorn Granlund  <tege at gmplib.org>
 
+	* mpz/get_str.c: Don't strip leading zeros since current mpn_get_str
+	won't generate any.  Misc streamlining.
+	* mpz/out_str.c: Analogous changes.
+
 	* tests/mpz/io.c: Use a wider range of bases.
 
 	* tests/mpz/t-cong.c (check_random): Rewrite random generation for
diff -r 0bac450f0eaf -r ac9e31000e89 mpz/get_str.c
--- a/mpz/get_str.c	Fri Jun 01 13:20:07 2012 +0200
+++ b/mpz/get_str.c	Fri Jun 01 18:30:10 2012 +0200
@@ -4,7 +4,7 @@
    result.  If STRING is not NULL, the caller must ensure enough space is
    available to store the result.
 
-Copyright 1991, 1993, 1994, 1996, 2000, 2001, 2002, 2005 Free Software
+Copyright 1991, 1993, 1994, 1996, 2000, 2001, 2002, 2005, 2012 Free Software
 Foundation, Inc.
 
 This file is part of the GNU MP Library.
@@ -32,7 +32,6 @@
 {
   mp_ptr xp;
   mp_size_t x_size = SIZ (x);
-  char *str;
   char *return_str;
   size_t str_size;
   size_t alloc_size = 0;
@@ -83,26 +82,16 @@
   xp = PTR (x);
   if (! POW2_P (base))
     {
-      xp = TMP_ALLOC_LIMBS (x_size + 1);  /* +1 in case x_size==0 */
+      xp = TMP_ALLOC_LIMBS (x_size);
       MPN_COPY (xp, PTR (x), x_size);
     }
 
   str_size = mpn_get_str ((unsigned char *) res_str, base, xp, x_size);
   ASSERT (alloc_size == 0 || str_size <= alloc_size - (SIZ(x) < 0));
 
-  /* might have a leading zero, skip it */
-  str = res_str;
-  if (*res_str == 0 && str_size != 1)
-    {
-      str_size--;
-      str++;
-      ASSERT (*str != 0);  /* at most one leading zero */
-    }
-
-  /* Convert result to printable chars, and move down if there was a leading
-     zero.  */
+  /* Convert result to printable chars.  */
   for (i = 0; i < str_size; i++)
-    res_str[i] = num_to_text[(int) str[i]];
+    res_str[i] = num_to_text[(int) res_str[i]];
   res_str[str_size] = 0;
 
   TMP_FREE;
diff -r 0bac450f0eaf -r ac9e31000e89 mpz/out_str.c
--- a/mpz/out_str.c	Fri Jun 01 13:20:07 2012 +0200
+++ b/mpz/out_str.c	Fri Jun 01 18:30:10 2012 +0200
@@ -1,8 +1,8 @@
 /* mpz_out_str(stream, base, integer) -- Output to STREAM the multi prec.
    integer INTEGER in base BASE.
 
-Copyright 1991, 1993, 1994, 1996, 2001, 2005, 2011 Free Software Foundation,
-Inc.
+Copyright 1991, 1993, 1994, 1996, 2001, 2005, 2011, 2012 Free Software
+Foundation, Inc.
 
 This file is part of the GNU MP Library.
 
@@ -61,12 +61,6 @@
       num_to_text = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
     }
 
-  if (x_size == 0)
-    {
-      fputc ('0', stream);
-      return ferror (stream) ? 0 : 1;
-    }
-
   written = 0;
 
   if (x_size < 0)
@@ -82,21 +76,16 @@
   str_size += 3;
   str = (unsigned char *) TMP_ALLOC (str_size);
 
-  /* Move the number to convert into temporary space, since mpn_get_str
-     clobbers its argument + needs one extra high limb....  */
-  xp = TMP_ALLOC_LIMBS (x_size + 1);
-  MPN_COPY (xp, PTR (x), x_size);
+  xp = PTR (x);
+  if (! POW2_P (base))
+    {
+      xp = TMP_ALLOC_LIMBS (x_size);
+      MPN_COPY (xp, PTR (x), x_size);
+    }
 
   str_size = mpn_get_str (str, base, xp, x_size);
 
-  /* mpn_get_str might make some leading zeros.  Skip them.  */
-  while (*str == 0)
-    {
-      str_size--;
-      str++;
-    }
-
-  /* Translate to printable chars.  */
+  /* Convert result to printable chars.  */
   for (i = 0; i < str_size; i++)
     str[i] = num_to_text[str[i]];
   str[str_size] = 0;


More information about the gmp-commit mailing list