OSDN Git Service

f7ea792c4f96f0db8f16c14eee791360052afaef
[uclinux-h8/uClibc.git] / libc / stdio / _fpmaxtostr.c
1 /*
2  * Copyright (C) 2000,2001,2003,2004    Manuel Novoa III <mjn3@codepoet.org>
3  *
4  * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
5  *
6  * Dedicated to Toni.  See uClibc/DEDICATION.mjn3 for details.
7  */
8
9 #include "_stdio.h"
10 #include <printf.h>
11 #include <float.h>
12 #include <locale.h>
13 #include "_fpmaxtostr.h"
14
15 /*
16  * Function:
17  *
18  *     ssize_t _fpmaxtostr(FILE * fp, __fpmax_t x, struct printf_info *info,
19  *                         __fp_outfunc_t fp_outfunc);
20  *
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
26  * output.
27  *
28  * Notes:
29  *
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
33  * package.  ;-)
34  *
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
37  * it could be.
38  */
39
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.
45  */
46 #define isnan(x)             ((x) != (x))
47
48 /* Without seminumerical functions to examine the sign bit, this is
49  * about the best we can do to test for '-0'.
50  */
51 #define zeroisnegative(x)    ((1./(x)) < 0)
52
53 /*****************************************************************************/
54 /* Don't change anything that follows peroid!!!  ;-)                         */
55 /*****************************************************************************/
56 #ifdef __UCLIBC_HAS_HEXADECIMAL_FLOATS__
57 #if FLT_RADIX != 2
58 #error FLT_RADIX != 2 is not currently supported
59 #endif
60 #endif /* __UCLIBC_HAS_HEXADECIMAL_FLOATS__ */
61
62 #define NUM_HEX_DIGITS      ((FPMAX_MANT_DIG + 3)/ 4)
63
64 #define HEX_DIGITS_PER_BLOCK 8
65
66 /* Maximum number of subcases to output double is...
67  *  0 - sign
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
73  *  6 - exponent
74  *  7 - trailing space padding
75  * although not all cases may occur.
76  */
77 #define MAX_CALLS 8
78
79 /*****************************************************************************/
80
81 #define NUM_HEX_DIGIT_BLOCKS \
82    ((NUM_HEX_DIGITS+HEX_DIGITS_PER_BLOCK-1)/HEX_DIGITS_PER_BLOCK)
83
84 /*****************************************************************************/
85
86 static const char fmt[] = "inf\0INF\0nan\0NAN\0.\0,";
87
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
92
93 #define EMPTY_STRING_OFFSET 3
94
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
98 #endif
99
100 static const __fpmax_t exp10_table[] =
101 {
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.
105 #endif
106 #if FPMAX_MAX_10_EXP >= 64
107         1e64L,
108 #endif
109 #if FPMAX_MAX_10_EXP >= 128
110         1e128L,
111 #endif
112 #if FPMAX_MAX_10_EXP >= 256
113         1e256L,
114 #endif
115 #if FPMAX_MAX_10_EXP >= 512
116         1e512L,
117 #endif
118 #if FPMAX_MAX_10_EXP >= 1024
119         1e1024L,
120 #endif
121 #if FPMAX_MAX_10_EXP >= 2048
122         1e2048L,
123 #endif
124 #if FPMAX_MAX_10_EXP >= 4096
125         1e4096L
126 #endif
127 #if FPMAX_MAX_10_EXP >= 8192
128 #error unsupported FPMAX_MAX_10_EXP.  please increase table
129 #endif
130 };
131
132 #define EXP10_TABLE_SIZE     (sizeof(exp10_table)/sizeof(exp10_table[0]))
133 #define EXP10_TABLE_MAX      (1U<<(EXP10_TABLE_SIZE-1))
134
135 /*****************************************************************************/
136 #ifdef __UCLIBC_HAS_HEXADECIMAL_FLOATS__
137
138 #if FLT_RADIX != 2
139 #error FLT_RADIX != 2 is not currently supported
140 #endif
141
142 #if FPMAX_MAX_EXP < -FPMAX_MIN_EXP
143 #error scaling code can not handle FPMAX_MAX_EXP < -FPMAX_MIN_EXP
144 #endif
145
146 static const __fpmax_t exp16_table[] = {
147         0x1.0p4L, 0x1.0p8L, 0x1.0p16L, 0x1.0p32L, 0x1.0p64L,
148 #if FPMAX_MAX_EXP >= 128
149         0x1.0p128L,
150 #endif
151 #if FPMAX_MAX_EXP >= 256
152         0x1.0p256L,
153 #endif
154 #if FPMAX_MAX_EXP >= 512
155         0x1.0p512L,
156 #endif
157 #if FPMAX_MAX_EXP >= 1024
158         0x1.0p1024L,
159 #endif
160 #if FPMAX_MAX_EXP >= 2048
161         0x1.0p2048L,
162 #endif
163 #if FPMAX_MAX_EXP >= 4096
164         0x1.0p4096L,
165 #endif
166 #if FPMAX_MAX_EXP >= 8192
167         0x1.0p8192L,
168 #endif
169 #if FPMAX_MAX_EXP >= 16384
170         0x1.0p16384L
171 #endif
172 #if FPMAX_MAX_EXP >= 32768
173 #error unsupported FPMAX_MAX_EXP.  please increase table
174 #endif
175 };
176
177 #define EXP16_TABLE_SIZE     (sizeof(exp16_table)/sizeof(exp16_table[0]))
178 #define EXP16_TABLE_MAX      (1U<<(EXP16_TABLE_SIZE-1))
179
180 #endif /* __UCLIBC_HAS_HEXADECIMAL_FLOATS__ */
181 /*****************************************************************************/
182
183 #define FPO_ZERO_PAD    (0x80 | '0')
184 #define FPO_STR_WIDTH   (0x80 | ' ');
185 #define FPO_STR_PREC    'p'
186
187 ssize_t _fpmaxtostr(FILE * fp, __fpmax_t x, struct printf_info *info,
188                                         __fp_outfunc_t fp_outfunc)
189 {
190 #ifdef __UCLIBC_HAS_HEXADECIMAL_FLOATS__
191         __fpmax_t lower_bnd;
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__
203         int num_groups = 0;
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__ */
209         int round, o_exp;
210         int exp;
211         int width, preci;
212         int cnt;
213         char *s;
214         char *e;
215         intptr_t pc_fwi[3*MAX_CALLS];
216         intptr_t *ppc;
217         intptr_t *ppc_last;
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__ */
221         char exp_buf[16];
222         char buf[BUF_SIZE];
223         char sign_str[6];                       /* Last 2 are for 1st digit + nul. */
224         char o_mode;
225         char mode;
226
227
228         width = info->width;
229         preci = info->prec;
230         mode = info->spec;
231
232         *exp_buf = 'e';
233         if ((mode|0x20) == 'a') {
234 #ifdef __UCLIBC_HAS_HEXADECIMAL_FLOATS__
235                 *exp_buf = 'p';
236                 if (preci < 0) {
237                         preci = NUM_HEX_DIGITS;
238                         sufficient_precision = 1;
239                 }
240 #else
241                 mode += ('g' - 'a');
242 #endif
243         }
244
245         if (preci < 0) {
246                 preci = 6;
247         }
248
249         *sign_str = '\0';
250         if (PRINT_INFO_FLAG_VAL(info,showsign)) {
251                 *sign_str = '+';
252         } else if (PRINT_INFO_FLAG_VAL(info,space)) {
253                 *sign_str = ' ';
254         }
255
256         *(sign_str+1) = 0;
257         pc_fwi[5] = INF_OFFSET;
258         if (isnan(x)) {                         /* First, check for nan. */
259                 pc_fwi[5] = NAN_OFFSET;
260                 goto INF_NAN;
261         }
262
263         if (x == 0) {                           /* Handle 0 now to avoid false positive. */
264 #ifdef __UCLIBC_HAVE_SIGNED_ZERO__
265                 if (zeroisnegative(x)) { /* Handle 'signed' zero. */
266                         *sign_str = '-';
267                 }
268 #endif /* __UCLIBC_HAVE_SIGNED_ZERO__ */
269                 exp = -1;
270                 goto GENERATE_DIGITS;
271         }
272
273         if (x < 0) {                            /* Convert negatives to positives. */
274                 *sign_str = '-';
275                 x = -x;
276         }
277
278         if (__FPMAX_ZERO_OR_INF_CHECK(x)) {     /* Inf since zero handled above. */
279         INF_NAN:
280                 info->pad = ' ';
281                 ppc = pc_fwi + 6;
282                 pc_fwi[3] = FPO_STR_PREC;
283                 pc_fwi[4] = 3;
284                 if (mode < 'a') {
285                         pc_fwi[5] += 4;
286                 }
287                 pc_fwi[5] = (intptr_t)(fmt + pc_fwi[5]);
288                 goto EXIT_SPECIAL;
289         }
290
291         {
292                 int i, j;
293 #ifdef __UCLIBC_MJN3_ONLY__
294 #warning TODO: Clean up defines when hexadecimal float notation is unsupported.
295 #endif /* __UCLIBC_MJN3_ONLY__ */
296
297 #ifdef __UCLIBC_HAS_HEXADECIMAL_FLOATS__
298
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;
305                         j = EXP16_TABLE_MAX;
306                         dpb = HEX_DIGITS_PER_BLOCK;
307                         ndb = NUM_HEX_DIGIT_BLOCKS;
308                         nd = NUM_HEX_DIGITS;
309                         base = 16;
310                 } else {
311                         lower_bnd = 1e8;
312                         /*              upper_bnd = 1e9; */
313                         power_table = exp10_table;
314                         exp = DIGITS_PER_BLOCK - 1;
315                         i = EXP10_TABLE_SIZE;
316                         j = EXP10_TABLE_MAX;
317                         /*              dpb = DIGITS_PER_BLOCK; */
318                         /*              ndb = NUM_DIGIT_BLOCKS; */
319                         /*              base = 10; */
320                 }
321
322
323
324 #else  /* __UCLIBC_HAS_HEXADECIMAL_FLOATS__ */
325
326 #define lower_bnd    1e8
327 #define upper_bnd    1e9
328 #define power_table  exp10_table
329 #define dpb          DIGITS_PER_BLOCK
330 #define base         10
331 #define ndb          NUM_DIGIT_BLOCKS
332 #define nd           DECIMAL_DIG
333
334                 exp = DIGITS_PER_BLOCK - 1;
335                 i = EXP10_TABLE_SIZE;
336                 j = EXP10_TABLE_MAX;
337
338 #endif /* __UCLIBC_HAS_HEXADECIMAL_FLOATS__ */
339
340                 {
341                         int exp_neg = 0;
342                         if (x < lower_bnd) { /* Do we need to scale up or down? */
343                                 exp_neg = 1;
344                         }
345
346                         do {
347                                 --i;
348                                 if (exp_neg) {
349                                         if (x * power_table[i] < upper_bnd) {
350                                                 x *= power_table[i];
351                                                 exp -= j;
352                                         }
353                                 } else {
354                                         if (x / power_table[i] >= lower_bnd) {
355                                                 x /= power_table[i];
356                                                 exp += j;
357                                         }
358                                 }
359                                 j >>= 1;
360                         } while (i);
361                 }
362         }
363         if (x >= upper_bnd) {           /* Handle bad rounding case. */
364                 x /= power_table[0];
365                 ++exp;
366         }
367         assert(x < upper_bnd);
368
369  GENERATE_DIGITS:
370         {
371                 int i, j;
372                 s = buf + 2;                    /* Leave space for '\0' and '0'. */
373                 i = 0;
374                 do {
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;
381                         s += dpb;
382                         j = 0;
383                         do {
384                                 s[- ++j] = '0' + (digit_block % base);
385                                 digit_block /= base;
386                         } while (j < dpb);
387                 } while (++i < ndb);
388         }
389
390         /*************************************************************************/
391
392         if (mode < 'a') {
393                 *exp_buf -= ('a' - 'A'); /* e->E and p->P */
394                 mode += ('a' - 'A');
395         }
396
397         o_mode = mode;
398         if ((mode == 'g') && (preci > 0)){
399                 --preci;
400         }
401         round = preci;
402
403         if (mode == 'f') {
404                 round += exp;
405                 if (round < -1) {
406                         memset(buf, '0', DECIMAL_DIG); /* OK, since 'f' -> decimal case. */
407                     exp = -1;
408                     round = -1;
409                 }
410         }
411
412         s = buf;
413         *s++ = 0;                                       /* Terminator for rounding and 0-triming. */
414         *s = '0';                                       /* Space to round. */
415
416         {
417                 int i;
418                 i = 0;
419                 e = s + nd + 1;
420                 if (round < nd) {
421                         e = s + round + 2;
422                         if (*e >= '0' + (base/2)) {     /* NOTE: We always round away from 0! */
423                                 i = 1;
424                         }
425                 }
426
427                 do {                       /* Handle rounding and trim trailing 0s. */
428                         *--e += i;                      /* Add the carry. */
429                 } while ((*e == '0') || (*e > '0' - 1 + base));
430         }
431
432 #ifdef __UCLIBC_HAS_HEXADECIMAL_FLOATS__
433         if ((mode|0x20) == 'a') {
434                 char *q;
435
436                 for (q = e ; *q ; --q) {
437                         if (*q > '9') {
438                                 *q += (*exp_buf - ('p' - 'a') - '9' - 1);
439                         }
440                 }
441
442                 if (e > s) {
443                         exp *= 4;                       /* Change from base 16 to base 2. */
444                 }
445         }
446 #endif /* __UCLIBC_HAS_HEXADECIMAL_FLOATS__ */
447
448         o_exp = exp;
449         if (e <= s) {                           /* We carried into an extra digit. */
450                 ++o_exp;
451                 e = s;                                  /* Needed if all 0s. */
452         } else {
453                 ++s;
454         }
455         *++e = 0;                                       /* Terminating nul char. */
456
457         if ((mode == 'g') && ((o_exp >= -4) && (o_exp <= round))) {
458                 mode = 'f';
459                 preci = round - o_exp;
460         }
461
462         exp = o_exp;
463         if (mode != 'f') {
464                 o_exp = 0;
465         }
466
467         if (o_exp < 0) {                        /* Exponent is < 0, so */
468                 *--s = '0';                             /* fake the first 0 digit. */
469         }
470
471         pc_fwi[3] = FPO_ZERO_PAD;
472         pc_fwi[4] = 1;
473         pc_fwi[5] = (intptr_t)(sign_str + 4);
474         sign_str[4] = *s++;
475         sign_str[5] = 0;
476         ppc = pc_fwi + 6;
477
478         {
479                 int i = e - s;                  /* Total digits is 'i'. */
480                 if (o_exp >= 0) {
481 #ifdef __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__
482
483                         const char *p;
484
485                         if (PRINT_INFO_FLAG_VAL(info,group)
486                          && *(p = __UCLIBC_CURLOCALE->grouping)
487                         ) {
488                                 int nblk1;
489
490                                 nblk2 = nblk1 = *p;
491                                 if (*++p) {
492                                         nblk2 = *p;
493                                         assert(!*++p);
494                                 }
495
496                                 if (o_exp >= nblk1) {
497                                         num_groups = (o_exp - nblk1) / nblk2 + 1;
498                                         initial_group = (o_exp - nblk1) % nblk2;
499
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;
504                                                 tslen = 1;
505                                         } else {
506 #endif /* __UCLIBC_HAS_WCHAR__ */
507                                                 ts = __UCLIBC_CURLOCALE->thousands_sep;
508                                                 tslen = __UCLIBC_CURLOCALE->thousands_sep_len;
509 #ifdef __UCLIBC_HAS_WCHAR__
510                                         }
511 #endif /* __UCLIBC_HAS_WCHAR__ */
512
513                                         width -= num_groups * tslen;
514                                 }
515                         }
516
517
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 */
522                                 ppc[1] = i;
523                                 ppc += 3;
524                                 o_exp -= i;
525                                 i = 0;
526                                 if (o_exp>0) {          /* have 0s left of decimal */
527                                         ppc[0] = FPO_ZERO_PAD;
528                                         ppc[1] = o_exp;
529                                         ppc[2] = (intptr_t)(fmt + EMPTY_STRING_OFFSET);
530                                         ppc += 3;
531                                 }
532                         } else if (o_exp > 0) { /* decimal between digits */
533                                 ppc[1] = o_exp;
534                                 ppc += 3;
535                                 s += o_exp;
536                                 i -= o_exp;
537                         }
538                         o_exp = -1;
539                 }
540
541                 if (PRINT_INFO_FLAG_VAL(info,alt)
542                         || (i)
543                         || ((o_mode != 'g')
544 #ifdef __UCLIBC_HAS_HEXADECIMAL_FLOATS__
545                                 && (o_mode != 'a')
546 #endif /* __UCLIBC_HAS_HEXADECIMAL_FLOATS__ */
547                                 && (preci > 0))
548                         ) {
549                         ppc[0] = FPO_STR_PREC;
550 #ifdef __LOCALE_C_ONLY
551                         ppc[1] = 1;
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. */
557                                 ppc[1] = 1;
558                                 ppc[2] = (intptr_t)(fmt + DECPT_OFFSET);
559                         } else {
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__
564                         }
565 #endif /* __UCLIBC_HAS_WCHAR__ */
566 #endif /* __LOCALE_C_ONLY */
567                         ppc += 3;
568                 }
569
570                 if (++o_exp < 0) {                      /* Have 0s right of decimal. */
571                         ppc[0] = FPO_ZERO_PAD;
572                         ppc[1] = -o_exp;
573                         ppc[2] = (intptr_t)(fmt + EMPTY_STRING_OFFSET);
574                         ppc += 3;
575                 }
576                 if (i) {                                        /* Have digit(s) right of decimal. */
577                         ppc[0] = FPO_STR_PREC;
578                         ppc[1] = i;
579                         ppc[2] = (intptr_t)(s);
580                         ppc += 3;
581                 }
582
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__ */
587                         ) {
588                         i -= o_exp;
589                         if (i < preci) {                /* Have 0s right of digits. */
590                                 i = preci - i;
591                                 ppc[0] = FPO_ZERO_PAD;
592                                 ppc[1] = i;
593                                 ppc[2] = (intptr_t)(fmt + EMPTY_STRING_OFFSET);
594                                 ppc += 3;
595                         }
596                 }
597         }
598
599         /* Build exponent string. */
600         if (mode != 'f') {
601                 char *p = exp_buf + sizeof(exp_buf);
602                 int j;
603                 char exp_char = *exp_buf;
604                 char exp_sign = '+';
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__ */
610
611                 if (exp < 0) {
612                         exp_sign = '-';
613                         exp = -exp;
614                 }
615
616                 *--p = 0;                       /* nul-terminate */
617                 j = 2;                          /* Count exp_char and exp_sign. */
618                 do {
619                         *--p = '0' + (exp % 10);
620                         exp /= 10;
621                 } while ((++j < min_exp_dig_plus_2) || exp); /* char+sign+mindigits */
622                 *--p = exp_sign;
623                 *--p = exp_char;
624
625                 ppc[0] = FPO_STR_PREC;
626                 ppc[1] = j;
627                 ppc[2] = (intptr_t)(p);
628                 ppc += 3;
629         }
630
631  EXIT_SPECIAL:
632         {
633                 int i;
634                 ppc_last = ppc;
635                 ppc = pc_fwi + 4;        /* Need width fields starting with second. */
636                 do {
637                         width -= *ppc;
638                         ppc += 3;
639                 } while (ppc < ppc_last);
640
641                 ppc = pc_fwi;
642                 ppc[0] = FPO_STR_WIDTH;
643                 ppc[1] = i = ((*sign_str) != 0);
644                 ppc[2] = (intptr_t) sign_str;
645
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;
650                         *h = '0';
651                         *++h = 'x' - 'p' + *exp_buf;
652                         *++h = 0;
653                         ppc[1] = (i += 2);
654                 }
655 #endif /* __UCLIBC_HAS_HEXADECIMAL_FLOATS__ */
656
657                 if ((width -= i) > 0) {
658                         if (PRINT_INFO_FLAG_VAL(info,left)) { /* Left-justified. */
659                                 ppc_last[0] = FPO_STR_WIDTH;
660                                 ppc_last[1] = width;
661                                 ppc_last[2] = (intptr_t)(fmt + EMPTY_STRING_OFFSET);
662                                 ppc_last += 3;
663                         } else if (info->pad == '0') { /* 0 padding */
664                                 ppc[4] += width;        /* Pad second field. */
665                         } else {
666                                 ppc[1] += width;        /* Pad first (sign) field. */
667                         }
668                 }
669
670                 cnt = 0;
671         }
672
673         do {
674 #ifdef __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__
675
676                 if ((ppc == pc_fwi + 6) && num_groups) {
677                         const char *gp = (const char *) ppc[2];
678                         int len = ppc[1];
679                         int blk = initial_group;
680
681                         cnt += num_groups * tslen; /* Adjust count now for sep chars. */
682
683 /*                      __printf("\n"); */
684                         do {
685                                 if (!blk) {             /* Initial group could be 0 digits long! */
686                                         blk = nblk2;
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) {
690                                                 return -1;
691                                         }
692                                         assert(gp);
693                                         if (*gp) {
694                                                 gp += blk;
695                                         }
696                                         len -= blk;
697                                 } else {                /* Transition to 0s. */
698 /*                                      __printf("trans: len=%d blk=%d  \"%.*s\"\n", len, blk, len, gp); */
699                                         if (len) {
700 /*                                              __printf("len\n"); */
701                                                 if (fp_outfunc(fp, *ppc, len, (intptr_t) gp) != len) {
702                                                         return -1;
703                                                 }
704                                                 gp += len;
705                                         }
706
707                                         if (ppc[3] == FPO_ZERO_PAD) { /* Need to group 0s */
708 /*                                              __printf("zeropad\n"); */
709                                                 cnt += ppc[1];
710                                                 ppc += 3;
711                                                 gp = (const char *) ppc[2];
712                                                 blk -= len;     /* blk > len, so blk still > 0. */
713                                                 len = ppc[1];
714                                                 continue; /* Don't decrement num_groups here. */
715                                         } else {
716                                                 assert(num_groups == 0);
717                                                 break;
718                                         }
719                                 }
720
721                                 if (num_groups <= 0) {
722                                         break;
723                                 }
724                                 --num_groups;
725
726                                 if (fp_outfunc(fp, FPO_STR_PREC, tslen, (intptr_t) ts) != tslen) {
727                                         return -1;
728                                 }
729                                 blk = nblk2;
730
731 /*                              __printf("num_groups=%d   blk=%d\n", num_groups, blk); */
732
733                         } while (1);
734                 } else
735
736 #endif /* __UCLIBC_HAS_GLIBC_DIGIT_GROUPING__ */
737                 {                                               /* NOTE: Remember 'else' above! */
738                         if (fp_outfunc(fp, *ppc, ppc[1], ppc[2]) != ppc[1]) {
739                                 return -1;
740                         }
741                 }
742
743                 cnt += ppc[1];
744                 ppc += 3;
745         } while (ppc < ppc_last);
746
747         return cnt;
748 }