1 /* mpz_export -- create word data from mpz.
3 Copyright 2002, 2003, 2012 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 either:
10 * the GNU Lesser General Public License as published by the Free
11 Software Foundation; either version 3 of the License, or (at your
12 option) any later version.
16 * the GNU General Public License as published by the Free Software
17 Foundation; either version 2 of the License, or (at your option) any
20 or both in parallel, as here.
22 The GNU MP Library is distributed in the hope that it will be useful, but
23 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
24 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
27 You should have received copies of the GNU General Public License and the
28 GNU Lesser General Public License along with the GNU MP Library. If not,
29 see https://www.gnu.org/licenses/. */
31 #include <stdio.h> /* for NULL */
37 #if HAVE_LIMB_BIG_ENDIAN
40 #if HAVE_LIMB_LITTLE_ENDIAN
41 #define HOST_ENDIAN (-1)
44 static const mp_limb_t endian_test
= (CNST_LIMB(1) << (GMP_LIMB_BITS
-7)) - 1;
45 #define HOST_ENDIAN (* (signed char *) &endian_test)
49 mpz_export (void *data
, size_t *countp
, int order
,
50 size_t size
, int endian
, size_t nail
, mpz_srcptr z
)
58 ASSERT (order
== 1 || order
== -1);
59 ASSERT (endian
== 1 || endian
== 0 || endian
== -1);
60 ASSERT (nail
<= 8*size
);
61 ASSERT (nail
< 8*size
|| SIZ(z
) == 0); /* nail < 8*size+(SIZ(z)==0) */
76 MPN_SIZEINBASE_2EXP (count
, zp
, zsize
, numb
);
80 data
= (*__gmp_allocate_func
) (count
*size
);
85 align
= ((char *) data
- (char *) NULL
) % sizeof (mp_limb_t
);
87 if (nail
== GMP_NAIL_BITS
)
89 if (size
== sizeof (mp_limb_t
) && align
== 0)
91 if (order
== -1 && endian
== HOST_ENDIAN
)
93 MPN_COPY ((mp_ptr
) data
, zp
, (mp_size_t
) count
);
96 if (order
== 1 && endian
== HOST_ENDIAN
)
98 MPN_REVERSE ((mp_ptr
) data
, zp
, (mp_size_t
) count
);
102 if (order
== -1 && endian
== -HOST_ENDIAN
)
104 MPN_BSWAP ((mp_ptr
) data
, zp
, (mp_size_t
) count
);
107 if (order
== 1 && endian
== -HOST_ENDIAN
)
109 MPN_BSWAP_REVERSE ((mp_ptr
) data
, zp
, (mp_size_t
) count
);
116 mp_limb_t limb
, wbitsmask
;
118 mp_size_t j
, wbytes
, woffset
;
123 numb
= size
* 8 - nail
;
125 /* whole bytes per word */
128 /* possible partial byte */
130 wbitsmask
= (CNST_LIMB(1) << wbits
) - 1;
132 /* offset to get to the next word */
133 woffset
= (endian
>= 0 ? size
: - (mp_size_t
) size
)
134 + (order
< 0 ? size
: - (mp_size_t
) size
);
136 /* least significant byte */
137 dp
= (unsigned char *) data
138 + (order
>= 0 ? (count
-1)*size
: 0) + (endian
>= 0 ? size
-1 : 0);
140 #define EXTRACT(N, MASK) \
151 newlimb = (zp == zend ? 0 : *zp++); \
152 *dp = (limb | (newlimb << lbits)) MASK; \
153 limb = newlimb >> ((N)-lbits); \
154 lbits += GMP_NUMB_BITS - (N); \
161 for (i
= 0; i
< count
; i
++)
163 for (j
= 0; j
< wbytes
; j
++)
170 EXTRACT (wbits
, & wbitsmask
);
174 for ( ; j
< size
; j
++)
182 ASSERT (zp
== PTR(z
) + ABSIZ(z
));
184 /* low byte of word after most significant */
185 ASSERT (dp
== (unsigned char *) data
186 + (order
< 0 ? count
*size
: - (mp_size_t
) size
)
187 + (endian
>= 0 ? (mp_size_t
) size
- 1 : 0));