1 /* mpz_inp_raw -- read an mpz_t in raw format.
3 Copyright 2001, 2002, 2005, 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/. */
36 /* NTOH_LIMB_FETCH fetches a limb which is in network byte order (ie. big
37 endian) and produces a normal host byte order result. */
39 #if HAVE_LIMB_BIG_ENDIAN
40 #define NTOH_LIMB_FETCH(limb, src) do { (limb) = *(src); } while (0)
43 #if HAVE_LIMB_LITTLE_ENDIAN
44 #define NTOH_LIMB_FETCH(limb, src) BSWAP_LIMB_FETCH (limb, src)
47 #ifndef NTOH_LIMB_FETCH
48 #define NTOH_LIMB_FETCH(limb, src) \
50 const unsigned char *__p = (const unsigned char *) (src); \
54 for (__i = 0; __i < GMP_LIMB_BYTES; __i++) \
55 __limb = (__limb << 8) | __p[__i]; \
61 /* Enhancement: The byte swap loop ought to be safe to vectorize on Cray
62 etc, but someone who knows what they're doing needs to check it. */
65 mpz_inp_raw (mpz_ptr x
, FILE *fp
)
67 unsigned char csize_bytes
[4];
68 mp_size_t csize
, abs_xsize
, i
;
72 mp_limb_t slimb
, elimb
;
77 /* 4 bytes for size */
78 if (fread (csize_bytes
, sizeof (csize_bytes
), 1, fp
) != 1)
82 ( (mp_size_t
) csize_bytes
[0] << 24)
83 + ((mp_size_t
) csize_bytes
[1] << 16)
84 + ((mp_size_t
) csize_bytes
[2] << 8)
85 + ((mp_size_t
) csize_bytes
[3]);
87 /* Sign extend if necessary.
88 Could write "csize -= ((csize & 0x80000000L) << 1)", but that tickles a
89 bug in gcc 3.0 for powerpc64 on AIX. */
90 if (sizeof (csize
) > 4 && csize
& 0x80000000L
)
91 csize
-= 0x80000000L
<< 1;
93 abs_csize
= ABS (csize
);
95 /* round up to a multiple of limbs */
96 abs_xsize
= BITS_TO_LIMBS (abs_csize
*8);
100 xp
= MPZ_NEWALLOC (x
, abs_xsize
);
102 /* Get limb boundaries right in the read, for the benefit of the
105 cp
= (char *) (xp
+ abs_xsize
) - abs_csize
;
106 if (fread (cp
, abs_csize
, 1, fp
) != 1)
109 if (GMP_NAIL_BITS
== 0)
111 /* Reverse limbs to least significant first, and byte swap. If
112 abs_xsize is odd then on the last iteration elimb and slimb are
113 the same. It doesn't seem extra code to handle that case
114 separately, to save an NTOH. */
116 ep
= xp
+ abs_xsize
-1;
117 for (i
= 0; i
< (abs_xsize
+1)/2; i
++)
119 NTOH_LIMB_FETCH (elimb
, ep
);
120 NTOH_LIMB_FETCH (slimb
, sp
);
127 /* It ought to be possible to do the transformation in-place, but
128 for now it's easier to use an extra temporary area. */
129 mp_limb_t byte
, limb
;
136 tp
= TMP_ALLOC_LIMBS (abs_xsize
);
140 for (i
= abs_csize
-1; i
>= 0; i
--)
142 byte
= (unsigned char) cp
[i
];
143 limb
|= (byte
<< bits
);
145 if (bits
>= GMP_NUMB_BITS
)
147 ASSERT (tpos
< abs_xsize
);
148 tp
[tpos
++] = limb
& GMP_NUMB_MASK
;
149 bits
-= GMP_NUMB_BITS
;
151 limb
= byte
>> (8 - bits
);
156 ASSERT (tpos
< abs_xsize
);
159 ASSERT (tpos
== abs_xsize
);
161 MPN_COPY (xp
, tp
, abs_xsize
);
165 /* GMP 1.x mpz_out_raw wrote high zero bytes, strip any high zero
166 limbs resulting from this. Should be a non-zero value here, but
167 for safety don't assume that. */
168 MPN_NORMALIZE (xp
, abs_xsize
);
171 SIZ(x
) = (csize
>= 0 ? abs_xsize
: -abs_xsize
);
172 return abs_csize
+ 4;