| 1 | /* Floating point output for `printf'. |
| 2 | Copyright (C) 1995-2023 Free Software Foundation, Inc. |
| 3 | |
| 4 | This file is part of the GNU C Library. |
| 5 | |
| 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. |
| 10 | |
| 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. |
| 15 | |
| 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/>. */ |
| 19 | |
| 20 | /* The gmp headers need some configuration frobs. */ |
| 21 | #define HAVE_ALLOCA 1 |
| 22 | |
| 23 | #include <array_length.h> |
| 24 | #include <libioP.h> |
| 25 | #include <alloca.h> |
| 26 | #include <ctype.h> |
| 27 | #include <float.h> |
| 28 | #include <gmp-mparam.h> |
| 29 | #include <gmp.h> |
| 30 | #include <ieee754.h> |
| 31 | #include <stdlib/gmp-impl.h> |
| 32 | #include <stdlib/longlong.h> |
| 33 | #include <stdlib/fpioconst.h> |
| 34 | #include <locale/localeinfo.h> |
| 35 | #include <limits.h> |
| 36 | #include <math.h> |
| 37 | #include <printf.h> |
| 38 | #include <string.h> |
| 39 | #include <unistd.h> |
| 40 | #include <stdlib.h> |
| 41 | #include <wchar.h> |
| 42 | #include <stdbool.h> |
| 43 | #include <rounding-mode.h> |
| 44 | #include <printf_buffer.h> |
| 45 | #include <printf_buffer_to_file.h> |
| 46 | #include <grouping_iterator.h> |
| 47 | |
| 48 | #include <assert.h> |
| 49 | |
| 50 | /* We use the GNU MP library to handle large numbers. |
| 51 | |
| 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 |
| 56 | |
| 57 | #define MPN_ASSIGN(dst,src) \ |
| 58 | memcpy (dst, src, (dst##size = src##size) * sizeof (mp_limb_t)) |
| 59 | #define MPN_GE(u,v) \ |
| 60 | (u##size > v##size || (u##size == v##size && __mpn_cmp (u, v, u##size) >= 0)) |
| 61 | |
| 62 | extern mp_size_t (mp_ptr res_ptr, mp_size_t size, |
| 63 | int *expt, int *is_neg, |
| 64 | double value); |
| 65 | extern mp_size_t (mp_ptr res_ptr, mp_size_t size, |
| 66 | int *expt, int *is_neg, |
| 67 | long double value); |
| 68 | |
| 69 | |
| 70 | struct hack_digit_param |
| 71 | { |
| 72 | /* Sign of the exponent. */ |
| 73 | int expsign; |
| 74 | /* The type of output format that will be used: 'e'/'E' or 'f'. */ |
| 75 | int type; |
| 76 | /* and the exponent. */ |
| 77 | int exponent; |
| 78 | /* The fraction of the floting-point value in question */ |
| 79 | MPN_VAR(frac); |
| 80 | /* Scaling factor. */ |
| 81 | MPN_VAR(scale); |
| 82 | /* Temporary bignum value. */ |
| 83 | MPN_VAR(tmp); |
| 84 | }; |
| 85 | |
| 86 | static char |
| 87 | hack_digit (struct hack_digit_param *p) |
| 88 | { |
| 89 | mp_limb_t hi; |
| 90 | |
| 91 | if (p->expsign != 0 && p->type == 'f' && p->exponent-- > 0) |
| 92 | hi = 0; |
| 93 | else if (p->scalesize == 0) |
| 94 | { |
| 95 | hi = p->frac[p->fracsize - 1]; |
| 96 | p->frac[p->fracsize - 1] = __mpn_mul_1 (p->frac, p->frac, |
| 97 | p->fracsize - 1, 10); |
| 98 | } |
| 99 | else |
| 100 | { |
| 101 | if (p->fracsize < p->scalesize) |
| 102 | hi = 0; |
| 103 | else |
| 104 | { |
| 105 | hi = mpn_divmod (p->tmp, p->frac, p->fracsize, |
| 106 | p->scale, p->scalesize); |
| 107 | p->tmp[p->fracsize - p->scalesize] = hi; |
| 108 | hi = p->tmp[0]; |
| 109 | |
| 110 | p->fracsize = p->scalesize; |
| 111 | while (p->fracsize != 0 && p->frac[p->fracsize - 1] == 0) |
| 112 | --p->fracsize; |
| 113 | if (p->fracsize == 0) |
| 114 | { |
| 115 | /* We're not prepared for an mpn variable with zero |
| 116 | limbs. */ |
| 117 | p->fracsize = 1; |
| 118 | return '0' + hi; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | mp_limb_t _cy = __mpn_mul_1 (p->frac, p->frac, p->fracsize, 10); |
| 123 | if (_cy != 0) |
| 124 | p->frac[p->fracsize++] = _cy; |
| 125 | } |
| 126 | |
| 127 | return '0' + hi; |
| 128 | } |
| 129 | |
| 130 | /* Version that performs grouping (if INFO->group && THOUSANDS_SEP != 0), |
| 131 | but not i18n digit translation. |
| 132 | |
| 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. */ |
| 137 | static void |
| 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) |
| 143 | { |
| 144 | /* The floating-point value to output. */ |
| 145 | union |
| 146 | { |
| 147 | double dbl; |
| 148 | long double ldbl; |
| 149 | #if __HAVE_DISTINCT_FLOAT128 |
| 150 | _Float128 f128; |
| 151 | #endif |
| 152 | } |
| 153 | fpnum; |
| 154 | |
| 155 | /* "NaN" or "Inf" for the special cases. */ |
| 156 | const char *special = NULL; |
| 157 | |
| 158 | /* Used to determine grouping rules. */ |
| 159 | int lc_category = info->extra ? LC_MONETARY : LC_NUMERIC; |
| 160 | |
| 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 |
| 165 | #else |
| 166 | # define GREATER_MANT_DIG LDBL_MANT_DIG |
| 167 | #endif |
| 168 | /* We need just a few limbs for the input before shifting to the right |
| 169 | position. */ |
| 170 | mp_limb_t fp_input[(GREATER_MANT_DIG + BITS_PER_MP_LIMB - 1) |
| 171 | / BITS_PER_MP_LIMB]; |
| 172 | /* We need to shift the contents of fp_input by this amount of bits. */ |
| 173 | int to_shift = 0; |
| 174 | |
| 175 | struct hack_digit_param p; |
| 176 | /* Sign of float number. */ |
| 177 | int is_neg = 0; |
| 178 | |
| 179 | /* General helper (carry limb). */ |
| 180 | mp_limb_t cy; |
| 181 | |
| 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; |
| 186 | |
| 187 | p.expsign = 0; |
| 188 | |
| 189 | #define PRINTF_FP_FETCH(FLOAT, VAR, SUFFIX, MANT_DIG) \ |
| 190 | { \ |
| 191 | (VAR) = *(const FLOAT *) args[0]; \ |
| 192 | \ |
| 193 | /* Check for special values: not a number or infinity. */ \ |
| 194 | if (isnan (VAR)) \ |
| 195 | { \ |
| 196 | is_neg = signbit (VAR); \ |
| 197 | if (isupper (info->spec)) \ |
| 198 | special = "NAN"; \ |
| 199 | else \ |
| 200 | special = "nan"; \ |
| 201 | } \ |
| 202 | else if (isinf (VAR)) \ |
| 203 | { \ |
| 204 | is_neg = signbit (VAR); \ |
| 205 | if (isupper (info->spec)) \ |
| 206 | special = "INF"; \ |
| 207 | else \ |
| 208 | special = "inf"; \ |
| 209 | } \ |
| 210 | else \ |
| 211 | { \ |
| 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; \ |
| 216 | } \ |
| 217 | } |
| 218 | |
| 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) |
| 223 | else |
| 224 | #endif |
| 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) |
| 228 | else |
| 229 | #endif |
| 230 | PRINTF_FP_FETCH (double, fpnum.dbl, double, DBL_MANT_DIG) |
| 231 | |
| 232 | #undef PRINTF_FP_FETCH |
| 233 | |
| 234 | if (special) |
| 235 | { |
| 236 | int width = info->width; |
| 237 | |
| 238 | if (is_neg || info->showsign || info->space) |
| 239 | --width; |
| 240 | width -= 3; |
| 241 | |
| 242 | if (!info->left) |
| 243 | __printf_buffer_pad (buf, ' ', width); |
| 244 | |
| 245 | if (is_neg) |
| 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, ' '); |
| 251 | |
| 252 | __printf_buffer_puts (buf, special); |
| 253 | |
| 254 | if (info->left) |
| 255 | __printf_buffer_pad (buf, ' ', width); |
| 256 | |
| 257 | return; |
| 258 | } |
| 259 | |
| 260 | |
| 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. */ |
| 265 | { |
| 266 | mp_size_t bignum_size = ((abs (p.exponent) + BITS_PER_MP_LIMB - 1) |
| 267 | / BITS_PER_MP_LIMB |
| 268 | + (GREATER_MANT_DIG / BITS_PER_MP_LIMB > 2 |
| 269 | ? 8 : 4)) |
| 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); |
| 274 | } |
| 275 | |
| 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 |
| 278 | for the other. */ |
| 279 | p.scalesize = 0; |
| 280 | if (p.exponent > 2) |
| 281 | { |
| 282 | /* |FP| >= 8.0. */ |
| 283 | int scaleexpo = 0; |
| 284 | int explog; |
| 285 | #if __HAVE_DISTINCT_FLOAT128 |
| 286 | if (info->is_binary128) |
| 287 | explog = FLT128_MAX_10_EXP_LOG; |
| 288 | else |
| 289 | explog = LDBL_MAX_10_EXP_LOG; |
| 290 | #else |
| 291 | explog = LDBL_MAX_10_EXP_LOG; |
| 292 | #endif |
| 293 | int exp10 = 0; |
| 294 | const struct mp_power *powers = &_fpioconst_pow10[explog + 1]; |
| 295 | int cnt_h, cnt_l, i; |
| 296 | |
| 297 | if ((p.exponent + to_shift) % BITS_PER_MP_LIMB == 0) |
| 298 | { |
| 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; |
| 302 | } |
| 303 | else |
| 304 | { |
| 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; |
| 310 | if (cy) |
| 311 | p.frac[p.fracsize++] = cy; |
| 312 | } |
| 313 | MPN_ZERO (p.frac, (p.exponent + to_shift) / BITS_PER_MP_LIMB); |
| 314 | |
| 315 | assert (powers > &_fpioconst_pow10[0]); |
| 316 | do |
| 317 | { |
| 318 | --powers; |
| 319 | |
| 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) |
| 323 | { |
| 324 | if (p.scalesize == 0) |
| 325 | { |
| 326 | #if __HAVE_DISTINCT_FLOAT128 |
| 327 | if ((FLT128_MANT_DIG |
| 328 | > _FPIO_CONST_OFFSET * BITS_PER_MP_LIMB) |
| 329 | && info->is_binary128) |
| 330 | { |
| 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 |
| 342 | bigger too. */ |
| 343 | p.exponent += _FLT128_FPIO_CONST_SHIFT * BITS_PER_MP_LIMB; |
| 344 | } |
| 345 | else |
| 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) |
| 350 | { |
| 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 |
| 362 | bigger too. */ |
| 363 | p.exponent += _FPIO_CONST_SHIFT * BITS_PER_MP_LIMB; |
| 364 | } |
| 365 | else |
| 366 | #endif |
| 367 | { |
| 368 | p.tmpsize = powers->arraysize; |
| 369 | memcpy (p.tmp, &__tens[powers->arrayoff], |
| 370 | p.tmpsize * sizeof (mp_limb_t)); |
| 371 | } |
| 372 | } |
| 373 | else |
| 374 | { |
| 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; |
| 381 | if (cy == 0) |
| 382 | --p.tmpsize; |
| 383 | } |
| 384 | |
| 385 | if (MPN_GE (p.frac, p.tmp)) |
| 386 | { |
| 387 | int cnt; |
| 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; |
| 392 | } |
| 393 | } |
| 394 | --explog; |
| 395 | } |
| 396 | while (powers > &_fpioconst_pow10[0]); |
| 397 | p.exponent = exp10; |
| 398 | |
| 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). */ |
| 403 | if (p.scalesize > 0) |
| 404 | { |
| 405 | /* Determine minimum number of zero bits at the end of |
| 406 | both numbers. */ |
| 407 | for (i = 0; p.scale[i] == 0 && p.frac[i] == 0; i++) |
| 408 | ; |
| 409 | |
| 410 | /* Determine number of bits the scaling factor is misplaced. */ |
| 411 | count_leading_zeros (cnt_h, p.scale[p.scalesize - 1]); |
| 412 | |
| 413 | if (cnt_h == 0) |
| 414 | { |
| 415 | /* The highest bit of the scaling factor is already set. So |
| 416 | we only have to remove the trailing empty limbs. */ |
| 417 | if (i > 0) |
| 418 | { |
| 419 | MPN_COPY_INCR (p.scale, p.scale + i, p.scalesize - i); |
| 420 | p.scalesize -= i; |
| 421 | MPN_COPY_INCR (p.frac, p.frac + i, p.fracsize - i); |
| 422 | p.fracsize -= i; |
| 423 | } |
| 424 | } |
| 425 | else |
| 426 | { |
| 427 | if (p.scale[i] != 0) |
| 428 | { |
| 429 | count_trailing_zeros (cnt_l, p.scale[i]); |
| 430 | if (p.frac[i] != 0) |
| 431 | { |
| 432 | int cnt_l2; |
| 433 | count_trailing_zeros (cnt_l2, p.frac[i]); |
| 434 | if (cnt_l2 < cnt_l) |
| 435 | cnt_l = cnt_l2; |
| 436 | } |
| 437 | } |
| 438 | else |
| 439 | count_trailing_zeros (cnt_l, p.frac[i]); |
| 440 | |
| 441 | /* Now shift the numbers to their optimal position. */ |
| 442 | if (i == 0 && BITS_PER_MP_LIMB - cnt_h > cnt_l) |
| 443 | { |
| 444 | /* We cannot save any memory. So just roll both numbers |
| 445 | so that the scaling factor has its highest bit set. */ |
| 446 | |
| 447 | (void) __mpn_lshift (p.scale, p.scale, p.scalesize, cnt_h); |
| 448 | cy = __mpn_lshift (p.frac, p.frac, p.fracsize, cnt_h); |
| 449 | if (cy != 0) |
| 450 | p.frac[p.fracsize++] = cy; |
| 451 | } |
| 452 | else if (BITS_PER_MP_LIMB - cnt_h <= cnt_l) |
| 453 | { |
| 454 | /* We can save memory by removing the trailing zero limbs |
| 455 | and by packing the non-zero limbs which gain another |
| 456 | free one. */ |
| 457 | |
| 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; |
| 464 | } |
| 465 | else |
| 466 | { |
| 467 | /* We can only save the memory of the limbs which are zero. |
| 468 | The non-zero parts occupy the same number of limbs. */ |
| 469 | |
| 470 | (void) __mpn_rshift (p.scale, p.scale + (i - 1), |
| 471 | p.scalesize - (i - 1), |
| 472 | BITS_PER_MP_LIMB - cnt_h); |
| 473 | p.scalesize -= i; |
| 474 | (void) __mpn_rshift (p.frac, p.frac + (i - 1), |
| 475 | p.fracsize - (i - 1), |
| 476 | BITS_PER_MP_LIMB - cnt_h); |
| 477 | p.fracsize -= |
| 478 | p.frac[p.fracsize - (i - 1) - 1] == 0 ? i : i - 1; |
| 479 | } |
| 480 | } |
| 481 | } |
| 482 | } |
| 483 | else if (p.exponent < 0) |
| 484 | { |
| 485 | /* |FP| < 1.0. */ |
| 486 | int exp10 = 0; |
| 487 | int explog; |
| 488 | #if __HAVE_DISTINCT_FLOAT128 |
| 489 | if (info->is_binary128) |
| 490 | explog = FLT128_MAX_10_EXP_LOG; |
| 491 | else |
| 492 | explog = LDBL_MAX_10_EXP_LOG; |
| 493 | #else |
| 494 | explog = LDBL_MAX_10_EXP_LOG; |
| 495 | #endif |
| 496 | const struct mp_power *powers = &_fpioconst_pow10[explog + 1]; |
| 497 | |
| 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)); |
| 502 | |
| 503 | p.expsign = 1; |
| 504 | p.exponent = -p.exponent; |
| 505 | |
| 506 | assert (powers != &_fpioconst_pow10[0]); |
| 507 | do |
| 508 | { |
| 509 | --powers; |
| 510 | |
| 511 | if (p.exponent >= powers->m_expo) |
| 512 | { |
| 513 | int i, incr, cnt_h, cnt_l; |
| 514 | mp_limb_t topval[2]; |
| 515 | |
| 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, |
| 522 | p.frac, p.fracsize); |
| 523 | else |
| 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; |
| 528 | if (cy == 0) |
| 529 | --p.tmpsize; |
| 530 | |
| 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; |
| 534 | |
| 535 | assert (incr <= powers->p_expo); |
| 536 | |
| 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) |
| 541 | { |
| 542 | if (cnt_h <= BITS_PER_MP_LIMB - 4) |
| 543 | { |
| 544 | topval[0] = 0; |
| 545 | topval[1] |
| 546 | = ((mp_limb_t) 10) << (BITS_PER_MP_LIMB - 4 - cnt_h); |
| 547 | } |
| 548 | else |
| 549 | { |
| 550 | topval[0] = ((mp_limb_t) 10) << (BITS_PER_MP_LIMB - 4); |
| 551 | topval[1] = 0; |
| 552 | (void) __mpn_lshift (topval, topval, 2, |
| 553 | BITS_PER_MP_LIMB - cnt_h); |
| 554 | } |
| 555 | } |
| 556 | |
| 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])))) |
| 567 | { |
| 568 | /* The factor is right. Adapt binary and decimal |
| 569 | exponents. */ |
| 570 | p.exponent -= incr; |
| 571 | exp10 |= 1 << explog; |
| 572 | |
| 573 | /* If this factor yields a number greater or equal to |
| 574 | 1.0, we must not shift the non-fractional digits down. */ |
| 575 | if (p.exponent < 0) |
| 576 | cnt_h += -p.exponent; |
| 577 | |
| 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) |
| 581 | { |
| 582 | MPN_COPY (p.frac, p.tmp + i, p.tmpsize - i); |
| 583 | p.fracsize = p.tmpsize - i; |
| 584 | } |
| 585 | else |
| 586 | { |
| 587 | count_trailing_zeros (cnt_l, p.tmp[i]); |
| 588 | |
| 589 | /* Now shift the numbers to their optimal position. */ |
| 590 | if (i == 0 && BITS_PER_MP_LIMB - 1 - cnt_h > cnt_l) |
| 591 | { |
| 592 | /* We cannot save any memory. Just roll the |
| 593 | number so that the leading digit is in a |
| 594 | separate limb. */ |
| 595 | |
| 596 | cy = __mpn_lshift (p.frac, p.tmp, p.tmpsize, |
| 597 | cnt_h + 1); |
| 598 | p.fracsize = p.tmpsize + 1; |
| 599 | p.frac[p.fracsize - 1] = cy; |
| 600 | } |
| 601 | else if (BITS_PER_MP_LIMB - 1 - cnt_h <= cnt_l) |
| 602 | { |
| 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; |
| 606 | } |
| 607 | else |
| 608 | { |
| 609 | /* We can only save the memory of the limbs which |
| 610 | are zero. The non-zero parts occupy the same |
| 611 | number of limbs. */ |
| 612 | |
| 613 | (void) __mpn_rshift (p.frac, p.tmp + (i - 1), |
| 614 | p.tmpsize - (i - 1), |
| 615 | BITS_PER_MP_LIMB - 1 - cnt_h); |
| 616 | p.fracsize = p.tmpsize - (i - 1); |
| 617 | } |
| 618 | } |
| 619 | } |
| 620 | } |
| 621 | --explog; |
| 622 | } |
| 623 | while (powers != &_fpioconst_pow10[1] && p.exponent > 0); |
| 624 | /* All factors but 10^-1 are tested now. */ |
| 625 | if (p.exponent > 0) |
| 626 | { |
| 627 | int cnt_l; |
| 628 | |
| 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); |
| 632 | |
| 633 | count_trailing_zeros (cnt_l, p.tmp[0]); |
| 634 | if (cnt_l < MIN (4, p.exponent)) |
| 635 | { |
| 636 | cy = __mpn_lshift (p.frac, p.tmp, p.tmpsize, |
| 637 | BITS_PER_MP_LIMB - MIN (4, p.exponent)); |
| 638 | if (cy != 0) |
| 639 | p.frac[p.tmpsize++] = cy; |
| 640 | } |
| 641 | else |
| 642 | (void) __mpn_rshift (p.frac, p.tmp, p.tmpsize, MIN (4, p.exponent)); |
| 643 | p.fracsize = p.tmpsize; |
| 644 | exp10 |= 1; |
| 645 | assert (p.frac[p.fracsize - 1] < 10); |
| 646 | } |
| 647 | p.exponent = exp10; |
| 648 | } |
| 649 | else |
| 650 | { |
| 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); |
| 657 | |
| 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; |
| 661 | p.exponent = 0; |
| 662 | } |
| 663 | |
| 664 | { |
| 665 | int width = info->width; |
| 666 | char *wstartp, *wcp; |
| 667 | size_t chars_needed; |
| 668 | int expscale; |
| 669 | int intdig_max, intdig_no = 0; |
| 670 | int fracdig_min; |
| 671 | int fracdig_max; |
| 672 | int dig_max; |
| 673 | int significant; |
| 674 | char spec = _tolower (info->spec); |
| 675 | |
| 676 | if (spec == 'e') |
| 677 | { |
| 678 | p.type = info->spec; |
| 679 | intdig_max = 1; |
| 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. */ |
| 685 | } |
| 686 | else if (spec == 'f') |
| 687 | { |
| 688 | p.type = '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. */ |
| 692 | if (p.expsign == 0) |
| 693 | { |
| 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; |
| 697 | } |
| 698 | else |
| 699 | { |
| 700 | intdig_max = 1; |
| 701 | chars_needed = 1 + 1 + (size_t) fracdig_max; |
| 702 | } |
| 703 | } |
| 704 | else |
| 705 | { |
| 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)) |
| 709 | { |
| 710 | if ('g' - 'G' == 'e' - 'E') |
| 711 | p.type = 'E' + (info->spec - 'G'); |
| 712 | else |
| 713 | p.type = isupper (info->spec) ? 'E' : 'e'; |
| 714 | fracdig_max = dig_max - 1; |
| 715 | intdig_max = 1; |
| 716 | chars_needed = 1 + 1 + (size_t) fracdig_max + 1 + 1 + 4; |
| 717 | } |
| 718 | else |
| 719 | { |
| 720 | p.type = 'f'; |
| 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; |
| 729 | } |
| 730 | fracdig_min = info->alt ? fracdig_max : 0; |
| 731 | significant = 0; /* We count significant digits. */ |
| 732 | } |
| 733 | |
| 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)) |
| 740 | { |
| 741 | /* Some overflow occurred. */ |
| 742 | __set_errno (ERANGE); |
| 743 | __printf_buffer_mark_failed (buf); |
| 744 | return; |
| 745 | } |
| 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)) |
| 749 | { |
| 750 | wbuffer = malloc (wbuffer_to_alloc); |
| 751 | if (wbuffer == NULL) |
| 752 | { |
| 753 | /* Signal an error to the caller. */ |
| 754 | __printf_buffer_mark_failed (buf); |
| 755 | return; |
| 756 | } |
| 757 | } |
| 758 | else |
| 759 | wbuffer = alloca (wbuffer_to_alloc); |
| 760 | wcp = wstartp = wbuffer + 2; /* Let room for rounding. */ |
| 761 | |
| 762 | /* Do the real work: put digits in allocated buffer. */ |
| 763 | if (p.expsign == 0 || p.type != 'f') |
| 764 | { |
| 765 | assert (p.expsign == 0 || intdig_max == 1); |
| 766 | while (intdig_no < intdig_max) |
| 767 | { |
| 768 | ++intdig_no; |
| 769 | *wcp++ = hack_digit (&p); |
| 770 | } |
| 771 | significant = 1; |
| 772 | if (info->alt |
| 773 | || fracdig_min > 0 |
| 774 | || (fracdig_max > 0 && (p.fracsize > 1 || p.frac[0] != 0))) |
| 775 | *wcp++ = decimal; |
| 776 | } |
| 777 | else |
| 778 | { |
| 779 | /* |fp| < 1.0 and the selected p.type is 'f', so put "0." |
| 780 | in the buffer. */ |
| 781 | *wcp++ = '0'; |
| 782 | --p.exponent; |
| 783 | *wcp++ = decimal; |
| 784 | } |
| 785 | |
| 786 | /* Generate the needed number of fractional digits. */ |
| 787 | int fracdig_no = 0; |
| 788 | int added_zeros = 0; |
| 789 | while (fracdig_no < fracdig_min + added_zeros |
| 790 | || (fracdig_no < fracdig_max && (p.fracsize > 1 || p.frac[0] != 0))) |
| 791 | { |
| 792 | ++fracdig_no; |
| 793 | *wcp = hack_digit (&p); |
| 794 | if (*wcp++ != '0') |
| 795 | significant = 1; |
| 796 | else if (significant == 0) |
| 797 | { |
| 798 | ++fracdig_max; |
| 799 | if (fracdig_min > 0) |
| 800 | ++added_zeros; |
| 801 | } |
| 802 | } |
| 803 | |
| 804 | /* Do rounding. */ |
| 805 | char last_digit = wcp[-1] != decimal ? wcp[-1] : wcp[-2]; |
| 806 | char next_digit = hack_digit (&p); |
| 807 | bool more_bits; |
| 808 | if (next_digit != '0' && next_digit != '5') |
| 809 | more_bits = true; |
| 810 | else if (p.fracsize == 1 && p.frac[0] == 0) |
| 811 | /* Rest of the number is zero. */ |
| 812 | more_bits = false; |
| 813 | else if (p.scalesize == 0) |
| 814 | { |
| 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) |
| 819 | --lcnt; |
| 820 | more_bits = lcnt > 0; |
| 821 | } |
| 822 | else |
| 823 | more_bits = true; |
| 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)) |
| 827 | { |
| 828 | char *wtp = wcp; |
| 829 | |
| 830 | if (fracdig_no > 0) |
| 831 | { |
| 832 | /* Process fractional digits. Terminate if not rounded or |
| 833 | radix character is reached. */ |
| 834 | int removed = 0; |
| 835 | while (*--wtp != decimal && *wtp == '9') |
| 836 | { |
| 837 | *wtp = '0'; |
| 838 | ++removed; |
| 839 | } |
| 840 | if (removed == fracdig_min && added_zeros > 0) |
| 841 | --added_zeros; |
| 842 | if (*wtp != decimal) |
| 843 | /* Round up. */ |
| 844 | (*wtp)++; |
| 845 | else if (__builtin_expect (spec == 'g' && p.type == 'f' && info->alt |
| 846 | && wtp == wstartp + 1 |
| 847 | && wstartp[0] == '0', |
| 848 | 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.". */ |
| 852 | --added_zeros; |
| 853 | } |
| 854 | |
| 855 | if (fracdig_no == 0 || *wtp == decimal) |
| 856 | { |
| 857 | /* Round the integer digits. */ |
| 858 | if (*(wtp - 1) == decimal) |
| 859 | --wtp; |
| 860 | |
| 861 | while (--wtp >= wstartp && *wtp == '9') |
| 862 | *wtp = '0'; |
| 863 | |
| 864 | if (wtp >= wstartp) |
| 865 | /* Round up. */ |
| 866 | (*wtp)++; |
| 867 | else |
| 868 | /* It is more critical. All digits were 9's. */ |
| 869 | { |
| 870 | if (p.type != 'f') |
| 871 | { |
| 872 | *wstartp = '1'; |
| 873 | p.exponent += p.expsign == 0 ? 1 : -1; |
| 874 | |
| 875 | /* The above p.exponent adjustment could lead to 1.0e-00, |
| 876 | e.g. for 0.999999999. Make sure p.exponent 0 always |
| 877 | uses + sign. */ |
| 878 | if (p.exponent == 0) |
| 879 | p.expsign = 0; |
| 880 | } |
| 881 | else if (intdig_no == dig_max) |
| 882 | { |
| 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; |
| 887 | *--wstartp = '1'; |
| 888 | |
| 889 | if (info->alt || fracdig_no > 0) |
| 890 | { |
| 891 | /* Overwrite the old radix character. */ |
| 892 | wstartp[intdig_no + 2] = '0'; |
| 893 | ++fracdig_no; |
| 894 | } |
| 895 | |
| 896 | fracdig_no += intdig_no; |
| 897 | intdig_no = 1; |
| 898 | fracdig_max = intdig_max - intdig_no; |
| 899 | ++p.exponent; |
| 900 | /* Now we must print the p.exponent. */ |
| 901 | p.type = isupper (info->spec) ? 'E' : 'e'; |
| 902 | } |
| 903 | else |
| 904 | { |
| 905 | /* We can simply add another another digit before the |
| 906 | radix. */ |
| 907 | *--wstartp = '1'; |
| 908 | ++intdig_no; |
| 909 | } |
| 910 | |
| 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) |
| 915 | { |
| 916 | wcp -= intdig_no + fracdig_no - dig_max; |
| 917 | fracdig_no -= intdig_no + fracdig_no - dig_max; |
| 918 | } |
| 919 | } |
| 920 | } |
| 921 | } |
| 922 | |
| 923 | /* Now remove unnecessary '0' at the end of the string. */ |
| 924 | while (fracdig_no > fracdig_min + added_zeros && *(wcp - 1) == '0') |
| 925 | { |
| 926 | --wcp; |
| 927 | --fracdig_no; |
| 928 | } |
| 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) |
| 932 | --wcp; |
| 933 | |
| 934 | /* Write the p.exponent if it is needed. */ |
| 935 | if (p.type != 'f') |
| 936 | { |
| 937 | if (__glibc_unlikely (p.expsign != 0 && p.exponent == 4 && spec == 'g')) |
| 938 | { |
| 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) |
| 947 | { |
| 948 | memset (wstartp + 6, '0', wcp - (wstartp + 2)); |
| 949 | wcp += 4; |
| 950 | } |
| 951 | else |
| 952 | wcp += 5; |
| 953 | } |
| 954 | else |
| 955 | { |
| 956 | *wcp++ = p.type; |
| 957 | *wcp++ = p.expsign ? '-' : '+'; |
| 958 | |
| 959 | /* Find the magnitude of the p.exponent. */ |
| 960 | expscale = 10; |
| 961 | while (expscale <= p.exponent) |
| 962 | expscale *= 10; |
| 963 | |
| 964 | if (p.exponent < 10) |
| 965 | /* Exponent always has at least two digits. */ |
| 966 | *wcp++ = '0'; |
| 967 | else |
| 968 | do |
| 969 | { |
| 970 | expscale /= 10; |
| 971 | *wcp++ = '0' + (p.exponent / expscale); |
| 972 | p.exponent %= expscale; |
| 973 | } |
| 974 | while (expscale > 10); |
| 975 | *wcp++ = '0' + p.exponent; |
| 976 | } |
| 977 | } |
| 978 | |
| 979 | struct grouping_iterator iter; |
| 980 | if (thousands_sep != '\0' && info->group) |
| 981 | __grouping_iterator_init (&iter, lc_category, loc, intdig_no); |
| 982 | else |
| 983 | iter.separators = 0; |
| 984 | |
| 985 | /* Compute number of characters which must be filled with the padding |
| 986 | character. */ |
| 987 | if (is_neg || info->showsign || info->space) |
| 988 | --width; |
| 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; |
| 995 | |
| 996 | if (!info->left && info->pad != '0') |
| 997 | __printf_buffer_pad (buf, info->pad, width); |
| 998 | |
| 999 | if (is_neg) |
| 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, ' '); |
| 1005 | |
| 1006 | if (!info->left && info->pad == '0') |
| 1007 | __printf_buffer_pad (buf, '0', width); |
| 1008 | |
| 1009 | if (iter.separators > 0) |
| 1010 | { |
| 1011 | char *cp = wstartp; |
| 1012 | for (int i = 0; i < intdig_no; ++i) |
| 1013 | { |
| 1014 | if (__grouping_iterator_next (&iter)) |
| 1015 | __printf_buffer_putc (buf, thousands_sep); |
| 1016 | __printf_buffer_putc (buf, *cp); |
| 1017 | ++cp; |
| 1018 | } |
| 1019 | __printf_buffer_write (buf, cp, wcp - cp); |
| 1020 | } |
| 1021 | else |
| 1022 | __printf_buffer_write (buf, wstartp, wcp - wstartp); |
| 1023 | |
| 1024 | if (info->left) |
| 1025 | __printf_buffer_pad (buf, info->pad, width); |
| 1026 | } |
| 1027 | |
| 1028 | if (buffer_malloced) |
| 1029 | free (wbuffer); |
| 1030 | } |
| 1031 | |
| 1032 | /* ASCII to localization translation. Multibyte version. */ |
| 1033 | struct __printf_buffer_fp |
| 1034 | { |
| 1035 | struct __printf_buffer base; |
| 1036 | |
| 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; |
| 1042 | |
| 1043 | /* Buffer to write to. */ |
| 1044 | struct __printf_buffer *next; |
| 1045 | |
| 1046 | /* Activates outdigit translation if not NULL. */ |
| 1047 | struct __locale_data *ctype; |
| 1048 | |
| 1049 | /* Buffer to which the untranslated ASCII digits are written. */ |
| 1050 | char untranslated[PRINTF_BUFFER_SIZE_DIGITS]; |
| 1051 | }; |
| 1052 | |
| 1053 | /* Multibyte buffer-to-buffer flush function with full translation. */ |
| 1054 | void |
| 1055 | __printf_buffer_flush_fp (struct __printf_buffer_fp *buf) |
| 1056 | { |
| 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) |
| 1060 | { |
| 1061 | char ch = *p; |
| 1062 | const char *replacement = NULL; |
| 1063 | unsigned int replacement_bytes; |
| 1064 | if (ch == ',') |
| 1065 | { |
| 1066 | replacement = buf->thousands_sep; |
| 1067 | replacement_bytes = buf->thousands_sep_length; |
| 1068 | } |
| 1069 | else if (ch == '.') |
| 1070 | { |
| 1071 | replacement = buf->decimal; |
| 1072 | replacement_bytes = buf->decimal_point_bytes; |
| 1073 | } |
| 1074 | else if (buf->ctype != NULL && '0' <= ch && ch <= '9') |
| 1075 | { |
| 1076 | int digit = ch - '0'; |
| 1077 | replacement |
| 1078 | = buf->ctype->values[_NL_ITEM_INDEX (_NL_CTYPE_OUTDIGIT0_MB) |
| 1079 | + digit].string; |
| 1080 | struct lc_ctype_data *ctype = buf->ctype->private; |
| 1081 | replacement_bytes = ctype->outdigit_bytes[digit]; |
| 1082 | } |
| 1083 | if (replacement == NULL) |
| 1084 | __printf_buffer_putc (buf->next, ch); |
| 1085 | else |
| 1086 | __printf_buffer_write (buf->next, replacement, replacement_bytes); |
| 1087 | } |
| 1088 | |
| 1089 | if (!__printf_buffer_has_failed (buf->next)) |
| 1090 | buf->base.write_ptr = buf->untranslated; |
| 1091 | else |
| 1092 | __printf_buffer_mark_failed (&buf->base); |
| 1093 | } |
| 1094 | |
| 1095 | void |
| 1096 | __printf_fp_l_buffer (struct __printf_buffer *buf, locale_t loc, |
| 1097 | const struct printf_info *info, |
| 1098 | const void *const *args) |
| 1099 | { |
| 1100 | struct __printf_buffer_fp tmp; |
| 1101 | |
| 1102 | if (info->extra) |
| 1103 | { |
| 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); |
| 1108 | } |
| 1109 | else |
| 1110 | { |
| 1111 | tmp.thousands_sep = _nl_lookup (loc, LC_NUMERIC, THOUSANDS_SEP); |
| 1112 | tmp.decimal = _nl_lookup (loc, LC_NUMERIC, DECIMAL_POINT); |
| 1113 | } |
| 1114 | |
| 1115 | tmp.thousands_sep_length = strlen (tmp.thousands_sep); |
| 1116 | if (tmp.decimal[1] == '\0' && tmp.thousands_sep_length <= 1 |
| 1117 | && !info->i18n) |
| 1118 | { |
| 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, |
| 1125 | info, args); |
| 1126 | return; |
| 1127 | } |
| 1128 | |
| 1129 | tmp.decimal_point_bytes = strlen (tmp.decimal); |
| 1130 | |
| 1131 | if (info->i18n) |
| 1132 | tmp.ctype = loc->__locales[LC_CTYPE]; |
| 1133 | else |
| 1134 | tmp.ctype = NULL; |
| 1135 | tmp.next = buf; |
| 1136 | |
| 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)) |
| 1142 | { |
| 1143 | __printf_buffer_mark_failed (tmp.next); |
| 1144 | return; |
| 1145 | } |
| 1146 | __printf_buffer_flush_fp (&tmp); |
| 1147 | } |
| 1148 | |
| 1149 | /* The wide version is implemented on top of the multibyte version using |
| 1150 | translation. */ |
| 1151 | |
| 1152 | struct __printf_buffer_fp_to_wide |
| 1153 | { |
| 1154 | struct __printf_buffer base; |
| 1155 | wchar_t thousands_sep_wc; |
| 1156 | wchar_t decimalwc; |
| 1157 | struct __wprintf_buffer *next; |
| 1158 | |
| 1159 | /* Activates outdigit translation if not NULL. */ |
| 1160 | struct __locale_data *ctype; |
| 1161 | |
| 1162 | char untranslated[PRINTF_BUFFER_SIZE_DIGITS]; |
| 1163 | }; |
| 1164 | |
| 1165 | void |
| 1166 | __printf_buffer_flush_fp_to_wide (struct __printf_buffer_fp_to_wide *buf) |
| 1167 | { |
| 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) |
| 1171 | { |
| 1172 | /* wchar_t overlaps with char in the ASCII range. */ |
| 1173 | wchar_t ch = *p; |
| 1174 | if (ch == L',') |
| 1175 | { |
| 1176 | ch = buf->thousands_sep_wc; |
| 1177 | if (ch == 0) |
| 1178 | continue; |
| 1179 | } |
| 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) |
| 1184 | + ch - L'0'].word; |
| 1185 | __wprintf_buffer_putc (buf->next, ch); |
| 1186 | } |
| 1187 | |
| 1188 | if (!__wprintf_buffer_has_failed (buf->next)) |
| 1189 | buf->base.write_ptr = buf->untranslated; |
| 1190 | else |
| 1191 | __printf_buffer_mark_failed (&buf->base); |
| 1192 | } |
| 1193 | |
| 1194 | void |
| 1195 | __wprintf_fp_l_buffer (struct __wprintf_buffer *buf, locale_t loc, |
| 1196 | const struct printf_info *info, |
| 1197 | const void *const *args) |
| 1198 | { |
| 1199 | struct __printf_buffer_fp_to_wide tmp; |
| 1200 | if (info->extra) |
| 1201 | { |
| 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); |
| 1209 | } |
| 1210 | else |
| 1211 | { |
| 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); |
| 1216 | } |
| 1217 | |
| 1218 | if (info->i18n) |
| 1219 | tmp.ctype = loc->__locales[LC_CTYPE]; |
| 1220 | else |
| 1221 | tmp.ctype = NULL; |
| 1222 | tmp.next = buf; |
| 1223 | |
| 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)) |
| 1228 | { |
| 1229 | __wprintf_buffer_mark_failed (tmp.next); |
| 1230 | return; |
| 1231 | } |
| 1232 | __printf_buffer_flush (&tmp.base); |
| 1233 | } |
| 1234 | |
| 1235 | int |
| 1236 | ___printf_fp (FILE *fp, const struct printf_info *info, |
| 1237 | const void *const *args) |
| 1238 | { |
| 1239 | if (info->wide) |
| 1240 | { |
| 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); |
| 1245 | } |
| 1246 | else |
| 1247 | { |
| 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); |
| 1252 | } |
| 1253 | } |
| 1254 | ldbl_hidden_def (___printf_fp, __printf_fp) |
| 1255 | ldbl_strong_alias (___printf_fp, __printf_fp) |
| 1256 | |