]> git.ipfire.org Git - thirdparty/glibc.git/blame - stdio-common/printf_fp.c
Use glibc_likely instead __builtin_expect.
[thirdparty/glibc.git] / stdio-common / printf_fp.c
CommitLineData
28f540f4 1/* Floating point output for `printf'.
d4697bc9 2 Copyright (C) 1995-2014 Free Software Foundation, Inc.
4c02bf1a 3
feb3c934
UD
4 This file is part of the GNU C Library.
5 Written by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1995.
6
7 The GNU C Library is free software; you can redistribute it and/or
41bdb6e2
AJ
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.
feb3c934
UD
11
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
41bdb6e2 15 Lesser General Public License for more details.
feb3c934 16
41bdb6e2 17 You should have received a copy of the GNU Lesser General Public
59ba27a6
PE
18 License along with the GNU C Library; if not, see
19 <http://www.gnu.org/licenses/>. */
28f540f4 20
3f92886a
RM
21/* The gmp headers need some configuration frobs. */
22#define HAVE_ALLOCA 1
23
8a7455e7 24#include <libioP.h>
28f540f4 25#include <alloca.h>
28f540f4
RM
26#include <ctype.h>
27#include <float.h>
28#include <gmp-mparam.h>
48896b9d 29#include <gmp.h>
003c9895 30#include <ieee754.h>
8f2ece69
UD
31#include <stdlib/gmp-impl.h>
32#include <stdlib/longlong.h>
33#include <stdlib/fpioconst.h>
34#include <locale/localeinfo.h>
933e73fa 35#include <limits.h>
28f540f4
RM
36#include <math.h>
37#include <printf.h>
28f540f4
RM
38#include <string.h>
39#include <unistd.h>
40#include <stdlib.h>
8d8c6efa 41#include <wchar.h>
784761be
JM
42#include <stdbool.h>
43#include <rounding-mode.h>
28f540f4 44
8a7455e7
UD
45#ifdef COMPILE_WPRINTF
46# define CHAR_T wchar_t
47#else
48# define CHAR_T char
49#endif
50
51#include "_i18n_number.h"
52
6973fc01
UD
53#ifndef NDEBUG
54# define NDEBUG /* Undefine this for debugging assertions. */
55#endif
28f540f4
RM
56#include <assert.h>
57
58/* This defines make it possible to use the same code for GNU C library and
59 the GNU I/O library. */
8a7455e7 60#define PUT(f, s, n) _IO_sputn (f, s, n)
d18ea0c5 61#define PAD(f, c, n) (wide ? _IO_wpadn (f, c, n) : _IO_padn (f, c, n))
28f540f4
RM
62/* We use this file GNU C library and GNU I/O library. So make
63 names equal. */
8a7455e7
UD
64#undef putc
65#define putc(c, f) (wide \
66 ? (int)_IO_putwc_unlocked (c, f) : _IO_putc_unlocked (c, f))
67#define size_t _IO_size_t
68#define FILE _IO_FILE
28f540f4
RM
69\f
70/* Macros for doing the actual output. */
71
72#define outchar(ch) \
73 do \
74 { \
2e09a79a 75 const int outc = (ch); \
28f540f4 76 if (putc (outc, fp) == EOF) \
c7df983c
UD
77 { \
78 if (buffer_malloced) \
79 free (wbuffer); \
80 return -1; \
81 } \
28f540f4
RM
82 ++done; \
83 } while (0)
84
a1d84548 85#define PRINT(ptr, wptr, len) \
28f540f4
RM
86 do \
87 { \
2e09a79a 88 size_t outlen = (len); \
28f540f4
RM
89 if (len > 20) \
90 { \
a1d84548 91 if (PUT (fp, wide ? (const char *) wptr : ptr, outlen) != outlen) \
c7df983c
UD
92 { \
93 if (buffer_malloced) \
94 free (wbuffer); \
95 return -1; \
96 } \
28f540f4
RM
97 ptr += outlen; \
98 done += outlen; \
99 } \
100 else \
101 { \
a1d84548
UD
102 if (wide) \
103 while (outlen-- > 0) \
104 outchar (*wptr++); \
105 else \
106 while (outlen-- > 0) \
107 outchar (*ptr++); \
28f540f4
RM
108 } \
109 } while (0)
110
111#define PADN(ch, len) \
112 do \
113 { \
114 if (PAD (fp, ch, len) != len) \
c7df983c
UD
115 { \
116 if (buffer_malloced) \
117 free (wbuffer); \
118 return -1; \
119 } \
28f540f4
RM
120 done += len; \
121 } \
122 while (0)
123\f
124/* We use the GNU MP library to handle large numbers.
125
126 An MP variable occupies a varying number of entries in its array. We keep
127 track of this number for efficiency reasons. Otherwise we would always
128 have to process the whole array. */
0e3426bb 129#define MPN_VAR(name) mp_limb_t *name; mp_size_t name##size
28f540f4
RM
130
131#define MPN_ASSIGN(dst,src) \
0e3426bb 132 memcpy (dst, src, (dst##size = src##size) * sizeof (mp_limb_t))
28f540f4
RM
133#define MPN_GE(u,v) \
134 (u##size > v##size || (u##size == v##size && __mpn_cmp (u, v, u##size) >= 0))
135
28f540f4
RM
136extern mp_size_t __mpn_extract_double (mp_ptr res_ptr, mp_size_t size,
137 int *expt, int *is_neg,
138 double value);
139extern mp_size_t __mpn_extract_long_double (mp_ptr res_ptr, mp_size_t size,
140 int *expt, int *is_neg,
141 long double value);
b8fe19fa 142extern unsigned int __guess_grouping (unsigned int intdig_max,
a1d84548 143 const char *grouping);
28f540f4 144
28f540f4 145
a1d84548
UD
146static wchar_t *group_number (wchar_t *buf, wchar_t *bufend,
147 unsigned int intdig_no, const char *grouping,
148 wchar_t thousands_sep, int ngroups)
dfd2257a 149 internal_function;
28f540f4
RM
150
151
152int
c6251f03
RM
153___printf_fp (FILE *fp,
154 const struct printf_info *info,
155 const void *const *args)
28f540f4
RM
156{
157 /* The floating-point value to output. */
158 union
159 {
160 double dbl;
0e3426bb 161 __long_double_t ldbl;
28f540f4
RM
162 }
163 fpnum;
164
165 /* Locale-dependent representation of decimal point. */
a1d84548
UD
166 const char *decimal;
167 wchar_t decimalwc;
28f540f4
RM
168
169 /* Locale-dependent thousands separator and grouping specification. */
a1d84548
UD
170 const char *thousands_sep = NULL;
171 wchar_t thousands_sepwc = 0;
28f540f4
RM
172 const char *grouping;
173
174 /* "NaN" or "Inf" for the special cases. */
0e3426bb 175 const char *special = NULL;
a1d84548 176 const wchar_t *wspecial = NULL;
28f540f4
RM
177
178 /* We need just a few limbs for the input before shifting to the right
179 position. */
0e3426bb 180 mp_limb_t fp_input[(LDBL_MANT_DIG + BITS_PER_MP_LIMB - 1) / BITS_PER_MP_LIMB];
28f540f4 181 /* We need to shift the contents of fp_input by this amount of bits. */
ba1ffaa1 182 int to_shift = 0;
28f540f4 183
6d52618b 184 /* The fraction of the floting-point value in question */
28f540f4
RM
185 MPN_VAR(frac);
186 /* and the exponent. */
187 int exponent;
188 /* Sign of the exponent. */
189 int expsign = 0;
190 /* Sign of float number. */
191 int is_neg = 0;
192
193 /* Scaling factor. */
194 MPN_VAR(scale);
195
196 /* Temporary bignum value. */
197 MPN_VAR(tmp);
198
28f540f4
RM
199 /* The type of output format that will be used: 'e'/'E' or 'f'. */
200 int type;
201
202 /* Counter for number of written characters. */
203 int done = 0;
204
205 /* General helper (carry limb). */
0e3426bb 206 mp_limb_t cy;
28f540f4 207
d64b6ad0
UD
208 /* Nonzero if this is output on a wide character stream. */
209 int wide = info->wide;
210
c7df983c
UD
211 /* Buffer in which we produce the output. */
212 wchar_t *wbuffer = NULL;
213 /* Flag whether wbuffer is malloc'ed or not. */
214 int buffer_malloced = 0;
215
938c669e
AJ
216 auto wchar_t hack_digit (void);
217
a1d84548 218 wchar_t hack_digit (void)
28f540f4 219 {
0e3426bb 220 mp_limb_t hi;
28f540f4
RM
221
222 if (expsign != 0 && type == 'f' && exponent-- > 0)
223 hi = 0;
224 else if (scalesize == 0)
225 {
226 hi = frac[fracsize - 1];
a3022b82 227 frac[fracsize - 1] = __mpn_mul_1 (frac, frac, fracsize - 1, 10);
28f540f4
RM
228 }
229 else
230 {
231 if (fracsize < scalesize)
232 hi = 0;
233 else
234 {
53f770e0 235 hi = mpn_divmod (tmp, frac, fracsize, scale, scalesize);
28f540f4
RM
236 tmp[fracsize - scalesize] = hi;
237 hi = tmp[0];
238
1177c8ba
RM
239 fracsize = scalesize;
240 while (fracsize != 0 && frac[fracsize - 1] == 0)
241 --fracsize;
28f540f4
RM
242 if (fracsize == 0)
243 {
244 /* We're not prepared for an mpn variable with zero
245 limbs. */
246 fracsize = 1;
a1d84548 247 return L'0' + hi;
28f540f4
RM
248 }
249 }
250
a3022b82
UD
251 mp_limb_t _cy = __mpn_mul_1 (frac, frac, fracsize, 10);
252 if (_cy != 0)
253 frac[fracsize++] = _cy;
28f540f4
RM
254 }
255
a1d84548 256 return L'0' + hi;
28f540f4
RM
257 }
258
259
260 /* Figure out the decimal point character. */
b8fe19fa
RM
261 if (info->extra == 0)
262 {
a1d84548
UD
263 decimal = _NL_CURRENT (LC_NUMERIC, DECIMAL_POINT);
264 decimalwc = _NL_CURRENT_WORD (LC_NUMERIC, _NL_NUMERIC_DECIMAL_POINT_WC);
b8fe19fa
RM
265 }
266 else
267 {
a1d84548 268 decimal = _NL_CURRENT (LC_MONETARY, MON_DECIMAL_POINT);
d8337213
UD
269 if (*decimal == '\0')
270 decimal = _NL_CURRENT (LC_NUMERIC, DECIMAL_POINT);
a1d84548
UD
271 decimalwc = _NL_CURRENT_WORD (LC_MONETARY,
272 _NL_MONETARY_DECIMAL_POINT_WC);
d8337213
UD
273 if (decimalwc == L'\0')
274 decimalwc = _NL_CURRENT_WORD (LC_NUMERIC,
275 _NL_NUMERIC_DECIMAL_POINT_WC);
b8fe19fa 276 }
a1d84548 277 /* The decimal point character must not be zero. */
d8337213
UD
278 assert (*decimal != '\0');
279 assert (decimalwc != L'\0');
28f540f4
RM
280
281 if (info->group)
282 {
b8fe19fa
RM
283 if (info->extra == 0)
284 grouping = _NL_CURRENT (LC_NUMERIC, GROUPING);
285 else
286 grouping = _NL_CURRENT (LC_MONETARY, MON_GROUPING);
14bab8de 287
28f540f4
RM
288 if (*grouping <= 0 || *grouping == CHAR_MAX)
289 grouping = NULL;
290 else
291 {
6d52618b 292 /* Figure out the thousands separator character. */
a1d84548 293 if (wide)
b8fe19fa 294 {
a1d84548
UD
295 if (info->extra == 0)
296 thousands_sepwc =
297 _NL_CURRENT_WORD (LC_NUMERIC, _NL_NUMERIC_THOUSANDS_SEP_WC);
298 else
299 thousands_sepwc =
300 _NL_CURRENT_WORD (LC_MONETARY,
301 _NL_MONETARY_THOUSANDS_SEP_WC);
b8fe19fa
RM
302 }
303 else
304 {
a1d84548
UD
305 if (info->extra == 0)
306 thousands_sep = _NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP);
307 else
308 thousands_sep = _NL_CURRENT (LC_MONETARY, MON_THOUSANDS_SEP);
b8fe19fa 309 }
14bab8de 310
a1d84548
UD
311 if ((wide && thousands_sepwc == L'\0')
312 || (! wide && *thousands_sep == '\0'))
28f540f4 313 grouping = NULL;
a1d84548
UD
314 else if (thousands_sepwc == L'\0')
315 /* If we are printing multibyte characters and there is a
316 multibyte representation for the thousands separator,
317 we must ensure the wide character thousands separator
318 is available, even if it is fake. */
319 thousands_sepwc = 0xfffffffe;
28f540f4
RM
320 }
321 }
322 else
323 grouping = NULL;
324
325 /* Fetch the argument value. */
f98b4bbd 326#ifndef __NO_LONG_DOUBLE_MATH
28f540f4
RM
327 if (info->is_long_double && sizeof (long double) > sizeof (double))
328 {
f0bf9cb9 329 fpnum.ldbl = *(const long double *) args[0];
28f540f4
RM
330
331 /* Check for special values: not a number or infinity. */
187da0ae 332 int res;
76f2646f 333 if (__isnanl (fpnum.ldbl))
28f540f4 334 {
1b6adf88 335 is_neg = signbit (fpnum.ldbl);
a1d84548
UD
336 if (isupper (info->spec))
337 {
338 special = "NAN";
339 wspecial = L"NAN";
340 }
341 else
342 {
343 special = "nan";
344 wspecial = L"nan";
345 }
28f540f4 346 }
187da0ae 347 else if ((res = __isinfl (fpnum.ldbl)))
28f540f4 348 {
187da0ae 349 is_neg = res < 0;
a1d84548
UD
350 if (isupper (info->spec))
351 {
352 special = "INF";
353 wspecial = L"INF";
354 }
355 else
356 {
357 special = "inf";
358 wspecial = L"inf";
359 }
28f540f4
RM
360 }
361 else
362 {
363 fracsize = __mpn_extract_long_double (fp_input,
364 (sizeof (fp_input) /
96aa2d94 365 sizeof (fp_input[0])),
28f540f4
RM
366 &exponent, &is_neg,
367 fpnum.ldbl);
368 to_shift = 1 + fracsize * BITS_PER_MP_LIMB - LDBL_MANT_DIG;
369 }
370 }
371 else
f98b4bbd 372#endif /* no long double */
28f540f4 373 {
f0bf9cb9 374 fpnum.dbl = *(const double *) args[0];
28f540f4
RM
375
376 /* Check for special values: not a number or infinity. */
187da0ae 377 int res;
76f2646f 378 if (__isnan (fpnum.dbl))
28f540f4 379 {
003c9895
UD
380 union ieee754_double u = { .d = fpnum.dbl };
381 is_neg = u.ieee.negative != 0;
a1d84548
UD
382 if (isupper (info->spec))
383 {
384 special = "NAN";
385 wspecial = L"NAN";
386 }
387 else
388 {
389 special = "nan";
390 wspecial = L"nan";
391 }
28f540f4 392 }
187da0ae 393 else if ((res = __isinf (fpnum.dbl)))
28f540f4 394 {
187da0ae 395 is_neg = res < 0;
a1d84548
UD
396 if (isupper (info->spec))
397 {
398 special = "INF";
399 wspecial = L"INF";
400 }
401 else
402 {
403 special = "inf";
404 wspecial = L"inf";
405 }
28f540f4
RM
406 }
407 else
408 {
409 fracsize = __mpn_extract_double (fp_input,
410 (sizeof (fp_input)
411 / sizeof (fp_input[0])),
412 &exponent, &is_neg, fpnum.dbl);
413 to_shift = 1 + fracsize * BITS_PER_MP_LIMB - DBL_MANT_DIG;
414 }
415 }
416
417 if (special)
418 {
1f205a47 419 int width = info->width;
28f540f4
RM
420
421 if (is_neg || info->showsign || info->space)
422 --width;
423 width -= 3;
424
425 if (!info->left && width > 0)
426 PADN (' ', width);
427
428 if (is_neg)
429 outchar ('-');
430 else if (info->showsign)
431 outchar ('+');
432 else if (info->space)
433 outchar (' ');
434
a1d84548 435 PRINT (special, wspecial, 3);
28f540f4
RM
436
437 if (info->left && width > 0)
438 PADN (' ', width);
439
440 return done;
441 }
442
443
444 /* We need three multiprecision variables. Now that we have the exponent
445 of the number we can allocate the needed memory. It would be more
446 efficient to use variables of the fixed maximum size but because this
447 would be really big it could lead to memory problems. */
448 {
449 mp_size_t bignum_size = ((ABS (exponent) + BITS_PER_MP_LIMB - 1)
9ce0ecbe
UD
450 / BITS_PER_MP_LIMB
451 + (LDBL_MANT_DIG / BITS_PER_MP_LIMB > 2 ? 8 : 4))
452 * sizeof (mp_limb_t);
0e3426bb
RM
453 frac = (mp_limb_t *) alloca (bignum_size);
454 tmp = (mp_limb_t *) alloca (bignum_size);
455 scale = (mp_limb_t *) alloca (bignum_size);
28f540f4
RM
456 }
457
458 /* We now have to distinguish between numbers with positive and negative
459 exponents because the method used for the one is not applicable/efficient
460 for the other. */
461 scalesize = 0;
462 if (exponent > 2)
463 {
77a58cad 464 /* |FP| >= 8.0. */
28f540f4
RM
465 int scaleexpo = 0;
466 int explog = LDBL_MAX_10_EXP_LOG;
467 int exp10 = 0;
c4563d2d 468 const struct mp_power *powers = &_fpioconst_pow10[explog + 1];
28f540f4
RM
469 int cnt_h, cnt_l, i;
470
471 if ((exponent + to_shift) % BITS_PER_MP_LIMB == 0)
472 {
473 MPN_COPY_DECR (frac + (exponent + to_shift) / BITS_PER_MP_LIMB,
474 fp_input, fracsize);
475 fracsize += (exponent + to_shift) / BITS_PER_MP_LIMB;
476 }
477 else
478 {
479 cy = __mpn_lshift (frac + (exponent + to_shift) / BITS_PER_MP_LIMB,
480 fp_input, fracsize,
481 (exponent + to_shift) % BITS_PER_MP_LIMB);
482 fracsize += (exponent + to_shift) / BITS_PER_MP_LIMB;
483 if (cy)
484 frac[fracsize++] = cy;
485 }
486 MPN_ZERO (frac, (exponent + to_shift) / BITS_PER_MP_LIMB);
487
c4563d2d 488 assert (powers > &_fpioconst_pow10[0]);
28f540f4
RM
489 do
490 {
c4563d2d 491 --powers;
28f540f4
RM
492
493 /* The number of the product of two binary numbers with n and m
494 bits respectively has m+n or m+n-1 bits. */
c4563d2d 495 if (exponent >= scaleexpo + powers->p_expo - 1)
28f540f4
RM
496 {
497 if (scalesize == 0)
c4563d2d 498 {
abfbdde1
UD
499#ifndef __NO_LONG_DOUBLE_MATH
500 if (LDBL_MANT_DIG > _FPIO_CONST_OFFSET * BITS_PER_MP_LIMB
501 && info->is_long_double)
502 {
503#define _FPIO_CONST_SHIFT \
504 (((LDBL_MANT_DIG + BITS_PER_MP_LIMB - 1) / BITS_PER_MP_LIMB) \
505 - _FPIO_CONST_OFFSET)
506 /* 64bit const offset is not enough for
507 IEEE quad long double. */
508 tmpsize = powers->arraysize + _FPIO_CONST_SHIFT;
509 memcpy (tmp + _FPIO_CONST_SHIFT,
510 &__tens[powers->arrayoff],
511 tmpsize * sizeof (mp_limb_t));
512 MPN_ZERO (tmp, _FPIO_CONST_SHIFT);
52e1b618
UD
513 /* Adjust exponent, as scaleexpo will be this much
514 bigger too. */
515 exponent += _FPIO_CONST_SHIFT * BITS_PER_MP_LIMB;
abfbdde1
UD
516 }
517 else
518#endif
519 {
520 tmpsize = powers->arraysize;
521 memcpy (tmp, &__tens[powers->arrayoff],
522 tmpsize * sizeof (mp_limb_t));
523 }
c4563d2d 524 }
28f540f4
RM
525 else
526 {
527 cy = __mpn_mul (tmp, scale, scalesize,
c4563d2d
UD
528 &__tens[powers->arrayoff
529 + _FPIO_CONST_OFFSET],
530 powers->arraysize - _FPIO_CONST_OFFSET);
531 tmpsize = scalesize + powers->arraysize - _FPIO_CONST_OFFSET;
28f540f4
RM
532 if (cy == 0)
533 --tmpsize;
534 }
535
536 if (MPN_GE (frac, tmp))
537 {
538 int cnt;
539 MPN_ASSIGN (scale, tmp);
540 count_leading_zeros (cnt, scale[scalesize - 1]);
541 scaleexpo = (scalesize - 2) * BITS_PER_MP_LIMB - cnt - 1;
542 exp10 |= 1 << explog;
543 }
544 }
545 --explog;
546 }
c4563d2d 547 while (powers > &_fpioconst_pow10[0]);
28f540f4
RM
548 exponent = exp10;
549
550 /* Optimize number representations. We want to represent the numbers
551 with the lowest number of bytes possible without losing any
552 bytes. Also the highest bit in the scaling factor has to be set
553 (this is a requirement of the MPN division routines). */
554 if (scalesize > 0)
555 {
556 /* Determine minimum number of zero bits at the end of
557 both numbers. */
558 for (i = 0; scale[i] == 0 && frac[i] == 0; i++)
559 ;
560
561 /* Determine number of bits the scaling factor is misplaced. */
562 count_leading_zeros (cnt_h, scale[scalesize - 1]);
563
564 if (cnt_h == 0)
565 {
566 /* The highest bit of the scaling factor is already set. So
567 we only have to remove the trailing empty limbs. */
568 if (i > 0)
569 {
570 MPN_COPY_INCR (scale, scale + i, scalesize - i);
571 scalesize -= i;
572 MPN_COPY_INCR (frac, frac + i, fracsize - i);
573 fracsize -= i;
574 }
575 }
576 else
577 {
578 if (scale[i] != 0)
579 {
580 count_trailing_zeros (cnt_l, scale[i]);
581 if (frac[i] != 0)
582 {
583 int cnt_l2;
584 count_trailing_zeros (cnt_l2, frac[i]);
585 if (cnt_l2 < cnt_l)
586 cnt_l = cnt_l2;
587 }
588 }
589 else
590 count_trailing_zeros (cnt_l, frac[i]);
591
592 /* Now shift the numbers to their optimal position. */
593 if (i == 0 && BITS_PER_MP_LIMB - cnt_h > cnt_l)
594 {
595 /* We cannot save any memory. So just roll both numbers
596 so that the scaling factor has its highest bit set. */
597
598 (void) __mpn_lshift (scale, scale, scalesize, cnt_h);
599 cy = __mpn_lshift (frac, frac, fracsize, cnt_h);
600 if (cy != 0)
601 frac[fracsize++] = cy;
602 }
603 else if (BITS_PER_MP_LIMB - cnt_h <= cnt_l)
604 {
605 /* We can save memory by removing the trailing zero limbs
606 and by packing the non-zero limbs which gain another
607 free one. */
608
609 (void) __mpn_rshift (scale, scale + i, scalesize - i,
610 BITS_PER_MP_LIMB - cnt_h);
611 scalesize -= i + 1;
612 (void) __mpn_rshift (frac, frac + i, fracsize - i,
613 BITS_PER_MP_LIMB - cnt_h);
614 fracsize -= frac[fracsize - i - 1] == 0 ? i + 1 : i;
615 }
616 else
617 {
618 /* We can only save the memory of the limbs which are zero.
619 The non-zero parts occupy the same number of limbs. */
620
621 (void) __mpn_rshift (scale, scale + (i - 1),
622 scalesize - (i - 1),
623 BITS_PER_MP_LIMB - cnt_h);
624 scalesize -= i;
625 (void) __mpn_rshift (frac, frac + (i - 1),
626 fracsize - (i - 1),
627 BITS_PER_MP_LIMB - cnt_h);
628 fracsize -= frac[fracsize - (i - 1) - 1] == 0 ? i : i - 1;
629 }
630 }
631 }
632 }
633 else if (exponent < 0)
634 {
635 /* |FP| < 1.0. */
636 int exp10 = 0;
637 int explog = LDBL_MAX_10_EXP_LOG;
c4563d2d 638 const struct mp_power *powers = &_fpioconst_pow10[explog + 1];
28f540f4
RM
639
640 /* Now shift the input value to its right place. */
641 cy = __mpn_lshift (frac, fp_input, fracsize, to_shift);
96aa2d94 642 frac[fracsize++] = cy;
28f540f4
RM
643 assert (cy == 1 || (frac[fracsize - 2] == 0 && frac[0] == 0));
644
645 expsign = 1;
646 exponent = -exponent;
647
c4563d2d 648 assert (powers != &_fpioconst_pow10[0]);
28f540f4
RM
649 do
650 {
c4563d2d 651 --powers;
28f540f4 652
c4563d2d 653 if (exponent >= powers->m_expo)
28f540f4
RM
654 {
655 int i, incr, cnt_h, cnt_l;
0e3426bb 656 mp_limb_t topval[2];
28f540f4
RM
657
658 /* The __mpn_mul function expects the first argument to be
659 bigger than the second. */
c4563d2d
UD
660 if (fracsize < powers->arraysize - _FPIO_CONST_OFFSET)
661 cy = __mpn_mul (tmp, &__tens[powers->arrayoff
662 + _FPIO_CONST_OFFSET],
663 powers->arraysize - _FPIO_CONST_OFFSET,
28f540f4
RM
664 frac, fracsize);
665 else
666 cy = __mpn_mul (tmp, frac, fracsize,
c4563d2d
UD
667 &__tens[powers->arrayoff + _FPIO_CONST_OFFSET],
668 powers->arraysize - _FPIO_CONST_OFFSET);
669 tmpsize = fracsize + powers->arraysize - _FPIO_CONST_OFFSET;
28f540f4
RM
670 if (cy == 0)
671 --tmpsize;
672
96aa2d94 673 count_leading_zeros (cnt_h, tmp[tmpsize - 1]);
28f540f4
RM
674 incr = (tmpsize - fracsize) * BITS_PER_MP_LIMB
675 + BITS_PER_MP_LIMB - 1 - cnt_h;
676
c4563d2d 677 assert (incr <= powers->p_expo);
28f540f4
RM
678
679 /* If we increased the exponent by exactly 3 we have to test
680 for overflow. This is done by comparing with 10 shifted
681 to the right position. */
682 if (incr == exponent + 3)
6e4c40ba
UD
683 {
684 if (cnt_h <= BITS_PER_MP_LIMB - 4)
685 {
686 topval[0] = 0;
687 topval[1]
688 = ((mp_limb_t) 10) << (BITS_PER_MP_LIMB - 4 - cnt_h);
689 }
690 else
691 {
692 topval[0] = ((mp_limb_t) 10) << (BITS_PER_MP_LIMB - 4);
693 topval[1] = 0;
694 (void) __mpn_lshift (topval, topval, 2,
695 BITS_PER_MP_LIMB - cnt_h);
696 }
697 }
28f540f4
RM
698
699 /* We have to be careful when multiplying the last factor.
700 If the result is greater than 1.0 be have to test it
701 against 10.0. If it is greater or equal to 10.0 the
702 multiplication was not valid. This is because we cannot
703 determine the number of bits in the result in advance. */
704 if (incr < exponent + 3
705 || (incr == exponent + 3 &&
706 (tmp[tmpsize - 1] < topval[1]
707 || (tmp[tmpsize - 1] == topval[1]
708 && tmp[tmpsize - 2] < topval[0]))))
709 {
710 /* The factor is right. Adapt binary and decimal
96aa2d94 711 exponents. */
28f540f4
RM
712 exponent -= incr;
713 exp10 |= 1 << explog;
714
715 /* If this factor yields a number greater or equal to
716 1.0, we must not shift the non-fractional digits down. */
717 if (exponent < 0)
718 cnt_h += -exponent;
719
720 /* Now we optimize the number representation. */
721 for (i = 0; tmp[i] == 0; ++i);
722 if (cnt_h == BITS_PER_MP_LIMB - 1)
723 {
724 MPN_COPY (frac, tmp + i, tmpsize - i);
725 fracsize = tmpsize - i;
726 }
727 else
728 {
729 count_trailing_zeros (cnt_l, tmp[i]);
730
731 /* Now shift the numbers to their optimal position. */
732 if (i == 0 && BITS_PER_MP_LIMB - 1 - cnt_h > cnt_l)
733 {
734 /* We cannot save any memory. Just roll the
735 number so that the leading digit is in a
6d52618b 736 separate limb. */
28f540f4
RM
737
738 cy = __mpn_lshift (frac, tmp, tmpsize, cnt_h + 1);
739 fracsize = tmpsize + 1;
740 frac[fracsize - 1] = cy;
741 }
742 else if (BITS_PER_MP_LIMB - 1 - cnt_h <= cnt_l)
743 {
744 (void) __mpn_rshift (frac, tmp + i, tmpsize - i,
745 BITS_PER_MP_LIMB - 1 - cnt_h);
746 fracsize = tmpsize - i;
747 }
748 else
749 {
750 /* We can only save the memory of the limbs which
751 are zero. The non-zero parts occupy the same
752 number of limbs. */
753
754 (void) __mpn_rshift (frac, tmp + (i - 1),
755 tmpsize - (i - 1),
756 BITS_PER_MP_LIMB - 1 - cnt_h);
757 fracsize = tmpsize - (i - 1);
758 }
759 }
28f540f4
RM
760 }
761 }
762 --explog;
763 }
c4563d2d 764 while (powers != &_fpioconst_pow10[1] && exponent > 0);
28f540f4
RM
765 /* All factors but 10^-1 are tested now. */
766 if (exponent > 0)
767 {
19bc17a9
RM
768 int cnt_l;
769
28f540f4
RM
770 cy = __mpn_mul_1 (tmp, frac, fracsize, 10);
771 tmpsize = fracsize;
772 assert (cy == 0 || tmp[tmpsize - 1] < 20);
773
19bc17a9
RM
774 count_trailing_zeros (cnt_l, tmp[0]);
775 if (cnt_l < MIN (4, exponent))
776 {
777 cy = __mpn_lshift (frac, tmp, tmpsize,
778 BITS_PER_MP_LIMB - MIN (4, exponent));
779 if (cy != 0)
780 frac[tmpsize++] = cy;
781 }
782 else
783 (void) __mpn_rshift (frac, tmp, tmpsize, MIN (4, exponent));
28f540f4
RM
784 fracsize = tmpsize;
785 exp10 |= 1;
786 assert (frac[fracsize - 1] < 10);
787 }
788 exponent = exp10;
789 }
790 else
791 {
792 /* This is a special case. We don't need a factor because the
b866373d 793 numbers are in the range of 1.0 <= |fp| < 8.0. We simply
28f540f4
RM
794 shift it to the right place and divide it by 1.0 to get the
795 leading digit. (Of course this division is not really made.) */
796 assert (0 <= exponent && exponent < 3 &&
797 exponent + to_shift < BITS_PER_MP_LIMB);
798
799 /* Now shift the input value to its right place. */
800 cy = __mpn_lshift (frac, fp_input, fracsize, (exponent + to_shift));
96aa2d94 801 frac[fracsize++] = cy;
28f540f4
RM
802 exponent = 0;
803 }
804
805 {
806 int width = info->width;
c7df983c 807 wchar_t *wstartp, *wcp;
9ca230d6 808 size_t chars_needed;
28f540f4
RM
809 int expscale;
810 int intdig_max, intdig_no = 0;
4c02bf1a
UD
811 int fracdig_min;
812 int fracdig_max;
28f540f4
RM
813 int dig_max;
814 int significant;
a1d84548 815 int ngroups = 0;
4c02bf1a 816 char spec = _tolower (info->spec);
28f540f4 817
4c02bf1a 818 if (spec == 'e')
28f540f4
RM
819 {
820 type = info->spec;
821 intdig_max = 1;
822 fracdig_min = fracdig_max = info->prec < 0 ? 6 : info->prec;
9ca230d6 823 chars_needed = 1 + 1 + (size_t) fracdig_max + 1 + 1 + 4;
28f540f4
RM
824 /* d . ddd e +- ddd */
825 dig_max = INT_MAX; /* Unlimited. */
826 significant = 1; /* Does not matter here. */
827 }
4c02bf1a 828 else if (spec == 'f')
28f540f4
RM
829 {
830 type = 'f';
831 fracdig_min = fracdig_max = info->prec < 0 ? 6 : info->prec;
a3022b82
UD
832 dig_max = INT_MAX; /* Unlimited. */
833 significant = 1; /* Does not matter here. */
28f540f4
RM
834 if (expsign == 0)
835 {
836 intdig_max = exponent + 1;
837 /* This can be really big! */ /* XXX Maybe malloc if too big? */
9ca230d6 838 chars_needed = (size_t) exponent + 1 + 1 + (size_t) fracdig_max;
28f540f4
RM
839 }
840 else
841 {
842 intdig_max = 1;
9ca230d6 843 chars_needed = 1 + 1 + (size_t) fracdig_max;
28f540f4 844 }
28f540f4
RM
845 }
846 else
847 {
848 dig_max = info->prec < 0 ? 6 : (info->prec == 0 ? 1 : info->prec);
849 if ((expsign == 0 && exponent >= dig_max)
850 || (expsign != 0 && exponent > 4))
851 {
d64b6ad0
UD
852 if ('g' - 'G' == 'e' - 'E')
853 type = 'E' + (info->spec - 'G');
854 else
855 type = isupper (info->spec) ? 'E' : 'e';
28f540f4
RM
856 fracdig_max = dig_max - 1;
857 intdig_max = 1;
9ca230d6 858 chars_needed = 1 + 1 + (size_t) fracdig_max + 1 + 1 + 4;
28f540f4
RM
859 }
860 else
861 {
862 type = 'f';
863 intdig_max = expsign == 0 ? exponent + 1 : 0;
864 fracdig_max = dig_max - intdig_max;
0f6b172f
UD
865 /* We need space for the significant digits and perhaps
866 for leading zeros when < 1.0. The number of leading
867 zeros can be as many as would be required for
868 exponential notation with a negative two-digit
869 exponent, which is 4. */
9ca230d6 870 chars_needed = (size_t) dig_max + 1 + 4;
28f540f4
RM
871 }
872 fracdig_min = info->alt ? fracdig_max : 0;
873 significant = 0; /* We count significant digits. */
874 }
875
876 if (grouping)
a1d84548
UD
877 {
878 /* Guess the number of groups we will make, and thus how
879 many spaces we need for separator characters. */
880 ngroups = __guess_grouping (intdig_max, grouping);
f57e41a5
UD
881 /* Allocate one more character in case rounding increases the
882 number of groups. */
883 chars_needed += ngroups + 1;
a1d84548 884 }
28f540f4
RM
885
886 /* Allocate buffer for output. We need two more because while rounding
887 it is possible that we need two more characters in front of all the
b526f8ac
UD
888 other output. If the amount of memory we have to allocate is too
889 large use `malloc' instead of `alloca'. */
199eb0de
AS
890 if (__builtin_expect (chars_needed >= (size_t) -1 / sizeof (wchar_t) - 2
891 || chars_needed < fracdig_max, 0))
892 {
893 /* Some overflow occurred. */
894 __set_errno (ERANGE);
895 return -1;
896 }
897 size_t wbuffer_to_alloc = (2 + chars_needed) * sizeof (wchar_t);
898 buffer_malloced = ! __libc_use_alloca (wbuffer_to_alloc);
4c02bf1a 899 if (__builtin_expect (buffer_malloced, 0))
b526f8ac 900 {
9ca230d6 901 wbuffer = (wchar_t *) malloc (wbuffer_to_alloc);
a1d84548 902 if (wbuffer == NULL)
b526f8ac
UD
903 /* Signal an error to the caller. */
904 return -1;
905 }
906 else
9ca230d6 907 wbuffer = (wchar_t *) alloca (wbuffer_to_alloc);
a1d84548 908 wcp = wstartp = wbuffer + 2; /* Let room for rounding. */
28f540f4
RM
909
910 /* Do the real work: put digits in allocated buffer. */
911 if (expsign == 0 || type != 'f')
912 {
913 assert (expsign == 0 || intdig_max == 1);
914 while (intdig_no < intdig_max)
915 {
916 ++intdig_no;
a1d84548 917 *wcp++ = hack_digit ();
28f540f4
RM
918 }
919 significant = 1;
920 if (info->alt
921 || fracdig_min > 0
922 || (fracdig_max > 0 && (fracsize > 1 || frac[0] != 0)))
a1d84548 923 *wcp++ = decimalwc;
28f540f4
RM
924 }
925 else
926 {
927 /* |fp| < 1.0 and the selected type is 'f', so put "0."
928 in the buffer. */
a1d84548 929 *wcp++ = L'0';
28f540f4 930 --exponent;
a1d84548 931 *wcp++ = decimalwc;
28f540f4
RM
932 }
933
934 /* Generate the needed number of fractional digits. */
4c02bf1a 935 int fracdig_no = 0;
0f7769f7
UD
936 int added_zeros = 0;
937 while (fracdig_no < fracdig_min + added_zeros
28f540f4
RM
938 || (fracdig_no < fracdig_max && (fracsize > 1 || frac[0] != 0)))
939 {
940 ++fracdig_no;
a1d84548 941 *wcp = hack_digit ();
a3022b82 942 if (*wcp++ != L'0')
28f540f4
RM
943 significant = 1;
944 else if (significant == 0)
945 {
946 ++fracdig_max;
947 if (fracdig_min > 0)
0f7769f7 948 ++added_zeros;
28f540f4 949 }
28f540f4
RM
950 }
951
952 /* Do rounding. */
784761be
JM
953 wchar_t last_digit = wcp[-1] != decimalwc ? wcp[-1] : wcp[-2];
954 wchar_t next_digit = hack_digit ();
955 bool more_bits;
956 if (next_digit != L'0' && next_digit != L'5')
957 more_bits = true;
958 else if (fracsize == 1 && frac[0] == 0)
959 /* Rest of the number is zero. */
960 more_bits = false;
961 else if (scalesize == 0)
962 {
963 /* Here we have to see whether all limbs are zero since no
964 normalization happened. */
965 size_t lcnt = fracsize;
966 while (lcnt >= 1 && frac[lcnt - 1] == 0)
967 --lcnt;
968 more_bits = lcnt > 0;
969 }
970 else
971 more_bits = true;
972 int rounding_mode = get_rounding_mode ();
973 if (round_away (is_neg, (last_digit - L'0') & 1, next_digit >= L'5',
974 more_bits, rounding_mode))
28f540f4 975 {
a1d84548 976 wchar_t *wtp = wcp;
28f540f4 977
28f540f4
RM
978 if (fracdig_no > 0)
979 {
980 /* Process fractional digits. Terminate if not rounded or
981 radix character is reached. */
0f7769f7 982 int removed = 0;
a1d84548 983 while (*--wtp != decimalwc && *wtp == L'9')
0f7769f7
UD
984 {
985 *wtp = L'0';
986 ++removed;
987 }
988 if (removed == fracdig_min && added_zeros > 0)
989 --added_zeros;
a1d84548 990 if (*wtp != decimalwc)
28f540f4 991 /* Round up. */
a1d84548 992 (*wtp)++;
8f5e1400
UD
993 else if (__builtin_expect (spec == 'g' && type == 'f' && info->alt
994 && wtp == wstartp + 1
995 && wstartp[0] == L'0',
0f7769f7
UD
996 0))
997 /* This is a special case: the rounded number is 1.0,
998 the format is 'g' or 'G', and the alternative format
d40e67f5 999 is selected. This means the result must be "1.". */
0f7769f7 1000 --added_zeros;
28f540f4
RM
1001 }
1002
a1d84548 1003 if (fracdig_no == 0 || *wtp == decimalwc)
28f540f4
RM
1004 {
1005 /* Round the integer digits. */
a1d84548
UD
1006 if (*(wtp - 1) == decimalwc)
1007 --wtp;
28f540f4 1008
a1d84548
UD
1009 while (--wtp >= wstartp && *wtp == L'9')
1010 *wtp = L'0';
28f540f4 1011
a1d84548 1012 if (wtp >= wstartp)
28f540f4 1013 /* Round up. */
a1d84548 1014 (*wtp)++;
28f540f4 1015 else
6d52618b 1016 /* It is more critical. All digits were 9's. */
28f540f4
RM
1017 {
1018 if (type != 'f')
1019 {
a1d84548 1020 *wstartp = '1';
28f540f4 1021 exponent += expsign == 0 ? 1 : -1;
b866373d
UD
1022
1023 /* The above exponent adjustment could lead to 1.0e-00,
1024 e.g. for 0.999999999. Make sure exponent 0 always
1025 uses + sign. */
1026 if (exponent == 0)
1027 expsign = 0;
28f540f4
RM
1028 }
1029 else if (intdig_no == dig_max)
1030 {
1031 /* This is the case where for type %g the number fits
1032 really in the range for %f output but after rounding
1033 the number of digits is too big. */
a1d84548
UD
1034 *--wstartp = decimalwc;
1035 *--wstartp = L'1';
28f540f4
RM
1036
1037 if (info->alt || fracdig_no > 0)
1038 {
1039 /* Overwrite the old radix character. */
a1d84548 1040 wstartp[intdig_no + 2] = L'0';
28f540f4
RM
1041 ++fracdig_no;
1042 }
1043
1044 fracdig_no += intdig_no;
1045 intdig_no = 1;
1046 fracdig_max = intdig_max - intdig_no;
1047 ++exponent;
1048 /* Now we must print the exponent. */
1049 type = isupper (info->spec) ? 'E' : 'e';
1050 }
1051 else
1052 {
1053 /* We can simply add another another digit before the
1054 radix. */
a1d84548 1055 *--wstartp = L'1';
28f540f4
RM
1056 ++intdig_no;
1057 }
1058
1059 /* While rounding the number of digits can change.
1060 If the number now exceeds the limits remove some
1061 fractional digits. */
1062 if (intdig_no + fracdig_no > dig_max)
1063 {
a1d84548 1064 wcp -= intdig_no + fracdig_no - dig_max;
28f540f4
RM
1065 fracdig_no -= intdig_no + fracdig_no - dig_max;
1066 }
1067 }
1068 }
1069 }
1070
28f540f4 1071 /* Now remove unnecessary '0' at the end of the string. */
0f7769f7 1072 while (fracdig_no > fracdig_min + added_zeros && *(wcp - 1) == L'0')
28f540f4 1073 {
a1d84548 1074 --wcp;
28f540f4
RM
1075 --fracdig_no;
1076 }
1077 /* If we eliminate all fractional digits we perhaps also can remove
1078 the radix character. */
a1d84548
UD
1079 if (fracdig_no == 0 && !info->alt && *(wcp - 1) == decimalwc)
1080 --wcp;
28f540f4
RM
1081
1082 if (grouping)
f57e41a5
UD
1083 {
1084 /* Rounding might have changed the number of groups. We allocated
1085 enough memory but we need here the correct number of groups. */
1086 if (intdig_no != intdig_max)
1087 ngroups = __guess_grouping (intdig_no, grouping);
1088
1089 /* Add in separator characters, overwriting the same buffer. */
1090 wcp = group_number (wstartp, wcp, intdig_no, grouping, thousands_sepwc,
1091 ngroups);
1092 }
28f540f4
RM
1093
1094 /* Write the exponent if it is needed. */
1095 if (type != 'f')
1096 {
a1ffb40e 1097 if (__glibc_unlikely (expsign != 0 && exponent == 4 && spec == 'g'))
0f7769f7
UD
1098 {
1099 /* This is another special case. The exponent of the number is
1100 really smaller than -4, which requires the 'e'/'E' format.
1101 But after rounding the number has an exponent of -4. */
d40e67f5 1102 assert (wcp >= wstartp + 1);
0f7769f7
UD
1103 assert (wstartp[0] == L'1');
1104 __wmemcpy (wstartp, L"0.0001", 6);
1105 wstartp[1] = decimalwc;
d40e67f5
UD
1106 if (wcp >= wstartp + 2)
1107 {
1108 wmemset (wstartp + 6, L'0', wcp - (wstartp + 2));
1109 wcp += 4;
1110 }
1111 else
1112 wcp += 5;
0f7769f7
UD
1113 }
1114 else
1115 {
1116 *wcp++ = (wchar_t) type;
1117 *wcp++ = expsign ? L'-' : L'+';
28f540f4 1118
0f7769f7
UD
1119 /* Find the magnitude of the exponent. */
1120 expscale = 10;
1121 while (expscale <= exponent)
1122 expscale *= 10;
28f540f4 1123
0f7769f7
UD
1124 if (exponent < 10)
1125 /* Exponent always has at least two digits. */
1126 *wcp++ = L'0';
1127 else
1128 do
1129 {
1130 expscale /= 10;
1131 *wcp++ = L'0' + (exponent / expscale);
1132 exponent %= expscale;
1133 }
1134 while (expscale > 10);
1135 *wcp++ = L'0' + exponent;
1136 }
28f540f4
RM
1137 }
1138
1139 /* Compute number of characters which must be filled with the padding
96aa2d94 1140 character. */
28f540f4
RM
1141 if (is_neg || info->showsign || info->space)
1142 --width;
a1d84548 1143 width -= wcp - wstartp;
28f540f4
RM
1144
1145 if (!info->left && info->pad != '0' && width > 0)
1146 PADN (info->pad, width);
1147
1148 if (is_neg)
1149 outchar ('-');
1150 else if (info->showsign)
1151 outchar ('+');
1152 else if (info->space)
1153 outchar (' ');
1154
1155 if (!info->left && info->pad == '0' && width > 0)
1156 PADN ('0', width);
1157
a1d84548
UD
1158 {
1159 char *buffer = NULL;
3703468e 1160 char *buffer_end = NULL;
a1d84548 1161 char *cp = NULL;
a5707dad 1162 char *tmpptr;
a1d84548
UD
1163
1164 if (! wide)
1165 {
1166 /* Create the single byte string. */
a1d84548
UD
1167 size_t decimal_len;
1168 size_t thousands_sep_len;
1169 wchar_t *copywc;
3703468e
UD
1170 size_t factor = (info->i18n
1171 ? _NL_CURRENT_WORD (LC_CTYPE, _NL_CTYPE_MB_CUR_MAX)
1172 : 1);
a1d84548 1173
a1d84548
UD
1174 decimal_len = strlen (decimal);
1175
1176 if (thousands_sep == NULL)
1177 thousands_sep_len = 0;
1178 else
1179 thousands_sep_len = strlen (thousands_sep);
1180
3703468e
UD
1181 size_t nbuffer = (2 + chars_needed * factor + decimal_len
1182 + ngroups * thousands_sep_len);
a1ffb40e 1183 if (__glibc_unlikely (buffer_malloced))
a1d84548 1184 {
3703468e 1185 buffer = (char *) malloc (nbuffer);
a1d84548 1186 if (buffer == NULL)
c7df983c
UD
1187 {
1188 /* Signal an error to the caller. */
0f7769f7 1189 free (wbuffer);
c7df983c
UD
1190 return -1;
1191 }
a1d84548
UD
1192 }
1193 else
3703468e
UD
1194 buffer = (char *) alloca (nbuffer);
1195 buffer_end = buffer + nbuffer;
a1d84548
UD
1196
1197 /* Now copy the wide character string. Since the character
1198 (except for the decimal point and thousands separator) must
1199 be coming from the ASCII range we can esily convert the
1200 string without mapping tables. */
1201 for (cp = buffer, copywc = wstartp; copywc < wcp; ++copywc)
1202 if (*copywc == decimalwc)
1203 cp = (char *) __mempcpy (cp, decimal, decimal_len);
1204 else if (*copywc == thousands_sepwc)
1205 cp = (char *) __mempcpy (cp, thousands_sep, thousands_sep_len);
1206 else
1207 *cp++ = (char) *copywc;
1208 }
1209
a5707dad 1210 tmpptr = buffer;
a1ffb40e 1211 if (__glibc_unlikely (info->i18n))
f57e41a5 1212 {
8a7455e7 1213#ifdef COMPILE_WPRINTF
3703468e
UD
1214 wstartp = _i18n_number_rewrite (wstartp, wcp,
1215 wbuffer + wbuffer_to_alloc);
1311b164
UD
1216 wcp = wbuffer + wbuffer_to_alloc;
1217 assert ((uintptr_t) wbuffer <= (uintptr_t) wstartp);
1218 assert ((uintptr_t) wstartp
1219 < (uintptr_t) wbuffer + wbuffer_to_alloc);
8a7455e7 1220#else
3703468e
UD
1221 tmpptr = _i18n_number_rewrite (tmpptr, cp, buffer_end);
1222 cp = buffer_end;
1223 assert ((uintptr_t) buffer <= (uintptr_t) tmpptr);
1224 assert ((uintptr_t) tmpptr < (uintptr_t) buffer_end);
8a7455e7 1225#endif
f57e41a5 1226 }
8a7455e7 1227
a5707dad 1228 PRINT (tmpptr, wstartp, wide ? wcp - wstartp : cp - tmpptr);
a1d84548
UD
1229
1230 /* Free the memory if necessary. */
a1ffb40e 1231 if (__glibc_unlikely (buffer_malloced))
a1d84548
UD
1232 {
1233 free (buffer);
1234 free (wbuffer);
1235 }
1236 }
28f540f4
RM
1237
1238 if (info->left && width > 0)
1239 PADN (info->pad, width);
1240 }
1241 return done;
1242}
c6251f03
RM
1243ldbl_hidden_def (___printf_fp, __printf_fp)
1244ldbl_strong_alias (___printf_fp, __printf_fp)
28f540f4
RM
1245\f
1246/* Return the number of extra grouping characters that will be inserted
1247 into a number with INTDIG_MAX integer digits. */
1248
b8fe19fa 1249unsigned int
a1d84548 1250__guess_grouping (unsigned int intdig_max, const char *grouping)
28f540f4
RM
1251{
1252 unsigned int groups;
1253
1254 /* We treat all negative values like CHAR_MAX. */
1255
1256 if (*grouping == CHAR_MAX || *grouping <= 0)
1257 /* No grouping should be done. */
1258 return 0;
1259
1260 groups = 0;
67a3a8ac 1261 while (intdig_max > (unsigned int) *grouping)
28f540f4
RM
1262 {
1263 ++groups;
1264 intdig_max -= *grouping++;
1265
60c96635
UD
1266 if (*grouping == CHAR_MAX
1267#if CHAR_MIN < 0
1268 || *grouping < 0
1269#endif
1270 )
28f540f4
RM
1271 /* No more grouping should be done. */
1272 break;
1273 else if (*grouping == 0)
1274 {
1275 /* Same grouping repeats. */
8a4b65b4 1276 groups += (intdig_max - 1) / grouping[-1];
28f540f4
RM
1277 break;
1278 }
1279 }
1280
1281 return groups;
1282}
1283
1284/* Group the INTDIG_NO integer digits of the number in [BUF,BUFEND).
1285 There is guaranteed enough space past BUFEND to extend it.
1286 Return the new end of buffer. */
1287
a1d84548 1288static wchar_t *
dfd2257a 1289internal_function
a1d84548
UD
1290group_number (wchar_t *buf, wchar_t *bufend, unsigned int intdig_no,
1291 const char *grouping, wchar_t thousands_sep, int ngroups)
28f540f4 1292{
a1d84548 1293 wchar_t *p;
28f540f4 1294
a1d84548 1295 if (ngroups == 0)
28f540f4
RM
1296 return bufend;
1297
1298 /* Move the fractional part down. */
4aebaa6b
UD
1299 __wmemmove (buf + intdig_no + ngroups, buf + intdig_no,
1300 bufend - (buf + intdig_no));
28f540f4 1301
a1d84548 1302 p = buf + intdig_no + ngroups - 1;
28f540f4
RM
1303 do
1304 {
1305 unsigned int len = *grouping++;
1306 do
1307 *p-- = buf[--intdig_no];
1308 while (--len > 0);
1309 *p-- = thousands_sep;
1310
60c96635
UD
1311 if (*grouping == CHAR_MAX
1312#if CHAR_MIN < 0
1313 || *grouping < 0
1314#endif
1315 )
28f540f4
RM
1316 /* No more grouping should be done. */
1317 break;
1318 else if (*grouping == 0)
1319 /* Same grouping repeats. */
1320 --grouping;
67a3a8ac 1321 } while (intdig_no > (unsigned int) *grouping);
28f540f4
RM
1322
1323 /* Copy the remaining ungrouped digits. */
1324 do
1325 *p-- = buf[--intdig_no];
1326 while (p > buf);
1327
a1d84548 1328 return bufend + ngroups;
28f540f4 1329}