1 /* Floating point output for `printf'.
2 Copyright (C) 1995, 1996, 1997 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Written by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1995.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public
17 License along with the GNU C Library; see the file COPYING.LIB. If not,
18 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 /* The gmp headers need some configuration frobs. */
32 #include <gmp-mparam.h>
33 #include "../stdlib/gmp.h"
34 #include "../stdlib/gmp-impl.h"
35 #include "../stdlib/longlong.h"
36 #include "../stdlib/fpioconst.h"
37 #include "../locale/localeinfo.h"
45 #define NDEBUG /* Undefine this for debugging assertions. */
48 /* This defines make it possible to use the same code for GNU C library and
49 the GNU I/O library. */
51 # define PUT(f, s, n) _IO_sputn (f, s, n)
52 # define PAD(f, c, n) _IO_padn (f, c, n)
53 /* We use this file GNU C library and GNU I/O library. So make
56 # define putc(c, f) _IO_putc_unlocked (c, f)
57 # define size_t _IO_size_t
58 # define FILE _IO_FILE
59 #else /* ! USE_IN_LIBIO */
60 # define PUT(f, s, n) fwrite (s, 1, n, f)
61 # define PAD(f, c, n) __printf_pad (f, c, n)
62 ssize_t __printf_pad __P ((FILE *, char pad, int n)); /* In vfprintf.c. */
63 #endif /* USE_IN_LIBIO */
65 /* Macros for doing the actual output. */
70 register const int outc = (ch); \
71 if (putc (outc, fp) == EOF) \
76 #define PRINT(ptr, len) \
79 register size_t outlen = (len); \
82 if (PUT (fp, ptr, outlen) != outlen) \
89 while (outlen-- > 0) \
94 #define PADN(ch, len) \
97 if (PAD (fp, ch, len) != len) \
103 /* We use the GNU MP library to handle large numbers.
105 An MP variable occupies a varying number of entries in its array. We keep
106 track of this number for efficiency reasons. Otherwise we would always
107 have to process the whole array. */
108 #define MPN_VAR(name) mp_limb_t *name; mp_size_t name##size
110 #define MPN_ASSIGN(dst,src) \
111 memcpy (dst, src, (dst##size = src##size) * sizeof (mp_limb_t))
112 #define MPN_GE(u,v) \
113 (u##size > v##size || (u##size == v##size && __mpn_cmp (u, v, u##size) >= 0))
115 extern int __isinfl (long double), __isnanl (long double);
117 extern mp_size_t __mpn_extract_double (mp_ptr res_ptr, mp_size_t size,
118 int *expt, int *is_neg,
120 extern mp_size_t __mpn_extract_long_double (mp_ptr res_ptr, mp_size_t size,
121 int *expt, int *is_neg,
123 extern unsigned int __guess_grouping (unsigned int intdig_max,
124 const char *grouping, wchar_t sepchar);
127 static char *group_number (char *buf, char *bufend, unsigned int intdig_no,
128 const char *grouping, wchar_t thousands_sep);
132 __printf_fp (FILE *fp,
133 const struct printf_info *info,
134 const void *const *args)
136 /* The floating-point value to output. */
140 __long_double_t ldbl;
144 /* Locale-dependent representation of decimal point. */
147 /* Locale-dependent thousands separator and grouping specification. */
148 wchar_t thousands_sep;
149 const char *grouping;
151 /* "NaN" or "Inf" for the special cases. */
152 const char *special = NULL;
154 /* We need just a few limbs for the input before shifting to the right
156 mp_limb_t fp_input[(LDBL_MANT_DIG + BITS_PER_MP_LIMB - 1) / BITS_PER_MP_LIMB];
157 /* We need to shift the contents of fp_input by this amount of bits. */
160 /* The fraction of the floting-point value in question */
162 /* and the exponent. */
164 /* Sign of the exponent. */
166 /* Sign of float number. */
169 /* Scaling factor. */
172 /* Temporary bignum value. */
175 /* Digit which is result of last hack_digit() call. */
178 /* The type of output format that will be used: 'e'/'E' or 'f'. */
181 /* Counter for number of written characters. */
184 /* General helper (carry limb). */
187 char hack_digit (void)
191 if (expsign != 0 && type == 'f' && exponent-- > 0)
193 else if (scalesize == 0)
195 hi = frac[fracsize - 1];
196 cy = __mpn_mul_1 (frac, frac, fracsize - 1, 10);
197 frac[fracsize - 1] = cy;
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 cy = __mpn_mul_1 (frac, frac, fracsize, 10);
223 frac[fracsize++] = cy;
230 /* Figure out the decimal point character. */
231 if (info->extra == 0)
233 if (mbtowc (&decimal, _NL_CURRENT (LC_NUMERIC, DECIMAL_POINT),
234 strlen (_NL_CURRENT (LC_NUMERIC, DECIMAL_POINT))) <= 0)
235 decimal = (wchar_t) *_NL_CURRENT (LC_NUMERIC, DECIMAL_POINT);
239 if (mbtowc (&decimal, _NL_CURRENT (LC_MONETARY, MON_DECIMAL_POINT),
240 strlen (_NL_CURRENT (LC_MONETARY, MON_DECIMAL_POINT))) <= 0)
241 decimal = (wchar_t) *_NL_CURRENT (LC_MONETARY, MON_DECIMAL_POINT);
243 /* Give default value. */
244 if (decimal == L'\0')
250 if (info->extra == 0)
251 grouping = _NL_CURRENT (LC_NUMERIC, GROUPING);
253 grouping = _NL_CURRENT (LC_MONETARY, MON_GROUPING);
255 if (*grouping <= 0 || *grouping == CHAR_MAX)
259 /* Figure out the thousands separator character. */
260 if (info->extra == 0)
262 if (mbtowc (&thousands_sep, _NL_CURRENT (LC_NUMERIC,
264 strlen (_NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP)))
266 thousands_sep = (wchar_t) *_NL_CURRENT (LC_NUMERIC,
271 if (mbtowc (&thousands_sep, _NL_CURRENT (LC_MONETARY,
273 strlen (_NL_CURRENT (LC_MONETARY,
274 MON_THOUSANDS_SEP))) <= 0)
275 thousands_sep = (wchar_t) *_NL_CURRENT (LC_MONETARY,
279 if (thousands_sep == L'\0')
286 /* Fetch the argument value. */
287 if (info->is_long_double && sizeof (long double) > sizeof (double))
289 fpnum.ldbl = *(const long double *) args[0];
291 /* Check for special values: not a number or infinity. */
292 if (__isnanl (fpnum.ldbl))
294 special = isupper (info->spec) ? "NAN" : "nan";
297 else if (__isinfl (fpnum.ldbl))
299 special = isupper (info->spec) ? "INF" : "inf";
300 is_neg = fpnum.ldbl < 0;
304 fracsize = __mpn_extract_long_double (fp_input,
306 sizeof (fp_input[0])),
309 to_shift = 1 + fracsize * BITS_PER_MP_LIMB - LDBL_MANT_DIG;
314 fpnum.dbl = *(const double *) args[0];
316 /* Check for special values: not a number or infinity. */
317 if (__isnan (fpnum.dbl))
319 special = isupper (info->spec) ? "NAN" : "nan";
322 else if (__isinf (fpnum.dbl))
324 special = isupper (info->spec) ? "INF" : "inf";
325 is_neg = fpnum.dbl < 0;
329 fracsize = __mpn_extract_double (fp_input,
331 / sizeof (fp_input[0])),
332 &exponent, &is_neg, fpnum.dbl);
333 to_shift = 1 + fracsize * BITS_PER_MP_LIMB - DBL_MANT_DIG;
339 int width = info->prec > info->width ? info->prec : info->width;
341 if (is_neg || info->showsign || info->space)
345 if (!info->left && width > 0)
350 else if (info->showsign)
352 else if (info->space)
357 if (info->left && width > 0)
364 /* We need three multiprecision variables. Now that we have the exponent
365 of the number we can allocate the needed memory. It would be more
366 efficient to use variables of the fixed maximum size but because this
367 would be really big it could lead to memory problems. */
369 mp_size_t bignum_size = ((ABS (exponent) + BITS_PER_MP_LIMB - 1)
370 / BITS_PER_MP_LIMB + 4) * sizeof (mp_limb_t);
371 frac = (mp_limb_t *) alloca (bignum_size);
372 tmp = (mp_limb_t *) alloca (bignum_size);
373 scale = (mp_limb_t *) alloca (bignum_size);
376 /* We now have to distinguish between numbers with positive and negative
377 exponents because the method used for the one is not applicable/efficient
384 int explog = LDBL_MAX_10_EXP_LOG;
386 const struct mp_power *tens = &_fpioconst_pow10[explog + 1];
389 if ((exponent + to_shift) % BITS_PER_MP_LIMB == 0)
391 MPN_COPY_DECR (frac + (exponent + to_shift) / BITS_PER_MP_LIMB,
393 fracsize += (exponent + to_shift) / BITS_PER_MP_LIMB;
397 cy = __mpn_lshift (frac + (exponent + to_shift) / BITS_PER_MP_LIMB,
399 (exponent + to_shift) % BITS_PER_MP_LIMB);
400 fracsize += (exponent + to_shift) / BITS_PER_MP_LIMB;
402 frac[fracsize++] = cy;
404 MPN_ZERO (frac, (exponent + to_shift) / BITS_PER_MP_LIMB);
406 assert (tens > &_fpioconst_pow10[0]);
411 /* The number of the product of two binary numbers with n and m
412 bits respectively has m+n or m+n-1 bits. */
413 if (exponent >= scaleexpo + tens->p_expo - 1)
416 MPN_ASSIGN (tmp, tens->array);
419 cy = __mpn_mul (tmp, scale, scalesize,
420 &tens->array[_FPIO_CONST_OFFSET],
421 tens->arraysize - _FPIO_CONST_OFFSET);
422 tmpsize = scalesize + tens->arraysize - _FPIO_CONST_OFFSET;
427 if (MPN_GE (frac, tmp))
430 MPN_ASSIGN (scale, tmp);
431 count_leading_zeros (cnt, scale[scalesize - 1]);
432 scaleexpo = (scalesize - 2) * BITS_PER_MP_LIMB - cnt - 1;
433 exp10 |= 1 << explog;
438 while (tens > &_fpioconst_pow10[0]);
441 /* Optimize number representations. We want to represent the numbers
442 with the lowest number of bytes possible without losing any
443 bytes. Also the highest bit in the scaling factor has to be set
444 (this is a requirement of the MPN division routines). */
447 /* Determine minimum number of zero bits at the end of
449 for (i = 0; scale[i] == 0 && frac[i] == 0; i++)
452 /* Determine number of bits the scaling factor is misplaced. */
453 count_leading_zeros (cnt_h, scale[scalesize - 1]);
457 /* The highest bit of the scaling factor is already set. So
458 we only have to remove the trailing empty limbs. */
461 MPN_COPY_INCR (scale, scale + i, scalesize - i);
463 MPN_COPY_INCR (frac, frac + i, fracsize - i);
471 count_trailing_zeros (cnt_l, scale[i]);
475 count_trailing_zeros (cnt_l2, frac[i]);
481 count_trailing_zeros (cnt_l, frac[i]);
483 /* Now shift the numbers to their optimal position. */
484 if (i == 0 && BITS_PER_MP_LIMB - cnt_h > cnt_l)
486 /* We cannot save any memory. So just roll both numbers
487 so that the scaling factor has its highest bit set. */
489 (void) __mpn_lshift (scale, scale, scalesize, cnt_h);
490 cy = __mpn_lshift (frac, frac, fracsize, cnt_h);
492 frac[fracsize++] = cy;
494 else if (BITS_PER_MP_LIMB - cnt_h <= cnt_l)
496 /* We can save memory by removing the trailing zero limbs
497 and by packing the non-zero limbs which gain another
500 (void) __mpn_rshift (scale, scale + i, scalesize - i,
501 BITS_PER_MP_LIMB - cnt_h);
503 (void) __mpn_rshift (frac, frac + i, fracsize - i,
504 BITS_PER_MP_LIMB - cnt_h);
505 fracsize -= frac[fracsize - i - 1] == 0 ? i + 1 : i;
509 /* We can only save the memory of the limbs which are zero.
510 The non-zero parts occupy the same number of limbs. */
512 (void) __mpn_rshift (scale, scale + (i - 1),
514 BITS_PER_MP_LIMB - cnt_h);
516 (void) __mpn_rshift (frac, frac + (i - 1),
518 BITS_PER_MP_LIMB - cnt_h);
519 fracsize -= frac[fracsize - (i - 1) - 1] == 0 ? i : i - 1;
524 else if (exponent < 0)
528 int explog = LDBL_MAX_10_EXP_LOG;
529 const struct mp_power *tens = &_fpioconst_pow10[explog + 1];
530 mp_size_t used_limbs = fracsize - 1;
532 /* Now shift the input value to its right place. */
533 cy = __mpn_lshift (frac, fp_input, fracsize, to_shift);
534 frac[fracsize++] = cy;
535 assert (cy == 1 || (frac[fracsize - 2] == 0 && frac[0] == 0));
538 exponent = -exponent;
540 assert (tens != &_fpioconst_pow10[0]);
545 if (exponent >= tens->m_expo)
547 int i, incr, cnt_h, cnt_l;
550 /* The __mpn_mul function expects the first argument to be
551 bigger than the second. */
552 if (fracsize < tens->arraysize - _FPIO_CONST_OFFSET)
553 cy = __mpn_mul (tmp, &tens->array[_FPIO_CONST_OFFSET],
554 tens->arraysize - _FPIO_CONST_OFFSET,
557 cy = __mpn_mul (tmp, frac, fracsize,
558 &tens->array[_FPIO_CONST_OFFSET],
559 tens->arraysize - _FPIO_CONST_OFFSET);
560 tmpsize = fracsize + tens->arraysize - _FPIO_CONST_OFFSET;
564 count_leading_zeros (cnt_h, tmp[tmpsize - 1]);
565 incr = (tmpsize - fracsize) * BITS_PER_MP_LIMB
566 + BITS_PER_MP_LIMB - 1 - cnt_h;
568 assert (incr <= tens->p_expo);
570 /* If we increased the exponent by exactly 3 we have to test
571 for overflow. This is done by comparing with 10 shifted
572 to the right position. */
573 if (incr == exponent + 3)
574 if (cnt_h <= BITS_PER_MP_LIMB - 4)
578 = ((mp_limb_t) 10) << (BITS_PER_MP_LIMB - 4 - cnt_h);
582 topval[0] = ((mp_limb_t) 10) << (BITS_PER_MP_LIMB - 4);
584 (void) __mpn_lshift (topval, topval, 2,
585 BITS_PER_MP_LIMB - cnt_h);
588 /* We have to be careful when multiplying the last factor.
589 If the result is greater than 1.0 be have to test it
590 against 10.0. If it is greater or equal to 10.0 the
591 multiplication was not valid. This is because we cannot
592 determine the number of bits in the result in advance. */
593 if (incr < exponent + 3
594 || (incr == exponent + 3 &&
595 (tmp[tmpsize - 1] < topval[1]
596 || (tmp[tmpsize - 1] == topval[1]
597 && tmp[tmpsize - 2] < topval[0]))))
599 /* The factor is right. Adapt binary and decimal
602 exp10 |= 1 << explog;
604 /* If this factor yields a number greater or equal to
605 1.0, we must not shift the non-fractional digits down. */
609 /* Now we optimize the number representation. */
610 for (i = 0; tmp[i] == 0; ++i);
611 if (cnt_h == BITS_PER_MP_LIMB - 1)
613 MPN_COPY (frac, tmp + i, tmpsize - i);
614 fracsize = tmpsize - i;
618 count_trailing_zeros (cnt_l, tmp[i]);
620 /* Now shift the numbers to their optimal position. */
621 if (i == 0 && BITS_PER_MP_LIMB - 1 - cnt_h > cnt_l)
623 /* We cannot save any memory. Just roll the
624 number so that the leading digit is in a
627 cy = __mpn_lshift (frac, tmp, tmpsize, cnt_h + 1);
628 fracsize = tmpsize + 1;
629 frac[fracsize - 1] = cy;
631 else if (BITS_PER_MP_LIMB - 1 - cnt_h <= cnt_l)
633 (void) __mpn_rshift (frac, tmp + i, tmpsize - i,
634 BITS_PER_MP_LIMB - 1 - cnt_h);
635 fracsize = tmpsize - i;
639 /* We can only save the memory of the limbs which
640 are zero. The non-zero parts occupy the same
643 (void) __mpn_rshift (frac, tmp + (i - 1),
645 BITS_PER_MP_LIMB - 1 - cnt_h);
646 fracsize = tmpsize - (i - 1);
649 used_limbs = fracsize - 1;
654 while (tens != &_fpioconst_pow10[1] && exponent > 0);
655 /* All factors but 10^-1 are tested now. */
660 cy = __mpn_mul_1 (tmp, frac, fracsize, 10);
662 assert (cy == 0 || tmp[tmpsize - 1] < 20);
664 count_trailing_zeros (cnt_l, tmp[0]);
665 if (cnt_l < MIN (4, exponent))
667 cy = __mpn_lshift (frac, tmp, tmpsize,
668 BITS_PER_MP_LIMB - MIN (4, exponent));
670 frac[tmpsize++] = cy;
673 (void) __mpn_rshift (frac, tmp, tmpsize, MIN (4, exponent));
676 assert (frac[fracsize - 1] < 10);
682 /* This is a special case. We don't need a factor because the
683 numbers are in the range of 0.0 <= fp < 8.0. We simply
684 shift it to the right place and divide it by 1.0 to get the
685 leading digit. (Of course this division is not really made.) */
686 assert (0 <= exponent && exponent < 3 &&
687 exponent + to_shift < BITS_PER_MP_LIMB);
689 /* Now shift the input value to its right place. */
690 cy = __mpn_lshift (frac, fp_input, fracsize, (exponent + to_shift));
691 frac[fracsize++] = cy;
696 int width = info->width;
697 char *buffer, *startp, *cp;
700 int intdig_max, intdig_no = 0;
701 int fracdig_min, fracdig_max, fracdig_no = 0;
705 if (tolower (info->spec) == 'e')
709 fracdig_min = fracdig_max = info->prec < 0 ? 6 : info->prec;
710 chars_needed = 1 + 1 + fracdig_max + 1 + 1 + 4;
711 /* d . ddd e +- ddd */
712 dig_max = INT_MAX; /* Unlimited. */
713 significant = 1; /* Does not matter here. */
715 else if (info->spec == 'f')
718 fracdig_min = fracdig_max = info->prec < 0 ? 6 : info->prec;
721 intdig_max = exponent + 1;
722 /* This can be really big! */ /* XXX Maybe malloc if too big? */
723 chars_needed = exponent + 1 + 1 + fracdig_max;
728 chars_needed = 1 + 1 + fracdig_max;
730 dig_max = INT_MAX; /* Unlimited. */
731 significant = 1; /* Does not matter here. */
735 dig_max = info->prec < 0 ? 6 : (info->prec == 0 ? 1 : info->prec);
736 if ((expsign == 0 && exponent >= dig_max)
737 || (expsign != 0 && exponent > 4))
739 type = isupper (info->spec) ? 'E' : 'e';
740 fracdig_max = dig_max - 1;
742 chars_needed = 1 + 1 + fracdig_max + 1 + 1 + 4;
747 intdig_max = expsign == 0 ? exponent + 1 : 0;
748 fracdig_max = dig_max - intdig_max;
749 /* We need space for the significant digits and perhaps for
750 leading zeros when < 1.0. Pessimistic guess: dig_max. */
751 chars_needed = dig_max + dig_max + 1;
753 fracdig_min = info->alt ? fracdig_max : 0;
754 significant = 0; /* We count significant digits. */
758 /* Guess the number of groups we will make, and thus how
759 many spaces we need for separator characters. */
760 chars_needed += __guess_grouping (intdig_max, grouping, thousands_sep);
762 /* Allocate buffer for output. We need two more because while rounding
763 it is possible that we need two more characters in front of all the
765 buffer = alloca (2 + chars_needed);
766 cp = startp = buffer + 2; /* Let room for rounding. */
768 /* Do the real work: put digits in allocated buffer. */
769 if (expsign == 0 || type != 'f')
771 assert (expsign == 0 || intdig_max == 1);
772 while (intdig_no < intdig_max)
775 *cp++ = hack_digit ();
780 || (fracdig_max > 0 && (fracsize > 1 || frac[0] != 0)))
785 /* |fp| < 1.0 and the selected type is 'f', so put "0."
792 /* Generate the needed number of fractional digits. */
793 while (fracdig_no < fracdig_min
794 || (fracdig_no < fracdig_max && (fracsize > 1 || frac[0] != 0)))
800 else if (significant == 0)
810 digit = hack_digit ();
816 /* This is the critical case. */
817 if (fracsize == 1 && frac[0] == 0)
818 /* Rest of the number is zero -> round to even.
819 (IEEE 754-1985 4.1 says this is the default rounding.) */
820 if ((*(cp - 1) & 1) == 0)
825 /* Process fractional digits. Terminate if not rounded or
826 radix character is reached. */
827 while (*--tp != decimal && *tp == '9')
834 if (fracdig_no == 0 || *tp == decimal)
836 /* Round the integer digits. */
837 if (*(tp - 1) == decimal)
840 while (--tp >= startp && *tp == '9')
847 /* It is more critical. All digits were 9's. */
852 exponent += expsign == 0 ? 1 : -1;
854 else if (intdig_no == dig_max)
856 /* This is the case where for type %g the number fits
857 really in the range for %f output but after rounding
858 the number of digits is too big. */
862 if (info->alt || fracdig_no > 0)
864 /* Overwrite the old radix character. */
865 startp[intdig_no + 2] = '0';
869 fracdig_no += intdig_no;
871 fracdig_max = intdig_max - intdig_no;
873 /* Now we must print the exponent. */
874 type = isupper (info->spec) ? 'E' : 'e';
878 /* We can simply add another another digit before the
884 /* While rounding the number of digits can change.
885 If the number now exceeds the limits remove some
886 fractional digits. */
887 if (intdig_no + fracdig_no > dig_max)
889 cp -= intdig_no + fracdig_no - dig_max;
890 fracdig_no -= intdig_no + fracdig_no - dig_max;
897 /* Now remove unnecessary '0' at the end of the string. */
898 while (fracdig_no > fracdig_min && *(cp - 1) == '0')
903 /* If we eliminate all fractional digits we perhaps also can remove
904 the radix character. */
905 if (fracdig_no == 0 && !info->alt && *(cp - 1) == decimal)
909 /* Add in separator characters, overwriting the same buffer. */
910 cp = group_number (startp, cp, intdig_no, grouping, thousands_sep);
912 /* Write the exponent if it is needed. */
916 *cp++ = expsign ? '-' : '+';
918 /* Find the magnitude of the exponent. */
920 while (expscale <= exponent)
924 /* Exponent always has at least two digits. */
930 *cp++ = '0' + (exponent / expscale);
931 exponent %= expscale;
933 while (expscale > 10);
934 *cp++ = '0' + exponent;
937 /* Compute number of characters which must be filled with the padding
939 if (is_neg || info->showsign || info->space)
941 width -= cp - startp;
943 if (!info->left && info->pad != '0' && width > 0)
944 PADN (info->pad, width);
948 else if (info->showsign)
950 else if (info->space)
953 if (!info->left && info->pad == '0' && width > 0)
956 PRINT (startp, cp - startp);
958 if (info->left && width > 0)
959 PADN (info->pad, width);
964 /* Return the number of extra grouping characters that will be inserted
965 into a number with INTDIG_MAX integer digits. */
968 __guess_grouping (unsigned int intdig_max, const char *grouping,
973 /* We treat all negative values like CHAR_MAX. */
975 if (*grouping == CHAR_MAX || *grouping <= 0)
976 /* No grouping should be done. */
980 while (intdig_max > (unsigned int) *grouping)
983 intdig_max -= *grouping++;
985 if (*grouping == CHAR_MAX || *grouping < 0)
986 /* No more grouping should be done. */
988 else if (*grouping == 0)
990 /* Same grouping repeats. */
991 groups += (intdig_max - 1) / grouping[-1];
999 /* Group the INTDIG_NO integer digits of the number in [BUF,BUFEND).
1000 There is guaranteed enough space past BUFEND to extend it.
1001 Return the new end of buffer. */
1004 group_number (char *buf, char *bufend, unsigned int intdig_no,
1005 const char *grouping, wchar_t thousands_sep)
1007 unsigned int groups = __guess_grouping (intdig_no, grouping, thousands_sep);
1013 /* Move the fractional part down. */
1014 memmove (buf + intdig_no + groups, buf + intdig_no,
1015 bufend - (buf + intdig_no));
1017 p = buf + intdig_no + groups - 1;
1020 unsigned int len = *grouping++;
1022 *p-- = buf[--intdig_no];
1024 *p-- = thousands_sep;
1026 if (*grouping == CHAR_MAX || *grouping < 0)
1027 /* No more grouping should be done. */
1029 else if (*grouping == 0)
1030 /* Same grouping repeats. */
1032 } while (intdig_no > (unsigned int) *grouping);
1034 /* Copy the remaining ungrouped digits. */
1036 *p-- = buf[--intdig_no];
1039 return bufend + groups;