1 /* Floating point output for `printf'.
2 Copyright (C) 1995-2015 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
5 Written by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1995.
7 The GNU C Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
12 The GNU C Library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public
18 License along with the GNU C Library; if not, see
19 <http://www.gnu.org/licenses/>. */
21 /* The gmp headers need some configuration frobs. */
28 #include <gmp-mparam.h>
31 #include <stdlib/gmp-impl.h>
32 #include <stdlib/longlong.h>
33 #include <stdlib/fpioconst.h>
34 #include <locale/localeinfo.h>
43 #include <rounding-mode.h>
45 #ifdef COMPILE_WPRINTF
46 # define CHAR_T wchar_t
51 #include "_i18n_number.h"
54 # define NDEBUG /* Undefine this for debugging assertions. */
58 /* This defines make it possible to use the same code for GNU C library and
59 the GNU I/O library. */
60 #define PUT(f, s, n) _IO_sputn (f, s, n)
61 #define PAD(f, c, n) (wide ? _IO_wpadn (f, c, n) : _IO_padn (f, c, n))
62 /* We use this file GNU C library and GNU I/O library. So make
65 #define putc(c, f) (wide \
66 ? (int)_IO_putwc_unlocked (c, f) : _IO_putc_unlocked (c, f))
67 #define size_t _IO_size_t
70 /* Macros for doing the actual output. */
75 const int outc = (ch); \
76 if (putc (outc, fp) == EOF) \
78 if (buffer_malloced) \
85 #define PRINT(ptr, wptr, len) \
88 size_t outlen = (len); \
91 if (PUT (fp, wide ? (const char *) wptr : ptr, outlen) != outlen) \
93 if (buffer_malloced) \
103 while (outlen-- > 0) \
106 while (outlen-- > 0) \
111 #define PADN(ch, len) \
114 if (PAD (fp, ch, len) != len) \
116 if (buffer_malloced) \
124 /* We use the GNU MP library to handle large numbers.
126 An MP variable occupies a varying number of entries in its array. We keep
127 track of this number for efficiency reasons. Otherwise we would always
128 have to process the whole array. */
129 #define MPN_VAR(name) mp_limb_t *name; mp_size_t name##size
131 #define MPN_ASSIGN(dst,src) \
132 memcpy (dst, src, (dst##size = src##size) * sizeof (mp_limb_t))
133 #define MPN_GE(u,v) \
134 (u##size > v##size || (u##size == v##size && __mpn_cmp (u, v, u##size) >= 0))
136 extern mp_size_t
__mpn_extract_double (mp_ptr res_ptr
, mp_size_t size
,
137 int *expt
, int *is_neg
,
139 extern mp_size_t
__mpn_extract_long_double (mp_ptr res_ptr
, mp_size_t size
,
140 int *expt
, int *is_neg
,
142 extern unsigned int __guess_grouping (unsigned int intdig_max
,
143 const char *grouping
);
146 static wchar_t *group_number (wchar_t *buf
, wchar_t *bufend
,
147 unsigned int intdig_no
, const char *grouping
,
148 wchar_t thousands_sep
, int ngroups
)
151 struct hack_digit_param
153 /* Sign of the exponent. */
155 /* The type of output format that will be used: 'e'/'E' or 'f'. */
157 /* and the exponent. */
159 /* The fraction of the floting-point value in question */
161 /* Scaling factor. */
163 /* Temporary bignum value. */
168 hack_digit (struct hack_digit_param
*p
)
172 if (p
->expsign
!= 0 && p
->type
== 'f' && p
->exponent
-- > 0)
174 else if (p
->scalesize
== 0)
176 hi
= p
->frac
[p
->fracsize
- 1];
177 p
->frac
[p
->fracsize
- 1] = __mpn_mul_1 (p
->frac
, p
->frac
,
178 p
->fracsize
- 1, 10);
182 if (p
->fracsize
< p
->scalesize
)
186 hi
= mpn_divmod (p
->tmp
, p
->frac
, p
->fracsize
,
187 p
->scale
, p
->scalesize
);
188 p
->tmp
[p
->fracsize
- p
->scalesize
] = hi
;
191 p
->fracsize
= p
->scalesize
;
192 while (p
->fracsize
!= 0 && p
->frac
[p
->fracsize
- 1] == 0)
194 if (p
->fracsize
== 0)
196 /* We're not prepared for an mpn variable with zero
203 mp_limb_t _cy
= __mpn_mul_1 (p
->frac
, p
->frac
, p
->fracsize
, 10);
205 p
->frac
[p
->fracsize
++] = _cy
;
212 ___printf_fp (FILE *fp
,
213 const struct printf_info
*info
,
214 const void *const *args
)
216 /* The floating-point value to output. */
220 __long_double_t ldbl
;
224 /* Locale-dependent representation of decimal point. */
228 /* Locale-dependent thousands separator and grouping specification. */
229 const char *thousands_sep
= NULL
;
230 wchar_t thousands_sepwc
= 0;
231 const char *grouping
;
233 /* "NaN" or "Inf" for the special cases. */
234 const char *special
= NULL
;
235 const wchar_t *wspecial
= NULL
;
237 /* We need just a few limbs for the input before shifting to the right
239 mp_limb_t fp_input
[(LDBL_MANT_DIG
+ BITS_PER_MP_LIMB
- 1) / BITS_PER_MP_LIMB
];
240 /* We need to shift the contents of fp_input by this amount of bits. */
243 struct hack_digit_param p
;
244 /* Sign of float number. */
247 /* Counter for number of written characters. */
250 /* General helper (carry limb). */
253 /* Nonzero if this is output on a wide character stream. */
254 int wide
= info
->wide
;
256 /* Buffer in which we produce the output. */
257 wchar_t *wbuffer
= NULL
;
258 /* Flag whether wbuffer is malloc'ed or not. */
259 int buffer_malloced
= 0;
263 /* Figure out the decimal point character. */
264 if (info
->extra
== 0)
266 decimal
= _NL_CURRENT (LC_NUMERIC
, DECIMAL_POINT
);
267 decimalwc
= _NL_CURRENT_WORD (LC_NUMERIC
, _NL_NUMERIC_DECIMAL_POINT_WC
);
271 decimal
= _NL_CURRENT (LC_MONETARY
, MON_DECIMAL_POINT
);
272 if (*decimal
== '\0')
273 decimal
= _NL_CURRENT (LC_NUMERIC
, DECIMAL_POINT
);
274 decimalwc
= _NL_CURRENT_WORD (LC_MONETARY
,
275 _NL_MONETARY_DECIMAL_POINT_WC
);
276 if (decimalwc
== L
'\0')
277 decimalwc
= _NL_CURRENT_WORD (LC_NUMERIC
,
278 _NL_NUMERIC_DECIMAL_POINT_WC
);
280 /* The decimal point character must not be zero. */
281 assert (*decimal
!= '\0');
282 assert (decimalwc
!= L
'\0');
286 if (info
->extra
== 0)
287 grouping
= _NL_CURRENT (LC_NUMERIC
, GROUPING
);
289 grouping
= _NL_CURRENT (LC_MONETARY
, MON_GROUPING
);
291 if (*grouping
<= 0 || *grouping
== CHAR_MAX
)
295 /* Figure out the thousands separator character. */
298 if (info
->extra
== 0)
300 _NL_CURRENT_WORD (LC_NUMERIC
, _NL_NUMERIC_THOUSANDS_SEP_WC
);
303 _NL_CURRENT_WORD (LC_MONETARY
,
304 _NL_MONETARY_THOUSANDS_SEP_WC
);
308 if (info
->extra
== 0)
309 thousands_sep
= _NL_CURRENT (LC_NUMERIC
, THOUSANDS_SEP
);
311 thousands_sep
= _NL_CURRENT (LC_MONETARY
, MON_THOUSANDS_SEP
);
314 if ((wide
&& thousands_sepwc
== L
'\0')
315 || (! wide
&& *thousands_sep
== '\0'))
317 else if (thousands_sepwc
== L
'\0')
318 /* If we are printing multibyte characters and there is a
319 multibyte representation for the thousands separator,
320 we must ensure the wide character thousands separator
321 is available, even if it is fake. */
322 thousands_sepwc
= 0xfffffffe;
328 /* Fetch the argument value. */
329 #ifndef __NO_LONG_DOUBLE_MATH
330 if (info
->is_long_double
&& sizeof (long double) > sizeof (double))
332 fpnum
.ldbl
= *(const long double *) args
[0];
334 /* Check for special values: not a number or infinity. */
335 if (isnan (fpnum
.ldbl
))
337 is_neg
= signbit (fpnum
.ldbl
);
338 if (isupper (info
->spec
))
349 else if (isinf (fpnum
.ldbl
))
351 is_neg
= signbit (fpnum
.ldbl
);
352 if (isupper (info
->spec
))
365 p
.fracsize
= __mpn_extract_long_double (fp_input
,
367 sizeof (fp_input
[0])),
368 &p
.exponent
, &is_neg
,
370 to_shift
= 1 + p
.fracsize
* BITS_PER_MP_LIMB
- LDBL_MANT_DIG
;
374 #endif /* no long double */
376 fpnum
.dbl
= *(const double *) args
[0];
378 /* Check for special values: not a number or infinity. */
379 if (isnan (fpnum
.dbl
))
381 is_neg
= signbit (fpnum
.dbl
);
382 if (isupper (info
->spec
))
393 else if (isinf (fpnum
.dbl
))
395 is_neg
= signbit (fpnum
.dbl
);
396 if (isupper (info
->spec
))
409 p
.fracsize
= __mpn_extract_double (fp_input
,
411 / sizeof (fp_input
[0])),
412 &p
.exponent
, &is_neg
, fpnum
.dbl
);
413 to_shift
= 1 + p
.fracsize
* BITS_PER_MP_LIMB
- DBL_MANT_DIG
;
419 int width
= info
->width
;
421 if (is_neg
|| info
->showsign
|| info
->space
)
425 if (!info
->left
&& width
> 0)
430 else if (info
->showsign
)
432 else if (info
->space
)
435 PRINT (special
, wspecial
, 3);
437 if (info
->left
&& width
> 0)
444 /* We need three multiprecision variables. Now that we have the p.exponent
445 of the number we can allocate the needed memory. It would be more
446 efficient to use variables of the fixed maximum size but because this
447 would be really big it could lead to memory problems. */
449 mp_size_t bignum_size
= ((abs (p
.exponent
) + BITS_PER_MP_LIMB
- 1)
451 + (LDBL_MANT_DIG
/ BITS_PER_MP_LIMB
> 2 ? 8 : 4))
452 * sizeof (mp_limb_t
);
453 p
.frac
= (mp_limb_t
*) alloca (bignum_size
);
454 p
.tmp
= (mp_limb_t
*) alloca (bignum_size
);
455 p
.scale
= (mp_limb_t
*) alloca (bignum_size
);
458 /* We now have to distinguish between numbers with positive and negative
459 exponents because the method used for the one is not applicable/efficient
466 int explog
= LDBL_MAX_10_EXP_LOG
;
468 const struct mp_power
*powers
= &_fpioconst_pow10
[explog
+ 1];
471 if ((p
.exponent
+ to_shift
) % BITS_PER_MP_LIMB
== 0)
473 MPN_COPY_DECR (p
.frac
+ (p
.exponent
+ to_shift
) / BITS_PER_MP_LIMB
,
474 fp_input
, p
.fracsize
);
475 p
.fracsize
+= (p
.exponent
+ to_shift
) / BITS_PER_MP_LIMB
;
479 cy
= __mpn_lshift (p
.frac
+
480 (p
.exponent
+ to_shift
) / BITS_PER_MP_LIMB
,
481 fp_input
, p
.fracsize
,
482 (p
.exponent
+ to_shift
) % BITS_PER_MP_LIMB
);
483 p
.fracsize
+= (p
.exponent
+ to_shift
) / BITS_PER_MP_LIMB
;
485 p
.frac
[p
.fracsize
++] = cy
;
487 MPN_ZERO (p
.frac
, (p
.exponent
+ to_shift
) / BITS_PER_MP_LIMB
);
489 assert (powers
> &_fpioconst_pow10
[0]);
494 /* The number of the product of two binary numbers with n and m
495 bits respectively has m+n or m+n-1 bits. */
496 if (p
.exponent
>= scaleexpo
+ powers
->p_expo
- 1)
498 if (p
.scalesize
== 0)
500 #ifndef __NO_LONG_DOUBLE_MATH
501 if (LDBL_MANT_DIG
> _FPIO_CONST_OFFSET
* BITS_PER_MP_LIMB
502 && info
->is_long_double
)
504 #define _FPIO_CONST_SHIFT \
505 (((LDBL_MANT_DIG + BITS_PER_MP_LIMB - 1) / BITS_PER_MP_LIMB) \
506 - _FPIO_CONST_OFFSET)
507 /* 64bit const offset is not enough for
508 IEEE quad long double. */
509 p
.tmpsize
= powers
->arraysize
+ _FPIO_CONST_SHIFT
;
510 memcpy (p
.tmp
+ _FPIO_CONST_SHIFT
,
511 &__tens
[powers
->arrayoff
],
512 p
.tmpsize
* sizeof (mp_limb_t
));
513 MPN_ZERO (p
.tmp
, _FPIO_CONST_SHIFT
);
514 /* Adjust p.exponent, as scaleexpo will be this much
516 p
.exponent
+= _FPIO_CONST_SHIFT
* BITS_PER_MP_LIMB
;
521 p
.tmpsize
= powers
->arraysize
;
522 memcpy (p
.tmp
, &__tens
[powers
->arrayoff
],
523 p
.tmpsize
* sizeof (mp_limb_t
));
528 cy
= __mpn_mul (p
.tmp
, p
.scale
, p
.scalesize
,
529 &__tens
[powers
->arrayoff
530 + _FPIO_CONST_OFFSET
],
531 powers
->arraysize
- _FPIO_CONST_OFFSET
);
532 p
.tmpsize
= p
.scalesize
+
533 powers
->arraysize
- _FPIO_CONST_OFFSET
;
538 if (MPN_GE (p
.frac
, p
.tmp
))
541 MPN_ASSIGN (p
.scale
, p
.tmp
);
542 count_leading_zeros (cnt
, p
.scale
[p
.scalesize
- 1]);
543 scaleexpo
= (p
.scalesize
- 2) * BITS_PER_MP_LIMB
- cnt
- 1;
544 exp10
|= 1 << explog
;
549 while (powers
> &_fpioconst_pow10
[0]);
552 /* Optimize number representations. We want to represent the numbers
553 with the lowest number of bytes possible without losing any
554 bytes. Also the highest bit in the scaling factor has to be set
555 (this is a requirement of the MPN division routines). */
558 /* Determine minimum number of zero bits at the end of
560 for (i
= 0; p
.scale
[i
] == 0 && p
.frac
[i
] == 0; i
++)
563 /* Determine number of bits the scaling factor is misplaced. */
564 count_leading_zeros (cnt_h
, p
.scale
[p
.scalesize
- 1]);
568 /* The highest bit of the scaling factor is already set. So
569 we only have to remove the trailing empty limbs. */
572 MPN_COPY_INCR (p
.scale
, p
.scale
+ i
, p
.scalesize
- i
);
574 MPN_COPY_INCR (p
.frac
, p
.frac
+ i
, p
.fracsize
- i
);
582 count_trailing_zeros (cnt_l
, p
.scale
[i
]);
586 count_trailing_zeros (cnt_l2
, p
.frac
[i
]);
592 count_trailing_zeros (cnt_l
, p
.frac
[i
]);
594 /* Now shift the numbers to their optimal position. */
595 if (i
== 0 && BITS_PER_MP_LIMB
- cnt_h
> cnt_l
)
597 /* We cannot save any memory. So just roll both numbers
598 so that the scaling factor has its highest bit set. */
600 (void) __mpn_lshift (p
.scale
, p
.scale
, p
.scalesize
, cnt_h
);
601 cy
= __mpn_lshift (p
.frac
, p
.frac
, p
.fracsize
, cnt_h
);
603 p
.frac
[p
.fracsize
++] = cy
;
605 else if (BITS_PER_MP_LIMB
- cnt_h
<= cnt_l
)
607 /* We can save memory by removing the trailing zero limbs
608 and by packing the non-zero limbs which gain another
611 (void) __mpn_rshift (p
.scale
, p
.scale
+ i
, p
.scalesize
- i
,
612 BITS_PER_MP_LIMB
- cnt_h
);
613 p
.scalesize
-= i
+ 1;
614 (void) __mpn_rshift (p
.frac
, p
.frac
+ i
, p
.fracsize
- i
,
615 BITS_PER_MP_LIMB
- cnt_h
);
616 p
.fracsize
-= p
.frac
[p
.fracsize
- i
- 1] == 0 ? i
+ 1 : i
;
620 /* We can only save the memory of the limbs which are zero.
621 The non-zero parts occupy the same number of limbs. */
623 (void) __mpn_rshift (p
.scale
, p
.scale
+ (i
- 1),
624 p
.scalesize
- (i
- 1),
625 BITS_PER_MP_LIMB
- cnt_h
);
627 (void) __mpn_rshift (p
.frac
, p
.frac
+ (i
- 1),
628 p
.fracsize
- (i
- 1),
629 BITS_PER_MP_LIMB
- cnt_h
);
631 p
.frac
[p
.fracsize
- (i
- 1) - 1] == 0 ? i
: i
- 1;
636 else if (p
.exponent
< 0)
640 int explog
= LDBL_MAX_10_EXP_LOG
;
641 const struct mp_power
*powers
= &_fpioconst_pow10
[explog
+ 1];
643 /* Now shift the input value to its right place. */
644 cy
= __mpn_lshift (p
.frac
, fp_input
, p
.fracsize
, to_shift
);
645 p
.frac
[p
.fracsize
++] = cy
;
646 assert (cy
== 1 || (p
.frac
[p
.fracsize
- 2] == 0 && p
.frac
[0] == 0));
649 p
.exponent
= -p
.exponent
;
651 assert (powers
!= &_fpioconst_pow10
[0]);
656 if (p
.exponent
>= powers
->m_expo
)
658 int i
, incr
, cnt_h
, cnt_l
;
661 /* The __mpn_mul function expects the first argument to be
662 bigger than the second. */
663 if (p
.fracsize
< powers
->arraysize
- _FPIO_CONST_OFFSET
)
664 cy
= __mpn_mul (p
.tmp
, &__tens
[powers
->arrayoff
665 + _FPIO_CONST_OFFSET
],
666 powers
->arraysize
- _FPIO_CONST_OFFSET
,
669 cy
= __mpn_mul (p
.tmp
, p
.frac
, p
.fracsize
,
670 &__tens
[powers
->arrayoff
+ _FPIO_CONST_OFFSET
],
671 powers
->arraysize
- _FPIO_CONST_OFFSET
);
672 p
.tmpsize
= p
.fracsize
+ powers
->arraysize
- _FPIO_CONST_OFFSET
;
676 count_leading_zeros (cnt_h
, p
.tmp
[p
.tmpsize
- 1]);
677 incr
= (p
.tmpsize
- p
.fracsize
) * BITS_PER_MP_LIMB
678 + BITS_PER_MP_LIMB
- 1 - cnt_h
;
680 assert (incr
<= powers
->p_expo
);
682 /* If we increased the p.exponent by exactly 3 we have to test
683 for overflow. This is done by comparing with 10 shifted
684 to the right position. */
685 if (incr
== p
.exponent
+ 3)
687 if (cnt_h
<= BITS_PER_MP_LIMB
- 4)
691 = ((mp_limb_t
) 10) << (BITS_PER_MP_LIMB
- 4 - cnt_h
);
695 topval
[0] = ((mp_limb_t
) 10) << (BITS_PER_MP_LIMB
- 4);
697 (void) __mpn_lshift (topval
, topval
, 2,
698 BITS_PER_MP_LIMB
- cnt_h
);
702 /* We have to be careful when multiplying the last factor.
703 If the result is greater than 1.0 be have to test it
704 against 10.0. If it is greater or equal to 10.0 the
705 multiplication was not valid. This is because we cannot
706 determine the number of bits in the result in advance. */
707 if (incr
< p
.exponent
+ 3
708 || (incr
== p
.exponent
+ 3 &&
709 (p
.tmp
[p
.tmpsize
- 1] < topval
[1]
710 || (p
.tmp
[p
.tmpsize
- 1] == topval
[1]
711 && p
.tmp
[p
.tmpsize
- 2] < topval
[0]))))
713 /* The factor is right. Adapt binary and decimal
716 exp10
|= 1 << explog
;
718 /* If this factor yields a number greater or equal to
719 1.0, we must not shift the non-fractional digits down. */
721 cnt_h
+= -p
.exponent
;
723 /* Now we optimize the number representation. */
724 for (i
= 0; p
.tmp
[i
] == 0; ++i
);
725 if (cnt_h
== BITS_PER_MP_LIMB
- 1)
727 MPN_COPY (p
.frac
, p
.tmp
+ i
, p
.tmpsize
- i
);
728 p
.fracsize
= p
.tmpsize
- i
;
732 count_trailing_zeros (cnt_l
, p
.tmp
[i
]);
734 /* Now shift the numbers to their optimal position. */
735 if (i
== 0 && BITS_PER_MP_LIMB
- 1 - cnt_h
> cnt_l
)
737 /* We cannot save any memory. Just roll the
738 number so that the leading digit is in a
741 cy
= __mpn_lshift (p
.frac
, p
.tmp
, p
.tmpsize
,
743 p
.fracsize
= p
.tmpsize
+ 1;
744 p
.frac
[p
.fracsize
- 1] = cy
;
746 else if (BITS_PER_MP_LIMB
- 1 - cnt_h
<= cnt_l
)
748 (void) __mpn_rshift (p
.frac
, p
.tmp
+ i
, p
.tmpsize
- i
,
749 BITS_PER_MP_LIMB
- 1 - cnt_h
);
750 p
.fracsize
= p
.tmpsize
- i
;
754 /* We can only save the memory of the limbs which
755 are zero. The non-zero parts occupy the same
758 (void) __mpn_rshift (p
.frac
, p
.tmp
+ (i
- 1),
760 BITS_PER_MP_LIMB
- 1 - cnt_h
);
761 p
.fracsize
= p
.tmpsize
- (i
- 1);
768 while (powers
!= &_fpioconst_pow10
[1] && p
.exponent
> 0);
769 /* All factors but 10^-1 are tested now. */
774 cy
= __mpn_mul_1 (p
.tmp
, p
.frac
, p
.fracsize
, 10);
775 p
.tmpsize
= p
.fracsize
;
776 assert (cy
== 0 || p
.tmp
[p
.tmpsize
- 1] < 20);
778 count_trailing_zeros (cnt_l
, p
.tmp
[0]);
779 if (cnt_l
< MIN (4, p
.exponent
))
781 cy
= __mpn_lshift (p
.frac
, p
.tmp
, p
.tmpsize
,
782 BITS_PER_MP_LIMB
- MIN (4, p
.exponent
));
784 p
.frac
[p
.tmpsize
++] = cy
;
787 (void) __mpn_rshift (p
.frac
, p
.tmp
, p
.tmpsize
, MIN (4, p
.exponent
));
788 p
.fracsize
= p
.tmpsize
;
790 assert (p
.frac
[p
.fracsize
- 1] < 10);
796 /* This is a special case. We don't need a factor because the
797 numbers are in the range of 1.0 <= |fp| < 8.0. We simply
798 shift it to the right place and divide it by 1.0 to get the
799 leading digit. (Of course this division is not really made.) */
800 assert (0 <= p
.exponent
&& p
.exponent
< 3 &&
801 p
.exponent
+ to_shift
< BITS_PER_MP_LIMB
);
803 /* Now shift the input value to its right place. */
804 cy
= __mpn_lshift (p
.frac
, fp_input
, p
.fracsize
, (p
.exponent
+ to_shift
));
805 p
.frac
[p
.fracsize
++] = cy
;
810 int width
= info
->width
;
811 wchar_t *wstartp
, *wcp
;
814 int intdig_max
, intdig_no
= 0;
820 char spec
= _tolower (info
->spec
);
826 fracdig_min
= fracdig_max
= info
->prec
< 0 ? 6 : info
->prec
;
827 chars_needed
= 1 + 1 + (size_t) fracdig_max
+ 1 + 1 + 4;
828 /* d . ddd e +- ddd */
829 dig_max
= INT_MAX
; /* Unlimited. */
830 significant
= 1; /* Does not matter here. */
832 else if (spec
== 'f')
835 fracdig_min
= fracdig_max
= info
->prec
< 0 ? 6 : info
->prec
;
836 dig_max
= INT_MAX
; /* Unlimited. */
837 significant
= 1; /* Does not matter here. */
840 intdig_max
= p
.exponent
+ 1;
841 /* This can be really big! */ /* XXX Maybe malloc if too big? */
842 chars_needed
= (size_t) p
.exponent
+ 1 + 1 + (size_t) fracdig_max
;
847 chars_needed
= 1 + 1 + (size_t) fracdig_max
;
852 dig_max
= info
->prec
< 0 ? 6 : (info
->prec
== 0 ? 1 : info
->prec
);
853 if ((p
.expsign
== 0 && p
.exponent
>= dig_max
)
854 || (p
.expsign
!= 0 && p
.exponent
> 4))
856 if ('g' - 'G' == 'e' - 'E')
857 p
.type
= 'E' + (info
->spec
- 'G');
859 p
.type
= isupper (info
->spec
) ? 'E' : 'e';
860 fracdig_max
= dig_max
- 1;
862 chars_needed
= 1 + 1 + (size_t) fracdig_max
+ 1 + 1 + 4;
867 intdig_max
= p
.expsign
== 0 ? p
.exponent
+ 1 : 0;
868 fracdig_max
= dig_max
- intdig_max
;
869 /* We need space for the significant digits and perhaps
870 for leading zeros when < 1.0. The number of leading
871 zeros can be as many as would be required for
872 exponential notation with a negative two-digit
873 p.exponent, which is 4. */
874 chars_needed
= (size_t) dig_max
+ 1 + 4;
876 fracdig_min
= info
->alt
? fracdig_max
: 0;
877 significant
= 0; /* We count significant digits. */
882 /* Guess the number of groups we will make, and thus how
883 many spaces we need for separator characters. */
884 ngroups
= __guess_grouping (intdig_max
, grouping
);
885 /* Allocate one more character in case rounding increases the
887 chars_needed
+= ngroups
+ 1;
890 /* Allocate buffer for output. We need two more because while rounding
891 it is possible that we need two more characters in front of all the
892 other output. If the amount of memory we have to allocate is too
893 large use `malloc' instead of `alloca'. */
894 if (__builtin_expect (chars_needed
>= (size_t) -1 / sizeof (wchar_t) - 2
895 || chars_needed
< fracdig_max
, 0))
897 /* Some overflow occurred. */
898 __set_errno (ERANGE
);
901 size_t wbuffer_to_alloc
= (2 + chars_needed
) * sizeof (wchar_t);
902 buffer_malloced
= ! __libc_use_alloca (wbuffer_to_alloc
);
903 if (__builtin_expect (buffer_malloced
, 0))
905 wbuffer
= (wchar_t *) malloc (wbuffer_to_alloc
);
907 /* Signal an error to the caller. */
911 wbuffer
= (wchar_t *) alloca (wbuffer_to_alloc
);
912 wcp
= wstartp
= wbuffer
+ 2; /* Let room for rounding. */
914 /* Do the real work: put digits in allocated buffer. */
915 if (p
.expsign
== 0 || p
.type
!= 'f')
917 assert (p
.expsign
== 0 || intdig_max
== 1);
918 while (intdig_no
< intdig_max
)
921 *wcp
++ = hack_digit (&p
);
926 || (fracdig_max
> 0 && (p
.fracsize
> 1 || p
.frac
[0] != 0)))
931 /* |fp| < 1.0 and the selected p.type is 'f', so put "0."
938 /* Generate the needed number of fractional digits. */
941 while (fracdig_no
< fracdig_min
+ added_zeros
942 || (fracdig_no
< fracdig_max
&& (p
.fracsize
> 1 || p
.frac
[0] != 0)))
945 *wcp
= hack_digit (&p
);
948 else if (significant
== 0)
957 wchar_t last_digit
= wcp
[-1] != decimalwc
? wcp
[-1] : wcp
[-2];
958 wchar_t next_digit
= hack_digit (&p
);
960 if (next_digit
!= L
'0' && next_digit
!= L
'5')
962 else if (p
.fracsize
== 1 && p
.frac
[0] == 0)
963 /* Rest of the number is zero. */
965 else if (p
.scalesize
== 0)
967 /* Here we have to see whether all limbs are zero since no
968 normalization happened. */
969 size_t lcnt
= p
.fracsize
;
970 while (lcnt
>= 1 && p
.frac
[lcnt
- 1] == 0)
972 more_bits
= lcnt
> 0;
976 int rounding_mode
= get_rounding_mode ();
977 if (round_away (is_neg
, (last_digit
- L
'0') & 1, next_digit
>= L
'5',
978 more_bits
, rounding_mode
))
984 /* Process fractional digits. Terminate if not rounded or
985 radix character is reached. */
987 while (*--wtp
!= decimalwc
&& *wtp
== L
'9')
992 if (removed
== fracdig_min
&& added_zeros
> 0)
994 if (*wtp
!= decimalwc
)
997 else if (__builtin_expect (spec
== 'g' && p
.type
== 'f' && info
->alt
998 && wtp
== wstartp
+ 1
999 && wstartp
[0] == L
'0',
1001 /* This is a special case: the rounded number is 1.0,
1002 the format is 'g' or 'G', and the alternative format
1003 is selected. This means the result must be "1.". */
1007 if (fracdig_no
== 0 || *wtp
== decimalwc
)
1009 /* Round the integer digits. */
1010 if (*(wtp
- 1) == decimalwc
)
1013 while (--wtp
>= wstartp
&& *wtp
== L
'9')
1020 /* It is more critical. All digits were 9's. */
1025 p
.exponent
+= p
.expsign
== 0 ? 1 : -1;
1027 /* The above p.exponent adjustment could lead to 1.0e-00,
1028 e.g. for 0.999999999. Make sure p.exponent 0 always
1030 if (p
.exponent
== 0)
1033 else if (intdig_no
== dig_max
)
1035 /* This is the case where for p.type %g the number fits
1036 really in the range for %f output but after rounding
1037 the number of digits is too big. */
1038 *--wstartp
= decimalwc
;
1041 if (info
->alt
|| fracdig_no
> 0)
1043 /* Overwrite the old radix character. */
1044 wstartp
[intdig_no
+ 2] = L
'0';
1048 fracdig_no
+= intdig_no
;
1050 fracdig_max
= intdig_max
- intdig_no
;
1052 /* Now we must print the p.exponent. */
1053 p
.type
= isupper (info
->spec
) ? 'E' : 'e';
1057 /* We can simply add another another digit before the
1063 /* While rounding the number of digits can change.
1064 If the number now exceeds the limits remove some
1065 fractional digits. */
1066 if (intdig_no
+ fracdig_no
> dig_max
)
1068 wcp
-= intdig_no
+ fracdig_no
- dig_max
;
1069 fracdig_no
-= intdig_no
+ fracdig_no
- dig_max
;
1075 /* Now remove unnecessary '0' at the end of the string. */
1076 while (fracdig_no
> fracdig_min
+ added_zeros
&& *(wcp
- 1) == L
'0')
1081 /* If we eliminate all fractional digits we perhaps also can remove
1082 the radix character. */
1083 if (fracdig_no
== 0 && !info
->alt
&& *(wcp
- 1) == decimalwc
)
1088 /* Rounding might have changed the number of groups. We allocated
1089 enough memory but we need here the correct number of groups. */
1090 if (intdig_no
!= intdig_max
)
1091 ngroups
= __guess_grouping (intdig_no
, grouping
);
1093 /* Add in separator characters, overwriting the same buffer. */
1094 wcp
= group_number (wstartp
, wcp
, intdig_no
, grouping
, thousands_sepwc
,
1098 /* Write the p.exponent if it is needed. */
1101 if (__glibc_unlikely (p
.expsign
!= 0 && p
.exponent
== 4 && spec
== 'g'))
1103 /* This is another special case. The p.exponent of the number is
1104 really smaller than -4, which requires the 'e'/'E' format.
1105 But after rounding the number has an p.exponent of -4. */
1106 assert (wcp
>= wstartp
+ 1);
1107 assert (wstartp
[0] == L
'1');
1108 __wmemcpy (wstartp
, L
"0.0001", 6);
1109 wstartp
[1] = decimalwc
;
1110 if (wcp
>= wstartp
+ 2)
1112 __wmemset (wstartp
+ 6, L
'0', wcp
- (wstartp
+ 2));
1120 *wcp
++ = (wchar_t) p
.type
;
1121 *wcp
++ = p
.expsign
? L
'-' : L
'+';
1123 /* Find the magnitude of the p.exponent. */
1125 while (expscale
<= p
.exponent
)
1128 if (p
.exponent
< 10)
1129 /* Exponent always has at least two digits. */
1135 *wcp
++ = L
'0' + (p
.exponent
/ expscale
);
1136 p
.exponent
%= expscale
;
1138 while (expscale
> 10);
1139 *wcp
++ = L
'0' + p
.exponent
;
1143 /* Compute number of characters which must be filled with the padding
1145 if (is_neg
|| info
->showsign
|| info
->space
)
1147 width
-= wcp
- wstartp
;
1149 if (!info
->left
&& info
->pad
!= '0' && width
> 0)
1150 PADN (info
->pad
, width
);
1154 else if (info
->showsign
)
1156 else if (info
->space
)
1159 if (!info
->left
&& info
->pad
== '0' && width
> 0)
1163 char *buffer
= NULL
;
1164 char *buffer_end
= NULL
;
1170 /* Create the single byte string. */
1172 size_t thousands_sep_len
;
1174 size_t factor
= (info
->i18n
1175 ? _NL_CURRENT_WORD (LC_CTYPE
, _NL_CTYPE_MB_CUR_MAX
)
1178 decimal_len
= strlen (decimal
);
1180 if (thousands_sep
== NULL
)
1181 thousands_sep_len
= 0;
1183 thousands_sep_len
= strlen (thousands_sep
);
1185 size_t nbuffer
= (2 + chars_needed
* factor
+ decimal_len
1186 + ngroups
* thousands_sep_len
);
1187 if (__glibc_unlikely (buffer_malloced
))
1189 buffer
= (char *) malloc (nbuffer
);
1192 /* Signal an error to the caller. */
1198 buffer
= (char *) alloca (nbuffer
);
1199 buffer_end
= buffer
+ nbuffer
;
1201 /* Now copy the wide character string. Since the character
1202 (except for the decimal point and thousands separator) must
1203 be coming from the ASCII range we can esily convert the
1204 string without mapping tables. */
1205 for (cp
= buffer
, copywc
= wstartp
; copywc
< wcp
; ++copywc
)
1206 if (*copywc
== decimalwc
)
1207 cp
= (char *) __mempcpy (cp
, decimal
, decimal_len
);
1208 else if (*copywc
== thousands_sepwc
)
1209 cp
= (char *) __mempcpy (cp
, thousands_sep
, thousands_sep_len
);
1211 *cp
++ = (char) *copywc
;
1215 if (__glibc_unlikely (info
->i18n
))
1217 #ifdef COMPILE_WPRINTF
1218 wstartp
= _i18n_number_rewrite (wstartp
, wcp
,
1219 wbuffer
+ wbuffer_to_alloc
);
1220 wcp
= wbuffer
+ wbuffer_to_alloc
;
1221 assert ((uintptr_t) wbuffer
<= (uintptr_t) wstartp
);
1222 assert ((uintptr_t) wstartp
1223 < (uintptr_t) wbuffer
+ wbuffer_to_alloc
);
1225 tmpptr
= _i18n_number_rewrite (tmpptr
, cp
, buffer_end
);
1227 assert ((uintptr_t) buffer
<= (uintptr_t) tmpptr
);
1228 assert ((uintptr_t) tmpptr
< (uintptr_t) buffer_end
);
1232 PRINT (tmpptr
, wstartp
, wide
? wcp
- wstartp
: cp
- tmpptr
);
1234 /* Free the memory if necessary. */
1235 if (__glibc_unlikely (buffer_malloced
))
1242 if (info
->left
&& width
> 0)
1243 PADN (info
->pad
, width
);
1247 ldbl_hidden_def (___printf_fp
, __printf_fp
)
1248 ldbl_strong_alias (___printf_fp
, __printf_fp
)
1250 /* Return the number of extra grouping characters that will be inserted
1251 into a number with INTDIG_MAX integer digits. */
1254 __guess_grouping (unsigned int intdig_max
, const char *grouping
)
1256 unsigned int groups
;
1258 /* We treat all negative values like CHAR_MAX. */
1260 if (*grouping
== CHAR_MAX
|| *grouping
<= 0)
1261 /* No grouping should be done. */
1265 while (intdig_max
> (unsigned int) *grouping
)
1268 intdig_max
-= *grouping
++;
1270 if (*grouping
== CHAR_MAX
1275 /* No more grouping should be done. */
1277 else if (*grouping
== 0)
1279 /* Same grouping repeats. */
1280 groups
+= (intdig_max
- 1) / grouping
[-1];
1288 /* Group the INTDIG_NO integer digits of the number in [BUF,BUFEND).
1289 There is guaranteed enough space past BUFEND to extend it.
1290 Return the new end of buffer. */
1294 group_number (wchar_t *buf
, wchar_t *bufend
, unsigned int intdig_no
,
1295 const char *grouping
, wchar_t thousands_sep
, int ngroups
)
1302 /* Move the fractional part down. */
1303 __wmemmove (buf
+ intdig_no
+ ngroups
, buf
+ intdig_no
,
1304 bufend
- (buf
+ intdig_no
));
1306 p
= buf
+ intdig_no
+ ngroups
- 1;
1309 unsigned int len
= *grouping
++;
1311 *p
-- = buf
[--intdig_no
];
1313 *p
-- = thousands_sep
;
1315 if (*grouping
== CHAR_MAX
1320 /* No more grouping should be done. */
1322 else if (*grouping
== 0)
1323 /* Same grouping repeats. */
1325 } while (intdig_no
> (unsigned int) *grouping
);
1327 /* Copy the remaining ungrouped digits. */
1329 *p
-- = buf
[--intdig_no
];
1332 return bufend
+ ngroups
;