2 * Copyright (C) 2000,2001,2003,2004 Manuel Novoa III <mjn3@codepoet.org>
4 * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
6 * Dedicated to Toni. See uClibc/DEDICATION.mjn3 for details.
13 #include "_fpmaxtostr.h"
18 * ssize_t _fpmaxtostr(FILE * fp, __fpmax_t x, struct printf_info *info,
19 * __fp_outfunc_t fp_outfunc);
21 * This is derived from the old _dtostr, whic I wrote for uClibc to provide
22 * floating point support for the printf functions. It handles +/- infinity,
23 * nan, and signed 0 assuming you have ieee arithmetic. It also now handles
24 * digit grouping (for the uClibc supported locales) and hexadecimal float
25 * notation. Finally, via the fp_outfunc parameter, it now supports wide
30 * At most DECIMAL_DIG significant digits are kept. Any trailing digits
31 * are treated as 0 as they are really just the results of rounding noise
32 * anyway. If you want to do better, use an arbitary precision arithmetic
35 * It should also be fairly portable, as no assumptions are made about the
36 * bit-layout of doubles. Of course, that does make it less efficient than
40 /*****************************************************************************/
41 /* Don't change anything that follows unless you know what you're doing. */
42 /*****************************************************************************/
43 /* Fairly portable nan check. Bitwise for i386 generated larger code.
44 * If you have a better version, comment this out.
46 #define isnan(x) ((x) != (x))
48 /* Without seminumerical functions to examine the sign bit, this is
49 * about the best we can do to test for '-0'.
51 #define zeroisnegative(x) ((1./(x)) < 0)
53 /*****************************************************************************/
54 /* Don't change anything that follows peroid!!! ;-) */
55 /*****************************************************************************/
56 #ifdef __UCLIBC_HAS_HEXADECIMAL_FLOATS__
58 #error FLT_RADIX != 2 is not currently supported
60 #endif /* __UCLIBC_HAS_HEXADECIMAL_FLOATS__ */
62 #define NUM_HEX_DIGITS ((FPMAX_MANT_DIG + 3)/ 4)
64 #define HEX_DIGITS_PER_BLOCK 8
66 /* Maximum number of subcases to output double is...
68 * 1 - padding and initial digit
69 * 2 - digits left of the radix
70 * 3 - 0s left of the radix or radix
71 * 4 - radix or digits right of the radix
72 * 5 - 0s right of the radix
74 * 7 - trailing space padding
75 * although not all cases may occur.
79 /*****************************************************************************/
81 #define NUM_HEX_DIGIT_BLOCKS \
82 ((NUM_HEX_DIGITS+HEX_DIGITS_PER_BLOCK-1)/HEX_DIGITS_PER_BLOCK)
84 /*****************************************************************************/
86 static const char fmt[] = "inf\0INF\0nan\0NAN\0.\0,";
88 #define INF_OFFSET 0 /* must be 1st */
89 #define NAN_OFFSET 8 /* must be 2nd.. see hex sign handling */
90 #define DECPT_OFFSET 16
91 #define THOUSEP_OFFSET 18
93 #define EMPTY_STRING_OFFSET 3
95 /*****************************************************************************/
96 #if FPMAX_MAX_10_EXP < -FPMAX_MIN_10_EXP
97 #error scaling code can not handle FPMAX_MAX_10_EXP < -FPMAX_MIN_10_EXP
100 static const __fpmax_t exp10_table[] =
102 1e1L, 1e2L, 1e4L, 1e8L, 1e16L, 1e32L, /* floats */
103 #if FPMAX_MAX_10_EXP < 32
104 #error unsupported FPMAX_MAX_10_EXP (< 32). ANSI/ISO C requires >= 37.
106 #if FPMAX_MAX_10_EXP >= 64
109 #if FPMAX_MAX_10_EXP >= 128
112 #if FPMAX_MAX_10_EXP >= 256
115 #if FPMAX_MAX_10_EXP >= 512
118 #if FPMAX_MAX_10_EXP >= 1024
121 #if FPMAX_MAX_10_EXP >= 2048
124 #if FPMAX_MAX_10_EXP >= 4096
127 #if FPMAX_MAX_10_EXP >= 8192
128 #error unsupported FPMAX_MAX_10_EXP. please increase table
132 #define EXP10_TABLE_SIZE (sizeof(exp10_table)/sizeof(exp10_table[0]))
133 #define EXP10_TABLE_MAX (1U<<(EXP10_TABLE_SIZE-1))
135 /*****************************************************************************/
136 #ifdef __UCLIBC_HAS_HEXADECIMAL_FLOATS__
139 #error FLT_RADIX != 2 is not currently supported
142 #if FPMAX_MAX_EXP < -FPMAX_MIN_EXP
143 #error scaling code can not handle FPMAX_MAX_EXP < -FPMAX_MIN_EXP
146 static const __fpmax_t exp16_table[] = {
147 0x1.0p4L, 0x1.0p8L, 0x1.0p16L, 0x1.0p32L, 0x1.0p64L,
148 #if FPMAX_MAX_EXP >= 128
151 #if FPMAX_MAX_EXP >= 256
154 #if FPMAX_MAX_EXP >= 512
157 #if FPMAX_MAX_EXP >= 1024
160 #if FPMAX_MAX_EXP >= 2048
163 #if FPMAX_MAX_EXP >= 4096
166 #if FPMAX_MAX_EXP >= 8192
169 #if FPMAX_MAX_EXP >= 16384
172 #if FPMAX_MAX_EXP >= 32768
173 #error unsupported FPMAX_MAX_EXP. please increase table
177 #define EXP16_TABLE_SIZE (sizeof(exp16_table)/sizeof(exp16_table[0]))
178 #define EXP16_TABLE_MAX (1U<<(EXP16_TABLE_SIZE-1))
180 #endif /* __UCLIBC_HAS_HEXADECIMAL_FLOATS__ */
181 /*****************************************************************************/
183 #define FPO_ZERO_PAD (0x80 | '0')
184 #define FPO_STR_WIDTH (0x80 | ' ');
185 #define FPO_STR_PREC 'p'
187 ssize_t _fpmaxtostr(FILE * fp, __fpmax_t x, struct printf_info *info,
188 __fp_outfunc_t fp_outfunc)
190 #ifdef __UCLIBC_HAS_HEXADECIMAL_FLOATS__
192 __fpmax_t upper_bnd = 1e9;
193 #endif /* __UCLIBC_HAS_HEXADECIMAL_FLOATS__ */
194 #ifdef __UCLIBC_HAS_HEXADECIMAL_FLOATS__
195 uint_fast32_t base = 10;
196 const __fpmax_t *power_table;
197 int dpb = DIGITS_PER_BLOCK;
198 int ndb = NUM_DIGIT_BLOCKS;
199 int nd = DECIMAL_DIG;
200 int sufficient_precision = 0;
201 #endif /* __UCLIBC_HAS_HEXADECIMAL_FLOATS__ */
202 #ifdef __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__
204 int initial_group; /* This does not need to be initialized. */
205 int tslen; /* This does not need to be initialized. */
206 int nblk2; /* This does not need to be initialized. */
207 const char *ts; /* This does not need to be initialized. */
208 #endif /* __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__ */
215 intptr_t pc_fwi[3*MAX_CALLS];
218 #ifdef __UCLIBC_MJN3_ONLY__
219 #warning TODO: The size of exp_buf[] should really be determined by the float constants.
220 #endif /* __UCLIBC_MJN3_ONLY__ */
223 char sign_str[6]; /* Last 2 are for 1st digit + nul. */
233 if ((mode|0x20) == 'a') {
234 #ifdef __UCLIBC_HAS_HEXADECIMAL_FLOATS__
237 preci = NUM_HEX_DIGITS;
238 sufficient_precision = 1;
250 if (PRINT_INFO_FLAG_VAL(info,showsign)) {
252 } else if (PRINT_INFO_FLAG_VAL(info,space)) {
257 pc_fwi[5] = INF_OFFSET;
258 if (isnan(x)) { /* First, check for nan. */
259 pc_fwi[5] = NAN_OFFSET;
263 if (x == 0) { /* Handle 0 now to avoid false positive. */
264 #ifdef __UCLIBC_HAVE_SIGNED_ZERO__
265 if (zeroisnegative(x)) { /* Handle 'signed' zero. */
268 #endif /* __UCLIBC_HAVE_SIGNED_ZERO__ */
270 goto GENERATE_DIGITS;
273 if (x < 0) { /* Convert negatives to positives. */
278 if (__FPMAX_ZERO_OR_INF_CHECK(x)) { /* Inf since zero handled above. */
282 pc_fwi[3] = FPO_STR_PREC;
287 pc_fwi[5] = (intptr_t)(fmt + pc_fwi[5]);
293 #ifdef __UCLIBC_MJN3_ONLY__
294 #warning TODO: Clean up defines when hexadecimal float notation is unsupported.
295 #endif /* __UCLIBC_MJN3_ONLY__ */
297 #ifdef __UCLIBC_HAS_HEXADECIMAL_FLOATS__
299 if ((mode|0x20) == 'a') {
300 lower_bnd = 0x1.0p31L;
301 upper_bnd = 0x1.0p32L;
302 power_table = exp16_table;
303 exp = HEX_DIGITS_PER_BLOCK - 1;
304 i = EXP16_TABLE_SIZE;
306 dpb = HEX_DIGITS_PER_BLOCK;
307 ndb = NUM_HEX_DIGIT_BLOCKS;
312 /* upper_bnd = 1e9; */
313 power_table = exp10_table;
314 exp = DIGITS_PER_BLOCK - 1;
315 i = EXP10_TABLE_SIZE;
317 /* dpb = DIGITS_PER_BLOCK; */
318 /* ndb = NUM_DIGIT_BLOCKS; */
324 #else /* __UCLIBC_HAS_HEXADECIMAL_FLOATS__ */
326 #define lower_bnd 1e8
327 #define upper_bnd 1e9
328 #define power_table exp10_table
329 #define dpb DIGITS_PER_BLOCK
331 #define ndb NUM_DIGIT_BLOCKS
332 #define nd DECIMAL_DIG
334 exp = DIGITS_PER_BLOCK - 1;
335 i = EXP10_TABLE_SIZE;
338 #endif /* __UCLIBC_HAS_HEXADECIMAL_FLOATS__ */
342 if (x < lower_bnd) { /* Do we need to scale up or down? */
349 if (x * power_table[i] < upper_bnd) {
354 if (x / power_table[i] >= lower_bnd) {
363 if (x >= upper_bnd) { /* Handle bad rounding case. */
367 assert(x < upper_bnd);
372 s = buf + 2; /* Leave space for '\0' and '0'. */
375 uint_fast32_t digit_block = (uint_fast32_t) x;
376 assert(digit_block < upper_bnd);
377 #ifdef __UCLIBC_MJN3_ONLY__
378 #warning CONSIDER: Can rounding be a problem?
379 #endif /* __UCLIBC_MJN3_ONLY__ */
380 x = (x - digit_block) * upper_bnd;
384 s[- ++j] = '0' + (digit_block % base);
390 /*************************************************************************/
393 *exp_buf -= ('a' - 'A'); /* e->E and p->P */
398 if ((mode == 'g') && (preci > 0)){
406 memset(buf, '0', DECIMAL_DIG); /* OK, since 'f' -> decimal case. */
413 *s++ = 0; /* Terminator for rounding and 0-triming. */
414 *s = '0'; /* Space to round. */
422 if (*e >= '0' + (base/2)) { /* NOTE: We always round away from 0! */
427 do { /* Handle rounding and trim trailing 0s. */
428 *--e += i; /* Add the carry. */
429 } while ((*e == '0') || (*e > '0' - 1 + base));
432 #ifdef __UCLIBC_HAS_HEXADECIMAL_FLOATS__
433 if ((mode|0x20) == 'a') {
436 for (q = e ; *q ; --q) {
438 *q += (*exp_buf - ('p' - 'a') - '9' - 1);
443 exp *= 4; /* Change from base 16 to base 2. */
446 #endif /* __UCLIBC_HAS_HEXADECIMAL_FLOATS__ */
449 if (e <= s) { /* We carried into an extra digit. */
451 e = s; /* Needed if all 0s. */
455 *++e = 0; /* Terminating nul char. */
457 if ((mode == 'g') && ((o_exp >= -4) && (o_exp <= round))) {
459 preci = round - o_exp;
467 if (o_exp < 0) { /* Exponent is < 0, so */
468 *--s = '0'; /* fake the first 0 digit. */
471 pc_fwi[3] = FPO_ZERO_PAD;
473 pc_fwi[5] = (intptr_t)(sign_str + 4);
479 int i = e - s; /* Total digits is 'i'. */
481 #ifdef __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__
485 if (PRINT_INFO_FLAG_VAL(info,group)
486 && *(p = __UCLIBC_CURLOCALE->grouping)
496 if (o_exp >= nblk1) {
497 num_groups = (o_exp - nblk1) / nblk2 + 1;
498 initial_group = (o_exp - nblk1) % nblk2;
500 #ifdef __UCLIBC_HAS_WCHAR__
501 if (PRINT_INFO_FLAG_VAL(info,wide)) {
502 /* _fp_out_wide() will fix this up. */
503 ts = fmt + THOUSEP_OFFSET;
506 #endif /* __UCLIBC_HAS_WCHAR__ */
507 ts = __UCLIBC_CURLOCALE->thousands_sep;
508 tslen = __UCLIBC_CURLOCALE->thousands_sep_len;
509 #ifdef __UCLIBC_HAS_WCHAR__
511 #endif /* __UCLIBC_HAS_WCHAR__ */
513 width -= num_groups * tslen;
518 #endif /* __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__ */
519 ppc[0] = FPO_STR_PREC;
520 ppc[2] = (intptr_t)(s);
521 if (o_exp >= i) { /* all digit(s) left of decimal */
526 if (o_exp>0) { /* have 0s left of decimal */
527 ppc[0] = FPO_ZERO_PAD;
529 ppc[2] = (intptr_t)(fmt + EMPTY_STRING_OFFSET);
532 } else if (o_exp > 0) { /* decimal between digits */
541 if (PRINT_INFO_FLAG_VAL(info,alt)
544 #ifdef __UCLIBC_HAS_HEXADECIMAL_FLOATS__
546 #endif /* __UCLIBC_HAS_HEXADECIMAL_FLOATS__ */
549 ppc[0] = FPO_STR_PREC;
550 #ifdef __LOCALE_C_ONLY
552 ppc[2] = (intptr_t)(fmt + DECPT_OFFSET);
553 #else /* __LOCALE_C_ONLY */
554 #ifdef __UCLIBC_HAS_WCHAR__
555 if (PRINT_INFO_FLAG_VAL(info,wide)) {
556 /* _fp_out_wide() will fix this up. */
558 ppc[2] = (intptr_t)(fmt + DECPT_OFFSET);
560 #endif /* __UCLIBC_HAS_WCHAR__ */
561 ppc[1] = __UCLIBC_CURLOCALE->decimal_point_len;
562 ppc[2] = (intptr_t)(__UCLIBC_CURLOCALE->decimal_point);
563 #ifdef __UCLIBC_HAS_WCHAR__
565 #endif /* __UCLIBC_HAS_WCHAR__ */
566 #endif /* __LOCALE_C_ONLY */
570 if (++o_exp < 0) { /* Have 0s right of decimal. */
571 ppc[0] = FPO_ZERO_PAD;
573 ppc[2] = (intptr_t)(fmt + EMPTY_STRING_OFFSET);
576 if (i) { /* Have digit(s) right of decimal. */
577 ppc[0] = FPO_STR_PREC;
579 ppc[2] = (intptr_t)(s);
583 if (((o_mode != 'g') || PRINT_INFO_FLAG_VAL(info,alt))
584 #ifdef __UCLIBC_HAS_HEXADECIMAL_FLOATS__
585 && !sufficient_precision
586 #endif /* __UCLIBC_HAS_HEXADECIMAL_FLOATS__ */
589 if (i < preci) { /* Have 0s right of digits. */
591 ppc[0] = FPO_ZERO_PAD;
593 ppc[2] = (intptr_t)(fmt + EMPTY_STRING_OFFSET);
599 /* Build exponent string. */
601 char *p = exp_buf + sizeof(exp_buf);
603 char exp_char = *exp_buf;
605 #ifdef __UCLIBC_HAS_HEXADECIMAL_FLOATS__
606 int min_exp_dig_plus_2 = ((o_mode != 'a') ? (2+2) : (2+1));
607 #else /* __UCLIBC_HAS_HEXADECIMAL_FLOATS__ */
608 #define min_exp_dig_plus_2 (2+2)
609 #endif /* __UCLIBC_HAS_HEXADECIMAL_FLOATS__ */
616 *--p = 0; /* nul-terminate */
617 j = 2; /* Count exp_char and exp_sign. */
619 *--p = '0' + (exp % 10);
621 } while ((++j < min_exp_dig_plus_2) || exp); /* char+sign+mindigits */
625 ppc[0] = FPO_STR_PREC;
627 ppc[2] = (intptr_t)(p);
635 ppc = pc_fwi + 4; /* Need width fields starting with second. */
639 } while (ppc < ppc_last);
642 ppc[0] = FPO_STR_WIDTH;
643 ppc[1] = i = ((*sign_str) != 0);
644 ppc[2] = (intptr_t) sign_str;
646 #ifdef __UCLIBC_HAS_HEXADECIMAL_FLOATS__
647 if (((mode|0x20) == 'a') && (pc_fwi[3] >= 16)) { /* Hex sign handling. */
648 /* Hex and not inf or nan, so prefix with 0x. */
649 char *h = sign_str + i;
651 *++h = 'x' - 'p' + *exp_buf;
655 #endif /* __UCLIBC_HAS_HEXADECIMAL_FLOATS__ */
657 if ((width -= i) > 0) {
658 if (PRINT_INFO_FLAG_VAL(info,left)) { /* Left-justified. */
659 ppc_last[0] = FPO_STR_WIDTH;
661 ppc_last[2] = (intptr_t)(fmt + EMPTY_STRING_OFFSET);
663 } else if (info->pad == '0') { /* 0 padding */
664 ppc[4] += width; /* Pad second field. */
666 ppc[1] += width; /* Pad first (sign) field. */
674 #ifdef __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__
676 if ((ppc == pc_fwi + 6) && num_groups) {
677 const char *gp = (const char *) ppc[2];
679 int blk = initial_group;
681 cnt += num_groups * tslen; /* Adjust count now for sep chars. */
683 /* __printf("\n"); */
685 if (!blk) { /* Initial group could be 0 digits long! */
687 } else if (len >= blk) { /* Enough digits for a group. */
688 /* __printf("norm: len=%d blk=%d \"%.*s\"\n", len, blk, blk, gp); */
689 if (fp_outfunc(fp, *ppc, blk, (intptr_t) gp) != blk) {
697 } else { /* Transition to 0s. */
698 /* __printf("trans: len=%d blk=%d \"%.*s\"\n", len, blk, len, gp); */
700 /* __printf("len\n"); */
701 if (fp_outfunc(fp, *ppc, len, (intptr_t) gp) != len) {
707 if (ppc[3] == FPO_ZERO_PAD) { /* Need to group 0s */
708 /* __printf("zeropad\n"); */
711 gp = (const char *) ppc[2];
712 blk -= len; /* blk > len, so blk still > 0. */
714 continue; /* Don't decrement num_groups here. */
716 assert(num_groups == 0);
721 if (num_groups <= 0) {
726 if (fp_outfunc(fp, FPO_STR_PREC, tslen, (intptr_t) ts) != tslen) {
731 /* __printf("num_groups=%d blk=%d\n", num_groups, blk); */
736 #endif /* __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__ */
737 { /* NOTE: Remember 'else' above! */
738 if (fp_outfunc(fp, *ppc, ppc[1], ppc[2]) != ppc[1]) {
745 } while (ppc < ppc_last);