1 /* Floating point output for `printf'.
2 Copyright (C) 1995-2003, 2006, 2007 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) \
76 if (buffer_malloced) \
83 #define PRINT(ptr, wptr, len) \
86 register size_t outlen = (len); \
89 if (PUT (fp, wide ? (const char *) wptr : ptr, outlen) != outlen) \
91 if (buffer_malloced) \
101 while (outlen-- > 0) \
104 while (outlen-- > 0) \
109 #define PADN(ch, len) \
112 if (PAD (fp, ch, len) != len) \
114 if (buffer_malloced) \
122 /* We use the GNU MP library to handle large numbers.
124 An MP variable occupies a varying number of entries in its array. We keep
125 track of this number for efficiency reasons. Otherwise we would always
126 have to process the whole array. */
127 #define MPN_VAR(name) mp_limb_t *name; mp_size_t name##size
129 #define MPN_ASSIGN(dst,src) \
130 memcpy (dst, src, (dst##size = src##size) * sizeof (mp_limb_t))
131 #define MPN_GE(u,v) \
132 (u##size > v##size || (u##size == v##size && __mpn_cmp (u, v, u##size) >= 0))
134 extern int __isinfl_internal (long double) attribute_hidden
;
135 extern int __isnanl_internal (long double) attribute_hidden
;
137 extern mp_size_t
__mpn_extract_double (mp_ptr res_ptr
, mp_size_t size
,
138 int *expt
, int *is_neg
,
140 extern mp_size_t
__mpn_extract_long_double (mp_ptr res_ptr
, mp_size_t size
,
141 int *expt
, int *is_neg
,
143 extern unsigned int __guess_grouping (unsigned int intdig_max
,
144 const char *grouping
);
147 static wchar_t *group_number (wchar_t *buf
, wchar_t *bufend
,
148 unsigned int intdig_no
, const char *grouping
,
149 wchar_t thousands_sep
, int ngroups
)
154 ___printf_fp (FILE *fp
,
155 const struct printf_info
*info
,
156 const void *const *args
)
158 /* The floating-point value to output. */
162 __long_double_t ldbl
;
166 /* Locale-dependent representation of decimal point. */
170 /* Locale-dependent thousands separator and grouping specification. */
171 const char *thousands_sep
= NULL
;
172 wchar_t thousands_sepwc
= 0;
173 const char *grouping
;
175 /* "NaN" or "Inf" for the special cases. */
176 const char *special
= NULL
;
177 const wchar_t *wspecial
= NULL
;
179 /* We need just a few limbs for the input before shifting to the right
181 mp_limb_t fp_input
[(LDBL_MANT_DIG
+ BITS_PER_MP_LIMB
- 1) / BITS_PER_MP_LIMB
];
182 /* We need to shift the contents of fp_input by this amount of bits. */
185 /* The fraction of the floting-point value in question */
187 /* and the exponent. */
189 /* Sign of the exponent. */
191 /* Sign of float number. */
194 /* Scaling factor. */
197 /* Temporary bignum value. */
200 /* Digit which is result of last hack_digit() call. */
203 /* The type of output format that will be used: 'e'/'E' or 'f'. */
206 /* Counter for number of written characters. */
209 /* General helper (carry limb). */
212 /* Nonzero if this is output on a wide character stream. */
213 int wide
= info
->wide
;
215 /* Buffer in which we produce the output. */
216 wchar_t *wbuffer
= NULL
;
217 /* Flag whether wbuffer is malloc'ed or not. */
218 int buffer_malloced
= 0;
220 auto wchar_t hack_digit (void);
222 wchar_t hack_digit (void)
226 if (expsign
!= 0 && type
== 'f' && exponent
-- > 0)
228 else if (scalesize
== 0)
230 hi
= frac
[fracsize
- 1];
231 frac
[fracsize
- 1] = __mpn_mul_1 (frac
, frac
, fracsize
- 1, 10);
235 if (fracsize
< scalesize
)
239 hi
= mpn_divmod (tmp
, frac
, fracsize
, scale
, scalesize
);
240 tmp
[fracsize
- scalesize
] = hi
;
243 fracsize
= scalesize
;
244 while (fracsize
!= 0 && frac
[fracsize
- 1] == 0)
248 /* We're not prepared for an mpn variable with zero
255 mp_limb_t _cy
= __mpn_mul_1 (frac
, frac
, fracsize
, 10);
257 frac
[fracsize
++] = _cy
;
264 /* Figure out the decimal point character. */
265 if (info
->extra
== 0)
267 decimal
= _NL_CURRENT (LC_NUMERIC
, DECIMAL_POINT
);
268 decimalwc
= _NL_CURRENT_WORD (LC_NUMERIC
, _NL_NUMERIC_DECIMAL_POINT_WC
);
272 decimal
= _NL_CURRENT (LC_MONETARY
, MON_DECIMAL_POINT
);
273 if (*decimal
== '\0')
274 decimal
= _NL_CURRENT (LC_NUMERIC
, DECIMAL_POINT
);
275 decimalwc
= _NL_CURRENT_WORD (LC_MONETARY
,
276 _NL_MONETARY_DECIMAL_POINT_WC
);
277 if (decimalwc
== L
'\0')
278 decimalwc
= _NL_CURRENT_WORD (LC_NUMERIC
,
279 _NL_NUMERIC_DECIMAL_POINT_WC
);
281 /* The decimal point character must not be zero. */
282 assert (*decimal
!= '\0');
283 assert (decimalwc
!= L
'\0');
287 if (info
->extra
== 0)
288 grouping
= _NL_CURRENT (LC_NUMERIC
, GROUPING
);
290 grouping
= _NL_CURRENT (LC_MONETARY
, MON_GROUPING
);
292 if (*grouping
<= 0 || *grouping
== CHAR_MAX
)
296 /* Figure out the thousands separator character. */
299 if (info
->extra
== 0)
301 _NL_CURRENT_WORD (LC_NUMERIC
, _NL_NUMERIC_THOUSANDS_SEP_WC
);
304 _NL_CURRENT_WORD (LC_MONETARY
,
305 _NL_MONETARY_THOUSANDS_SEP_WC
);
309 if (info
->extra
== 0)
310 thousands_sep
= _NL_CURRENT (LC_NUMERIC
, THOUSANDS_SEP
);
312 thousands_sep
= _NL_CURRENT (LC_MONETARY
, MON_THOUSANDS_SEP
);
315 if ((wide
&& thousands_sepwc
== L
'\0')
316 || (! wide
&& *thousands_sep
== '\0'))
318 else if (thousands_sepwc
== L
'\0')
319 /* If we are printing multibyte characters and there is a
320 multibyte representation for the thousands separator,
321 we must ensure the wide character thousands separator
322 is available, even if it is fake. */
323 thousands_sepwc
= 0xfffffffe;
329 /* Fetch the argument value. */
330 #ifndef __NO_LONG_DOUBLE_MATH
331 if (info
->is_long_double
&& sizeof (long double) > sizeof (double))
333 fpnum
.ldbl
= *(const long double *) args
[0];
335 /* Check for special values: not a number or infinity. */
336 if (__isnanl (fpnum
.ldbl
))
338 if (isupper (info
->spec
))
350 else if (__isinfl (fpnum
.ldbl
))
352 if (isupper (info
->spec
))
362 is_neg
= fpnum
.ldbl
< 0;
366 fracsize
= __mpn_extract_long_double (fp_input
,
368 sizeof (fp_input
[0])),
371 to_shift
= 1 + 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. */
380 if (__isnan (fpnum
.dbl
))
383 if (isupper (info
->spec
))
394 else if (__isinf (fpnum
.dbl
))
396 is_neg
= fpnum
.dbl
< 0;
397 if (isupper (info
->spec
))
410 fracsize
= __mpn_extract_double (fp_input
,
412 / sizeof (fp_input
[0])),
413 &exponent
, &is_neg
, fpnum
.dbl
);
414 to_shift
= 1 + fracsize
* BITS_PER_MP_LIMB
- DBL_MANT_DIG
;
420 int width
= info
->width
;
422 if (is_neg
|| info
->showsign
|| info
->space
)
426 if (!info
->left
&& width
> 0)
431 else if (info
->showsign
)
433 else if (info
->space
)
436 PRINT (special
, wspecial
, 3);
438 if (info
->left
&& width
> 0)
445 /* We need three multiprecision variables. Now that we have the exponent
446 of the number we can allocate the needed memory. It would be more
447 efficient to use variables of the fixed maximum size but because this
448 would be really big it could lead to memory problems. */
450 mp_size_t bignum_size
= ((ABS (exponent
) + BITS_PER_MP_LIMB
- 1)
452 + (LDBL_MANT_DIG
/ BITS_PER_MP_LIMB
> 2 ? 8 : 4))
453 * sizeof (mp_limb_t
);
454 frac
= (mp_limb_t
*) alloca (bignum_size
);
455 tmp
= (mp_limb_t
*) alloca (bignum_size
);
456 scale
= (mp_limb_t
*) alloca (bignum_size
);
459 /* We now have to distinguish between numbers with positive and negative
460 exponents because the method used for the one is not applicable/efficient
467 int explog
= LDBL_MAX_10_EXP_LOG
;
469 const struct mp_power
*powers
= &_fpioconst_pow10
[explog
+ 1];
472 if ((exponent
+ to_shift
) % BITS_PER_MP_LIMB
== 0)
474 MPN_COPY_DECR (frac
+ (exponent
+ to_shift
) / BITS_PER_MP_LIMB
,
476 fracsize
+= (exponent
+ to_shift
) / BITS_PER_MP_LIMB
;
480 cy
= __mpn_lshift (frac
+ (exponent
+ to_shift
) / BITS_PER_MP_LIMB
,
482 (exponent
+ to_shift
) % BITS_PER_MP_LIMB
);
483 fracsize
+= (exponent
+ to_shift
) / BITS_PER_MP_LIMB
;
485 frac
[fracsize
++] = cy
;
487 MPN_ZERO (frac
, (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 (exponent
>= scaleexpo
+ powers
->p_expo
- 1)
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 tmpsize
= powers
->arraysize
+ _FPIO_CONST_SHIFT
;
510 memcpy (tmp
+ _FPIO_CONST_SHIFT
,
511 &__tens
[powers
->arrayoff
],
512 tmpsize
* sizeof (mp_limb_t
));
513 MPN_ZERO (tmp
, _FPIO_CONST_SHIFT
);
514 /* Adjust exponent, as scaleexpo will be this much
516 exponent
+= _FPIO_CONST_SHIFT
* BITS_PER_MP_LIMB
;
521 tmpsize
= powers
->arraysize
;
522 memcpy (tmp
, &__tens
[powers
->arrayoff
],
523 tmpsize
* sizeof (mp_limb_t
));
528 cy
= __mpn_mul (tmp
, scale
, scalesize
,
529 &__tens
[powers
->arrayoff
530 + _FPIO_CONST_OFFSET
],
531 powers
->arraysize
- _FPIO_CONST_OFFSET
);
532 tmpsize
= scalesize
+ powers
->arraysize
- _FPIO_CONST_OFFSET
;
537 if (MPN_GE (frac
, tmp
))
540 MPN_ASSIGN (scale
, tmp
);
541 count_leading_zeros (cnt
, scale
[scalesize
- 1]);
542 scaleexpo
= (scalesize
- 2) * BITS_PER_MP_LIMB
- cnt
- 1;
543 exp10
|= 1 << explog
;
548 while (powers
> &_fpioconst_pow10
[0]);
551 /* Optimize number representations. We want to represent the numbers
552 with the lowest number of bytes possible without losing any
553 bytes. Also the highest bit in the scaling factor has to be set
554 (this is a requirement of the MPN division routines). */
557 /* Determine minimum number of zero bits at the end of
559 for (i
= 0; scale
[i
] == 0 && frac
[i
] == 0; i
++)
562 /* Determine number of bits the scaling factor is misplaced. */
563 count_leading_zeros (cnt_h
, scale
[scalesize
- 1]);
567 /* The highest bit of the scaling factor is already set. So
568 we only have to remove the trailing empty limbs. */
571 MPN_COPY_INCR (scale
, scale
+ i
, scalesize
- i
);
573 MPN_COPY_INCR (frac
, frac
+ i
, fracsize
- i
);
581 count_trailing_zeros (cnt_l
, scale
[i
]);
585 count_trailing_zeros (cnt_l2
, frac
[i
]);
591 count_trailing_zeros (cnt_l
, frac
[i
]);
593 /* Now shift the numbers to their optimal position. */
594 if (i
== 0 && BITS_PER_MP_LIMB
- cnt_h
> cnt_l
)
596 /* We cannot save any memory. So just roll both numbers
597 so that the scaling factor has its highest bit set. */
599 (void) __mpn_lshift (scale
, scale
, scalesize
, cnt_h
);
600 cy
= __mpn_lshift (frac
, frac
, fracsize
, cnt_h
);
602 frac
[fracsize
++] = cy
;
604 else if (BITS_PER_MP_LIMB
- cnt_h
<= cnt_l
)
606 /* We can save memory by removing the trailing zero limbs
607 and by packing the non-zero limbs which gain another
610 (void) __mpn_rshift (scale
, scale
+ i
, scalesize
- i
,
611 BITS_PER_MP_LIMB
- cnt_h
);
613 (void) __mpn_rshift (frac
, frac
+ i
, fracsize
- i
,
614 BITS_PER_MP_LIMB
- cnt_h
);
615 fracsize
-= frac
[fracsize
- i
- 1] == 0 ? i
+ 1 : i
;
619 /* We can only save the memory of the limbs which are zero.
620 The non-zero parts occupy the same number of limbs. */
622 (void) __mpn_rshift (scale
, scale
+ (i
- 1),
624 BITS_PER_MP_LIMB
- cnt_h
);
626 (void) __mpn_rshift (frac
, frac
+ (i
- 1),
628 BITS_PER_MP_LIMB
- cnt_h
);
629 fracsize
-= frac
[fracsize
- (i
- 1) - 1] == 0 ? i
: i
- 1;
634 else if (exponent
< 0)
638 int explog
= LDBL_MAX_10_EXP_LOG
;
639 const struct mp_power
*powers
= &_fpioconst_pow10
[explog
+ 1];
640 mp_size_t used_limbs
= fracsize
- 1;
642 /* Now shift the input value to its right place. */
643 cy
= __mpn_lshift (frac
, fp_input
, fracsize
, to_shift
);
644 frac
[fracsize
++] = cy
;
645 assert (cy
== 1 || (frac
[fracsize
- 2] == 0 && frac
[0] == 0));
648 exponent
= -exponent
;
650 assert (powers
!= &_fpioconst_pow10
[0]);
655 if (exponent
>= powers
->m_expo
)
657 int i
, incr
, cnt_h
, cnt_l
;
660 /* The __mpn_mul function expects the first argument to be
661 bigger than the second. */
662 if (fracsize
< powers
->arraysize
- _FPIO_CONST_OFFSET
)
663 cy
= __mpn_mul (tmp
, &__tens
[powers
->arrayoff
664 + _FPIO_CONST_OFFSET
],
665 powers
->arraysize
- _FPIO_CONST_OFFSET
,
668 cy
= __mpn_mul (tmp
, frac
, fracsize
,
669 &__tens
[powers
->arrayoff
+ _FPIO_CONST_OFFSET
],
670 powers
->arraysize
- _FPIO_CONST_OFFSET
);
671 tmpsize
= fracsize
+ powers
->arraysize
- _FPIO_CONST_OFFSET
;
675 count_leading_zeros (cnt_h
, tmp
[tmpsize
- 1]);
676 incr
= (tmpsize
- fracsize
) * BITS_PER_MP_LIMB
677 + BITS_PER_MP_LIMB
- 1 - cnt_h
;
679 assert (incr
<= powers
->p_expo
);
681 /* If we increased the exponent by exactly 3 we have to test
682 for overflow. This is done by comparing with 10 shifted
683 to the right position. */
684 if (incr
== exponent
+ 3)
686 if (cnt_h
<= BITS_PER_MP_LIMB
- 4)
690 = ((mp_limb_t
) 10) << (BITS_PER_MP_LIMB
- 4 - cnt_h
);
694 topval
[0] = ((mp_limb_t
) 10) << (BITS_PER_MP_LIMB
- 4);
696 (void) __mpn_lshift (topval
, topval
, 2,
697 BITS_PER_MP_LIMB
- cnt_h
);
701 /* We have to be careful when multiplying the last factor.
702 If the result is greater than 1.0 be have to test it
703 against 10.0. If it is greater or equal to 10.0 the
704 multiplication was not valid. This is because we cannot
705 determine the number of bits in the result in advance. */
706 if (incr
< exponent
+ 3
707 || (incr
== exponent
+ 3 &&
708 (tmp
[tmpsize
- 1] < topval
[1]
709 || (tmp
[tmpsize
- 1] == topval
[1]
710 && tmp
[tmpsize
- 2] < topval
[0]))))
712 /* The factor is right. Adapt binary and decimal
715 exp10
|= 1 << explog
;
717 /* If this factor yields a number greater or equal to
718 1.0, we must not shift the non-fractional digits down. */
722 /* Now we optimize the number representation. */
723 for (i
= 0; tmp
[i
] == 0; ++i
);
724 if (cnt_h
== BITS_PER_MP_LIMB
- 1)
726 MPN_COPY (frac
, tmp
+ i
, tmpsize
- i
);
727 fracsize
= tmpsize
- i
;
731 count_trailing_zeros (cnt_l
, tmp
[i
]);
733 /* Now shift the numbers to their optimal position. */
734 if (i
== 0 && BITS_PER_MP_LIMB
- 1 - cnt_h
> cnt_l
)
736 /* We cannot save any memory. Just roll the
737 number so that the leading digit is in a
740 cy
= __mpn_lshift (frac
, tmp
, tmpsize
, cnt_h
+ 1);
741 fracsize
= tmpsize
+ 1;
742 frac
[fracsize
- 1] = cy
;
744 else if (BITS_PER_MP_LIMB
- 1 - cnt_h
<= cnt_l
)
746 (void) __mpn_rshift (frac
, tmp
+ i
, tmpsize
- i
,
747 BITS_PER_MP_LIMB
- 1 - cnt_h
);
748 fracsize
= tmpsize
- i
;
752 /* We can only save the memory of the limbs which
753 are zero. The non-zero parts occupy the same
756 (void) __mpn_rshift (frac
, tmp
+ (i
- 1),
758 BITS_PER_MP_LIMB
- 1 - cnt_h
);
759 fracsize
= tmpsize
- (i
- 1);
762 used_limbs
= fracsize
- 1;
767 while (powers
!= &_fpioconst_pow10
[1] && exponent
> 0);
768 /* All factors but 10^-1 are tested now. */
773 cy
= __mpn_mul_1 (tmp
, frac
, fracsize
, 10);
775 assert (cy
== 0 || tmp
[tmpsize
- 1] < 20);
777 count_trailing_zeros (cnt_l
, tmp
[0]);
778 if (cnt_l
< MIN (4, exponent
))
780 cy
= __mpn_lshift (frac
, tmp
, tmpsize
,
781 BITS_PER_MP_LIMB
- MIN (4, exponent
));
783 frac
[tmpsize
++] = cy
;
786 (void) __mpn_rshift (frac
, tmp
, tmpsize
, MIN (4, exponent
));
789 assert (frac
[fracsize
- 1] < 10);
795 /* This is a special case. We don't need a factor because the
796 numbers are in the range of 0.0 <= fp < 8.0. We simply
797 shift it to the right place and divide it by 1.0 to get the
798 leading digit. (Of course this division is not really made.) */
799 assert (0 <= exponent
&& exponent
< 3 &&
800 exponent
+ to_shift
< BITS_PER_MP_LIMB
);
802 /* Now shift the input value to its right place. */
803 cy
= __mpn_lshift (frac
, fp_input
, fracsize
, (exponent
+ to_shift
));
804 frac
[fracsize
++] = cy
;
809 int width
= info
->width
;
810 wchar_t *wstartp
, *wcp
;
813 int intdig_max
, intdig_no
= 0;
819 char spec
= _tolower (info
->spec
);
825 fracdig_min
= fracdig_max
= info
->prec
< 0 ? 6 : info
->prec
;
826 chars_needed
= 1 + 1 + fracdig_max
+ 1 + 1 + 4;
827 /* d . ddd e +- ddd */
828 dig_max
= INT_MAX
; /* Unlimited. */
829 significant
= 1; /* Does not matter here. */
831 else if (spec
== 'f')
834 fracdig_min
= fracdig_max
= info
->prec
< 0 ? 6 : info
->prec
;
835 dig_max
= INT_MAX
; /* Unlimited. */
836 significant
= 1; /* Does not matter here. */
839 intdig_max
= exponent
+ 1;
840 /* This can be really big! */ /* XXX Maybe malloc if too big? */
841 chars_needed
= exponent
+ 1 + 1 + fracdig_max
;
846 chars_needed
= 1 + 1 + fracdig_max
;
851 dig_max
= info
->prec
< 0 ? 6 : (info
->prec
== 0 ? 1 : info
->prec
);
852 if ((expsign
== 0 && exponent
>= dig_max
)
853 || (expsign
!= 0 && exponent
> 4))
855 if ('g' - 'G' == 'e' - 'E')
856 type
= 'E' + (info
->spec
- 'G');
858 type
= isupper (info
->spec
) ? 'E' : 'e';
859 fracdig_max
= dig_max
- 1;
861 chars_needed
= 1 + 1 + fracdig_max
+ 1 + 1 + 4;
866 intdig_max
= expsign
== 0 ? exponent
+ 1 : 0;
867 fracdig_max
= dig_max
- intdig_max
;
868 /* We need space for the significant digits and perhaps
869 for leading zeros when < 1.0. The number of leading
870 zeros can be as many as would be required for
871 exponential notation with a negative two-digit
872 exponent, which is 4. */
873 chars_needed
= dig_max
+ 1 + 4;
875 fracdig_min
= info
->alt
? fracdig_max
: 0;
876 significant
= 0; /* We count significant digits. */
881 /* Guess the number of groups we will make, and thus how
882 many spaces we need for separator characters. */
883 ngroups
= __guess_grouping (intdig_max
, grouping
);
884 chars_needed
+= ngroups
;
887 /* Allocate buffer for output. We need two more because while rounding
888 it is possible that we need two more characters in front of all the
889 other output. If the amount of memory we have to allocate is too
890 large use `malloc' instead of `alloca'. */
891 buffer_malloced
= ! __libc_use_alloca (chars_needed
* 2 * sizeof (wchar_t));
892 if (__builtin_expect (buffer_malloced
, 0))
894 wbuffer
= (wchar_t *) malloc ((2 + chars_needed
) * sizeof (wchar_t));
896 /* Signal an error to the caller. */
900 wbuffer
= (wchar_t *) alloca ((2 + chars_needed
) * sizeof (wchar_t));
901 wcp
= wstartp
= wbuffer
+ 2; /* Let room for rounding. */
903 /* Do the real work: put digits in allocated buffer. */
904 if (expsign
== 0 || type
!= 'f')
906 assert (expsign
== 0 || intdig_max
== 1);
907 while (intdig_no
< intdig_max
)
910 *wcp
++ = hack_digit ();
915 || (fracdig_max
> 0 && (fracsize
> 1 || frac
[0] != 0)))
920 /* |fp| < 1.0 and the selected type is 'f', so put "0."
927 /* Generate the needed number of fractional digits. */
930 while (fracdig_no
< fracdig_min
+ added_zeros
931 || (fracdig_no
< fracdig_max
&& (fracsize
> 1 || frac
[0] != 0)))
934 *wcp
= hack_digit ();
937 else if (significant
== 0)
946 digit
= hack_digit ();
952 && ((*(wcp
- 1) != decimalwc
&& (*(wcp
- 1) & 1) == 0)
953 || ((*(wcp
- 1) == decimalwc
&& (*(wcp
- 2) & 1) == 0))))
955 /* This is the critical case. */
956 if (fracsize
== 1 && frac
[0] == 0)
957 /* Rest of the number is zero -> round to even.
958 (IEEE 754-1985 4.1 says this is the default rounding.) */
960 else if (scalesize
== 0)
962 /* Here we have to see whether all limbs are zero since no
963 normalization happened. */
964 size_t lcnt
= fracsize
;
965 while (lcnt
>= 1 && frac
[lcnt
- 1] == 0)
968 /* Rest of the number is zero -> round to even.
969 (IEEE 754-1985 4.1 says this is the default rounding.) */
976 /* Process fractional digits. Terminate if not rounded or
977 radix character is reached. */
979 while (*--wtp
!= decimalwc
&& *wtp
== L
'9')
984 if (removed
== fracdig_min
&& added_zeros
> 0)
986 if (*wtp
!= decimalwc
)
989 else if (__builtin_expect (spec
== 'g' && type
== 'f' && info
->alt
,
991 /* This is a special case: the rounded number is 1.0,
992 the format is 'g' or 'G', and the alternative format
993 is selected. This means the result mist be "1.". */
997 if (fracdig_no
== 0 || *wtp
== decimalwc
)
999 /* Round the integer digits. */
1000 if (*(wtp
- 1) == decimalwc
)
1003 while (--wtp
>= wstartp
&& *wtp
== L
'9')
1010 /* It is more critical. All digits were 9's. */
1015 exponent
+= expsign
== 0 ? 1 : -1;
1017 else if (intdig_no
== dig_max
)
1019 /* This is the case where for type %g the number fits
1020 really in the range for %f output but after rounding
1021 the number of digits is too big. */
1022 *--wstartp
= decimalwc
;
1025 if (info
->alt
|| fracdig_no
> 0)
1027 /* Overwrite the old radix character. */
1028 wstartp
[intdig_no
+ 2] = L
'0';
1032 fracdig_no
+= intdig_no
;
1034 fracdig_max
= intdig_max
- intdig_no
;
1036 /* Now we must print the exponent. */
1037 type
= isupper (info
->spec
) ? 'E' : 'e';
1041 /* We can simply add another another digit before the
1047 /* While rounding the number of digits can change.
1048 If the number now exceeds the limits remove some
1049 fractional digits. */
1050 if (intdig_no
+ fracdig_no
> dig_max
)
1052 wcp
-= intdig_no
+ fracdig_no
- dig_max
;
1053 fracdig_no
-= intdig_no
+ fracdig_no
- dig_max
;
1060 /* Now remove unnecessary '0' at the end of the string. */
1061 while (fracdig_no
> fracdig_min
+ added_zeros
&& *(wcp
- 1) == L
'0')
1066 /* If we eliminate all fractional digits we perhaps also can remove
1067 the radix character. */
1068 if (fracdig_no
== 0 && !info
->alt
&& *(wcp
- 1) == decimalwc
)
1072 /* Add in separator characters, overwriting the same buffer. */
1073 wcp
= group_number (wstartp
, wcp
, intdig_no
, grouping
, thousands_sepwc
,
1076 /* Write the exponent if it is needed. */
1079 if (__builtin_expect (expsign
!= 0 && exponent
== 4 && spec
== 'g', 0))
1081 /* This is another special case. The exponent of the number is
1082 really smaller than -4, which requires the 'e'/'E' format.
1083 But after rounding the number has an exponent of -4. */
1084 assert (wcp
>= wstartp
+ 2);
1085 assert (wstartp
[0] == L
'1');
1086 __wmemcpy (wstartp
, L
"0.0001", 6);
1087 wstartp
[1] = decimalwc
;
1088 wmemset (wstartp
+ 6, L
'0', wcp
- (wstartp
+ 2));
1093 *wcp
++ = (wchar_t) type
;
1094 *wcp
++ = expsign
? L
'-' : L
'+';
1096 /* Find the magnitude of the exponent. */
1098 while (expscale
<= exponent
)
1102 /* Exponent always has at least two digits. */
1108 *wcp
++ = L
'0' + (exponent
/ expscale
);
1109 exponent
%= expscale
;
1111 while (expscale
> 10);
1112 *wcp
++ = L
'0' + exponent
;
1116 /* Compute number of characters which must be filled with the padding
1118 if (is_neg
|| info
->showsign
|| info
->space
)
1120 width
-= wcp
- wstartp
;
1122 if (!info
->left
&& info
->pad
!= '0' && width
> 0)
1123 PADN (info
->pad
, width
);
1127 else if (info
->showsign
)
1129 else if (info
->space
)
1132 if (!info
->left
&& info
->pad
== '0' && width
> 0)
1136 char *buffer
= NULL
;
1142 /* Create the single byte string. */
1144 size_t thousands_sep_len
;
1147 decimal_len
= strlen (decimal
);
1149 if (thousands_sep
== NULL
)
1150 thousands_sep_len
= 0;
1152 thousands_sep_len
= strlen (thousands_sep
);
1154 if (__builtin_expect (buffer_malloced
, 0))
1156 buffer
= (char *) malloc (2 + chars_needed
+ decimal_len
1157 + ngroups
* thousands_sep_len
);
1160 /* Signal an error to the caller. */
1166 buffer
= (char *) alloca (2 + chars_needed
+ decimal_len
1167 + ngroups
* thousands_sep_len
);
1169 /* Now copy the wide character string. Since the character
1170 (except for the decimal point and thousands separator) must
1171 be coming from the ASCII range we can esily convert the
1172 string without mapping tables. */
1173 for (cp
= buffer
, copywc
= wstartp
; copywc
< wcp
; ++copywc
)
1174 if (*copywc
== decimalwc
)
1175 cp
= (char *) __mempcpy (cp
, decimal
, decimal_len
);
1176 else if (*copywc
== thousands_sepwc
)
1177 cp
= (char *) __mempcpy (cp
, thousands_sep
, thousands_sep_len
);
1179 *cp
++ = (char) *copywc
;
1183 if (__builtin_expect (info
->i18n
, 0))
1185 #ifdef COMPILE_WPRINTF
1186 wstartp
= _i18n_number_rewrite (wstartp
, wcp
);
1188 tmpptr
= _i18n_number_rewrite (tmpptr
, cp
);
1192 PRINT (tmpptr
, wstartp
, wide
? wcp
- wstartp
: cp
- tmpptr
);
1194 /* Free the memory if necessary. */
1195 if (__builtin_expect (buffer_malloced
, 0))
1202 if (info
->left
&& width
> 0)
1203 PADN (info
->pad
, width
);
1207 ldbl_hidden_def (___printf_fp
, __printf_fp
)
1208 ldbl_strong_alias (___printf_fp
, __printf_fp
)
1210 /* Return the number of extra grouping characters that will be inserted
1211 into a number with INTDIG_MAX integer digits. */
1214 __guess_grouping (unsigned int intdig_max
, const char *grouping
)
1216 unsigned int groups
;
1218 /* We treat all negative values like CHAR_MAX. */
1220 if (*grouping
== CHAR_MAX
|| *grouping
<= 0)
1221 /* No grouping should be done. */
1225 while (intdig_max
> (unsigned int) *grouping
)
1228 intdig_max
-= *grouping
++;
1230 if (*grouping
== CHAR_MAX
1235 /* No more grouping should be done. */
1237 else if (*grouping
== 0)
1239 /* Same grouping repeats. */
1240 groups
+= (intdig_max
- 1) / grouping
[-1];
1248 /* Group the INTDIG_NO integer digits of the number in [BUF,BUFEND).
1249 There is guaranteed enough space past BUFEND to extend it.
1250 Return the new end of buffer. */
1254 group_number (wchar_t *buf
, wchar_t *bufend
, unsigned int intdig_no
,
1255 const char *grouping
, wchar_t thousands_sep
, int ngroups
)
1262 /* Move the fractional part down. */
1263 __wmemmove (buf
+ intdig_no
+ ngroups
, buf
+ intdig_no
,
1264 bufend
- (buf
+ intdig_no
));
1266 p
= buf
+ intdig_no
+ ngroups
- 1;
1269 unsigned int len
= *grouping
++;
1271 *p
-- = buf
[--intdig_no
];
1273 *p
-- = thousands_sep
;
1275 if (*grouping
== CHAR_MAX
1280 /* No more grouping should be done. */
1282 else if (*grouping
== 0)
1283 /* Same grouping repeats. */
1285 } while (intdig_no
> (unsigned int) *grouping
);
1287 /* Copy the remaining ungrouped digits. */
1289 *p
-- = buf
[--intdig_no
];
1292 return bufend
+ ngroups
;