1 /* mpz_out_raw -- Output a mpz_t in binary. Use an endianess and word size
4 Copyright (C) 1995 Free Software Foundation, Inc.
6 This file is part of the GNU MP Library.
8 The GNU MP Library is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Library General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or (at your
11 option) any later version.
13 The GNU MP Library is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
16 License for more details.
18 You should have received a copy of the GNU Library General Public License
19 along with the GNU MP Library; see the file COPYING.LIB. If not, write to
20 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
21 MA 02111-1307, USA. */
30 mpz_out_raw (FILE *stream, mpz_srcptr x)
32 mpz_out_raw (stream, x)
39 mp_size_t xsize = ABS (x->_mp_size);
40 mp_srcptr xp = x->_mp_d;
41 mp_size_t out_bytesize;
43 int n_bytes_in_hi_limb;
50 for (i = 4 - 1; i >= 0; i--)
52 return ferror (stream) ? 0 : 4;
55 hi_limb = xp[xsize - 1];
56 for (i = BYTES_PER_MP_LIMB - 1; i > 0; i--)
58 if ((hi_limb >> i * BITS_PER_CHAR) != 0)
61 n_bytes_in_hi_limb = i + 1;
62 out_bytesize = BYTES_PER_MP_LIMB * (xsize - 1) + n_bytes_in_hi_limb;
64 out_bytesize = -out_bytesize;
66 /* Make the size 4 bytes on all machines, to make the format portable. */
67 for (i = 4 - 1; i >= 0; i--)
68 fputc ((out_bytesize >> (i * BITS_PER_CHAR)) % (1 << BITS_PER_CHAR),
71 /* Output from the most significant limb to the least significant limb,
72 with each limb also output in decreasing significance order. */
74 /* Output the most significant limb separately, since we will only
75 output some of its bytes. */
76 for (i = n_bytes_in_hi_limb - 1; i >= 0; i--)
77 fputc ((hi_limb >> (i * BITS_PER_CHAR)) % (1 << BITS_PER_CHAR), stream);
79 /* Output the remaining limbs. */
80 for (s = xsize - 2; s >= 0; s--)
85 for (i = BYTES_PER_MP_LIMB - 1; i >= 0; i--)
86 fputc ((x_limb >> (i * BITS_PER_CHAR)) % (1 << BITS_PER_CHAR), stream);
88 return ferror (stream) ? 0 : ABS (out_bytesize) + 4;