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