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
35 #include "quadmath-printf.h"
36 #include "fpioconst.h"
38 #ifdef USE_I18N_NUMBER_H
39 #include "_i18n_number.h"
41 #define USE_I18N_NUMBER_H 0
45 /* Macros for doing the actual output. */
50 register const int outc = (ch); \
51 if (PUTC (outc, fp) == EOF) \
53 if (buffer_malloced) \
60 #define PRINT(ptr, wptr, len) \
63 register size_t outlen = (len); \
66 if (PUT (fp, wide ? (const char *) wptr : ptr, outlen) != outlen) \
68 if (buffer_malloced) \
78 while (outlen-- > 0) \
81 while (outlen-- > 0) \
86 #define PADN(ch, len) \
89 if (PAD (fp, ch, len) != len) \
91 if (buffer_malloced) \
100 /* We use the GNU MP library to handle large numbers.
102 An MP variable occupies a varying number of entries in its array. We keep
103 track of this number for efficiency reasons. Otherwise we would always
104 have to process the whole array. */
105 #define MPN_VAR(name) mp_limb_t *name; mp_size_t name##size
107 #define MPN_ASSIGN(dst,src) \
108 memcpy (dst, src, (dst##size = src##size) * sizeof (mp_limb_t))
109 #define MPN_GE(u,v) \
110 (u##size > v##size || (u##size == v##size && mpn_cmp (u, v, u##size) >= 0))
112 extern mp_size_t mpn_extract_flt128 (mp_ptr res_ptr, mp_size_t size,
113 int *expt, int *is_neg,
114 __float128 value) attribute_hidden;
115 static unsigned int guess_grouping (unsigned int intdig_max,
116 const char *grouping);
119 static wchar_t *group_number (wchar_t *buf, wchar_t *bufend,
120 unsigned int intdig_no, const char *grouping,
121 wchar_t thousands_sep, int ngroups);
125 __quadmath_printf_fp (struct __quadmath_printf_file *fp,
126 const struct printf_info *info,
127 const void *const *args)
129 /* The floating-point value to output. */
132 /* Locale-dependent representation of decimal point. */
136 /* Locale-dependent thousands separator and grouping specification. */
137 const char *thousands_sep = NULL;
138 wchar_t thousands_sepwc = L_('\0');
139 const char *grouping;
141 /* "NaN" or "Inf" for the special cases. */
142 const char *special = NULL;
143 const wchar_t *wspecial = NULL;
145 /* We need just a few limbs for the input before shifting to the right
147 mp_limb_t fp_input[(FLT128_MANT_DIG + BITS_PER_MP_LIMB - 1) / BITS_PER_MP_LIMB];
148 /* We need to shift the contents of fp_input by this amount of bits. */
151 /* The fraction of the floting-point value in question */
153 /* and the exponent. */
155 /* Sign of the exponent. */
157 /* Sign of float number. */
160 /* Scaling factor. */
163 /* Temporary bignum value. */
166 /* Digit which is result of last hack_digit() call. */
169 /* The type of output format that will be used: 'e'/'E' or 'f'. */
172 /* Counter for number of written characters. */
175 /* General helper (carry limb). */
178 /* Nonzero if this is output on a wide character stream. */
179 int wide = info->wide;
181 /* Buffer in which we produce the output. */
182 wchar_t *wbuffer = NULL;
183 /* Flag whether wbuffer is malloc'ed or not. */
184 int buffer_malloced = 0;
186 auto wchar_t hack_digit (void);
188 wchar_t hack_digit (void)
192 if (expsign != 0 && type == 'f' && exponent-- > 0)
194 else if (scalesize == 0)
196 hi = frac[fracsize - 1];
197 frac[fracsize - 1] = mpn_mul_1 (frac, frac, fracsize - 1, 10);
201 if (fracsize < scalesize)
205 hi = mpn_divmod (tmp, frac, fracsize, scale, scalesize);
206 tmp[fracsize - scalesize] = hi;
209 fracsize = scalesize;
210 while (fracsize != 0 && frac[fracsize - 1] == 0)
214 /* We're not prepared for an mpn variable with zero
221 mp_limb_t _cy = mpn_mul_1 (frac, frac, fracsize, 10);
223 frac[fracsize++] = _cy;
229 /* Figure out the decimal point character. */
230 #ifdef USE_LOCALE_SUPPORT
231 if (info->extra == 0)
233 decimal = nl_langinfo (DECIMAL_POINT);
234 decimalwc = nl_langinfo_wc (_NL_NUMERIC_DECIMAL_POINT_WC);
238 decimal = nl_langinfo (MON_DECIMAL_POINT);
239 if (*decimal == '\0')
240 decimal = nl_langinfo (DECIMAL_POINT);
241 decimalwc = nl_langinfo_wc (_NL_MONETARY_DECIMAL_POINT_WC);
242 if (decimalwc == L_('\0'))
243 decimalwc = nl_langinfo_wc (_NL_NUMERIC_DECIMAL_POINT_WC);
245 /* The decimal point character must not be zero. */
246 assert (*decimal != '\0');
247 assert (decimalwc != L_('\0'));
253 #ifdef USE_LOCALE_SUPPORT
256 if (info->extra == 0)
257 grouping = nl_langinfo (GROUPING);
259 grouping = nl_langinfo (MON_GROUPING);
261 if (*grouping <= 0 || *grouping == CHAR_MAX)
265 /* Figure out the thousands separator character. */
268 if (info->extra == 0)
269 thousands_sepwc = nl_langinfo_wc (_NL_NUMERIC_THOUSANDS_SEP_WC);
271 thousands_sepwc = nl_langinfo_wc (_NL_MONETARY_THOUSANDS_SEP_WC);
275 if (info->extra == 0)
276 thousands_sep = nl_langinfo (THOUSANDS_SEP);
278 thousands_sep = nl_langinfo (MON_THOUSANDS_SEP);
281 if ((wide && thousands_sepwc == L_('\0'))
282 || (! wide && *thousands_sep == '\0'))
284 else if (thousands_sepwc == L_('\0'))
285 /* If we are printing multibyte characters and there is a
286 multibyte representation for the thousands separator,
287 we must ensure the wide character thousands separator
288 is available, even if it is fake. */
289 thousands_sepwc = (wchar_t) 0xfffffffe;
296 /* Fetch the argument value. */
298 fpnum = **(const __float128 **) args[0];
300 /* Check for special values: not a number or infinity. */
303 ieee854_float128 u = { .value = fpnum };
304 is_neg = u.ieee.negative != 0;
305 if (isupper (info->spec))
308 wspecial = L_("NAN");
313 wspecial = L_("nan");
316 else if (isinfq (fpnum))
319 if (isupper (info->spec))
322 wspecial = L_("INF");
327 wspecial = L_("inf");
332 fracsize = mpn_extract_flt128 (fp_input,
334 sizeof (fp_input[0])),
335 &exponent, &is_neg, fpnum);
336 to_shift = 1 + fracsize * BITS_PER_MP_LIMB - FLT128_MANT_DIG;
342 int width = info->width;
344 if (is_neg || info->showsign || info->space)
348 if (!info->left && width > 0)
353 else if (info->showsign)
355 else if (info->space)
358 PRINT (special, wspecial, 3);
360 if (info->left && width > 0)
367 /* We need three multiprecision variables. Now that we have the exponent
368 of the number we can allocate the needed memory. It would be more
369 efficient to use variables of the fixed maximum size but because this
370 would be really big it could lead to memory problems. */
372 mp_size_t bignum_size = ((ABS (exponent) + BITS_PER_MP_LIMB - 1)
374 + (FLT128_MANT_DIG / BITS_PER_MP_LIMB > 2 ? 8 : 4))
375 * sizeof (mp_limb_t);
376 frac = (mp_limb_t *) alloca (bignum_size);
377 tmp = (mp_limb_t *) alloca (bignum_size);
378 scale = (mp_limb_t *) alloca (bignum_size);
381 /* We now have to distinguish between numbers with positive and negative
382 exponents because the method used for the one is not applicable/efficient
389 int explog = FLT128_MAX_10_EXP_LOG;
391 const struct mp_power *powers = &_fpioconst_pow10[explog + 1];
394 if ((exponent + to_shift) % BITS_PER_MP_LIMB == 0)
396 MPN_COPY_DECR (frac + (exponent + to_shift) / BITS_PER_MP_LIMB,
398 fracsize += (exponent + to_shift) / BITS_PER_MP_LIMB;
402 cy = mpn_lshift (frac + (exponent + to_shift) / BITS_PER_MP_LIMB,
404 (exponent + to_shift) % BITS_PER_MP_LIMB);
405 fracsize += (exponent + to_shift) / BITS_PER_MP_LIMB;
407 frac[fracsize++] = cy;
409 MPN_ZERO (frac, (exponent + to_shift) / BITS_PER_MP_LIMB);
411 assert (powers > &_fpioconst_pow10[0]);
416 /* The number of the product of two binary numbers with n and m
417 bits respectively has m+n or m+n-1 bits. */
418 if (exponent >= scaleexpo + powers->p_expo - 1)
422 if (FLT128_MANT_DIG > _FPIO_CONST_OFFSET * BITS_PER_MP_LIMB)
424 #define _FPIO_CONST_SHIFT \
425 (((FLT128_MANT_DIG + BITS_PER_MP_LIMB - 1) / BITS_PER_MP_LIMB) \
426 - _FPIO_CONST_OFFSET)
427 /* 64bit const offset is not enough for
428 IEEE quad long double. */
429 tmpsize = powers->arraysize + _FPIO_CONST_SHIFT;
430 memcpy (tmp + _FPIO_CONST_SHIFT,
431 &__tens[powers->arrayoff],
432 tmpsize * sizeof (mp_limb_t));
433 MPN_ZERO (tmp, _FPIO_CONST_SHIFT);
434 /* Adjust exponent, as scaleexpo will be this much
436 exponent += _FPIO_CONST_SHIFT * BITS_PER_MP_LIMB;
440 tmpsize = powers->arraysize;
441 memcpy (tmp, &__tens[powers->arrayoff],
442 tmpsize * sizeof (mp_limb_t));
447 cy = mpn_mul (tmp, scale, scalesize,
448 &__tens[powers->arrayoff
449 + _FPIO_CONST_OFFSET],
450 powers->arraysize - _FPIO_CONST_OFFSET);
451 tmpsize = scalesize + powers->arraysize - _FPIO_CONST_OFFSET;
456 if (MPN_GE (frac, tmp))
459 MPN_ASSIGN (scale, tmp);
460 count_leading_zeros (cnt, scale[scalesize - 1]);
461 scaleexpo = (scalesize - 2) * BITS_PER_MP_LIMB - cnt - 1;
462 exp10 |= 1 << explog;
467 while (powers > &_fpioconst_pow10[0]);
470 /* Optimize number representations. We want to represent the numbers
471 with the lowest number of bytes possible without losing any
472 bytes. Also the highest bit in the scaling factor has to be set
473 (this is a requirement of the MPN division routines). */
476 /* Determine minimum number of zero bits at the end of
478 for (i = 0; scale[i] == 0 && frac[i] == 0; i++)
481 /* Determine number of bits the scaling factor is misplaced. */
482 count_leading_zeros (cnt_h, scale[scalesize - 1]);
486 /* The highest bit of the scaling factor is already set. So
487 we only have to remove the trailing empty limbs. */
490 MPN_COPY_INCR (scale, scale + i, scalesize - i);
492 MPN_COPY_INCR (frac, frac + i, fracsize - i);
500 count_trailing_zeros (cnt_l, scale[i]);
504 count_trailing_zeros (cnt_l2, frac[i]);
510 count_trailing_zeros (cnt_l, frac[i]);
512 /* Now shift the numbers to their optimal position. */
513 if (i == 0 && BITS_PER_MP_LIMB - cnt_h > cnt_l)
515 /* We cannot save any memory. So just roll both numbers
516 so that the scaling factor has its highest bit set. */
518 (void) mpn_lshift (scale, scale, scalesize, cnt_h);
519 cy = mpn_lshift (frac, frac, fracsize, cnt_h);
521 frac[fracsize++] = cy;
523 else if (BITS_PER_MP_LIMB - cnt_h <= cnt_l)
525 /* We can save memory by removing the trailing zero limbs
526 and by packing the non-zero limbs which gain another
529 (void) mpn_rshift (scale, scale + i, scalesize - i,
530 BITS_PER_MP_LIMB - cnt_h);
532 (void) mpn_rshift (frac, frac + i, fracsize - i,
533 BITS_PER_MP_LIMB - cnt_h);
534 fracsize -= frac[fracsize - i - 1] == 0 ? i + 1 : i;
538 /* We can only save the memory of the limbs which are zero.
539 The non-zero parts occupy the same number of limbs. */
541 (void) mpn_rshift (scale, scale + (i - 1),
543 BITS_PER_MP_LIMB - cnt_h);
545 (void) mpn_rshift (frac, frac + (i - 1),
547 BITS_PER_MP_LIMB - cnt_h);
548 fracsize -= frac[fracsize - (i - 1) - 1] == 0 ? i : i - 1;
553 else if (exponent < 0)
557 int explog = FLT128_MAX_10_EXP_LOG;
558 const struct mp_power *powers = &_fpioconst_pow10[explog + 1];
560 /* Now shift the input value to its right place. */
561 cy = mpn_lshift (frac, fp_input, fracsize, to_shift);
562 frac[fracsize++] = cy;
563 assert (cy == 1 || (frac[fracsize - 2] == 0 && frac[0] == 0));
566 exponent = -exponent;
568 assert (powers != &_fpioconst_pow10[0]);
573 if (exponent >= powers->m_expo)
575 int i, incr, cnt_h, cnt_l;
578 /* The mpn_mul function expects the first argument to be
579 bigger than the second. */
580 if (fracsize < powers->arraysize - _FPIO_CONST_OFFSET)
581 cy = mpn_mul (tmp, &__tens[powers->arrayoff
582 + _FPIO_CONST_OFFSET],
583 powers->arraysize - _FPIO_CONST_OFFSET,
586 cy = mpn_mul (tmp, frac, fracsize,
587 &__tens[powers->arrayoff + _FPIO_CONST_OFFSET],
588 powers->arraysize - _FPIO_CONST_OFFSET);
589 tmpsize = fracsize + powers->arraysize - _FPIO_CONST_OFFSET;
593 count_leading_zeros (cnt_h, tmp[tmpsize - 1]);
594 incr = (tmpsize - fracsize) * BITS_PER_MP_LIMB
595 + BITS_PER_MP_LIMB - 1 - cnt_h;
597 assert (incr <= powers->p_expo);
599 /* If we increased the exponent by exactly 3 we have to test
600 for overflow. This is done by comparing with 10 shifted
601 to the right position. */
602 if (incr == exponent + 3)
604 if (cnt_h <= BITS_PER_MP_LIMB - 4)
608 = ((mp_limb_t) 10) << (BITS_PER_MP_LIMB - 4 - cnt_h);
612 topval[0] = ((mp_limb_t) 10) << (BITS_PER_MP_LIMB - 4);
614 (void) mpn_lshift (topval, topval, 2,
615 BITS_PER_MP_LIMB - cnt_h);
619 /* We have to be careful when multiplying the last factor.
620 If the result is greater than 1.0 be have to test it
621 against 10.0. If it is greater or equal to 10.0 the
622 multiplication was not valid. This is because we cannot
623 determine the number of bits in the result in advance. */
624 if (incr < exponent + 3
625 || (incr == exponent + 3 &&
626 (tmp[tmpsize - 1] < topval[1]
627 || (tmp[tmpsize - 1] == topval[1]
628 && tmp[tmpsize - 2] < topval[0]))))
630 /* The factor is right. Adapt binary and decimal
633 exp10 |= 1 << explog;
635 /* If this factor yields a number greater or equal to
636 1.0, we must not shift the non-fractional digits down. */
640 /* Now we optimize the number representation. */
641 for (i = 0; tmp[i] == 0; ++i);
642 if (cnt_h == BITS_PER_MP_LIMB - 1)
644 MPN_COPY (frac, tmp + i, tmpsize - i);
645 fracsize = tmpsize - i;
649 count_trailing_zeros (cnt_l, tmp[i]);
651 /* Now shift the numbers to their optimal position. */
652 if (i == 0 && BITS_PER_MP_LIMB - 1 - cnt_h > cnt_l)
654 /* We cannot save any memory. Just roll the
655 number so that the leading digit is in a
658 cy = mpn_lshift (frac, tmp, tmpsize, cnt_h + 1);
659 fracsize = tmpsize + 1;
660 frac[fracsize - 1] = cy;
662 else if (BITS_PER_MP_LIMB - 1 - cnt_h <= cnt_l)
664 (void) mpn_rshift (frac, tmp + i, tmpsize - i,
665 BITS_PER_MP_LIMB - 1 - cnt_h);
666 fracsize = tmpsize - i;
670 /* We can only save the memory of the limbs which
671 are zero. The non-zero parts occupy the same
674 (void) mpn_rshift (frac, tmp + (i - 1),
676 BITS_PER_MP_LIMB - 1 - cnt_h);
677 fracsize = tmpsize - (i - 1);
684 while (powers != &_fpioconst_pow10[1] && exponent > 0);
685 /* All factors but 10^-1 are tested now. */
690 cy = mpn_mul_1 (tmp, frac, fracsize, 10);
692 assert (cy == 0 || tmp[tmpsize - 1] < 20);
694 count_trailing_zeros (cnt_l, tmp[0]);
695 if (cnt_l < MIN (4, exponent))
697 cy = mpn_lshift (frac, tmp, tmpsize,
698 BITS_PER_MP_LIMB - MIN (4, exponent));
700 frac[tmpsize++] = cy;
703 (void) mpn_rshift (frac, tmp, tmpsize, MIN (4, exponent));
706 assert (frac[fracsize - 1] < 10);
712 /* This is a special case. We don't need a factor because the
713 numbers are in the range of 1.0 <= |fp| < 8.0. We simply
714 shift it to the right place and divide it by 1.0 to get the
715 leading digit. (Of course this division is not really made.) */
716 assert (0 <= exponent && exponent < 3 &&
717 exponent + to_shift < BITS_PER_MP_LIMB);
719 /* Now shift the input value to its right place. */
720 cy = mpn_lshift (frac, fp_input, fracsize, (exponent + to_shift));
721 frac[fracsize++] = cy;
726 int width = info->width;
727 wchar_t *wstartp, *wcp;
730 int intdig_max, intdig_no = 0;
736 char spec = tolower (info->spec);
742 fracdig_min = fracdig_max = info->prec < 0 ? 6 : info->prec;
743 chars_needed = 1 + 1 + (size_t) fracdig_max + 1 + 1 + 4;
744 /* d . ddd e +- ddd */
745 dig_max = __INT_MAX__; /* Unlimited. */
746 significant = 1; /* Does not matter here. */
748 else if (spec == 'f')
751 fracdig_min = fracdig_max = info->prec < 0 ? 6 : info->prec;
752 dig_max = __INT_MAX__; /* Unlimited. */
753 significant = 1; /* Does not matter here. */
756 intdig_max = exponent + 1;
757 /* This can be really big! */ /* XXX Maybe malloc if too big? */
758 chars_needed = (size_t) exponent + 1 + 1 + (size_t) fracdig_max;
763 chars_needed = 1 + 1 + (size_t) fracdig_max;
768 dig_max = info->prec < 0 ? 6 : (info->prec == 0 ? 1 : info->prec);
769 if ((expsign == 0 && exponent >= dig_max)
770 || (expsign != 0 && exponent > 4))
772 if ('g' - 'G' == 'e' - 'E')
773 type = 'E' + (info->spec - 'G');
775 type = isupper (info->spec) ? 'E' : 'e';
776 fracdig_max = dig_max - 1;
778 chars_needed = 1 + 1 + (size_t) fracdig_max + 1 + 1 + 4;
783 intdig_max = expsign == 0 ? exponent + 1 : 0;
784 fracdig_max = dig_max - intdig_max;
785 /* We need space for the significant digits and perhaps
786 for leading zeros when < 1.0. The number of leading
787 zeros can be as many as would be required for
788 exponential notation with a negative two-digit
789 exponent, which is 4. */
790 chars_needed = (size_t) dig_max + 1 + 4;
792 fracdig_min = info->alt ? fracdig_max : 0;
793 significant = 0; /* We count significant digits. */
798 /* Guess the number of groups we will make, and thus how
799 many spaces we need for separator characters. */
800 ngroups = guess_grouping (intdig_max, grouping);
801 /* Allocate one more character in case rounding increases the
803 chars_needed += ngroups + 1;
806 /* Allocate buffer for output. We need two more because while rounding
807 it is possible that we need two more characters in front of all the
808 other output. If the amount of memory we have to allocate is too
809 large use `malloc' instead of `alloca'. */
810 if (__builtin_expect (chars_needed >= (size_t) -1 / sizeof (wchar_t) - 2
811 || chars_needed < fracdig_max, 0))
813 /* Some overflow occurred. */
814 #if defined HAVE_ERRNO_H && defined ERANGE
819 size_t wbuffer_to_alloc = (2 + chars_needed) * sizeof (wchar_t);
820 buffer_malloced = wbuffer_to_alloc >= 4096;
821 if (__builtin_expect (buffer_malloced, 0))
823 wbuffer = (wchar_t *) malloc (wbuffer_to_alloc);
825 /* Signal an error to the caller. */
829 wbuffer = (wchar_t *) alloca (wbuffer_to_alloc);
830 wcp = wstartp = wbuffer + 2; /* Let room for rounding. */
832 /* Do the real work: put digits in allocated buffer. */
833 if (expsign == 0 || type != 'f')
835 assert (expsign == 0 || intdig_max == 1);
836 while (intdig_no < intdig_max)
839 *wcp++ = hack_digit ();
844 || (fracdig_max > 0 && (fracsize > 1 || frac[0] != 0)))
849 /* |fp| < 1.0 and the selected type is 'f', so put "0."
856 /* Generate the needed number of fractional digits. */
859 while (fracdig_no < fracdig_min + added_zeros
860 || (fracdig_no < fracdig_max && (fracsize > 1 || frac[0] != 0)))
863 *wcp = hack_digit ();
864 if (*wcp++ != L_('0'))
866 else if (significant == 0)
875 digit = hack_digit ();
881 && ((*(wcp - 1) != decimalwc && (*(wcp - 1) & 1) == 0)
882 || ((*(wcp - 1) == decimalwc && (*(wcp - 2) & 1) == 0))))
884 /* This is the critical case. */
885 if (fracsize == 1 && frac[0] == 0)
886 /* Rest of the number is zero -> round to even.
887 (IEEE 754-1985 4.1 says this is the default rounding.) */
889 else if (scalesize == 0)
891 /* Here we have to see whether all limbs are zero since no
892 normalization happened. */
893 size_t lcnt = fracsize;
894 while (lcnt >= 1 && frac[lcnt - 1] == 0)
897 /* Rest of the number is zero -> round to even.
898 (IEEE 754-1985 4.1 says this is the default rounding.) */
905 /* Process fractional digits. Terminate if not rounded or
906 radix character is reached. */
908 while (*--wtp != decimalwc && *wtp == L_('9'))
913 if (removed == fracdig_min && added_zeros > 0)
915 if (*wtp != decimalwc)
918 else if (__builtin_expect (spec == 'g' && type == 'f' && info->alt
919 && wtp == wstartp + 1
920 && wstartp[0] == L_('0'),
922 /* This is a special case: the rounded number is 1.0,
923 the format is 'g' or 'G', and the alternative format
924 is selected. This means the result must be "1.". */
928 if (fracdig_no == 0 || *wtp == decimalwc)
930 /* Round the integer digits. */
931 if (*(wtp - 1) == decimalwc)
934 while (--wtp >= wstartp && *wtp == L_('9'))
941 /* It is more critical. All digits were 9's. */
946 exponent += expsign == 0 ? 1 : -1;
948 /* The above exponent adjustment could lead to 1.0e-00,
949 e.g. for 0.999999999. Make sure exponent 0 always
954 else if (intdig_no == dig_max)
956 /* This is the case where for type %g the number fits
957 really in the range for %f output but after rounding
958 the number of digits is too big. */
959 *--wstartp = decimalwc;
960 *--wstartp = L_('1');
962 if (info->alt || fracdig_no > 0)
964 /* Overwrite the old radix character. */
965 wstartp[intdig_no + 2] = L_('0');
969 fracdig_no += intdig_no;
971 fracdig_max = intdig_max - intdig_no;
973 /* Now we must print the exponent. */
974 type = isupper (info->spec) ? 'E' : 'e';
978 /* We can simply add another another digit before the
980 *--wstartp = L_('1');
984 /* While rounding the number of digits can change.
985 If the number now exceeds the limits remove some
986 fractional digits. */
987 if (intdig_no + fracdig_no > dig_max)
989 wcp -= intdig_no + fracdig_no - dig_max;
990 fracdig_no -= intdig_no + fracdig_no - dig_max;
997 /* Now remove unnecessary '0' at the end of the string. */
998 while (fracdig_no > fracdig_min + added_zeros && *(wcp - 1) == L_('0'))
1003 /* If we eliminate all fractional digits we perhaps also can remove
1004 the radix character. */
1005 if (fracdig_no == 0 && !info->alt && *(wcp - 1) == decimalwc)
1010 /* Rounding might have changed the number of groups. We allocated
1011 enough memory but we need here the correct number of groups. */
1012 if (intdig_no != intdig_max)
1013 ngroups = guess_grouping (intdig_no, grouping);
1015 /* Add in separator characters, overwriting the same buffer. */
1016 wcp = group_number (wstartp, wcp, intdig_no, grouping, thousands_sepwc,
1020 /* Write the exponent if it is needed. */
1023 if (__builtin_expect (expsign != 0 && exponent == 4 && spec == 'g', 0))
1025 /* This is another special case. The exponent of the number is
1026 really smaller than -4, which requires the 'e'/'E' format.
1027 But after rounding the number has an exponent of -4. */
1028 assert (wcp >= wstartp + 1);
1029 assert (wstartp[0] == L_('1'));
1030 memcpy (wstartp, L_("0.0001"), 6 * sizeof (wchar_t));
1031 wstartp[1] = decimalwc;
1032 if (wcp >= wstartp + 2)
1035 for (cnt = 0; cnt < wcp - (wstartp + 2); cnt++)
1036 wstartp[6 + cnt] = L_('0');
1044 *wcp++ = (wchar_t) type;
1045 *wcp++ = expsign ? L_('-') : L_('+');
1047 /* Find the magnitude of the exponent. */
1049 while (expscale <= exponent)
1053 /* Exponent always has at least two digits. */
1059 *wcp++ = L_('0') + (exponent / expscale);
1060 exponent %= expscale;
1062 while (expscale > 10);
1063 *wcp++ = L_('0') + exponent;
1067 /* Compute number of characters which must be filled with the padding
1069 if (is_neg || info->showsign || info->space)
1071 width -= wcp - wstartp;
1073 if (!info->left && info->pad != '0' && width > 0)
1074 PADN (info->pad, width);
1078 else if (info->showsign)
1080 else if (info->space)
1083 if (!info->left && info->pad == '0' && width > 0)
1087 char *buffer = NULL;
1088 char *buffer_end __attribute__((__unused__)) = NULL;
1094 /* Create the single byte string. */
1096 size_t thousands_sep_len;
1098 #ifdef USE_LOCALE_SUPPORT
1099 size_t factor = ((info->i18n && USE_I18N_NUMBER_H)
1100 ? nl_langinfo_wc (_NL_CTYPE_MB_CUR_MAX)
1106 decimal_len = strlen (decimal);
1108 if (thousands_sep == NULL)
1109 thousands_sep_len = 0;
1111 thousands_sep_len = strlen (thousands_sep);
1113 size_t nbuffer = (2 + chars_needed * factor + decimal_len
1114 + ngroups * thousands_sep_len);
1115 if (__builtin_expect (buffer_malloced, 0))
1117 buffer = (char *) malloc (nbuffer);
1120 /* Signal an error to the caller. */
1126 buffer = (char *) alloca (nbuffer);
1127 buffer_end = buffer + nbuffer;
1129 /* Now copy the wide character string. Since the character
1130 (except for the decimal point and thousands separator) must
1131 be coming from the ASCII range we can esily convert the
1132 string without mapping tables. */
1133 for (cp = buffer, copywc = wstartp; copywc < wcp; ++copywc)
1134 if (*copywc == decimalwc)
1135 memcpy (cp, decimal, decimal_len), cp += decimal_len;
1136 else if (*copywc == thousands_sepwc)
1137 mempcpy (cp, thousands_sep, thousands_sep_len), cp += thousands_sep_len;
1139 *cp++ = (char) *copywc;
1143 #if USE_I18N_NUMBER_H
1144 if (__builtin_expect (info->i18n, 0))
1146 tmpptr = _i18n_number_rewrite (tmpptr, cp, buffer_end);
1148 assert ((uintptr_t) buffer <= (uintptr_t) tmpptr);
1149 assert ((uintptr_t) tmpptr < (uintptr_t) buffer_end);
1153 PRINT (tmpptr, wstartp, wide ? wcp - wstartp : cp - tmpptr);
1155 /* Free the memory if necessary. */
1156 if (__builtin_expect (buffer_malloced, 0))
1163 if (info->left && width > 0)
1164 PADN (info->pad, width);
1169 /* Return the number of extra grouping characters that will be inserted
1170 into a number with INTDIG_MAX integer digits. */
1173 guess_grouping (unsigned int intdig_max, const char *grouping)
1175 unsigned int groups;
1177 /* We treat all negative values like CHAR_MAX. */
1179 if (*grouping == CHAR_MAX || *grouping <= 0)
1180 /* No grouping should be done. */
1184 while (intdig_max > (unsigned int) *grouping)
1187 intdig_max -= *grouping++;
1191 /* Same grouping repeats. */
1192 groups += (intdig_max - 1) / grouping[-1];
1195 else if (*grouping == CHAR_MAX || *grouping <= 0)
1196 /* No more grouping should be done. */
1203 /* Group the INTDIG_NO integer digits of the number in [BUF,BUFEND).
1204 There is guaranteed enough space past BUFEND to extend it.
1205 Return the new end of buffer. */
1208 group_number (wchar_t *buf, wchar_t *bufend, unsigned int intdig_no,
1209 const char *grouping, wchar_t thousands_sep, int ngroups)
1216 /* Move the fractional part down. */
1217 memmove (buf + intdig_no + ngroups, buf + intdig_no,
1218 (bufend - (buf + intdig_no)) * sizeof (wchar_t));
1220 p = buf + intdig_no + ngroups - 1;
1223 unsigned int len = *grouping++;
1225 *p-- = buf[--intdig_no];
1227 *p-- = thousands_sep;
1230 /* Same grouping repeats. */
1232 else if (*grouping == CHAR_MAX || *grouping <= 0)
1233 /* No more grouping should be done. */
1235 } while (intdig_no > (unsigned int) *grouping);
1237 /* Copy the remaining ungrouped digits. */
1239 *p-- = buf[--intdig_no];
1242 return bufend + ngroups;