summaryrefslogtreecommitdiff
path: root/gmp/mpz/export.c
diff options
context:
space:
mode:
Diffstat (limited to 'gmp/mpz/export.c')
-rw-r--r--gmp/mpz/export.c128
1 files changed, 65 insertions, 63 deletions
diff --git a/gmp/mpz/export.c b/gmp/mpz/export.c
index bc4c4df976..484b9d91bb 100644
--- a/gmp/mpz/export.c
+++ b/gmp/mpz/export.c
@@ -1,32 +1,21 @@
/* mpz_export -- create word data from mpz.
-Copyright 2002, 2003, 2012 Free Software Foundation, Inc.
+Copyright 2002, 2003 Free Software Foundation, Inc.
This file is part of the GNU MP Library.
The GNU MP Library is free software; you can redistribute it and/or modify
-it under the terms of either:
-
- * the GNU Lesser General Public License as published by the Free
- Software Foundation; either version 3 of the License, or (at your
- option) any later version.
-
-or
-
- * the GNU General Public License as published by the Free Software
- Foundation; either version 2 of the License, or (at your option) any
- later version.
-
-or both in parallel, as here.
+it under the terms of the GNU Lesser 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 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.
+or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
+License for more details.
-You should have received copies of the GNU General Public License and the
-GNU Lesser General Public License along with the GNU MP Library. If not,
-see https://www.gnu.org/licenses/. */
+You should have received a copy of the GNU Lesser General Public License
+along with the GNU MP Library. If not, see http://www.gnu.org/licenses/. */
#include <stdio.h> /* for NULL */
#include "gmp.h"
@@ -45,9 +34,22 @@ static const mp_limb_t endian_test = (CNST_LIMB(1) << (GMP_LIMB_BITS-7)) - 1;
#define HOST_ENDIAN (* (signed char *) &endian_test)
#endif
+
+#define MPN_SIZEINBASE_2EXP(result, ptr, size, base2exp) \
+ do { \
+ int __cnt; \
+ unsigned long __totbits; \
+ ASSERT ((size) > 0); \
+ ASSERT ((ptr)[(size)-1] != 0); \
+ count_leading_zeros (__cnt, (ptr)[(size)-1]); \
+ __totbits = (size) * GMP_NUMB_BITS - (__cnt - GMP_NAIL_BITS); \
+ (result) = (__totbits + (base2exp)-1) / (base2exp); \
+ } while (0)
+
+
void *
mpz_export (void *data, size_t *countp, int order,
- size_t size, int endian, size_t nail, mpz_srcptr z)
+ size_t size, int endian, size_t nail, mpz_srcptr z)
{
mp_size_t zsize;
mp_srcptr zp;
@@ -58,7 +60,7 @@ mpz_export (void *data, size_t *countp, int order,
ASSERT (order == 1 || order == -1);
ASSERT (endian == 1 || endian == 0 || endian == -1);
ASSERT (nail <= 8*size);
- ASSERT (nail < 8*size || SIZ(z) == 0); /* nail < 8*size+(SIZ(z)==0) */
+ ASSERT (8*size-nail > 0);
if (countp == NULL)
countp = &dummy;
@@ -87,29 +89,29 @@ mpz_export (void *data, size_t *countp, int order,
if (nail == GMP_NAIL_BITS)
{
if (size == sizeof (mp_limb_t) && align == 0)
- {
- if (order == -1 && endian == HOST_ENDIAN)
- {
- MPN_COPY ((mp_ptr) data, zp, (mp_size_t) count);
- return data;
- }
- if (order == 1 && endian == HOST_ENDIAN)
- {
- MPN_REVERSE ((mp_ptr) data, zp, (mp_size_t) count);
- return data;
- }
-
- if (order == -1 && endian == -HOST_ENDIAN)
- {
- MPN_BSWAP ((mp_ptr) data, zp, (mp_size_t) count);
- return data;
- }
- if (order == 1 && endian == -HOST_ENDIAN)
- {
- MPN_BSWAP_REVERSE ((mp_ptr) data, zp, (mp_size_t) count);
- return data;
- }
- }
+ {
+ if (order == -1 && endian == HOST_ENDIAN)
+ {
+ MPN_COPY ((mp_ptr) data, zp, (mp_size_t) count);
+ return data;
+ }
+ if (order == 1 && endian == HOST_ENDIAN)
+ {
+ MPN_REVERSE ((mp_ptr) data, zp, (mp_size_t) count);
+ return data;
+ }
+
+ if (order == -1 && endian == -HOST_ENDIAN)
+ {
+ MPN_BSWAP ((mp_ptr) data, zp, (mp_size_t) count);
+ return data;
+ }
+ if (order == 1 && endian == -HOST_ENDIAN)
+ {
+ MPN_BSWAP_REVERSE ((mp_ptr) data, zp, (mp_size_t) count);
+ return data;
+ }
+ }
}
{
@@ -160,31 +162,31 @@ mpz_export (void *data, size_t *countp, int order,
limb = 0;
for (i = 0; i < count; i++)
{
- for (j = 0; j < wbytes; j++)
- {
- EXTRACT (8, + 0);
- dp -= endian;
- }
- if (wbits != 0)
- {
- EXTRACT (wbits, & wbitsmask);
- dp -= endian;
- j++;
- }
- for ( ; j < size; j++)
- {
- *dp = '\0';
- dp -= endian;
- }
- dp += woffset;
+ for (j = 0; j < wbytes; j++)
+ {
+ EXTRACT (8, + 0);
+ dp -= endian;
+ }
+ if (wbits != 0)
+ {
+ EXTRACT (wbits, & wbitsmask);
+ dp -= endian;
+ j++;
+ }
+ for ( ; j < size; j++)
+ {
+ *dp = '\0';
+ dp -= endian;
+ }
+ dp += woffset;
}
ASSERT (zp == PTR(z) + ABSIZ(z));
/* low byte of word after most significant */
ASSERT (dp == (unsigned char *) data
- + (order < 0 ? count*size : - (mp_size_t) size)
- + (endian >= 0 ? (mp_size_t) size - 1 : 0));
+ + (order < 0 ? count*size : - (mp_size_t) size)
+ + (endian >= 0 ? (mp_size_t) size - 1 : 0));
}
return data;
}