1 /****************************************************************
3 * The author of this software is David M. Gay.
5 * Copyright (c) 1991, 2006 by AT&T.
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose without fee is hereby granted, provided that this entire notice
9 * is included in all copies of any software which is or includes a copy
10 * or modification of this software and in all copies of the supporting
11 * documentation for such software.
13 * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
14 * WARRANTY. IN PARTICULAR, NEITHER THE AUTHOR NOR AT&T MAKES ANY
15 * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
16 * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
18 ***************************************************************/
20 /* Please send bug reports to
22 AT&T Bell Laboratories, Room 2C-463
24 Murray Hill, NJ 07974-2070
26 dmg@research.att.com or research!dmg
36 _Jv_Bigint * b _AND _Jv_Bigint * S)
40 unsigned long carry, q, ys;
41 unsigned long *bx, *bxe, *sx, *sxe;
49 /*debug*/ if (b->_wds > n)
50 /*debug*/ Bug ("oversize b in quorem");
58 q = *bxe / (*sxe + 1); /* ensure q <= true quotient */
61 /*debug*/ Bug ("oversized quotient in quorem");
71 ys = (si & 0xffff) * q + carry;
72 zs = (si >> 16) * q + (ys >> 16);
74 y = (*bx & 0xffff) - (ys & 0xffff) + borrow;
76 Sign_Extend (borrow, y);
77 z = (*bx >> 16) - (zs & 0xffff) + borrow;
79 Sign_Extend (borrow, z);
82 ys = *sx++ * q + carry;
84 y = *bx - (ys & 0xffff) + borrow;
86 Sign_Extend (borrow, y);
94 while (--bxe > bx && !*bxe)
110 ys = (si & 0xffff) + carry;
111 zs = (si >> 16) + (ys >> 16);
113 y = (*bx & 0xffff) - (ys & 0xffff) + borrow;
115 Sign_Extend (borrow, y);
116 z = (*bx >> 16) - (zs & 0xffff) + borrow;
118 Sign_Extend (borrow, z);
123 y = *bx - (ys & 0xffff) + borrow;
125 Sign_Extend (borrow, y);
134 while (--bxe > bx && !*bxe)
146 print (_Jv_Bigint * b)
156 fprintf (stderr, "%08x", *x);
159 fprintf (stderr, "\n");
163 /* dtoa for IEEE arithmetic (dmg): convert double to ASCII string.
165 * Inspired by "How to Print Floating-Point Numbers Accurately" by
166 * Guy L. Steele, Jr. and Jon L. White [Proc. ACM SIGPLAN '90, pp. 92-101].
169 * 1. Rather than iterating, we use a simple numeric overestimate
170 * to determine k = floor(log10(d)). We scale relevant
171 * quantities using O(log2(k)) rather than O(k) multiplications.
172 * 2. For some modes > 2 (corresponding to ecvt and fcvt), we don't
173 * try to generate digits strictly left to right. Instead, we
174 * compute with fewer bits and propagate the carry if necessary
175 * when rounding the final digit up. This is often faster.
176 * 3. Under the assumption that input will be rounded nearest,
177 * mode 0 renders 1e23 as 1e23 rather than 9.999999999999999e22.
178 * That is, we allow equality in stopping tests when the
179 * round-nearest rule will give the same floating-point value
180 * as would satisfaction of the stopping test with strict
182 * 4. We remove common factors of powers of 2 from relevant
184 * 5. When converting floating-point integers less than 1e16,
185 * we use floating-point arithmetic rather than resorting
186 * to multiple-precision integers.
187 * 6. When asked to produce fewer than 15 digits, we first try
188 * to get by with floating-point arithmetic; we resort to
189 * multiple-precision integer arithmetic only if we cannot
190 * guarantee that the floating-point calculation has given
191 * the correctly rounded result. For k requested digits and
192 * "uniformly" distributed input, the probability is
193 * something like 10^(k-15) that we must resort to the long
200 (ptr, _d, mode, ndigits, decpt, sign, rve, float_type),
201 struct _Jv_reent *ptr _AND
211 float_type == 0 for double precision, 1 for float.
213 Arguments ndigits, decpt, sign are similar to those
214 of ecvt and fcvt; trailing zeros are suppressed from
215 the returned string. If not null, *rve is set to point
216 to the end of the return value. If d is +-Infinity or NaN,
217 then *decpt is set to 9999.
220 0 ==> shortest string that yields d when read in
221 and rounded to nearest.
222 1 ==> like 0, but with Steele & White stopping rule;
223 e.g. with IEEE P754 arithmetic , mode 0 gives
224 1e23 whereas mode 1 gives 9.999999999999999e22.
225 2 ==> max(1,ndigits) significant digits. This gives a
226 return value similar to that of ecvt, except
227 that trailing zeros are suppressed.
228 3 ==> through ndigits past the decimal point. This
229 gives a return value similar to that from fcvt,
230 except that trailing zeros are suppressed, and
231 ndigits can be negative.
232 4-9 should give the same return values as 2-3, i.e.,
233 4 <= mode <= 9 ==> same return as mode
234 2 + (mode & 1). These modes are mainly for
235 debugging; often they run slower but sometimes
236 faster than modes 2-3.
237 4,5,8,9 ==> left-to-right digit generation.
238 6-9 ==> don't try fast floating-point estimate
241 > 16 ==> Floating-point arg is treated as single precision.
243 Values of mode other than 0-9 are treated as mode 0.
245 Sufficient space is allocated to the return value
246 to hold the suppressed trailing zeros.
249 int bbits, b2, b5, be, dig, i, ieps, ilim0, j, j1, k, k0,
250 k_check, leftright, m2, m5, s2, s5, try_quick;
251 int ilim = 0, ilim1 = 0, spec_case = 0;
252 union double_union d, d2, eps;
254 #ifndef Sudden_Underflow
258 _Jv_Bigint *b, *b1, *delta, *mlo = NULL, *mhi, *S;
266 ptr->_result->_k = ptr->_result_k;
267 ptr->_result->_maxwds = 1 << ptr->_result_k;
268 Bfree (ptr, ptr->_result);
272 if (word0 (d) & Sign_bit)
274 /* set sign for everything, including 0's and NaNs */
276 word0 (d) &= ~Sign_bit; /* clear sign bit */
281 #if defined(IEEE_Arith) + defined(VAX)
283 if ((word0 (d) & Exp_mask) == Exp_mask)
285 if (word0 (d) == 0x8000)
288 /* Infinity or NaN */
292 !word1 (d) && !(word0 (d) & 0xfffff) ? "Infinity" :
305 d.d += 0; /* normalize */
316 b = d2b (ptr, d.d, &be, &bbits);
317 #ifdef Sudden_Underflow
318 i = (int) (word0 (d) >> Exp_shift1 & (Exp_mask >> Exp_shift1));
320 if ((i = (int) (word0 (d) >> Exp_shift1 & (Exp_mask >> Exp_shift1))))
324 word0 (d2) &= Frac_mask1;
325 word0 (d2) |= Exp_11;
327 if (j = 11 - hi0bits (word0 (d2) & Frac_mask))
331 /* log(x) ~=~ log(1.5) + (x-1.5)/1.5
332 * log10(x) = log(x) / log(10)
333 * ~=~ log(1.5)/log(10) + (x-1.5)/(1.5*log(10))
334 * log10(d) = (i-Bias)*log(2)/log(10) + log10(d2)
336 * This suggests computing an approximation k to log10(d) by
338 * k = (i - Bias)*0.301029995663981
339 * + ( (d2-1.5)*0.289529654602168 + 0.176091259055681 );
341 * We want k to be too large rather than too small.
342 * The error in the first-order Taylor series approximation
343 * is in our favor, so we just round up the constant enough
344 * to compensate for any error in the multiplication of
345 * (i - Bias) by 0.301029995663981; since |i - Bias| <= 1077,
346 * and 1077 * 0.30103 * 2^-52 ~=~ 7.2e-14,
347 * adding 1e-13 to the constant term more than suffices.
348 * Hence we adjust the constant term to 0.1760912590558.
349 * (We could get a more accurate k by invoking log10,
350 * but this is probably not worthwhile.)
358 #ifndef Sudden_Underflow
363 /* d is denormalized */
365 i = bbits + be + (Bias + (P - 1) - 1);
366 x = i > 32 ? word0 (d) << (64 - i) | word1 (d) >> (i - 32)
367 : word1 (d) << (32 - i);
369 word0 (d2) -= 31 * Exp_msk1; /* adjust exponent */
370 i -= (Bias + (P - 1) - 1) + 1;
374 ds = (d2.d - 1.5) * 0.289529654602168 + 0.1760912590558 + i * 0.301029995663981;
376 if (ds < 0. && ds != k)
377 k--; /* want k = floor(ds) */
379 if (k >= 0 && k <= Ten_pmax)
408 if (mode < 0 || mode > 9)
431 ilim = ilim1 = i = ndigits;
443 j = sizeof (unsigned long);
444 for (ptr->_result_k = 0; (int) (sizeof (_Jv_Bigint) - sizeof (unsigned long)) + j <= i;
447 ptr->_result = Balloc (ptr, ptr->_result_k);
448 s = s0 = (char *) ptr->_result;
450 if (ilim >= 0 && ilim <= Quick_max && try_quick)
452 /* Try to get by with floating-point arithmetic. */
458 ieps = 2; /* conservative */
465 /* prevent overflows */
467 d.d /= bigtens[n_bigtens - 1];
470 for (; j; j >>= 1, i++)
480 d.d *= tens[j1 & 0xf];
481 for (j = j1 >> 4; j; j >>= 1, i++)
488 if (k_check && d.d < 1. && ilim > 0)
497 eps.d = ieps * d.d + 7.;
498 word0 (eps) -= (P - 1) * Exp_msk1;
512 /* Use Steele & White method of only
513 * generating digits needed.
515 eps.d = 0.5 / tens[ilim - 1] - eps.d;
520 *s++ = '0' + (int) L;
523 if (1. - d.d < eps.d)
534 /* Generate ilim digits, then fix them up. */
535 eps.d *= tens[ilim - 1];
536 for (i = 1;; i++, d.d *= 10.)
540 *s++ = '0' + (int) L;
543 if (d.d > 0.5 + eps.d)
545 else if (d.d < 0.5 - eps.d)
564 /* Do we have a "small" integer? */
566 if (be >= 0 && k <= Int_max)
570 if (ndigits < 0 && ilim <= 0)
573 if (ilim < 0 || d.d <= 5 * ds)
581 #ifdef Check_FLT_ROUNDS
582 /* If FLT_ROUNDS == 2, L will usually be high by 1 */
589 *s++ = '0' + (int) L;
593 if (d.d > ds || (d.d == ds && L & 1))
621 #ifndef Sudden_Underflow
622 denorm ? be + (Bias + (P - 1) - 1 + 1) :
625 1 + 4 * P - 3 - bbits + ((bbits + be - 1) & 3);
651 if (m2 > 0 && s2 > 0)
653 i = m2 < s2 ? m2 : s2;
664 mhi = pow5mult (ptr, mhi, m5);
665 b1 = mult (ptr, mhi, b);
670 b = pow5mult (ptr, b, j);
673 b = pow5mult (ptr, b, b5);
677 S = pow5mult (ptr, S, s5);
679 /* Check for special case that d is a normalized power of 2. */
683 if (!word1 (d) && !(word0 (d) & Bndry_mask)
684 #ifndef Sudden_Underflow
685 && word0(d) & Exp_mask
689 /* The special case */
698 /* Arrange for convenient computation of quotients:
699 * shift left if necessary so divisor has 4 leading 0 bits.
701 * Perhaps we should just compute leading 28 bits of S once
702 * and for all and pass them and a shift to quorem, so it
703 * can do shifts and ors to compute the numerator for q.
707 if ((i = ((s5 ? 32 - hi0bits (S->_x[S->_wds - 1]) : 1) + s2) & 0x1f))
710 if ((i = ((s5 ? 32 - hi0bits (S->_x[S->_wds - 1]) : 1) + s2) & 0xf))
728 b = lshift (ptr, b, b2);
730 S = lshift (ptr, S, s2);
736 b = multadd (ptr, b, 10, 0); /* we botched the k estimate */
738 mhi = multadd (ptr, mhi, 10, 0);
742 if (ilim <= 0 && mode > 2)
744 if (ilim < 0 || cmp (b, S = multadd (ptr, S, 5, 0)) <= 0)
746 /* no digits, fcvt style */
759 mhi = lshift (ptr, mhi, m2);
761 /* Single precision case, */
763 mhi = lshift (ptr, mhi, 29);
765 /* Compute mlo -- check for special case
766 * that d is a normalized power of 2.
772 mhi = Balloc (ptr, mhi->_k);
774 mhi = lshift (ptr, mhi, Log2P);
779 dig = quorem (b, S) + '0';
780 /* Do we yet have the shortest decimal string
781 * that will round to d?
784 delta = diff (ptr, S, mhi);
785 j1 = delta->_sign ? 1 : cmp (b, delta);
788 if (j1 == 0 && !mode && !(word1 (d) & 1))
798 if (j < 0 || (j == 0 && !mode
806 b = lshift (ptr, b, 1);
808 if ((j1 > 0 || (j1 == 0 && dig & 1))
818 { /* possible if i == 1 */
829 b = multadd (ptr, b, 10, 0);
831 mlo = mhi = multadd (ptr, mhi, 10, 0);
834 mlo = multadd (ptr, mlo, 10, 0);
835 mhi = multadd (ptr, mhi, 10, 0);
842 *s++ = dig = quorem (b, S) + '0';
845 b = multadd (ptr, b, 10, 0);
848 /* Round off last digit */
850 b = lshift (ptr, b, 1);
852 if (j > 0 || (j == 0 && dig & 1))
873 if (mlo && mlo != mhi)
889 (_d, mode, ndigits, decpt, sign, rve, buf, float_type),
899 struct _Jv_reent reent;
903 memset (&reent, 0, sizeof reent);
905 p = _dtoa_r (&reent, _d, mode, ndigits, decpt, sign, rve, float_type);
908 for (i = 0; i < reent._result_k; ++i)
910 struct _Jv_Bigint *l = reent._freelist[i];
913 struct _Jv_Bigint *next = l->_next;
919 free (reent._freelist);