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