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. */
336 if (isnan (fpnum
.ldbl
))
338 is_neg
= signbit (fpnum
.ldbl
);
339 if (isupper (info
->spec
))
350 else if ((res
= __isinfl (fpnum
.ldbl
)))
353 if (isupper (info
->spec
))
366 p
.fracsize
= __mpn_extract_long_double (fp_input
,
368 sizeof (fp_input
[0])),
369 &p
.exponent
, &is_neg
,
371 to_shift
= 1 + p
.fracsize
* BITS_PER_MP_LIMB
- LDBL_MANT_DIG
;
375 #endif /* no long double */
377 fpnum
.dbl
= *(const double *) args
[0];
379 /* Check for special values: not a number or infinity. */
381 if (isnan (fpnum
.dbl
))
383 union ieee754_double u
= { .d
= fpnum
.dbl
};
384 is_neg
= u
.ieee
.negative
!= 0;
385 if (isupper (info
->spec
))
396 else if ((res
= __isinf (fpnum
.dbl
)))
399 if (isupper (info
->spec
))
412 p
.fracsize
= __mpn_extract_double (fp_input
,
414 / sizeof (fp_input
[0])),
415 &p
.exponent
, &is_neg
, fpnum
.dbl
);
416 to_shift
= 1 + p
.fracsize
* BITS_PER_MP_LIMB
- DBL_MANT_DIG
;
422 int width
= info
->width
;
424 if (is_neg
|| info
->showsign
|| info
->space
)
428 if (!info
->left
&& width
> 0)
433 else if (info
->showsign
)
435 else if (info
->space
)
438 PRINT (special
, wspecial
, 3);
440 if (info
->left
&& width
> 0)
447 /* We need three multiprecision variables. Now that we have the p.exponent
448 of the number we can allocate the needed memory. It would be more
449 efficient to use variables of the fixed maximum size but because this
450 would be really big it could lead to memory problems. */
452 mp_size_t bignum_size
= ((abs (p
.exponent
) + BITS_PER_MP_LIMB
- 1)
454 + (LDBL_MANT_DIG
/ BITS_PER_MP_LIMB
> 2 ? 8 : 4))
455 * sizeof (mp_limb_t
);
456 p
.frac
= (mp_limb_t
*) alloca (bignum_size
);
457 p
.tmp
= (mp_limb_t
*) alloca (bignum_size
);
458 p
.scale
= (mp_limb_t
*) alloca (bignum_size
);
461 /* We now have to distinguish between numbers with positive and negative
462 exponents because the method used for the one is not applicable/efficient
469 int explog
= LDBL_MAX_10_EXP_LOG
;
471 const struct mp_power
*powers
= &_fpioconst_pow10
[explog
+ 1];
474 if ((p
.exponent
+ to_shift
) % BITS_PER_MP_LIMB
== 0)
476 MPN_COPY_DECR (p
.frac
+ (p
.exponent
+ to_shift
) / BITS_PER_MP_LIMB
,
477 fp_input
, p
.fracsize
);
478 p
.fracsize
+= (p
.exponent
+ to_shift
) / BITS_PER_MP_LIMB
;
482 cy
= __mpn_lshift (p
.frac
+
483 (p
.exponent
+ to_shift
) / BITS_PER_MP_LIMB
,
484 fp_input
, p
.fracsize
,
485 (p
.exponent
+ to_shift
) % BITS_PER_MP_LIMB
);
486 p
.fracsize
+= (p
.exponent
+ to_shift
) / BITS_PER_MP_LIMB
;
488 p
.frac
[p
.fracsize
++] = cy
;
490 MPN_ZERO (p
.frac
, (p
.exponent
+ to_shift
) / BITS_PER_MP_LIMB
);
492 assert (powers
> &_fpioconst_pow10
[0]);
497 /* The number of the product of two binary numbers with n and m
498 bits respectively has m+n or m+n-1 bits. */
499 if (p
.exponent
>= scaleexpo
+ powers
->p_expo
- 1)
501 if (p
.scalesize
== 0)
503 #ifndef __NO_LONG_DOUBLE_MATH
504 if (LDBL_MANT_DIG
> _FPIO_CONST_OFFSET
* BITS_PER_MP_LIMB
505 && info
->is_long_double
)
507 #define _FPIO_CONST_SHIFT \
508 (((LDBL_MANT_DIG + BITS_PER_MP_LIMB - 1) / BITS_PER_MP_LIMB) \
509 - _FPIO_CONST_OFFSET)
510 /* 64bit const offset is not enough for
511 IEEE quad long double. */
512 p
.tmpsize
= powers
->arraysize
+ _FPIO_CONST_SHIFT
;
513 memcpy (p
.tmp
+ _FPIO_CONST_SHIFT
,
514 &__tens
[powers
->arrayoff
],
515 p
.tmpsize
* sizeof (mp_limb_t
));
516 MPN_ZERO (p
.tmp
, _FPIO_CONST_SHIFT
);
517 /* Adjust p.exponent, as scaleexpo will be this much
519 p
.exponent
+= _FPIO_CONST_SHIFT
* BITS_PER_MP_LIMB
;
524 p
.tmpsize
= powers
->arraysize
;
525 memcpy (p
.tmp
, &__tens
[powers
->arrayoff
],
526 p
.tmpsize
* sizeof (mp_limb_t
));
531 cy
= __mpn_mul (p
.tmp
, p
.scale
, p
.scalesize
,
532 &__tens
[powers
->arrayoff
533 + _FPIO_CONST_OFFSET
],
534 powers
->arraysize
- _FPIO_CONST_OFFSET
);
535 p
.tmpsize
= p
.scalesize
+
536 powers
->arraysize
- _FPIO_CONST_OFFSET
;
541 if (MPN_GE (p
.frac
, p
.tmp
))
544 MPN_ASSIGN (p
.scale
, p
.tmp
);
545 count_leading_zeros (cnt
, p
.scale
[p
.scalesize
- 1]);
546 scaleexpo
= (p
.scalesize
- 2) * BITS_PER_MP_LIMB
- cnt
- 1;
547 exp10
|= 1 << explog
;
552 while (powers
> &_fpioconst_pow10
[0]);
555 /* Optimize number representations. We want to represent the numbers
556 with the lowest number of bytes possible without losing any
557 bytes. Also the highest bit in the scaling factor has to be set
558 (this is a requirement of the MPN division routines). */
561 /* Determine minimum number of zero bits at the end of
563 for (i
= 0; p
.scale
[i
] == 0 && p
.frac
[i
] == 0; i
++)
566 /* Determine number of bits the scaling factor is misplaced. */
567 count_leading_zeros (cnt_h
, p
.scale
[p
.scalesize
- 1]);
571 /* The highest bit of the scaling factor is already set. So
572 we only have to remove the trailing empty limbs. */
575 MPN_COPY_INCR (p
.scale
, p
.scale
+ i
, p
.scalesize
- i
);
577 MPN_COPY_INCR (p
.frac
, p
.frac
+ i
, p
.fracsize
- i
);
585 count_trailing_zeros (cnt_l
, p
.scale
[i
]);
589 count_trailing_zeros (cnt_l2
, p
.frac
[i
]);
595 count_trailing_zeros (cnt_l
, p
.frac
[i
]);
597 /* Now shift the numbers to their optimal position. */
598 if (i
== 0 && BITS_PER_MP_LIMB
- cnt_h
> cnt_l
)
600 /* We cannot save any memory. So just roll both numbers
601 so that the scaling factor has its highest bit set. */
603 (void) __mpn_lshift (p
.scale
, p
.scale
, p
.scalesize
, cnt_h
);
604 cy
= __mpn_lshift (p
.frac
, p
.frac
, p
.fracsize
, cnt_h
);
606 p
.frac
[p
.fracsize
++] = cy
;
608 else if (BITS_PER_MP_LIMB
- cnt_h
<= cnt_l
)
610 /* We can save memory by removing the trailing zero limbs
611 and by packing the non-zero limbs which gain another
614 (void) __mpn_rshift (p
.scale
, p
.scale
+ i
, p
.scalesize
- i
,
615 BITS_PER_MP_LIMB
- cnt_h
);
616 p
.scalesize
-= i
+ 1;
617 (void) __mpn_rshift (p
.frac
, p
.frac
+ i
, p
.fracsize
- i
,
618 BITS_PER_MP_LIMB
- cnt_h
);
619 p
.fracsize
-= p
.frac
[p
.fracsize
- i
- 1] == 0 ? i
+ 1 : i
;
623 /* We can only save the memory of the limbs which are zero.
624 The non-zero parts occupy the same number of limbs. */
626 (void) __mpn_rshift (p
.scale
, p
.scale
+ (i
- 1),
627 p
.scalesize
- (i
- 1),
628 BITS_PER_MP_LIMB
- cnt_h
);
630 (void) __mpn_rshift (p
.frac
, p
.frac
+ (i
- 1),
631 p
.fracsize
- (i
- 1),
632 BITS_PER_MP_LIMB
- cnt_h
);
634 p
.frac
[p
.fracsize
- (i
- 1) - 1] == 0 ? i
: i
- 1;
639 else if (p
.exponent
< 0)
643 int explog
= LDBL_MAX_10_EXP_LOG
;
644 const struct mp_power
*powers
= &_fpioconst_pow10
[explog
+ 1];
646 /* Now shift the input value to its right place. */
647 cy
= __mpn_lshift (p
.frac
, fp_input
, p
.fracsize
, to_shift
);
648 p
.frac
[p
.fracsize
++] = cy
;
649 assert (cy
== 1 || (p
.frac
[p
.fracsize
- 2] == 0 && p
.frac
[0] == 0));
652 p
.exponent
= -p
.exponent
;
654 assert (powers
!= &_fpioconst_pow10
[0]);
659 if (p
.exponent
>= powers
->m_expo
)
661 int i
, incr
, cnt_h
, cnt_l
;
664 /* The __mpn_mul function expects the first argument to be
665 bigger than the second. */
666 if (p
.fracsize
< powers
->arraysize
- _FPIO_CONST_OFFSET
)
667 cy
= __mpn_mul (p
.tmp
, &__tens
[powers
->arrayoff
668 + _FPIO_CONST_OFFSET
],
669 powers
->arraysize
- _FPIO_CONST_OFFSET
,
672 cy
= __mpn_mul (p
.tmp
, p
.frac
, p
.fracsize
,
673 &__tens
[powers
->arrayoff
+ _FPIO_CONST_OFFSET
],
674 powers
->arraysize
- _FPIO_CONST_OFFSET
);
675 p
.tmpsize
= p
.fracsize
+ powers
->arraysize
- _FPIO_CONST_OFFSET
;
679 count_leading_zeros (cnt_h
, p
.tmp
[p
.tmpsize
- 1]);
680 incr
= (p
.tmpsize
- p
.fracsize
) * BITS_PER_MP_LIMB
681 + BITS_PER_MP_LIMB
- 1 - cnt_h
;
683 assert (incr
<= powers
->p_expo
);
685 /* If we increased the p.exponent by exactly 3 we have to test
686 for overflow. This is done by comparing with 10 shifted
687 to the right position. */
688 if (incr
== p
.exponent
+ 3)
690 if (cnt_h
<= BITS_PER_MP_LIMB
- 4)
694 = ((mp_limb_t
) 10) << (BITS_PER_MP_LIMB
- 4 - cnt_h
);
698 topval
[0] = ((mp_limb_t
) 10) << (BITS_PER_MP_LIMB
- 4);
700 (void) __mpn_lshift (topval
, topval
, 2,
701 BITS_PER_MP_LIMB
- cnt_h
);
705 /* We have to be careful when multiplying the last factor.
706 If the result is greater than 1.0 be have to test it
707 against 10.0. If it is greater or equal to 10.0 the
708 multiplication was not valid. This is because we cannot
709 determine the number of bits in the result in advance. */
710 if (incr
< p
.exponent
+ 3
711 || (incr
== p
.exponent
+ 3 &&
712 (p
.tmp
[p
.tmpsize
- 1] < topval
[1]
713 || (p
.tmp
[p
.tmpsize
- 1] == topval
[1]
714 && p
.tmp
[p
.tmpsize
- 2] < topval
[0]))))
716 /* The factor is right. Adapt binary and decimal
719 exp10
|= 1 << explog
;
721 /* If this factor yields a number greater or equal to
722 1.0, we must not shift the non-fractional digits down. */
724 cnt_h
+= -p
.exponent
;
726 /* Now we optimize the number representation. */
727 for (i
= 0; p
.tmp
[i
] == 0; ++i
);
728 if (cnt_h
== BITS_PER_MP_LIMB
- 1)
730 MPN_COPY (p
.frac
, p
.tmp
+ i
, p
.tmpsize
- i
);
731 p
.fracsize
= p
.tmpsize
- i
;
735 count_trailing_zeros (cnt_l
, p
.tmp
[i
]);
737 /* Now shift the numbers to their optimal position. */
738 if (i
== 0 && BITS_PER_MP_LIMB
- 1 - cnt_h
> cnt_l
)
740 /* We cannot save any memory. Just roll the
741 number so that the leading digit is in a
744 cy
= __mpn_lshift (p
.frac
, p
.tmp
, p
.tmpsize
,
746 p
.fracsize
= p
.tmpsize
+ 1;
747 p
.frac
[p
.fracsize
- 1] = cy
;
749 else if (BITS_PER_MP_LIMB
- 1 - cnt_h
<= cnt_l
)
751 (void) __mpn_rshift (p
.frac
, p
.tmp
+ i
, p
.tmpsize
- i
,
752 BITS_PER_MP_LIMB
- 1 - cnt_h
);
753 p
.fracsize
= p
.tmpsize
- i
;
757 /* We can only save the memory of the limbs which
758 are zero. The non-zero parts occupy the same
761 (void) __mpn_rshift (p
.frac
, p
.tmp
+ (i
- 1),
763 BITS_PER_MP_LIMB
- 1 - cnt_h
);
764 p
.fracsize
= p
.tmpsize
- (i
- 1);
771 while (powers
!= &_fpioconst_pow10
[1] && p
.exponent
> 0);
772 /* All factors but 10^-1 are tested now. */
777 cy
= __mpn_mul_1 (p
.tmp
, p
.frac
, p
.fracsize
, 10);
778 p
.tmpsize
= p
.fracsize
;
779 assert (cy
== 0 || p
.tmp
[p
.tmpsize
- 1] < 20);
781 count_trailing_zeros (cnt_l
, p
.tmp
[0]);
782 if (cnt_l
< MIN (4, p
.exponent
))
784 cy
= __mpn_lshift (p
.frac
, p
.tmp
, p
.tmpsize
,
785 BITS_PER_MP_LIMB
- MIN (4, p
.exponent
));
787 p
.frac
[p
.tmpsize
++] = cy
;
790 (void) __mpn_rshift (p
.frac
, p
.tmp
, p
.tmpsize
, MIN (4, p
.exponent
));
791 p
.fracsize
= p
.tmpsize
;
793 assert (p
.frac
[p
.fracsize
- 1] < 10);
799 /* This is a special case. We don't need a factor because the
800 numbers are in the range of 1.0 <= |fp| < 8.0. We simply
801 shift it to the right place and divide it by 1.0 to get the
802 leading digit. (Of course this division is not really made.) */
803 assert (0 <= p
.exponent
&& p
.exponent
< 3 &&
804 p
.exponent
+ to_shift
< BITS_PER_MP_LIMB
);
806 /* Now shift the input value to its right place. */
807 cy
= __mpn_lshift (p
.frac
, fp_input
, p
.fracsize
, (p
.exponent
+ to_shift
));
808 p
.frac
[p
.fracsize
++] = cy
;
813 int width
= info
->width
;
814 wchar_t *wstartp
, *wcp
;
817 int intdig_max
, intdig_no
= 0;
823 char spec
= _tolower (info
->spec
);
829 fracdig_min
= fracdig_max
= info
->prec
< 0 ? 6 : info
->prec
;
830 chars_needed
= 1 + 1 + (size_t) fracdig_max
+ 1 + 1 + 4;
831 /* d . ddd e +- ddd */
832 dig_max
= INT_MAX
; /* Unlimited. */
833 significant
= 1; /* Does not matter here. */
835 else if (spec
== 'f')
838 fracdig_min
= fracdig_max
= info
->prec
< 0 ? 6 : info
->prec
;
839 dig_max
= INT_MAX
; /* Unlimited. */
840 significant
= 1; /* Does not matter here. */
843 intdig_max
= p
.exponent
+ 1;
844 /* This can be really big! */ /* XXX Maybe malloc if too big? */
845 chars_needed
= (size_t) p
.exponent
+ 1 + 1 + (size_t) fracdig_max
;
850 chars_needed
= 1 + 1 + (size_t) fracdig_max
;
855 dig_max
= info
->prec
< 0 ? 6 : (info
->prec
== 0 ? 1 : info
->prec
);
856 if ((p
.expsign
== 0 && p
.exponent
>= dig_max
)
857 || (p
.expsign
!= 0 && p
.exponent
> 4))
859 if ('g' - 'G' == 'e' - 'E')
860 p
.type
= 'E' + (info
->spec
- 'G');
862 p
.type
= isupper (info
->spec
) ? 'E' : 'e';
863 fracdig_max
= dig_max
- 1;
865 chars_needed
= 1 + 1 + (size_t) fracdig_max
+ 1 + 1 + 4;
870 intdig_max
= p
.expsign
== 0 ? p
.exponent
+ 1 : 0;
871 fracdig_max
= dig_max
- intdig_max
;
872 /* We need space for the significant digits and perhaps
873 for leading zeros when < 1.0. The number of leading
874 zeros can be as many as would be required for
875 exponential notation with a negative two-digit
876 p.exponent, which is 4. */
877 chars_needed
= (size_t) dig_max
+ 1 + 4;
879 fracdig_min
= info
->alt
? fracdig_max
: 0;
880 significant
= 0; /* We count significant digits. */
885 /* Guess the number of groups we will make, and thus how
886 many spaces we need for separator characters. */
887 ngroups
= __guess_grouping (intdig_max
, grouping
);
888 /* Allocate one more character in case rounding increases the
890 chars_needed
+= ngroups
+ 1;
893 /* Allocate buffer for output. We need two more because while rounding
894 it is possible that we need two more characters in front of all the
895 other output. If the amount of memory we have to allocate is too
896 large use `malloc' instead of `alloca'. */
897 if (__builtin_expect (chars_needed
>= (size_t) -1 / sizeof (wchar_t) - 2
898 || chars_needed
< fracdig_max
, 0))
900 /* Some overflow occurred. */
901 __set_errno (ERANGE
);
904 size_t wbuffer_to_alloc
= (2 + chars_needed
) * sizeof (wchar_t);
905 buffer_malloced
= ! __libc_use_alloca (wbuffer_to_alloc
);
906 if (__builtin_expect (buffer_malloced
, 0))
908 wbuffer
= (wchar_t *) malloc (wbuffer_to_alloc
);
910 /* Signal an error to the caller. */
914 wbuffer
= (wchar_t *) alloca (wbuffer_to_alloc
);
915 wcp
= wstartp
= wbuffer
+ 2; /* Let room for rounding. */
917 /* Do the real work: put digits in allocated buffer. */
918 if (p
.expsign
== 0 || p
.type
!= 'f')
920 assert (p
.expsign
== 0 || intdig_max
== 1);
921 while (intdig_no
< intdig_max
)
924 *wcp
++ = hack_digit (&p
);
929 || (fracdig_max
> 0 && (p
.fracsize
> 1 || p
.frac
[0] != 0)))
934 /* |fp| < 1.0 and the selected p.type is 'f', so put "0."
941 /* Generate the needed number of fractional digits. */
944 while (fracdig_no
< fracdig_min
+ added_zeros
945 || (fracdig_no
< fracdig_max
&& (p
.fracsize
> 1 || p
.frac
[0] != 0)))
948 *wcp
= hack_digit (&p
);
951 else if (significant
== 0)
960 wchar_t last_digit
= wcp
[-1] != decimalwc
? wcp
[-1] : wcp
[-2];
961 wchar_t next_digit
= hack_digit (&p
);
963 if (next_digit
!= L
'0' && next_digit
!= L
'5')
965 else if (p
.fracsize
== 1 && p
.frac
[0] == 0)
966 /* Rest of the number is zero. */
968 else if (p
.scalesize
== 0)
970 /* Here we have to see whether all limbs are zero since no
971 normalization happened. */
972 size_t lcnt
= p
.fracsize
;
973 while (lcnt
>= 1 && p
.frac
[lcnt
- 1] == 0)
975 more_bits
= lcnt
> 0;
979 int rounding_mode
= get_rounding_mode ();
980 if (round_away (is_neg
, (last_digit
- L
'0') & 1, next_digit
>= L
'5',
981 more_bits
, rounding_mode
))
987 /* Process fractional digits. Terminate if not rounded or
988 radix character is reached. */
990 while (*--wtp
!= decimalwc
&& *wtp
== L
'9')
995 if (removed
== fracdig_min
&& added_zeros
> 0)
997 if (*wtp
!= decimalwc
)
1000 else if (__builtin_expect (spec
== 'g' && p
.type
== 'f' && info
->alt
1001 && wtp
== wstartp
+ 1
1002 && wstartp
[0] == L
'0',
1004 /* This is a special case: the rounded number is 1.0,
1005 the format is 'g' or 'G', and the alternative format
1006 is selected. This means the result must be "1.". */
1010 if (fracdig_no
== 0 || *wtp
== decimalwc
)
1012 /* Round the integer digits. */
1013 if (*(wtp
- 1) == decimalwc
)
1016 while (--wtp
>= wstartp
&& *wtp
== L
'9')
1023 /* It is more critical. All digits were 9's. */
1028 p
.exponent
+= p
.expsign
== 0 ? 1 : -1;
1030 /* The above p.exponent adjustment could lead to 1.0e-00,
1031 e.g. for 0.999999999. Make sure p.exponent 0 always
1033 if (p
.exponent
== 0)
1036 else if (intdig_no
== dig_max
)
1038 /* This is the case where for p.type %g the number fits
1039 really in the range for %f output but after rounding
1040 the number of digits is too big. */
1041 *--wstartp
= decimalwc
;
1044 if (info
->alt
|| fracdig_no
> 0)
1046 /* Overwrite the old radix character. */
1047 wstartp
[intdig_no
+ 2] = L
'0';
1051 fracdig_no
+= intdig_no
;
1053 fracdig_max
= intdig_max
- intdig_no
;
1055 /* Now we must print the p.exponent. */
1056 p
.type
= isupper (info
->spec
) ? 'E' : 'e';
1060 /* We can simply add another another digit before the
1066 /* While rounding the number of digits can change.
1067 If the number now exceeds the limits remove some
1068 fractional digits. */
1069 if (intdig_no
+ fracdig_no
> dig_max
)
1071 wcp
-= intdig_no
+ fracdig_no
- dig_max
;
1072 fracdig_no
-= intdig_no
+ fracdig_no
- dig_max
;
1078 /* Now remove unnecessary '0' at the end of the string. */
1079 while (fracdig_no
> fracdig_min
+ added_zeros
&& *(wcp
- 1) == L
'0')
1084 /* If we eliminate all fractional digits we perhaps also can remove
1085 the radix character. */
1086 if (fracdig_no
== 0 && !info
->alt
&& *(wcp
- 1) == decimalwc
)
1091 /* Rounding might have changed the number of groups. We allocated
1092 enough memory but we need here the correct number of groups. */
1093 if (intdig_no
!= intdig_max
)
1094 ngroups
= __guess_grouping (intdig_no
, grouping
);
1096 /* Add in separator characters, overwriting the same buffer. */
1097 wcp
= group_number (wstartp
, wcp
, intdig_no
, grouping
, thousands_sepwc
,
1101 /* Write the p.exponent if it is needed. */
1104 if (__glibc_unlikely (p
.expsign
!= 0 && p
.exponent
== 4 && spec
== 'g'))
1106 /* This is another special case. The p.exponent of the number is
1107 really smaller than -4, which requires the 'e'/'E' format.
1108 But after rounding the number has an p.exponent of -4. */
1109 assert (wcp
>= wstartp
+ 1);
1110 assert (wstartp
[0] == L
'1');
1111 __wmemcpy (wstartp
, L
"0.0001", 6);
1112 wstartp
[1] = decimalwc
;
1113 if (wcp
>= wstartp
+ 2)
1115 __wmemset (wstartp
+ 6, L
'0', wcp
- (wstartp
+ 2));
1123 *wcp
++ = (wchar_t) p
.type
;
1124 *wcp
++ = p
.expsign
? L
'-' : L
'+';
1126 /* Find the magnitude of the p.exponent. */
1128 while (expscale
<= p
.exponent
)
1131 if (p
.exponent
< 10)
1132 /* Exponent always has at least two digits. */
1138 *wcp
++ = L
'0' + (p
.exponent
/ expscale
);
1139 p
.exponent
%= expscale
;
1141 while (expscale
> 10);
1142 *wcp
++ = L
'0' + p
.exponent
;
1146 /* Compute number of characters which must be filled with the padding
1148 if (is_neg
|| info
->showsign
|| info
->space
)
1150 width
-= wcp
- wstartp
;
1152 if (!info
->left
&& info
->pad
!= '0' && width
> 0)
1153 PADN (info
->pad
, width
);
1157 else if (info
->showsign
)
1159 else if (info
->space
)
1162 if (!info
->left
&& info
->pad
== '0' && width
> 0)
1166 char *buffer
= NULL
;
1167 char *buffer_end
= NULL
;
1173 /* Create the single byte string. */
1175 size_t thousands_sep_len
;
1177 size_t factor
= (info
->i18n
1178 ? _NL_CURRENT_WORD (LC_CTYPE
, _NL_CTYPE_MB_CUR_MAX
)
1181 decimal_len
= strlen (decimal
);
1183 if (thousands_sep
== NULL
)
1184 thousands_sep_len
= 0;
1186 thousands_sep_len
= strlen (thousands_sep
);
1188 size_t nbuffer
= (2 + chars_needed
* factor
+ decimal_len
1189 + ngroups
* thousands_sep_len
);
1190 if (__glibc_unlikely (buffer_malloced
))
1192 buffer
= (char *) malloc (nbuffer
);
1195 /* Signal an error to the caller. */
1201 buffer
= (char *) alloca (nbuffer
);
1202 buffer_end
= buffer
+ nbuffer
;
1204 /* Now copy the wide character string. Since the character
1205 (except for the decimal point and thousands separator) must
1206 be coming from the ASCII range we can esily convert the
1207 string without mapping tables. */
1208 for (cp
= buffer
, copywc
= wstartp
; copywc
< wcp
; ++copywc
)
1209 if (*copywc
== decimalwc
)
1210 cp
= (char *) __mempcpy (cp
, decimal
, decimal_len
);
1211 else if (*copywc
== thousands_sepwc
)
1212 cp
= (char *) __mempcpy (cp
, thousands_sep
, thousands_sep_len
);
1214 *cp
++ = (char) *copywc
;
1218 if (__glibc_unlikely (info
->i18n
))
1220 #ifdef COMPILE_WPRINTF
1221 wstartp
= _i18n_number_rewrite (wstartp
, wcp
,
1222 wbuffer
+ wbuffer_to_alloc
);
1223 wcp
= wbuffer
+ wbuffer_to_alloc
;
1224 assert ((uintptr_t) wbuffer
<= (uintptr_t) wstartp
);
1225 assert ((uintptr_t) wstartp
1226 < (uintptr_t) wbuffer
+ wbuffer_to_alloc
);
1228 tmpptr
= _i18n_number_rewrite (tmpptr
, cp
, buffer_end
);
1230 assert ((uintptr_t) buffer
<= (uintptr_t) tmpptr
);
1231 assert ((uintptr_t) tmpptr
< (uintptr_t) buffer_end
);
1235 PRINT (tmpptr
, wstartp
, wide
? wcp
- wstartp
: cp
- tmpptr
);
1237 /* Free the memory if necessary. */
1238 if (__glibc_unlikely (buffer_malloced
))
1245 if (info
->left
&& width
> 0)
1246 PADN (info
->pad
, width
);
1250 ldbl_hidden_def (___printf_fp
, __printf_fp
)
1251 ldbl_strong_alias (___printf_fp
, __printf_fp
)
1253 /* Return the number of extra grouping characters that will be inserted
1254 into a number with INTDIG_MAX integer digits. */
1257 __guess_grouping (unsigned int intdig_max
, const char *grouping
)
1259 unsigned int groups
;
1261 /* We treat all negative values like CHAR_MAX. */
1263 if (*grouping
== CHAR_MAX
|| *grouping
<= 0)
1264 /* No grouping should be done. */
1268 while (intdig_max
> (unsigned int) *grouping
)
1271 intdig_max
-= *grouping
++;
1273 if (*grouping
== CHAR_MAX
1278 /* No more grouping should be done. */
1280 else if (*grouping
== 0)
1282 /* Same grouping repeats. */
1283 groups
+= (intdig_max
- 1) / grouping
[-1];
1291 /* Group the INTDIG_NO integer digits of the number in [BUF,BUFEND).
1292 There is guaranteed enough space past BUFEND to extend it.
1293 Return the new end of buffer. */
1297 group_number (wchar_t *buf
, wchar_t *bufend
, unsigned int intdig_no
,
1298 const char *grouping
, wchar_t thousands_sep
, int ngroups
)
1305 /* Move the fractional part down. */
1306 __wmemmove (buf
+ intdig_no
+ ngroups
, buf
+ intdig_no
,
1307 bufend
- (buf
+ intdig_no
));
1309 p
= buf
+ intdig_no
+ ngroups
- 1;
1312 unsigned int len
= *grouping
++;
1314 *p
-- = buf
[--intdig_no
];
1316 *p
-- = thousands_sep
;
1318 if (*grouping
== CHAR_MAX
1323 /* No more grouping should be done. */
1325 else if (*grouping
== 0)
1326 /* Same grouping repeats. */
1328 } while (intdig_no
> (unsigned int) *grouping
);
1330 /* Copy the remaining ungrouped digits. */
1332 *p
-- = buf
[--intdig_no
];
1335 return bufend
+ ngroups
;