1 /* mpz_export -- create word data from mpz.
3 Copyright 2002, 2003 Free Software Foundation, Inc.
5 This file is part of the GNU MP Library.
7 The GNU MP Library is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
12 The GNU MP Library is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with the GNU MP Library. If not, see http://www.gnu.org/licenses/. */
20 #include <stdio.h> /* for NULL */
26 #if HAVE_LIMB_BIG_ENDIAN
29 #if HAVE_LIMB_LITTLE_ENDIAN
30 #define HOST_ENDIAN (-1)
33 static const mp_limb_t endian_test
= (CNST_LIMB(1) << (GMP_LIMB_BITS
-7)) - 1;
34 #define HOST_ENDIAN (* (signed char *) &endian_test)
38 #define MPN_SIZEINBASE_2EXP(result, ptr, size, base2exp) \
41 unsigned long __totbits; \
42 ASSERT ((size) > 0); \
43 ASSERT ((ptr)[(size)-1] != 0); \
44 count_leading_zeros (__cnt, (ptr)[(size)-1]); \
45 __totbits = (size) * GMP_NUMB_BITS - (__cnt - GMP_NAIL_BITS); \
46 (result) = (__totbits + (base2exp)-1) / (base2exp); \
51 mpz_export (void *data
, size_t *countp
, int order
,
52 size_t size
, int endian
, size_t nail
, mpz_srcptr z
)
60 ASSERT (order
== 1 || order
== -1);
61 ASSERT (endian
== 1 || endian
== 0 || endian
== -1);
62 ASSERT (nail
<= 8*size
);
63 ASSERT (8*size
-nail
> 0);
78 MPN_SIZEINBASE_2EXP (count
, zp
, zsize
, numb
);
82 data
= (*__gmp_allocate_func
) (count
*size
);
87 align
= ((char *) data
- (char *) NULL
) % sizeof (mp_limb_t
);
89 if (nail
== GMP_NAIL_BITS
)
91 if (size
== sizeof (mp_limb_t
) && align
== 0)
93 if (order
== -1 && endian
== HOST_ENDIAN
)
95 MPN_COPY ((mp_ptr
) data
, zp
, (mp_size_t
) count
);
98 if (order
== 1 && endian
== HOST_ENDIAN
)
100 MPN_REVERSE ((mp_ptr
) data
, zp
, (mp_size_t
) count
);
104 if (order
== -1 && endian
== -HOST_ENDIAN
)
106 MPN_BSWAP ((mp_ptr
) data
, zp
, (mp_size_t
) count
);
109 if (order
== 1 && endian
== -HOST_ENDIAN
)
111 MPN_BSWAP_REVERSE ((mp_ptr
) data
, zp
, (mp_size_t
) count
);
118 mp_limb_t limb
, wbitsmask
;
120 mp_size_t j
, wbytes
, woffset
;
125 numb
= size
* 8 - nail
;
127 /* whole bytes per word */
130 /* possible partial byte */
132 wbitsmask
= (CNST_LIMB(1) << wbits
) - 1;
134 /* offset to get to the next word */
135 woffset
= (endian
>= 0 ? size
: - (mp_size_t
) size
)
136 + (order
< 0 ? size
: - (mp_size_t
) size
);
138 /* least significant byte */
139 dp
= (unsigned char *) data
140 + (order
>= 0 ? (count
-1)*size
: 0) + (endian
>= 0 ? size
-1 : 0);
142 #define EXTRACT(N, MASK) \
153 newlimb = (zp == zend ? 0 : *zp++); \
154 *dp = (limb | (newlimb << lbits)) MASK; \
155 limb = newlimb >> ((N)-lbits); \
156 lbits += GMP_NUMB_BITS - (N); \
163 for (i
= 0; i
< count
; i
++)
165 for (j
= 0; j
< wbytes
; j
++)
172 EXTRACT (wbits
, & wbitsmask
);
176 for ( ; j
< size
; j
++)
184 ASSERT (zp
== PTR(z
) + ABSIZ(z
));
186 /* low byte of word after most significant */
187 ASSERT (dp
== (unsigned char *) data
188 + (order
< 0 ? count
*size
: - (mp_size_t
) size
)
189 + (endian
>= 0 ? (mp_size_t
) size
- 1 : 0));