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