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
36 #include "quadmath-printf.h"
37 #include "fpioconst.h"
39 #ifdef USE_I18N_NUMBER_H
40 #include "_i18n_number.h"
42 #define USE_I18N_NUMBER_H 0
46 /* Macros for doing the actual output. */
51 register const int outc = (ch); \
52 if (PUTC (outc, fp) == EOF) \
54 if (buffer_malloced) \
61 #define PRINT(ptr, wptr, len) \
64 register size_t outlen = (len); \
67 if (PUT (fp, wide ? (const char *) wptr : ptr, outlen) != outlen) \
69 if (buffer_malloced) \
79 while (outlen-- > 0) \
82 while (outlen-- > 0) \
87 #define PADN(ch, len) \
90 if (PAD (fp, ch, len) != len) \
92 if (buffer_malloced) \
101 /* We use the GNU MP library to handle large numbers.
103 An MP variable occupies a varying number of entries in its array. We keep
104 track of this number for efficiency reasons. Otherwise we would always
105 have to process the whole array. */
106 #define MPN_VAR(name) mp_limb_t *name; mp_size_t name##size
108 #define MPN_ASSIGN(dst,src) \
109 memcpy (dst, src, (dst##size = src##size) * sizeof (mp_limb_t))
110 #define MPN_GE(u,v) \
111 (u##size > v##size || (u##size == v##size && mpn_cmp (u, v, u##size) >= 0))
113 extern mp_size_t mpn_extract_flt128 (mp_ptr res_ptr, mp_size_t size,
114 int *expt, int *is_neg,
115 __float128 value) attribute_hidden;
116 static unsigned int guess_grouping (unsigned int intdig_max,
117 const char *grouping);
120 static wchar_t *group_number (wchar_t *buf, wchar_t *bufend,
121 unsigned int intdig_no, const char *grouping,
122 wchar_t thousands_sep, int ngroups);
126 __quadmath_printf_fp (struct __quadmath_printf_file *fp,
127 const struct printf_info *info,
128 const void *const *args)
130 /* The floating-point value to output. */
133 /* Locale-dependent representation of decimal point. */
137 /* Locale-dependent thousands separator and grouping specification. */
138 const char *thousands_sep = NULL;
139 wchar_t thousands_sepwc = L_('\0');
140 const char *grouping;
142 /* "NaN" or "Inf" for the special cases. */
143 const char *special = NULL;
144 const wchar_t *wspecial = NULL;
146 /* We need just a few limbs for the input before shifting to the right
148 mp_limb_t fp_input[(FLT128_MANT_DIG + BITS_PER_MP_LIMB - 1) / BITS_PER_MP_LIMB];
149 /* We need to shift the contents of fp_input by this amount of bits. */
152 /* The fraction of the floting-point value in question */
154 /* and the exponent. */
156 /* Sign of the exponent. */
158 /* Sign of float number. */
161 /* Scaling factor. */
164 /* Temporary bignum value. */
167 /* Digit which is result of last hack_digit() call. */
170 /* The type of output format that will be used: 'e'/'E' or 'f'. */
173 /* Counter for number of written characters. */
176 /* General helper (carry limb). */
179 /* Nonzero if this is output on a wide character stream. */
180 int wide = info->wide;
182 /* Buffer in which we produce the output. */
183 wchar_t *wbuffer = NULL;
184 /* Flag whether wbuffer is malloc'ed or not. */
185 int buffer_malloced = 0;
187 auto wchar_t hack_digit (void);
189 wchar_t hack_digit (void)
193 if (expsign != 0 && type == 'f' && exponent-- > 0)
195 else if (scalesize == 0)
197 hi = frac[fracsize - 1];
198 frac[fracsize - 1] = mpn_mul_1 (frac, frac, fracsize - 1, 10);
202 if (fracsize < scalesize)
206 hi = mpn_divmod (tmp, frac, fracsize, scale, scalesize);
207 tmp[fracsize - scalesize] = hi;
210 fracsize = scalesize;
211 while (fracsize != 0 && frac[fracsize - 1] == 0)
215 /* We're not prepared for an mpn variable with zero
222 mp_limb_t _cy = mpn_mul_1 (frac, frac, fracsize, 10);
224 frac[fracsize++] = _cy;
230 /* Figure out the decimal point character. */
231 #ifdef USE_LOCALE_SUPPORT
232 if (info->extra == 0)
234 decimal = nl_langinfo (DECIMAL_POINT);
235 decimalwc = nl_langinfo_wc (_NL_NUMERIC_DECIMAL_POINT_WC);
239 decimal = nl_langinfo (MON_DECIMAL_POINT);
240 if (*decimal == '\0')
241 decimal = nl_langinfo (DECIMAL_POINT);
242 decimalwc = nl_langinfo_wc (_NL_MONETARY_DECIMAL_POINT_WC);
243 if (decimalwc == L_('\0'))
244 decimalwc = nl_langinfo_wc (_NL_NUMERIC_DECIMAL_POINT_WC);
246 /* The decimal point character must not be zero. */
247 assert (*decimal != '\0');
248 assert (decimalwc != L_('\0'));
254 #ifdef USE_LOCALE_SUPPORT
257 if (info->extra == 0)
258 grouping = nl_langinfo (GROUPING);
260 grouping = nl_langinfo (MON_GROUPING);
262 if (*grouping <= 0 || *grouping == CHAR_MAX)
266 /* Figure out the thousands separator character. */
269 if (info->extra == 0)
270 thousands_sepwc = nl_langinfo_wc (_NL_NUMERIC_THOUSANDS_SEP_WC);
272 thousands_sepwc = nl_langinfo_wc (_NL_MONETARY_THOUSANDS_SEP_WC);
276 if (info->extra == 0)
277 thousands_sep = nl_langinfo (THOUSANDS_SEP);
279 thousands_sep = nl_langinfo (MON_THOUSANDS_SEP);
282 if ((wide && thousands_sepwc == L_('\0'))
283 || (! wide && *thousands_sep == '\0'))
285 else if (thousands_sepwc == L_('\0'))
286 /* If we are printing multibyte characters and there is a
287 multibyte representation for the thousands separator,
288 we must ensure the wide character thousands separator
289 is available, even if it is fake. */
290 thousands_sepwc = (wchar_t) 0xfffffffe;
297 /* Fetch the argument value. */
299 fpnum = **(const __float128 **) args[0];
301 /* Check for special values: not a number or infinity. */
304 ieee854_float128 u = { .value = fpnum };
305 is_neg = u.ieee.negative != 0;
306 if (isupper (info->spec))
309 wspecial = L_("NAN");
314 wspecial = L_("nan");
317 else if (isinfq (fpnum))
320 if (isupper (info->spec))
323 wspecial = L_("INF");
328 wspecial = L_("inf");
333 fracsize = mpn_extract_flt128 (fp_input,
335 sizeof (fp_input[0])),
336 &exponent, &is_neg, fpnum);
337 to_shift = 1 + fracsize * BITS_PER_MP_LIMB - FLT128_MANT_DIG;
343 int width = info->width;
345 if (is_neg || info->showsign || info->space)
349 if (!info->left && width > 0)
354 else if (info->showsign)
356 else if (info->space)
359 PRINT (special, wspecial, 3);
361 if (info->left && width > 0)
368 /* We need three multiprecision variables. Now that we have the exponent
369 of the number we can allocate the needed memory. It would be more
370 efficient to use variables of the fixed maximum size but because this
371 would be really big it could lead to memory problems. */
373 mp_size_t bignum_size = ((ABS (exponent) + BITS_PER_MP_LIMB - 1)
375 + (FLT128_MANT_DIG / BITS_PER_MP_LIMB > 2 ? 8 : 4))
376 * sizeof (mp_limb_t);
377 frac = (mp_limb_t *) alloca (bignum_size);
378 tmp = (mp_limb_t *) alloca (bignum_size);
379 scale = (mp_limb_t *) alloca (bignum_size);
382 /* We now have to distinguish between numbers with positive and negative
383 exponents because the method used for the one is not applicable/efficient
390 int explog = FLT128_MAX_10_EXP_LOG;
392 const struct mp_power *powers = &_fpioconst_pow10[explog + 1];
395 if ((exponent + to_shift) % BITS_PER_MP_LIMB == 0)
397 MPN_COPY_DECR (frac + (exponent + to_shift) / BITS_PER_MP_LIMB,
399 fracsize += (exponent + to_shift) / BITS_PER_MP_LIMB;
403 cy = mpn_lshift (frac + (exponent + to_shift) / BITS_PER_MP_LIMB,
405 (exponent + to_shift) % BITS_PER_MP_LIMB);
406 fracsize += (exponent + to_shift) / BITS_PER_MP_LIMB;
408 frac[fracsize++] = cy;
410 MPN_ZERO (frac, (exponent + to_shift) / BITS_PER_MP_LIMB);
412 assert (powers > &_fpioconst_pow10[0]);
417 /* The number of the product of two binary numbers with n and m
418 bits respectively has m+n or m+n-1 bits. */
419 if (exponent >= scaleexpo + powers->p_expo - 1)
423 if (FLT128_MANT_DIG > _FPIO_CONST_OFFSET * BITS_PER_MP_LIMB)
425 #define _FPIO_CONST_SHIFT \
426 (((FLT128_MANT_DIG + BITS_PER_MP_LIMB - 1) / BITS_PER_MP_LIMB) \
427 - _FPIO_CONST_OFFSET)
428 /* 64bit const offset is not enough for
429 IEEE quad long double. */
430 tmpsize = powers->arraysize + _FPIO_CONST_SHIFT;
431 memcpy (tmp + _FPIO_CONST_SHIFT,
432 &__tens[powers->arrayoff],
433 tmpsize * sizeof (mp_limb_t));
434 MPN_ZERO (tmp, _FPIO_CONST_SHIFT);
435 /* Adjust exponent, as scaleexpo will be this much
437 exponent += _FPIO_CONST_SHIFT * BITS_PER_MP_LIMB;
441 tmpsize = powers->arraysize;
442 memcpy (tmp, &__tens[powers->arrayoff],
443 tmpsize * sizeof (mp_limb_t));
448 cy = mpn_mul (tmp, scale, scalesize,
449 &__tens[powers->arrayoff
450 + _FPIO_CONST_OFFSET],
451 powers->arraysize - _FPIO_CONST_OFFSET);
452 tmpsize = scalesize + powers->arraysize - _FPIO_CONST_OFFSET;
457 if (MPN_GE (frac, tmp))
460 MPN_ASSIGN (scale, tmp);
461 count_leading_zeros (cnt, scale[scalesize - 1]);
462 scaleexpo = (scalesize - 2) * BITS_PER_MP_LIMB - cnt - 1;
463 exp10 |= 1 << explog;
468 while (powers > &_fpioconst_pow10[0]);
471 /* Optimize number representations. We want to represent the numbers
472 with the lowest number of bytes possible without losing any
473 bytes. Also the highest bit in the scaling factor has to be set
474 (this is a requirement of the MPN division routines). */
477 /* Determine minimum number of zero bits at the end of
479 for (i = 0; scale[i] == 0 && frac[i] == 0; i++)
482 /* Determine number of bits the scaling factor is misplaced. */
483 count_leading_zeros (cnt_h, scale[scalesize - 1]);
487 /* The highest bit of the scaling factor is already set. So
488 we only have to remove the trailing empty limbs. */
491 MPN_COPY_INCR (scale, scale + i, scalesize - i);
493 MPN_COPY_INCR (frac, frac + i, fracsize - i);
501 count_trailing_zeros (cnt_l, scale[i]);
505 count_trailing_zeros (cnt_l2, frac[i]);
511 count_trailing_zeros (cnt_l, frac[i]);
513 /* Now shift the numbers to their optimal position. */
514 if (i == 0 && BITS_PER_MP_LIMB - cnt_h > cnt_l)
516 /* We cannot save any memory. So just roll both numbers
517 so that the scaling factor has its highest bit set. */
519 (void) mpn_lshift (scale, scale, scalesize, cnt_h);
520 cy = mpn_lshift (frac, frac, fracsize, cnt_h);
522 frac[fracsize++] = cy;
524 else if (BITS_PER_MP_LIMB - cnt_h <= cnt_l)
526 /* We can save memory by removing the trailing zero limbs
527 and by packing the non-zero limbs which gain another
530 (void) mpn_rshift (scale, scale + i, scalesize - i,
531 BITS_PER_MP_LIMB - cnt_h);
533 (void) mpn_rshift (frac, frac + i, fracsize - i,
534 BITS_PER_MP_LIMB - cnt_h);
535 fracsize -= frac[fracsize - i - 1] == 0 ? i + 1 : i;
539 /* We can only save the memory of the limbs which are zero.
540 The non-zero parts occupy the same number of limbs. */
542 (void) mpn_rshift (scale, scale + (i - 1),
544 BITS_PER_MP_LIMB - cnt_h);
546 (void) mpn_rshift (frac, frac + (i - 1),
548 BITS_PER_MP_LIMB - cnt_h);
549 fracsize -= frac[fracsize - (i - 1) - 1] == 0 ? i : i - 1;
554 else if (exponent < 0)
558 int explog = FLT128_MAX_10_EXP_LOG;
559 const struct mp_power *powers = &_fpioconst_pow10[explog + 1];
561 /* Now shift the input value to its right place. */
562 cy = mpn_lshift (frac, fp_input, fracsize, to_shift);
563 frac[fracsize++] = cy;
564 assert (cy == 1 || (frac[fracsize - 2] == 0 && frac[0] == 0));
567 exponent = -exponent;
569 assert (powers != &_fpioconst_pow10[0]);
574 if (exponent >= powers->m_expo)
576 int i, incr, cnt_h, cnt_l;
579 /* The mpn_mul function expects the first argument to be
580 bigger than the second. */
581 if (fracsize < powers->arraysize - _FPIO_CONST_OFFSET)
582 cy = mpn_mul (tmp, &__tens[powers->arrayoff
583 + _FPIO_CONST_OFFSET],
584 powers->arraysize - _FPIO_CONST_OFFSET,
587 cy = mpn_mul (tmp, frac, fracsize,
588 &__tens[powers->arrayoff + _FPIO_CONST_OFFSET],
589 powers->arraysize - _FPIO_CONST_OFFSET);
590 tmpsize = fracsize + powers->arraysize - _FPIO_CONST_OFFSET;
594 count_leading_zeros (cnt_h, tmp[tmpsize - 1]);
595 incr = (tmpsize - fracsize) * BITS_PER_MP_LIMB
596 + BITS_PER_MP_LIMB - 1 - cnt_h;
598 assert (incr <= powers->p_expo);
600 /* If we increased the exponent by exactly 3 we have to test
601 for overflow. This is done by comparing with 10 shifted
602 to the right position. */
603 if (incr == exponent + 3)
605 if (cnt_h <= BITS_PER_MP_LIMB - 4)
609 = ((mp_limb_t) 10) << (BITS_PER_MP_LIMB - 4 - cnt_h);
613 topval[0] = ((mp_limb_t) 10) << (BITS_PER_MP_LIMB - 4);
615 (void) mpn_lshift (topval, topval, 2,
616 BITS_PER_MP_LIMB - cnt_h);
620 /* We have to be careful when multiplying the last factor.
621 If the result is greater than 1.0 be have to test it
622 against 10.0. If it is greater or equal to 10.0 the
623 multiplication was not valid. This is because we cannot
624 determine the number of bits in the result in advance. */
625 if (incr < exponent + 3
626 || (incr == exponent + 3 &&
627 (tmp[tmpsize - 1] < topval[1]
628 || (tmp[tmpsize - 1] == topval[1]
629 && tmp[tmpsize - 2] < topval[0]))))
631 /* The factor is right. Adapt binary and decimal
634 exp10 |= 1 << explog;
636 /* If this factor yields a number greater or equal to
637 1.0, we must not shift the non-fractional digits down. */
641 /* Now we optimize the number representation. */
642 for (i = 0; tmp[i] == 0; ++i);
643 if (cnt_h == BITS_PER_MP_LIMB - 1)
645 MPN_COPY (frac, tmp + i, tmpsize - i);
646 fracsize = tmpsize - i;
650 count_trailing_zeros (cnt_l, tmp[i]);
652 /* Now shift the numbers to their optimal position. */
653 if (i == 0 && BITS_PER_MP_LIMB - 1 - cnt_h > cnt_l)
655 /* We cannot save any memory. Just roll the
656 number so that the leading digit is in a
659 cy = mpn_lshift (frac, tmp, tmpsize, cnt_h + 1);
660 fracsize = tmpsize + 1;
661 frac[fracsize - 1] = cy;
663 else if (BITS_PER_MP_LIMB - 1 - cnt_h <= cnt_l)
665 (void) mpn_rshift (frac, tmp + i, tmpsize - i,
666 BITS_PER_MP_LIMB - 1 - cnt_h);
667 fracsize = tmpsize - i;
671 /* We can only save the memory of the limbs which
672 are zero. The non-zero parts occupy the same
675 (void) mpn_rshift (frac, tmp + (i - 1),
677 BITS_PER_MP_LIMB - 1 - cnt_h);
678 fracsize = tmpsize - (i - 1);
685 while (powers != &_fpioconst_pow10[1] && exponent > 0);
686 /* All factors but 10^-1 are tested now. */
691 cy = mpn_mul_1 (tmp, frac, fracsize, 10);
693 assert (cy == 0 || tmp[tmpsize - 1] < 20);
695 count_trailing_zeros (cnt_l, tmp[0]);
696 if (cnt_l < MIN (4, exponent))
698 cy = mpn_lshift (frac, tmp, tmpsize,
699 BITS_PER_MP_LIMB - MIN (4, exponent));
701 frac[tmpsize++] = cy;
704 (void) mpn_rshift (frac, tmp, tmpsize, MIN (4, exponent));
707 assert (frac[fracsize - 1] < 10);
713 /* This is a special case. We don't need a factor because the
714 numbers are in the range of 1.0 <= |fp| < 8.0. We simply
715 shift it to the right place and divide it by 1.0 to get the
716 leading digit. (Of course this division is not really made.) */
717 assert (0 <= exponent && exponent < 3 &&
718 exponent + to_shift < BITS_PER_MP_LIMB);
720 /* Now shift the input value to its right place. */
721 cy = mpn_lshift (frac, fp_input, fracsize, (exponent + to_shift));
722 frac[fracsize++] = cy;
727 int width = info->width;
728 wchar_t *wstartp, *wcp;
731 int intdig_max, intdig_no = 0;
737 char spec = tolower (info->spec);
743 fracdig_min = fracdig_max = info->prec < 0 ? 6 : info->prec;
744 chars_needed = 1 + 1 + (size_t) fracdig_max + 1 + 1 + 4;
745 /* d . ddd e +- ddd */
746 dig_max = __INT_MAX__; /* Unlimited. */
747 significant = 1; /* Does not matter here. */
749 else if (spec == 'f')
752 fracdig_min = fracdig_max = info->prec < 0 ? 6 : info->prec;
753 dig_max = __INT_MAX__; /* Unlimited. */
754 significant = 1; /* Does not matter here. */
757 intdig_max = exponent + 1;
758 /* This can be really big! */ /* XXX Maybe malloc if too big? */
759 chars_needed = (size_t) exponent + 1 + 1 + (size_t) fracdig_max;
764 chars_needed = 1 + 1 + (size_t) fracdig_max;
769 dig_max = info->prec < 0 ? 6 : (info->prec == 0 ? 1 : info->prec);
770 if ((expsign == 0 && exponent >= dig_max)
771 || (expsign != 0 && exponent > 4))
773 if ('g' - 'G' == 'e' - 'E')
774 type = 'E' + (info->spec - 'G');
776 type = isupper (info->spec) ? 'E' : 'e';
777 fracdig_max = dig_max - 1;
779 chars_needed = 1 + 1 + (size_t) fracdig_max + 1 + 1 + 4;
784 intdig_max = expsign == 0 ? exponent + 1 : 0;
785 fracdig_max = dig_max - intdig_max;
786 /* We need space for the significant digits and perhaps
787 for leading zeros when < 1.0. The number of leading
788 zeros can be as many as would be required for
789 exponential notation with a negative two-digit
790 exponent, which is 4. */
791 chars_needed = (size_t) dig_max + 1 + 4;
793 fracdig_min = info->alt ? fracdig_max : 0;
794 significant = 0; /* We count significant digits. */
799 /* Guess the number of groups we will make, and thus how
800 many spaces we need for separator characters. */
801 ngroups = guess_grouping (intdig_max, grouping);
802 /* Allocate one more character in case rounding increases the
804 chars_needed += ngroups + 1;
807 /* Allocate buffer for output. We need two more because while rounding
808 it is possible that we need two more characters in front of all the
809 other output. If the amount of memory we have to allocate is too
810 large use `malloc' instead of `alloca'. */
811 if (__builtin_expect (chars_needed >= (size_t) -1 / sizeof (wchar_t) - 2
812 || chars_needed < fracdig_max, 0))
814 /* Some overflow occurred. */
815 #if defined HAVE_ERRNO_H && defined ERANGE
820 size_t wbuffer_to_alloc = (2 + chars_needed) * sizeof (wchar_t);
821 buffer_malloced = wbuffer_to_alloc >= 4096;
822 if (__builtin_expect (buffer_malloced, 0))
824 wbuffer = (wchar_t *) malloc (wbuffer_to_alloc);
826 /* Signal an error to the caller. */
830 wbuffer = (wchar_t *) alloca (wbuffer_to_alloc);
831 wcp = wstartp = wbuffer + 2; /* Let room for rounding. */
833 /* Do the real work: put digits in allocated buffer. */
834 if (expsign == 0 || type != 'f')
836 assert (expsign == 0 || intdig_max == 1);
837 while (intdig_no < intdig_max)
840 *wcp++ = hack_digit ();
845 || (fracdig_max > 0 && (fracsize > 1 || frac[0] != 0)))
850 /* |fp| < 1.0 and the selected type is 'f', so put "0."
857 /* Generate the needed number of fractional digits. */
860 while (fracdig_no < fracdig_min + added_zeros
861 || (fracdig_no < fracdig_max && (fracsize > 1 || frac[0] != 0)))
864 *wcp = hack_digit ();
865 if (*wcp++ != L_('0'))
867 else if (significant == 0)
876 digit = hack_digit ();
882 && ((*(wcp - 1) != decimalwc && (*(wcp - 1) & 1) == 0)
883 || ((*(wcp - 1) == decimalwc && (*(wcp - 2) & 1) == 0))))
885 /* This is the critical case. */
886 if (fracsize == 1 && frac[0] == 0)
887 /* Rest of the number is zero -> round to even.
888 (IEEE 754-1985 4.1 says this is the default rounding.) */
890 else if (scalesize == 0)
892 /* Here we have to see whether all limbs are zero since no
893 normalization happened. */
894 size_t lcnt = fracsize;
895 while (lcnt >= 1 && frac[lcnt - 1] == 0)
898 /* Rest of the number is zero -> round to even.
899 (IEEE 754-1985 4.1 says this is the default rounding.) */
906 /* Process fractional digits. Terminate if not rounded or
907 radix character is reached. */
909 while (*--wtp != decimalwc && *wtp == L_('9'))
914 if (removed == fracdig_min && added_zeros > 0)
916 if (*wtp != decimalwc)
919 else if (__builtin_expect (spec == 'g' && type == 'f' && info->alt
920 && wtp == wstartp + 1
921 && wstartp[0] == L_('0'),
923 /* This is a special case: the rounded number is 1.0,
924 the format is 'g' or 'G', and the alternative format
925 is selected. This means the result must be "1.". */
929 if (fracdig_no == 0 || *wtp == decimalwc)
931 /* Round the integer digits. */
932 if (*(wtp - 1) == decimalwc)
935 while (--wtp >= wstartp && *wtp == L_('9'))
942 /* It is more critical. All digits were 9's. */
947 exponent += expsign == 0 ? 1 : -1;
949 /* The above exponent adjustment could lead to 1.0e-00,
950 e.g. for 0.999999999. Make sure exponent 0 always
955 else if (intdig_no == dig_max)
957 /* This is the case where for type %g the number fits
958 really in the range for %f output but after rounding
959 the number of digits is too big. */
960 *--wstartp = decimalwc;
961 *--wstartp = L_('1');
963 if (info->alt || fracdig_no > 0)
965 /* Overwrite the old radix character. */
966 wstartp[intdig_no + 2] = L_('0');
970 fracdig_no += intdig_no;
972 fracdig_max = intdig_max - intdig_no;
974 /* Now we must print the exponent. */
975 type = isupper (info->spec) ? 'E' : 'e';
979 /* We can simply add another another digit before the
981 *--wstartp = L_('1');
985 /* While rounding the number of digits can change.
986 If the number now exceeds the limits remove some
987 fractional digits. */
988 if (intdig_no + fracdig_no > dig_max)
990 wcp -= intdig_no + fracdig_no - dig_max;
991 fracdig_no -= intdig_no + fracdig_no - dig_max;
998 /* Now remove unnecessary '0' at the end of the string. */
999 while (fracdig_no > fracdig_min + added_zeros && *(wcp - 1) == L_('0'))
1004 /* If we eliminate all fractional digits we perhaps also can remove
1005 the radix character. */
1006 if (fracdig_no == 0 && !info->alt && *(wcp - 1) == decimalwc)
1011 /* Rounding might have changed the number of groups. We allocated
1012 enough memory but we need here the correct number of groups. */
1013 if (intdig_no != intdig_max)
1014 ngroups = guess_grouping (intdig_no, grouping);
1016 /* Add in separator characters, overwriting the same buffer. */
1017 wcp = group_number (wstartp, wcp, intdig_no, grouping, thousands_sepwc,
1021 /* Write the exponent if it is needed. */
1024 if (__builtin_expect (expsign != 0 && exponent == 4 && spec == 'g', 0))
1026 /* This is another special case. The exponent of the number is
1027 really smaller than -4, which requires the 'e'/'E' format.
1028 But after rounding the number has an exponent of -4. */
1029 assert (wcp >= wstartp + 1);
1030 assert (wstartp[0] == L_('1'));
1031 memcpy (wstartp, L_("0.0001"), 6 * sizeof (wchar_t));
1032 wstartp[1] = decimalwc;
1033 if (wcp >= wstartp + 2)
1036 for (cnt = 0; cnt < wcp - (wstartp + 2); cnt++)
1037 wstartp[6 + cnt] = L_('0');
1045 *wcp++ = (wchar_t) type;
1046 *wcp++ = expsign ? L_('-') : L_('+');
1048 /* Find the magnitude of the exponent. */
1050 while (expscale <= exponent)
1054 /* Exponent always has at least two digits. */
1060 *wcp++ = L_('0') + (exponent / expscale);
1061 exponent %= expscale;
1063 while (expscale > 10);
1064 *wcp++ = L_('0') + exponent;
1068 /* Compute number of characters which must be filled with the padding
1070 if (is_neg || info->showsign || info->space)
1072 width -= wcp - wstartp;
1074 if (!info->left && info->pad != '0' && width > 0)
1075 PADN (info->pad, width);
1079 else if (info->showsign)
1081 else if (info->space)
1084 if (!info->left && info->pad == '0' && width > 0)
1088 char *buffer = NULL;
1089 char *buffer_end __attribute__((__unused__)) = NULL;
1095 /* Create the single byte string. */
1097 size_t thousands_sep_len;
1099 #ifdef USE_LOCALE_SUPPORT
1100 size_t factor = ((info->i18n && USE_I18N_NUMBER_H)
1101 ? nl_langinfo_wc (_NL_CTYPE_MB_CUR_MAX)
1107 decimal_len = strlen (decimal);
1109 if (thousands_sep == NULL)
1110 thousands_sep_len = 0;
1112 thousands_sep_len = strlen (thousands_sep);
1114 size_t nbuffer = (2 + chars_needed * factor + decimal_len
1115 + ngroups * thousands_sep_len);
1116 if (__builtin_expect (buffer_malloced, 0))
1118 buffer = (char *) malloc (nbuffer);
1121 /* Signal an error to the caller. */
1127 buffer = (char *) alloca (nbuffer);
1128 buffer_end = buffer + nbuffer;
1130 /* Now copy the wide character string. Since the character
1131 (except for the decimal point and thousands separator) must
1132 be coming from the ASCII range we can esily convert the
1133 string without mapping tables. */
1134 for (cp = buffer, copywc = wstartp; copywc < wcp; ++copywc)
1135 if (*copywc == decimalwc)
1136 memcpy (cp, decimal, decimal_len), cp += decimal_len;
1137 else if (*copywc == thousands_sepwc)
1138 mempcpy (cp, thousands_sep, thousands_sep_len), cp += thousands_sep_len;
1140 *cp++ = (char) *copywc;
1144 #if USE_I18N_NUMBER_H
1145 if (__builtin_expect (info->i18n, 0))
1147 tmpptr = _i18n_number_rewrite (tmpptr, cp, buffer_end);
1149 assert ((uintptr_t) buffer <= (uintptr_t) tmpptr);
1150 assert ((uintptr_t) tmpptr < (uintptr_t) buffer_end);
1154 PRINT (tmpptr, wstartp, wide ? wcp - wstartp : cp - tmpptr);
1156 /* Free the memory if necessary. */
1157 if (__builtin_expect (buffer_malloced, 0))
1164 if (info->left && width > 0)
1165 PADN (info->pad, width);
1170 /* Return the number of extra grouping characters that will be inserted
1171 into a number with INTDIG_MAX integer digits. */
1174 guess_grouping (unsigned int intdig_max, const char *grouping)
1176 unsigned int groups;
1178 /* We treat all negative values like CHAR_MAX. */
1180 if (*grouping == CHAR_MAX || *grouping <= 0)
1181 /* No grouping should be done. */
1185 while (intdig_max > (unsigned int) *grouping)
1188 intdig_max -= *grouping++;
1192 /* Same grouping repeats. */
1193 groups += (intdig_max - 1) / grouping[-1];
1196 else if (*grouping == CHAR_MAX || *grouping <= 0)
1197 /* No more grouping should be done. */
1204 /* Group the INTDIG_NO integer digits of the number in [BUF,BUFEND).
1205 There is guaranteed enough space past BUFEND to extend it.
1206 Return the new end of buffer. */
1209 group_number (wchar_t *buf, wchar_t *bufend, unsigned int intdig_no,
1210 const char *grouping, wchar_t thousands_sep, int ngroups)
1217 /* Move the fractional part down. */
1218 memmove (buf + intdig_no + ngroups, buf + intdig_no,
1219 (bufend - (buf + intdig_no)) * sizeof (wchar_t));
1221 p = buf + intdig_no + ngroups - 1;
1224 unsigned int len = *grouping++;
1226 *p-- = buf[--intdig_no];
1228 *p-- = thousands_sep;
1231 /* Same grouping repeats. */
1233 else if (*grouping == CHAR_MAX || *grouping <= 0)
1234 /* No more grouping should be done. */
1236 } while (intdig_no > (unsigned int) *grouping);
1238 /* Copy the remaining ungrouped digits. */
1240 *p-- = buf[--intdig_no];
1243 return bufend + ngroups;