Sync with experimental
[luatex.git] / source / libs / gmp / gmp-src / mpz / inp_raw.c
blobd95c19ede725ebb63e84a44c72e1b1f27e64722a
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
18 later version.
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
25 for more details.
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>
32 #include "gmp.h"
33 #include "gmp-impl.h"
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)
41 #endif
43 #if HAVE_LIMB_LITTLE_ENDIAN
44 #define NTOH_LIMB_FETCH(limb, src) BSWAP_LIMB_FETCH (limb, src)
45 #endif
47 #ifndef NTOH_LIMB_FETCH
48 #define NTOH_LIMB_FETCH(limb, src) \
49 do { \
50 const unsigned char *__p = (const unsigned char *) (src); \
51 mp_limb_t __limb; \
52 int __i; \
53 __limb = 0; \
54 for (__i = 0; __i < GMP_LIMB_BYTES; __i++) \
55 __limb = (__limb << 8) | __p[__i]; \
56 (limb) = __limb; \
57 } while (0)
58 #endif
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. */
64 size_t
65 mpz_inp_raw (mpz_ptr x, FILE *fp)
67 unsigned char csize_bytes[4];
68 mp_size_t csize, abs_xsize, i;
69 size_t abs_csize;
70 char *cp;
71 mp_ptr xp, sp, ep;
72 mp_limb_t slimb, elimb;
74 if (fp == 0)
75 fp = stdin;
77 /* 4 bytes for size */
78 if (fread (csize_bytes, sizeof (csize_bytes), 1, fp) != 1)
79 return 0;
81 csize =
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);
98 if (abs_xsize != 0)
100 xp = MPZ_NEWALLOC (x, abs_xsize);
102 /* Get limb boundaries right in the read, for the benefit of the
103 non-nails case. */
104 xp[0] = 0;
105 cp = (char *) (xp + abs_xsize) - abs_csize;
106 if (fread (cp, abs_csize, 1, fp) != 1)
107 return 0;
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. */
115 sp = xp;
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);
121 *sp++ = elimb;
122 *ep-- = slimb;
125 else
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;
130 int bits;
131 mp_size_t tpos;
132 mp_ptr tp;
133 TMP_DECL;
135 TMP_MARK;
136 tp = TMP_ALLOC_LIMBS (abs_xsize);
137 limb = 0;
138 bits = 0;
139 tpos = 0;
140 for (i = abs_csize-1; i >= 0; i--)
142 byte = (unsigned char) cp[i];
143 limb |= (byte << bits);
144 bits += 8;
145 if (bits >= GMP_NUMB_BITS)
147 ASSERT (tpos < abs_xsize);
148 tp[tpos++] = limb & GMP_NUMB_MASK;
149 bits -= GMP_NUMB_BITS;
150 ASSERT (bits < 8);
151 limb = byte >> (8 - bits);
154 if (bits != 0)
156 ASSERT (tpos < abs_xsize);
157 tp[tpos++] = limb;
159 ASSERT (tpos == abs_xsize);
161 MPN_COPY (xp, tp, abs_xsize);
162 TMP_FREE;
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;