96
182
# define SNPRINTF swprintf
99
# define VASNPRINTF vasnprintf
101
# define DIRECTIVE char_directive
102
# define DIRECTIVES char_directives
103
# define PRINTF_PARSE printf_parse
104
# define USE_SNPRINTF (HAVE_DECL__SNPRINTF || HAVE_SNPRINTF)
185
/* TCHAR_T is char. */
186
# /* Use snprintf if it exists under the name 'snprintf' or '_snprintf'.
187
But don't use it on BeOS, since BeOS snprintf produces no output if the
188
size argument is >= 0x3000000. */
189
# if (HAVE_DECL__SNPRINTF || HAVE_SNPRINTF) && !defined __BEOS__
190
# define USE_SNPRINTF 1
192
# define USE_SNPRINTF 0
105
194
# if HAVE_DECL__SNPRINTF
107
196
# define SNPRINTF _snprintf
110
199
# define SNPRINTF snprintf
115
VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list args)
200
/* Here we need to call the native snprintf, not rpl_snprintf. */
204
/* Here we need to call the native sprintf, not rpl_sprintf. */
207
#if (NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE) && !defined IN_LIBINTL
208
/* Determine the decimal-point character according to the current locale. */
209
# ifndef decimal_point_char_defined
210
# define decimal_point_char_defined 1
212
decimal_point_char ()
215
/* Determine it in a multithread-safe way. We know nl_langinfo is
216
multithread-safe on glibc systems, but is not required to be multithread-
217
safe by POSIX. sprintf(), however, is multithread-safe. localeconv()
218
is rarely multithread-safe. */
219
# if HAVE_NL_LANGINFO && __GLIBC__
220
point = nl_langinfo (RADIXCHAR);
223
sprintf (pointbuf, "%#.0f", 1.0);
224
point = &pointbuf[1];
226
point = localeconv () -> decimal_point;
228
/* The decimal point is always a single byte: either '.' or ','. */
229
return (point[0] != '\0' ? point[0] : '.');
234
#if NEED_PRINTF_INFINITE_DOUBLE && !NEED_PRINTF_DOUBLE && !defined IN_LIBINTL
236
/* Equivalent to !isfinite(x) || x == 0, but does not require libm. */
238
is_infinite_or_zero (double x)
240
return isnan (x) || x + x == x;
245
#if NEED_PRINTF_INFINITE_LONG_DOUBLE && !NEED_PRINTF_LONG_DOUBLE && !defined IN_LIBINTL
247
/* Equivalent to !isfinite(x), but does not require libm. */
249
is_infinitel (long double x)
251
return isnanl (x) || (x + x == x && x != 0.0L);
256
#if (NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_DOUBLE) && !defined IN_LIBINTL
258
/* Converting 'long double' to decimal without rare rounding bugs requires
259
real bignums. We use the naming conventions of GNU gmp, but vastly simpler
260
(and slower) algorithms. */
262
typedef unsigned int mp_limb_t;
263
# define GMP_LIMB_BITS 32
264
typedef int mp_limb_verify[2 * (sizeof (mp_limb_t) * CHAR_BIT == GMP_LIMB_BITS) - 1];
266
typedef unsigned long long mp_twolimb_t;
267
# define GMP_TWOLIMB_BITS 64
268
typedef int mp_twolimb_verify[2 * (sizeof (mp_twolimb_t) * CHAR_BIT == GMP_TWOLIMB_BITS) - 1];
270
/* Representation of a bignum >= 0. */
274
mp_limb_t *limbs; /* Bits in little-endian order, allocated with malloc(). */
277
/* Compute the product of two bignums >= 0.
278
Return the allocated memory in case of success, NULL in case of memory
279
allocation failure. */
281
multiply (mpn_t src1, mpn_t src2, mpn_t *dest)
288
if (src1.nlimbs <= src2.nlimbs)
302
/* Now 0 <= len1 <= len2. */
305
/* src1 or src2 is zero. */
307
dest->limbs = (mp_limb_t *) malloc (1);
311
/* Here 1 <= len1 <= len2. */
317
dp = (mp_limb_t *) malloc (dlen * sizeof (mp_limb_t));
320
for (k = len2; k > 0; )
322
for (i = 0; i < len1; i++)
324
mp_limb_t digit1 = p1[i];
325
mp_twolimb_t carry = 0;
326
for (j = 0; j < len2; j++)
328
mp_limb_t digit2 = p2[j];
329
carry += (mp_twolimb_t) digit1 * (mp_twolimb_t) digit2;
331
dp[i + j] = (mp_limb_t) carry;
332
carry = carry >> GMP_LIMB_BITS;
334
dp[i + len2] = (mp_limb_t) carry;
337
while (dlen > 0 && dp[dlen - 1] == 0)
345
/* Compute the quotient of a bignum a >= 0 and a bignum b > 0.
346
a is written as a = q * b + r with 0 <= r < b. q is the quotient, r
348
Finally, round-to-even is performed: If r > b/2 or if r = b/2 and q is odd,
350
Return the allocated memory in case of success, NULL in case of memory
351
allocation failure. */
353
divide (mpn_t a, mpn_t b, mpn_t *q)
356
First normalise a and b: a=[a[m-1],...,a[0]], b=[b[n-1],...,b[0]]
357
with m>=0 and n>0 (in base beta = 2^GMP_LIMB_BITS).
358
If m<n, then q:=0 and r:=a.
359
If m>=n=1, perform a single-precision division:
362
{Here (q[m-1]*beta^(m-1)+...+q[j]*beta^j) * b[0] + r*beta^j =
363
= a[m-1]*beta^(m-1)+...+a[j]*beta^j und 0<=r<b[0]<beta}
364
j:=j-1, r:=r*beta+a[j], q[j]:=floor(r/b[0]), r:=r-b[0]*q[j].
365
Normalise [q[m-1],...,q[0]], yields q.
366
If m>=n>1, perform a multiple-precision division:
367
We have a/b < beta^(m-n+1).
368
s:=intDsize-1-(hightest bit in b[n-1]), 0<=s<intDsize.
369
Shift a and b left by s bits, copying them. r:=a.
370
r=[r[m],...,r[0]], b=[b[n-1],...,b[0]] with b[n-1]>=beta/2.
371
For j=m-n,...,0: {Here 0 <= r < b*beta^(j+1).}
373
q* := floor((r[j+n]*beta+r[j+n-1])/b[n-1]).
374
In case of overflow (q* >= beta) set q* := beta-1.
375
Compute c2 := ((r[j+n]*beta+r[j+n-1]) - q* * b[n-1])*beta + r[j+n-2]
376
and c3 := b[n-2] * q*.
377
{We have 0 <= c2 < 2*beta^2, even 0 <= c2 < beta^2 if no overflow
378
occurred. Furthermore 0 <= c3 < beta^2.
379
If there was overflow and
380
r[j+n]*beta+r[j+n-1] - q* * b[n-1] >= beta, i.e. c2 >= beta^2,
381
the next test can be skipped.}
382
While c3 > c2, {Here 0 <= c2 < c3 < beta^2}
383
Put q* := q* - 1, c2 := c2 + b[n-1]*beta, c3 := c3 - b[n-2].
385
Put r := r - b * q* * beta^j. In detail:
386
[r[n+j],...,r[j]] := [r[n+j],...,r[j]] - q* * [b[n-1],...,b[0]].
387
hence: u:=0, for i:=0 to n-1 do
389
r[j+i]:=r[j+i]-(u mod beta) (+ beta, if carry),
390
u:=u div beta (+ 1, if carry in subtraction)
392
{Since always u = (q* * [b[i-1],...,b[0]] div beta^i) + 1
394
the carry u does not overflow.}
395
If a negative carry occurs, put q* := q* - 1
396
and [r[n+j],...,r[j]] := [r[n+j],...,r[j]] + [0,b[n-1],...,b[0]].
398
Normalise [q[m-n],..,q[0]]; this yields the quotient q.
399
Shift [r[n-1],...,r[0]] right by s bits and normalise; this yields the
401
The room for q[j] can be allocated at the memory location of r[n+j].
402
Finally, round-to-even:
403
Shift r left by 1 bit.
404
If r > b or if r = b and q[0] is odd, q := q+1.
406
const mp_limb_t *a_ptr = a.limbs;
407
size_t a_len = a.nlimbs;
408
const mp_limb_t *b_ptr = b.limbs;
409
size_t b_len = b.nlimbs;
411
mp_limb_t *tmp_roomptr = NULL;
417
/* Allocate room for a_len+2 digits.
418
(Need a_len+1 digits for the real division and 1 more digit for the
419
final rounding of q.) */
420
roomptr = (mp_limb_t *) malloc ((a_len + 2) * sizeof (mp_limb_t));
425
while (a_len > 0 && a_ptr[a_len - 1] == 0)
432
/* Division by zero. */
434
if (b_ptr[b_len - 1] == 0)
440
/* Here m = a_len >= 0 and n = b_len > 0. */
444
/* m<n: trivial case. q=0, r := copy of a. */
447
memcpy (r_ptr, a_ptr, a_len * sizeof (mp_limb_t));
448
q_ptr = roomptr + a_len;
453
/* n=1: single precision division.
454
beta^(m-1) <= a < beta^m ==> beta^(m-2) <= a/b < beta^m */
458
mp_limb_t den = b_ptr[0];
459
mp_limb_t remainder = 0;
460
const mp_limb_t *sourceptr = a_ptr + a_len;
461
mp_limb_t *destptr = q_ptr + a_len;
463
for (count = a_len; count > 0; count--)
466
((mp_twolimb_t) remainder << GMP_LIMB_BITS) | *--sourceptr;
467
*--destptr = num / den;
468
remainder = num % den;
470
/* Normalise and store r. */
473
r_ptr[0] = remainder;
480
if (q_ptr[q_len - 1] == 0)
486
/* n>1: multiple precision division.
487
beta^(m-1) <= a < beta^m, beta^(n-1) <= b < beta^n ==>
488
beta^(m-n-1) <= a/b < beta^(m-n+1). */
492
mp_limb_t msd = b_ptr[b_len - 1]; /* = b[n-1], > 0 */
520
/* 0 <= s < GMP_LIMB_BITS.
521
Copy b, shifting it left by s bits. */
524
tmp_roomptr = (mp_limb_t *) malloc (b_len * sizeof (mp_limb_t));
525
if (tmp_roomptr == NULL)
531
const mp_limb_t *sourceptr = b_ptr;
532
mp_limb_t *destptr = tmp_roomptr;
533
mp_twolimb_t accu = 0;
535
for (count = b_len; count > 0; count--)
537
accu += (mp_twolimb_t) *sourceptr++ << s;
538
*destptr++ = (mp_limb_t) accu;
539
accu = accu >> GMP_LIMB_BITS;
541
/* accu must be zero, since that was how s was determined. */
547
/* Copy a, shifting it left by s bits, yields r.
549
At the beginning: r = roomptr[0..a_len],
550
at the end: r = roomptr[0..b_len-1], q = roomptr[b_len..a_len] */
554
memcpy (r_ptr, a_ptr, a_len * sizeof (mp_limb_t));
559
const mp_limb_t *sourceptr = a_ptr;
560
mp_limb_t *destptr = r_ptr;
561
mp_twolimb_t accu = 0;
563
for (count = a_len; count > 0; count--)
565
accu += (mp_twolimb_t) *sourceptr++ << s;
566
*destptr++ = (mp_limb_t) accu;
567
accu = accu >> GMP_LIMB_BITS;
569
*destptr++ = (mp_limb_t) accu;
571
q_ptr = roomptr + b_len;
572
q_len = a_len - b_len + 1; /* q will have m-n+1 limbs */
574
size_t j = a_len - b_len; /* m-n */
575
mp_limb_t b_msd = b_ptr[b_len - 1]; /* b[n-1] */
576
mp_limb_t b_2msd = b_ptr[b_len - 2]; /* b[n-2] */
577
mp_twolimb_t b_msdd = /* b[n-1]*beta+b[n-2] */
578
((mp_twolimb_t) b_msd << GMP_LIMB_BITS) | b_2msd;
579
/* Division loop, traversed m-n+1 times.
580
j counts down, b is unchanged, beta/2 <= b[n-1] < beta. */
585
if (r_ptr[j + b_len] < b_msd) /* r[j+n] < b[n-1] ? */
587
/* Divide r[j+n]*beta+r[j+n-1] by b[n-1], no overflow. */
589
((mp_twolimb_t) r_ptr[j + b_len] << GMP_LIMB_BITS)
590
| r_ptr[j + b_len - 1];
591
q_star = num / b_msd;
596
/* Overflow, hence r[j+n]*beta+r[j+n-1] >= beta*b[n-1]. */
597
q_star = (mp_limb_t)~(mp_limb_t)0; /* q* = beta-1 */
598
/* Test whether r[j+n]*beta+r[j+n-1] - (beta-1)*b[n-1] >= beta
599
<==> r[j+n]*beta+r[j+n-1] + b[n-1] >= beta*b[n-1]+beta
600
<==> b[n-1] < floor((r[j+n]*beta+r[j+n-1]+b[n-1])/beta)
602
If yes, jump directly to the subtraction loop.
603
(Otherwise, r[j+n]*beta+r[j+n-1] - (beta-1)*b[n-1] < beta
604
<==> floor((r[j+n]*beta+r[j+n-1]+b[n-1])/beta) = b[n-1] ) */
605
if (r_ptr[j + b_len] > b_msd
606
|| (c1 = r_ptr[j + b_len - 1] + b_msd) < b_msd)
607
/* r[j+n] >= b[n-1]+1 or
608
r[j+n] = b[n-1] and the addition r[j+n-1]+b[n-1] gives a
613
c1 = (r[j+n]*beta+r[j+n-1]) - q* * b[n-1] (>=0, <beta). */
615
mp_twolimb_t c2 = /* c1*beta+r[j+n-2] */
616
((mp_twolimb_t) c1 << GMP_LIMB_BITS) | r_ptr[j + b_len - 2];
617
mp_twolimb_t c3 = /* b[n-2] * q* */
618
(mp_twolimb_t) b_2msd * (mp_twolimb_t) q_star;
619
/* While c2 < c3, increase c2 and decrease c3.
620
Consider c3-c2. While it is > 0, decrease it by
621
b[n-1]*beta+b[n-2]. Because of b[n-1]*beta+b[n-2] >= beta^2/2
622
this can happen only twice. */
625
q_star = q_star - 1; /* q* := q* - 1 */
626
if (c3 - c2 > b_msdd)
627
q_star = q_star - 1; /* q* := q* - 1 */
633
/* Subtract r := r - b * q* * beta^j. */
636
const mp_limb_t *sourceptr = b_ptr;
637
mp_limb_t *destptr = r_ptr + j;
638
mp_twolimb_t carry = 0;
640
for (count = b_len; count > 0; count--)
642
/* Here 0 <= carry <= q*. */
645
+ (mp_twolimb_t) q_star * (mp_twolimb_t) *sourceptr++
646
+ (mp_limb_t) ~(*destptr);
647
/* Here 0 <= carry <= beta*q* + beta-1. */
648
*destptr++ = ~(mp_limb_t) carry;
649
carry = carry >> GMP_LIMB_BITS; /* <= q* */
651
cr = (mp_limb_t) carry;
653
/* Subtract cr from r_ptr[j + b_len], then forget about
655
if (cr > r_ptr[j + b_len])
657
/* Subtraction gave a carry. */
658
q_star = q_star - 1; /* q* := q* - 1 */
661
const mp_limb_t *sourceptr = b_ptr;
662
mp_limb_t *destptr = r_ptr + j;
665
for (count = b_len; count > 0; count--)
667
mp_limb_t source1 = *sourceptr++;
668
mp_limb_t source2 = *destptr;
669
*destptr++ = source1 + source2 + carry;
672
? source1 >= (mp_limb_t) ~source2
673
: source1 > (mp_limb_t) ~source2);
676
/* Forget about the carry and about r[j+n]. */
679
/* q* is determined. Store it as q[j]. */
688
if (q_ptr[q_len - 1] == 0)
690
# if 0 /* Not needed here, since we need r only to compare it with b/2, and
691
b is shifted left by s bits. */
692
/* Shift r right by s bits. */
695
mp_limb_t ptr = r_ptr + r_len;
696
mp_twolimb_t accu = 0;
698
for (count = r_len; count > 0; count--)
700
accu = (mp_twolimb_t) (mp_limb_t) accu << GMP_LIMB_BITS;
701
accu += (mp_twolimb_t) *--ptr << (GMP_LIMB_BITS - s);
702
*ptr = (mp_limb_t) (accu >> GMP_LIMB_BITS);
707
while (r_len > 0 && r_ptr[r_len - 1] == 0)
710
/* Compare r << 1 with b. */
718
(i <= r_len && i > 0 ? r_ptr[i - 1] >> (GMP_LIMB_BITS - 1) : 0)
719
| (i < r_len ? r_ptr[i] << 1 : 0);
720
mp_limb_t b_i = (i < b_len ? b_ptr[i] : 0);
730
if (q_len > 0 && ((q_ptr[0] & 1) != 0))
735
for (i = 0; i < q_len; i++)
736
if (++(q_ptr[i]) != 0)
741
if (tmp_roomptr != NULL)
748
/* Convert a bignum a >= 0, multiplied with 10^extra_zeroes, to decimal
750
Destroys the contents of a.
751
Return the allocated memory - containing the decimal digits in low-to-high
752
order, terminated with a NUL character - in case of success, NULL in case
753
of memory allocation failure. */
755
convert_to_decimal (mpn_t a, size_t extra_zeroes)
757
mp_limb_t *a_ptr = a.limbs;
758
size_t a_len = a.nlimbs;
759
/* 0.03345 is slightly larger than log(2)/(9*log(10)). */
760
size_t c_len = 9 * ((size_t)(a_len * (GMP_LIMB_BITS * 0.03345f)) + 1);
761
char *c_ptr = (char *) malloc (xsum (c_len, extra_zeroes));
765
for (; extra_zeroes > 0; extra_zeroes--)
769
/* Divide a by 10^9, in-place. */
770
mp_limb_t remainder = 0;
771
mp_limb_t *ptr = a_ptr + a_len;
773
for (count = a_len; count > 0; count--)
776
((mp_twolimb_t) remainder << GMP_LIMB_BITS) | *--ptr;
777
*ptr = num / 1000000000;
778
remainder = num % 1000000000;
780
/* Store the remainder as 9 decimal digits. */
781
for (count = 9; count > 0; count--)
783
*d_ptr++ = '0' + (remainder % 10);
784
remainder = remainder / 10;
787
if (a_ptr[a_len - 1] == 0)
790
/* Remove leading zeroes. */
791
while (d_ptr > c_ptr && d_ptr[-1] == '0')
793
/* But keep at least one zero. */
796
/* Terminate the string. */
802
# if NEED_PRINTF_LONG_DOUBLE
804
/* Assuming x is finite and >= 0:
805
write x as x = 2^e * m, where m is a bignum.
806
Return the allocated memory in case of success, NULL in case of memory
807
allocation failure. */
809
decode_long_double (long double x, int *ep, mpn_t *mp)
816
/* Allocate memory for result. */
817
m.nlimbs = (LDBL_MANT_BIT + GMP_LIMB_BITS - 1) / GMP_LIMB_BITS;
818
m.limbs = (mp_limb_t *) malloc (m.nlimbs * sizeof (mp_limb_t));
821
/* Split into exponential part and mantissa. */
822
y = frexpl (x, &exp);
823
if (!(y >= 0.0L && y < 1.0L))
825
/* x = 2^exp * y = 2^(exp - LDBL_MANT_BIT) * (y * LDBL_MANT_BIT), and the
826
latter is an integer. */
827
/* Convert the mantissa (y * LDBL_MANT_BIT) to a sequence of limbs.
828
I'm not sure whether it's safe to cast a 'long double' value between
829
2^31 and 2^32 to 'unsigned int', therefore play safe and cast only
830
'long double' values between 0 and 2^16 (to 'unsigned int' or 'int',
832
# if (LDBL_MANT_BIT % GMP_LIMB_BITS) != 0
833
# if (LDBL_MANT_BIT % GMP_LIMB_BITS) > GMP_LIMB_BITS / 2
836
y *= (mp_limb_t) 1 << (LDBL_MANT_BIT % (GMP_LIMB_BITS / 2));
839
if (!(y >= 0.0L && y < 1.0L))
841
y *= (mp_limb_t) 1 << (GMP_LIMB_BITS / 2);
844
if (!(y >= 0.0L && y < 1.0L))
846
m.limbs[LDBL_MANT_BIT / GMP_LIMB_BITS] = (hi << (GMP_LIMB_BITS / 2)) | lo;
851
y *= (mp_limb_t) 1 << (LDBL_MANT_BIT % GMP_LIMB_BITS);
854
if (!(y >= 0.0L && y < 1.0L))
856
m.limbs[LDBL_MANT_BIT / GMP_LIMB_BITS] = d;
860
for (i = LDBL_MANT_BIT / GMP_LIMB_BITS; i > 0; )
863
y *= (mp_limb_t) 1 << (GMP_LIMB_BITS / 2);
866
if (!(y >= 0.0L && y < 1.0L))
868
y *= (mp_limb_t) 1 << (GMP_LIMB_BITS / 2);
871
if (!(y >= 0.0L && y < 1.0L))
873
m.limbs[--i] = (hi << (GMP_LIMB_BITS / 2)) | lo;
878
while (m.nlimbs > 0 && m.limbs[m.nlimbs - 1] == 0)
881
*ep = exp - LDBL_MANT_BIT;
887
# if NEED_PRINTF_DOUBLE
889
/* Assuming x is finite and >= 0:
890
write x as x = 2^e * m, where m is a bignum.
891
Return the allocated memory in case of success, NULL in case of memory
892
allocation failure. */
894
decode_double (double x, int *ep, mpn_t *mp)
901
/* Allocate memory for result. */
902
m.nlimbs = (DBL_MANT_BIT + GMP_LIMB_BITS - 1) / GMP_LIMB_BITS;
903
m.limbs = (mp_limb_t *) malloc (m.nlimbs * sizeof (mp_limb_t));
906
/* Split into exponential part and mantissa. */
908
if (!(y >= 0.0 && y < 1.0))
910
/* x = 2^exp * y = 2^(exp - DBL_MANT_BIT) * (y * DBL_MANT_BIT), and the
911
latter is an integer. */
912
/* Convert the mantissa (y * DBL_MANT_BIT) to a sequence of limbs.
913
I'm not sure whether it's safe to cast a 'double' value between
914
2^31 and 2^32 to 'unsigned int', therefore play safe and cast only
915
'double' values between 0 and 2^16 (to 'unsigned int' or 'int',
917
# if (DBL_MANT_BIT % GMP_LIMB_BITS) != 0
918
# if (DBL_MANT_BIT % GMP_LIMB_BITS) > GMP_LIMB_BITS / 2
921
y *= (mp_limb_t) 1 << (DBL_MANT_BIT % (GMP_LIMB_BITS / 2));
924
if (!(y >= 0.0 && y < 1.0))
926
y *= (mp_limb_t) 1 << (GMP_LIMB_BITS / 2);
929
if (!(y >= 0.0 && y < 1.0))
931
m.limbs[DBL_MANT_BIT / GMP_LIMB_BITS] = (hi << (GMP_LIMB_BITS / 2)) | lo;
936
y *= (mp_limb_t) 1 << (DBL_MANT_BIT % GMP_LIMB_BITS);
939
if (!(y >= 0.0 && y < 1.0))
941
m.limbs[DBL_MANT_BIT / GMP_LIMB_BITS] = d;
945
for (i = DBL_MANT_BIT / GMP_LIMB_BITS; i > 0; )
948
y *= (mp_limb_t) 1 << (GMP_LIMB_BITS / 2);
951
if (!(y >= 0.0 && y < 1.0))
953
y *= (mp_limb_t) 1 << (GMP_LIMB_BITS / 2);
956
if (!(y >= 0.0 && y < 1.0))
958
m.limbs[--i] = (hi << (GMP_LIMB_BITS / 2)) | lo;
963
while (m.nlimbs > 0 && m.limbs[m.nlimbs - 1] == 0)
966
*ep = exp - DBL_MANT_BIT;
972
/* Assuming x = 2^e * m is finite and >= 0, and n is an integer:
973
Returns the decimal representation of round (x * 10^n).
974
Return the allocated memory - containing the decimal digits in low-to-high
975
order, terminated with a NUL character - in case of success, NULL in case
976
of memory allocation failure. */
978
scale10_round_decimal_decoded (int e, mpn_t m, void *memory, int n)
986
unsigned int s_limbs;
995
/* x = 2^e * m, hence
996
y = round (2^e * 10^n * m) = round (2^(e+n) * 5^n * m)
997
= round (2^s * 5^n * m). */
1000
/* Factor out a common power of 10 if possible. */
1003
extra_zeroes = (s < n ? s : n);
1007
/* Here y = round (2^s * 5^n * m) * 10^extra_zeroes.
1008
Before converting to decimal, we need to compute
1009
z = round (2^s * 5^n * m). */
1010
/* Compute 5^|n|, possibly shifted by |s| bits if n and s have the same
1011
sign. 2.322 is slightly larger than log(5)/log(2). */
1012
abs_n = (n >= 0 ? n : -n);
1013
abs_s = (s >= 0 ? s : -s);
1014
pow5_ptr = (mp_limb_t *) malloc (((int)(abs_n * (2.322f / GMP_LIMB_BITS)) + 1
1015
+ abs_s / GMP_LIMB_BITS + 1)
1016
* sizeof (mp_limb_t));
1017
if (pow5_ptr == NULL)
1022
/* Initialize with 1. */
1025
/* Multiply with 5^|n|. */
1028
static mp_limb_t const small_pow5[13 + 1] =
1030
1, 5, 25, 125, 625, 3125, 15625, 78125, 390625, 1953125, 9765625,
1031
48828125, 244140625, 1220703125
1034
for (n13 = 0; n13 <= abs_n; n13 += 13)
1036
mp_limb_t digit1 = small_pow5[n13 + 13 <= abs_n ? 13 : abs_n - n13];
1038
mp_twolimb_t carry = 0;
1039
for (j = 0; j < pow5_len; j++)
1041
mp_limb_t digit2 = pow5_ptr[j];
1042
carry += (mp_twolimb_t) digit1 * (mp_twolimb_t) digit2;
1043
pow5_ptr[j] = (mp_limb_t) carry;
1044
carry = carry >> GMP_LIMB_BITS;
1047
pow5_ptr[pow5_len++] = (mp_limb_t) carry;
1050
s_limbs = abs_s / GMP_LIMB_BITS;
1051
s_bits = abs_s % GMP_LIMB_BITS;
1052
if (n >= 0 ? s >= 0 : s <= 0)
1054
/* Multiply with 2^|s|. */
1057
mp_limb_t *ptr = pow5_ptr;
1058
mp_twolimb_t accu = 0;
1060
for (count = pow5_len; count > 0; count--)
1062
accu += (mp_twolimb_t) *ptr << s_bits;
1063
*ptr++ = (mp_limb_t) accu;
1064
accu = accu >> GMP_LIMB_BITS;
1068
*ptr = (mp_limb_t) accu;
1075
for (count = pow5_len; count > 0;)
1078
pow5_ptr[s_limbs + count] = pow5_ptr[count];
1080
for (count = s_limbs; count > 0;)
1083
pow5_ptr[count] = 0;
1085
pow5_len += s_limbs;
1087
pow5.limbs = pow5_ptr;
1088
pow5.nlimbs = pow5_len;
1091
/* Multiply m with pow5. No division needed. */
1092
z_memory = multiply (m, pow5, &z);
1096
/* Divide m by pow5 and round. */
1097
z_memory = divide (m, pow5, &z);
1102
pow5.limbs = pow5_ptr;
1103
pow5.nlimbs = pow5_len;
1107
Multiply m with pow5, then divide by 2^|s|. */
1111
tmp_memory = multiply (m, pow5, &numerator);
1112
if (tmp_memory == NULL)
1118
/* Construct 2^|s|. */
1120
mp_limb_t *ptr = pow5_ptr + pow5_len;
1122
for (i = 0; i < s_limbs; i++)
1124
ptr[s_limbs] = (mp_limb_t) 1 << s_bits;
1125
denominator.limbs = ptr;
1126
denominator.nlimbs = s_limbs + 1;
1128
z_memory = divide (numerator, denominator, &z);
1134
Multiply m with 2^s, then divide by pow5. */
1137
num_ptr = (mp_limb_t *) malloc ((m.nlimbs + s_limbs + 1)
1138
* sizeof (mp_limb_t));
1139
if (num_ptr == NULL)
1146
mp_limb_t *destptr = num_ptr;
1149
for (i = 0; i < s_limbs; i++)
1154
const mp_limb_t *sourceptr = m.limbs;
1155
mp_twolimb_t accu = 0;
1157
for (count = m.nlimbs; count > 0; count--)
1159
accu += (mp_twolimb_t) *sourceptr++ << s_bits;
1160
*destptr++ = (mp_limb_t) accu;
1161
accu = accu >> GMP_LIMB_BITS;
1164
*destptr++ = (mp_limb_t) accu;
1168
const mp_limb_t *sourceptr = m.limbs;
1170
for (count = m.nlimbs; count > 0; count--)
1171
*destptr++ = *sourceptr++;
1173
numerator.limbs = num_ptr;
1174
numerator.nlimbs = destptr - num_ptr;
1176
z_memory = divide (numerator, pow5, &z);
1183
/* Here y = round (x * 10^n) = z * 10^extra_zeroes. */
1185
if (z_memory == NULL)
1187
digits = convert_to_decimal (z, extra_zeroes);
1192
# if NEED_PRINTF_LONG_DOUBLE
1194
/* Assuming x is finite and >= 0, and n is an integer:
1195
Returns the decimal representation of round (x * 10^n).
1196
Return the allocated memory - containing the decimal digits in low-to-high
1197
order, terminated with a NUL character - in case of success, NULL in case
1198
of memory allocation failure. */
1200
scale10_round_decimal_long_double (long double x, int n)
1204
void *memory = decode_long_double (x, &e, &m);
1205
return scale10_round_decimal_decoded (e, m, memory, n);
1210
# if NEED_PRINTF_DOUBLE
1212
/* Assuming x is finite and >= 0, and n is an integer:
1213
Returns the decimal representation of round (x * 10^n).
1214
Return the allocated memory - containing the decimal digits in low-to-high
1215
order, terminated with a NUL character - in case of success, NULL in case
1216
of memory allocation failure. */
1218
scale10_round_decimal_double (double x, int n)
1222
void *memory = decode_double (x, &e, &m);
1223
return scale10_round_decimal_decoded (e, m, memory, n);
1228
# if NEED_PRINTF_LONG_DOUBLE
1230
/* Assuming x is finite and > 0:
1231
Return an approximation for n with 10^n <= x < 10^(n+1).
1232
The approximation is usually the right n, but may be off by 1 sometimes. */
1234
floorlog10l (long double x)
1241
/* Split into exponential part and mantissa. */
1242
y = frexpl (x, &exp);
1243
if (!(y >= 0.0L && y < 1.0L))
1249
while (y < (1.0L / (1 << (GMP_LIMB_BITS / 2)) / (1 << (GMP_LIMB_BITS / 2))))
1251
y *= 1.0L * (1 << (GMP_LIMB_BITS / 2)) * (1 << (GMP_LIMB_BITS / 2));
1252
exp -= GMP_LIMB_BITS;
1254
if (y < (1.0L / (1 << 16)))
1256
y *= 1.0L * (1 << 16);
1259
if (y < (1.0L / (1 << 8)))
1261
y *= 1.0L * (1 << 8);
1264
if (y < (1.0L / (1 << 4)))
1266
y *= 1.0L * (1 << 4);
1269
if (y < (1.0L / (1 << 2)))
1271
y *= 1.0L * (1 << 2);
1274
if (y < (1.0L / (1 << 1)))
1276
y *= 1.0L * (1 << 1);
1280
if (!(y >= 0.5L && y < 1.0L))
1282
/* Compute an approximation for l = log2(x) = exp + log2(y). */
1285
if (z < 0.70710678118654752444)
1287
z *= 1.4142135623730950488;
1290
if (z < 0.8408964152537145431)
1292
z *= 1.1892071150027210667;
1295
if (z < 0.91700404320467123175)
1297
z *= 1.0905077326652576592;
1300
if (z < 0.9576032806985736469)
1302
z *= 1.0442737824274138403;
1305
/* Now 0.95 <= z <= 1.01. */
1307
/* log(1-z) = - z - z^2/2 - z^3/3 - z^4/4 - ...
1308
Four terms are enough to get an approximation with error < 10^-7. */
1309
l -= z * (1.0 + z * (0.5 + z * ((1.0 / 3) + z * 0.25)));
1310
/* Finally multiply with log(2)/log(10), yields an approximation for
1312
l *= 0.30102999566398119523;
1313
/* Round down to the next integer. */
1314
return (int) l + (l < 0 ? -1 : 0);
1319
# if NEED_PRINTF_DOUBLE
1321
/* Assuming x is finite and > 0:
1322
Return an approximation for n with 10^n <= x < 10^(n+1).
1323
The approximation is usually the right n, but may be off by 1 sometimes. */
1325
floorlog10 (double x)
1332
/* Split into exponential part and mantissa. */
1333
y = frexp (x, &exp);
1334
if (!(y >= 0.0 && y < 1.0))
1340
while (y < (1.0 / (1 << (GMP_LIMB_BITS / 2)) / (1 << (GMP_LIMB_BITS / 2))))
1342
y *= 1.0 * (1 << (GMP_LIMB_BITS / 2)) * (1 << (GMP_LIMB_BITS / 2));
1343
exp -= GMP_LIMB_BITS;
1345
if (y < (1.0 / (1 << 16)))
1347
y *= 1.0 * (1 << 16);
1350
if (y < (1.0 / (1 << 8)))
1352
y *= 1.0 * (1 << 8);
1355
if (y < (1.0 / (1 << 4)))
1357
y *= 1.0 * (1 << 4);
1360
if (y < (1.0 / (1 << 2)))
1362
y *= 1.0 * (1 << 2);
1365
if (y < (1.0 / (1 << 1)))
1367
y *= 1.0 * (1 << 1);
1371
if (!(y >= 0.5 && y < 1.0))
1373
/* Compute an approximation for l = log2(x) = exp + log2(y). */
1376
if (z < 0.70710678118654752444)
1378
z *= 1.4142135623730950488;
1381
if (z < 0.8408964152537145431)
1383
z *= 1.1892071150027210667;
1386
if (z < 0.91700404320467123175)
1388
z *= 1.0905077326652576592;
1391
if (z < 0.9576032806985736469)
1393
z *= 1.0442737824274138403;
1396
/* Now 0.95 <= z <= 1.01. */
1398
/* log(1-z) = - z - z^2/2 - z^3/3 - z^4/4 - ...
1399
Four terms are enough to get an approximation with error < 10^-7. */
1400
l -= z * (1.0 + z * (0.5 + z * ((1.0 / 3) + z * 0.25)));
1401
/* Finally multiply with log(2)/log(10), yields an approximation for
1403
l *= 0.30102999566398119523;
1404
/* Round down to the next integer. */
1405
return (int) l + (l < 0 ? -1 : 0);
1413
VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
1414
const FCHAR_T *format, va_list args)
120
1419
if (PRINTF_PARSE (format, &d, &a) < 0)
1420
/* errno is already set. */
126
1423
#define CLEANUP() \
131
if (printf_fetchargs (args, &a) < 0)
1428
if (PRINTF_FETCHARGS (args, &a) < 0)
1578
/* The unistdio extensions. */
1579
else if (dp->conversion == 'U')
1581
arg_type type = a.arg[dp->arg_index].type;
1582
int flags = dp->flags;
1590
if (dp->width_start != dp->width_end)
1592
if (dp->width_arg_index != ARG_NONE)
1596
if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
1598
arg = a.arg[dp->width_arg_index].a.a_int;
1601
/* "A negative field width is taken as a '-' flag
1602
followed by a positive field width." */
1604
width = (unsigned int) (-arg);
1611
const FCHAR_T *digitp = dp->width_start;
1614
width = xsum (xtimes (width, 10), *digitp++ - '0');
1615
while (digitp != dp->width_end);
1622
if (dp->precision_start != dp->precision_end)
1624
if (dp->precision_arg_index != ARG_NONE)
1628
if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
1630
arg = a.arg[dp->precision_arg_index].a.a_int;
1631
/* "A negative precision is taken as if the precision
1641
const FCHAR_T *digitp = dp->precision_start + 1;
1644
while (digitp != dp->precision_end)
1645
precision = xsum (xtimes (precision, 10), *digitp++ - '0');
1652
case TYPE_U8_STRING:
1654
const uint8_t *arg = a.arg[dp->arg_index].a.a_u8_string;
1655
const uint8_t *arg_end;
1660
/* Use only PRECISION characters, from the left. */
1663
for (; precision > 0; precision--)
1665
int count = u8_strmblen (arg_end);
1670
if (!(result == resultbuf || result == NULL))
1672
if (buf_malloced != NULL)
1673
free (buf_malloced);
1684
/* Use the entire string, and count the number of
1690
int count = u8_strmblen (arg_end);
1695
if (!(result == resultbuf || result == NULL))
1697
if (buf_malloced != NULL)
1698
free (buf_malloced);
1709
/* Use the entire string. */
1710
arg_end = arg + u8_strlen (arg);
1711
/* The number of characters doesn't matter. */
1715
if (has_width && width > characters
1716
&& !(dp->flags & FLAG_LEFT))
1718
size_t n = width - characters;
1719
ENSURE_ALLOCATION (xsum (length, n));
1720
DCHAR_SET (result + length, ' ', n);
1724
# if DCHAR_IS_UINT8_T
1726
size_t n = arg_end - arg;
1727
ENSURE_ALLOCATION (xsum (length, n));
1728
DCHAR_CPY (result + length, arg, n);
1733
DCHAR_T *converted = result + length;
1734
size_t converted_len = allocated - length;
1736
/* Convert from UTF-8 to locale encoding. */
1737
if (u8_conv_to_encoding (locale_charset (),
1738
iconveh_question_mark,
1739
arg, arg_end - arg, NULL,
1740
&converted, &converted_len)
1743
/* Convert from UTF-8 to UTF-16/UTF-32. */
1745
U8_TO_DCHAR (arg, arg_end - arg,
1746
converted, &converted_len);
1747
if (converted == NULL)
1750
int saved_errno = errno;
1751
if (!(result == resultbuf || result == NULL))
1753
if (buf_malloced != NULL)
1754
free (buf_malloced);
1756
errno = saved_errno;
1759
if (converted != result + length)
1761
ENSURE_ALLOCATION (xsum (length, converted_len));
1762
DCHAR_CPY (result + length, converted, converted_len);
1765
length += converted_len;
1769
if (has_width && width > characters
1770
&& (dp->flags & FLAG_LEFT))
1772
size_t n = width - characters;
1773
ENSURE_ALLOCATION (xsum (length, n));
1774
DCHAR_SET (result + length, ' ', n);
1780
case TYPE_U16_STRING:
1782
const uint16_t *arg = a.arg[dp->arg_index].a.a_u16_string;
1783
const uint16_t *arg_end;
1788
/* Use only PRECISION characters, from the left. */
1791
for (; precision > 0; precision--)
1793
int count = u16_strmblen (arg_end);
1798
if (!(result == resultbuf || result == NULL))
1800
if (buf_malloced != NULL)
1801
free (buf_malloced);
1812
/* Use the entire string, and count the number of
1818
int count = u16_strmblen (arg_end);
1823
if (!(result == resultbuf || result == NULL))
1825
if (buf_malloced != NULL)
1826
free (buf_malloced);
1837
/* Use the entire string. */
1838
arg_end = arg + u16_strlen (arg);
1839
/* The number of characters doesn't matter. */
1843
if (has_width && width > characters
1844
&& !(dp->flags & FLAG_LEFT))
1846
size_t n = width - characters;
1847
ENSURE_ALLOCATION (xsum (length, n));
1848
DCHAR_SET (result + length, ' ', n);
1852
# if DCHAR_IS_UINT16_T
1854
size_t n = arg_end - arg;
1855
ENSURE_ALLOCATION (xsum (length, n));
1856
DCHAR_CPY (result + length, arg, n);
1861
DCHAR_T *converted = result + length;
1862
size_t converted_len = allocated - length;
1864
/* Convert from UTF-16 to locale encoding. */
1865
if (u16_conv_to_encoding (locale_charset (),
1866
iconveh_question_mark,
1867
arg, arg_end - arg, NULL,
1868
&converted, &converted_len)
1871
/* Convert from UTF-16 to UTF-8/UTF-32. */
1873
U16_TO_DCHAR (arg, arg_end - arg,
1874
converted, &converted_len);
1875
if (converted == NULL)
1878
int saved_errno = errno;
1879
if (!(result == resultbuf || result == NULL))
1881
if (buf_malloced != NULL)
1882
free (buf_malloced);
1884
errno = saved_errno;
1887
if (converted != result + length)
1889
ENSURE_ALLOCATION (xsum (length, converted_len));
1890
DCHAR_CPY (result + length, converted, converted_len);
1893
length += converted_len;
1897
if (has_width && width > characters
1898
&& (dp->flags & FLAG_LEFT))
1900
size_t n = width - characters;
1901
ENSURE_ALLOCATION (xsum (length, n));
1902
DCHAR_SET (result + length, ' ', n);
1908
case TYPE_U32_STRING:
1910
const uint32_t *arg = a.arg[dp->arg_index].a.a_u32_string;
1911
const uint32_t *arg_end;
1916
/* Use only PRECISION characters, from the left. */
1919
for (; precision > 0; precision--)
1921
int count = u32_strmblen (arg_end);
1926
if (!(result == resultbuf || result == NULL))
1928
if (buf_malloced != NULL)
1929
free (buf_malloced);
1940
/* Use the entire string, and count the number of
1946
int count = u32_strmblen (arg_end);
1951
if (!(result == resultbuf || result == NULL))
1953
if (buf_malloced != NULL)
1954
free (buf_malloced);
1965
/* Use the entire string. */
1966
arg_end = arg + u32_strlen (arg);
1967
/* The number of characters doesn't matter. */
1971
if (has_width && width > characters
1972
&& !(dp->flags & FLAG_LEFT))
1974
size_t n = width - characters;
1975
ENSURE_ALLOCATION (xsum (length, n));
1976
DCHAR_SET (result + length, ' ', n);
1980
# if DCHAR_IS_UINT32_T
1982
size_t n = arg_end - arg;
1983
ENSURE_ALLOCATION (xsum (length, n));
1984
DCHAR_CPY (result + length, arg, n);
1989
DCHAR_T *converted = result + length;
1990
size_t converted_len = allocated - length;
1992
/* Convert from UTF-32 to locale encoding. */
1993
if (u32_conv_to_encoding (locale_charset (),
1994
iconveh_question_mark,
1995
arg, arg_end - arg, NULL,
1996
&converted, &converted_len)
1999
/* Convert from UTF-32 to UTF-8/UTF-16. */
2001
U32_TO_DCHAR (arg, arg_end - arg,
2002
converted, &converted_len);
2003
if (converted == NULL)
2006
int saved_errno = errno;
2007
if (!(result == resultbuf || result == NULL))
2009
if (buf_malloced != NULL)
2010
free (buf_malloced);
2012
errno = saved_errno;
2015
if (converted != result + length)
2017
ENSURE_ALLOCATION (xsum (length, converted_len));
2018
DCHAR_CPY (result + length, converted, converted_len);
2021
length += converted_len;
2025
if (has_width && width > characters
2026
&& (dp->flags & FLAG_LEFT))
2028
size_t n = width - characters;
2029
ENSURE_ALLOCATION (xsum (length, n));
2030
DCHAR_SET (result + length, ' ', n);
2041
#if (NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_DOUBLE) && !defined IN_LIBINTL
2042
else if ((dp->conversion == 'a' || dp->conversion == 'A')
2043
# if !(NEED_PRINTF_DIRECTIVE_A || (NEED_PRINTF_LONG_DOUBLE && NEED_PRINTF_DOUBLE))
2045
# if NEED_PRINTF_DOUBLE
2046
|| a.arg[dp->arg_index].type == TYPE_DOUBLE
2048
# if NEED_PRINTF_LONG_DOUBLE
2049
|| a.arg[dp->arg_index].type == TYPE_LONGDOUBLE
2055
arg_type type = a.arg[dp->arg_index].type;
2056
int flags = dp->flags;
2062
DCHAR_T tmpbuf[700];
2069
if (dp->width_start != dp->width_end)
2071
if (dp->width_arg_index != ARG_NONE)
2075
if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
2077
arg = a.arg[dp->width_arg_index].a.a_int;
2080
/* "A negative field width is taken as a '-' flag
2081
followed by a positive field width." */
2083
width = (unsigned int) (-arg);
2090
const FCHAR_T *digitp = dp->width_start;
2093
width = xsum (xtimes (width, 10), *digitp++ - '0');
2094
while (digitp != dp->width_end);
2101
if (dp->precision_start != dp->precision_end)
2103
if (dp->precision_arg_index != ARG_NONE)
2107
if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
2109
arg = a.arg[dp->precision_arg_index].a.a_int;
2110
/* "A negative precision is taken as if the precision
2120
const FCHAR_T *digitp = dp->precision_start + 1;
2123
while (digitp != dp->precision_end)
2124
precision = xsum (xtimes (precision, 10), *digitp++ - '0');
2129
/* Allocate a temporary buffer of sufficient size. */
2130
if (type == TYPE_LONGDOUBLE)
2132
(unsigned int) ((LDBL_DIG + 1)
2133
* 0.831 /* decimal -> hexadecimal */
2135
+ 1; /* turn floor into ceil */
2138
(unsigned int) ((DBL_DIG + 1)
2139
* 0.831 /* decimal -> hexadecimal */
2141
+ 1; /* turn floor into ceil */
2142
if (tmp_length < precision)
2143
tmp_length = precision;
2144
/* Account for sign, decimal point etc. */
2145
tmp_length = xsum (tmp_length, 12);
2147
if (tmp_length < width)
2150
tmp_length = xsum (tmp_length, 1); /* account for trailing NUL */
2152
if (tmp_length <= sizeof (tmpbuf) / sizeof (DCHAR_T))
2156
size_t tmp_memsize = xtimes (tmp_length, sizeof (DCHAR_T));
2158
if (size_overflow_p (tmp_memsize))
2159
/* Overflow, would lead to out of memory. */
2161
tmp = (DCHAR_T *) malloc (tmp_memsize);
2163
/* Out of memory. */
2169
if (type == TYPE_LONGDOUBLE)
2171
# if NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_LONG_DOUBLE
2172
long double arg = a.arg[dp->arg_index].a.a_longdouble;
2176
if (dp->conversion == 'A')
2178
*p++ = 'N'; *p++ = 'A'; *p++ = 'N';
2182
*p++ = 'n'; *p++ = 'a'; *p++ = 'n';
2188
DECL_LONG_DOUBLE_ROUNDING
2190
BEGIN_LONG_DOUBLE_ROUNDING ();
2192
if (signbit (arg)) /* arg < 0.0L or negative zero */
2200
else if (flags & FLAG_SHOWSIGN)
2202
else if (flags & FLAG_SPACE)
2205
if (arg > 0.0L && arg + arg == arg)
2207
if (dp->conversion == 'A')
2209
*p++ = 'I'; *p++ = 'N'; *p++ = 'F';
2213
*p++ = 'i'; *p++ = 'n'; *p++ = 'f';
2219
long double mantissa;
2222
mantissa = printf_frexpl (arg, &exponent);
2230
&& precision < (unsigned int) ((LDBL_DIG + 1) * 0.831) + 1)
2232
/* Round the mantissa. */
2233
long double tail = mantissa;
2236
for (q = precision; ; q--)
2238
int digit = (int) tail;
2242
if (digit & 1 ? tail >= 0.5L : tail > 0.5L)
2251
for (q = precision; q > 0; q--)
2257
*p++ = dp->conversion - 'A' + 'X';
2262
digit = (int) mantissa;
2265
if ((flags & FLAG_ALT)
2266
|| mantissa > 0.0L || precision > 0)
2268
*p++ = decimal_point_char ();
2269
/* This loop terminates because we assume
2270
that FLT_RADIX is a power of 2. */
2271
while (mantissa > 0.0L)
2274
digit = (int) mantissa;
2279
: dp->conversion - 10);
2283
while (precision > 0)
2290
*p++ = dp->conversion - 'A' + 'P';
2291
# if WIDE_CHAR_VERSION
2293
static const wchar_t decimal_format[] =
2294
{ '%', '+', 'd', '\0' };
2295
SNPRINTF (p, 6 + 1, decimal_format, exponent);
2300
if (sizeof (DCHAR_T) == 1)
2302
sprintf ((char *) p, "%+d", exponent);
2310
sprintf (expbuf, "%+d", exponent);
2311
for (ep = expbuf; (*p = *ep) != '\0'; ep++)
2317
END_LONG_DOUBLE_ROUNDING ();
2325
# if NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_DOUBLE
2326
double arg = a.arg[dp->arg_index].a.a_double;
2330
if (dp->conversion == 'A')
2332
*p++ = 'N'; *p++ = 'A'; *p++ = 'N';
2336
*p++ = 'n'; *p++ = 'a'; *p++ = 'n';
2343
if (signbit (arg)) /* arg < 0.0 or negative zero */
2351
else if (flags & FLAG_SHOWSIGN)
2353
else if (flags & FLAG_SPACE)
2356
if (arg > 0.0 && arg + arg == arg)
2358
if (dp->conversion == 'A')
2360
*p++ = 'I'; *p++ = 'N'; *p++ = 'F';
2364
*p++ = 'i'; *p++ = 'n'; *p++ = 'f';
2373
mantissa = printf_frexp (arg, &exponent);
2381
&& precision < (unsigned int) ((DBL_DIG + 1) * 0.831) + 1)
2383
/* Round the mantissa. */
2384
double tail = mantissa;
2387
for (q = precision; ; q--)
2389
int digit = (int) tail;
2393
if (digit & 1 ? tail >= 0.5 : tail > 0.5)
2402
for (q = precision; q > 0; q--)
2408
*p++ = dp->conversion - 'A' + 'X';
2413
digit = (int) mantissa;
2416
if ((flags & FLAG_ALT)
2417
|| mantissa > 0.0 || precision > 0)
2419
*p++ = decimal_point_char ();
2420
/* This loop terminates because we assume
2421
that FLT_RADIX is a power of 2. */
2422
while (mantissa > 0.0)
2425
digit = (int) mantissa;
2430
: dp->conversion - 10);
2434
while (precision > 0)
2441
*p++ = dp->conversion - 'A' + 'P';
2442
# if WIDE_CHAR_VERSION
2444
static const wchar_t decimal_format[] =
2445
{ '%', '+', 'd', '\0' };
2446
SNPRINTF (p, 6 + 1, decimal_format, exponent);
2451
if (sizeof (DCHAR_T) == 1)
2453
sprintf ((char *) p, "%+d", exponent);
2461
sprintf (expbuf, "%+d", exponent);
2462
for (ep = expbuf; (*p = *ep) != '\0'; ep++)
2472
/* The generated string now extends from tmp to p, with the
2473
zero padding insertion point being at pad_ptr. */
2474
if (has_width && p - tmp < width)
2476
size_t pad = width - (p - tmp);
2477
DCHAR_T *end = p + pad;
2479
if (flags & FLAG_LEFT)
2481
/* Pad with spaces on the right. */
2482
for (; pad > 0; pad--)
2485
else if ((flags & FLAG_ZERO) && pad_ptr != NULL)
2487
/* Pad with zeroes. */
2492
for (; pad > 0; pad--)
2497
/* Pad with spaces on the left. */
2502
for (; pad > 0; pad--)
2510
size_t count = p - tmp;
2512
if (count >= tmp_length)
2513
/* tmp_length was incorrectly calculated - fix the
2517
/* Make room for the result. */
2518
if (count >= allocated - length)
2520
size_t n = xsum (length, count);
2522
ENSURE_ALLOCATION (n);
2525
/* Append the result. */
2526
memcpy (result + length, tmp, count * sizeof (DCHAR_T));
2533
#if (NEED_PRINTF_INFINITE_DOUBLE || NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE || NEED_PRINTF_LONG_DOUBLE) && !defined IN_LIBINTL
2534
else if ((dp->conversion == 'f' || dp->conversion == 'F'
2535
|| dp->conversion == 'e' || dp->conversion == 'E'
2536
|| dp->conversion == 'g' || dp->conversion == 'G'
2537
|| dp->conversion == 'a' || dp->conversion == 'A')
2539
# if NEED_PRINTF_DOUBLE
2540
|| a.arg[dp->arg_index].type == TYPE_DOUBLE
2541
# elif NEED_PRINTF_INFINITE_DOUBLE
2542
|| (a.arg[dp->arg_index].type == TYPE_DOUBLE
2543
/* The systems (mingw) which produce wrong output
2544
for Inf, -Inf, and NaN also do so for -0.0.
2545
Therefore we treat this case here as well. */
2546
&& is_infinite_or_zero (a.arg[dp->arg_index].a.a_double))
2548
# if NEED_PRINTF_LONG_DOUBLE
2549
|| a.arg[dp->arg_index].type == TYPE_LONGDOUBLE
2550
# elif NEED_PRINTF_INFINITE_LONG_DOUBLE
2551
|| (a.arg[dp->arg_index].type == TYPE_LONGDOUBLE
2552
/* Some systems produce wrong output for Inf,
2554
&& is_infinitel (a.arg[dp->arg_index].a.a_longdouble))
2558
# if (NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE) && (NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE)
2559
arg_type type = a.arg[dp->arg_index].type;
2561
int flags = dp->flags;
2567
DCHAR_T tmpbuf[700];
2574
if (dp->width_start != dp->width_end)
2576
if (dp->width_arg_index != ARG_NONE)
2580
if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
2582
arg = a.arg[dp->width_arg_index].a.a_int;
2585
/* "A negative field width is taken as a '-' flag
2586
followed by a positive field width." */
2588
width = (unsigned int) (-arg);
2595
const FCHAR_T *digitp = dp->width_start;
2598
width = xsum (xtimes (width, 10), *digitp++ - '0');
2599
while (digitp != dp->width_end);
2606
if (dp->precision_start != dp->precision_end)
2608
if (dp->precision_arg_index != ARG_NONE)
2612
if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
2614
arg = a.arg[dp->precision_arg_index].a.a_int;
2615
/* "A negative precision is taken as if the precision
2625
const FCHAR_T *digitp = dp->precision_start + 1;
2628
while (digitp != dp->precision_end)
2629
precision = xsum (xtimes (precision, 10), *digitp++ - '0');
2634
/* POSIX specifies the default precision to be 6 for %f, %F,
2635
%e, %E, but not for %g, %G. Implementations appear to use
2636
the same default precision also for %g, %G. */
2640
/* Allocate a temporary buffer of sufficient size. */
2641
# if NEED_PRINTF_DOUBLE && NEED_PRINTF_LONG_DOUBLE
2642
tmp_length = (type == TYPE_LONGDOUBLE ? LDBL_DIG + 1 : DBL_DIG + 1);
2643
# elif NEED_PRINTF_INFINITE_DOUBLE && NEED_PRINTF_LONG_DOUBLE
2644
tmp_length = (type == TYPE_LONGDOUBLE ? LDBL_DIG + 1 : 0);
2645
# elif NEED_PRINTF_LONG_DOUBLE
2646
tmp_length = LDBL_DIG + 1;
2647
# elif NEED_PRINTF_DOUBLE
2648
tmp_length = DBL_DIG + 1;
2652
if (tmp_length < precision)
2653
tmp_length = precision;
2654
# if NEED_PRINTF_LONG_DOUBLE
2655
# if NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE
2656
if (type == TYPE_LONGDOUBLE)
2658
if (dp->conversion == 'f' || dp->conversion == 'F')
2660
long double arg = a.arg[dp->arg_index].a.a_longdouble;
2661
if (!(isnanl (arg) || arg + arg == arg))
2663
/* arg is finite and nonzero. */
2664
int exponent = floorlog10l (arg < 0 ? -arg : arg);
2665
if (exponent >= 0 && tmp_length < exponent + precision)
2666
tmp_length = exponent + precision;
2670
# if NEED_PRINTF_DOUBLE
2671
# if NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE
2672
if (type == TYPE_DOUBLE)
2674
if (dp->conversion == 'f' || dp->conversion == 'F')
2676
double arg = a.arg[dp->arg_index].a.a_double;
2677
if (!(isnan (arg) || arg + arg == arg))
2679
/* arg is finite and nonzero. */
2680
int exponent = floorlog10 (arg < 0 ? -arg : arg);
2681
if (exponent >= 0 && tmp_length < exponent + precision)
2682
tmp_length = exponent + precision;
2686
/* Account for sign, decimal point etc. */
2687
tmp_length = xsum (tmp_length, 12);
2689
if (tmp_length < width)
2692
tmp_length = xsum (tmp_length, 1); /* account for trailing NUL */
2694
if (tmp_length <= sizeof (tmpbuf) / sizeof (DCHAR_T))
2698
size_t tmp_memsize = xtimes (tmp_length, sizeof (DCHAR_T));
2700
if (size_overflow_p (tmp_memsize))
2701
/* Overflow, would lead to out of memory. */
2703
tmp = (DCHAR_T *) malloc (tmp_memsize);
2705
/* Out of memory. */
2712
# if NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE
2713
# if NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE
2714
if (type == TYPE_LONGDOUBLE)
2717
long double arg = a.arg[dp->arg_index].a.a_longdouble;
2721
if (dp->conversion >= 'A' && dp->conversion <= 'Z')
2723
*p++ = 'N'; *p++ = 'A'; *p++ = 'N';
2727
*p++ = 'n'; *p++ = 'a'; *p++ = 'n';
2733
DECL_LONG_DOUBLE_ROUNDING
2735
BEGIN_LONG_DOUBLE_ROUNDING ();
2737
if (signbit (arg)) /* arg < 0.0L or negative zero */
2745
else if (flags & FLAG_SHOWSIGN)
2747
else if (flags & FLAG_SPACE)
2750
if (arg > 0.0L && arg + arg == arg)
2752
if (dp->conversion >= 'A' && dp->conversion <= 'Z')
2754
*p++ = 'I'; *p++ = 'N'; *p++ = 'F';
2758
*p++ = 'i'; *p++ = 'n'; *p++ = 'f';
2763
# if NEED_PRINTF_LONG_DOUBLE
2766
if (dp->conversion == 'f' || dp->conversion == 'F')
2772
scale10_round_decimal_long_double (arg, precision);
2775
END_LONG_DOUBLE_ROUNDING ();
2778
ndigits = strlen (digits);
2780
if (ndigits > precision)
2784
*p++ = digits[ndigits];
2786
while (ndigits > precision);
2789
/* Here ndigits <= precision. */
2790
if ((flags & FLAG_ALT) || precision > 0)
2792
*p++ = decimal_point_char ();
2793
for (; precision > ndigits; precision--)
2798
*p++ = digits[ndigits];
2804
else if (dp->conversion == 'e' || dp->conversion == 'E')
2812
if ((flags & FLAG_ALT) || precision > 0)
2814
*p++ = decimal_point_char ();
2815
for (; precision > 0; precision--)
2826
exponent = floorlog10l (arg);
2831
scale10_round_decimal_long_double (arg,
2832
(int)precision - exponent);
2835
END_LONG_DOUBLE_ROUNDING ();
2838
ndigits = strlen (digits);
2840
if (ndigits == precision + 1)
2842
if (ndigits < precision
2843
|| ndigits > precision + 2)
2844
/* The exponent was not guessed
2845
precisely enough. */
2848
/* None of two values of exponent is
2849
the right one. Prevent an endless
2853
if (ndigits == precision)
2860
/* Here ndigits = precision+1. */
2861
*p++ = digits[--ndigits];
2862
if ((flags & FLAG_ALT) || precision > 0)
2864
*p++ = decimal_point_char ();
2868
*p++ = digits[ndigits];
2875
*p++ = dp->conversion; /* 'e' or 'E' */
2876
# if WIDE_CHAR_VERSION
2878
static const wchar_t decimal_format[] =
2879
{ '%', '+', '.', '2', 'd', '\0' };
2880
SNPRINTF (p, 6 + 1, decimal_format, exponent);
2885
if (sizeof (DCHAR_T) == 1)
2887
sprintf ((char *) p, "%+.2d", exponent);
2895
sprintf (expbuf, "%+.2d", exponent);
2896
for (ep = expbuf; (*p = *ep) != '\0'; ep++)
2901
else if (dp->conversion == 'g' || dp->conversion == 'G')
2905
/* precision >= 1. */
2908
/* The exponent is 0, >= -4, < precision.
2909
Use fixed-point notation. */
2911
size_t ndigits = precision;
2912
/* Number of trailing zeroes that have to be
2915
(flags & FLAG_ALT ? 0 : precision - 1);
2919
if ((flags & FLAG_ALT) || ndigits > nzeroes)
2921
*p++ = decimal_point_char ();
2922
while (ndigits > nzeroes)
2938
exponent = floorlog10l (arg);
2943
scale10_round_decimal_long_double (arg,
2944
(int)(precision - 1) - exponent);
2947
END_LONG_DOUBLE_ROUNDING ();
2950
ndigits = strlen (digits);
2952
if (ndigits == precision)
2954
if (ndigits < precision - 1
2955
|| ndigits > precision + 1)
2956
/* The exponent was not guessed
2957
precisely enough. */
2960
/* None of two values of exponent is
2961
the right one. Prevent an endless
2965
if (ndigits < precision)
2971
/* Here ndigits = precision. */
2973
/* Determine the number of trailing zeroes
2974
that have to be dropped. */
2976
if ((flags & FLAG_ALT) == 0)
2977
while (nzeroes < ndigits
2978
&& digits[nzeroes] == '0')
2981
/* The exponent is now determined. */
2983
&& exponent < (long)precision)
2985
/* Fixed-point notation:
2986
max(exponent,0)+1 digits, then the
2987
decimal point, then the remaining
2988
digits without trailing zeroes. */
2991
size_t count = exponent + 1;
2992
/* Note: count <= precision = ndigits. */
2993
for (; count > 0; count--)
2994
*p++ = digits[--ndigits];
2995
if ((flags & FLAG_ALT) || ndigits > nzeroes)
2997
*p++ = decimal_point_char ();
2998
while (ndigits > nzeroes)
3001
*p++ = digits[ndigits];
3007
size_t count = -exponent - 1;
3009
*p++ = decimal_point_char ();
3010
for (; count > 0; count--)
3012
while (ndigits > nzeroes)
3015
*p++ = digits[ndigits];
3021
/* Exponential notation. */
3022
*p++ = digits[--ndigits];
3023
if ((flags & FLAG_ALT) || ndigits > nzeroes)
3025
*p++ = decimal_point_char ();
3026
while (ndigits > nzeroes)
3029
*p++ = digits[ndigits];
3032
*p++ = dp->conversion - 'G' + 'E'; /* 'e' or 'E' */
3033
# if WIDE_CHAR_VERSION
3035
static const wchar_t decimal_format[] =
3036
{ '%', '+', '.', '2', 'd', '\0' };
3037
SNPRINTF (p, 6 + 1, decimal_format, exponent);
3042
if (sizeof (DCHAR_T) == 1)
3044
sprintf ((char *) p, "%+.2d", exponent);
3052
sprintf (expbuf, "%+.2d", exponent);
3053
for (ep = expbuf; (*p = *ep) != '\0'; ep++)
3065
/* arg is finite. */
3070
END_LONG_DOUBLE_ROUNDING ();
3073
# if NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE
3077
# if NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE
3079
double arg = a.arg[dp->arg_index].a.a_double;
3083
if (dp->conversion >= 'A' && dp->conversion <= 'Z')
3085
*p++ = 'N'; *p++ = 'A'; *p++ = 'N';
3089
*p++ = 'n'; *p++ = 'a'; *p++ = 'n';
3096
if (signbit (arg)) /* arg < 0.0 or negative zero */
3104
else if (flags & FLAG_SHOWSIGN)
3106
else if (flags & FLAG_SPACE)
3109
if (arg > 0.0 && arg + arg == arg)
3111
if (dp->conversion >= 'A' && dp->conversion <= 'Z')
3113
*p++ = 'I'; *p++ = 'N'; *p++ = 'F';
3117
*p++ = 'i'; *p++ = 'n'; *p++ = 'f';
3122
# if NEED_PRINTF_DOUBLE
3125
if (dp->conversion == 'f' || dp->conversion == 'F')
3131
scale10_round_decimal_double (arg, precision);
3134
ndigits = strlen (digits);
3136
if (ndigits > precision)
3140
*p++ = digits[ndigits];
3142
while (ndigits > precision);
3145
/* Here ndigits <= precision. */
3146
if ((flags & FLAG_ALT) || precision > 0)
3148
*p++ = decimal_point_char ();
3149
for (; precision > ndigits; precision--)
3154
*p++ = digits[ndigits];
3160
else if (dp->conversion == 'e' || dp->conversion == 'E')
3168
if ((flags & FLAG_ALT) || precision > 0)
3170
*p++ = decimal_point_char ();
3171
for (; precision > 0; precision--)
3182
exponent = floorlog10 (arg);
3187
scale10_round_decimal_double (arg,
3188
(int)precision - exponent);
3191
ndigits = strlen (digits);
3193
if (ndigits == precision + 1)
3195
if (ndigits < precision
3196
|| ndigits > precision + 2)
3197
/* The exponent was not guessed
3198
precisely enough. */
3201
/* None of two values of exponent is
3202
the right one. Prevent an endless
3206
if (ndigits == precision)
3213
/* Here ndigits = precision+1. */
3214
*p++ = digits[--ndigits];
3215
if ((flags & FLAG_ALT) || precision > 0)
3217
*p++ = decimal_point_char ();
3221
*p++ = digits[ndigits];
3228
*p++ = dp->conversion; /* 'e' or 'E' */
3229
# if WIDE_CHAR_VERSION
3231
static const wchar_t decimal_format[] =
3232
/* Produce the same number of exponent digits
3233
as the native printf implementation. */
3234
# if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
3235
{ '%', '+', '.', '3', 'd', '\0' };
3237
{ '%', '+', '.', '2', 'd', '\0' };
3239
SNPRINTF (p, 6 + 1, decimal_format, exponent);
3245
static const char decimal_format[] =
3246
/* Produce the same number of exponent digits
3247
as the native printf implementation. */
3248
# if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
3253
if (sizeof (DCHAR_T) == 1)
3255
sprintf ((char *) p, decimal_format, exponent);
3263
sprintf (expbuf, decimal_format, exponent);
3264
for (ep = expbuf; (*p = *ep) != '\0'; ep++)
3270
else if (dp->conversion == 'g' || dp->conversion == 'G')
3274
/* precision >= 1. */
3277
/* The exponent is 0, >= -4, < precision.
3278
Use fixed-point notation. */
3280
size_t ndigits = precision;
3281
/* Number of trailing zeroes that have to be
3284
(flags & FLAG_ALT ? 0 : precision - 1);
3288
if ((flags & FLAG_ALT) || ndigits > nzeroes)
3290
*p++ = decimal_point_char ();
3291
while (ndigits > nzeroes)
3307
exponent = floorlog10 (arg);
3312
scale10_round_decimal_double (arg,
3313
(int)(precision - 1) - exponent);
3316
ndigits = strlen (digits);
3318
if (ndigits == precision)
3320
if (ndigits < precision - 1
3321
|| ndigits > precision + 1)
3322
/* The exponent was not guessed
3323
precisely enough. */
3326
/* None of two values of exponent is
3327
the right one. Prevent an endless
3331
if (ndigits < precision)
3337
/* Here ndigits = precision. */
3339
/* Determine the number of trailing zeroes
3340
that have to be dropped. */
3342
if ((flags & FLAG_ALT) == 0)
3343
while (nzeroes < ndigits
3344
&& digits[nzeroes] == '0')
3347
/* The exponent is now determined. */
3349
&& exponent < (long)precision)
3351
/* Fixed-point notation:
3352
max(exponent,0)+1 digits, then the
3353
decimal point, then the remaining
3354
digits without trailing zeroes. */
3357
size_t count = exponent + 1;
3358
/* Note: count <= precision = ndigits. */
3359
for (; count > 0; count--)
3360
*p++ = digits[--ndigits];
3361
if ((flags & FLAG_ALT) || ndigits > nzeroes)
3363
*p++ = decimal_point_char ();
3364
while (ndigits > nzeroes)
3367
*p++ = digits[ndigits];
3373
size_t count = -exponent - 1;
3375
*p++ = decimal_point_char ();
3376
for (; count > 0; count--)
3378
while (ndigits > nzeroes)
3381
*p++ = digits[ndigits];
3387
/* Exponential notation. */
3388
*p++ = digits[--ndigits];
3389
if ((flags & FLAG_ALT) || ndigits > nzeroes)
3391
*p++ = decimal_point_char ();
3392
while (ndigits > nzeroes)
3395
*p++ = digits[ndigits];
3398
*p++ = dp->conversion - 'G' + 'E'; /* 'e' or 'E' */
3399
# if WIDE_CHAR_VERSION
3401
static const wchar_t decimal_format[] =
3402
/* Produce the same number of exponent digits
3403
as the native printf implementation. */
3404
# if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
3405
{ '%', '+', '.', '3', 'd', '\0' };
3407
{ '%', '+', '.', '2', 'd', '\0' };
3409
SNPRINTF (p, 6 + 1, decimal_format, exponent);
3415
static const char decimal_format[] =
3416
/* Produce the same number of exponent digits
3417
as the native printf implementation. */
3418
# if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
3423
if (sizeof (DCHAR_T) == 1)
3425
sprintf ((char *) p, decimal_format, exponent);
3433
sprintf (expbuf, decimal_format, exponent);
3434
for (ep = expbuf; (*p = *ep) != '\0'; ep++)
3447
/* arg is finite. */
3453
if (dp->conversion == 'f' || dp->conversion == 'F')
3456
if ((flags & FLAG_ALT) || precision > 0)
3458
*p++ = decimal_point_char ();
3459
for (; precision > 0; precision--)
3463
else if (dp->conversion == 'e' || dp->conversion == 'E')
3466
if ((flags & FLAG_ALT) || precision > 0)
3468
*p++ = decimal_point_char ();
3469
for (; precision > 0; precision--)
3472
*p++ = dp->conversion; /* 'e' or 'E' */
3474
/* Produce the same number of exponent digits as
3475
the native printf implementation. */
3476
# if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
3482
else if (dp->conversion == 'g' || dp->conversion == 'G')
3485
if (flags & FLAG_ALT)
3488
(precision > 0 ? precision - 1 : 0);
3489
*p++ = decimal_point_char ();
3490
for (; ndigits > 0; --ndigits)
3502
/* The generated string now extends from tmp to p, with the
3503
zero padding insertion point being at pad_ptr. */
3504
if (has_width && p - tmp < width)
3506
size_t pad = width - (p - tmp);
3507
DCHAR_T *end = p + pad;
3509
if (flags & FLAG_LEFT)
3511
/* Pad with spaces on the right. */
3512
for (; pad > 0; pad--)
3515
else if ((flags & FLAG_ZERO) && pad_ptr != NULL)
3517
/* Pad with zeroes. */
3522
for (; pad > 0; pad--)
3527
/* Pad with spaces on the left. */
3532
for (; pad > 0; pad--)
3540
size_t count = p - tmp;
3542
if (count >= tmp_length)
3543
/* tmp_length was incorrectly calculated - fix the
3547
/* Make room for the result. */
3548
if (count >= allocated - length)
3550
size_t n = xsum (length, count);
3552
ENSURE_ALLOCATION (n);
3555
/* Append the result. */
3556
memcpy (result + length, tmp, count * sizeof (DCHAR_T));
270
3565
arg_type type = a.arg[dp->arg_index].type;
3566
int flags = dp->flags;
3567
#if !USE_SNPRINTF || !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION
3571
#if !USE_SNPRINTF || NEED_PRINTF_UNBOUNDED_PRECISION
3575
#if NEED_PRINTF_UNBOUNDED_PRECISION
3578
# define prec_ourselves 0
3580
#if !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION
3583
# define pad_ourselves 0
272
3586
unsigned int prefix_count;
273
3587
int prefixes[2];
274
3588
#if !USE_SNPRINTF
275
3589
size_t tmp_length;
3590
TCHAR_T tmpbuf[700];
3594
#if !USE_SNPRINTF || !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION
3597
if (dp->width_start != dp->width_end)
3599
if (dp->width_arg_index != ARG_NONE)
3603
if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
3605
arg = a.arg[dp->width_arg_index].a.a_int;
3608
/* "A negative field width is taken as a '-' flag
3609
followed by a positive field width." */
3611
width = (unsigned int) (-arg);
3618
const FCHAR_T *digitp = dp->width_start;
3621
width = xsum (xtimes (width, 10), *digitp++ - '0');
3622
while (digitp != dp->width_end);
3628
#if !USE_SNPRINTF || NEED_PRINTF_UNBOUNDED_PRECISION
3631
if (dp->precision_start != dp->precision_end)
3633
if (dp->precision_arg_index != ARG_NONE)
3637
if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
3639
arg = a.arg[dp->precision_arg_index].a.a_int;
3640
/* "A negative precision is taken as if the precision
3650
const FCHAR_T *digitp = dp->precision_start + 1;
3653
while (digitp != dp->precision_end)
3654
precision = xsum (xtimes (precision, 10), *digitp++ - '0');
279
3661
/* Allocate a temporary buffer of sufficient size for calling
286
if (dp->width_start != dp->width_end)
288
if (dp->width_arg_index != ARG_NONE)
292
if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
294
arg = a.arg[dp->width_arg_index].a.a_int;
295
width = (arg < 0 ? (unsigned int) (-arg) : arg);
299
const CHAR_T *digitp = dp->width_start;
302
width = xsum (xtimes (width, 10), *digitp++ - '0');
303
while (digitp != dp->width_end);
308
if (dp->precision_start != dp->precision_end)
310
if (dp->precision_arg_index != ARG_NONE)
314
if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
316
arg = a.arg[dp->precision_arg_index].a.a_int;
317
precision = (arg < 0 ? 0 : arg);
321
const CHAR_T *digitp = dp->precision_start + 1;
324
while (digitp != dp->precision_end)
325
precision = xsum (xtimes (precision, 10), *digitp++ - '0');
329
3664
switch (dp->conversion)
332
3667
case 'd': case 'i': case 'u':
333
# ifdef HAVE_LONG_LONG
3668
# if HAVE_LONG_LONG_INT
334
3669
if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
336
3671
(unsigned int) (sizeof (unsigned long long) * CHAR_BIT
337
3672
* 0.30103 /* binary -> decimal */
338
* 2 /* estimate for FLAG_GROUP */
340
+ 1 /* turn floor into ceil */
341
+ 1; /* account for leading sign */
3674
+ 1; /* turn floor into ceil */
344
3677
if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
346
3679
(unsigned int) (sizeof (unsigned long) * CHAR_BIT
347
3680
* 0.30103 /* binary -> decimal */
348
* 2 /* estimate for FLAG_GROUP */
350
+ 1 /* turn floor into ceil */
351
+ 1; /* account for leading sign */
3682
+ 1; /* turn floor into ceil */
354
3685
(unsigned int) (sizeof (unsigned int) * CHAR_BIT
355
3686
* 0.30103 /* binary -> decimal */
356
* 2 /* estimate for FLAG_GROUP */
358
+ 1 /* turn floor into ceil */
359
+ 1; /* account for leading sign */
3688
+ 1; /* turn floor into ceil */
3689
if (tmp_length < precision)
3690
tmp_length = precision;
3691
/* Multiply by 2, as an estimate for FLAG_GROUP. */
3692
tmp_length = xsum (tmp_length, tmp_length);
3693
/* Add 1, to account for a leading sign. */
3694
tmp_length = xsum (tmp_length, 1);
363
# ifdef HAVE_LONG_LONG
3698
# if HAVE_LONG_LONG_INT
364
3699
if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
366
3701
(unsigned int) (sizeof (unsigned long long) * CHAR_BIT
367
3702
* 0.333334 /* binary -> octal */
369
+ 1 /* turn floor into ceil */
370
+ 1; /* account for leading sign */
3704
+ 1; /* turn floor into ceil */
373
3707
if (type == TYPE_LONGINT || type == TYPE_ULONGINT)