1 /* mpz_import -- set mpz from word data.
3 Copyright 2002 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/. */
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)
39 mpz_import (mpz_ptr z, size_t count, int order,
40 size_t size, int endian, size_t nail, const void *data)
45 ASSERT (order == 1 || order == -1);
46 ASSERT (endian == 1 || endian == 0 || endian == -1);
47 ASSERT (nail <= 8*size);
49 zsize = (count * (8*size - nail) + GMP_NUMB_BITS-1) / GMP_NUMB_BITS;
50 MPZ_REALLOC (z, zsize);
56 /* Can't use these special cases with nails currently, since they don't
57 mask out the nail bits in the input data. */
58 if (nail == 0 && GMP_NAIL_BITS == 0)
60 unsigned align = ((char *) data - (char *) NULL) % sizeof (mp_limb_t);
63 && size == sizeof (mp_limb_t)
64 && endian == HOST_ENDIAN
67 MPN_COPY (zp, (mp_srcptr) data, (mp_size_t) count);
72 && size == sizeof (mp_limb_t)
73 && endian == - HOST_ENDIAN
76 MPN_BSWAP (zp, (mp_srcptr) data, (mp_size_t) count);
81 && size == sizeof (mp_limb_t)
82 && endian == HOST_ENDIAN
85 MPN_REVERSE (zp, (mp_srcptr) data, (mp_size_t) count);
91 mp_limb_t limb, byte, wbitsmask;
92 size_t i, j, numb, wbytes;
97 numb = size * 8 - nail;
99 /* whole bytes to process */
102 /* partial byte to process */
104 wbitsmask = (CNST_LIMB(1) << wbits) - 1;
106 /* offset to get to the next word after processing wbytes and wbits */
107 woffset = (numb + 7) / 8;
108 woffset = (endian >= 0 ? woffset : -woffset)
109 + (order < 0 ? size : - (mp_size_t) size);
111 /* least significant byte */
112 dp = (unsigned char *) data
113 + (order >= 0 ? (count-1)*size : 0) + (endian >= 0 ? size-1 : 0);
115 #define ACCUMULATE(N) \
117 ASSERT (lbits < GMP_NUMB_BITS); \
118 ASSERT (limb <= (CNST_LIMB(1) << lbits) - 1); \
120 limb |= (mp_limb_t) byte << lbits; \
122 if (lbits >= GMP_NUMB_BITS) \
124 *zp++ = limb & GMP_NUMB_MASK; \
125 lbits -= GMP_NUMB_BITS; \
126 ASSERT (lbits < (N)); \
127 limb = byte >> ((N) - lbits); \
133 for (i = 0; i < count; i++)
135 for (j = 0; j < wbytes; j++)
143 byte = *dp & wbitsmask;
152 ASSERT (lbits <= GMP_NUMB_BITS);
157 ASSERT (zp == PTR(z) + zsize);
159 /* low byte of word after most significant */
160 ASSERT (dp == (unsigned char *) data
161 + (order < 0 ? count*size : - (mp_size_t) size)
162 + (endian >= 0 ? (mp_size_t) size - 1 : 0));
168 MPN_NORMALIZE (zp, zsize);