1 /* mpz_out_raw -- write an mpz_t in raw format.
3 Copyright 2001, 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 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/. */
37 /* HTON_LIMB_STORE takes a normal host byte order limb and stores it as
38 network byte order (ie. big endian). */
40 #if HAVE_LIMB_BIG_ENDIAN
41 #define HTON_LIMB_STORE(dst, limb) do { *(dst) = (limb); } while (0)
44 #if HAVE_LIMB_LITTLE_ENDIAN
45 #define HTON_LIMB_STORE(dst, limb) BSWAP_LIMB_STORE (dst, limb)
48 #ifndef HTON_LIMB_STORE
49 #define HTON_LIMB_STORE(dst, limb) \
51 mp_limb_t __limb = (limb); \
52 char *__p = (char *) (dst); \
54 for (__i = 0; __i < GMP_LIMB_BYTES; __i++) \
55 __p[__i] = (char) (__limb >> ((GMP_LIMB_BYTES-1 - __i) * 8)); \
61 mpz_out_raw (FILE *fp
, mpz_srcptr x
)
63 mp_size_t xsize
, abs_xsize
, bytes
, i
;
71 abs_xsize
= ABS (xsize
);
72 bytes
= (abs_xsize
* GMP_NUMB_BITS
+ 7) / 8;
73 tsize
= ROUND_UP_MULTIPLE ((unsigned) 4, GMP_LIMB_BYTES
) + bytes
;
75 tp
= __GMP_ALLOCATE_FUNC_TYPE (tsize
, char);
76 bp
= tp
+ ROUND_UP_MULTIPLE ((unsigned) 4, GMP_LIMB_BYTES
);
84 if (GMP_NAIL_BITS
== 0)
86 /* reverse limb order, and byte swap if necessary */
88 _Pragma ("_CRI ivdep");
94 HTON_LIMB_STORE ((mp_ptr
) bp
, xlimb
);
99 /* strip high zero bytes (without fetching from bp) */
100 count_leading_zeros (zeros
, xlimb
);
109 ASSERT_CODE (char *bp_orig
= bp
- bytes
);
111 ASSERT_ALWAYS (GMP_NUMB_BITS
>= 8);
119 ASSERT (bp
> bp_orig
);
120 *--bp
= xlimb
& 0xFF;
130 ASSERT (bp
> bp_orig
);
131 *--bp
= (xlimb
| (new_xlimb
<< bits
)) & 0xFF;
132 xlimb
= new_xlimb
>> (8 - bits
);
133 bits
+= GMP_NUMB_BITS
- 8;
138 ASSERT (bp
> bp_orig
);
142 ASSERT (bp
== bp_orig
);
151 /* total bytes to be written */
154 /* twos complement negative for the size value */
155 bytes
= (xsize
>= 0 ? bytes
: -bytes
);
157 /* so we don't rely on sign extension in ">>" */
158 ASSERT_ALWAYS (sizeof (bytes
) >= 4);
160 bp
[-4] = bytes
>> 24;
161 bp
[-3] = bytes
>> 16;
168 if (fwrite (bp
, ssize
, 1, fp
) != 1)
171 (*__gmp_free_func
) (tp
, tsize
);