OSDN Git Service

hidden_def/hidden_proto: convert all users (I hope) termios split, add some missing...
[uclinux-h8/uClibc.git] / libc / stdio / _fpmaxtostr.c
1 /* Copyright (C) 2004       Manuel Novoa III    <mjn3@codepoet.org>
2  *
3  * GNU Library General Public License (LGPL) version 2 or later.
4  *
5  * Dedicated to Toni.  See uClibc/DEDICATION.mjn3 for details.
6  */
7
8 #include "_stdio.h"
9 #include <printf.h>
10 #include <float.h>
11 #include <locale.h>
12 #include <bits/uClibc_fpmax.h>
13
14 libc_hidden_proto(memset)
15
16 typedef size_t (__fp_outfunc_t)(FILE *fp, intptr_t type, intptr_t len,
17                                                                 intptr_t buf);
18
19
20 /* Copyright (C) 2000, 2001, 2003      Manuel Novoa III
21  *
22  * Function: 
23  *
24  *     ssize_t _fpmaxtostr(FILE * fp, __fpmax_t x, struct printf_info *info,
25  *                         __fp_outfunc_t fp_outfunc);
26  *
27  * This is derived from the old _dtostr, whic I wrote for uClibc to provide
28  * floating point support for the printf functions.  It handles +/- infinity,
29  * nan, and signed 0 assuming you have ieee arithmetic.  It also now handles
30  * digit grouping (for the uClibc supported locales) and hexadecimal float
31  * notation.  Finally, via the fp_outfunc parameter, it now supports wide
32  * output.
33  *
34  * Notes:
35  *
36  * At most DECIMAL_DIG significant digits are kept.  Any trailing digits
37  * are treated as 0 as they are really just the results of rounding noise
38  * anyway.  If you want to do better, use an arbitary precision arithmetic
39  * package.  ;-)
40  *
41  * It should also be fairly portable, as no assumptions are made about the
42  * bit-layout of doubles.  Of course, that does make it less efficient than
43  * it could be.
44  *
45  */
46
47 /*****************************************************************************/
48 /* Don't change anything that follows unless you know what you're doing.     */
49 /*****************************************************************************/
50 /* Fairly portable nan check.  Bitwise for i386 generated larger code.
51  * If you have a better version, comment this out.
52  */
53 #define isnan(x)             ((x) != (x))
54
55 /* Without seminumerical functions to examine the sign bit, this is
56  * about the best we can do to test for '-0'.
57  */
58 #define zeroisnegative(x)    ((1./(x)) < 0)
59
60 /*****************************************************************************/
61 /* Don't change anything that follows peroid!!!  ;-)                         */
62 /*****************************************************************************/
63 #ifdef __UCLIBC_HAS_HEXADECIMAL_FLOATS__
64 #if FLT_RADIX != 2
65 #error FLT_RADIX != 2 is not currently supported
66 #endif
67 #endif /* __UCLIBC_HAS_HEXADECIMAL_FLOATS__ */
68
69 #define NUM_HEX_DIGITS      ((FPMAX_MANT_DIG + 3)/ 4)
70
71 /* WARNING: Adjust _fp_out_wide() below if this changes! */
72 /* With 32 bit ints, we can get 9 decimal digits per block. */
73 #define DIGITS_PER_BLOCK     9
74 #define HEX_DIGITS_PER_BLOCK 8
75
76 /* Maximum number of subcases to output double is...
77  *  0 - sign
78  *  1 - padding and initial digit
79  *  2 - digits left of the radix
80  *  3 - 0s left of the radix        or   radix
81  *  4 - radix                       or   digits right of the radix
82  *  5 - 0s right of the radix
83  *  6 - exponent
84  *  7 - trailing space padding
85  * although not all cases may occur.
86  */
87 #define MAX_CALLS 8
88
89 /*****************************************************************************/
90
91 #define NUM_DIGIT_BLOCKS   ((DECIMAL_DIG+DIGITS_PER_BLOCK-1)/DIGITS_PER_BLOCK)
92 #define NUM_HEX_DIGIT_BLOCKS \
93    ((NUM_HEX_DIGITS+HEX_DIGITS_PER_BLOCK-1)/HEX_DIGITS_PER_BLOCK)
94
95 /* WARNING: Adjust _fp_out_wide() below if this changes! */
96
97 /* extra space for '-', '.', 'e+###', and nul */
98 #define BUF_SIZE  ( 3 + NUM_DIGIT_BLOCKS * DIGITS_PER_BLOCK )
99
100 /*****************************************************************************/
101
102 static const char fmt[] = "inf\0INF\0nan\0NAN\0.\0,";
103
104 #define INF_OFFSET        0             /* must be 1st */
105 #define NAN_OFFSET        8             /* must be 2nd.. see hex sign handling */
106 #define DECPT_OFFSET     16
107 #define THOUSEP_OFFSET   18
108
109 #define EMPTY_STRING_OFFSET 3
110
111 /*****************************************************************************/
112 #if FPMAX_MAX_10_EXP < -FPMAX_MIN_10_EXP
113 #error scaling code can not handle FPMAX_MAX_10_EXP < -FPMAX_MIN_10_EXP
114 #endif
115
116 static const __fpmax_t exp10_table[] =
117 {
118         1e1L, 1e2L, 1e4L, 1e8L, 1e16L, 1e32L,   /* floats */
119 #if FPMAX_MAX_10_EXP < 32
120 #error unsupported FPMAX_MAX_10_EXP (< 32).  ANSI/ISO C requires >= 37.
121 #endif
122 #if FPMAX_MAX_10_EXP >= 64
123         1e64L,
124 #endif
125 #if FPMAX_MAX_10_EXP >= 128
126         1e128L,
127 #endif
128 #if FPMAX_MAX_10_EXP >= 256
129         1e256L,
130 #endif
131 #if FPMAX_MAX_10_EXP >= 512
132         1e512L,
133 #endif
134 #if FPMAX_MAX_10_EXP >= 1024
135         1e1024L,
136 #endif
137 #if FPMAX_MAX_10_EXP >= 2048
138         1e2048L,
139 #endif
140 #if FPMAX_MAX_10_EXP >= 4096
141         1e4096L
142 #endif
143 #if FPMAX_MAX_10_EXP >= 8192
144 #error unsupported FPMAX_MAX_10_EXP.  please increase table
145 #endif
146 };
147
148 #define EXP10_TABLE_SIZE     (sizeof(exp10_table)/sizeof(exp10_table[0]))
149 #define EXP10_TABLE_MAX      (1U<<(EXP10_TABLE_SIZE-1))
150
151 /*****************************************************************************/
152 #ifdef __UCLIBC_HAS_HEXADECIMAL_FLOATS__
153
154 #if FLT_RADIX != 2
155 #error FLT_RADIX != 2 is not currently supported
156 #endif
157
158 #if FPMAX_MAX_EXP < -FPMAX_MIN_EXP
159 #error scaling code can not handle FPMAX_MAX_EXP < -FPMAX_MIN_EXP
160 #endif
161
162 static const __fpmax_t exp16_table[] = {
163         0x1.0p4L, 0x1.0p8L, 0x1.0p16L, 0x1.0p32L, 0x1.0p64L,
164 #if FPMAX_MAX_EXP >= 128
165         0x1.0p128L,
166 #endif
167 #if FPMAX_MAX_EXP >= 256
168         0x1.0p256L,
169 #endif
170 #if FPMAX_MAX_EXP >= 512
171         0x1.0p512L,
172 #endif
173 #if FPMAX_MAX_EXP >= 1024
174         0x1.0p1024L,
175 #endif
176 #if FPMAX_MAX_EXP >= 2048
177         0x1.0p2048L,
178 #endif
179 #if FPMAX_MAX_EXP >= 4096
180         0x1.0p4096L,
181 #endif
182 #if FPMAX_MAX_EXP >= 8192
183         0x1.0p8192L,
184 #endif
185 #if FPMAX_MAX_EXP >= 16384
186         0x1.0p16384L
187 #endif
188 #if FPMAX_MAX_EXP >= 32768 
189 #error unsupported FPMAX_MAX_EXP.  please increase table
190 #endif
191 };
192
193 #define EXP16_TABLE_SIZE     (sizeof(exp16_table)/sizeof(exp16_table[0]))
194 #define EXP16_TABLE_MAX      (1U<<(EXP16_TABLE_SIZE-1))
195
196 #endif /* __UCLIBC_HAS_HEXADECIMAL_FLOATS__ */
197 /*****************************************************************************/
198
199 #define FPO_ZERO_PAD    (0x80 | '0')
200 #define FPO_STR_WIDTH   (0x80 | ' ');
201 #define FPO_STR_PREC    'p'
202
203 ssize_t attribute_hidden _fpmaxtostr(FILE * fp, __fpmax_t x, struct printf_info *info,
204                                         __fp_outfunc_t fp_outfunc)
205 {
206 #ifdef __UCLIBC_HAS_HEXADECIMAL_FLOATS__
207         __fpmax_t lower_bnd;
208         __fpmax_t upper_bnd = 1e9;
209 #endif /* __UCLIBC_HAS_HEXADECIMAL_FLOATS__ */
210 #ifdef __UCLIBC_HAS_HEXADECIMAL_FLOATS__
211         uint_fast32_t base = 10;
212         const __fpmax_t *power_table;
213         int dpb = DIGITS_PER_BLOCK;
214         int ndb = NUM_DIGIT_BLOCKS;
215         int nd = DECIMAL_DIG;
216         int sufficient_precision = 0;
217 #endif /* __UCLIBC_HAS_HEXADECIMAL_FLOATS__ */
218 #ifdef __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__
219         int num_groups = 0;
220         int initial_group;         /* This does not need to be initialized. */
221         int tslen;                         /* This does not need to be initialized. */
222         int nblk2;                         /* This does not need to be initialized. */
223         const char *ts;            /* This does not need to be initialized. */
224 #endif /* __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__ */
225         int round, o_exp;
226         int exp;
227         int width, preci;
228         int cnt;
229         char *s;
230         char *e;
231         intptr_t pc_fwi[3*MAX_CALLS];
232         intptr_t *ppc;
233         intptr_t *ppc_last;
234 #ifdef __UCLIBC_MJN3_ONLY__
235 #warning TODO: The size of exp_buf[] should really be determined by the float constants.
236 #endif /* __UCLIBC_MJN3_ONLY__ */
237         char exp_buf[16];
238         char buf[BUF_SIZE];
239         char sign_str[6];                       /* Last 2 are for 1st digit + nul. */
240         char o_mode;
241         char mode;
242
243
244         width = info->width;
245         preci = info->prec;
246         mode = info->spec;
247
248         *exp_buf = 'e';
249         if ((mode|0x20) == 'a') {
250 #ifdef __UCLIBC_HAS_HEXADECIMAL_FLOATS__
251                 *exp_buf = 'p';
252                 if (preci < 0) {
253                         preci = NUM_HEX_DIGITS;
254                         sufficient_precision = 1;
255                 }
256 #else
257                 mode += ('g' - 'a');
258 #endif
259         }
260
261         if (preci < 0) {
262                 preci = 6;
263         }
264
265         *sign_str = '\0';
266         if (PRINT_INFO_FLAG_VAL(info,showsign)) {
267                 *sign_str = '+';
268         } else if (PRINT_INFO_FLAG_VAL(info,space)) {
269                 *sign_str = ' ';
270         }
271
272         *(sign_str+1) = 0;
273         pc_fwi[5] = INF_OFFSET;
274         if (isnan(x)) {                         /* First, check for nan. */
275                 pc_fwi[5] = NAN_OFFSET;
276                 goto INF_NAN;
277         }
278
279         if (x == 0) {                           /* Handle 0 now to avoid false positive. */
280 #if 1
281                 if (zeroisnegative(x)) { /* Handle 'signed' zero. */
282                         *sign_str = '-';
283                 }
284 #endif
285                 exp = -1;
286                 goto GENERATE_DIGITS;
287         }
288
289         if (x < 0) {                            /* Convert negatives to positives. */
290                 *sign_str = '-';
291                 x = -x;
292         }
293
294         if (__FPMAX_ZERO_OR_INF_CHECK(x)) {     /* Inf since zero handled above. */
295         INF_NAN:
296                 info->pad = ' ';
297                 ppc = pc_fwi + 6;
298                 pc_fwi[3] = FPO_STR_PREC;
299                 pc_fwi[4] = 3;
300                 if (mode < 'a') {
301                         pc_fwi[5] += 4;
302                 }
303                 pc_fwi[5] = (intptr_t)(fmt + pc_fwi[5]);
304                 goto EXIT_SPECIAL;
305         }
306
307         {
308                 int i, j;
309 #ifdef __UCLIBC_MJN3_ONLY__
310 #warning TODO: Clean up defines when hexadecimal float notation is unsupported.
311 #endif /* __UCLIBC_MJN3_ONLY__ */
312
313 #ifdef __UCLIBC_HAS_HEXADECIMAL_FLOATS__
314
315                 if ((mode|0x20) == 'a') {
316                         lower_bnd = 0x1.0p31L;
317                         upper_bnd = 0x1.0p32L;
318                         power_table = exp16_table;
319                         exp = HEX_DIGITS_PER_BLOCK - 1;
320                         i = EXP16_TABLE_SIZE;
321                         j = EXP16_TABLE_MAX;
322                         dpb = HEX_DIGITS_PER_BLOCK;
323                         ndb = NUM_HEX_DIGIT_BLOCKS;
324                         nd = NUM_HEX_DIGITS;
325                         base = 16;
326                 } else {
327                         lower_bnd = 1e8;
328                         /*              upper_bnd = 1e9; */
329                         power_table = exp10_table;
330                         exp = DIGITS_PER_BLOCK - 1;
331                         i = EXP10_TABLE_SIZE;
332                         j = EXP10_TABLE_MAX;
333                         /*              dpb = DIGITS_PER_BLOCK; */
334                         /*              ndb = NUM_DIGIT_BLOCKS; */
335                         /*              base = 10; */
336                 }
337
338
339
340 #else  /* __UCLIBC_HAS_HEXADECIMAL_FLOATS__ */
341
342 #define lower_bnd    1e8
343 #define upper_bnd    1e9
344 #define power_table  exp10_table
345 #define dpb          DIGITS_PER_BLOCK
346 #define base         10
347 #define ndb          NUM_DIGIT_BLOCKS
348 #define nd           DECIMAL_DIG
349
350                 exp = DIGITS_PER_BLOCK - 1;
351                 i = EXP10_TABLE_SIZE;
352                 j = EXP10_TABLE_MAX;
353
354 #endif /* __UCLIBC_HAS_HEXADECIMAL_FLOATS__ */
355
356                 {
357                         int exp_neg = 0;
358                         if (x < lower_bnd) { /* Do we need to scale up or down? */
359                                 exp_neg = 1;
360                         }
361
362                         do {
363                                 --i;
364                                 if (exp_neg) {
365                                         if (x * power_table[i] < upper_bnd) {
366                                                 x *= power_table[i];
367                                                 exp -= j;
368                                         }
369                                 } else {
370                                         if (x / power_table[i] >= lower_bnd) {
371                                                 x /= power_table[i];
372                                                 exp += j;
373                                         }
374                                 }
375                                 j >>= 1;
376                         } while (i);
377                 }
378         }
379         if (x >= upper_bnd) {           /* Handle bad rounding case. */
380                 x /= power_table[0];
381                 ++exp;
382         }
383         assert(x < upper_bnd);
384
385  GENERATE_DIGITS:
386         {
387                 int i, j;
388                 s = buf + 2;                    /* Leave space for '\0' and '0'. */
389                 i = 0;
390                 do {
391                         uint_fast32_t digit_block = (uint_fast32_t) x;
392                         assert(digit_block < upper_bnd);
393 #ifdef __UCLIBC_MJN3_ONLY__
394 #warning CONSIDER: Can rounding be a problem?
395 #endif /* __UCLIBC_MJN3_ONLY__ */
396                         x = (x - digit_block) * upper_bnd;
397                         s += dpb;
398                         j = 0;
399                         do {
400                                 s[- ++j] = '0' + (digit_block % base);
401                                 digit_block /= base;
402                         } while (j < dpb);
403                 } while (++i < ndb);
404         }
405
406         /*************************************************************************/
407
408         if (mode < 'a') {
409                 *exp_buf -= ('a' - 'A'); /* e->E and p->P */
410                 mode += ('a' - 'A');
411         } 
412
413         o_mode = mode;
414         if ((mode == 'g') && (preci > 0)){
415                 --preci;
416         }
417         round = preci;
418
419         if (mode == 'f') {
420                 round += exp;
421                 if (round < -1) {
422                         memset(buf, '0', DECIMAL_DIG); /* OK, since 'f' -> decimal case. */
423                     exp = -1;
424                     round = -1;
425                 }
426         }
427
428         s = buf;
429         *s++ = 0;                                       /* Terminator for rounding and 0-triming. */
430         *s = '0';                                       /* Space to round. */
431
432         {
433                 int i;
434                 i = 0;
435                 e = s + nd + 1;
436                 if (round < nd) {
437                         e = s + round + 2;
438                         if (*e >= '0' + (base/2)) {     /* NOTE: We always round away from 0! */
439                                 i = 1;
440                         }
441                 }
442
443                 do {                       /* Handle rounding and trim trailing 0s. */
444                         *--e += i;                      /* Add the carry. */
445                 } while ((*e == '0') || (*e > '0' - 1 + base));
446         }
447
448 #ifdef __UCLIBC_HAS_HEXADECIMAL_FLOATS__
449         if ((mode|0x20) == 'a') {
450                 char *q;
451                         
452                 for (q = e ; *q ; --q) {
453                         if (*q > '9') {
454                                 *q += (*exp_buf - ('p' - 'a') - '9' - 1);
455                         }
456                 }
457
458                 if (e > s) {
459                         exp *= 4;                       /* Change from base 16 to base 2. */
460                 }
461         }
462 #endif /* __UCLIBC_HAS_HEXADECIMAL_FLOATS__ */
463
464         o_exp = exp;
465         if (e <= s) {                           /* We carried into an extra digit. */
466                 ++o_exp;
467                 e = s;                                  /* Needed if all 0s. */
468         } else {
469                 ++s;
470         }
471         *++e = 0;                                       /* Terminating nul char. */
472
473         if ((mode == 'g') && ((o_exp >= -4) && (o_exp <= round))) {
474                 mode = 'f';
475                 preci = round - o_exp;
476         }
477
478         exp = o_exp;
479         if (mode != 'f') {
480                 o_exp = 0;
481         }
482
483         if (o_exp < 0) {                        /* Exponent is < 0, so */
484                 *--s = '0';                             /* fake the first 0 digit. */
485         }
486
487         pc_fwi[3] = FPO_ZERO_PAD;
488         pc_fwi[4] = 1;
489         pc_fwi[5] = (intptr_t)(sign_str + 4);
490         sign_str[4] = *s++;
491         sign_str[5] = 0;
492         ppc = pc_fwi + 6;
493
494         {
495                 int i = e - s;                  /* Total digits is 'i'. */
496                 if (o_exp >= 0) {
497 #ifdef __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__
498
499                         const char *p;
500
501                         if (PRINT_INFO_FLAG_VAL(info,group)
502                                 && *(p = __UCLIBC_CURLOCALE_DATA.grouping)
503                                 ) {
504                                 int nblk1;
505
506                                 nblk2 = nblk1 = *p;
507                                 if (*++p) {
508                                         nblk2 = *p;
509                                         assert(!*++p);
510                                 }
511
512                                 if (o_exp >= nblk1) {
513                                         num_groups = (o_exp - nblk1) / nblk2 + 1;
514                                         initial_group = (o_exp - nblk1) % nblk2;
515
516 #ifdef __UCLIBC_HAS_WCHAR__
517                                         if (PRINT_INFO_FLAG_VAL(info,wide)) {
518                                                 /* _fp_out_wide() will fix this up. */
519                                                 ts = fmt + THOUSEP_OFFSET;
520                                                 tslen = 1;
521                                         } else {
522 #endif /* __UCLIBC_HAS_WCHAR__ */
523                                                 ts = __UCLIBC_CURLOCALE_DATA.thousands_sep;
524                                                 tslen = __UCLIBC_CURLOCALE_DATA.thousands_sep_len;
525 #ifdef __UCLIBC_HAS_WCHAR__
526                                         }
527 #endif /* __UCLIBC_HAS_WCHAR__ */
528
529                                         width -= num_groups * tslen;
530                                 }
531                         }
532
533
534 #endif /* __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__ */
535                         ppc[0] = FPO_STR_PREC;
536                         ppc[2] = (intptr_t)(s);
537                         if (o_exp >= i) {               /* all digit(s) left of decimal */
538                                 ppc[1] = i;
539                                 ppc += 3;
540                                 o_exp -= i;
541                                 i = 0;
542                                 if (o_exp>0) {          /* have 0s left of decimal */
543                                         ppc[0] = FPO_ZERO_PAD;
544                                         ppc[1] = o_exp;
545                                         ppc[2] = (intptr_t)(fmt + EMPTY_STRING_OFFSET);
546                                         ppc += 3;
547                                 }
548                         } else if (o_exp > 0) { /* decimal between digits */
549                                 ppc[1] = o_exp;
550                                 ppc += 3;
551                                 s += o_exp;
552                                 i -= o_exp;
553                         }
554                         o_exp = -1;
555                 }
556
557                 if (PRINT_INFO_FLAG_VAL(info,alt)
558                         || (i)
559                         || ((o_mode != 'g')
560 #ifdef __UCLIBC_HAS_HEXADECIMAL_FLOATS__
561                                 && (o_mode != 'a')
562 #endif /* __UCLIBC_HAS_HEXADECIMAL_FLOATS__ */
563                                 && (preci > 0))
564                         ) {
565                         ppc[0] = FPO_STR_PREC;
566 #ifdef __LOCALE_C_ONLY
567                         ppc[1] = 1;
568                         ppc[2] = (intptr_t)(fmt + DECPT_OFFSET);
569 #else  /* __LOCALE_C_ONLY */
570 #ifdef __UCLIBC_HAS_WCHAR__
571                         if (PRINT_INFO_FLAG_VAL(info,wide)) {
572                                 /* _fp_out_wide() will fix this up. */
573                                 ppc[1] = 1;
574                                 ppc[2] = (intptr_t)(fmt + DECPT_OFFSET);
575                         } else {
576 #endif /* __UCLIBC_HAS_WCHAR__ */
577                                 ppc[1] = __UCLIBC_CURLOCALE_DATA.decimal_point_len;
578                                 ppc[2] = (intptr_t)(__UCLIBC_CURLOCALE_DATA.decimal_point);
579 #ifdef __UCLIBC_HAS_WCHAR__
580                         }
581 #endif /* __UCLIBC_HAS_WCHAR__ */
582 #endif /* __LOCALE_C_ONLY */
583                         ppc += 3;
584                 }
585
586                 if (++o_exp < 0) {                      /* Have 0s right of decimal. */
587                         ppc[0] = FPO_ZERO_PAD;
588                         ppc[1] = -o_exp;
589                         ppc[2] = (intptr_t)(fmt + EMPTY_STRING_OFFSET);
590                         ppc += 3;
591                 }
592                 if (i) {                                        /* Have digit(s) right of decimal. */
593                         ppc[0] = FPO_STR_PREC;
594                         ppc[1] = i;
595                         ppc[2] = (intptr_t)(s);
596                         ppc += 3;
597                 }
598
599                 if (((o_mode != 'g') || PRINT_INFO_FLAG_VAL(info,alt))
600 #ifdef __UCLIBC_HAS_HEXADECIMAL_FLOATS__
601                         && !sufficient_precision
602 #endif /* __UCLIBC_HAS_HEXADECIMAL_FLOATS__ */
603                         ) {
604                         i -= o_exp;
605                         if (i < preci) {                /* Have 0s right of digits. */
606                                 i = preci - i;
607                                 ppc[0] = FPO_ZERO_PAD;
608                                 ppc[1] = i;
609                                 ppc[2] = (intptr_t)(fmt + EMPTY_STRING_OFFSET);
610                                 ppc += 3;
611                         }
612                 }
613         }
614
615         /* Build exponent string. */
616         if (mode != 'f') {
617                 char *p = exp_buf + sizeof(exp_buf);
618                 int j;
619                 char exp_char = *exp_buf;
620                 char exp_sign = '+';
621 #ifdef __UCLIBC_HAS_HEXADECIMAL_FLOATS__
622                 int min_exp_dig_plus_2 = ((o_mode != 'a') ? (2+2) : (2+1));
623 #else  /* __UCLIBC_HAS_HEXADECIMAL_FLOATS__ */
624 #define min_exp_dig_plus_2  (2+2)
625 #endif /* __UCLIBC_HAS_HEXADECIMAL_FLOATS__ */
626
627                 if (exp < 0) {
628                         exp_sign = '-';
629                         exp = -exp;
630                 }
631
632                 *--p = 0;                       /* nul-terminate */
633                 j = 2;                          /* Count exp_char and exp_sign. */
634                 do {
635                         *--p = '0' + (exp % 10);
636                         exp /= 10;
637                 } while ((++j < min_exp_dig_plus_2) || exp); /* char+sign+mindigits */
638                 *--p = exp_sign;
639                 *--p = exp_char;
640
641                 ppc[0] = FPO_STR_PREC;
642                 ppc[1] = j;
643                 ppc[2] = (intptr_t)(p);
644                 ppc += 3;
645         }
646
647  EXIT_SPECIAL:
648         {
649                 int i;
650                 ppc_last = ppc;
651                 ppc = pc_fwi + 4;        /* Need width fields starting with second. */
652                 do {
653                         width -= *ppc;
654                         ppc += 3;
655                 } while (ppc < ppc_last);
656
657                 ppc = pc_fwi;
658                 ppc[0] = FPO_STR_WIDTH;
659                 ppc[1] = i = ((*sign_str) != 0);
660                 ppc[2] = (intptr_t) sign_str;
661
662 #ifdef __UCLIBC_HAS_HEXADECIMAL_FLOATS__
663                 if (((mode|0x20) == 'a') && (pc_fwi[3] >= 16)) { /* Hex sign handling. */
664                         /* Hex and not inf or nan, so prefix with 0x. */
665                         char *h = sign_str + i;
666                         *h = '0';
667                         *++h = 'x' - 'p' + *exp_buf;
668                         *++h = 0;
669                         ppc[1] = (i += 2);
670                 }
671 #endif /* __UCLIBC_HAS_HEXADECIMAL_FLOATS__ */
672
673                 if ((width -= i) > 0) {
674                         if (PRINT_INFO_FLAG_VAL(info,left)) { /* Left-justified. */
675                                 ppc_last[0] = FPO_STR_WIDTH;
676                                 ppc_last[1] = width;
677                                 ppc_last[2] = (intptr_t)(fmt + EMPTY_STRING_OFFSET);
678                                 ppc_last += 3;
679                         } else if (info->pad == '0') { /* 0 padding */
680                                 ppc[4] += width;        /* Pad second field. */
681                         } else {
682                                 ppc[1] += width;        /* Pad first (sign) field. */
683                         }
684                 }
685
686                 cnt = 0;
687         }
688
689         do {
690 #ifdef __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__
691
692                 if ((ppc == pc_fwi + 6) && num_groups) {
693                         const char *gp = (const char *) ppc[2];
694                         int len = ppc[1];
695                         int blk = initial_group;
696
697                         cnt += num_groups * tslen; /* Adjust count now for sep chars. */
698
699 /*                      __printf("\n"); */
700                         do {
701                                 if (!blk) {             /* Initial group could be 0 digits long! */
702                                         blk = nblk2;
703                                 } else if (len >= blk) { /* Enough digits for a group. */
704 /*                                      __printf("norm:  len=%d blk=%d  \"%.*s\"\n", len, blk, blk, gp); */
705                                         if (fp_outfunc(fp, *ppc, blk, (intptr_t) gp) != blk) {
706                                                 return -1;
707                                         }
708                                         assert(gp);
709                                         if (*gp) {
710                                                 gp += blk;
711                                         }
712                                         len -= blk;
713                                 } else {                /* Transition to 0s. */
714 /*                                      __printf("trans: len=%d blk=%d  \"%.*s\"\n", len, blk, len, gp); */
715                                         if (len) {
716 /*                                              __printf("len\n"); */
717                                                 if (fp_outfunc(fp, *ppc, len, (intptr_t) gp) != len) {
718                                                         return -1;
719                                                 }
720                                                 gp += len;
721                                         }
722
723                                         if (ppc[3] == FPO_ZERO_PAD) { /* Need to group 0s */
724 /*                                              __printf("zeropad\n"); */
725                                                 cnt += ppc[1];
726                                                 ppc += 3;
727                                                 gp = (const char *) ppc[2];
728                                                 blk -= len;     /* blk > len, so blk still > 0. */
729                                                 len = ppc[1];
730                                                 continue; /* Don't decrement num_groups here. */
731                                         } else {
732                                                 assert(num_groups == 0);
733                                                 break;
734                                         }
735                                 }
736
737                                 if (num_groups <= 0) {
738                                         break;
739                                 }
740                                 --num_groups;
741
742                                 if (fp_outfunc(fp, FPO_STR_PREC, tslen, (intptr_t) ts) != tslen) {
743                                         return -1;
744                                 }
745                                 blk = nblk2;
746
747 /*                              __printf("num_groups=%d   blk=%d\n", num_groups, blk); */
748
749                         } while (1);
750                 } else
751
752 #endif /* __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__ */
753                 {                                               /* NOTE: Remember 'else' above! */
754                         if (fp_outfunc(fp, *ppc, ppc[1], ppc[2]) != ppc[1]) {
755                                 return -1;
756                         }
757                 }
758
759                 cnt += ppc[1];
760                 ppc += 3;
761         } while (ppc < ppc_last);
762
763         return cnt;
764 }