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