summaryrefslogtreecommitdiff
path: root/printf/doprntf.c
blob: 9c0608b1ef19d8115237201ed430add0f978db53 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/* __gmp_doprnt_mpf -- mpf formatted output.

   THE FUNCTIONS IN THIS FILE ARE FOR INTERNAL USE ONLY.  THEY'RE ALMOST
   CERTAIN TO BE SUBJECT TO INCOMPATIBLE CHANGES OR DISAPPEAR COMPLETELY IN
   FUTURE GNU MP RELEASES.

Copyright 2001, 2002 Free Software Foundation, Inc.

This file is part of the GNU MP Library.

The GNU MP Library is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 3 of the License, or (at your
option) any later version.

The GNU MP Library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
License for more details.

You should have received a copy of the GNU Lesser General Public License
along with the GNU MP Library.  If not, see http://www.gnu.org/licenses/.  */

#include "config.h"

#if HAVE_STDARG
#include <stdarg.h>    /* for va_list and hence doprnt_funs_t */
#else
#include <varargs.h>
#endif

#include <ctype.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

#include "gmp.h"
#include "gmp-impl.h"


/* change this to "#define TRACE(x) x" for diagnostics */
#define TRACE(x) 


/* The separate of __gmp_doprnt_float_digits and __gmp_doprnt_float is so
   some C++ can do the mpf_get_str and release it in case of an exception */

#define DIGIT_VALUE(c)                  \
  (isdigit (c)   ? (c) - '0'            \
   : islower (c) ? (c) - 'a' + 10       \
   :               (c) - 'A' + 10)

