/****************************************************************12The author of this software is David M. Gay.34Copyright (C) 1998, 1999 by Lucent Technologies5All Rights Reserved67Permission to use, copy, modify, and distribute this software and8its documentation for any purpose and without fee is hereby9granted, provided that the above copyright notice appear in all10copies and that both that the copyright notice and this11permission notice and warranty disclaimer appear in supporting12documentation, and that the name of Lucent or any of its entities13not be used in advertising or publicity pertaining to14distribution of the software without specific, written prior15permission.1617LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,18INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.19IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY20SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES21WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER22IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,23ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF24THIS SOFTWARE.2526****************************************************************/2728/* Please send bug reports to David M. Gay (dmg at acm dot org,29* with " at " changed at "@" and " dot " changed to "."). */3031#include "gdtoaimp.h"3233static Bigint *34#ifdef KR_headers35bitstob(bits, nbits, bbits) ULong *bits; int nbits; int *bbits;36#else37bitstob(ULong *bits, int nbits, int *bbits)38#endif39{40int i, k;41Bigint *b;42ULong *be, *x, *x0;4344i = ULbits;45k = 0;46while(i < nbits) {47i <<= 1;48k++;49}50#ifndef Pack_3251if (!k)52k = 1;53#endif54b = Balloc(k);55be = bits + ((nbits - 1) >> kshift);56x = x0 = b->x;57do {58*x++ = *bits & ALL_ON;59#ifdef Pack_1660*x++ = (*bits >> 16) & ALL_ON;61#endif62} while(++bits <= be);63i = x - x0;64while(!x0[--i])65if (!i) {66b->wds = 0;67*bbits = 0;68goto ret;69}70b->wds = i + 1;71*bbits = i*ULbits + 32 - hi0bits(b->x[i]);72ret:73return b;74}7576/* dtoa for IEEE arithmetic (dmg): convert double to ASCII string.77*78* Inspired by "How to Print Floating-Point Numbers Accurately" by79* Guy L. Steele, Jr. and Jon L. White [Proc. ACM SIGPLAN '90, pp. 112-126].80*81* Modifications:82* 1. Rather than iterating, we use a simple numeric overestimate83* to determine k = floor(log10(d)). We scale relevant84* quantities using O(log2(k)) rather than O(k) multiplications.85* 2. For some modes > 2 (corresponding to ecvt and fcvt), we don't86* try to generate digits strictly left to right. Instead, we87* compute with fewer bits and propagate the carry if necessary88* when rounding the final digit up. This is often faster.89* 3. Under the assumption that input will be rounded nearest,90* mode 0 renders 1e23 as 1e23 rather than 9.999999999999999e22.91* That is, we allow equality in stopping tests when the92* round-nearest rule will give the same floating-point value93* as would satisfaction of the stopping test with strict94* inequality.95* 4. We remove common factors of powers of 2 from relevant96* quantities.97* 5. When converting floating-point integers less than 1e16,98* we use floating-point arithmetic rather than resorting99* to multiple-precision integers.100* 6. When asked to produce fewer than 15 digits, we first try101* to get by with floating-point arithmetic; we resort to102* multiple-precision integer arithmetic only if we cannot103* guarantee that the floating-point calculation has given104* the correctly rounded result. For k requested digits and105* "uniformly" distributed input, the probability is106* something like 10^(k-15) that we must resort to the Long107* calculation.108*/109110char *111gdtoa112#ifdef KR_headers113(fpi, be, bits, kindp, mode, ndigits, decpt, rve)114FPI *fpi; int be; ULong *bits;115int *kindp, mode, ndigits, *decpt; char **rve;116#else117(FPI *fpi, int be, ULong *bits, int *kindp, int mode, int ndigits, int *decpt, char **rve)118#endif119{120/* Arguments ndigits and decpt are similar to the second and third121arguments of ecvt and fcvt; trailing zeros are suppressed from122the returned string. If not null, *rve is set to point123to the end of the return value. If d is +-Infinity or NaN,124then *decpt is set to 9999.125be = exponent: value = (integer represented by bits) * (2 to the power of be).126127mode:1280 ==> shortest string that yields d when read in129and rounded to nearest.1301 ==> like 0, but with Steele & White stopping rule;131e.g. with IEEE P754 arithmetic , mode 0 gives1321e23 whereas mode 1 gives 9.999999999999999e22.1332 ==> max(1,ndigits) significant digits. This gives a134return value similar to that of ecvt, except135that trailing zeros are suppressed.1363 ==> through ndigits past the decimal point. This137gives a return value similar to that from fcvt,138except that trailing zeros are suppressed, and139ndigits can be negative.1404-9 should give the same return values as 2-3, i.e.,1414 <= mode <= 9 ==> same return as mode1422 + (mode & 1). These modes are mainly for143debugging; often they run slower but sometimes144faster than modes 2-3.1454,5,8,9 ==> left-to-right digit generation.1466-9 ==> don't try fast floating-point estimate147(if applicable).148149Values of mode other than 0-9 are treated as mode 0.150151Sufficient space is allocated to the return value152to hold the suppressed trailing zeros.153*/154155int bbits, b2, b5, be0, dig, i, ieps, ilim, ilim0, ilim1, inex;156int j, j1, k, k0, k_check, kind, leftright, m2, m5, nbits;157int rdir, s2, s5, spec_case, try_quick;158Long L;159Bigint *b, *b1, *delta, *mlo, *mhi, *mhi1, *S;160double d2, ds;161char *s, *s0;162U d, eps;163164#ifndef MULTIPLE_THREADS165if (dtoa_result) {166freedtoa(dtoa_result);167dtoa_result = 0;168}169#endif170inex = 0;171kind = *kindp &= ~STRTOG_Inexact;172switch(kind & STRTOG_Retmask) {173case STRTOG_Zero:174goto ret_zero;175case STRTOG_Normal:176case STRTOG_Denormal:177break;178case STRTOG_Infinite:179*decpt = -32768;180return nrv_alloc("Infinity", rve, 8);181case STRTOG_NaN:182*decpt = -32768;183return nrv_alloc("NaN", rve, 3);184default:185return 0;186}187b = bitstob(bits, nbits = fpi->nbits, &bbits);188be0 = be;189if ( (i = trailz(b)) !=0) {190rshift(b, i);191be += i;192bbits -= i;193}194if (!b->wds) {195Bfree(b);196ret_zero:197*decpt = 1;198return nrv_alloc("0", rve, 1);199}200201dval(&d) = b2d(b, &i);202i = be + bbits - 1;203word0(&d) &= Frac_mask1;204word0(&d) |= Exp_11;205#ifdef IBM206if ( (j = 11 - hi0bits(word0(&d) & Frac_mask)) !=0)207dval(&d) /= 1 << j;208#endif209210/* log(x) ~=~ log(1.5) + (x-1.5)/1.5211* log10(x) = log(x) / log(10)212* ~=~ log(1.5)/log(10) + (x-1.5)/(1.5*log(10))213* log10(&d) = (i-Bias)*log(2)/log(10) + log10(d2)214*215* This suggests computing an approximation k to log10(&d) by216*217* k = (i - Bias)*0.301029995663981218* + ( (d2-1.5)*0.289529654602168 + 0.176091259055681 );219*220* We want k to be too large rather than too small.221* The error in the first-order Taylor series approximation222* is in our favor, so we just round up the constant enough223* to compensate for any error in the multiplication of224* (i - Bias) by 0.301029995663981; since |i - Bias| <= 1077,225* and 1077 * 0.30103 * 2^-52 ~=~ 7.2e-14,226* adding 1e-13 to the constant term more than suffices.227* Hence we adjust the constant term to 0.1760912590558.228* (We could get a more accurate k by invoking log10,229* but this is probably not worthwhile.)230*/231#ifdef IBM232i <<= 2;233i += j;234#endif235ds = (dval(&d)-1.5)*0.289529654602168 + 0.1760912590558 + i*0.301029995663981;236237/* correct assumption about exponent range */238if ((j = i) < 0)239j = -j;240if ((j -= 1077) > 0)241ds += j * 7e-17;242243k = (int)ds;244if (ds < 0. && ds != k)245k--; /* want k = floor(ds) */246k_check = 1;247#ifdef IBM248j = be + bbits - 1;249if ( (j1 = j & 3) !=0)250dval(&d) *= 1 << j1;251word0(&d) += j << Exp_shift - 2 & Exp_mask;252#else253word0(&d) += (be + bbits - 1) << Exp_shift;254#endif255if (k >= 0 && k <= Ten_pmax) {256if (dval(&d) < tens[k])257k--;258k_check = 0;259}260j = bbits - i - 1;261if (j >= 0) {262b2 = 0;263s2 = j;264}265else {266b2 = -j;267s2 = 0;268}269if (k >= 0) {270b5 = 0;271s5 = k;272s2 += k;273}274else {275b2 -= k;276b5 = -k;277s5 = 0;278}279if (mode < 0 || mode > 9)280mode = 0;281try_quick = 1;282if (mode > 5) {283mode -= 4;284try_quick = 0;285}286else if (i >= -4 - Emin || i < Emin)287try_quick = 0;288leftright = 1;289ilim = ilim1 = -1; /* Values for cases 0 and 1; done here to */290/* silence erroneous "gcc -Wall" warning. */291switch(mode) {292case 0:293case 1:294i = (int)(nbits * .30103) + 3;295ndigits = 0;296break;297case 2:298leftright = 0;299/* no break */300case 4:301if (ndigits <= 0)302ndigits = 1;303ilim = ilim1 = i = ndigits;304break;305case 3:306leftright = 0;307/* no break */308case 5:309i = ndigits + k + 1;310ilim = i;311ilim1 = i - 1;312if (i <= 0)313i = 1;314}315s = s0 = rv_alloc(i);316317if ( (rdir = fpi->rounding - 1) !=0) {318if (rdir < 0)319rdir = 2;320if (kind & STRTOG_Neg)321rdir = 3 - rdir;322}323324/* Now rdir = 0 ==> round near, 1 ==> round up, 2 ==> round down. */325326if (ilim >= 0 && ilim <= Quick_max && try_quick && !rdir327#ifndef IMPRECISE_INEXACT328&& k == 0329#endif330) {331332/* Try to get by with floating-point arithmetic. */333334i = 0;335d2 = dval(&d);336#ifdef IBM337if ( (j = 11 - hi0bits(word0(&d) & Frac_mask)) !=0)338dval(&d) /= 1 << j;339#endif340k0 = k;341ilim0 = ilim;342ieps = 2; /* conservative */343if (k > 0) {344ds = tens[k&0xf];345j = k >> 4;346if (j & Bletch) {347/* prevent overflows */348j &= Bletch - 1;349dval(&d) /= bigtens[n_bigtens-1];350ieps++;351}352for(; j; j >>= 1, i++)353if (j & 1) {354ieps++;355ds *= bigtens[i];356}357}358else {359ds = 1.;360if ( (j1 = -k) !=0) {361dval(&d) *= tens[j1 & 0xf];362for(j = j1 >> 4; j; j >>= 1, i++)363if (j & 1) {364ieps++;365dval(&d) *= bigtens[i];366}367}368}369if (k_check && dval(&d) < 1. && ilim > 0) {370if (ilim1 <= 0)371goto fast_failed;372ilim = ilim1;373k--;374dval(&d) *= 10.;375ieps++;376}377dval(&eps) = ieps*dval(&d) + 7.;378word0(&eps) -= (P-1)*Exp_msk1;379if (ilim == 0) {380S = mhi = 0;381dval(&d) -= 5.;382if (dval(&d) > dval(&eps))383goto one_digit;384if (dval(&d) < -dval(&eps))385goto no_digits;386goto fast_failed;387}388#ifndef No_leftright389if (leftright) {390/* Use Steele & White method of only391* generating digits needed.392*/393dval(&eps) = ds*0.5/tens[ilim-1] - dval(&eps);394for(i = 0;;) {395L = (Long)(dval(&d)/ds);396dval(&d) -= L*ds;397*s++ = '0' + (int)L;398if (dval(&d) < dval(&eps)) {399if (dval(&d))400inex = STRTOG_Inexlo;401goto ret1;402}403if (ds - dval(&d) < dval(&eps))404goto bump_up;405if (++i >= ilim)406break;407dval(&eps) *= 10.;408dval(&d) *= 10.;409}410}411else {412#endif413/* Generate ilim digits, then fix them up. */414dval(&eps) *= tens[ilim-1];415for(i = 1;; i++, dval(&d) *= 10.) {416if ( (L = (Long)(dval(&d)/ds)) !=0)417dval(&d) -= L*ds;418*s++ = '0' + (int)L;419if (i == ilim) {420ds *= 0.5;421if (dval(&d) > ds + dval(&eps))422goto bump_up;423else if (dval(&d) < ds - dval(&eps)) {424if (dval(&d))425inex = STRTOG_Inexlo;426goto clear_trailing0;427}428break;429}430}431#ifndef No_leftright432}433#endif434fast_failed:435s = s0;436dval(&d) = d2;437k = k0;438ilim = ilim0;439}440441/* Do we have a "small" integer? */442443if (be >= 0 && k <= Int_max) {444/* Yes. */445ds = tens[k];446if (ndigits < 0 && ilim <= 0) {447S = mhi = 0;448if (ilim < 0 || dval(&d) <= 5*ds)449goto no_digits;450goto one_digit;451}452for(i = 1;; i++, dval(&d) *= 10.) {453L = dval(&d) / ds;454dval(&d) -= L*ds;455#ifdef Check_FLT_ROUNDS456/* If FLT_ROUNDS == 2, L will usually be high by 1 */457if (dval(&d) < 0) {458L--;459dval(&d) += ds;460}461#endif462*s++ = '0' + (int)L;463if (dval(&d) == 0.)464break;465if (i == ilim) {466if (rdir) {467if (rdir == 1)468goto bump_up;469inex = STRTOG_Inexlo;470goto ret1;471}472dval(&d) += dval(&d);473#ifdef ROUND_BIASED474if (dval(&d) >= ds)475#else476if (dval(&d) > ds || (dval(&d) == ds && L & 1))477#endif478{479bump_up:480inex = STRTOG_Inexhi;481while(*--s == '9')482if (s == s0) {483k++;484*s = '0';485break;486}487++*s++;488}489else {490inex = STRTOG_Inexlo;491clear_trailing0:492while(*--s == '0'){}493++s;494}495break;496}497}498goto ret1;499}500501m2 = b2;502m5 = b5;503mhi = mlo = 0;504if (leftright) {505i = nbits - bbits;506if (be - i++ < fpi->emin && mode != 3 && mode != 5) {507/* denormal */508i = be - fpi->emin + 1;509if (mode >= 2 && ilim > 0 && ilim < i)510goto small_ilim;511}512else if (mode >= 2) {513small_ilim:514j = ilim - 1;515if (m5 >= j)516m5 -= j;517else {518s5 += j -= m5;519b5 += j;520m5 = 0;521}522if ((i = ilim) < 0) {523m2 -= i;524i = 0;525}526}527b2 += i;528s2 += i;529mhi = i2b(1);530}531if (m2 > 0 && s2 > 0) {532i = m2 < s2 ? m2 : s2;533b2 -= i;534m2 -= i;535s2 -= i;536}537if (b5 > 0) {538if (leftright) {539if (m5 > 0) {540mhi = pow5mult(mhi, m5);541b1 = mult(mhi, b);542Bfree(b);543b = b1;544}545if ( (j = b5 - m5) !=0)546b = pow5mult(b, j);547}548else549b = pow5mult(b, b5);550}551S = i2b(1);552if (s5 > 0)553S = pow5mult(S, s5);554555/* Check for special case that d is a normalized power of 2. */556557spec_case = 0;558if (mode < 2) {559if (bbits == 1 && be0 > fpi->emin + 1) {560/* The special case */561b2++;562s2++;563spec_case = 1;564}565}566567/* Arrange for convenient computation of quotients:568* shift left if necessary so divisor has 4 leading 0 bits.569*570* Perhaps we should just compute leading 28 bits of S once571* and for all and pass them and a shift to quorem, so it572* can do shifts and ors to compute the numerator for q.573*/574i = ((s5 ? hi0bits(S->x[S->wds-1]) : ULbits - 1) - s2 - 4) & kmask;575m2 += i;576if ((b2 += i) > 0)577b = lshift(b, b2);578if ((s2 += i) > 0)579S = lshift(S, s2);580if (k_check) {581if (cmp(b,S) < 0) {582k--;583b = multadd(b, 10, 0); /* we botched the k estimate */584if (leftright)585mhi = multadd(mhi, 10, 0);586ilim = ilim1;587}588}589if (ilim <= 0 && mode > 2) {590if (ilim < 0 || cmp(b,S = multadd(S,5,0)) <= 0) {591/* no digits, fcvt style */592no_digits:593k = -1 - ndigits;594inex = STRTOG_Inexlo;595goto ret;596}597one_digit:598inex = STRTOG_Inexhi;599*s++ = '1';600k++;601goto ret;602}603if (leftright) {604if (m2 > 0)605mhi = lshift(mhi, m2);606607/* Compute mlo -- check for special case608* that d is a normalized power of 2.609*/610611mlo = mhi;612if (spec_case) {613mhi = Balloc(mhi->k);614Bcopy(mhi, mlo);615mhi = lshift(mhi, 1);616}617618for(i = 1;;i++) {619dig = quorem(b,S) + '0';620/* Do we yet have the shortest decimal string621* that will round to d?622*/623j = cmp(b, mlo);624delta = diff(S, mhi);625j1 = delta->sign ? 1 : cmp(b, delta);626Bfree(delta);627#ifndef ROUND_BIASED628if (j1 == 0 && !mode && !(bits[0] & 1) && !rdir) {629if (dig == '9')630goto round_9_up;631if (j <= 0) {632if (b->wds > 1 || b->x[0])633inex = STRTOG_Inexlo;634}635else {636dig++;637inex = STRTOG_Inexhi;638}639*s++ = dig;640goto ret;641}642#endif643if (j < 0 || (j == 0 && !mode644#ifndef ROUND_BIASED645&& !(bits[0] & 1)646#endif647)) {648if (rdir && (b->wds > 1 || b->x[0])) {649if (rdir == 2) {650inex = STRTOG_Inexlo;651goto accept;652}653while (cmp(S,mhi) > 0) {654*s++ = dig;655mhi1 = multadd(mhi, 10, 0);656if (mlo == mhi)657mlo = mhi1;658mhi = mhi1;659b = multadd(b, 10, 0);660dig = quorem(b,S) + '0';661}662if (dig++ == '9')663goto round_9_up;664inex = STRTOG_Inexhi;665goto accept;666}667if (j1 > 0) {668b = lshift(b, 1);669j1 = cmp(b, S);670#ifdef ROUND_BIASED671if (j1 >= 0 /*)*/672#else673if ((j1 > 0 || (j1 == 0 && dig & 1))674#endif675&& dig++ == '9')676goto round_9_up;677inex = STRTOG_Inexhi;678}679if (b->wds > 1 || b->x[0])680inex = STRTOG_Inexlo;681accept:682*s++ = dig;683goto ret;684}685if (j1 > 0 && rdir != 2) {686if (dig == '9') { /* possible if i == 1 */687round_9_up:688*s++ = '9';689inex = STRTOG_Inexhi;690goto roundoff;691}692inex = STRTOG_Inexhi;693*s++ = dig + 1;694goto ret;695}696*s++ = dig;697if (i == ilim)698break;699b = multadd(b, 10, 0);700if (mlo == mhi)701mlo = mhi = multadd(mhi, 10, 0);702else {703mlo = multadd(mlo, 10, 0);704mhi = multadd(mhi, 10, 0);705}706}707}708else709for(i = 1;; i++) {710*s++ = dig = quorem(b,S) + '0';711if (i >= ilim)712break;713b = multadd(b, 10, 0);714}715716/* Round off last digit */717718if (rdir) {719if (rdir == 2 || (b->wds <= 1 && !b->x[0]))720goto chopzeros;721goto roundoff;722}723b = lshift(b, 1);724j = cmp(b, S);725#ifdef ROUND_BIASED726if (j >= 0)727#else728if (j > 0 || (j == 0 && dig & 1))729#endif730{731roundoff:732inex = STRTOG_Inexhi;733while(*--s == '9')734if (s == s0) {735k++;736*s++ = '1';737goto ret;738}739++*s++;740}741else {742chopzeros:743if (b->wds > 1 || b->x[0])744inex = STRTOG_Inexlo;745while(*--s == '0'){}746++s;747}748ret:749Bfree(S);750if (mhi) {751if (mlo && mlo != mhi)752Bfree(mlo);753Bfree(mhi);754}755ret1:756Bfree(b);757*s = 0;758*decpt = k + 1;759if (rve)760*rve = s;761*kindp |= inex;762return s0;763}764765766