1 /* mpz_inp_raw -- read an mpz_t in raw format.
3 Copyright 2001, 2002, 2005 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/. */
25 /* NTOH_LIMB_FETCH fetches a limb which is in network byte order (ie. big
26 endian) and produces a normal host byte order result. */
28 #if HAVE_LIMB_BIG_ENDIAN
29 #define NTOH_LIMB_FETCH(limb, src) do { (limb) = *(src); } while (0)
32 #if HAVE_LIMB_LITTLE_ENDIAN
33 #define NTOH_LIMB_FETCH(limb, src) BSWAP_LIMB_FETCH (limb, src)
36 #ifndef NTOH_LIMB_FETCH
37 #define NTOH_LIMB_FETCH(limb, src) \
39 const unsigned char *__p = (const unsigned char *) (src); \
43 for (__i = 0; __i < BYTES_PER_MP_LIMB; __i++) \
44 __limb = (__limb << 8) | __p[__i]; \
50 /* Enhancement: The byte swap loop ought to be safe to vectorize on Cray
51 etc, but someone who knows what they're doing needs to check it. */
54 mpz_inp_raw (mpz_ptr x, FILE *fp)
56 unsigned char csize_bytes[4];
57 mp_size_t csize, abs_xsize, i;
61 mp_limb_t slimb, elimb;
66 /* 4 bytes for size */
67 if (fread (csize_bytes, sizeof (csize_bytes), 1, fp) != 1)
71 ( (mp_size_t) csize_bytes[0] << 24)
72 + ((mp_size_t) csize_bytes[1] << 16)
73 + ((mp_size_t) csize_bytes[2] << 8)
74 + ((mp_size_t) csize_bytes[3]);
76 /* Sign extend if necessary.
77 Could write "csize -= ((csize & 0x80000000L) << 1)", but that tickles a
78 bug in gcc 3.0 for powerpc64 on AIX. */
79 if (sizeof (csize) > 4 && csize & 0x80000000L)
80 csize -= 0x80000000L << 1;
82 abs_csize = ABS (csize);
84 /* round up to a multiple of limbs */
85 abs_xsize = (abs_csize*8 + GMP_NUMB_BITS-1) / GMP_NUMB_BITS;
89 MPZ_REALLOC (x, abs_xsize);
92 /* Get limb boundaries right in the read, for the benefit of the
95 cp = (char *) (xp + abs_xsize) - abs_csize;
96 if (fread (cp, abs_csize, 1, fp) != 1)
99 if (GMP_NAIL_BITS == 0)
101 /* Reverse limbs to least significant first, and byte swap. If
102 abs_xsize is odd then on the last iteration elimb and slimb are
103 the same. It doesn't seem extra code to handle that case
104 separately, to save an NTOH. */
106 ep = xp + abs_xsize-1;
107 for (i = 0; i < (abs_xsize+1)/2; i++)
109 NTOH_LIMB_FETCH (elimb, ep);
110 NTOH_LIMB_FETCH (slimb, sp);
117 /* It ought to be possible to do the transformation in-place, but
118 for now it's easier to use an extra temporary area. */
119 mp_limb_t byte, limb;
126 tp = TMP_ALLOC_LIMBS (abs_xsize);
130 for (i = abs_csize-1; i >= 0; i--)
132 byte = (unsigned char) cp[i];
133 limb |= (byte << bits);
135 if (bits >= GMP_NUMB_BITS)
137 ASSERT (tpos < abs_xsize);
138 tp[tpos++] = limb & GMP_NUMB_MASK;
139 bits -= GMP_NUMB_BITS;
141 limb = byte >> (8 - bits);
146 ASSERT (tpos < abs_xsize);
149 ASSERT (tpos == abs_xsize);
151 MPN_COPY (xp, tp, abs_xsize);
155 /* GMP 1.x mpz_out_raw wrote high zero bytes, strip any high zero
156 limbs resulting from this. Should be a non-zero value here, but
157 for safety don't assume that. */
158 MPN_NORMALIZE (xp, abs_xsize);
161 SIZ(x) = (csize >= 0 ? abs_xsize : -abs_xsize);
162 return abs_csize + 4;