Initial import from FreeBSD RELENG_4:
[dragonfly.git] / contrib / libgmp / mpbsd / mout.c
1 /* mout(MINT) -- Do decimal output of MINT to standard output.
2
3 Copyright (C) 1991, 1994, 1996 Free Software Foundation, Inc.
4
5 This file is part of the GNU MP Library.
6
7 The GNU MP Library is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Library General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or (at your
10 option) any later version.
11
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 Library General Public
15 License for more details.
16
17 You should have received a copy of the GNU Library General Public License
18 along with the GNU MP Library; see the file COPYING.LIB.  If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20 MA 02111-1307, USA. */
21
22 #include <stdio.h>
23 #include "mp.h"
24 #include "gmp.h"
25 #include "gmp-impl.h"
26
27 void
28 #if __STDC__
29 mout (const MINT *x)
30 #else
31 mout (x)
32      const MINT *x;
33 #endif
34 {
35   mp_ptr xp;
36   mp_size_t x_size = x->_mp_size;
37   unsigned char *str;
38   size_t str_size;
39   char *num_to_text;
40   int i;
41   TMP_DECL (marker);
42
43   if (x_size == 0)
44     {
45       fputc ('0', stdout);
46       return;
47     }
48   if (x_size < 0)
49     {
50       fputc ('-', stdout);
51       x_size = -x_size;
52     }
53
54   TMP_MARK (marker);
55   str_size = ((size_t) (x_size * BITS_PER_MP_LIMB
56                         * __mp_bases[10].chars_per_bit_exactly)) + 3;
57   str = (unsigned char *) TMP_ALLOC (str_size);
58
59   /* Move the number to convert into temporary space, since mpn_get_str
60      clobbers its argument + needs one extra high limb....  */
61   xp = (mp_ptr) TMP_ALLOC ((x_size + 1) * BYTES_PER_MP_LIMB);
62   MPN_COPY (xp, x->_mp_d, x_size);
63
64   str_size = mpn_get_str (str, 10, xp, x_size);
65
66   /* mpn_get_str might make some leading zeros.  Skip them.  */
67   while (*str == 0)
68     {
69       str_size--;
70       str++;
71     }
72
73   /* Translate to printable chars.  */
74   for (i = 0; i < str_size; i++)
75     str[i] = "0123456789"[str[i]];
76   str[str_size] = 0;
77
78   str_size = strlen (str);
79   if (str_size % 10 != 0)
80     {
81       fwrite (str, 1, str_size % 10, stdout);
82       str += str_size % 10;
83       str_size -= str_size % 10;
84       if (str_size != 0)
85         fputc (' ', stdout);
86     }
87   for (i = 0; i < str_size; i += 10)
88     {
89       fwrite (str, 1, 10, stdout);
90       str += 10;
91       if (i + 10 < str_size)
92         fputc (' ', stdout);
93     }
94   fputc ('\n', stdout);
95   TMP_FREE (marker);
96 }