1 /* Floating point output for `printf'.
2 Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2006
3 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>
31 #include <stdlib/gmp-impl.h>
32 #include <stdlib/longlong.h>
33 #include <stdlib/fpioconst.h>
34 #include <locale/localeinfo.h>
43 #ifdef COMPILE_WPRINTF
44 # define CHAR_T wchar_t
49 #include "_i18n_number.h"
52 # define NDEBUG /* Undefine this for debugging assertions. */
56 /* This defines make it possible to use the same code for GNU C library and
57 the GNU I/O library. */
58 #define PUT(f, s, n) _IO_sputn (f, s, n)
59 #define PAD(f, c, n) (wide ? _IO_wpadn (f, c, n) : INTUSE(_IO_padn) (f, c, n))
60 /* We use this file GNU C library and GNU I/O library. So make
63 #define putc(c, f) (wide \
64 ? (int)_IO_putwc_unlocked (c, f) : _IO_putc_unlocked (c, f))
65 #define size_t _IO_size_t
68 /* Macros for doing the actual output. */
73 register const int outc = (ch); \
74 if (putc (outc, fp) == EOF) \
79 #define PRINT(ptr, wptr, len) \
82 register size_t outlen = (len); \
85 if (PUT (fp, wide ? (const char *) wptr : ptr, outlen) != outlen) \
93 while (outlen-- > 0) \
96 while (outlen-- > 0) \
101 #define PADN(ch, len) \
104 if (PAD (fp, ch, len) != len) \
110 /* We use the GNU MP library to handle large numbers.
112 An MP variable occupies a varying number of entries in its array. We keep
113 track of this number for efficiency reasons. Otherwise we would always
114 have to process the whole array. */
115 #define MPN_VAR(name) mp_limb_t *name; mp_size_t name##size
117 #define MPN_ASSIGN(dst,src) \
118 memcpy (dst, src, (dst##size = src##size) * sizeof (mp_limb_t))
119 #define MPN_GE(u,v) \
120 (u##size > v##size || (u##size == v##size && __mpn_cmp (u, v, u##size) >= 0))
122 extern int __isinfl_internal (long double) attribute_hidden
;
123 extern int __isnanl_internal (long double) attribute_hidden
;
125 extern mp_size_t
__mpn_extract_double (mp_ptr res_ptr
, mp_size_t size
,
126 int *expt
, int *is_neg
,
128 extern mp_size_t
__mpn_extract_long_double (mp_ptr res_ptr
, mp_size_t size
,
129 int *expt
, int *is_neg
,
131 extern unsigned int __guess_grouping (unsigned int intdig_max
,
132 const char *grouping
);
135 static wchar_t *group_number (wchar_t *buf
, wchar_t *bufend
,
136 unsigned int intdig_no
, const char *grouping
,
137 wchar_t thousands_sep
, int ngroups
)
142 ___printf_fp (FILE *fp
,
143 const struct printf_info
*info
,
144 const void *const *args
)
146 /* The floating-point value to output. */
150 __long_double_t ldbl
;
154 /* Locale-dependent representation of decimal point. */
158 /* Locale-dependent thousands separator and grouping specification. */
159 const char *thousands_sep
= NULL
;
160 wchar_t thousands_sepwc
= 0;
161 const char *grouping
;
163 /* "NaN" or "Inf" for the special cases. */
164 const char *special
= NULL
;
165 const wchar_t *wspecial
= NULL
;
167 /* We need just a few limbs for the input before shifting to the right
169 mp_limb_t fp_input
[(LDBL_MANT_DIG
+ BITS_PER_MP_LIMB
- 1) / BITS_PER_MP_LIMB
];
170 /* We need to shift the contents of fp_input by this amount of bits. */
173 /* The fraction of the floting-point value in question */
175 /* and the exponent. */
177 /* Sign of the exponent. */
179 /* Sign of float number. */
182 /* Scaling factor. */
185 /* Temporary bignum value. */
188 /* Digit which is result of last hack_digit() call. */
191 /* The type of output format that will be used: 'e'/'E' or 'f'. */
194 /* Counter for number of written characters. */
197 /* General helper (carry limb). */
200 /* Nonzero if this is output on a wide character stream. */
201 int wide
= info
->wide
;
203 auto wchar_t hack_digit (void);
205 wchar_t hack_digit (void)
209 if (expsign
!= 0 && type
== 'f' && exponent
-- > 0)
211 else if (scalesize
== 0)
213 hi
= frac
[fracsize
- 1];
214 frac
[fracsize
- 1] = __mpn_mul_1 (frac
, frac
, fracsize
- 1, 10);
218 if (fracsize
< scalesize
)
222 hi
= mpn_divmod (tmp
, frac
, fracsize
, scale
, scalesize
);
223 tmp
[fracsize
- scalesize
] = hi
;
226 fracsize
= scalesize
;
227 while (fracsize
!= 0 && frac
[fracsize
- 1] == 0)
231 /* We're not prepared for an mpn variable with zero
238 mp_limb_t _cy
= __mpn_mul_1 (frac
, frac
, fracsize
, 10);
240 frac
[fracsize
++] = _cy
;
247 /* Figure out the decimal point character. */
248 if (info
->extra
== 0)
250 decimal
= _NL_CURRENT (LC_NUMERIC
, DECIMAL_POINT
);
251 decimalwc
= _NL_CURRENT_WORD (LC_NUMERIC
, _NL_NUMERIC_DECIMAL_POINT_WC
);
255 decimal
= _NL_CURRENT (LC_MONETARY
, MON_DECIMAL_POINT
);
256 if (*decimal
== '\0')
257 decimal
= _NL_CURRENT (LC_NUMERIC
, DECIMAL_POINT
);
258 decimalwc
= _NL_CURRENT_WORD (LC_MONETARY
,
259 _NL_MONETARY_DECIMAL_POINT_WC
);
260 if (decimalwc
== L
'\0')
261 decimalwc
= _NL_CURRENT_WORD (LC_NUMERIC
,
262 _NL_NUMERIC_DECIMAL_POINT_WC
);
264 /* The decimal point character must not be zero. */
265 assert (*decimal
!= '\0');
266 assert (decimalwc
!= L
'\0');
270 if (info
->extra
== 0)
271 grouping
= _NL_CURRENT (LC_NUMERIC
, GROUPING
);
273 grouping
= _NL_CURRENT (LC_MONETARY
, MON_GROUPING
);
275 if (*grouping
<= 0 || *grouping
== CHAR_MAX
)
279 /* Figure out the thousands separator character. */
282 if (info
->extra
== 0)
284 _NL_CURRENT_WORD (LC_NUMERIC
, _NL_NUMERIC_THOUSANDS_SEP_WC
);
287 _NL_CURRENT_WORD (LC_MONETARY
,
288 _NL_MONETARY_THOUSANDS_SEP_WC
);
292 if (info
->extra
== 0)
293 thousands_sep
= _NL_CURRENT (LC_NUMERIC
, THOUSANDS_SEP
);
295 thousands_sep
= _NL_CURRENT (LC_MONETARY
, MON_THOUSANDS_SEP
);
298 if ((wide
&& thousands_sepwc
== L
'\0')
299 || (! wide
&& *thousands_sep
== '\0'))
301 else if (thousands_sepwc
== L
'\0')
302 /* If we are printing multibyte characters and there is a
303 multibyte representation for the thousands separator,
304 we must ensure the wide character thousands separator
305 is available, even if it is fake. */
306 thousands_sepwc
= 0xfffffffe;
312 /* Fetch the argument value. */
313 #ifndef __NO_LONG_DOUBLE_MATH
314 if (info
->is_long_double
&& sizeof (long double) > sizeof (double))
316 fpnum
.ldbl
= *(const long double *) args
[0];
318 /* Check for special values: not a number or infinity. */
319 if (__isnanl (fpnum
.ldbl
))
321 if (isupper (info
->spec
))
333 else if (__isinfl (fpnum
.ldbl
))
335 if (isupper (info
->spec
))
345 is_neg
= fpnum
.ldbl
< 0;
349 fracsize
= __mpn_extract_long_double (fp_input
,
351 sizeof (fp_input
[0])),
354 to_shift
= 1 + fracsize
* BITS_PER_MP_LIMB
- LDBL_MANT_DIG
;
358 #endif /* no long double */
360 fpnum
.dbl
= *(const double *) args
[0];
362 /* Check for special values: not a number or infinity. */
363 if (__isnan (fpnum
.dbl
))
366 if (isupper (info
->spec
))
377 else if (__isinf (fpnum
.dbl
))
379 is_neg
= fpnum
.dbl
< 0;
380 if (isupper (info
->spec
))
393 fracsize
= __mpn_extract_double (fp_input
,
395 / sizeof (fp_input
[0])),
396 &exponent
, &is_neg
, fpnum
.dbl
);
397 to_shift
= 1 + fracsize
* BITS_PER_MP_LIMB
- DBL_MANT_DIG
;
403 int width
= info
->width
;
405 if (is_neg
|| info
->showsign
|| info
->space
)
409 if (!info
->left
&& width
> 0)
414 else if (info
->showsign
)
416 else if (info
->space
)
419 PRINT (special
, wspecial
, 3);
421 if (info
->left
&& width
> 0)
428 /* We need three multiprecision variables. Now that we have the exponent
429 of the number we can allocate the needed memory. It would be more
430 efficient to use variables of the fixed maximum size but because this
431 would be really big it could lead to memory problems. */
433 mp_size_t bignum_size
= ((ABS (exponent
) + BITS_PER_MP_LIMB
- 1)
435 + (LDBL_MANT_DIG
/ BITS_PER_MP_LIMB
> 2 ? 8 : 4))
436 * sizeof (mp_limb_t
);
437 frac
= (mp_limb_t
*) alloca (bignum_size
);
438 tmp
= (mp_limb_t
*) alloca (bignum_size
);
439 scale
= (mp_limb_t
*) alloca (bignum_size
);
442 /* We now have to distinguish between numbers with positive and negative
443 exponents because the method used for the one is not applicable/efficient
450 int explog
= LDBL_MAX_10_EXP_LOG
;
452 const struct mp_power
*powers
= &_fpioconst_pow10
[explog
+ 1];
455 if ((exponent
+ to_shift
) % BITS_PER_MP_LIMB
== 0)
457 MPN_COPY_DECR (frac
+ (exponent
+ to_shift
) / BITS_PER_MP_LIMB
,
459 fracsize
+= (exponent
+ to_shift
) / BITS_PER_MP_LIMB
;
463 cy
= __mpn_lshift (frac
+ (exponent
+ to_shift
) / BITS_PER_MP_LIMB
,
465 (exponent
+ to_shift
) % BITS_PER_MP_LIMB
);
466 fracsize
+= (exponent
+ to_shift
) / BITS_PER_MP_LIMB
;
468 frac
[fracsize
++] = cy
;
470 MPN_ZERO (frac
, (exponent
+ to_shift
) / BITS_PER_MP_LIMB
);
472 assert (powers
> &_fpioconst_pow10
[0]);
477 /* The number of the product of two binary numbers with n and m
478 bits respectively has m+n or m+n-1 bits. */
479 if (exponent
>= scaleexpo
+ powers
->p_expo
- 1)
483 #ifndef __NO_LONG_DOUBLE_MATH
484 if (LDBL_MANT_DIG
> _FPIO_CONST_OFFSET
* BITS_PER_MP_LIMB
485 && info
->is_long_double
)
487 #define _FPIO_CONST_SHIFT \
488 (((LDBL_MANT_DIG + BITS_PER_MP_LIMB - 1) / BITS_PER_MP_LIMB) \
489 - _FPIO_CONST_OFFSET)
490 /* 64bit const offset is not enough for
491 IEEE quad long double. */
492 tmpsize
= powers
->arraysize
+ _FPIO_CONST_SHIFT
;
493 memcpy (tmp
+ _FPIO_CONST_SHIFT
,
494 &__tens
[powers
->arrayoff
],
495 tmpsize
* sizeof (mp_limb_t
));
496 MPN_ZERO (tmp
, _FPIO_CONST_SHIFT
);
497 /* Adjust exponent, as scaleexpo will be this much
499 exponent
+= _FPIO_CONST_SHIFT
* BITS_PER_MP_LIMB
;
504 tmpsize
= powers
->arraysize
;
505 memcpy (tmp
, &__tens
[powers
->arrayoff
],
506 tmpsize
* sizeof (mp_limb_t
));
511 cy
= __mpn_mul (tmp
, scale
, scalesize
,
512 &__tens
[powers
->arrayoff
513 + _FPIO_CONST_OFFSET
],
514 powers
->arraysize
- _FPIO_CONST_OFFSET
);
515 tmpsize
= scalesize
+ powers
->arraysize
- _FPIO_CONST_OFFSET
;
520 if (MPN_GE (frac
, tmp
))
523 MPN_ASSIGN (scale
, tmp
);
524 count_leading_zeros (cnt
, scale
[scalesize
- 1]);
525 scaleexpo
= (scalesize
- 2) * BITS_PER_MP_LIMB
- cnt
- 1;
526 exp10
|= 1 << explog
;
531 while (powers
> &_fpioconst_pow10
[0]);
534 /* Optimize number representations. We want to represent the numbers
535 with the lowest number of bytes possible without losing any
536 bytes. Also the highest bit in the scaling factor has to be set
537 (this is a requirement of the MPN division routines). */
540 /* Determine minimum number of zero bits at the end of
542 for (i
= 0; scale
[i
] == 0 && frac
[i
] == 0; i
++)
545 /* Determine number of bits the scaling factor is misplaced. */
546 count_leading_zeros (cnt_h
, scale
[scalesize
- 1]);
550 /* The highest bit of the scaling factor is already set. So
551 we only have to remove the trailing empty limbs. */
554 MPN_COPY_INCR (scale
, scale
+ i
, scalesize
- i
);
556 MPN_COPY_INCR (frac
, frac
+ i
, fracsize
- i
);
564 count_trailing_zeros (cnt_l
, scale
[i
]);
568 count_trailing_zeros (cnt_l2
, frac
[i
]);
574 count_trailing_zeros (cnt_l
, frac
[i
]);
576 /* Now shift the numbers to their optimal position. */
577 if (i
== 0 && BITS_PER_MP_LIMB
- cnt_h
> cnt_l
)
579 /* We cannot save any memory. So just roll both numbers
580 so that the scaling factor has its highest bit set. */
582 (void) __mpn_lshift (scale
, scale
, scalesize
, cnt_h
);
583 cy
= __mpn_lshift (frac
, frac
, fracsize
, cnt_h
);
585 frac
[fracsize
++] = cy
;
587 else if (BITS_PER_MP_LIMB
- cnt_h
<= cnt_l
)
589 /* We can save memory by removing the trailing zero limbs
590 and by packing the non-zero limbs which gain another
593 (void) __mpn_rshift (scale
, scale
+ i
, scalesize
- i
,
594 BITS_PER_MP_LIMB
- cnt_h
);
596 (void) __mpn_rshift (frac
, frac
+ i
, fracsize
- i
,
597 BITS_PER_MP_LIMB
- cnt_h
);
598 fracsize
-= frac
[fracsize
- i
- 1] == 0 ? i
+ 1 : i
;
602 /* We can only save the memory of the limbs which are zero.
603 The non-zero parts occupy the same number of limbs. */
605 (void) __mpn_rshift (scale
, scale
+ (i
- 1),
607 BITS_PER_MP_LIMB
- cnt_h
);
609 (void) __mpn_rshift (frac
, frac
+ (i
- 1),
611 BITS_PER_MP_LIMB
- cnt_h
);
612 fracsize
-= frac
[fracsize
- (i
- 1) - 1] == 0 ? i
: i
- 1;
617 else if (exponent
< 0)
621 int explog
= LDBL_MAX_10_EXP_LOG
;
622 const struct mp_power
*powers
= &_fpioconst_pow10
[explog
+ 1];
623 mp_size_t used_limbs
= fracsize
- 1;
625 /* Now shift the input value to its right place. */
626 cy
= __mpn_lshift (frac
, fp_input
, fracsize
, to_shift
);
627 frac
[fracsize
++] = cy
;
628 assert (cy
== 1 || (frac
[fracsize
- 2] == 0 && frac
[0] == 0));
631 exponent
= -exponent
;
633 assert (powers
!= &_fpioconst_pow10
[0]);
638 if (exponent
>= powers
->m_expo
)
640 int i
, incr
, cnt_h
, cnt_l
;
643 /* The __mpn_mul function expects the first argument to be
644 bigger than the second. */
645 if (fracsize
< powers
->arraysize
- _FPIO_CONST_OFFSET
)
646 cy
= __mpn_mul (tmp
, &__tens
[powers
->arrayoff
647 + _FPIO_CONST_OFFSET
],
648 powers
->arraysize
- _FPIO_CONST_OFFSET
,
651 cy
= __mpn_mul (tmp
, frac
, fracsize
,
652 &__tens
[powers
->arrayoff
+ _FPIO_CONST_OFFSET
],
653 powers
->arraysize
- _FPIO_CONST_OFFSET
);
654 tmpsize
= fracsize
+ powers
->arraysize
- _FPIO_CONST_OFFSET
;
658 count_leading_zeros (cnt_h
, tmp
[tmpsize
- 1]);
659 incr
= (tmpsize
- fracsize
) * BITS_PER_MP_LIMB
660 + BITS_PER_MP_LIMB
- 1 - cnt_h
;
662 assert (incr
<= powers
->p_expo
);
664 /* If we increased the exponent by exactly 3 we have to test
665 for overflow. This is done by comparing with 10 shifted
666 to the right position. */
667 if (incr
== exponent
+ 3)
669 if (cnt_h
<= BITS_PER_MP_LIMB
- 4)
673 = ((mp_limb_t
) 10) << (BITS_PER_MP_LIMB
- 4 - cnt_h
);
677 topval
[0] = ((mp_limb_t
) 10) << (BITS_PER_MP_LIMB
- 4);
679 (void) __mpn_lshift (topval
, topval
, 2,
680 BITS_PER_MP_LIMB
- cnt_h
);
684 /* We have to be careful when multiplying the last factor.
685 If the result is greater than 1.0 be have to test it
686 against 10.0. If it is greater or equal to 10.0 the
687 multiplication was not valid. This is because we cannot
688 determine the number of bits in the result in advance. */
689 if (incr
< exponent
+ 3
690 || (incr
== exponent
+ 3 &&
691 (tmp
[tmpsize
- 1] < topval
[1]
692 || (tmp
[tmpsize
- 1] == topval
[1]
693 && tmp
[tmpsize
- 2] < topval
[0]))))
695 /* The factor is right. Adapt binary and decimal
698 exp10
|= 1 << explog
;
700 /* If this factor yields a number greater or equal to
701 1.0, we must not shift the non-fractional digits down. */
705 /* Now we optimize the number representation. */
706 for (i
= 0; tmp
[i
] == 0; ++i
);
707 if (cnt_h
== BITS_PER_MP_LIMB
- 1)
709 MPN_COPY (frac
, tmp
+ i
, tmpsize
- i
);
710 fracsize
= tmpsize
- i
;
714 count_trailing_zeros (cnt_l
, tmp
[i
]);
716 /* Now shift the numbers to their optimal position. */
717 if (i
== 0 && BITS_PER_MP_LIMB
- 1 - cnt_h
> cnt_l
)
719 /* We cannot save any memory. Just roll the
720 number so that the leading digit is in a
723 cy
= __mpn_lshift (frac
, tmp
, tmpsize
, cnt_h
+ 1);
724 fracsize
= tmpsize
+ 1;
725 frac
[fracsize
- 1] = cy
;
727 else if (BITS_PER_MP_LIMB
- 1 - cnt_h
<= cnt_l
)
729 (void) __mpn_rshift (frac
, tmp
+ i
, tmpsize
- i
,
730 BITS_PER_MP_LIMB
- 1 - cnt_h
);
731 fracsize
= tmpsize
- i
;
735 /* We can only save the memory of the limbs which
736 are zero. The non-zero parts occupy the same
739 (void) __mpn_rshift (frac
, tmp
+ (i
- 1),
741 BITS_PER_MP_LIMB
- 1 - cnt_h
);
742 fracsize
= tmpsize
- (i
- 1);
745 used_limbs
= fracsize
- 1;
750 while (powers
!= &_fpioconst_pow10
[1] && exponent
> 0);
751 /* All factors but 10^-1 are tested now. */
756 cy
= __mpn_mul_1 (tmp
, frac
, fracsize
, 10);
758 assert (cy
== 0 || tmp
[tmpsize
- 1] < 20);
760 count_trailing_zeros (cnt_l
, tmp
[0]);
761 if (cnt_l
< MIN (4, exponent
))
763 cy
= __mpn_lshift (frac
, tmp
, tmpsize
,
764 BITS_PER_MP_LIMB
- MIN (4, exponent
));
766 frac
[tmpsize
++] = cy
;
769 (void) __mpn_rshift (frac
, tmp
, tmpsize
, MIN (4, exponent
));
772 assert (frac
[fracsize
- 1] < 10);
778 /* This is a special case. We don't need a factor because the
779 numbers are in the range of 0.0 <= fp < 8.0. We simply
780 shift it to the right place and divide it by 1.0 to get the
781 leading digit. (Of course this division is not really made.) */
782 assert (0 <= exponent
&& exponent
< 3 &&
783 exponent
+ to_shift
< BITS_PER_MP_LIMB
);
785 /* Now shift the input value to its right place. */
786 cy
= __mpn_lshift (frac
, fp_input
, fracsize
, (exponent
+ to_shift
));
787 frac
[fracsize
++] = cy
;
792 int width
= info
->width
;
793 wchar_t *wbuffer
, *wstartp
, *wcp
;
797 int intdig_max
, intdig_no
= 0;
798 int fracdig_min
, fracdig_max
, fracdig_no
= 0;
803 if (_tolower (info
->spec
) == 'e')
807 fracdig_min
= fracdig_max
= info
->prec
< 0 ? 6 : info
->prec
;
808 chars_needed
= 1 + 1 + fracdig_max
+ 1 + 1 + 4;
809 /* d . ddd e +- ddd */
810 dig_max
= INT_MAX
; /* Unlimited. */
811 significant
= 1; /* Does not matter here. */
813 else if (_tolower (info
->spec
) == 'f')
816 fracdig_min
= fracdig_max
= info
->prec
< 0 ? 6 : info
->prec
;
817 dig_max
= INT_MAX
; /* Unlimited. */
818 significant
= 1; /* Does not matter here. */
821 intdig_max
= exponent
+ 1;
822 /* This can be really big! */ /* XXX Maybe malloc if too big? */
823 chars_needed
= exponent
+ 1 + 1 + fracdig_max
;
828 chars_needed
= 1 + 1 + fracdig_max
;
833 dig_max
= info
->prec
< 0 ? 6 : (info
->prec
== 0 ? 1 : info
->prec
);
834 if ((expsign
== 0 && exponent
>= dig_max
)
835 || (expsign
!= 0 && exponent
> 4))
837 if ('g' - 'G' == 'e' - 'E')
838 type
= 'E' + (info
->spec
- 'G');
840 type
= isupper (info
->spec
) ? 'E' : 'e';
841 fracdig_max
= dig_max
- 1;
843 chars_needed
= 1 + 1 + fracdig_max
+ 1 + 1 + 4;
848 intdig_max
= expsign
== 0 ? exponent
+ 1 : 0;
849 fracdig_max
= dig_max
- intdig_max
;
850 /* We need space for the significant digits and perhaps
851 for leading zeros when < 1.0. The number of leading
852 zeros can be as many as would be required for
853 exponential notation with a negative two-digit
854 exponent, which is 4. */
855 chars_needed
= dig_max
+ 1 + 4;
857 fracdig_min
= info
->alt
? fracdig_max
: 0;
858 significant
= 0; /* We count significant digits. */
863 /* Guess the number of groups we will make, and thus how
864 many spaces we need for separator characters. */
865 ngroups
= __guess_grouping (intdig_max
, grouping
);
866 chars_needed
+= ngroups
;
869 /* Allocate buffer for output. We need two more because while rounding
870 it is possible that we need two more characters in front of all the
871 other output. If the amount of memory we have to allocate is too
872 large use `malloc' instead of `alloca'. */
873 buffer_malloced
= ! __libc_use_alloca (chars_needed
* 2 * sizeof (wchar_t));
876 wbuffer
= (wchar_t *) malloc ((2 + chars_needed
) * sizeof (wchar_t));
878 /* Signal an error to the caller. */
882 wbuffer
= (wchar_t *) alloca ((2 + chars_needed
) * sizeof (wchar_t));
883 wcp
= wstartp
= wbuffer
+ 2; /* Let room for rounding. */
885 /* Do the real work: put digits in allocated buffer. */
886 if (expsign
== 0 || type
!= 'f')
888 assert (expsign
== 0 || intdig_max
== 1);
889 while (intdig_no
< intdig_max
)
892 *wcp
++ = hack_digit ();
897 || (fracdig_max
> 0 && (fracsize
> 1 || frac
[0] != 0)))
902 /* |fp| < 1.0 and the selected type is 'f', so put "0."
909 /* Generate the needed number of fractional digits. */
910 while (fracdig_no
< fracdig_min
911 || (fracdig_no
< fracdig_max
&& (fracsize
> 1 || frac
[0] != 0)))
914 *wcp
= hack_digit ();
917 else if (significant
== 0)
926 digit
= hack_digit ();
932 && ((*(wcp
- 1) != decimalwc
&& (*(wcp
- 1) & 1) == 0)
933 || ((*(wcp
- 1) == decimalwc
&& (*(wcp
- 2) & 1) == 0))))
935 /* This is the critical case. */
936 if (fracsize
== 1 && frac
[0] == 0)
937 /* Rest of the number is zero -> round to even.
938 (IEEE 754-1985 4.1 says this is the default rounding.) */
940 else if (scalesize
== 0)
942 /* Here we have to see whether all limbs are zero since no
943 normalization happened. */
944 size_t lcnt
= fracsize
;
945 while (lcnt
>= 1 && frac
[lcnt
- 1] == 0)
948 /* Rest of the number is zero -> round to even.
949 (IEEE 754-1985 4.1 says this is the default rounding.) */
956 /* Process fractional digits. Terminate if not rounded or
957 radix character is reached. */
958 while (*--wtp
!= decimalwc
&& *wtp
== L
'9')
960 if (*wtp
!= decimalwc
)
965 if (fracdig_no
== 0 || *wtp
== decimalwc
)
967 /* Round the integer digits. */
968 if (*(wtp
- 1) == decimalwc
)
971 while (--wtp
>= wstartp
&& *wtp
== L
'9')
978 /* It is more critical. All digits were 9's. */
983 exponent
+= expsign
== 0 ? 1 : -1;
985 else if (intdig_no
== dig_max
)
987 /* This is the case where for type %g the number fits
988 really in the range for %f output but after rounding
989 the number of digits is too big. */
990 *--wstartp
= decimalwc
;
993 if (info
->alt
|| fracdig_no
> 0)
995 /* Overwrite the old radix character. */
996 wstartp
[intdig_no
+ 2] = L
'0';
1000 fracdig_no
+= intdig_no
;
1002 fracdig_max
= intdig_max
- intdig_no
;
1004 /* Now we must print the exponent. */
1005 type
= isupper (info
->spec
) ? 'E' : 'e';
1009 /* We can simply add another another digit before the
1015 /* While rounding the number of digits can change.
1016 If the number now exceeds the limits remove some
1017 fractional digits. */
1018 if (intdig_no
+ fracdig_no
> dig_max
)
1020 wcp
-= intdig_no
+ fracdig_no
- dig_max
;
1021 fracdig_no
-= intdig_no
+ fracdig_no
- dig_max
;
1028 /* Now remove unnecessary '0' at the end of the string. */
1029 while (fracdig_no
> fracdig_min
&& *(wcp
- 1) == L
'0')
1034 /* If we eliminate all fractional digits we perhaps also can remove
1035 the radix character. */
1036 if (fracdig_no
== 0 && !info
->alt
&& *(wcp
- 1) == decimalwc
)
1040 /* Add in separator characters, overwriting the same buffer. */
1041 wcp
= group_number (wstartp
, wcp
, intdig_no
, grouping
, thousands_sepwc
,
1044 /* Write the exponent if it is needed. */
1047 *wcp
++ = (wchar_t) type
;
1048 *wcp
++ = expsign
? L
'-' : L
'+';
1050 /* Find the magnitude of the exponent. */
1052 while (expscale
<= exponent
)
1056 /* Exponent always has at least two digits. */
1062 *wcp
++ = L
'0' + (exponent
/ expscale
);
1063 exponent
%= expscale
;
1065 while (expscale
> 10);
1066 *wcp
++ = L
'0' + exponent
;
1069 /* Compute number of characters which must be filled with the padding
1071 if (is_neg
|| info
->showsign
|| info
->space
)
1073 width
-= wcp
- wstartp
;
1075 if (!info
->left
&& info
->pad
!= '0' && width
> 0)
1076 PADN (info
->pad
, width
);
1080 else if (info
->showsign
)
1082 else if (info
->space
)
1085 if (!info
->left
&& info
->pad
== '0' && width
> 0)
1089 char *buffer
= NULL
;
1095 /* Create the single byte string. */
1097 size_t thousands_sep_len
;
1100 decimal_len
= strlen (decimal
);
1102 if (thousands_sep
== NULL
)
1103 thousands_sep_len
= 0;
1105 thousands_sep_len
= strlen (thousands_sep
);
1107 if (buffer_malloced
)
1109 buffer
= (char *) malloc (2 + chars_needed
+ decimal_len
1110 + ngroups
* thousands_sep_len
);
1112 /* Signal an error to the caller. */
1116 buffer
= (char *) alloca (2 + chars_needed
+ decimal_len
1117 + ngroups
* thousands_sep_len
);
1119 /* Now copy the wide character string. Since the character
1120 (except for the decimal point and thousands separator) must
1121 be coming from the ASCII range we can esily convert the
1122 string without mapping tables. */
1123 for (cp
= buffer
, copywc
= wstartp
; copywc
< wcp
; ++copywc
)
1124 if (*copywc
== decimalwc
)
1125 cp
= (char *) __mempcpy (cp
, decimal
, decimal_len
);
1126 else if (*copywc
== thousands_sepwc
)
1127 cp
= (char *) __mempcpy (cp
, thousands_sep
, thousands_sep_len
);
1129 *cp
++ = (char) *copywc
;
1133 if (__builtin_expect (info
->i18n
, 0))
1135 #ifdef COMPILE_WPRINTF
1136 wstartp
= _i18n_number_rewrite (wstartp
, wcp
);
1138 tmpptr
= _i18n_number_rewrite (tmpptr
, cp
);
1142 PRINT (tmpptr
, wstartp
, wide
? wcp
- wstartp
: cp
- tmpptr
);
1144 /* Free the memory if necessary. */
1145 if (buffer_malloced
)
1152 if (info
->left
&& width
> 0)
1153 PADN (info
->pad
, width
);
1157 ldbl_hidden_def (___printf_fp
, __printf_fp
)
1158 ldbl_strong_alias (___printf_fp
, __printf_fp
)
1160 /* Return the number of extra grouping characters that will be inserted
1161 into a number with INTDIG_MAX integer digits. */
1164 __guess_grouping (unsigned int intdig_max
, const char *grouping
)
1166 unsigned int groups
;
1168 /* We treat all negative values like CHAR_MAX. */
1170 if (*grouping
== CHAR_MAX
|| *grouping
<= 0)
1171 /* No grouping should be done. */
1175 while (intdig_max
> (unsigned int) *grouping
)
1178 intdig_max
-= *grouping
++;
1180 if (*grouping
== CHAR_MAX
1185 /* No more grouping should be done. */
1187 else if (*grouping
== 0)
1189 /* Same grouping repeats. */
1190 groups
+= (intdig_max
- 1) / grouping
[-1];
1198 /* Group the INTDIG_NO integer digits of the number in [BUF,BUFEND).
1199 There is guaranteed enough space past BUFEND to extend it.
1200 Return the new end of buffer. */
1204 group_number (wchar_t *buf
, wchar_t *bufend
, unsigned int intdig_no
,
1205 const char *grouping
, wchar_t thousands_sep
, int ngroups
)
1212 /* Move the fractional part down. */
1213 __wmemmove (buf
+ intdig_no
+ ngroups
, buf
+ intdig_no
,
1214 bufend
- (buf
+ intdig_no
));
1216 p
= buf
+ intdig_no
+ ngroups
- 1;
1219 unsigned int len
= *grouping
++;
1221 *p
-- = buf
[--intdig_no
];
1223 *p
-- = thousands_sep
;
1225 if (*grouping
== CHAR_MAX
1230 /* No more grouping should be done. */
1232 else if (*grouping
== 0)
1233 /* Same grouping repeats. */
1235 } while (intdig_no
> (unsigned int) *grouping
);
1237 /* Copy the remaining ungrouped digits. */
1239 *p
-- = buf
[--intdig_no
];
1242 return bufend
+ ngroups
;