1 /* Floating point output for `printf'.
2 Copyright (C) 1995-2003, 2006-2008, 2011 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, write to the Free
19 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
22 /* The gmp headers need some configuration frobs. */
29 #include <gmp-mparam.h>
32 #include <stdlib/gmp-impl.h>
33 #include <stdlib/longlong.h>
34 #include <stdlib/fpioconst.h>
35 #include <locale/localeinfo.h>
44 #ifdef COMPILE_WPRINTF
45 # define CHAR_T wchar_t
50 #include "_i18n_number.h"
53 # define NDEBUG /* Undefine this for debugging assertions. */
57 /* This defines make it possible to use the same code for GNU C library and
58 the GNU I/O library. */
59 #define PUT(f, s, n) _IO_sputn (f, s, n)
60 #define PAD(f, c, n) (wide ? _IO_wpadn (f, c, n) : INTUSE(_IO_padn) (f, c, n))
61 /* We use this file GNU C library and GNU I/O library. So make
64 #define putc(c, f) (wide \
65 ? (int)_IO_putwc_unlocked (c, f) : _IO_putc_unlocked (c, f))
66 #define size_t _IO_size_t
69 /* Macros for doing the actual output. */
74 register const int outc = (ch); \
75 if (putc (outc, fp) == EOF) \
77 if (buffer_malloced) \
84 #define PRINT(ptr, wptr, len) \
87 register size_t outlen = (len); \
90 if (PUT (fp, wide ? (const char *) wptr : ptr, outlen) != outlen) \
92 if (buffer_malloced) \
102 while (outlen-- > 0) \
105 while (outlen-- > 0) \
110 #define PADN(ch, len) \
113 if (PAD (fp, ch, len) != len) \
115 if (buffer_malloced) \
123 /* We use the GNU MP library to handle large numbers.
125 An MP variable occupies a varying number of entries in its array. We keep
126 track of this number for efficiency reasons. Otherwise we would always
127 have to process the whole array. */
128 #define MPN_VAR(name) mp_limb_t *name; mp_size_t name##size
130 #define MPN_ASSIGN(dst,src) \
131 memcpy (dst, src, (dst##size = src##size) * sizeof (mp_limb_t))
132 #define MPN_GE(u,v) \
133 (u##size > v##size || (u##size == v##size && __mpn_cmp (u, v, u##size) >= 0))
135 extern int __isinfl_internal (long double) attribute_hidden
;
136 extern int __isnanl_internal (long double) attribute_hidden
;
138 extern mp_size_t
__mpn_extract_double (mp_ptr res_ptr
, mp_size_t size
,
139 int *expt
, int *is_neg
,
141 extern mp_size_t
__mpn_extract_long_double (mp_ptr res_ptr
, mp_size_t size
,
142 int *expt
, int *is_neg
,
144 extern unsigned int __guess_grouping (unsigned int intdig_max
,
145 const char *grouping
);
148 static wchar_t *group_number (wchar_t *buf
, wchar_t *bufend
,
149 unsigned int intdig_no
, const char *grouping
,
150 wchar_t thousands_sep
, int ngroups
)
155 ___printf_fp (FILE *fp
,
156 const struct printf_info
*info
,
157 const void *const *args
)
159 /* The floating-point value to output. */
163 __long_double_t ldbl
;
167 /* Locale-dependent representation of decimal point. */
171 /* Locale-dependent thousands separator and grouping specification. */
172 const char *thousands_sep
= NULL
;
173 wchar_t thousands_sepwc
= 0;
174 const char *grouping
;
176 /* "NaN" or "Inf" for the special cases. */
177 const char *special
= NULL
;
178 const wchar_t *wspecial
= NULL
;
180 /* We need just a few limbs for the input before shifting to the right
182 mp_limb_t fp_input
[(LDBL_MANT_DIG
+ BITS_PER_MP_LIMB
- 1) / BITS_PER_MP_LIMB
];
183 /* We need to shift the contents of fp_input by this amount of bits. */
186 /* The fraction of the floting-point value in question */
188 /* and the exponent. */
190 /* Sign of the exponent. */
192 /* Sign of float number. */
195 /* Scaling factor. */
198 /* Temporary bignum value. */
201 /* Digit which is result of last hack_digit() call. */
204 /* The type of output format that will be used: 'e'/'E' or 'f'. */
207 /* Counter for number of written characters. */
210 /* General helper (carry limb). */
213 /* Nonzero if this is output on a wide character stream. */
214 int wide
= info
->wide
;
216 /* Buffer in which we produce the output. */
217 wchar_t *wbuffer
= NULL
;
218 /* Flag whether wbuffer is malloc'ed or not. */
219 int buffer_malloced
= 0;
221 auto wchar_t hack_digit (void);
223 wchar_t hack_digit (void)
227 if (expsign
!= 0 && type
== 'f' && exponent
-- > 0)
229 else if (scalesize
== 0)
231 hi
= frac
[fracsize
- 1];
232 frac
[fracsize
- 1] = __mpn_mul_1 (frac
, frac
, fracsize
- 1, 10);
236 if (fracsize
< scalesize
)
240 hi
= mpn_divmod (tmp
, frac
, fracsize
, scale
, scalesize
);
241 tmp
[fracsize
- scalesize
] = hi
;
244 fracsize
= scalesize
;
245 while (fracsize
!= 0 && frac
[fracsize
- 1] == 0)
249 /* We're not prepared for an mpn variable with zero
256 mp_limb_t _cy
= __mpn_mul_1 (frac
, frac
, fracsize
, 10);
258 frac
[fracsize
++] = _cy
;
265 /* Figure out the decimal point character. */
266 if (info
->extra
== 0)
268 decimal
= _NL_CURRENT (LC_NUMERIC
, DECIMAL_POINT
);
269 decimalwc
= _NL_CURRENT_WORD (LC_NUMERIC
, _NL_NUMERIC_DECIMAL_POINT_WC
);
273 decimal
= _NL_CURRENT (LC_MONETARY
, MON_DECIMAL_POINT
);
274 if (*decimal
== '\0')
275 decimal
= _NL_CURRENT (LC_NUMERIC
, DECIMAL_POINT
);
276 decimalwc
= _NL_CURRENT_WORD (LC_MONETARY
,
277 _NL_MONETARY_DECIMAL_POINT_WC
);
278 if (decimalwc
== L
'\0')
279 decimalwc
= _NL_CURRENT_WORD (LC_NUMERIC
,
280 _NL_NUMERIC_DECIMAL_POINT_WC
);
282 /* The decimal point character must not be zero. */
283 assert (*decimal
!= '\0');
284 assert (decimalwc
!= L
'\0');
288 if (info
->extra
== 0)
289 grouping
= _NL_CURRENT (LC_NUMERIC
, GROUPING
);
291 grouping
= _NL_CURRENT (LC_MONETARY
, MON_GROUPING
);
293 if (*grouping
<= 0 || *grouping
== CHAR_MAX
)
297 /* Figure out the thousands separator character. */
300 if (info
->extra
== 0)
302 _NL_CURRENT_WORD (LC_NUMERIC
, _NL_NUMERIC_THOUSANDS_SEP_WC
);
305 _NL_CURRENT_WORD (LC_MONETARY
,
306 _NL_MONETARY_THOUSANDS_SEP_WC
);
310 if (info
->extra
== 0)
311 thousands_sep
= _NL_CURRENT (LC_NUMERIC
, THOUSANDS_SEP
);
313 thousands_sep
= _NL_CURRENT (LC_MONETARY
, MON_THOUSANDS_SEP
);
316 if ((wide
&& thousands_sepwc
== L
'\0')
317 || (! wide
&& *thousands_sep
== '\0'))
319 else if (thousands_sepwc
== L
'\0')
320 /* If we are printing multibyte characters and there is a
321 multibyte representation for the thousands separator,
322 we must ensure the wide character thousands separator
323 is available, even if it is fake. */
324 thousands_sepwc
= 0xfffffffe;
330 /* Fetch the argument value. */
331 #ifndef __NO_LONG_DOUBLE_MATH
332 if (info
->is_long_double
&& sizeof (long double) > sizeof (double))
334 fpnum
.ldbl
= *(const long double *) args
[0];
336 /* Check for special values: not a number or infinity. */
338 if (__isnanl (fpnum
.ldbl
))
340 union ieee854_long_double u
= { .d
= fpnum
.ldbl
};
341 is_neg
= u
.ieee
.negative
!= 0;
342 if (isupper (info
->spec
))
353 else if ((res
= __isinfl (fpnum
.ldbl
)))
356 if (isupper (info
->spec
))
369 fracsize
= __mpn_extract_long_double (fp_input
,
371 sizeof (fp_input
[0])),
374 to_shift
= 1 + fracsize
* BITS_PER_MP_LIMB
- LDBL_MANT_DIG
;
378 #endif /* no long double */
380 fpnum
.dbl
= *(const double *) args
[0];
382 /* Check for special values: not a number or infinity. */
384 if (__isnan (fpnum
.dbl
))
386 union ieee754_double u
= { .d
= fpnum
.dbl
};
387 is_neg
= u
.ieee
.negative
!= 0;
388 if (isupper (info
->spec
))
399 else if ((res
= __isinf (fpnum
.dbl
)))
402 if (isupper (info
->spec
))
415 fracsize
= __mpn_extract_double (fp_input
,
417 / sizeof (fp_input
[0])),
418 &exponent
, &is_neg
, fpnum
.dbl
);
419 to_shift
= 1 + fracsize
* BITS_PER_MP_LIMB
- DBL_MANT_DIG
;
425 int width
= info
->width
;
427 if (is_neg
|| info
->showsign
|| info
->space
)
431 if (!info
->left
&& width
> 0)
436 else if (info
->showsign
)
438 else if (info
->space
)
441 PRINT (special
, wspecial
, 3);
443 if (info
->left
&& width
> 0)
450 /* We need three multiprecision variables. Now that we have the exponent
451 of the number we can allocate the needed memory. It would be more
452 efficient to use variables of the fixed maximum size but because this
453 would be really big it could lead to memory problems. */
455 mp_size_t bignum_size
= ((ABS (exponent
) + BITS_PER_MP_LIMB
- 1)
457 + (LDBL_MANT_DIG
/ BITS_PER_MP_LIMB
> 2 ? 8 : 4))
458 * sizeof (mp_limb_t
);
459 frac
= (mp_limb_t
*) alloca (bignum_size
);
460 tmp
= (mp_limb_t
*) alloca (bignum_size
);
461 scale
= (mp_limb_t
*) alloca (bignum_size
);
464 /* We now have to distinguish between numbers with positive and negative
465 exponents because the method used for the one is not applicable/efficient
472 int explog
= LDBL_MAX_10_EXP_LOG
;
474 const struct mp_power
*powers
= &_fpioconst_pow10
[explog
+ 1];
477 if ((exponent
+ to_shift
) % BITS_PER_MP_LIMB
== 0)
479 MPN_COPY_DECR (frac
+ (exponent
+ to_shift
) / BITS_PER_MP_LIMB
,
481 fracsize
+= (exponent
+ to_shift
) / BITS_PER_MP_LIMB
;
485 cy
= __mpn_lshift (frac
+ (exponent
+ to_shift
) / BITS_PER_MP_LIMB
,
487 (exponent
+ to_shift
) % BITS_PER_MP_LIMB
);
488 fracsize
+= (exponent
+ to_shift
) / BITS_PER_MP_LIMB
;
490 frac
[fracsize
++] = cy
;
492 MPN_ZERO (frac
, (exponent
+ to_shift
) / BITS_PER_MP_LIMB
);
494 assert (powers
> &_fpioconst_pow10
[0]);
499 /* The number of the product of two binary numbers with n and m
500 bits respectively has m+n or m+n-1 bits. */
501 if (exponent
>= scaleexpo
+ powers
->p_expo
- 1)
505 #ifndef __NO_LONG_DOUBLE_MATH
506 if (LDBL_MANT_DIG
> _FPIO_CONST_OFFSET
* BITS_PER_MP_LIMB
507 && info
->is_long_double
)
509 #define _FPIO_CONST_SHIFT \
510 (((LDBL_MANT_DIG + BITS_PER_MP_LIMB - 1) / BITS_PER_MP_LIMB) \
511 - _FPIO_CONST_OFFSET)
512 /* 64bit const offset is not enough for
513 IEEE quad long double. */
514 tmpsize
= powers
->arraysize
+ _FPIO_CONST_SHIFT
;
515 memcpy (tmp
+ _FPIO_CONST_SHIFT
,
516 &__tens
[powers
->arrayoff
],
517 tmpsize
* sizeof (mp_limb_t
));
518 MPN_ZERO (tmp
, _FPIO_CONST_SHIFT
);
519 /* Adjust exponent, as scaleexpo will be this much
521 exponent
+= _FPIO_CONST_SHIFT
* BITS_PER_MP_LIMB
;
526 tmpsize
= powers
->arraysize
;
527 memcpy (tmp
, &__tens
[powers
->arrayoff
],
528 tmpsize
* sizeof (mp_limb_t
));
533 cy
= __mpn_mul (tmp
, scale
, scalesize
,
534 &__tens
[powers
->arrayoff
535 + _FPIO_CONST_OFFSET
],
536 powers
->arraysize
- _FPIO_CONST_OFFSET
);
537 tmpsize
= scalesize
+ powers
->arraysize
- _FPIO_CONST_OFFSET
;
542 if (MPN_GE (frac
, tmp
))
545 MPN_ASSIGN (scale
, tmp
);
546 count_leading_zeros (cnt
, scale
[scalesize
- 1]);
547 scaleexpo
= (scalesize
- 2) * BITS_PER_MP_LIMB
- cnt
- 1;
548 exp10
|= 1 << explog
;
553 while (powers
> &_fpioconst_pow10
[0]);
556 /* Optimize number representations. We want to represent the numbers
557 with the lowest number of bytes possible without losing any
558 bytes. Also the highest bit in the scaling factor has to be set
559 (this is a requirement of the MPN division routines). */
562 /* Determine minimum number of zero bits at the end of
564 for (i
= 0; scale
[i
] == 0 && frac
[i
] == 0; i
++)
567 /* Determine number of bits the scaling factor is misplaced. */
568 count_leading_zeros (cnt_h
, scale
[scalesize
- 1]);
572 /* The highest bit of the scaling factor is already set. So
573 we only have to remove the trailing empty limbs. */
576 MPN_COPY_INCR (scale
, scale
+ i
, scalesize
- i
);
578 MPN_COPY_INCR (frac
, frac
+ i
, fracsize
- i
);
586 count_trailing_zeros (cnt_l
, scale
[i
]);
590 count_trailing_zeros (cnt_l2
, frac
[i
]);
596 count_trailing_zeros (cnt_l
, frac
[i
]);
598 /* Now shift the numbers to their optimal position. */
599 if (i
== 0 && BITS_PER_MP_LIMB
- cnt_h
> cnt_l
)
601 /* We cannot save any memory. So just roll both numbers
602 so that the scaling factor has its highest bit set. */
604 (void) __mpn_lshift (scale
, scale
, scalesize
, cnt_h
);
605 cy
= __mpn_lshift (frac
, frac
, fracsize
, cnt_h
);
607 frac
[fracsize
++] = cy
;
609 else if (BITS_PER_MP_LIMB
- cnt_h
<= cnt_l
)
611 /* We can save memory by removing the trailing zero limbs
612 and by packing the non-zero limbs which gain another
615 (void) __mpn_rshift (scale
, scale
+ i
, scalesize
- i
,
616 BITS_PER_MP_LIMB
- cnt_h
);
618 (void) __mpn_rshift (frac
, frac
+ i
, fracsize
- i
,
619 BITS_PER_MP_LIMB
- cnt_h
);
620 fracsize
-= frac
[fracsize
- i
- 1] == 0 ? i
+ 1 : i
;
624 /* We can only save the memory of the limbs which are zero.
625 The non-zero parts occupy the same number of limbs. */
627 (void) __mpn_rshift (scale
, scale
+ (i
- 1),
629 BITS_PER_MP_LIMB
- cnt_h
);
631 (void) __mpn_rshift (frac
, frac
+ (i
- 1),
633 BITS_PER_MP_LIMB
- cnt_h
);
634 fracsize
-= frac
[fracsize
- (i
- 1) - 1] == 0 ? i
: i
- 1;
639 else if (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 (frac
, fp_input
, fracsize
, to_shift
);
648 frac
[fracsize
++] = cy
;
649 assert (cy
== 1 || (frac
[fracsize
- 2] == 0 && frac
[0] == 0));
652 exponent
= -exponent
;
654 assert (powers
!= &_fpioconst_pow10
[0]);
659 if (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 (fracsize
< powers
->arraysize
- _FPIO_CONST_OFFSET
)
667 cy
= __mpn_mul (tmp
, &__tens
[powers
->arrayoff
668 + _FPIO_CONST_OFFSET
],
669 powers
->arraysize
- _FPIO_CONST_OFFSET
,
672 cy
= __mpn_mul (tmp
, frac
, fracsize
,
673 &__tens
[powers
->arrayoff
+ _FPIO_CONST_OFFSET
],
674 powers
->arraysize
- _FPIO_CONST_OFFSET
);
675 tmpsize
= fracsize
+ powers
->arraysize
- _FPIO_CONST_OFFSET
;
679 count_leading_zeros (cnt_h
, tmp
[tmpsize
- 1]);
680 incr
= (tmpsize
- fracsize
) * BITS_PER_MP_LIMB
681 + BITS_PER_MP_LIMB
- 1 - cnt_h
;
683 assert (incr
<= powers
->p_expo
);
685 /* If we increased the 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
== 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
< exponent
+ 3
711 || (incr
== exponent
+ 3 &&
712 (tmp
[tmpsize
- 1] < topval
[1]
713 || (tmp
[tmpsize
- 1] == topval
[1]
714 && tmp
[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. */
726 /* Now we optimize the number representation. */
727 for (i
= 0; tmp
[i
] == 0; ++i
);
728 if (cnt_h
== BITS_PER_MP_LIMB
- 1)
730 MPN_COPY (frac
, tmp
+ i
, tmpsize
- i
);
731 fracsize
= tmpsize
- i
;
735 count_trailing_zeros (cnt_l
, 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 (frac
, tmp
, tmpsize
, cnt_h
+ 1);
745 fracsize
= tmpsize
+ 1;
746 frac
[fracsize
- 1] = cy
;
748 else if (BITS_PER_MP_LIMB
- 1 - cnt_h
<= cnt_l
)
750 (void) __mpn_rshift (frac
, tmp
+ i
, tmpsize
- i
,
751 BITS_PER_MP_LIMB
- 1 - cnt_h
);
752 fracsize
= tmpsize
- i
;
756 /* We can only save the memory of the limbs which
757 are zero. The non-zero parts occupy the same
760 (void) __mpn_rshift (frac
, tmp
+ (i
- 1),
762 BITS_PER_MP_LIMB
- 1 - cnt_h
);
763 fracsize
= tmpsize
- (i
- 1);
770 while (powers
!= &_fpioconst_pow10
[1] && exponent
> 0);
771 /* All factors but 10^-1 are tested now. */
776 cy
= __mpn_mul_1 (tmp
, frac
, fracsize
, 10);
778 assert (cy
== 0 || tmp
[tmpsize
- 1] < 20);
780 count_trailing_zeros (cnt_l
, tmp
[0]);
781 if (cnt_l
< MIN (4, exponent
))
783 cy
= __mpn_lshift (frac
, tmp
, tmpsize
,
784 BITS_PER_MP_LIMB
- MIN (4, exponent
));
786 frac
[tmpsize
++] = cy
;
789 (void) __mpn_rshift (frac
, tmp
, tmpsize
, MIN (4, exponent
));
792 assert (frac
[fracsize
- 1] < 10);
798 /* This is a special case. We don't need a factor because the
799 numbers are in the range of 1.0 <= |fp| < 8.0. We simply
800 shift it to the right place and divide it by 1.0 to get the
801 leading digit. (Of course this division is not really made.) */
802 assert (0 <= exponent
&& exponent
< 3 &&
803 exponent
+ to_shift
< BITS_PER_MP_LIMB
);
805 /* Now shift the input value to its right place. */
806 cy
= __mpn_lshift (frac
, fp_input
, fracsize
, (exponent
+ to_shift
));
807 frac
[fracsize
++] = cy
;
812 int width
= info
->width
;
813 wchar_t *wstartp
, *wcp
;
816 int intdig_max
, intdig_no
= 0;
822 char spec
= _tolower (info
->spec
);
828 fracdig_min
= fracdig_max
= info
->prec
< 0 ? 6 : info
->prec
;
829 chars_needed
= 1 + 1 + (size_t) fracdig_max
+ 1 + 1 + 4;
830 /* d . ddd e +- ddd */
831 dig_max
= INT_MAX
; /* Unlimited. */
832 significant
= 1; /* Does not matter here. */
834 else if (spec
== 'f')
837 fracdig_min
= fracdig_max
= info
->prec
< 0 ? 6 : info
->prec
;
838 dig_max
= INT_MAX
; /* Unlimited. */
839 significant
= 1; /* Does not matter here. */
842 intdig_max
= exponent
+ 1;
843 /* This can be really big! */ /* XXX Maybe malloc if too big? */
844 chars_needed
= (size_t) exponent
+ 1 + 1 + (size_t) fracdig_max
;
849 chars_needed
= 1 + 1 + (size_t) fracdig_max
;
854 dig_max
= info
->prec
< 0 ? 6 : (info
->prec
== 0 ? 1 : info
->prec
);
855 if ((expsign
== 0 && exponent
>= dig_max
)
856 || (expsign
!= 0 && exponent
> 4))
858 if ('g' - 'G' == 'e' - 'E')
859 type
= 'E' + (info
->spec
- 'G');
861 type
= isupper (info
->spec
) ? 'E' : 'e';
862 fracdig_max
= dig_max
- 1;
864 chars_needed
= 1 + 1 + (size_t) fracdig_max
+ 1 + 1 + 4;
869 intdig_max
= expsign
== 0 ? exponent
+ 1 : 0;
870 fracdig_max
= dig_max
- intdig_max
;
871 /* We need space for the significant digits and perhaps
872 for leading zeros when < 1.0. The number of leading
873 zeros can be as many as would be required for
874 exponential notation with a negative two-digit
875 exponent, which is 4. */
876 chars_needed
= (size_t) dig_max
+ 1 + 4;
878 fracdig_min
= info
->alt
? fracdig_max
: 0;
879 significant
= 0; /* We count significant digits. */
884 /* Guess the number of groups we will make, and thus how
885 many spaces we need for separator characters. */
886 ngroups
= __guess_grouping (intdig_max
, grouping
);
887 /* Allocate one more character in case rounding increases the
889 chars_needed
+= ngroups
+ 1;
892 /* Allocate buffer for output. We need two more because while rounding
893 it is possible that we need two more characters in front of all the
894 other output. If the amount of memory we have to allocate is too
895 large use `malloc' instead of `alloca'. */
896 if (__builtin_expect (chars_needed
>= (size_t) -1 / sizeof (wchar_t) - 2
897 || chars_needed
< fracdig_max
, 0))
899 /* Some overflow occurred. */
900 __set_errno (ERANGE
);
903 size_t wbuffer_to_alloc
= (2 + chars_needed
) * sizeof (wchar_t);
904 buffer_malloced
= ! __libc_use_alloca (wbuffer_to_alloc
);
905 if (__builtin_expect (buffer_malloced
, 0))
907 wbuffer
= (wchar_t *) malloc (wbuffer_to_alloc
);
909 /* Signal an error to the caller. */
913 wbuffer
= (wchar_t *) alloca (wbuffer_to_alloc
);
914 wcp
= wstartp
= wbuffer
+ 2; /* Let room for rounding. */
916 /* Do the real work: put digits in allocated buffer. */
917 if (expsign
== 0 || type
!= 'f')
919 assert (expsign
== 0 || intdig_max
== 1);
920 while (intdig_no
< intdig_max
)
923 *wcp
++ = hack_digit ();
928 || (fracdig_max
> 0 && (fracsize
> 1 || frac
[0] != 0)))
933 /* |fp| < 1.0 and the selected type is 'f', so put "0."
940 /* Generate the needed number of fractional digits. */
943 while (fracdig_no
< fracdig_min
+ added_zeros
944 || (fracdig_no
< fracdig_max
&& (fracsize
> 1 || frac
[0] != 0)))
947 *wcp
= hack_digit ();
950 else if (significant
== 0)
959 digit
= hack_digit ();
965 && ((*(wcp
- 1) != decimalwc
&& (*(wcp
- 1) & 1) == 0)
966 || ((*(wcp
- 1) == decimalwc
&& (*(wcp
- 2) & 1) == 0))))
968 /* This is the critical case. */
969 if (fracsize
== 1 && frac
[0] == 0)
970 /* Rest of the number is zero -> round to even.
971 (IEEE 754-1985 4.1 says this is the default rounding.) */
973 else if (scalesize
== 0)
975 /* Here we have to see whether all limbs are zero since no
976 normalization happened. */
977 size_t lcnt
= fracsize
;
978 while (lcnt
>= 1 && frac
[lcnt
- 1] == 0)
981 /* Rest of the number is zero -> round to even.
982 (IEEE 754-1985 4.1 says this is the default rounding.) */
989 /* Process fractional digits. Terminate if not rounded or
990 radix character is reached. */
992 while (*--wtp
!= decimalwc
&& *wtp
== L
'9')
997 if (removed
== fracdig_min
&& added_zeros
> 0)
999 if (*wtp
!= decimalwc
)
1002 else if (__builtin_expect (spec
== 'g' && type
== 'f' && info
->alt
1003 && wtp
== wstartp
+ 1
1004 && wstartp
[0] == L
'0',
1006 /* This is a special case: the rounded number is 1.0,
1007 the format is 'g' or 'G', and the alternative format
1008 is selected. This means the result must be "1.". */
1012 if (fracdig_no
== 0 || *wtp
== decimalwc
)
1014 /* Round the integer digits. */
1015 if (*(wtp
- 1) == decimalwc
)
1018 while (--wtp
>= wstartp
&& *wtp
== L
'9')
1025 /* It is more critical. All digits were 9's. */
1030 exponent
+= expsign
== 0 ? 1 : -1;
1032 /* The above exponent adjustment could lead to 1.0e-00,
1033 e.g. for 0.999999999. Make sure exponent 0 always
1038 else if (intdig_no
== dig_max
)
1040 /* This is the case where for type %g the number fits
1041 really in the range for %f output but after rounding
1042 the number of digits is too big. */
1043 *--wstartp
= decimalwc
;
1046 if (info
->alt
|| fracdig_no
> 0)
1048 /* Overwrite the old radix character. */
1049 wstartp
[intdig_no
+ 2] = L
'0';
1053 fracdig_no
+= intdig_no
;
1055 fracdig_max
= intdig_max
- intdig_no
;
1057 /* Now we must print the exponent. */
1058 type
= isupper (info
->spec
) ? 'E' : 'e';
1062 /* We can simply add another another digit before the
1068 /* While rounding the number of digits can change.
1069 If the number now exceeds the limits remove some
1070 fractional digits. */
1071 if (intdig_no
+ fracdig_no
> dig_max
)
1073 wcp
-= intdig_no
+ fracdig_no
- dig_max
;
1074 fracdig_no
-= intdig_no
+ fracdig_no
- dig_max
;
1081 /* Now remove unnecessary '0' at the end of the string. */
1082 while (fracdig_no
> fracdig_min
+ added_zeros
&& *(wcp
- 1) == L
'0')
1087 /* If we eliminate all fractional digits we perhaps also can remove
1088 the radix character. */
1089 if (fracdig_no
== 0 && !info
->alt
&& *(wcp
- 1) == decimalwc
)
1094 /* Rounding might have changed the number of groups. We allocated
1095 enough memory but we need here the correct number of groups. */
1096 if (intdig_no
!= intdig_max
)
1097 ngroups
= __guess_grouping (intdig_no
, grouping
);
1099 /* Add in separator characters, overwriting the same buffer. */
1100 wcp
= group_number (wstartp
, wcp
, intdig_no
, grouping
, thousands_sepwc
,
1104 /* Write the exponent if it is needed. */
1107 if (__builtin_expect (expsign
!= 0 && exponent
== 4 && spec
== 'g', 0))
1109 /* This is another special case. The exponent of the number is
1110 really smaller than -4, which requires the 'e'/'E' format.
1111 But after rounding the number has an exponent of -4. */
1112 assert (wcp
>= wstartp
+ 1);
1113 assert (wstartp
[0] == L
'1');
1114 __wmemcpy (wstartp
, L
"0.0001", 6);
1115 wstartp
[1] = decimalwc
;
1116 if (wcp
>= wstartp
+ 2)
1118 wmemset (wstartp
+ 6, L
'0', wcp
- (wstartp
+ 2));
1126 *wcp
++ = (wchar_t) type
;
1127 *wcp
++ = expsign
? L
'-' : L
'+';
1129 /* Find the magnitude of the exponent. */
1131 while (expscale
<= exponent
)
1135 /* Exponent always has at least two digits. */
1141 *wcp
++ = L
'0' + (exponent
/ expscale
);
1142 exponent
%= expscale
;
1144 while (expscale
> 10);
1145 *wcp
++ = L
'0' + exponent
;
1149 /* Compute number of characters which must be filled with the padding
1151 if (is_neg
|| info
->showsign
|| info
->space
)
1153 width
-= wcp
- wstartp
;
1155 if (!info
->left
&& info
->pad
!= '0' && width
> 0)
1156 PADN (info
->pad
, width
);
1160 else if (info
->showsign
)
1162 else if (info
->space
)
1165 if (!info
->left
&& info
->pad
== '0' && width
> 0)
1169 char *buffer
= NULL
;
1170 char *buffer_end
= NULL
;
1176 /* Create the single byte string. */
1178 size_t thousands_sep_len
;
1180 size_t factor
= (info
->i18n
1181 ? _NL_CURRENT_WORD (LC_CTYPE
, _NL_CTYPE_MB_CUR_MAX
)
1184 decimal_len
= strlen (decimal
);
1186 if (thousands_sep
== NULL
)
1187 thousands_sep_len
= 0;
1189 thousands_sep_len
= strlen (thousands_sep
);
1191 size_t nbuffer
= (2 + chars_needed
* factor
+ decimal_len
1192 + ngroups
* thousands_sep_len
);
1193 if (__builtin_expect (buffer_malloced
, 0))
1195 buffer
= (char *) malloc (nbuffer
);
1198 /* Signal an error to the caller. */
1204 buffer
= (char *) alloca (nbuffer
);
1205 buffer_end
= buffer
+ nbuffer
;
1207 /* Now copy the wide character string. Since the character
1208 (except for the decimal point and thousands separator) must
1209 be coming from the ASCII range we can esily convert the
1210 string without mapping tables. */
1211 for (cp
= buffer
, copywc
= wstartp
; copywc
< wcp
; ++copywc
)
1212 if (*copywc
== decimalwc
)
1213 cp
= (char *) __mempcpy (cp
, decimal
, decimal_len
);
1214 else if (*copywc
== thousands_sepwc
)
1215 cp
= (char *) __mempcpy (cp
, thousands_sep
, thousands_sep_len
);
1217 *cp
++ = (char) *copywc
;
1221 if (__builtin_expect (info
->i18n
, 0))
1223 #ifdef COMPILE_WPRINTF
1224 wstartp
= _i18n_number_rewrite (wstartp
, wcp
,
1225 wbuffer
+ wbuffer_to_alloc
);
1226 wcp
= wbuffer
+ wbuffer_to_alloc
;
1227 assert ((uintptr_t) wbuffer
<= (uintptr_t) wstartp
);
1228 assert ((uintptr_t) wstartp
1229 < (uintptr_t) wbuffer
+ wbuffer_to_alloc
);
1231 tmpptr
= _i18n_number_rewrite (tmpptr
, cp
, buffer_end
);
1233 assert ((uintptr_t) buffer
<= (uintptr_t) tmpptr
);
1234 assert ((uintptr_t) tmpptr
< (uintptr_t) buffer_end
);
1238 PRINT (tmpptr
, wstartp
, wide
? wcp
- wstartp
: cp
- tmpptr
);
1240 /* Free the memory if necessary. */
1241 if (__builtin_expect (buffer_malloced
, 0))
1248 if (info
->left
&& width
> 0)
1249 PADN (info
->pad
, width
);
1253 ldbl_hidden_def (___printf_fp
, __printf_fp
)
1254 ldbl_strong_alias (___printf_fp
, __printf_fp
)
1256 /* Return the number of extra grouping characters that will be inserted
1257 into a number with INTDIG_MAX integer digits. */
1260 __guess_grouping (unsigned int intdig_max
, const char *grouping
)
1262 unsigned int groups
;
1264 /* We treat all negative values like CHAR_MAX. */
1266 if (*grouping
== CHAR_MAX
|| *grouping
<= 0)
1267 /* No grouping should be done. */
1271 while (intdig_max
> (unsigned int) *grouping
)
1274 intdig_max
-= *grouping
++;
1276 if (*grouping
== CHAR_MAX
1281 /* No more grouping should be done. */
1283 else if (*grouping
== 0)
1285 /* Same grouping repeats. */
1286 groups
+= (intdig_max
- 1) / grouping
[-1];
1294 /* Group the INTDIG_NO integer digits of the number in [BUF,BUFEND).
1295 There is guaranteed enough space past BUFEND to extend it.
1296 Return the new end of buffer. */
1300 group_number (wchar_t *buf
, wchar_t *bufend
, unsigned int intdig_no
,
1301 const char *grouping
, wchar_t thousands_sep
, int ngroups
)
1308 /* Move the fractional part down. */
1309 __wmemmove (buf
+ intdig_no
+ ngroups
, buf
+ intdig_no
,
1310 bufend
- (buf
+ intdig_no
));
1312 p
= buf
+ intdig_no
+ ngroups
- 1;
1315 unsigned int len
= *grouping
++;
1317 *p
-- = buf
[--intdig_no
];
1319 *p
-- = thousands_sep
;
1321 if (*grouping
== CHAR_MAX
1326 /* No more grouping should be done. */
1328 else if (*grouping
== 0)
1329 /* Same grouping repeats. */
1331 } while (intdig_no
> (unsigned int) *grouping
);
1333 /* Copy the remaining ungrouped digits. */
1335 *p
-- = buf
[--intdig_no
];
1338 return bufend
+ ngroups
;