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