1 /* Floating point output for `printf'.
2 Copyright (C) 1995-2023 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <https://www.gnu.org/licenses/>. */
20 /* The gmp headers need some configuration frobs. */
23 #include <array_length.h>
28 #include <gmp-mparam.h>
31 #include <stdlib/gmp-impl.h>
32 #include <stdlib/longlong.h>
33 #include <stdlib/fpioconst.h>
34 #include <locale/localeinfo.h>
43 #include <rounding-mode.h>
44 #include <printf_buffer.h>
45 #include <printf_buffer_to_file.h>
46 #include <grouping_iterator.h>
50 /* We use the GNU MP library to handle large numbers.
52 An MP variable occupies a varying number of entries in its array. We keep
53 track of this number for efficiency reasons. Otherwise we would always
54 have to process the whole array. */
55 #define MPN_VAR(name) mp_limb_t *name; mp_size_t name##size
57 #define MPN_ASSIGN(dst,src) \
58 memcpy (dst, src, (dst##size = src##size) * sizeof (mp_limb_t))
60 (u##size > v##size || (u##size == v##size && __mpn_cmp (u, v, u##size) >= 0))
62 extern mp_size_t
__mpn_extract_double (mp_ptr res_ptr
, mp_size_t size
,
63 int *expt
, int *is_neg
,
65 extern mp_size_t
__mpn_extract_long_double (mp_ptr res_ptr
, mp_size_t size
,
66 int *expt
, int *is_neg
,
70 struct hack_digit_param
72 /* Sign of the exponent. */
74 /* The type of output format that will be used: 'e'/'E' or 'f'. */
76 /* and the exponent. */
78 /* The fraction of the floting-point value in question */
82 /* Temporary bignum value. */
87 hack_digit (struct hack_digit_param
*p
)
91 if (p
->expsign
!= 0 && p
->type
== 'f' && p
->exponent
-- > 0)
93 else if (p
->scalesize
== 0)
95 hi
= p
->frac
[p
->fracsize
- 1];
96 p
->frac
[p
->fracsize
- 1] = __mpn_mul_1 (p
->frac
, p
->frac
,
101 if (p
->fracsize
< p
->scalesize
)
105 hi
= mpn_divmod (p
->tmp
, p
->frac
, p
->fracsize
,
106 p
->scale
, p
->scalesize
);
107 p
->tmp
[p
->fracsize
- p
->scalesize
] = hi
;
110 p
->fracsize
= p
->scalesize
;
111 while (p
->fracsize
!= 0 && p
->frac
[p
->fracsize
- 1] == 0)
113 if (p
->fracsize
== 0)
115 /* We're not prepared for an mpn variable with zero
122 mp_limb_t _cy
= __mpn_mul_1 (p
->frac
, p
->frac
, p
->fracsize
, 10);
124 p
->frac
[p
->fracsize
++] = _cy
;
130 /* Version that performs grouping (if INFO->group && THOUSANDS_SEP != 0),
131 but not i18n digit translation.
133 The output buffer is always multibyte (not wide) at this stage.
134 Wide conversion and i18n digit translation happen later, with a
135 temporary buffer. To prepare for that, THOUSANDS_SEP_LENGTH is the
136 final length of the thousands separator. */
138 __printf_fp_buffer_1 (struct __printf_buffer
*buf
, locale_t loc
,
139 char thousands_sep
, char decimal
,
140 unsigned int thousands_sep_length
,
141 const struct printf_info
*info
,
142 const void *const *args
)
144 /* The floating-point value to output. */
149 #if __HAVE_DISTINCT_FLOAT128
155 /* "NaN" or "Inf" for the special cases. */
156 const char *special
= NULL
;
158 /* Used to determine grouping rules. */
159 int lc_category
= info
->extra
? LC_MONETARY
: LC_NUMERIC
;
161 /* When _Float128 is enabled in the library and ABI-distinct from long
162 double, we need mp_limbs enough for any of them. */
163 #if __HAVE_DISTINCT_FLOAT128
164 # define GREATER_MANT_DIG FLT128_MANT_DIG
166 # define GREATER_MANT_DIG LDBL_MANT_DIG
168 /* We need just a few limbs for the input before shifting to the right
170 mp_limb_t fp_input
[(GREATER_MANT_DIG
+ BITS_PER_MP_LIMB
- 1)
172 /* We need to shift the contents of fp_input by this amount of bits. */
175 struct hack_digit_param p
;
176 /* Sign of float number. */
179 /* General helper (carry limb). */
182 /* Buffer in which we produce the output. */
183 char *wbuffer
= NULL
;
184 /* Flag whether wbuffer and buffer are malloc'ed or not. */
185 int buffer_malloced
= 0;
189 #define PRINTF_FP_FETCH(FLOAT, VAR, SUFFIX, MANT_DIG) \
191 (VAR) = *(const FLOAT *) args[0]; \
193 /* Check for special values: not a number or infinity. */ \
196 is_neg = signbit (VAR); \
197 if (isupper (info->spec)) \
202 else if (isinf (VAR)) \
204 is_neg = signbit (VAR); \
205 if (isupper (info->spec)) \
212 p.fracsize = __mpn_extract_##SUFFIX \
213 (fp_input, array_length (fp_input), \
214 &p.exponent, &is_neg, VAR); \
215 to_shift = 1 + p.fracsize * BITS_PER_MP_LIMB - MANT_DIG; \
219 /* Fetch the argument value. */
220 #if __HAVE_DISTINCT_FLOAT128
221 if (info
->is_binary128
)
222 PRINTF_FP_FETCH (_Float128
, fpnum
.f128
, float128
, FLT128_MANT_DIG
)
225 #ifndef __NO_LONG_DOUBLE_MATH
226 if (info
->is_long_double
&& sizeof (long double) > sizeof (double))
227 PRINTF_FP_FETCH (long double, fpnum
.ldbl
, long_double
, LDBL_MANT_DIG
)
230 PRINTF_FP_FETCH (double, fpnum
.dbl
, double, DBL_MANT_DIG
)
232 #undef PRINTF_FP_FETCH
236 int width
= info
->width
;
238 if (is_neg
|| info
->showsign
|| info
->space
)
243 __printf_buffer_pad (buf
, ' ', width
);
246 __printf_buffer_putc (buf
, '-');
247 else if (info
->showsign
)
248 __printf_buffer_putc (buf
, '+');
249 else if (info
->space
)
250 __printf_buffer_putc (buf
, ' ');
252 __printf_buffer_puts (buf
, special
);
255 __printf_buffer_pad (buf
, ' ', width
);
261 /* We need three multiprecision variables. Now that we have the p.exponent
262 of the number we can allocate the needed memory. It would be more
263 efficient to use variables of the fixed maximum size but because this
264 would be really big it could lead to memory problems. */
266 mp_size_t bignum_size
= ((abs (p
.exponent
) + BITS_PER_MP_LIMB
- 1)
268 + (GREATER_MANT_DIG
/ BITS_PER_MP_LIMB
> 2
270 * sizeof (mp_limb_t
);
271 p
.frac
= (mp_limb_t
*) alloca (bignum_size
);
272 p
.tmp
= (mp_limb_t
*) alloca (bignum_size
);
273 p
.scale
= (mp_limb_t
*) alloca (bignum_size
);
276 /* We now have to distinguish between numbers with positive and negative
277 exponents because the method used for the one is not applicable/efficient
285 #if __HAVE_DISTINCT_FLOAT128
286 if (info
->is_binary128
)
287 explog
= FLT128_MAX_10_EXP_LOG
;
289 explog
= LDBL_MAX_10_EXP_LOG
;
291 explog
= LDBL_MAX_10_EXP_LOG
;
294 const struct mp_power
*powers
= &_fpioconst_pow10
[explog
+ 1];
297 if ((p
.exponent
+ to_shift
) % BITS_PER_MP_LIMB
== 0)
299 MPN_COPY_DECR (p
.frac
+ (p
.exponent
+ to_shift
) / BITS_PER_MP_LIMB
,
300 fp_input
, p
.fracsize
);
301 p
.fracsize
+= (p
.exponent
+ to_shift
) / BITS_PER_MP_LIMB
;
305 cy
= __mpn_lshift (p
.frac
306 + (p
.exponent
+ to_shift
) / BITS_PER_MP_LIMB
,
307 fp_input
, p
.fracsize
,
308 (p
.exponent
+ to_shift
) % BITS_PER_MP_LIMB
);
309 p
.fracsize
+= (p
.exponent
+ to_shift
) / BITS_PER_MP_LIMB
;
311 p
.frac
[p
.fracsize
++] = cy
;
313 MPN_ZERO (p
.frac
, (p
.exponent
+ to_shift
) / BITS_PER_MP_LIMB
);
315 assert (powers
> &_fpioconst_pow10
[0]);
320 /* The number of the product of two binary numbers with n and m
321 bits respectively has m+n or m+n-1 bits. */
322 if (p
.exponent
>= scaleexpo
+ powers
->p_expo
- 1)
324 if (p
.scalesize
== 0)
326 #if __HAVE_DISTINCT_FLOAT128
328 > _FPIO_CONST_OFFSET
* BITS_PER_MP_LIMB
)
329 && info
->is_binary128
)
331 #define _FLT128_FPIO_CONST_SHIFT \
332 (((FLT128_MANT_DIG + BITS_PER_MP_LIMB - 1) / BITS_PER_MP_LIMB) \
333 - _FPIO_CONST_OFFSET)
334 /* 64bit const offset is not enough for
335 IEEE 854 quad long double (_Float128). */
336 p
.tmpsize
= powers
->arraysize
+ _FLT128_FPIO_CONST_SHIFT
;
337 memcpy (p
.tmp
+ _FLT128_FPIO_CONST_SHIFT
,
338 &__tens
[powers
->arrayoff
],
339 p
.tmpsize
* sizeof (mp_limb_t
));
340 MPN_ZERO (p
.tmp
, _FLT128_FPIO_CONST_SHIFT
);
341 /* Adjust p.exponent, as scaleexpo will be this much
343 p
.exponent
+= _FLT128_FPIO_CONST_SHIFT
* BITS_PER_MP_LIMB
;
346 #endif /* __HAVE_DISTINCT_FLOAT128 */
347 #ifndef __NO_LONG_DOUBLE_MATH
348 if (LDBL_MANT_DIG
> _FPIO_CONST_OFFSET
* BITS_PER_MP_LIMB
349 && info
->is_long_double
)
351 #define _FPIO_CONST_SHIFT \
352 (((LDBL_MANT_DIG + BITS_PER_MP_LIMB - 1) / BITS_PER_MP_LIMB) \
353 - _FPIO_CONST_OFFSET)
354 /* 64bit const offset is not enough for
355 IEEE quad long double. */
356 p
.tmpsize
= powers
->arraysize
+ _FPIO_CONST_SHIFT
;
357 memcpy (p
.tmp
+ _FPIO_CONST_SHIFT
,
358 &__tens
[powers
->arrayoff
],
359 p
.tmpsize
* sizeof (mp_limb_t
));
360 MPN_ZERO (p
.tmp
, _FPIO_CONST_SHIFT
);
361 /* Adjust p.exponent, as scaleexpo will be this much
363 p
.exponent
+= _FPIO_CONST_SHIFT
* BITS_PER_MP_LIMB
;
368 p
.tmpsize
= powers
->arraysize
;
369 memcpy (p
.tmp
, &__tens
[powers
->arrayoff
],
370 p
.tmpsize
* sizeof (mp_limb_t
));
375 cy
= __mpn_mul (p
.tmp
, p
.scale
, p
.scalesize
,
376 &__tens
[powers
->arrayoff
377 + _FPIO_CONST_OFFSET
],
378 powers
->arraysize
- _FPIO_CONST_OFFSET
);
379 p
.tmpsize
= p
.scalesize
380 + powers
->arraysize
- _FPIO_CONST_OFFSET
;
385 if (MPN_GE (p
.frac
, p
.tmp
))
388 MPN_ASSIGN (p
.scale
, p
.tmp
);
389 count_leading_zeros (cnt
, p
.scale
[p
.scalesize
- 1]);
390 scaleexpo
= (p
.scalesize
- 2) * BITS_PER_MP_LIMB
- cnt
- 1;
391 exp10
|= 1 << explog
;
396 while (powers
> &_fpioconst_pow10
[0]);
399 /* Optimize number representations. We want to represent the numbers
400 with the lowest number of bytes possible without losing any
401 bytes. Also the highest bit in the scaling factor has to be set
402 (this is a requirement of the MPN division routines). */
405 /* Determine minimum number of zero bits at the end of
407 for (i
= 0; p
.scale
[i
] == 0 && p
.frac
[i
] == 0; i
++)
410 /* Determine number of bits the scaling factor is misplaced. */
411 count_leading_zeros (cnt_h
, p
.scale
[p
.scalesize
- 1]);
415 /* The highest bit of the scaling factor is already set. So
416 we only have to remove the trailing empty limbs. */
419 MPN_COPY_INCR (p
.scale
, p
.scale
+ i
, p
.scalesize
- i
);
421 MPN_COPY_INCR (p
.frac
, p
.frac
+ i
, p
.fracsize
- i
);
429 count_trailing_zeros (cnt_l
, p
.scale
[i
]);
433 count_trailing_zeros (cnt_l2
, p
.frac
[i
]);
439 count_trailing_zeros (cnt_l
, p
.frac
[i
]);
441 /* Now shift the numbers to their optimal position. */
442 if (i
== 0 && BITS_PER_MP_LIMB
- cnt_h
> cnt_l
)
444 /* We cannot save any memory. So just roll both numbers
445 so that the scaling factor has its highest bit set. */
447 (void) __mpn_lshift (p
.scale
, p
.scale
, p
.scalesize
, cnt_h
);
448 cy
= __mpn_lshift (p
.frac
, p
.frac
, p
.fracsize
, cnt_h
);
450 p
.frac
[p
.fracsize
++] = cy
;
452 else if (BITS_PER_MP_LIMB
- cnt_h
<= cnt_l
)
454 /* We can save memory by removing the trailing zero limbs
455 and by packing the non-zero limbs which gain another
458 (void) __mpn_rshift (p
.scale
, p
.scale
+ i
, p
.scalesize
- i
,
459 BITS_PER_MP_LIMB
- cnt_h
);
460 p
.scalesize
-= i
+ 1;
461 (void) __mpn_rshift (p
.frac
, p
.frac
+ i
, p
.fracsize
- i
,
462 BITS_PER_MP_LIMB
- cnt_h
);
463 p
.fracsize
-= p
.frac
[p
.fracsize
- i
- 1] == 0 ? i
+ 1 : i
;
467 /* We can only save the memory of the limbs which are zero.
468 The non-zero parts occupy the same number of limbs. */
470 (void) __mpn_rshift (p
.scale
, p
.scale
+ (i
- 1),
471 p
.scalesize
- (i
- 1),
472 BITS_PER_MP_LIMB
- cnt_h
);
474 (void) __mpn_rshift (p
.frac
, p
.frac
+ (i
- 1),
475 p
.fracsize
- (i
- 1),
476 BITS_PER_MP_LIMB
- cnt_h
);
478 p
.frac
[p
.fracsize
- (i
- 1) - 1] == 0 ? i
: i
- 1;
483 else if (p
.exponent
< 0)
488 #if __HAVE_DISTINCT_FLOAT128
489 if (info
->is_binary128
)
490 explog
= FLT128_MAX_10_EXP_LOG
;
492 explog
= LDBL_MAX_10_EXP_LOG
;
494 explog
= LDBL_MAX_10_EXP_LOG
;
496 const struct mp_power
*powers
= &_fpioconst_pow10
[explog
+ 1];
498 /* Now shift the input value to its right place. */
499 cy
= __mpn_lshift (p
.frac
, fp_input
, p
.fracsize
, to_shift
);
500 p
.frac
[p
.fracsize
++] = cy
;
501 assert (cy
== 1 || (p
.frac
[p
.fracsize
- 2] == 0 && p
.frac
[0] == 0));
504 p
.exponent
= -p
.exponent
;
506 assert (powers
!= &_fpioconst_pow10
[0]);
511 if (p
.exponent
>= powers
->m_expo
)
513 int i
, incr
, cnt_h
, cnt_l
;
516 /* The __mpn_mul function expects the first argument to be
517 bigger than the second. */
518 if (p
.fracsize
< powers
->arraysize
- _FPIO_CONST_OFFSET
)
519 cy
= __mpn_mul (p
.tmp
, &__tens
[powers
->arrayoff
520 + _FPIO_CONST_OFFSET
],
521 powers
->arraysize
- _FPIO_CONST_OFFSET
,
524 cy
= __mpn_mul (p
.tmp
, p
.frac
, p
.fracsize
,
525 &__tens
[powers
->arrayoff
+ _FPIO_CONST_OFFSET
],
526 powers
->arraysize
- _FPIO_CONST_OFFSET
);
527 p
.tmpsize
= p
.fracsize
+ powers
->arraysize
- _FPIO_CONST_OFFSET
;
531 count_leading_zeros (cnt_h
, p
.tmp
[p
.tmpsize
- 1]);
532 incr
= (p
.tmpsize
- p
.fracsize
) * BITS_PER_MP_LIMB
533 + BITS_PER_MP_LIMB
- 1 - cnt_h
;
535 assert (incr
<= powers
->p_expo
);
537 /* If we increased the p.exponent by exactly 3 we have to test
538 for overflow. This is done by comparing with 10 shifted
539 to the right position. */
540 if (incr
== p
.exponent
+ 3)
542 if (cnt_h
<= BITS_PER_MP_LIMB
- 4)
546 = ((mp_limb_t
) 10) << (BITS_PER_MP_LIMB
- 4 - cnt_h
);
550 topval
[0] = ((mp_limb_t
) 10) << (BITS_PER_MP_LIMB
- 4);
552 (void) __mpn_lshift (topval
, topval
, 2,
553 BITS_PER_MP_LIMB
- cnt_h
);
557 /* We have to be careful when multiplying the last factor.
558 If the result is greater than 1.0 be have to test it
559 against 10.0. If it is greater or equal to 10.0 the
560 multiplication was not valid. This is because we cannot
561 determine the number of bits in the result in advance. */
562 if (incr
< p
.exponent
+ 3
563 || (incr
== p
.exponent
+ 3
564 && (p
.tmp
[p
.tmpsize
- 1] < topval
[1]
565 || (p
.tmp
[p
.tmpsize
- 1] == topval
[1]
566 && p
.tmp
[p
.tmpsize
- 2] < topval
[0]))))
568 /* The factor is right. Adapt binary and decimal
571 exp10
|= 1 << explog
;
573 /* If this factor yields a number greater or equal to
574 1.0, we must not shift the non-fractional digits down. */
576 cnt_h
+= -p
.exponent
;
578 /* Now we optimize the number representation. */
579 for (i
= 0; p
.tmp
[i
] == 0; ++i
);
580 if (cnt_h
== BITS_PER_MP_LIMB
- 1)
582 MPN_COPY (p
.frac
, p
.tmp
+ i
, p
.tmpsize
- i
);
583 p
.fracsize
= p
.tmpsize
- i
;
587 count_trailing_zeros (cnt_l
, p
.tmp
[i
]);
589 /* Now shift the numbers to their optimal position. */
590 if (i
== 0 && BITS_PER_MP_LIMB
- 1 - cnt_h
> cnt_l
)
592 /* We cannot save any memory. Just roll the
593 number so that the leading digit is in a
596 cy
= __mpn_lshift (p
.frac
, p
.tmp
, p
.tmpsize
,
598 p
.fracsize
= p
.tmpsize
+ 1;
599 p
.frac
[p
.fracsize
- 1] = cy
;
601 else if (BITS_PER_MP_LIMB
- 1 - cnt_h
<= cnt_l
)
603 (void) __mpn_rshift (p
.frac
, p
.tmp
+ i
, p
.tmpsize
- i
,
604 BITS_PER_MP_LIMB
- 1 - cnt_h
);
605 p
.fracsize
= p
.tmpsize
- i
;
609 /* We can only save the memory of the limbs which
610 are zero. The non-zero parts occupy the same
613 (void) __mpn_rshift (p
.frac
, p
.tmp
+ (i
- 1),
615 BITS_PER_MP_LIMB
- 1 - cnt_h
);
616 p
.fracsize
= p
.tmpsize
- (i
- 1);
623 while (powers
!= &_fpioconst_pow10
[1] && p
.exponent
> 0);
624 /* All factors but 10^-1 are tested now. */
629 cy
= __mpn_mul_1 (p
.tmp
, p
.frac
, p
.fracsize
, 10);
630 p
.tmpsize
= p
.fracsize
;
631 assert (cy
== 0 || p
.tmp
[p
.tmpsize
- 1] < 20);
633 count_trailing_zeros (cnt_l
, p
.tmp
[0]);
634 if (cnt_l
< MIN (4, p
.exponent
))
636 cy
= __mpn_lshift (p
.frac
, p
.tmp
, p
.tmpsize
,
637 BITS_PER_MP_LIMB
- MIN (4, p
.exponent
));
639 p
.frac
[p
.tmpsize
++] = cy
;
642 (void) __mpn_rshift (p
.frac
, p
.tmp
, p
.tmpsize
, MIN (4, p
.exponent
));
643 p
.fracsize
= p
.tmpsize
;
645 assert (p
.frac
[p
.fracsize
- 1] < 10);
651 /* This is a special case. We don't need a factor because the
652 numbers are in the range of 1.0 <= |fp| < 8.0. We simply
653 shift it to the right place and divide it by 1.0 to get the
654 leading digit. (Of course this division is not really made.) */
655 assert (0 <= p
.exponent
&& p
.exponent
< 3
656 && p
.exponent
+ to_shift
< BITS_PER_MP_LIMB
);
658 /* Now shift the input value to its right place. */
659 cy
= __mpn_lshift (p
.frac
, fp_input
, p
.fracsize
, (p
.exponent
+ to_shift
));
660 p
.frac
[p
.fracsize
++] = cy
;
665 int width
= info
->width
;
669 int intdig_max
, intdig_no
= 0;
674 char spec
= _tolower (info
->spec
);
680 fracdig_min
= fracdig_max
= info
->prec
< 0 ? 6 : info
->prec
;
681 chars_needed
= 1 + 1 + (size_t) fracdig_max
+ 1 + 1 + 4;
682 /* d . ddd e +- ddd */
683 dig_max
= INT_MAX
; /* Unlimited. */
684 significant
= 1; /* Does not matter here. */
686 else if (spec
== 'f')
689 fracdig_min
= fracdig_max
= info
->prec
< 0 ? 6 : info
->prec
;
690 dig_max
= INT_MAX
; /* Unlimited. */
691 significant
= 1; /* Does not matter here. */
694 intdig_max
= p
.exponent
+ 1;
695 /* This can be really big! */ /* XXX Maybe malloc if too big? */
696 chars_needed
= (size_t) p
.exponent
+ 1 + 1 + (size_t) fracdig_max
;
701 chars_needed
= 1 + 1 + (size_t) fracdig_max
;
706 dig_max
= info
->prec
< 0 ? 6 : (info
->prec
== 0 ? 1 : info
->prec
);
707 if ((p
.expsign
== 0 && p
.exponent
>= dig_max
)
708 || (p
.expsign
!= 0 && p
.exponent
> 4))
710 if ('g' - 'G' == 'e' - 'E')
711 p
.type
= 'E' + (info
->spec
- 'G');
713 p
.type
= isupper (info
->spec
) ? 'E' : 'e';
714 fracdig_max
= dig_max
- 1;
716 chars_needed
= 1 + 1 + (size_t) fracdig_max
+ 1 + 1 + 4;
721 intdig_max
= p
.expsign
== 0 ? p
.exponent
+ 1 : 0;
722 fracdig_max
= dig_max
- intdig_max
;
723 /* We need space for the significant digits and perhaps
724 for leading zeros when < 1.0. The number of leading
725 zeros can be as many as would be required for
726 exponential notation with a negative two-digit
727 p.exponent, which is 4. */
728 chars_needed
= (size_t) dig_max
+ 1 + 4;
730 fracdig_min
= info
->alt
? fracdig_max
: 0;
731 significant
= 0; /* We count significant digits. */
734 /* Allocate buffer for output. We need two more because while rounding
735 it is possible that we need two more characters in front of all the
736 other output. If the amount of memory we have to allocate is too
737 large use `malloc' instead of `alloca'. */
738 if (__glibc_unlikely (chars_needed
>= (size_t) -1 - 2
739 || chars_needed
< fracdig_max
))
741 /* Some overflow occurred. */
742 __set_errno (ERANGE
);
743 __printf_buffer_mark_failed (buf
);
746 size_t wbuffer_to_alloc
= 2 + chars_needed
;
747 buffer_malloced
= ! __libc_use_alloca (wbuffer_to_alloc
);
748 if (__builtin_expect (buffer_malloced
, 0))
750 wbuffer
= malloc (wbuffer_to_alloc
);
753 /* Signal an error to the caller. */
754 __printf_buffer_mark_failed (buf
);
759 wbuffer
= alloca (wbuffer_to_alloc
);
760 wcp
= wstartp
= wbuffer
+ 2; /* Let room for rounding. */
762 /* Do the real work: put digits in allocated buffer. */
763 if (p
.expsign
== 0 || p
.type
!= 'f')
765 assert (p
.expsign
== 0 || intdig_max
== 1);
766 while (intdig_no
< intdig_max
)
769 *wcp
++ = hack_digit (&p
);
774 || (fracdig_max
> 0 && (p
.fracsize
> 1 || p
.frac
[0] != 0)))
779 /* |fp| < 1.0 and the selected p.type is 'f', so put "0."
786 /* Generate the needed number of fractional digits. */
789 while (fracdig_no
< fracdig_min
+ added_zeros
790 || (fracdig_no
< fracdig_max
&& (p
.fracsize
> 1 || p
.frac
[0] != 0)))
793 *wcp
= hack_digit (&p
);
796 else if (significant
== 0)
805 char last_digit
= wcp
[-1] != decimal
? wcp
[-1] : wcp
[-2];
806 char next_digit
= hack_digit (&p
);
808 if (next_digit
!= '0' && next_digit
!= '5')
810 else if (p
.fracsize
== 1 && p
.frac
[0] == 0)
811 /* Rest of the number is zero. */
813 else if (p
.scalesize
== 0)
815 /* Here we have to see whether all limbs are zero since no
816 normalization happened. */
817 size_t lcnt
= p
.fracsize
;
818 while (lcnt
>= 1 && p
.frac
[lcnt
- 1] == 0)
820 more_bits
= lcnt
> 0;
824 int rounding_mode
= get_rounding_mode ();
825 if (round_away (is_neg
, (last_digit
- '0') & 1, next_digit
>= '5',
826 more_bits
, rounding_mode
))
832 /* Process fractional digits. Terminate if not rounded or
833 radix character is reached. */
835 while (*--wtp
!= decimal
&& *wtp
== '9')
840 if (removed
== fracdig_min
&& added_zeros
> 0)
845 else if (__builtin_expect (spec
== 'g' && p
.type
== 'f' && info
->alt
846 && wtp
== wstartp
+ 1
847 && wstartp
[0] == '0',
849 /* This is a special case: the rounded number is 1.0,
850 the format is 'g' or 'G', and the alternative format
851 is selected. This means the result must be "1.". */
855 if (fracdig_no
== 0 || *wtp
== decimal
)
857 /* Round the integer digits. */
858 if (*(wtp
- 1) == decimal
)
861 while (--wtp
>= wstartp
&& *wtp
== '9')
868 /* It is more critical. All digits were 9's. */
873 p
.exponent
+= p
.expsign
== 0 ? 1 : -1;
875 /* The above p.exponent adjustment could lead to 1.0e-00,
876 e.g. for 0.999999999. Make sure p.exponent 0 always
881 else if (intdig_no
== dig_max
)
883 /* This is the case where for p.type %g the number fits
884 really in the range for %f output but after rounding
885 the number of digits is too big. */
886 *--wstartp
= decimal
;
889 if (info
->alt
|| fracdig_no
> 0)
891 /* Overwrite the old radix character. */
892 wstartp
[intdig_no
+ 2] = '0';
896 fracdig_no
+= intdig_no
;
898 fracdig_max
= intdig_max
- intdig_no
;
900 /* Now we must print the p.exponent. */
901 p
.type
= isupper (info
->spec
) ? 'E' : 'e';
905 /* We can simply add another another digit before the
911 /* While rounding the number of digits can change.
912 If the number now exceeds the limits remove some
913 fractional digits. */
914 if (intdig_no
+ fracdig_no
> dig_max
)
916 wcp
-= intdig_no
+ fracdig_no
- dig_max
;
917 fracdig_no
-= intdig_no
+ fracdig_no
- dig_max
;
923 /* Now remove unnecessary '0' at the end of the string. */
924 while (fracdig_no
> fracdig_min
+ added_zeros
&& *(wcp
- 1) == '0')
929 /* If we eliminate all fractional digits we perhaps also can remove
930 the radix character. */
931 if (fracdig_no
== 0 && !info
->alt
&& *(wcp
- 1) == decimal
)
934 /* Write the p.exponent if it is needed. */
937 if (__glibc_unlikely (p
.expsign
!= 0 && p
.exponent
== 4 && spec
== 'g'))
939 /* This is another special case. The p.exponent of the number is
940 really smaller than -4, which requires the 'e'/'E' format.
941 But after rounding the number has an p.exponent of -4. */
942 assert (wcp
>= wstartp
+ 1);
943 assert (wstartp
[0] == '1');
944 memcpy (wstartp
, "0.0001", 6);
945 wstartp
[1] = decimal
;
946 if (wcp
>= wstartp
+ 2)
948 memset (wstartp
+ 6, '0', wcp
- (wstartp
+ 2));
957 *wcp
++ = p
.expsign
? '-' : '+';
959 /* Find the magnitude of the p.exponent. */
961 while (expscale
<= p
.exponent
)
965 /* Exponent always has at least two digits. */
971 *wcp
++ = '0' + (p
.exponent
/ expscale
);
972 p
.exponent
%= expscale
;
974 while (expscale
> 10);
975 *wcp
++ = '0' + p
.exponent
;
979 struct grouping_iterator iter
;
980 if (thousands_sep
!= '\0' && info
->group
)
981 __grouping_iterator_init (&iter
, lc_category
, loc
, intdig_no
);
985 /* Compute number of characters which must be filled with the padding
987 if (is_neg
|| info
->showsign
|| info
->space
)
989 /* To count bytes, we would have to use __translated_number_width
990 for info->i18n && !info->wide. See bug 28943. */
991 width
-= wcp
- wstartp
;
992 /* For counting bytes, we would have to multiply by
993 thousands_sep_length. */
994 width
-= iter
.separators
;
996 if (!info
->left
&& info
->pad
!= '0')
997 __printf_buffer_pad (buf
, info
->pad
, width
);
1000 __printf_buffer_putc (buf
, '-');
1001 else if (info
->showsign
)
1002 __printf_buffer_putc (buf
, '+');
1003 else if (info
->space
)
1004 __printf_buffer_putc (buf
, ' ');
1006 if (!info
->left
&& info
->pad
== '0')
1007 __printf_buffer_pad (buf
, '0', width
);
1009 if (iter
.separators
> 0)
1012 for (int i
= 0; i
< intdig_no
; ++i
)
1014 if (__grouping_iterator_next (&iter
))
1015 __printf_buffer_putc (buf
, thousands_sep
);
1016 __printf_buffer_putc (buf
, *cp
);
1019 __printf_buffer_write (buf
, cp
, wcp
- cp
);
1022 __printf_buffer_write (buf
, wstartp
, wcp
- wstartp
);
1025 __printf_buffer_pad (buf
, info
->pad
, width
);
1028 if (buffer_malloced
)
1032 /* ASCII to localization translation. Multibyte version. */
1033 struct __printf_buffer_fp
1035 struct __printf_buffer base
;
1037 /* Replacement for ',' and '.'. */
1038 const char *thousands_sep
;
1039 const char *decimal
;
1040 unsigned char decimal_point_bytes
;
1041 unsigned char thousands_sep_length
;
1043 /* Buffer to write to. */
1044 struct __printf_buffer
*next
;
1046 /* Activates outdigit translation if not NULL. */
1047 struct __locale_data
*ctype
;
1049 /* Buffer to which the untranslated ASCII digits are written. */
1050 char untranslated
[PRINTF_BUFFER_SIZE_DIGITS
];
1053 /* Multibyte buffer-to-buffer flush function with full translation. */
1055 __printf_buffer_flush_fp (struct __printf_buffer_fp
*buf
)
1057 /* No need to update buf->base.written; the actual count is
1058 maintained in buf->next->written. */
1059 for (char *p
= buf
->untranslated
; p
< buf
->base
.write_ptr
; ++p
)
1062 const char *replacement
= NULL
;
1063 unsigned int replacement_bytes
;
1066 replacement
= buf
->thousands_sep
;
1067 replacement_bytes
= buf
->thousands_sep_length
;
1071 replacement
= buf
->decimal
;
1072 replacement_bytes
= buf
->decimal_point_bytes
;
1074 else if (buf
->ctype
!= NULL
&& '0' <= ch
&& ch
<= '9')
1076 int digit
= ch
- '0';
1078 = buf
->ctype
->values
[_NL_ITEM_INDEX (_NL_CTYPE_OUTDIGIT0_MB
)
1080 struct lc_ctype_data
*ctype
= buf
->ctype
->private;
1081 replacement_bytes
= ctype
->outdigit_bytes
[digit
];
1083 if (replacement
== NULL
)
1084 __printf_buffer_putc (buf
->next
, ch
);
1086 __printf_buffer_write (buf
->next
, replacement
, replacement_bytes
);
1089 if (!__printf_buffer_has_failed (buf
->next
))
1090 buf
->base
.write_ptr
= buf
->untranslated
;
1092 __printf_buffer_mark_failed (&buf
->base
);
1096 __printf_fp_l_buffer (struct __printf_buffer
*buf
, locale_t loc
,
1097 const struct printf_info
*info
,
1098 const void *const *args
)
1100 struct __printf_buffer_fp tmp
;
1104 tmp
.thousands_sep
= _nl_lookup (loc
, LC_MONETARY
, MON_THOUSANDS_SEP
);
1105 tmp
.decimal
= _nl_lookup (loc
, LC_MONETARY
, MON_DECIMAL_POINT
);
1106 if (tmp
.decimal
[0] == '\0')
1107 tmp
.decimal
= _nl_lookup (loc
, LC_NUMERIC
, DECIMAL_POINT
);
1111 tmp
.thousands_sep
= _nl_lookup (loc
, LC_NUMERIC
, THOUSANDS_SEP
);
1112 tmp
.decimal
= _nl_lookup (loc
, LC_NUMERIC
, DECIMAL_POINT
);
1115 tmp
.thousands_sep_length
= strlen (tmp
.thousands_sep
);
1116 if (tmp
.decimal
[1] == '\0' && tmp
.thousands_sep_length
<= 1
1119 /* Emit the the characters directly. This is only possible if the
1120 separators have length 1 (or 0 in case of thousands_sep). i18n
1121 digit translation still needs the full conversion. */
1122 __printf_fp_buffer_1 (buf
, loc
,
1123 tmp
.thousands_sep
[0], tmp
.decimal
[0],
1124 tmp
.thousands_sep_length
,
1129 tmp
.decimal_point_bytes
= strlen (tmp
.decimal
);
1132 tmp
.ctype
= loc
->__locales
[LC_CTYPE
];
1137 __printf_buffer_init (&tmp
.base
, tmp
.untranslated
, sizeof (tmp
.untranslated
),
1138 __printf_buffer_mode_fp
);
1139 __printf_fp_buffer_1 (&tmp
.base
, loc
, ',', '.',
1140 tmp
.thousands_sep_length
, info
, args
);
1141 if (__printf_buffer_has_failed (&tmp
.base
))
1143 __printf_buffer_mark_failed (tmp
.next
);
1146 __printf_buffer_flush_fp (&tmp
);
1149 /* The wide version is implemented on top of the multibyte version using
1152 struct __printf_buffer_fp_to_wide
1154 struct __printf_buffer base
;
1155 wchar_t thousands_sep_wc
;
1157 struct __wprintf_buffer
*next
;
1159 /* Activates outdigit translation if not NULL. */
1160 struct __locale_data
*ctype
;
1162 char untranslated
[PRINTF_BUFFER_SIZE_DIGITS
];
1166 __printf_buffer_flush_fp_to_wide (struct __printf_buffer_fp_to_wide
*buf
)
1168 /* No need to update buf->base.written; the actual count is
1169 maintained in buf->next->written. */
1170 for (char *p
= buf
->untranslated
; p
< buf
->base
.write_ptr
; ++p
)
1172 /* wchar_t overlaps with char in the ASCII range. */
1176 ch
= buf
->thousands_sep_wc
;
1180 else if (ch
== L
'.')
1181 ch
= buf
->decimalwc
;
1182 else if (buf
->ctype
!= NULL
&& L
'0' <= ch
&& ch
<= L
'9')
1183 ch
= buf
->ctype
->values
[_NL_ITEM_INDEX (_NL_CTYPE_OUTDIGIT0_WC
)
1185 __wprintf_buffer_putc (buf
->next
, ch
);
1188 if (!__wprintf_buffer_has_failed (buf
->next
))
1189 buf
->base
.write_ptr
= buf
->untranslated
;
1191 __printf_buffer_mark_failed (&buf
->base
);
1195 __wprintf_fp_l_buffer (struct __wprintf_buffer
*buf
, locale_t loc
,
1196 const struct printf_info
*info
,
1197 const void *const *args
)
1199 struct __printf_buffer_fp_to_wide tmp
;
1202 tmp
.decimalwc
= _nl_lookup_word (loc
, LC_MONETARY
,
1203 _NL_MONETARY_DECIMAL_POINT_WC
);
1204 tmp
.thousands_sep_wc
= _nl_lookup_word (loc
, LC_MONETARY
,
1205 _NL_MONETARY_THOUSANDS_SEP_WC
);
1206 if (tmp
.decimalwc
== 0)
1207 tmp
.decimalwc
= _nl_lookup_word (loc
, LC_NUMERIC
,
1208 _NL_NUMERIC_DECIMAL_POINT_WC
);
1212 tmp
.decimalwc
= _nl_lookup_word (loc
, LC_NUMERIC
,
1213 _NL_NUMERIC_DECIMAL_POINT_WC
);
1214 tmp
.thousands_sep_wc
= _nl_lookup_word (loc
, LC_NUMERIC
,
1215 _NL_NUMERIC_THOUSANDS_SEP_WC
);
1219 tmp
.ctype
= loc
->__locales
[LC_CTYPE
];
1224 __printf_buffer_init (&tmp
.base
, tmp
.untranslated
, sizeof (tmp
.untranslated
),
1225 __printf_buffer_mode_fp_to_wide
);
1226 __printf_fp_buffer_1 (&tmp
.base
, loc
, ',', '.', 1, info
, args
);
1227 if (__printf_buffer_has_failed (&tmp
.base
))
1229 __wprintf_buffer_mark_failed (tmp
.next
);
1232 __printf_buffer_flush (&tmp
.base
);
1236 ___printf_fp (FILE *fp
, const struct printf_info
*info
,
1237 const void *const *args
)
1241 struct __wprintf_buffer_to_file buf
;
1242 __wprintf_buffer_to_file_init (&buf
, fp
);
1243 __wprintf_fp_l_buffer (&buf
.base
, _NL_CURRENT_LOCALE
, info
, args
);
1244 return __wprintf_buffer_to_file_done (&buf
);
1248 struct __printf_buffer_to_file buf
;
1249 __printf_buffer_to_file_init (&buf
, fp
);
1250 __printf_fp_l_buffer (&buf
.base
, _NL_CURRENT_LOCALE
, info
, args
);
1251 return __printf_buffer_to_file_done (&buf
);
1254 ldbl_hidden_def (___printf_fp
, __printf_fp
)
1255 ldbl_strong_alias (___printf_fp
, __printf_fp
)