int
__gmp_doprnt_mpf (const struct doprnt_funs_t *funs,
                  void *data,
                  const struct doprnt_params_t *p,
                  const char *point,
                  mpf_srcptr f)
{
  int         prec, ndigits, free_size, len, newlen, justify, justlen, explen;
  int         showbaselen, sign, signlen, intlen, intzeros, pointlen;
  int         fraczeros, fraclen, preczeros;
  char        *s, *free_ptr;
  mp_exp_t    exp;
  char        exponent[BITS_PER_MP_LIMB + 10];
  const char  *showbase;
  int         retval = 0;

  TRACE (printf ("__gmp_doprnt_float\n");
         printf ("  conv=%d prec=%d\n", p->conv, p->prec));

  prec = p->prec;
  if (prec <= -1)
    {
      /* all digits */
      ndigits = 0;

      /* arrange the fixed/scientific decision on a "prec" implied by how
         many significant digits there are */
      if (p->conv == DOPRNT_CONV_GENERAL)
        MPF_SIGNIFICANT_DIGITS (prec, PREC(f), ABS(p->base));
    }
  else
    {
      switch (p->conv) {
      case DOPRNT_CONV_FIXED:
        /* Precision is digits after the radix point.  Try not to generate
           too many more than will actually be required.  If f>=1 then
           overestimate the integer part, and add prec.  If f<1 then
           underestimate the zeros between the radix point and the first
           digit and subtract that from prec.  In either case add 2 so the
           round to nearest can be applied accurately.  */
        ndigits = prec + 2
          + EXP(f) * (__mp_bases[ABS(p->base)].chars_per_limb + (EXP(f)>=0));
        ndigits = MAX (ndigits, 1);
        break;

      case DOPRNT_CONV_SCIENTIFIC:
        /* precision is digits after the radix point, and there's one digit
           before */
        ndigits = prec + 1;
        break;

      default:
        ASSERT (0);
        /*FALLTHRU*/
        
      case DOPRNT_CONV_GENERAL:
        /* precision is total digits, but be sure to ask mpf_get_str for at
           least 1, not 0 */
        ndigits = MAX (prec, 1);
        break;
      }
    }
  TRACE (printf ("  ndigits %d\n", ndigits));

  s = mpf_get_str (NULL, &exp, p->base, ndigits, f);
  len = strlen (s);
  free_ptr = s;
  free_size = len + 1;
  TRACE (printf ("  s   %s\n", s);
         printf ("  exp %ld\n", exp);
         printf ("  len %d\n", len));

  /* For fixed mode check the ndigits formed above was in fact enough for
     the integer part plus p->prec after the radix point. */
  ASSERT ((p->conv == DOPRNT_CONV_FIXED && p->prec > -1)
          ? ndigits >= MAX (1, exp + p->prec + 2) : 1);

  sign = p->sign;
  if (s[0] == '-')
    {
      sign = s[0];
      s++, len--;
    }
  signlen = (sign != '\0');
  TRACE (printf ("  sign %c  signlen %d\n", sign, signlen));

  switch (p->conv) {
  case DOPRNT_CONV_FIXED:
    if (prec <= -1)
      prec = MAX (0, len-exp);   /* retain all digits */

    /* Truncate if necessary so fraction will be at most prec digits. */
    ASSERT (prec >= 0);
    newlen = exp + prec;
    if (newlen < 0)
      {
        /* first non-zero digit is below target prec, and at least one zero
           digit in between, so print zero */
        len = 0;
        exp = 0;
      }
    else if (len <= newlen)
      {
        /* already got few enough digits */
      }
    else
      {
        /* discard excess digits and round to nearest */

        const char  *num_to_text = (p->base >= 0
                                    ? "0123456789abcdefghijklmnopqrstuvwxyz"
                                    : "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ");
        int  base = ABS(p->base);
        int  n;

        ASSERT (base <= 36);

        len = newlen;
        n = DIGIT_VALUE (s[len]);
        TRACE (printf ("  rounding with %d\n", n));
        if (n >= (base + 1) / 2)
          {
            /* propagate a carry */
            for (;;)
              {
                if (len == 0)
                  {
                    s[0] = '1';
                    len = 1;
                    exp++;
                    break;
                  }
                n = DIGIT_VALUE (s[len-1]);
                ASSERT (n >= 0 && n < base);
                n++;
                if (n != base)
                  {
                    TRACE (printf ("  storing now %d\n", n));
                    s[len-1] = num_to_text[n];
                    break;
                  }
                len--;
              }
          }
        else
          {
            /* truncate only, strip any trailing zeros now exposed */
            while (len > 0 && s[len-1] == '0')
              len--;
          }

        /* Can have newlen==0, in which case the truncate was just to check
           for a carry turning it into "1".  If we're left with len==0 then
           adjust exp to match.  */
        if (len == 0)
          exp = 0;
      }  

  fixed:
    ASSERT (len == 0 ? exp == 0 : 1);
    if (exp <= 0)
      {
        TRACE (printf ("  fixed 0.000sss\n"));
        intlen = 0;
        intzeros = 1;
        fraczeros = -exp;
        fraclen = len;
      }
    else
      {
        TRACE (printf ("  fixed sss.sss or sss000\n"));
        intlen = MIN (len, exp);
        intzeros = exp - intlen;
        fraczeros = 0;
        fraclen = len - intlen;
      }
    explen = 0;
    break;

  case DOPRNT_CONV_SCIENTIFIC:
    {
      int   expval;
      char  expsign;

      if (prec <= -1)
        prec = MAX (0, len-1);   /* retain all digits */

    scientific:
      TRACE (printf ("  scientific s.sss\n"));

      intlen = MIN (1, len);
      intzeros = (intlen == 0 ? 1 : 0);
      fraczeros = 0;
      fraclen = len - intlen;

      expval = (exp-intlen);
      if (p->exptimes4)
        expval <<= 2;

      /* Split out the sign since %o or %x in expfmt give negatives as twos
         complement, not with a sign. */
      expsign = (expval >= 0 ? '+' : '-');
      expval = ABS (expval);

#if HAVE_VSNPRINTF
      explen = snprintf (exponent, sizeof(exponent),
                         p->expfmt, expsign, expval);
      /* test for < sizeof-1 since a glibc 2.0.x return of sizeof-1 might
         mean truncation */
      ASSERT (explen >= 0 && explen < sizeof(exponent)-1);
#else
      sprintf (exponent, p->expfmt, expsign, expval);
      explen = strlen (exponent);
      ASSERT (explen < sizeof(exponent));
#endif
      TRACE (printf ("  expfmt %s gives %s\n", p->expfmt, exponent));
    }
    break;

  default:
    ASSERT (0);
    /*FALLTHRU*/  /* to stop variables looking uninitialized */

  case DOPRNT_CONV_GENERAL:
    /* The exponent for "scientific" will be exp-1, choose scientific if
       this is < -4 or >= prec (and minimum 1 for prec).  For f==0 will have
       exp==0 and get the desired "fixed".  This rule follows glibc.  For
       fixed there's no need to truncate, the desired ndigits will already
       be as required.  */
    if (exp-1 < -4 || exp-1 >= MAX (1, prec))
      goto scientific;
    else
      goto fixed;
  }

  TRACE (printf ("  intlen %d intzeros %d fraczeros %d fraclen %d\n",
                 intlen, intzeros, fraczeros, fraclen));
  ASSERT (p->prec <= -1
          ? intlen + fraclen == strlen (s)
          : intlen + fraclen <= strlen (s));

  if (p->showtrailing)
    {
      /* Pad to requested precision with trailing zeros, for general this is
         all digits, for fixed and scientific just the fraction.  */
      preczeros = prec - (fraczeros + fraclen
                          + (p->conv == DOPRNT_CONV_GENERAL
                             ? intlen + intzeros : 0));
      preczeros = MAX (0, preczeros);
    }
  else
    preczeros = 0;
  TRACE (printf ("  prec=%d showtrailing=%d, pad with preczeros %d\n",
                 prec, p->showtrailing, preczeros));

  /* radix point if needed, or if forced */
  pointlen = ((fraczeros + fraclen + preczeros) != 0 || p->showpoint != 0)
    ? strlen (point) : 0;
  TRACE (printf ("  point |%s|  pointlen %d\n", point, pointlen));

  /* Notice the test for a non-zero value is done after any truncation for
     DOPRNT_CONV_FIXED. */
  showbase = NULL;
  showbaselen = 0;
  switch (p->showbase) {
  default:
    ASSERT (0);
    /*FALLTHRU*/
  case DOPRNT_SHOWBASE_NO:
    break;
  case DOPRNT_SHOWBASE_NONZERO:
    if (intlen == 0 && fraclen == 0)
      break;
    /*FALLTHRU*/
  case DOPRNT_SHOWBASE_YES:
    switch (p->base) {
    case 16:  showbase = "0x"; showbaselen = 2; break;
    case -16: showbase = "0X"; showbaselen = 2; break;
    case 8:   showbase = "0";  showbaselen = 1; break;
    }
    break;
  }
  TRACE (printf ("  showbase %s showbaselen %d\n",
                 showbase == NULL ? "" : showbase, showbaselen));

  /* left over field width */
  justlen = p->width - (signlen + showbaselen + intlen + intzeros + pointlen
                        + fraczeros + fraclen + preczeros + explen);
  TRACE (printf ("  justlen %d fill 0x%X\n", justlen, p->fill));

  justify = p->justify;
  if (justlen <= 0) /* no justifying if exceed width */
    justify = DOPRNT_JUSTIFY_NONE;

  TRACE (printf ("  justify type %d  intlen %d pointlen %d fraclen %d\n",
                 justify, intlen, pointlen, fraclen));

  if (justify == DOPRNT_JUSTIFY_RIGHT)         /* pad for right */
    DOPRNT_REPS (p->fill, justlen);

  if (signlen)                                 /* sign */
    DOPRNT_REPS (sign, 1);

  DOPRNT_MEMORY_MAYBE (showbase, showbaselen); /* base */

  if (justify == DOPRNT_JUSTIFY_INTERNAL)      /* pad for internal */
    DOPRNT_REPS (p->fill, justlen);

  DOPRNT_MEMORY (s, intlen);                   /* integer */
  DOPRNT_REPS_MAYBE ('0', intzeros);

  DOPRNT_MEMORY_MAYBE (point, pointlen);       /* point */

  DOPRNT_REPS_MAYBE ('0', fraczeros);          /* frac */
  DOPRNT_MEMORY_MAYBE (s+intlen, fraclen);

  DOPRNT_REPS_MAYBE ('0', preczeros);          /* prec */

  DOPRNT_MEMORY_MAYBE (exponent, explen);      /* exp */

  if (justify == DOPRNT_JUSTIFY_LEFT)          /* pad for left */         
    DOPRNT_REPS (p->fill, justlen);

 done:
  (*__gmp_free_func) (free_ptr, free_size);
  return retval;

 error:
  retval = -1;
  goto done;
}