home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / nsprpub / pr / src / misc / prdtoa.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  67.2 KB  |  3,190 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2.  
  3. #include "primpl.h"
  4.  
  5. /****************************************************************
  6.  *
  7.  * The author of this software is David M. Gay.
  8.  *
  9.  * Copyright (c) 1991 by AT&T.
  10.  *
  11.  * Permission to use, copy, modify, and distribute this software for any
  12.  * purpose without fee is hereby granted, provided that this entire notice
  13.  * is included in all copies of any software which is or includes a copy
  14.  * or modification of this software and in all copies of the supporting
  15.  * documentation for such software.
  16.  *
  17.  * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
  18.  * WARRANTY.  IN PARTICULAR, NEITHER THE AUTHOR NOR AT&T MAKES ANY
  19.  * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
  20.  * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
  21.  *
  22.  ***************************************************************/
  23.  
  24. /* Please send bug reports to
  25.     David M. Gay
  26.     AT&T Bell Laboratories, Room 2C-463
  27.     600 Mountain Avenue
  28.     Murray Hill, NJ 07974-2070
  29.     U.S.A.
  30.     dmg@research.att.com or research!dmg
  31.  */
  32.  
  33. /* strtod for IEEE-, VAX-, and IBM-arithmetic machines.
  34.  *
  35.  * This strtod returns a nearest machine number to the input decimal
  36.  * string (or sets errno to ERANGE).  With IEEE arithmetic, ties are
  37.  * broken by the IEEE round-even rule.  Otherwise ties are broken by
  38.  * biased rounding (add half and chop).
  39.  *
  40.  * Inspired loosely by William D. Clinger's paper "How to Read Floating
  41.  * Point Numbers Accurately" [Proc. ACM SIGPLAN '90, pp. 92-101].
  42.  *
  43.  * Modifications:
  44.  *
  45.  *    1. We only require IEEE, IBM, or VAX double-precision
  46.  *        arithmetic (not IEEE double-extended).
  47.  *    2. We get by with floating-point arithmetic in a case that
  48.  *        Clinger missed -- when we're computing d * 10^n
  49.  *        for a small integer d and the integer n is not too
  50.  *        much larger than 22 (the maximum integer k for which
  51.  *        we can represent 10^k exactly), we may be able to
  52.  *        compute (d*10^k) * 10^(e-k) with just one roundoff.
  53.  *    3. Rather than a bit-at-a-time adjustment of the binary
  54.  *        result in the hard case, we use floating-point
  55.  *        arithmetic to determine the adjustment to within
  56.  *        one bit; only in really hard cases do we need to
  57.  *        compute a second residual.
  58.  *    4. Because of 3., we don't need a large table of powers of 10
  59.  *        for ten-to-e (just some small tables, e.g. of 10^k
  60.  *        for 0 <= k <= 22).
  61.  */
  62.  
  63. /*
  64.  * #define IEEE_8087 for IEEE-arithmetic machines where the least
  65.  *    significant byte has the lowest address.
  66.  * #define IEEE_MC68k for IEEE-arithmetic machines where the most
  67.  *    significant byte has the lowest address.
  68.  * #define Long int on machines with 32-bit ints and 64-bit longs.
  69.  * #define Sudden_Underflow for IEEE-format machines without gradual
  70.  *    underflow (i.e., that flush to zero on underflow).
  71.  * #define IBM for IBM mainframe-style floating-point arithmetic.
  72.  * #define VAX for VAX-style floating-point arithmetic.
  73.  * #define Unsigned_Shifts if >> does treats its left operand as unsigned.
  74.  * #define No_leftright to omit left-right logic in fast floating-point
  75.  *    computation of PR_dtoa.
  76.  * #define Check_FLT_ROUNDS if FLT_ROUNDS can assume the values 2 or 3.
  77.  * #define RND_PRODQUOT to use rnd_prod and rnd_quot (assembly routines
  78.  *    that use extended-precision instructions to compute rounded
  79.  *    products and quotients) with IBM.
  80.  * #define ROUND_BIASED for IEEE-format with biased rounding.
  81.  * #define Inaccurate_Divide for IEEE-format with correctly rounded
  82.  *    products but inaccurate quotients, e.g., for Intel i860.
  83.  * #define Just_16 to store 16 bits per 32-bit Long when doing high-precision
  84.  *    integer arithmetic.  Whether this speeds things up or slows things
  85.  *    down depends on the machine and the number being converted.
  86.  * #define KR_headers for old-style C function headers.
  87.  * #define Bad_float_h if your system lacks a float.h or if it does not
  88.  *    define some or all of DBL_DIG, DBL_MAX_10_EXP, DBL_MAX_EXP,
  89.  *    FLT_RADIX, FLT_ROUNDS, and DBL_MAX.
  90.  * #define MALLOC your_malloc, where your_malloc(n) acts like malloc(n)
  91.  *    if memory is available and otherwise does something you deem
  92.  *    appropriate.  If MALLOC is undefined, malloc will be invoked
  93.  *    directly -- and assumed always to succeed.
  94.  */
  95. #if  defined(IS_LITTLE_ENDIAN)
  96. #define IEEE_8087
  97. #else
  98. #define IEEE_MC68k
  99. #endif
  100.  
  101. #ifndef Long
  102. #if PR_BYTES_PER_LONG == 4
  103. #define Long long
  104. #elif PR_BYTES_PER_INT == 4
  105. #define Long int
  106. #else
  107. #error "No suitable type for Long"
  108. #endif
  109. #endif
  110.  
  111. #ifdef DEBUG_DTOA
  112. #include "stdio.h"
  113. #define Bug(x) {fprintf(stderr, "%s\n", x); exit(1);}
  114. #else
  115. #define Bug(x)
  116. #endif
  117.  
  118. #include "stdlib.h"
  119. #include "string.h"
  120.  
  121. #ifdef MALLOC
  122. extern void *MALLOC(size_t);
  123. #else
  124. #define MALLOC PR_MALLOC
  125. #endif
  126.  
  127. #include "errno.h"
  128. #ifdef Bad_float_h
  129. #undef __STDC__
  130. #ifdef IEEE_MC68k
  131. #define IEEE_ARITHMETIC
  132. #endif
  133. #ifdef IEEE_8087
  134. #define IEEE_ARITHMETIC
  135. #endif
  136.  
  137. #ifdef IEEE_ARITHMETIC
  138. #define DBL_DIG 15
  139. #define DBL_MAX_10_EXP 308
  140. #define DBL_MAX_EXP 1024
  141. #define FLT_RADIX 2
  142. #define FLT_ROUNDS 1
  143. #define DBL_MAX 1.7976931348623157e+308
  144. #endif
  145.  
  146. #ifdef IBM
  147. #define DBL_DIG 16
  148. #define DBL_MAX_10_EXP 75
  149. #define DBL_MAX_EXP 63
  150. #define FLT_RADIX 16
  151. #define FLT_ROUNDS 0
  152. #define DBL_MAX 7.2370055773322621e+75
  153. #endif
  154.  
  155. #ifdef VAX
  156. #define DBL_DIG 16
  157. #define DBL_MAX_10_EXP 38
  158. #define DBL_MAX_EXP 127
  159. #define FLT_RADIX 2
  160. #define FLT_ROUNDS 1
  161. #define DBL_MAX 1.7014118346046923e+38
  162. #endif
  163.  
  164. #ifndef LONG_MAX
  165. #define LONG_MAX 2147483647
  166. #endif
  167. #else
  168. #include "float.h"
  169. #endif
  170. #ifndef __MATH_H__
  171. #include "math.h"
  172. #endif
  173.  
  174. #ifndef CONST
  175. #define CONST const
  176. #endif
  177.  
  178. #ifdef Unsigned_Shifts
  179. #define Sign_Extend(a,b) if (b < 0) a |= 0xffff0000;
  180. #else
  181. #define Sign_Extend(a,b) /*no-op*/
  182. #endif
  183.  
  184. #if defined(IEEE_8087) + defined(IEEE_MC68k) + defined(VAX) + defined(IBM)    != 1
  185. Exactly one of IEEE_8087, IEEE_MC68k, VAX, or IBM should be defined.
  186. #endif
  187.  
  188. #ifdef IEEE_8087
  189. #define word0(x) ((unsigned Long *)&x)[1]
  190. #define word1(x) ((unsigned Long *)&x)[0]
  191. #else
  192. #define word0(x) ((unsigned Long *)&x)[0]
  193. #define word1(x) ((unsigned Long *)&x)[1]
  194. #endif
  195.  
  196. /* The following definition of Storeinc is appropriate for MIPS processors.
  197.  * An alternative that might be better on some machines is
  198.  * #define Storeinc(a,b,c) (*a++ = b << 16 | c & 0xffff)
  199.  */
  200. #if defined(IEEE_8087) + defined(VAX)
  201. #define Storeinc(a,b,c) (((unsigned short *)a)[1] = (unsigned short)b, \
  202. ((unsigned short *)a)[0] = (unsigned short)c, a++)
  203. #else
  204. #define Storeinc(a,b,c) (((unsigned short *)a)[0] = (unsigned short)b, \
  205. ((unsigned short *)a)[1] = (unsigned short)c, a++)
  206. #endif
  207.  
  208. /* #define P DBL_MANT_DIG */
  209. /* Ten_pmax = floor(P*log(2)/log(5)) */
  210. /* Bletch = (highest power of 2 < DBL_MAX_10_EXP) / 16 */
  211. /* Quick_max = floor((P-1)*log(FLT_RADIX)/log(10) - 1) */
  212. /* Int_max = floor(P*log(FLT_RADIX)/log(10) - 1) */
  213.  
  214. #if defined(IEEE_8087) + defined(IEEE_MC68k)
  215. #define Exp_shift  20
  216. #define Exp_shift1 20
  217. #define Exp_msk1    0x100000
  218. #define Exp_msk11   0x100000
  219. #define Exp_mask  0x7ff00000
  220. #define P 53
  221. #define Bias 1023
  222. #define IEEE_Arith
  223. #define Emin (-1022)
  224. #define Exp_1  0x3ff00000
  225. #define Exp_11 0x3ff00000
  226. #define Ebits 11
  227. #define Frac_mask  0xfffff
  228. #define Frac_mask1 0xfffff
  229. #define Ten_pmax 22
  230. #define Bletch 0x10
  231. #define Bndry_mask  0xfffff
  232. #define Bndry_mask1 0xfffff
  233. #define LSB 1
  234. #define Sign_bit 0x80000000
  235. #define Log2P 1
  236. #define Tiny0 0
  237. #define Tiny1 1
  238. #define Quick_max 14
  239. #define Int_max 14
  240. #define Infinite(x) (word0(x) == 0x7ff00000) /* sufficient test for here */
  241. #else
  242. #undef  Sudden_Underflow
  243. #define Sudden_Underflow
  244. #ifdef IBM
  245. #define Exp_shift  24
  246. #define Exp_shift1 24
  247. #define Exp_msk1   0x1000000
  248. #define Exp_msk11  0x1000000
  249. #define Exp_mask  0x7f000000
  250. #define P 14
  251. #define Bias 65
  252. #define Exp_1  0x41000000
  253. #define Exp_11 0x41000000
  254. #define Ebits 8    /* exponent has 7 bits, but 8 is the right value in b2d */
  255. #define Frac_mask  0xffffff
  256. #define Frac_mask1 0xffffff
  257. #define Bletch 4
  258. #define Ten_pmax 22
  259. #define Bndry_mask  0xefffff
  260. #define Bndry_mask1 0xffffff
  261. #define LSB 1
  262. #define Sign_bit 0x80000000
  263. #define Log2P 4
  264. #define Tiny0 0x100000
  265. #define Tiny1 0
  266. #define Quick_max 14
  267. #define Int_max 15
  268. #else /* VAX */
  269. #define Exp_shift  23
  270. #define Exp_shift1 7
  271. #define Exp_msk1    0x80
  272. #define Exp_msk11   0x800000
  273. #define Exp_mask  0x7f80
  274. #define P 56
  275. #define Bias 129
  276. #define Exp_1  0x40800000
  277. #define Exp_11 0x4080
  278. #define Ebits 8
  279. #define Frac_mask  0x7fffff
  280. #define Frac_mask1 0xffff007f
  281. #define Ten_pmax 24
  282. #define Bletch 2
  283. #define Bndry_mask  0xffff007f
  284. #define Bndry_mask1 0xffff007f
  285. #define LSB 0x10000
  286. #define Sign_bit 0x8000
  287. #define Log2P 1
  288. #define Tiny0 0x80
  289. #define Tiny1 0
  290. #define Quick_max 15
  291. #define Int_max 15
  292. #endif
  293. #endif
  294.  
  295. #ifndef IEEE_Arith
  296. #define ROUND_BIASED
  297. #endif
  298.  
  299. #ifdef RND_PRODQUOT
  300. #define rounded_product(a,b) a = rnd_prod(a, b)
  301. #define rounded_quotient(a,b) a = rnd_quot(a, b)
  302. extern double rnd_prod(double, double), rnd_quot(double, double);
  303. #else
  304. #define rounded_product(a,b) a *= b
  305. #define rounded_quotient(a,b) a /= b
  306. #endif
  307.  
  308. #define Big0 (Frac_mask1 | Exp_msk1*(DBL_MAX_EXP+Bias-1))
  309. #define Big1 0xffffffff
  310.  
  311. #ifndef Just_16
  312. /* When Pack_32 is not defined, we store 16 bits per 32-bit Long.
  313.  * This makes some inner loops simpler and sometimes saves work
  314.  * during multiplications, but it often seems to make things slightly
  315.  * slower.  Hence the default is now to store 32 bits per Long.
  316.  */
  317. #ifndef Pack_32
  318. #define Pack_32
  319. #endif
  320. #endif
  321.  
  322. #define Kmax 15
  323.  
  324. /*
  325.  * Note: if you ever change struct Bigint, make sure that the
  326.  * definition of the Bcopy(x,y) macro is still correct.
  327.  */
  328. struct Bigint {
  329.     struct Bigint *next;
  330.     PRInt32 k, maxwds, sign, wds;
  331.     unsigned Long x[1];
  332. };
  333.  
  334. typedef struct Bigint Bigint;
  335.  
  336. static Bigint *freelist[Kmax+1];
  337.  
  338. static PRLock *freelist_lock;
  339.  
  340. static Bigint *Balloc(PRInt32 k)
  341. {
  342.     PRInt32 x;
  343.     Bigint *rv;
  344.  
  345.     PR_Lock(freelist_lock);
  346.     if ((rv = freelist[k]) != NULL) {
  347.         freelist[k] = rv->next;
  348.     }
  349.     PR_Unlock(freelist_lock);
  350.     if (rv == NULL) {
  351.         x = 1 << k;
  352.         rv = (Bigint *)MALLOC(sizeof(Bigint) + (x-1)*sizeof(Long));
  353.         rv->k = k;
  354.         rv->maxwds = x;
  355.     }
  356.     rv->sign = rv->wds = 0;
  357.     return rv;
  358. }
  359.  
  360. static void Bfree (Bigint *v)
  361. {
  362.     if (v) {
  363.         PR_Lock(freelist_lock);
  364.         v->next = freelist[v->k];
  365.         freelist[v->k] = v;
  366.         PR_Unlock(freelist_lock);
  367.     }
  368. }
  369.  
  370. /*
  371.  * The definition of the Bcopy macro is highly dependent on the
  372.  * ordering of members in struct Bigint.
  373.  */
  374. #define Bcopy(x,y) memcpy((char *)&x->sign, (char *)&y->sign, \
  375.                           y->wds*sizeof(Long) + 2*sizeof(PRInt32))
  376.  
  377. static Bigint *multadd(Bigint *b, PRInt32 m, PRInt32 a)    /* multiply by m and add a */
  378. {
  379.     PRInt32 i, wds;
  380.     unsigned Long *x, y;
  381. #ifdef Pack_32
  382.     unsigned Long xi, z;
  383. #endif
  384.     Bigint *b1;
  385.  
  386.     wds = b->wds;
  387.     x = b->x;
  388.     i = 0;
  389.     do {
  390. #ifdef Pack_32
  391.         xi = *x;
  392.         y = (xi & 0xffff) * m + a;
  393.         z = (xi >> 16) * m + (y >> 16);
  394.         a = (PRInt32)(z >> 16);
  395.         *x++ = (z << 16) + (y & 0xffff);
  396. #else
  397.         y = *x * m + a;
  398.         a = (PRInt32)(y >> 16);
  399.         *x++ = y & 0xffff;
  400. #endif
  401.     }
  402.     while(++i < wds);
  403.     if (a) {
  404.         if (wds >= b->maxwds) {
  405.             b1 = Balloc(b->k+1);
  406.             Bcopy(b1, b);
  407.             Bfree(b);
  408.             b = b1;
  409.         }
  410.         b->x[wds++] = a;
  411.         b->wds = wds;
  412.     }
  413.     return b;
  414. }
  415.  
  416. static Bigint *s2b(CONST char *s, PRInt32 nd0, PRInt32 nd, unsigned Long y9)
  417. {
  418.     Bigint *b;
  419.     PRInt32 i, k;
  420.     Long x, y;
  421.  
  422.     x = (nd + 8) / 9;
  423.     for(k = 0, y = 1; x > y; y <<= 1, k++) ;
  424. #ifdef Pack_32
  425.     b = Balloc(k);
  426.     b->x[0] = y9;
  427.     b->wds = 1;
  428. #else
  429.     b = Balloc(k+1);
  430.     b->x[0] = y9 & 0xffff;
  431.     b->wds = (b->x[1] = y9 >> 16) ? 2 : 1;
  432. #endif
  433.  
  434.     i = 9;
  435.     if (9 < nd0) {
  436.         s += 9;
  437.         do b = multadd(b, 10, *s++ - '0');
  438.         while(++i < nd0);
  439.         s++;
  440.     }
  441.     else
  442.         s += 10;
  443.     for(; i < nd; i++)
  444.         b = multadd(b, 10, *s++ - '0');
  445.     return b;
  446. }
  447.  
  448. static PRInt32 hi0bits(register unsigned Long x)
  449. {
  450.     register PRInt32 k = 0;
  451.  
  452.     if (!(x & 0xffff0000)) {
  453.         k = 16;
  454.         x <<= 16;
  455.     }
  456.     if (!(x & 0xff000000)) {
  457.         k += 8;
  458.         x <<= 8;
  459.     }
  460.     if (!(x & 0xf0000000)) {
  461.         k += 4;
  462.         x <<= 4;
  463.     }
  464.     if (!(x & 0xc0000000)) {
  465.         k += 2;
  466.         x <<= 2;
  467.     }
  468.     if (!(x & 0x80000000)) {
  469.         k++;
  470.         if (!(x & 0x40000000))
  471.             return 32;
  472.     }
  473.     return k;
  474. }
  475.  
  476. static PRInt32 lo0bits(unsigned Long *y)
  477. {
  478.     register PRInt32 k;
  479.     register unsigned Long x = *y;
  480.  
  481.     if (x & 7) {
  482.         if (x & 1)
  483.             return 0;
  484.         if (x & 2) {
  485.             *y = x >> 1;
  486.             return 1;
  487.         }
  488.         *y = x >> 2;
  489.         return 2;
  490.     }
  491.     k = 0;
  492.     if (!(x & 0xffff)) {
  493.         k = 16;
  494.         x >>= 16;
  495.     }
  496.     if (!(x & 0xff)) {
  497.         k += 8;
  498.         x >>= 8;
  499.     }
  500.     if (!(x & 0xf)) {
  501.         k += 4;
  502.         x >>= 4;
  503.     }
  504.     if (!(x & 0x3)) {
  505.         k += 2;
  506.         x >>= 2;
  507.     }
  508.     if (!(x & 1)) {
  509.         k++;
  510.         x >>= 1;
  511.         if (!x & 1)
  512.             return 32;
  513.     }
  514.     *y = x;
  515.     return k;
  516. }
  517.  
  518. static Bigint *i2b(PRInt32 i)
  519. {
  520.     Bigint *b;
  521.  
  522.     b = Balloc(1);
  523.     b->x[0] = i;
  524.     b->wds = 1;
  525.     return b;
  526. }
  527.  
  528. static Bigint *mult(CONST Bigint *a, CONST Bigint *b)
  529. {
  530.     CONST Bigint *t;
  531.     Bigint *c;
  532.     PRInt32 k, wa, wb, wc;
  533.     unsigned Long carry, y, z;
  534.     unsigned Long *xc, *xc0, *xce;
  535.     CONST unsigned Long *x, *xa, *xae, *xb, *xbe;
  536. #ifdef Pack_32
  537.     unsigned Long z2;
  538. #endif
  539.  
  540.     if (a->wds < b->wds) {
  541.         t = a;
  542.         a = b;
  543.         b = t;
  544.     }
  545.     k = a->k;
  546.     wa = a->wds;
  547.     wb = b->wds;
  548.     wc = wa + wb;
  549.     if (wc > a->maxwds)
  550.         k++;
  551.     c = Balloc(k);
  552.     for(xc = c->x, xce = xc + wc; xc < xce; xc++)
  553.         *xc = 0;
  554.     xa = a->x;
  555.     xae = xa + wa;
  556.     xb = b->x;
  557.     xbe = xb + wb;
  558.     xc0 = c->x;
  559. #ifdef Pack_32
  560.     for(; xb < xbe; xb++, xc0++) {
  561.         if ((y = *xb & 0xffff) != 0) {
  562.             x = xa;
  563.             xc = xc0;
  564.             carry = 0;
  565.             do {
  566.                 z = (*x & 0xffff) * y + (*xc & 0xffff) + carry;
  567.                 carry = z >> 16;
  568.                 z2 = (*x++ >> 16) * y + (*xc >> 16) + carry;
  569.                 carry = z2 >> 16;
  570.                 Storeinc(xc, z2, z);
  571.             }
  572.             while(x < xae);
  573.             *xc = carry;
  574.         }
  575.         if ((y = *xb >> 16) != 0) {
  576.             x = xa;
  577.             xc = xc0;
  578.             carry = 0;
  579.             z2 = *xc;
  580.             do {
  581.                 z = (*x & 0xffff) * y + (*xc >> 16) + carry;
  582.                 carry = z >> 16;
  583.                 Storeinc(xc, z, z2);
  584.                 z2 = (*x++ >> 16) * y + (*xc & 0xffff) + carry;
  585.                 carry = z2 >> 16;
  586.             }
  587.             while(x < xae);
  588.             *xc = z2;
  589.         }
  590.     }
  591. #else
  592.     for(; xb < xbe; xc0++) {
  593.         if (y = *xb++) {
  594.             x = xa;
  595.             xc = xc0;
  596.             carry = 0;
  597.             do {
  598.                 z = *x++ * y + *xc + carry;
  599.                 carry = z >> 16;
  600.                 *xc++ = z & 0xffff;
  601.             }
  602.             while(x < xae);
  603.             *xc = carry;
  604.         }
  605.     }
  606. #endif
  607.     for(xc0 = c->x, xc = xc0 + wc; wc > 0 && !*--xc; --wc) ;
  608.     c->wds = wc;
  609.     return c;
  610. }
  611.  
  612. /*
  613.  * 'p5s' points to a linked list of Bigints that are powers of 5.
  614.  * This list grows on demand, and it can only grow: it won't change
  615.  * in any other way.  So if we read 'p5s' or the 'next' field of
  616.  * some Bigint on the list, and it is not NULL, we know it won't
  617.  * change to NULL or some other value.  Only when the value of
  618.  * 'p5s' or 'next' is NULL do we need to acquire the lock and add
  619.  * a new Bigint to the list.
  620.  */
  621.  
  622. static Bigint *p5s;
  623.  
  624. static PRLock *p5s_lock;
  625.  
  626. static Bigint *pow5mult(Bigint *b, PRInt32 k)
  627. {
  628.     Bigint *b1, *p5, *p51;
  629.     PRInt32 i;
  630.     static CONST PRInt32 p05[3] = { 5, 25, 125 };
  631.  
  632.     if ((i = k & 3) != 0)
  633.         b = multadd(b, p05[i-1], 0);
  634.  
  635.     if (!(k >>= 2))
  636.         return b;
  637.     if (!(p5 = p5s)) {
  638.         /*
  639.          * We take great care to not call i2b() and Bfree()
  640.          * while holding the lock.
  641.          */
  642.         Bigint *wasted_effort = NULL;
  643.         p5 = i2b(625);
  644.         /* lock and check again */
  645.         PR_Lock(p5s_lock);
  646.         if (!p5s) {
  647.             /* first time */
  648.             p5s = p5;
  649.             p5->next = 0;
  650.         } else {
  651.             /* some other thread just beat us */
  652.             wasted_effort = p5;
  653.             p5 = p5s;
  654.         }
  655.         PR_Unlock(p5s_lock);
  656.         if (wasted_effort) {
  657.             Bfree(wasted_effort);
  658.         }
  659.     }
  660.     for(;;) {
  661.         if (k & 1) {
  662.             b1 = mult(b, p5);
  663.             Bfree(b);
  664.             b = b1;
  665.         }
  666.         if (!(k >>= 1))
  667.             break;
  668.         if (!(p51 = p5->next)) {
  669.             Bigint *wasted_effort = NULL;
  670.             p51 = mult(p5, p5);
  671.             PR_Lock(p5s_lock);
  672.             if (!p5->next) {
  673.                 p5->next = p51;
  674.                 p51->next = 0;
  675.             } else {
  676.                 wasted_effort = p51;
  677.                 p51 = p5->next;
  678.             }
  679.             PR_Unlock(p5s_lock);
  680.             if (wasted_effort) {
  681.                 Bfree(wasted_effort);
  682.             }
  683.         }
  684.         p5 = p51;
  685.     }
  686.     return b;
  687. }
  688.  
  689. static Bigint *lshift(Bigint *b, PRInt32 k)
  690. {
  691.     PRInt32 i, k1, n, n1;
  692.     Bigint *b1;
  693.     unsigned Long *x, *x1, *xe, z;
  694.  
  695. #ifdef Pack_32
  696.     n = k >> 5;
  697. #else
  698.     n = k >> 4;
  699. #endif
  700.     k1 = b->k;
  701.     n1 = n + b->wds + 1;
  702.     for(i = b->maxwds; n1 > i; i <<= 1)
  703.         k1++;
  704.     b1 = Balloc(k1);
  705.     x1 = b1->x;
  706.     for(i = 0; i < n; i++)
  707.         *x1++ = 0;
  708.     x = b->x;
  709.     xe = x + b->wds;
  710. #ifdef Pack_32
  711.     if (k &= 0x1f) {
  712.         k1 = 32 - k;
  713.         z = 0;
  714.         do {
  715.             *x1++ = *x << k | z;
  716.             z = *x++ >> k1;
  717.         }
  718.         while(x < xe);
  719.         if ((*x1 = z) != 0)
  720.             ++n1;
  721.     }
  722. #else
  723.     if (k &= 0xf) {
  724.         k1 = 16 - k;
  725.         z = 0;
  726.         do {
  727.             *x1++ = *x << k  & 0xffff | z;
  728.             z = *x++ >> k1;
  729.         }
  730.         while(x < xe);
  731.         if ((*x1 = z) != 0)
  732.             ++n1;
  733.     }
  734. #endif
  735.     else do
  736.         *x1++ = *x++;
  737.          while(x < xe);
  738.     b1->wds = n1 - 1;
  739.     Bfree(b);
  740.     return b1;
  741. }
  742.  
  743. static PRInt32 cmp(Bigint *a, Bigint *b)
  744. {
  745.     unsigned Long *xa, *xa0, *xb, *xb0;
  746.     PRInt32 i, j;
  747.  
  748.     i = a->wds;
  749.     j = b->wds;
  750. #ifdef DEBUG_DTOA
  751.     if ((i > 1 && !a->x[i-1]))
  752.         Bug("cmp called with a->x[a->wds-1] == 0");
  753.     if ((j > 1 && !b->x[j-1]))
  754.         Bug("cmp called with b->x[b->wds-1] == 0");
  755. #endif
  756.     if (i -= j)
  757.         return i;
  758.     xa0 = a->x;
  759.     xa = xa0 + j;
  760.     xb0 = b->x;
  761.     xb = xb0 + j;
  762.     for(;;) {
  763.         if (*--xa != *--xb)
  764.             return *xa < *xb ? -1 : 1;
  765.         if (xa <= xa0)
  766.             break;
  767.     }
  768.     return 0;
  769. }
  770.  
  771. static Bigint *diff(Bigint *a, Bigint *b)
  772. {
  773.     Bigint *c;
  774.     PRInt32 i, wa, wb;
  775.     Long borrow, y;    /* We need signed shifts here. */
  776.     unsigned Long *xa, *xae, *xb, *xbe, *xc;
  777. #ifdef Pack_32
  778.     Long z;
  779. #endif
  780.  
  781.     i = cmp(a,b);
  782.     if (!i) {
  783.         c = Balloc(0);
  784.         c->wds = 1;
  785.         c->x[0] = 0;
  786.         return c;
  787.     }
  788.     if (i < 0) {
  789.         c = a;
  790.         a = b;
  791.         b = c;
  792.         i = 1;
  793.     }
  794.     else
  795.         i = 0;
  796.     c = Balloc(a->k);
  797.     c->sign = i;
  798.     wa = a->wds;
  799.     xa = a->x;
  800.     xae = xa + wa;
  801.     wb = b->wds;
  802.     xb = b->x;
  803.     xbe = xb + wb;
  804.     xc = c->x;
  805.     borrow = 0;
  806. #ifdef Pack_32
  807.     do {
  808.         y = (long)((*xa & 0xffff) - (*xb & 0xffff) + borrow);
  809.         borrow = y >> 16;
  810.         Sign_Extend(borrow, y);
  811.         z = (long)((*xa++ >> 16) - (*xb++ >> 16) + borrow);
  812.         borrow = z >> 16;
  813.         Sign_Extend(borrow, z);
  814.         Storeinc(xc, z, y);
  815.     }
  816.     while(xb < xbe);
  817.     while(xa < xae) {
  818.         y = (long)((*xa & 0xffff) + borrow);
  819.         borrow = y >> 16;
  820.         Sign_Extend(borrow, y);
  821.         z = (long)((*xa++ >> 16) + borrow);
  822.         borrow = z >> 16;
  823.         Sign_Extend(borrow, z);
  824.         Storeinc(xc, z, y);
  825.     }
  826. #else
  827.     do {
  828.         y = *xa++ - *xb++ + borrow;
  829.         borrow = y >> 16;
  830.         Sign_Extend(borrow, y);
  831.         *xc++ = y & 0xffff;
  832.     }
  833.     while(xb < xbe);
  834.     while(xa < xae) {
  835.         y = *xa++ + borrow;
  836.         borrow = y >> 16;
  837.         Sign_Extend(borrow, y);
  838.         *xc++ = y & 0xffff;
  839.     }
  840. #endif
  841.     while(!*--xc)
  842.         wa--;
  843.     c->wds = wa;
  844.     return c;
  845. }
  846.  
  847. static double ulp(double x)
  848. {
  849.     register Long L;
  850.     double a;
  851.  
  852.     L = (long)((word0(x) & Exp_mask) - (P-1)*Exp_msk1);
  853. #ifndef Sudden_Underflow
  854.     if (L > 0) {
  855. #endif
  856. #ifdef IBM
  857.         L |= Exp_msk1 >> 4;
  858. #endif
  859.         word0(a) = L;
  860.         word1(a) = 0;
  861. #ifndef Sudden_Underflow
  862.     }
  863.     else {
  864.         L = -L >> Exp_shift;
  865.         if (L < Exp_shift) {
  866.             word0(a) = 0x80000 >> L;
  867.             word1(a) = 0;
  868.         }
  869.         else {
  870.             word0(a) = 0;
  871.             L -= Exp_shift;
  872.             word1(a) = L >= 31 ? 1 : 1 << (31 - L);
  873.         }
  874.     }
  875. #endif
  876.     return a;
  877. }
  878.  
  879. static double
  880. b2d
  881. #ifdef KR_headers
  882. (a, e) Bigint *a; PRInt32 *e;
  883. #else
  884. (Bigint *a, PRInt32 *e)
  885. #endif
  886. {
  887.     unsigned Long *xa, *xa0, w, y, z;
  888.     PRInt32 k;
  889.     double d;
  890. #ifdef VAX
  891.     unsigned Long d0, d1;
  892. #else
  893. #define d0 word0(d)
  894. #define d1 word1(d)
  895. #endif
  896.  
  897.     xa0 = a->x;
  898.     xa = xa0 + a->wds;
  899.     y = *--xa;
  900. #ifdef DEBUG_DTOA
  901.     if (!y) Bug("zero y in b2d");
  902. #endif
  903.     k = hi0bits(y);
  904.     *e = 32 - k;
  905. #ifdef Pack_32
  906.     if (k < Ebits) {
  907.         d0 = Exp_1 | y >> (Ebits - k);
  908.         w = xa > xa0 ? *--xa : 0;
  909.         d1 = y << (32 - Ebits + k) | w >> (Ebits - k);
  910.         goto ret_d;
  911.     }
  912.     z = xa > xa0 ? *--xa : 0;
  913.     if (k -= Ebits) {
  914.         d0 = Exp_1 | y << k | z >> (32 - k);
  915.         y = xa > xa0 ? *--xa : 0;
  916.         d1 = z << k | y >> (32 - k);
  917.     }
  918.     else {
  919.         d0 = Exp_1 | y;
  920.         d1 = z;
  921.     }
  922. #else
  923.     if (k < Ebits + 16) {
  924.         z = xa > xa0 ? *--xa : 0;
  925.         d0 = Exp_1 | y << k - Ebits | z >> Ebits + 16 - k;
  926.         w = xa > xa0 ? *--xa : 0;
  927.         y = xa > xa0 ? *--xa : 0;
  928.         d1 = z << k + 16 - Ebits | w << k - Ebits | y >> 16 + Ebits - k;
  929.         goto ret_d;
  930.     }
  931.     z = xa > xa0 ? *--xa : 0;
  932.     w = xa > xa0 ? *--xa : 0;
  933.     k -= Ebits + 16;
  934.     d0 = Exp_1 | y << k + 16 | z << k | w >> 16 - k;
  935.     y = xa > xa0 ? *--xa : 0;
  936.     d1 = w << k + 16 | y << k;
  937. #endif
  938. ret_d:
  939. #ifdef VAX
  940.     word0(d) = d0 >> 16 | d0 << 16;
  941.     word1(d) = d1 >> 16 | d1 << 16;
  942. #else
  943. #undef d0
  944. #undef d1
  945. #endif
  946.     return d;
  947. }
  948.  
  949. static Bigint *
  950. d2b
  951. #ifdef KR_headers
  952. (d, e, bits) double d; PRInt32 *e, *bits;
  953. #else
  954. (double d, PRInt32 *e, PRInt32 *bits)
  955. #endif
  956. {
  957.     Bigint *b;
  958.     PRInt32 de, i, k;
  959.     unsigned Long *x, y, z;
  960. #ifdef VAX
  961.     unsigned Long d0, d1;
  962.     d0 = word0(d) >> 16 | word0(d) << 16;
  963.     d1 = word1(d) >> 16 | word1(d) << 16;
  964. #else
  965. #define d0 word0(d)
  966. #define d1 word1(d)
  967. #endif
  968.  
  969. #ifdef Pack_32
  970.     b = Balloc(1);
  971. #else
  972.     b = Balloc(2);
  973. #endif
  974.     x = b->x;
  975.  
  976.     z = d0 & Frac_mask;
  977.     d0 &= 0x7fffffff;    /* clear sign bit, which we ignore */
  978. #ifdef Sudden_Underflow
  979.     de = (PRInt32)(d0 >> Exp_shift);
  980. #ifndef IBM
  981.     z |= Exp_msk11;
  982. #endif
  983. #else
  984.     if ((de = (PRInt32)(d0 >> Exp_shift)) != 0)
  985.         z |= Exp_msk1;
  986. #endif
  987. #ifdef Pack_32
  988.     if ((y = d1) != 0) {
  989.         if ((k = lo0bits(&y)) != 0) {
  990.             x[0] = y | z << (32 - k);
  991.             z >>= k;
  992.         }
  993.         else
  994.             x[0] = y;
  995.         i = b->wds = (x[1] = z) ? 2 : 1;
  996.     }
  997.     else {
  998. #ifdef DEBUG_DTOA
  999.         if (!z)
  1000.             Bug("Zero passed to d2b");
  1001. #endif
  1002.         k = lo0bits(&z);
  1003.         x[0] = z;
  1004.         i = b->wds = 1;
  1005.         k += 32;
  1006.     }
  1007. #else
  1008.     if ((y = d1) != 0) {
  1009.         if ((k = lo0bits(&y)) != 0)
  1010.             if (k >= 16) {
  1011.                 x[0] = y | z << 32 - k & 0xffff;
  1012.                 x[1] = z >> k - 16 & 0xffff;
  1013.                 x[2] = z >> k;
  1014.                 i = 2;
  1015.             }
  1016.             else {
  1017.                 x[0] = y & 0xffff;
  1018.                 x[1] = y >> 16 | z << 16 - k & 0xffff;
  1019.                 x[2] = z >> k & 0xffff;
  1020.                 x[3] = z >> k+16;
  1021.                 i = 3;
  1022.             }
  1023.         else {
  1024.             x[0] = y & 0xffff;
  1025.             x[1] = y >> 16;
  1026.             x[2] = z & 0xffff;
  1027.             x[3] = z >> 16;
  1028.             i = 3;
  1029.         }
  1030.     }
  1031.     else {
  1032. #ifdef DEBUG_DTOA
  1033.         if (!z)
  1034.             Bug("Zero passed to d2b");
  1035. #endif
  1036.         k = lo0bits(&z);
  1037.         if (k >= 16) {
  1038.             x[0] = z;
  1039.             i = 0;
  1040.         }
  1041.         else {
  1042.             x[0] = z & 0xffff;
  1043.             x[1] = z >> 16;
  1044.             i = 1;
  1045.         }
  1046.         k += 32;
  1047.     }
  1048.     while(!x[i])
  1049.         --i;
  1050.     b->wds = i + 1;
  1051. #endif
  1052. #ifndef Sudden_Underflow
  1053.     if (de) {
  1054. #endif
  1055. #ifdef IBM
  1056.         *e = (de - Bias - (P-1) << 2) + k;
  1057.         *bits = 4*P + 8 - k - hi0bits(word0(d) & Frac_mask);
  1058. #else
  1059.         *e = de - Bias - (P-1) + k;
  1060.         *bits = P - k;
  1061. #endif
  1062. #ifndef Sudden_Underflow
  1063.     }
  1064.     else {
  1065.         *e = de - Bias - (P-1) + 1 + k;
  1066. #ifdef Pack_32
  1067.         *bits = 32*i - hi0bits(x[i-1]);
  1068. #else
  1069.         *bits = (i+2)*16 - hi0bits(x[i]);
  1070. #endif
  1071.     }
  1072. #endif
  1073.     return b;
  1074. }
  1075. #undef d0
  1076. #undef d1
  1077.  
  1078. static double
  1079. ratio
  1080. #ifdef KR_headers
  1081. (a, b) Bigint *a, *b;
  1082. #else
  1083. (Bigint *a, Bigint *b)
  1084. #endif
  1085. {
  1086.     double da, db;
  1087.     PRInt32 k, ka, kb;
  1088.  
  1089.     da = b2d(a, &ka);
  1090.     db = b2d(b, &kb);
  1091. #ifdef Pack_32
  1092.     k = ka - kb + 32*(a->wds - b->wds);
  1093. #else
  1094.     k = ka - kb + 16*(a->wds - b->wds);
  1095. #endif
  1096. #ifdef IBM
  1097.     if (k > 0) {
  1098.         word0(da) += (k >> 2)*Exp_msk1;
  1099.         if (k &= 3)
  1100.             da *= 1 << k;
  1101.     }
  1102.     else {
  1103.         k = -k;
  1104.         word0(db) += (k >> 2)*Exp_msk1;
  1105.         if (k &= 3)
  1106.             db *= 1 << k;
  1107.     }
  1108. #else
  1109.     if (k > 0)
  1110.         word0(da) += k*Exp_msk1;
  1111.     else {
  1112.         k = -k;
  1113.         word0(db) += k*Exp_msk1;
  1114.     }
  1115. #endif
  1116.     return da / db;
  1117. }
  1118.  
  1119. static CONST double
  1120. tens[] = {
  1121.     1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9,
  1122.     1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19,
  1123.     1e20, 1e21, 1e22
  1124. #ifdef VAX
  1125.     , 1e23, 1e24
  1126. #endif
  1127. };
  1128.  
  1129. static CONST double
  1130. #ifdef IEEE_Arith
  1131. bigtens[] = { 1e16, 1e32, 1e64, 1e128, 1e256 };
  1132. static CONST double tinytens[] = { 1e-16, 1e-32, 1e-64, 1e-128, 1e-256 };
  1133. #define n_bigtens 5
  1134. #else
  1135. #ifdef IBM
  1136. bigtens[] = { 1e16, 1e32, 1e64 };
  1137. static CONST double tinytens[] = { 1e-16, 1e-32, 1e-64 };
  1138. #define n_bigtens 3
  1139. #else
  1140. bigtens[] = { 1e16, 1e32 };
  1141. static CONST double tinytens[] = { 1e-16, 1e-32 };
  1142. #define n_bigtens 2
  1143. #endif
  1144. #endif
  1145.  
  1146. void _PR_InitDtoa(void)
  1147. {
  1148.     freelist_lock = PR_NewLock();
  1149.     p5s_lock = PR_NewLock();
  1150. }
  1151.  
  1152. #if defined(HAVE_WATCOM_BUG_1)
  1153. PRFloat64 __pascal __loadds  __export
  1154. #else
  1155. PR_IMPLEMENT(PRFloat64) 
  1156. #endif
  1157. PR_strtod(CONST char *s00, char **se)
  1158. {
  1159.     PRInt32 bb2, bb5, bbe, bd2, bd5, bbbits, bs2, c, dsign,
  1160.         e, e1, esign, i, j, k, nd, nd0, nf, nz, nz0, sign;
  1161.     CONST char *s, *s0, *s1;
  1162.     PRFloat64 aadj, aadj1, adj, rv, rv0;
  1163.     Long L;
  1164.     unsigned Long y, z;
  1165.     Bigint *bb, *bb1, *bd, *bd0, *bs, *delta;
  1166.  
  1167.  
  1168.     if (!_pr_initialized) _PR_ImplicitInitialization();
  1169.  
  1170.     sign = nz0 = nz = 0;
  1171.     rv = 0.;
  1172.     for(s = s00;;s++) switch(*s) {
  1173.     case '-':
  1174.         sign = 1;
  1175.         /* no break */
  1176.     case '+':
  1177.         if (*++s)
  1178.             goto break2;
  1179.         /* no break */
  1180.     case 0:
  1181.         s = s00;
  1182.         goto ret;
  1183.     case '\t':
  1184.     case '\n':
  1185.     case '\v':
  1186.     case '\f':
  1187.     case '\r':
  1188.     case ' ':
  1189.         continue;
  1190.     default:
  1191.         goto break2;
  1192.     }
  1193. break2:
  1194.     if (*s == '0') {
  1195.         nz0 = 1;
  1196.         while(*++s == '0') ;
  1197.         if (!*s)
  1198.             goto ret;
  1199.     }
  1200.     s0 = s;
  1201.     y = z = 0;
  1202.     for(nd = nf = 0; (c = *s) >= '0' && c <= '9'; nd++, s++)
  1203.         if (nd < 9)
  1204.             y = 10*y + c - '0';
  1205.         else if (nd < 16)
  1206.             z = 10*z + c - '0';
  1207.     nd0 = nd;
  1208.     if (c == '.') {
  1209.         c = *++s;
  1210.         if (!nd) {
  1211.             for(; c == '0'; c = *++s)
  1212.                 nz++;
  1213.             if (c > '0' && c <= '9') {
  1214.                 s0 = s;
  1215.                 nf += nz;
  1216.                 nz = 0;
  1217.                 goto have_dig;
  1218.             }
  1219.             goto dig_done;
  1220.         }
  1221.         for(; c >= '0' && c <= '9'; c = *++s) {
  1222.         have_dig:
  1223.             nz++;
  1224.             if (c -= '0') {
  1225.                 nf += nz;
  1226.                 for(i = 1; i < nz; i++)
  1227.                     if (nd++ < 9)
  1228.                         y *= 10;
  1229.                     else if (nd <= DBL_DIG + 1)
  1230.                         z *= 10;
  1231.                 if (nd++ < 9)
  1232.                     y = 10*y + c;
  1233.                 else if (nd <= DBL_DIG + 1)
  1234.                     z = 10*z + c;
  1235.                 nz = 0;
  1236.             }
  1237.         }
  1238.     }
  1239. dig_done:
  1240.     e = 0;
  1241.     if (c == 'e' || c == 'E') {
  1242.         if (!nd && !nz && !nz0) {
  1243.             s = s00;
  1244.             goto ret;
  1245.         }
  1246.         s00 = s;
  1247.         esign = 0;
  1248.         switch(c = *++s) {
  1249.         case '-':
  1250.             esign = 1;
  1251.         case '+':
  1252.             c = *++s;
  1253.         }
  1254.         if (c >= '0' && c <= '9') {
  1255.             while(c == '0')
  1256.                 c = *++s;
  1257.             if (c > '0' && c <= '9') {
  1258.                 L = c - '0';
  1259.                 s1 = s;
  1260.                 while((c = *++s) >= '0' && c <= '9')
  1261.                     L = 10*L + c - '0';
  1262.                 if (s - s1 > 8 || L > 19999)
  1263.                     /* Avoid confusion from exponents
  1264.                      * so large that e might overflow.
  1265.                      */
  1266.                     e = 19999; /* safe for 16 bit ints */
  1267.                 else
  1268.                     e = (PRInt32)L;
  1269.                 if (esign)
  1270.                     e = -e;
  1271.             }
  1272.             else
  1273.                 e = 0;
  1274.         }
  1275.         else
  1276.             s = s00;
  1277.     }
  1278.     if (!nd) {
  1279.         if (!nz && !nz0)
  1280.             s = s00;
  1281.         goto ret;
  1282.     }
  1283.     e1 = e -= nf;
  1284.  
  1285.     /* Now we have nd0 digits, starting at s0, followed by a
  1286.      * decimal point, followed by nd-nd0 digits.  The number we're
  1287.      * after is the integer represented by those digits times
  1288.      * 10**e */
  1289.  
  1290.     if (!nd0)
  1291.         nd0 = nd;
  1292.     k = nd < DBL_DIG + 1 ? nd : DBL_DIG + 1;
  1293.     rv = y;
  1294.     if (k > 9)
  1295.         rv = tens[k - 9] * rv + z;
  1296.     bd0 = 0;
  1297.     if (nd <= DBL_DIG
  1298. #ifndef RND_PRODQUOT
  1299.         && FLT_ROUNDS == 1
  1300. #endif
  1301.         ) {
  1302.         if (!e)
  1303.             goto ret;
  1304.         if (e > 0) {
  1305.             if (e <= Ten_pmax) {
  1306. #ifdef VAX
  1307.                 goto vax_ovfl_check;
  1308. #else
  1309.                 /* rv = */ rounded_product(rv, tens[e]);
  1310.                 goto ret;
  1311. #endif
  1312.             }
  1313.             i = DBL_DIG - nd;
  1314.             if (e <= Ten_pmax + i) {
  1315.                 /* A fancier test would sometimes let us do
  1316.                  * this for larger i values.
  1317.                  */
  1318.                 e -= i;
  1319.                 rv *= tens[i];
  1320. #ifdef VAX
  1321.                 /* VAX exponent range is so narrow we must
  1322.                  * worry about overflow here...
  1323.                  */
  1324.             vax_ovfl_check:
  1325.                 word0(rv) -= P*Exp_msk1;
  1326.                 /* rv = */ rounded_product(rv, tens[e]);
  1327.                 if ((word0(rv) & Exp_mask)
  1328.                     > Exp_msk1*(DBL_MAX_EXP+Bias-1-P))
  1329.                     goto ovfl;
  1330.                 word0(rv) += P*Exp_msk1;
  1331. #else
  1332.                 /* rv = */ rounded_product(rv, tens[e]);
  1333. #endif
  1334.                 goto ret;
  1335.             }
  1336.         }
  1337. #ifndef Inaccurate_Divide
  1338.         else if (e >= -Ten_pmax) {
  1339.             /* rv = */ rounded_quotient(rv, tens[-e]);
  1340.             goto ret;
  1341.         }
  1342. #endif
  1343.     }
  1344.     e1 += nd - k;
  1345.  
  1346.     /* Get starting approximation = rv * 10**e1 */
  1347.  
  1348.     if (e1 > 0) {
  1349.         if ((i = e1 & 15) != 0)
  1350.             rv *= tens[i];
  1351.         if (e1 &= ~15) {
  1352.             if (e1 > DBL_MAX_10_EXP) {
  1353.             ovfl:
  1354.                 errno = ERANGE;
  1355. #ifdef __STDC__
  1356.                 rv = HUGE_VAL;
  1357. #else
  1358.                 /* Can't trust HUGE_VAL */
  1359. #ifdef IEEE_Arith
  1360.                 word0(rv) = Exp_mask;
  1361.                 word1(rv) = 0;
  1362. #else
  1363.                 word0(rv) = Big0;
  1364.                 word1(rv) = Big1;
  1365. #endif
  1366. #endif
  1367.                 if (bd0)
  1368.                     goto retfree;
  1369.                 goto ret;
  1370.             }
  1371.             if (e1 >>= 4) {
  1372.                 for(j = 0; e1 > 1; j++, e1 >>= 1)
  1373.                     if (e1 & 1)
  1374.                         rv *= bigtens[j];
  1375.                 /* The last multiplication could overflow. */
  1376.                 word0(rv) -= P*Exp_msk1;
  1377.                 rv *= bigtens[j];
  1378.                 if ((z = word0(rv) & Exp_mask)
  1379.                     > Exp_msk1*(DBL_MAX_EXP+Bias-P))
  1380.                     goto ovfl;
  1381.                 if (z > Exp_msk1*(DBL_MAX_EXP+Bias-1-P)) {
  1382.                     /* set to largest number */
  1383.                     /* (Can't trust DBL_MAX) */
  1384.                     word0(rv) = Big0;
  1385.                     word1(rv) = Big1;
  1386.                 }
  1387.                 else
  1388.                     word0(rv) += P*Exp_msk1;
  1389.             }
  1390.  
  1391.         }
  1392.     }
  1393.     else if (e1 < 0) {
  1394.         e1 = -e1;
  1395.         if ((i = e1 & 15) != 0)
  1396.             rv /= tens[i];
  1397.         if (e1 &= ~15) {
  1398.             e1 >>= 4;
  1399.             if (e1 >= 1 << n_bigtens)
  1400.                 goto undfl;
  1401.             for(j = 0; e1 > 1; j++, e1 >>= 1)
  1402.                 if (e1 & 1)
  1403.                     rv *= tinytens[j];
  1404.             /* The last multiplication could underflow. */
  1405.             rv0 = rv;
  1406.             rv *= tinytens[j];
  1407.             if (!rv) {
  1408.                 rv = 2.*rv0;
  1409.                 rv *= tinytens[j];
  1410.                 if (!rv) {
  1411.                 undfl:
  1412.                     rv = 0.;
  1413.                     errno = ERANGE;
  1414.                     if (bd0)
  1415.                         goto retfree;
  1416.                     goto ret;
  1417.                 }
  1418.                 word0(rv) = Tiny0;
  1419.                 word1(rv) = Tiny1;
  1420.                 /* The refinement below will clean
  1421.                  * this approximation up.
  1422.                  */
  1423.             }
  1424.         }
  1425.     }
  1426.  
  1427.     /* Now the hard part -- adjusting rv to the correct value.*/
  1428.  
  1429.     /* Put digits into bd: true value = bd * 10^e */
  1430.  
  1431.     bd0 = s2b(s0, nd0, nd, y);
  1432.  
  1433.     for(;;) {
  1434.         bd = Balloc(bd0->k);
  1435.         Bcopy(bd, bd0);
  1436.         bb = d2b(rv, &bbe, &bbbits);    /* rv = bb * 2^bbe */
  1437.         bs = i2b(1);
  1438.  
  1439.         if (e >= 0) {
  1440.             bb2 = bb5 = 0;
  1441.             bd2 = bd5 = e;
  1442.         }
  1443.         else {
  1444.             bb2 = bb5 = -e;
  1445.             bd2 = bd5 = 0;
  1446.         }
  1447.         if (bbe >= 0)
  1448.             bb2 += bbe;
  1449.         else
  1450.             bd2 -= bbe;
  1451.         bs2 = bb2;
  1452. #ifdef Sudden_Underflow
  1453. #ifdef IBM
  1454.         j = 1 + 4*P - 3 - bbbits + ((bbe + bbbits - 1) & 3);
  1455. #else
  1456.         j = P + 1 - bbbits;
  1457. #endif
  1458. #else
  1459.         i = bbe + bbbits - 1;    /* logb(rv) */
  1460.         if (i < Emin)    /* denormal */
  1461.             j = bbe + (P-Emin);
  1462.         else
  1463.             j = P + 1 - bbbits;
  1464. #endif
  1465.         bb2 += j;
  1466.         bd2 += j;
  1467.         i = bb2 < bd2 ? bb2 : bd2;
  1468.         if (i > bs2)
  1469.             i = bs2;
  1470.         if (i > 0) {
  1471.             bb2 -= i;
  1472.             bd2 -= i;
  1473.             bs2 -= i;
  1474.         }
  1475.         if (bb5 > 0) {
  1476.             bs = pow5mult(bs, bb5);
  1477.             bb1 = mult(bs, bb);
  1478.             Bfree(bb);
  1479.             bb = bb1;
  1480.         }
  1481.         if (bb2 > 0)
  1482.             bb = lshift(bb, bb2);
  1483.         if (bd5 > 0)
  1484.             bd = pow5mult(bd, bd5);
  1485.         if (bd2 > 0)
  1486.             bd = lshift(bd, bd2);
  1487.         if (bs2 > 0)
  1488.             bs = lshift(bs, bs2);
  1489.         delta = diff(bb, bd);
  1490.         dsign = delta->sign;
  1491.         delta->sign = 0;
  1492.         i = cmp(delta, bs);
  1493.         if (i < 0) {
  1494.             /* Error is less than half an ulp -- check for
  1495.              * special case of mantissa a power of two.
  1496.              */
  1497.             if (dsign || word1(rv) || word0(rv) & Bndry_mask)
  1498.                 break;
  1499.             delta = lshift(delta,Log2P);
  1500.             if (cmp(delta, bs) > 0)
  1501.                 goto drop_down;
  1502.             break;
  1503.         }
  1504.         if (i == 0) {
  1505.             /* exactly half-way between */
  1506.             if (dsign) {
  1507.                 if ((word0(rv) & Bndry_mask1) == Bndry_mask1
  1508.                     &&  word1(rv) == 0xffffffff) {
  1509.                     /*boundary case -- increment exponent*/
  1510.                     word0(rv) = (word0(rv) & Exp_mask)
  1511.                         + Exp_msk1
  1512. #ifdef IBM
  1513.                         | Exp_msk1 >> 4
  1514. #endif
  1515.                         ;
  1516.                     word1(rv) = 0;
  1517.                     break;
  1518.                 }
  1519.             }
  1520.             else if (!(word0(rv) & Bndry_mask) && !word1(rv)) {
  1521.             drop_down:
  1522.                 /* boundary case -- decrement exponent */
  1523. #ifdef Sudden_Underflow
  1524.                 L = word0(rv) & Exp_mask;
  1525. #ifdef IBM
  1526.                 if (L <  Exp_msk1)
  1527. #else
  1528.                     if (L <= Exp_msk1)
  1529. #endif
  1530.                         goto undfl;
  1531.                 L -= Exp_msk1;
  1532. #else
  1533.                 L = (long)((word0(rv) & Exp_mask) - Exp_msk1);
  1534. #endif
  1535.                 word0(rv) = L | Bndry_mask1;
  1536.                 word1(rv) = 0xffffffff;
  1537. #ifdef IBM
  1538.                 goto cont;
  1539. #else
  1540.                 break;
  1541. #endif
  1542.             }
  1543. #ifndef ROUND_BIASED
  1544.             if (!(word1(rv) & LSB))
  1545.                 break;
  1546. #endif
  1547.             if (dsign)
  1548.                 rv += ulp(rv);
  1549. #ifndef ROUND_BIASED
  1550.             else {
  1551.                 rv -= ulp(rv);
  1552. #ifndef Sudden_Underflow
  1553.                 if (!rv)
  1554.                     goto undfl;
  1555. #endif
  1556.             }
  1557. #endif
  1558.             break;
  1559.         }
  1560.         if ((aadj = ratio(delta, bs)) <= 2.) {
  1561.             if (dsign)
  1562.                 aadj = aadj1 = 1.;
  1563.             else if (word1(rv) || word0(rv) & Bndry_mask) {
  1564. #ifndef Sudden_Underflow
  1565.                 if (word1(rv) == Tiny1 && !word0(rv))
  1566.                     goto undfl;
  1567. #endif
  1568.                 aadj = 1.;
  1569.                 aadj1 = -1.;
  1570.             }
  1571.             else {
  1572.                 /* special case -- power of FLT_RADIX to be */
  1573.                 /* rounded down... */
  1574.  
  1575.                 if (aadj < 2./FLT_RADIX)
  1576.                     aadj = 1./FLT_RADIX;
  1577.                 else
  1578.                     aadj *= 0.5;
  1579.                 aadj1 = -aadj;
  1580.             }
  1581.         }
  1582.         else {
  1583.             aadj *= 0.5;
  1584.             aadj1 = dsign ? aadj : -aadj;
  1585. #ifdef Check_FLT_ROUNDS
  1586.             switch(FLT_ROUNDS) {
  1587.             case 2: /* towards +infinity */
  1588.                 aadj1 -= 0.5;
  1589.                 break;
  1590.             case 0: /* towards 0 */
  1591.             case 3: /* towards -infinity */
  1592.                 aadj1 += 0.5;
  1593.             }
  1594. #else
  1595.             if (FLT_ROUNDS == 0)
  1596.                 aadj1 += 0.5;
  1597. #endif
  1598.         }
  1599.         y = word0(rv) & Exp_mask;
  1600.  
  1601.         /* Check for overflow */
  1602.  
  1603.         if (y == Exp_msk1*(DBL_MAX_EXP+Bias-1)) {
  1604.             rv0 = rv;
  1605.             word0(rv) -= P*Exp_msk1;
  1606.             adj = aadj1 * ulp(rv);
  1607.             rv += adj;
  1608.             if ((word0(rv) & Exp_mask) >=
  1609.                 Exp_msk1*(DBL_MAX_EXP+Bias-P)) {
  1610.                 if (word0(rv0) == Big0 && word1(rv0) == Big1)
  1611.                     goto ovfl;
  1612.                 word0(rv) = Big0;
  1613.                 word1(rv) = Big1;
  1614.                 goto cont;
  1615.             }
  1616.             else
  1617.                 word0(rv) += P*Exp_msk1;
  1618.         }
  1619.         else {
  1620. #ifdef Sudden_Underflow
  1621.             if ((word0(rv) & Exp_mask) <= P*Exp_msk1) {
  1622.                 rv0 = rv;
  1623.                 word0(rv) += P*Exp_msk1;
  1624.                 adj = aadj1 * ulp(rv);
  1625.                 rv += adj;
  1626. #ifdef IBM
  1627.                 if ((word0(rv) & Exp_mask) <  P*Exp_msk1)
  1628. #else
  1629.                     if ((word0(rv) & Exp_mask) <= P*Exp_msk1)
  1630. #endif
  1631.                         {
  1632.                             if (word0(rv0) == Tiny0
  1633.                                 && word1(rv0) == Tiny1)
  1634.                                 goto undfl;
  1635.                             word0(rv) = Tiny0;
  1636.                             word1(rv) = Tiny1;
  1637.                             goto cont;
  1638.                         }
  1639.                     else
  1640.                         word0(rv) -= P*Exp_msk1;
  1641.             }
  1642.             else {
  1643.                 adj = aadj1 * ulp(rv);
  1644.                 rv += adj;
  1645.             }
  1646. #else
  1647.             /* Compute adj so that the IEEE rounding rules will
  1648.              * correctly round rv + adj in some half-way cases.
  1649.              * If rv * ulp(rv) is denormalized (i.e.,
  1650.              * y <= (P-1)*Exp_msk1), we must adjust aadj to avoid
  1651.              * trouble from bits lost to denormalization;
  1652.              * example: 1.2e-307 .
  1653.              */
  1654.             if (y <= (P-1)*Exp_msk1 && aadj >= 1.) {
  1655.                 aadj1 = (double)(PRInt32)(aadj + 0.5);
  1656.                 if (!dsign)
  1657.                     aadj1 = -aadj1;
  1658.             }
  1659.             adj = aadj1 * ulp(rv);
  1660.             rv += adj;
  1661. #endif
  1662.         }
  1663.         z = word0(rv) & Exp_mask;
  1664.         if (y == z) {
  1665.             /* Can we stop now? */
  1666.             L = (Long) aadj;
  1667.             aadj -= L;
  1668.             /* The tolerances below are conservative. */
  1669.             if (dsign || word1(rv) || word0(rv) & Bndry_mask) {
  1670.                 if (aadj < .4999999 || aadj > .5000001)
  1671.                     break;
  1672.             }
  1673.             else if (aadj < .4999999/FLT_RADIX)
  1674.                 break;
  1675.         }
  1676.     cont:
  1677.         Bfree(bb);
  1678.         Bfree(bd);
  1679.         Bfree(bs);
  1680.         Bfree(delta);
  1681.     }
  1682. retfree:
  1683.     Bfree(bb);
  1684.     Bfree(bd);
  1685.     Bfree(bs);
  1686.     Bfree(bd0);
  1687.     Bfree(delta);
  1688. ret:
  1689.     if (se)
  1690.         *se = (char *)s;
  1691.     return sign ? -rv : rv;
  1692. }
  1693.  
  1694. static PRInt32
  1695. quorem(Bigint *b, Bigint *S)
  1696. {
  1697.     PRInt32 n;
  1698.     Long borrow, y;
  1699.     unsigned Long carry, q, ys;
  1700.     unsigned Long *bx, *bxe, *sx, *sxe;
  1701. #ifdef Pack_32
  1702.     Long z;
  1703.     unsigned Long si, zs;
  1704. #endif
  1705.  
  1706.     n = S->wds;
  1707. #ifdef DEBUG_DTOA
  1708.     /*debug*/ if (b->wds > n)
  1709.         /*debug*/    Bug("oversize b in quorem");
  1710. #endif
  1711.     if (b->wds < n)
  1712.         return 0;
  1713.     sx = S->x;
  1714.     sxe = sx + --n;
  1715.     bx = b->x;
  1716.     bxe = bx + n;
  1717.     q = *bxe / (*sxe + 1);    /* ensure q <= true quotient */
  1718. #ifdef DEBUG_DTOA
  1719.     /*debug*/ if (q > 9)
  1720.         /*debug*/    Bug("oversized quotient in quorem");
  1721. #endif
  1722.     if (q) {
  1723.         borrow = 0;
  1724.         carry = 0;
  1725.         do {
  1726. #ifdef Pack_32
  1727.             si = *sx++;
  1728.             ys = (si & 0xffff) * q + carry;
  1729.             zs = (si >> 16) * q + (ys >> 16);
  1730.             carry = zs >> 16;
  1731.             y = (long)((*bx & 0xffff) - (ys & 0xffff) + borrow);
  1732.             borrow = y >> 16;
  1733.             Sign_Extend(borrow, y);
  1734.             z = (long)((*bx >> 16) - (zs & 0xffff) + borrow);
  1735.             borrow = z >> 16;
  1736.             Sign_Extend(borrow, z);
  1737.             Storeinc(bx, z, y);
  1738. #else
  1739.             ys = *sx++ * q + carry;
  1740.             carry = ys >> 16;
  1741.             y = *bx - (ys & 0xffff) + borrow;
  1742.             borrow = y >> 16;
  1743.             Sign_Extend(borrow, y);
  1744.             *bx++ = y & 0xffff;
  1745. #endif
  1746.         }
  1747.         while(sx <= sxe);
  1748.         if (!*bxe) {
  1749.             bx = b->x;
  1750.             while(--bxe > bx && !*bxe)
  1751.                 --n;
  1752.             b->wds = n;
  1753.         }
  1754.     }
  1755.     if (cmp(b, S) >= 0) {
  1756.         q++;
  1757.         borrow = 0;
  1758.         carry = 0;
  1759.         bx = b->x;
  1760.         sx = S->x;
  1761.         do {
  1762. #ifdef Pack_32
  1763.             si = *sx++;
  1764.             ys = (si & 0xffff) + carry;
  1765.             zs = (si >> 16) + (ys >> 16);
  1766.             carry = zs >> 16;
  1767.             y = (long)((*bx & 0xffff) - (ys & 0xffff) + borrow);
  1768.             borrow = y >> 16;
  1769.             Sign_Extend(borrow, y);
  1770.             z = (long)((*bx >> 16) - (zs & 0xffff) + borrow);
  1771.             borrow = z >> 16;
  1772.             Sign_Extend(borrow, z);
  1773.             Storeinc(bx, z, y);
  1774. #else
  1775.             ys = *sx++ + carry;
  1776.             carry = ys >> 16;
  1777.             y = *bx - (ys & 0xffff) + borrow;
  1778.             borrow = y >> 16;
  1779.             Sign_Extend(borrow, y);
  1780.             *bx++ = y & 0xffff;
  1781. #endif
  1782.         }
  1783.         while(sx <= sxe);
  1784.         bx = b->x;
  1785.         bxe = bx + n;
  1786.         if (!*bxe) {
  1787.             while(--bxe > bx && !*bxe)
  1788.                 --n;
  1789.             b->wds = n;
  1790.         }
  1791.     }
  1792.     return (int)q;
  1793. }
  1794.  
  1795. /* dtoa for IEEE arithmetic (dmg): convert double to ASCII string.
  1796.  *
  1797.  * Inspired by "How to Print Floating-Point Numbers Accurately" by
  1798.  * Guy L. Steele, Jr. and Jon L. White [Proc. ACM SIGPLAN '90, pp. 92-101].
  1799.  *
  1800.  * Modifications:
  1801.  *    1. Rather than iterating, we use a simple numeric overestimate
  1802.  *       to determine k = floor(log10(d)).  We scale relevant
  1803.  *       quantities using O(log2(k)) rather than O(k) multiplications.
  1804.  *    2. For some modes > 2 (corresponding to ecvt and fcvt), we don't
  1805.  *       try to generate digits strictly left to right.  Instead, we
  1806.  *       compute with fewer bits and propagate the carry if necessary
  1807.  *       when rounding the final digit up.  This is often faster.
  1808.  *    3. Under the assumption that input will be rounded nearest,
  1809.  *       mode 0 renders 1e23 as 1e23 rather than 9.999999999999999e22.
  1810.  *       That is, we allow equality in stopping tests when the
  1811.  *       round-nearest rule will give the same floating-point value
  1812.  *       as would satisfaction of the stopping test with strict
  1813.  *       inequality.
  1814.  *    4. We remove common factors of powers of 2 from relevant
  1815.  *       quantities.
  1816.  *    5. When converting floating-point integers less than 1e16,
  1817.  *       we use floating-point arithmetic rather than resorting
  1818.  *       to multiple-precision integers.
  1819.  *    6. When asked to produce fewer than 15 digits, we first try
  1820.  *       to get by with floating-point arithmetic; we resort to
  1821.  *       multiple-precision integer arithmetic only if we cannot
  1822.  *       guarantee that the floating-point calculation has given
  1823.  *       the correctly rounded result.  For k requested digits and
  1824.  *       "uniformly" distributed input, the probability is
  1825.  *       something like 10^(k-15) that we must resort to the Long
  1826.  *       calculation.
  1827.  */
  1828.  
  1829. PR_IMPLEMENT(char *)
  1830. PR_dtoa(double d, int mode, int ndigits, int *decpt, int *sign, char **rve)
  1831. {
  1832.     /*    Arguments ndigits, decpt, sign are similar to those
  1833.         of ecvt and fcvt; trailing zeros are suppressed from
  1834.         the returned string.  If not null, *rve is set to point
  1835.         to the end of the return value.  If d is +-Infinity or NaN,
  1836.         then *decpt is set to 9999.
  1837.  
  1838.         mode:
  1839.         0 ==> shortest string that yields d when read in
  1840.         and rounded to nearest.
  1841.         1 ==> like 0, but with Steele & White stopping rule;
  1842.         e.g. with IEEE P754 arithmetic , mode 0 gives
  1843.         1e23 whereas mode 1 gives 9.999999999999999e22.
  1844.         2 ==> max(1,ndigits) significant digits.  This gives a
  1845.         return value similar to that of ecvt, except
  1846.         that trailing zeros are suppressed.
  1847.         3 ==> through ndigits past the decimal point.  This
  1848.         gives a return value similar to that from fcvt,
  1849.         except that trailing zeros are suppressed, and
  1850.         ndigits can be negative.
  1851.         4-9 should give the same return values as 2-3, i.e.,
  1852.         4 <= mode <= 9 ==> same return as mode
  1853.         2 + (mode & 1).  These modes are mainly for
  1854.         debugging; often they run slower but sometimes
  1855.         faster than modes 2-3.
  1856.         4,5,8,9 ==> left-to-right digit generation.
  1857.         6-9 ==> don't try fast floating-point estimate
  1858.         (if applicable).
  1859.  
  1860.         Values of mode other than 0-9 are treated as mode 0.
  1861.  
  1862.         Sufficient space is allocated to the return value
  1863.         to hold the suppressed trailing zeros.
  1864.     */
  1865.  
  1866.     PRInt32 bbits, b2, b5, be, dig, i, ieps, ilim, ilim0, ilim1,
  1867.         j, j1, k, k0, k_check, leftright, m2, m5, s2, s5,
  1868.         spec_case, try_quick;
  1869.     Long L;
  1870. #ifndef Sudden_Underflow
  1871.     PRInt32 denorm;
  1872.     unsigned Long x;
  1873. #endif
  1874.     Bigint *b, *b1, *delta, *mlo, *mhi, *S;
  1875.     double d2, ds, eps;
  1876.     char *s, *s0;
  1877.     static Bigint *result;
  1878.     static PRInt32 result_k;
  1879.  
  1880.     if (!_pr_initialized) _PR_ImplicitInitialization();
  1881.  
  1882.     if (result) {
  1883.         result->k = result_k;
  1884.         result->maxwds = 1 << result_k;
  1885.         Bfree(result);
  1886.         result = 0;
  1887.     }
  1888.  
  1889.     if (word0(d) & Sign_bit) {
  1890.         /* set sign for everything, including 0's and NaNs */
  1891.         *sign = 1;
  1892.         word0(d) &= ~Sign_bit;    /* clear sign bit */
  1893.     }
  1894.     else
  1895.         *sign = 0;
  1896.  
  1897. #if defined(IEEE_Arith) + defined(VAX)
  1898. #ifdef IEEE_Arith
  1899.     if ((word0(d) & Exp_mask) == Exp_mask)
  1900. #else
  1901.         if (word0(d)  == 0x8000)
  1902. #endif
  1903.             {
  1904.                 /* Infinity or NaN */
  1905.                 *decpt = 9999;
  1906.                 s =
  1907. #ifdef IEEE_Arith
  1908.                     !word1(d) && !(word0(d) & 0xfffff) ? "Infinity" :
  1909. #endif
  1910.                     "NaN";
  1911.                 if (rve)
  1912.                     *rve =
  1913. #ifdef IEEE_Arith
  1914.                         s[3] ? s + 8 :
  1915. #endif
  1916.                     s + 3;
  1917.                 return s;
  1918.             }
  1919. #endif
  1920. #ifdef IBM
  1921.     d += 0; /* normalize */
  1922. #endif
  1923.     if (!d) {
  1924.         *decpt = 1;
  1925.         s = "0";
  1926.         if (rve)
  1927.             *rve = s + 1;
  1928.         return s;
  1929.     }
  1930.  
  1931.     b = d2b(d, &be, &bbits);
  1932. #ifdef Sudden_Underflow
  1933.     i = (PRInt32)(word0(d) >> Exp_shift1 & (Exp_mask>>Exp_shift1));
  1934. #else
  1935.     if ((i = (PRInt32)(word0(d) >> Exp_shift1 & (Exp_mask>>Exp_shift1))) != 0) {
  1936. #endif
  1937.         d2 = d;
  1938.         word0(d2) &= Frac_mask1;
  1939.         word0(d2) |= Exp_11;
  1940. #ifdef IBM
  1941.         if (j = 11 - hi0bits(word0(d2) & Frac_mask))
  1942.             d2 /= 1 << j;
  1943. #endif
  1944.  
  1945.         /* log(x)    ~=~ log(1.5) + (x-1.5)/1.5
  1946.          * log10(x)     =  log(x) / log(10)
  1947.          *        ~=~ log(1.5)/log(10) + (x-1.5)/(1.5*log(10))
  1948.          * log10(d) = (i-Bias)*log(2)/log(10) + log10(d2)
  1949.          *
  1950.          * This suggests computing an approximation k to log10(d) by
  1951.          *
  1952.          * k = (i - Bias)*0.301029995663981
  1953.          *    + ( (d2-1.5)*0.289529654602168 + 0.176091259055681 );
  1954.          *
  1955.          * We want k to be too large rather than too small.
  1956.          * The error in the first-order Taylor series approximation
  1957.          * is in our favor, so we just round up the constant enough
  1958.          * to compensate for any error in the multiplication of
  1959.          * (i - Bias) by 0.301029995663981; since |i - Bias| <= 1077,
  1960.          * and 1077 * 0.30103 * 2^-52 ~=~ 7.2e-14,
  1961.          * adding 1e-13 to the constant term more than suffices.
  1962.          * Hence we adjust the constant term to 0.1760912590558.
  1963.          * (We could get a more accurate k by invoking log10,
  1964.          *  but this is probably not worthwhile.)
  1965.          */
  1966.  
  1967.         i -= Bias;
  1968. #ifdef IBM
  1969.         i <<= 2;
  1970.         i += j;
  1971. #endif
  1972. #ifndef Sudden_Underflow
  1973.         denorm = 0;
  1974.     }
  1975.     else {
  1976.         /* d is denormalized */
  1977.  
  1978.         i = bbits + be + (Bias + (P-1) - 1);
  1979.         x = i > 32  ? word0(d) << (64 - i) | word1(d) >> (i - 32)
  1980.             : word1(d) << (32 - i);
  1981.         d2 = x;
  1982.         word0(d2) -= 31*Exp_msk1; /* adjust exponent */
  1983.         i -= (Bias + (P-1) - 1) + 1;
  1984.         denorm = 1;
  1985.     }
  1986. #endif
  1987.     ds = (d2-1.5)*0.289529654602168 + 0.1760912590558 + i*0.301029995663981;
  1988.     k = (PRInt32)ds;
  1989.     if (ds < 0. && ds != k)
  1990.         k--;    /* want k = floor(ds) */
  1991.     k_check = 1;
  1992.     if (k >= 0 && k <= Ten_pmax) {
  1993.         if (d < tens[k])
  1994.             k--;
  1995.         k_check = 0;
  1996.     }
  1997.     j = bbits - i - 1;
  1998.     if (j >= 0) {
  1999.         b2 = 0;
  2000.         s2 = j;
  2001.     }
  2002.     else {
  2003.         b2 = -j;
  2004.         s2 = 0;
  2005.     }
  2006.     if (k >= 0) {
  2007.         b5 = 0;
  2008.         s5 = k;
  2009.         s2 += k;
  2010.     }
  2011.     else {
  2012.         b2 -= k;
  2013.         b5 = -k;
  2014.         s5 = 0;
  2015.     }
  2016.     if (mode < 0 || mode > 9)
  2017.         mode = 0;
  2018.     try_quick = 1;
  2019.     if (mode > 5) {
  2020.         mode -= 4;
  2021.         try_quick = 0;
  2022.     }
  2023.     leftright = 1;
  2024.     switch(mode) {
  2025.     case 0:
  2026.     case 1:
  2027.         ilim = ilim1 = -1;
  2028.         i = 18;
  2029.         ndigits = 0;
  2030.         break;
  2031.     case 2:
  2032.         leftright = 0;
  2033.         /* no break */
  2034.     case 4:
  2035.         if (ndigits <= 0)
  2036.             ndigits = 1;
  2037.         ilim = ilim1 = i = ndigits;
  2038.         break;
  2039.     case 3:
  2040.         leftright = 0;
  2041.         /* no break */
  2042.     case 5:
  2043.         i = ndigits + k + 1;
  2044.         ilim = i;
  2045.         ilim1 = i - 1;
  2046.         if (i <= 0)
  2047.             i = 1;
  2048.     }
  2049.     j = sizeof(unsigned Long);
  2050.     for(result_k = 0; sizeof(Bigint) - sizeof(unsigned Long) <= i - j;
  2051.         j <<= 1) result_k++;
  2052.     result = Balloc(result_k);
  2053.     s = s0 = (char *)result;
  2054.  
  2055.     if (ilim >= 0 && ilim <= Quick_max && try_quick) {
  2056.  
  2057.         /* Try to get by with floating-point arithmetic. */
  2058.  
  2059.         i = 0;
  2060.         d2 = d;
  2061.         k0 = k;
  2062.         ilim0 = ilim;
  2063.         ieps = 2; /* conservative */
  2064.         if (k > 0) {
  2065.             ds = tens[k&0xf];
  2066.             j = k >> 4;
  2067.             if (j & Bletch) {
  2068.                 /* prevent overflows */
  2069.                 j &= Bletch - 1;
  2070.                 d /= bigtens[n_bigtens-1];
  2071.                 ieps++;
  2072.             }
  2073.             for(; j; j >>= 1, i++)
  2074.                 if (j & 1) {
  2075.                     ieps++;
  2076.                     ds *= bigtens[i];
  2077.                 }
  2078.             d /= ds;
  2079.         }
  2080.         else if ((j1 = -k) != 0) {
  2081.             d *= tens[j1 & 0xf];
  2082.             for(j = j1 >> 4; j; j >>= 1, i++)
  2083.                 if (j & 1) {
  2084.                     ieps++;
  2085.                     d *= bigtens[i];
  2086.                 }
  2087.         }
  2088.         if (k_check && d < 1. && ilim > 0) {
  2089.             if (ilim1 <= 0)
  2090.                 goto fast_failed;
  2091.             ilim = ilim1;
  2092.             k--;
  2093.             d *= 10.;
  2094.             ieps++;
  2095.         }
  2096.         eps = ieps*d + 7.;
  2097.         word0(eps) -= (P-1)*Exp_msk1;
  2098.         if (ilim == 0) {
  2099.             S = mhi = 0;
  2100.             d -= 5.;
  2101.             if (d > eps)
  2102.                 goto one_digit;
  2103.             if (d < -eps)
  2104.                 goto no_digits;
  2105.             goto fast_failed;
  2106.         }
  2107. #ifndef No_leftright
  2108.         if (leftright) {
  2109.             /* Use Steele & White method of only
  2110.              * generating digits needed.
  2111.              */
  2112.             eps = 0.5/tens[ilim-1] - eps;
  2113.             for(i = 0;;) {
  2114.                 L = (Long) d;
  2115.                 d -= L;
  2116.                 *s++ = '0' + (PRInt32)L;
  2117.                 if (d < eps)
  2118.                     goto ret1;
  2119.                 if (1. - d < eps)
  2120.                     goto bump_up;
  2121.                 if (++i >= ilim)
  2122.                     break;
  2123.                 eps *= 10.;
  2124.                 d *= 10.;
  2125.             }
  2126.         }
  2127.         else {
  2128. #endif
  2129.             /* Generate ilim digits, then fix them up. */
  2130.             eps *= tens[ilim-1];
  2131.             for(i = 1;; i++, d *= 10.) {
  2132.                 L = (Long) d;
  2133.                 d -= L;
  2134.                 *s++ = '0' + (PRInt32)L;
  2135.                 if (i == ilim) {
  2136.                     if (d > 0.5 + eps)
  2137.                         goto bump_up;
  2138.                     else if (d < 0.5 - eps) {
  2139.                         while(*--s == '0'){} /* just count -- nothing to execute */
  2140.                         s++;
  2141.                         goto ret1;
  2142.                     }
  2143.                     break;
  2144.                 }
  2145.             }
  2146. #ifndef No_leftright
  2147.         }
  2148. #endif
  2149.     fast_failed:
  2150.         s = s0;
  2151.         d = d2;
  2152.         k = k0;
  2153.         ilim = ilim0;
  2154.     }
  2155.  
  2156.     /* Do we have a "small" integer? */
  2157.  
  2158.     if (be >= 0 && k <= Int_max) {
  2159.         /* Yes. */
  2160.         ds = tens[k];
  2161.         if (ndigits < 0 && ilim <= 0) {
  2162.             S = mhi = 0;
  2163.             if (ilim < 0 || d <= 5*ds)
  2164.                 goto no_digits;
  2165.             goto one_digit;
  2166.         }
  2167.         for(i = 1;; i++) {
  2168.             L = (Long) (d / ds);
  2169.             d -= L*ds;
  2170. #ifdef Check_FLT_ROUNDS
  2171.             /* If FLT_ROUNDS == 2, L will usually be high by 1 */
  2172.             if (d < 0) {
  2173.                 L--;
  2174.                 d += ds;
  2175.             }
  2176. #endif
  2177.             *s++ = '0' + (PRInt32)L;
  2178.             if (i == ilim) {
  2179.                 d += d;
  2180.                 if ((d > ds) || (d == ds && L & 1)) {
  2181.                 bump_up:
  2182.                     while(*--s == '9')
  2183.                         if (s == s0) {
  2184.                             k++;
  2185.                             *s = '0';
  2186.                             break;
  2187.                         }
  2188.                     ++*s++;
  2189.                 }
  2190.                 break;
  2191.             }
  2192.             if (!(d *= 10.))
  2193.                 break;
  2194.         }
  2195.         goto ret1;
  2196.     }
  2197.  
  2198.     m2 = b2;
  2199.     m5 = b5;
  2200.     mhi = mlo = 0;
  2201.     if (leftright) {
  2202.         if (mode < 2) {
  2203.             i =
  2204. #ifndef Sudden_Underflow
  2205.                 denorm ? be + (Bias + (P-1) - 1 + 1) :
  2206. #endif
  2207. #ifdef IBM
  2208.                 1 + 4*P - 3 - bbits + ((bbits + be - 1) & 3);
  2209. #else
  2210.             1 + P - bbits;
  2211. #endif
  2212.         }
  2213.         else {
  2214.             j = ilim - 1;
  2215.             if (m5 >= j)
  2216.                 m5 -= j;
  2217.             else {
  2218.                 s5 += j -= m5;
  2219.                 b5 += j;
  2220.                 m5 = 0;
  2221.             }
  2222.             if ((i = ilim) < 0) {
  2223.                 m2 -= i;
  2224.                 i = 0;
  2225.             }
  2226.         }
  2227.         b2 += i;
  2228.         s2 += i;
  2229.         mhi = i2b(1);
  2230.     }
  2231.     if (m2 > 0 && s2 > 0) {
  2232.         i = m2 < s2 ? m2 : s2;
  2233.         b2 -= i;
  2234.         m2 -= i;
  2235.         s2 -= i;
  2236.     }
  2237.     if (b5 > 0) {
  2238.         if (leftright) {
  2239.             if (m5 > 0) {
  2240.                 mhi = pow5mult(mhi, m5);
  2241.                 b1 = mult(mhi, b);
  2242.                 Bfree(b);
  2243.                 b = b1;
  2244.             }
  2245.             if ((j = b5 - m5) != 0)
  2246.                 b = pow5mult(b, j);
  2247.         }
  2248.         else
  2249.             b = pow5mult(b, b5);
  2250.     }
  2251.     S = i2b(1);
  2252.     if (s5 > 0)
  2253.         S = pow5mult(S, s5);
  2254.  
  2255.     /* Check for special case that d is a normalized power of 2. */
  2256.  
  2257.     if (mode < 2) {
  2258.         if (!word1(d) && !(word0(d) & Bndry_mask)
  2259. #ifndef Sudden_Underflow
  2260.             && word0(d) & Exp_mask
  2261. #endif
  2262.             ) {
  2263.             /* The special case */
  2264.             b2 += Log2P;
  2265.             s2 += Log2P;
  2266.             spec_case = 1;
  2267.         }
  2268.         else
  2269.             spec_case = 0;
  2270.     }
  2271.  
  2272.     /* Arrange for convenient computation of quotients:
  2273.      * shift left if necessary so divisor has 4 leading 0 bits.
  2274.      *
  2275.      * Perhaps we should just compute leading 28 bits of S once
  2276.      * and for all and pass them and a shift to quorem, so it
  2277.      * can do shifts and ors to compute the numerator for q.
  2278.      */
  2279. #ifdef Pack_32
  2280.     if ((i = ((s5 ? 32 - hi0bits(S->x[S->wds-1]) : 1) + s2) & 0x1f) != 0)
  2281.         i = 32 - i;
  2282. #else
  2283.     if (i = ((s5 ? 32 - hi0bits(S->x[S->wds-1]) : 1) + s2) & 0xf)
  2284.         i = 16 - i;
  2285. #endif
  2286.     if (i > 4) {
  2287.         i -= 4;
  2288.         b2 += i;
  2289.         m2 += i;
  2290.         s2 += i;
  2291.     }
  2292.     else if (i < 4) {
  2293.         i += 28;
  2294.         b2 += i;
  2295.         m2 += i;
  2296.         s2 += i;
  2297.     }
  2298.     if (b2 > 0)
  2299.         b = lshift(b, b2);
  2300.     if (s2 > 0)
  2301.         S = lshift(S, s2);
  2302.     if (k_check) {
  2303.         if (cmp(b,S) < 0) {
  2304.             k--;
  2305.             b = multadd(b, 10, 0);    /* we botched the k estimate */
  2306.             if (leftright)
  2307.                 mhi = multadd(mhi, 10, 0);
  2308.             ilim = ilim1;
  2309.         }
  2310.     }
  2311.     if (ilim <= 0 && mode > 2) {
  2312.         if (ilim < 0 || cmp(b,S = multadd(S,5,0)) <= 0) {
  2313.             /* no digits, fcvt style */
  2314.         no_digits:
  2315.             k = -1 - ndigits;
  2316.             goto ret;
  2317.         }
  2318.     one_digit:
  2319.         *s++ = '1';
  2320.         k++;
  2321.         goto ret;
  2322.     }
  2323.     if (leftright) {
  2324.         if (m2 > 0)
  2325.             mhi = lshift(mhi, m2);
  2326.  
  2327.         /* Compute mlo -- check for special case
  2328.          * that d is a normalized power of 2.
  2329.          */
  2330.  
  2331.         mlo = mhi;
  2332.         if (spec_case) {
  2333.             mhi = Balloc(mhi->k);
  2334.             Bcopy(mhi, mlo);
  2335.             mhi = lshift(mhi, Log2P);
  2336.         }
  2337.  
  2338.         for(i = 1;;i++) {
  2339.             dig = quorem(b,S) + '0';
  2340.             /* Do we yet have the shortest decimal string
  2341.              * that will round to d?
  2342.              */
  2343.             j = cmp(b, mlo);
  2344.             delta = diff(S, mhi);
  2345.             j1 = delta->sign ? 1 : cmp(b, delta);
  2346.             Bfree(delta);
  2347. #ifndef ROUND_BIASED
  2348.             if (j1 == 0 && !mode && !(word1(d) & 1)) {
  2349.                 if (dig == '9')
  2350.                     goto round_9_up;
  2351.                 if (j > 0)
  2352.                     dig++;
  2353.                 *s++ = dig;
  2354.                 goto ret;
  2355.             }
  2356. #endif
  2357.             if ((j < 0) || ((j == 0) && (!mode)
  2358. #ifndef ROUND_BIASED
  2359.                 && (!(word1(d) & 1)))
  2360. #endif
  2361.                 ) {
  2362.                 if (j1 > 0) {
  2363.                     b = lshift(b, 1);
  2364.                     j1 = cmp(b, S);
  2365.                     if (((j1 > 0) || (j1 == 0 && dig & 1))
  2366.                         && (dig++ == '9'))
  2367.                         goto round_9_up;
  2368.                 }
  2369.                 *s++ = dig;
  2370.                 goto ret;
  2371.             }
  2372.             if (j1 > 0) {
  2373.                 if (dig == '9') { /* possible if i == 1 */
  2374.                 round_9_up:
  2375.                     *s++ = '9';
  2376.                     goto roundoff;
  2377.                 }
  2378.                 *s++ = dig + 1;
  2379.                 goto ret;
  2380.             }
  2381.             *s++ = dig;
  2382.             if (i == ilim)
  2383.                 break;
  2384.             b = multadd(b, 10, 0);
  2385.             if (mlo == mhi)
  2386.                 mlo = mhi = multadd(mhi, 10, 0);
  2387.             else {
  2388.                 mlo = multadd(mlo, 10, 0);
  2389.                 mhi = multadd(mhi, 10, 0);
  2390.             }
  2391.         }
  2392.     }
  2393.     else
  2394.         for(i = 1;; i++) {
  2395.             *s++ = dig = quorem(b,S) + '0';
  2396.             if (i >= ilim)
  2397.                 break;
  2398.             b = multadd(b, 10, 0);
  2399.         }
  2400.  
  2401.     /* Round off last digit */
  2402.  
  2403.     b = lshift(b, 1);
  2404.     j = cmp(b, S);
  2405.     if ((j > 0) || (j == 0 && dig & 1)) {
  2406.     roundoff:
  2407.         while(*--s == '9')
  2408.             if (s == s0) {
  2409.                 k++;
  2410.                 *s++ = '1';
  2411.                 goto ret;
  2412.             }
  2413.         ++*s++;
  2414.     }
  2415.     else {
  2416.         while(*--s == '0'){} /* just count -- nothing to execute */
  2417.         s++;
  2418.     }
  2419. ret:
  2420.     Bfree(S);
  2421.     if (mhi) {
  2422.         if (mlo && mlo != mhi)
  2423.             Bfree(mlo);
  2424.         Bfree(mhi);
  2425.     }
  2426. ret1:
  2427.     Bfree(b);
  2428.     *s = 0;
  2429.     *decpt = k + 1;
  2430.     if (rve)
  2431.         *rve = s;
  2432.     return s0;
  2433. }
  2434.  
  2435. /* dtoa for IEEE arithmetic (dmg): convert double to ASCII string.
  2436.  *
  2437.  * Inspired by "How to Print Floating-Point Numbers Accurately" by
  2438.  * Guy L. Steele, Jr. and Jon L. White [Proc. ACM SIGPLAN '90, pp. 92-101].
  2439.  *
  2440.  * Modifications:
  2441.  *    1. Rather than iterating, we use a simple numeric overestimate
  2442.  *       to determine k = floor(log10(d)).  We scale relevant
  2443.  *       quantities using O(log2(k)) rather than O(k) multiplications.
  2444.  *    2. For some modes > 2 (corresponding to ecvt and fcvt), we don't
  2445.  *       try to generate digits strictly left to right.  Instead, we
  2446.  *       compute with fewer bits and propagate the carry if necessary
  2447.  *       when rounding the final digit up.  This is often faster.
  2448.  *    3. Under the assumption that input will be rounded nearest,
  2449.  *       mode 0 renders 1e23 as 1e23 rather than 9.999999999999999e22.
  2450.  *       That is, we allow equality in stopping tests when the
  2451.  *       round-nearest rule will give the same floating-point value
  2452.  *       as would satisfaction of the stopping test with strict
  2453.  *       inequality.
  2454.  *    4. We remove common factors of powers of 2 from relevant
  2455.  *       quantities.
  2456.  *    5. When converting floating-point integers less than 1e16,
  2457.  *       we use floating-point arithmetic rather than resorting
  2458.  *       to multiple-precision integers.
  2459.  *    6. When asked to produce fewer than 15 digits, we first try
  2460.  *       to get by with floating-point arithmetic; we resort to
  2461.  *       multiple-precision integer arithmetic only if we cannot
  2462.  *       guarantee that the floating-point calculation has given
  2463.  *       the correctly rounded result.  For k requested digits and
  2464.  *       "uniformly" distributed input, the probability is
  2465.  *       something like 10^(k-15) that we must resort to the Long
  2466.  *       calculation.
  2467.  */
  2468.  
  2469. PR_IMPLEMENT(PRStatus)
  2470. PR_dtoa_r(double d, int mode, int ndigits,
  2471.     int *decpt, int *sign, char **rve, char *buf, PRSize bufsize)
  2472. {
  2473.     /*    Arguments ndigits, decpt, sign are similar to those
  2474.         of ecvt and fcvt; trailing zeros are suppressed from
  2475.         the returned string.  If not null, *rve is set to point
  2476.         to the end of the return value.  If d is +-Infinity or NaN,
  2477.         then *decpt is set to 9999.
  2478.  
  2479.         mode:
  2480.         0 ==> shortest string that yields d when read in
  2481.         and rounded to nearest.
  2482.         1 ==> like 0, but with Steele & White stopping rule;
  2483.         e.g. with IEEE P754 arithmetic , mode 0 gives
  2484.         1e23 whereas mode 1 gives 9.999999999999999e22.
  2485.         2 ==> max(1,ndigits) significant digits.  This gives a
  2486.         return value similar to that of ecvt, except
  2487.         that trailing zeros are suppressed.
  2488.         3 ==> through ndigits past the decimal point.  This
  2489.         gives a return value similar to that from fcvt,
  2490.         except that trailing zeros are suppressed, and
  2491.         ndigits can be negative.
  2492.         4-9 should give the same return values as 2-3, i.e.,
  2493.         4 <= mode <= 9 ==> same return as mode
  2494.         2 + (mode & 1).  These modes are mainly for
  2495.         debugging; often they run slower but sometimes
  2496.         faster than modes 2-3.
  2497.         4,5,8,9 ==> left-to-right digit generation.
  2498.         6-9 ==> don't try fast floating-point estimate
  2499.         (if applicable).
  2500.  
  2501.         Values of mode other than 0-9 are treated as mode 0.
  2502.  
  2503.         Sufficient space is allocated to the return value
  2504.         to hold the suppressed trailing zeros.
  2505.     */
  2506.  
  2507.     PRInt32 bbits, b2, b5, be, dig, i, ieps, ilim, ilim0, ilim1,
  2508.         j, j1, k, k0, k_check, leftright, m2, m5, s2, s5,
  2509.         spec_case, try_quick;
  2510.     Long L;
  2511. #ifndef Sudden_Underflow
  2512.     PRInt32 denorm;
  2513.     unsigned Long x;
  2514. #endif
  2515.     Bigint *b, *b1, *delta, *mlo, *mhi, *S;
  2516.     double d2, ds, eps;
  2517.     char *s, *s0;
  2518.     Bigint *result = 0;
  2519.     PRInt32 result_k;
  2520.     PRStatus retval;
  2521.     PRSize strsize;
  2522.  
  2523.     if (!_pr_initialized) _PR_ImplicitInitialization();
  2524.  
  2525.     if (word0(d) & Sign_bit) {
  2526.         /* set sign for everything, including 0's and NaNs */
  2527.         *sign = 1;
  2528.         word0(d) &= ~Sign_bit;    /* clear sign bit */
  2529.     }
  2530.     else
  2531.         *sign = 0;
  2532.  
  2533. #if defined(IEEE_Arith) + defined(VAX)
  2534. #ifdef IEEE_Arith
  2535.     if ((word0(d) & Exp_mask) == Exp_mask)
  2536. #else
  2537.         if (word0(d)  == 0x8000)
  2538. #endif
  2539.             {
  2540.                 /* Infinity or NaN */
  2541.                 *decpt = 9999;
  2542.                 s =
  2543. #ifdef IEEE_Arith
  2544.                     !word1(d) && !(word0(d) & 0xfffff) ? "Infinity" :
  2545. #endif
  2546.                     "NaN";
  2547.                 if ((s[0] == 'I' && bufsize < 9) || (s[0] == 'N' && bufsize < 4)) {
  2548.                     PR_SetError(PR_BUFFER_OVERFLOW_ERROR, 0);
  2549.                     return PR_FAILURE;
  2550.                 }
  2551.                 strcpy(buf, s);
  2552.                 if (rve) {
  2553.                     *rve =
  2554. #ifdef IEEE_Arith
  2555.                         buf[3] ? buf + 8 :
  2556. #endif
  2557.                     buf + 3;
  2558.                     PR_ASSERT(**rve == '\0');
  2559.                 }
  2560.                 return PR_SUCCESS;
  2561.             }
  2562. #endif
  2563. #ifdef IBM
  2564.     d += 0; /* normalize */
  2565. #endif
  2566.     if (!d) {
  2567.         *decpt = 1;
  2568.         if (bufsize < 2) {
  2569.             PR_SetError(PR_BUFFER_OVERFLOW_ERROR, 0);
  2570.             return PR_FAILURE;
  2571.         }
  2572.         buf[0] = '0'; buf[1] = '\0';  /* copy "0" to buffer */
  2573.         if (rve) {
  2574.             *rve = buf + 1;
  2575.             PR_ASSERT(**rve == '\0');
  2576.         }
  2577.         return PR_SUCCESS;
  2578.     }
  2579.  
  2580.     b = d2b(d, &be, &bbits);
  2581. #ifdef Sudden_Underflow
  2582.     i = (PRInt32)(word0(d) >> Exp_shift1 & (Exp_mask>>Exp_shift1));
  2583. #else
  2584.     if ((i = (PRInt32)(word0(d) >> Exp_shift1 & (Exp_mask>>Exp_shift1))) != 0) {
  2585. #endif
  2586.         d2 = d;
  2587.         word0(d2) &= Frac_mask1;
  2588.         word0(d2) |= Exp_11;
  2589. #ifdef IBM
  2590.         if (j = 11 - hi0bits(word0(d2) & Frac_mask))
  2591.             d2 /= 1 << j;
  2592. #endif
  2593.  
  2594.         /* log(x)    ~=~ log(1.5) + (x-1.5)/1.5
  2595.          * log10(x)     =  log(x) / log(10)
  2596.          *        ~=~ log(1.5)/log(10) + (x-1.5)/(1.5*log(10))
  2597.          * log10(d) = (i-Bias)*log(2)/log(10) + log10(d2)
  2598.          *
  2599.          * This suggests computing an approximation k to log10(d) by
  2600.          *
  2601.          * k = (i - Bias)*0.301029995663981
  2602.          *    + ( (d2-1.5)*0.289529654602168 + 0.176091259055681 );
  2603.          *
  2604.          * We want k to be too large rather than too small.
  2605.          * The error in the first-order Taylor series approximation
  2606.          * is in our favor, so we just round up the constant enough
  2607.          * to compensate for any error in the multiplication of
  2608.          * (i - Bias) by 0.301029995663981; since |i - Bias| <= 1077,
  2609.          * and 1077 * 0.30103 * 2^-52 ~=~ 7.2e-14,
  2610.          * adding 1e-13 to the constant term more than suffices.
  2611.          * Hence we adjust the constant term to 0.1760912590558.
  2612.          * (We could get a more accurate k by invoking log10,
  2613.          *  but this is probably not worthwhile.)
  2614.          */
  2615.  
  2616.         i -= Bias;
  2617. #ifdef IBM
  2618.         i <<= 2;
  2619.         i += j;
  2620. #endif
  2621. #ifndef Sudden_Underflow
  2622.         denorm = 0;
  2623.     }
  2624.     else {
  2625.         /* d is denormalized */
  2626.  
  2627.         i = bbits + be + (Bias + (P-1) - 1);
  2628.         x = i > 32  ? word0(d) << (64 - i) | word1(d) >> (i - 32)
  2629.             : word1(d) << (32 - i);
  2630.         d2 = x;
  2631.         word0(d2) -= 31*Exp_msk1; /* adjust exponent */
  2632.         i -= (Bias + (P-1) - 1) + 1;
  2633.         denorm = 1;
  2634.     }
  2635. #endif
  2636.     ds = (d2-1.5)*0.289529654602168 + 0.1760912590558 + i*0.301029995663981;
  2637.     k = (PRInt32)ds;
  2638.     if (ds < 0. && ds != k)
  2639.         k--;    /* want k = floor(ds) */
  2640.     k_check = 1;
  2641.     if (k >= 0 && k <= Ten_pmax) {
  2642.         if (d < tens[k])
  2643.             k--;
  2644.         k_check = 0;
  2645.     }
  2646.     j = bbits - i - 1;
  2647.     if (j >= 0) {
  2648.         b2 = 0;
  2649.         s2 = j;
  2650.     }
  2651.     else {
  2652.         b2 = -j;
  2653.         s2 = 0;
  2654.     }
  2655.     if (k >= 0) {
  2656.         b5 = 0;
  2657.         s5 = k;
  2658.         s2 += k;
  2659.     }
  2660.     else {
  2661.         b2 -= k;
  2662.         b5 = -k;
  2663.         s5 = 0;
  2664.     }
  2665.     if (mode < 0 || mode > 9)
  2666.         mode = 0;
  2667.     try_quick = 1;
  2668.     if (mode > 5) {
  2669.         mode -= 4;
  2670.         try_quick = 0;
  2671.     }
  2672.     leftright = 1;
  2673.     switch(mode) {
  2674.     case 0:
  2675.     case 1:
  2676.         ilim = ilim1 = -1;
  2677.         i = 18;
  2678.         ndigits = 0;
  2679.         break;
  2680.     case 2:
  2681.         leftright = 0;
  2682.         /* no break */
  2683.     case 4:
  2684.         if (ndigits <= 0)
  2685.             ndigits = 1;
  2686.         ilim = ilim1 = i = ndigits;
  2687.         break;
  2688.     case 3:
  2689.         leftright = 0;
  2690.         /* no break */
  2691.     case 5:
  2692.         i = ndigits + k + 1;
  2693.         ilim = i;
  2694.         ilim1 = i - 1;
  2695.         if (i <= 0)
  2696.             i = 1;
  2697.     }
  2698.     j = sizeof(unsigned Long);
  2699.     for(result_k = 0; sizeof(Bigint) - sizeof(unsigned Long) <= i - j;
  2700.         j <<= 1) result_k++;
  2701.     result = Balloc(result_k);
  2702.     s = s0 = (char *)result;
  2703.  
  2704.     if (ilim >= 0 && ilim <= Quick_max && try_quick) {
  2705.  
  2706.         /* Try to get by with floating-point arithmetic. */
  2707.  
  2708.         i = 0;
  2709.         d2 = d;
  2710.         k0 = k;
  2711.         ilim0 = ilim;
  2712.         ieps = 2; /* conservative */
  2713.         if (k > 0) {
  2714.             ds = tens[k&0xf];
  2715.             j = k >> 4;
  2716.             if (j & Bletch) {
  2717.                 /* prevent overflows */
  2718.                 j &= Bletch - 1;
  2719.                 d /= bigtens[n_bigtens-1];
  2720.                 ieps++;
  2721.             }
  2722.             for(; j; j >>= 1, i++)
  2723.                 if (j & 1) {
  2724.                     ieps++;
  2725.                     ds *= bigtens[i];
  2726.                 }
  2727.             d /= ds;
  2728.         }
  2729.         else if ((j1 = -k) != 0) {
  2730.             d *= tens[j1 & 0xf];
  2731.             for(j = j1 >> 4; j; j >>= 1, i++)
  2732.                 if (j & 1) {
  2733.                     ieps++;
  2734.                     d *= bigtens[i];
  2735.                 }
  2736.         }
  2737.         if (k_check && d < 1. && ilim > 0) {
  2738.             if (ilim1 <= 0)
  2739.                 goto fast_failed;
  2740.             ilim = ilim1;
  2741.             k--;
  2742.             d *= 10.;
  2743.             ieps++;
  2744.         }
  2745.         eps = ieps*d + 7.;
  2746.         word0(eps) -= (P-1)*Exp_msk1;
  2747.         if (ilim == 0) {
  2748.             S = mhi = 0;
  2749.             d -= 5.;
  2750.             if (d > eps)
  2751.                 goto one_digit;
  2752.             if (d < -eps)
  2753.                 goto no_digits;
  2754.             goto fast_failed;
  2755.         }
  2756. #ifndef No_leftright
  2757.         if (leftright) {
  2758.             /* Use Steele & White method of only
  2759.              * generating digits needed.
  2760.              */
  2761.             eps = 0.5/tens[ilim-1] - eps;
  2762.             for(i = 0;;) {
  2763.                 L = (Long) d;
  2764.                 d -= L;
  2765.                 *s++ = '0' + (PRInt32)L;
  2766.                 if (d < eps)
  2767.                     goto ret1;
  2768.                 if (1. - d < eps)
  2769.                     goto bump_up;
  2770.                 if (++i >= ilim)
  2771.                     break;
  2772.                 eps *= 10.;
  2773.                 d *= 10.;
  2774.             }
  2775.         }
  2776.         else {
  2777. #endif
  2778.             /* Generate ilim digits, then fix them up. */
  2779.             eps *= tens[ilim-1];
  2780.             for(i = 1;; i++, d *= 10.) {
  2781.                 L = (Long) d;
  2782.                 d -= L;
  2783.                 *s++ = '0' + (PRInt32)L;
  2784.                 if (i == ilim) {
  2785.                     if (d > 0.5 + eps)
  2786.                         goto bump_up;
  2787.                     else if (d < 0.5 - eps) {
  2788.                         while(*--s == '0'){} /* just count -- nothing to execute */
  2789.                         s++;
  2790.                         goto ret1;
  2791.                     }
  2792.                     break;
  2793.                 }
  2794.             }
  2795. #ifndef No_leftright
  2796.         }
  2797. #endif
  2798.     fast_failed:
  2799.         s = s0;
  2800.         d = d2;
  2801.         k = k0;
  2802.         ilim = ilim0;
  2803.     }
  2804.  
  2805.     /* Do we have a "small" integer? */
  2806.  
  2807.     if (be >= 0 && k <= Int_max) {
  2808.         /* Yes. */
  2809.         ds = tens[k];
  2810.         if (ndigits < 0 && ilim <= 0) {
  2811.             S = mhi = 0;
  2812.             if (ilim < 0 || d <= 5*ds)
  2813.                 goto no_digits;
  2814.             goto one_digit;
  2815.         }
  2816.         for(i = 1;; i++) {
  2817.             L = (Long) (d / ds);
  2818.             d -= L*ds;
  2819. #ifdef Check_FLT_ROUNDS
  2820.             /* If FLT_ROUNDS == 2, L will usually be high by 1 */
  2821.             if (d < 0) {
  2822.                 L--;
  2823.                 d += ds;
  2824.             }
  2825. #endif
  2826.             *s++ = '0' + (PRInt32)L;
  2827.             if (i == ilim) {
  2828.                 d += d;
  2829.                 if ((d > ds) || (d == ds && L & 1)) {
  2830.                 bump_up:
  2831.                     while(*--s == '9')
  2832.                         if (s == s0) {
  2833.                             k++;
  2834.                             *s = '0';
  2835.                             break;
  2836.                         }
  2837.                     ++*s++;
  2838.                 }
  2839.                 break;
  2840.             }
  2841.             if (!(d *= 10.))
  2842.                 break;
  2843.         }
  2844.         goto ret1;
  2845.     }
  2846.  
  2847.     m2 = b2;
  2848.     m5 = b5;
  2849.     mhi = mlo = 0;
  2850.     if (leftright) {
  2851.         if (mode < 2) {
  2852.             i =
  2853. #ifndef Sudden_Underflow
  2854.                 denorm ? be + (Bias + (P-1) - 1 + 1) :
  2855. #endif
  2856. #ifdef IBM
  2857.                 1 + 4*P - 3 - bbits + ((bbits + be - 1) & 3);
  2858. #else
  2859.             1 + P - bbits;
  2860. #endif
  2861.         }
  2862.         else {
  2863.             j = ilim - 1;
  2864.             if (m5 >= j)
  2865.                 m5 -= j;
  2866.             else {
  2867.                 s5 += j -= m5;
  2868.                 b5 += j;
  2869.                 m5 = 0;
  2870.             }
  2871.             if ((i = ilim) < 0) {
  2872.                 m2 -= i;
  2873.                 i = 0;
  2874.             }
  2875.         }
  2876.         b2 += i;
  2877.         s2 += i;
  2878.         mhi = i2b(1);
  2879.     }
  2880.     if (m2 > 0 && s2 > 0) {
  2881.         i = m2 < s2 ? m2 : s2;
  2882.         b2 -= i;
  2883.         m2 -= i;
  2884.         s2 -= i;
  2885.     }
  2886.     if (b5 > 0) {
  2887.         if (leftright) {
  2888.             if (m5 > 0) {
  2889.                 mhi = pow5mult(mhi, m5);
  2890.                 b1 = mult(mhi, b);
  2891.                 Bfree(b);
  2892.                 b = b1;
  2893.             }
  2894.             if ((j = b5 - m5) != 0)
  2895.                 b = pow5mult(b, j);
  2896.         }
  2897.         else
  2898.             b = pow5mult(b, b5);
  2899.     }
  2900.     S = i2b(1);
  2901.     if (s5 > 0)
  2902.         S = pow5mult(S, s5);
  2903.  
  2904.     /* Check for special case that d is a normalized power of 2. */
  2905.  
  2906.     if (mode < 2) {
  2907.         if (!word1(d) && !(word0(d) & Bndry_mask)
  2908. #ifndef Sudden_Underflow
  2909.             && word0(d) & Exp_mask
  2910. #endif
  2911.             ) {
  2912.             /* The special case */
  2913.             b2 += Log2P;
  2914.             s2 += Log2P;
  2915.             spec_case = 1;
  2916.         }
  2917.         else
  2918.             spec_case = 0;
  2919.     }
  2920.  
  2921.     /* Arrange for convenient computation of quotients:
  2922.      * shift left if necessary so divisor has 4 leading 0 bits.
  2923.      *
  2924.      * Perhaps we should just compute leading 28 bits of S once
  2925.      * and for all and pass them and a shift to quorem, so it
  2926.      * can do shifts and ors to compute the numerator for q.
  2927.      */
  2928. #ifdef Pack_32
  2929.     if ((i = ((s5 ? 32 - hi0bits(S->x[S->wds-1]) : 1) + s2) & 0x1f) != 0)
  2930.         i = 32 - i;
  2931. #else
  2932.     if (i = ((s5 ? 32 - hi0bits(S->x[S->wds-1]) : 1) + s2) & 0xf)
  2933.         i = 16 - i;
  2934. #endif
  2935.     if (i > 4) {
  2936.         i -= 4;
  2937.         b2 += i;
  2938.         m2 += i;
  2939.         s2 += i;
  2940.     }
  2941.     else if (i < 4) {
  2942.         i += 28;
  2943.         b2 += i;
  2944.         m2 += i;
  2945.         s2 += i;
  2946.     }
  2947.     if (b2 > 0)
  2948.         b = lshift(b, b2);
  2949.     if (s2 > 0)
  2950.         S = lshift(S, s2);
  2951.     if (k_check) {
  2952.         if (cmp(b,S) < 0) {
  2953.             k--;
  2954.             b = multadd(b, 10, 0);    /* we botched the k estimate */
  2955.             if (leftright)
  2956.                 mhi = multadd(mhi, 10, 0);
  2957.             ilim = ilim1;
  2958.         }
  2959.     }
  2960.     if (ilim <= 0 && mode > 2) {
  2961.         if (ilim < 0 || cmp(b,S = multadd(S,5,0)) <= 0) {
  2962.             /* no digits, fcvt style */
  2963.         no_digits:
  2964.             k = -1 - ndigits;
  2965.             goto ret;
  2966.         }
  2967.     one_digit:
  2968.         *s++ = '1';
  2969.         k++;
  2970.         goto ret;
  2971.     }
  2972.     if (leftright) {
  2973.         if (m2 > 0)
  2974.             mhi = lshift(mhi, m2);
  2975.  
  2976.         /* Compute mlo -- check for special case
  2977.          * that d is a normalized power of 2.
  2978.          */
  2979.  
  2980.         mlo = mhi;
  2981.         if (spec_case) {
  2982.             mhi = Balloc(mhi->k);
  2983.             Bcopy(mhi, mlo);
  2984.             mhi = lshift(mhi, Log2P);
  2985.         }
  2986.  
  2987.         for(i = 1;;i++) {
  2988.             dig = quorem(b,S) + '0';
  2989.             /* Do we yet have the shortest decimal string
  2990.              * that will round to d?
  2991.              */
  2992.             j = cmp(b, mlo);
  2993.             delta = diff(S, mhi);
  2994.             j1 = delta->sign ? 1 : cmp(b, delta);
  2995.             Bfree(delta);
  2996. #ifndef ROUND_BIASED
  2997.             if (j1 == 0 && !mode && !(word1(d) & 1)) {
  2998.                 if (dig == '9')
  2999.                     goto round_9_up;
  3000.                 if (j > 0)
  3001.                     dig++;
  3002.                 *s++ = dig;
  3003.                 goto ret;
  3004.             }
  3005. #endif
  3006.             if ((j < 0) || ((j == 0) && (!mode)
  3007. #ifndef ROUND_BIASED
  3008.                 && (!(word1(d) & 1)))
  3009. #endif
  3010.                 ) {
  3011.                 if (j1 > 0) {
  3012.                     b = lshift(b, 1);
  3013.                     j1 = cmp(b, S);
  3014.                     if (((j1 > 0) || (j1 == 0 && dig & 1))
  3015.                         && (dig++ == '9'))
  3016.                         goto round_9_up;
  3017.                 }
  3018.                 *s++ = dig;
  3019.                 goto ret;
  3020.             }
  3021.             if (j1 > 0) {
  3022.                 if (dig == '9') { /* possible if i == 1 */
  3023.                 round_9_up:
  3024.                     *s++ = '9';
  3025.                     goto roundoff;
  3026.                 }
  3027.                 *s++ = dig + 1;
  3028.                 goto ret;
  3029.             }
  3030.             *s++ = dig;
  3031.             if (i == ilim)
  3032.                 break;
  3033.             b = multadd(b, 10, 0);
  3034.             if (mlo == mhi)
  3035.                 mlo = mhi = multadd(mhi, 10, 0);
  3036.             else {
  3037.                 mlo = multadd(mlo, 10, 0);
  3038.                 mhi = multadd(mhi, 10, 0);
  3039.             }
  3040.         }
  3041.     }
  3042.     else
  3043.         for(i = 1;; i++) {
  3044.             *s++ = dig = quorem(b,S) + '0';
  3045.             if (i >= ilim)
  3046.                 break;
  3047.             b = multadd(b, 10, 0);
  3048.         }
  3049.  
  3050.     /* Round off last digit */
  3051.  
  3052.     b = lshift(b, 1);
  3053.     j = cmp(b, S);
  3054.     if ((j > 0) || (j == 0 && dig & 1)) {
  3055.     roundoff:
  3056.         while(*--s == '9')
  3057.             if (s == s0) {
  3058.                 k++;
  3059.                 *s++ = '1';
  3060.                 goto ret;
  3061.             }
  3062.         ++*s++;
  3063.     }
  3064.     else {
  3065.         while(*--s == '0'){} /* just count -- nothing to execute */
  3066.         s++;
  3067.     }
  3068. ret:
  3069.     Bfree(S);
  3070.     if (mhi) {
  3071.         if (mlo && mlo != mhi)
  3072.             Bfree(mlo);
  3073.         Bfree(mhi);
  3074.     }
  3075. ret1:
  3076.     Bfree(b);
  3077.     *s = 0;
  3078.     *decpt = k + 1;
  3079.     strsize = (s - s0) + 1;
  3080.     if (strsize <= bufsize) {
  3081.         retval = PR_SUCCESS;
  3082.         memcpy(buf, s0, strsize);
  3083.         if (rve) {
  3084.             *rve = buf + strsize - 1;
  3085.             PR_ASSERT(**rve == '\0');
  3086.         }
  3087.     } else {
  3088.         PR_SetError(PR_BUFFER_OVERFLOW_ERROR, 0);
  3089.         retval = PR_FAILURE;
  3090.     }
  3091.  
  3092.     /* cleanup */
  3093.     result->k = result_k;
  3094.     result->maxwds = 1 << result_k;
  3095.     Bfree(result);
  3096.  
  3097.     return retval;
  3098. }
  3099.  
  3100. /*
  3101. ** conversion routines for floating point
  3102. ** prcsn - number of digits of precision to generate floating
  3103. ** point value.
  3104. ** This should be reparameterized so that you can send in a
  3105. **   prcn for the positive and negative ranges.  For now, 
  3106. **   conform to the ECMA JavaScript spec which says numbers
  3107. **   less than 1e-6 are in scientific notation.
  3108. ** Also, the ECMA spec says that there should always be a
  3109. **   '+' or '-' after the 'e' in scientific notation
  3110. */
  3111. PR_IMPLEMENT(void)
  3112. PR_cnvtf(char *buf,int bufsz, int prcsn,double fval)
  3113. {
  3114.     PRIntn decpt, sign, numdigits;
  3115.     char *num, *nump;
  3116.     char *bufp = buf;
  3117.     char *endnum;
  3118.  
  3119.     /* If anything fails, we store an empty string in 'buf' */
  3120.     num = (char*)PR_MALLOC(bufsz);
  3121.     if (num == NULL) {
  3122.         buf[0] = '\0';
  3123.         return;
  3124.     }
  3125.     /* XXX Why use mode 1? */
  3126.     if (PR_dtoa_r(fval,1,prcsn,&decpt,&sign,&endnum,num,bufsz)
  3127.             == PR_FAILURE) {
  3128.         buf[0] = '\0';
  3129.         goto done;
  3130.     }
  3131.     numdigits = endnum - num;
  3132.     nump = num;
  3133.  
  3134.     if (sign &&
  3135.         !(word0(fval) == Sign_bit && word1(fval) == 0) &&
  3136.         !((word0(fval) & Exp_mask) == Exp_mask &&
  3137.           (word1(fval) || (word0(fval) & 0xfffff)))) {
  3138.         *bufp++ = '-';
  3139.     }
  3140.     
  3141.     if(decpt == 9999){
  3142.         while((*bufp++ = *nump++) != 0){} /* nothing to execute */
  3143.         goto done;
  3144.     }
  3145.  
  3146.     if(decpt > (prcsn+1) || decpt < -(prcsn-1) || decpt < -5){
  3147.         *bufp++ = *nump++;
  3148.         if(numdigits != 1){
  3149.             *bufp++ = '.';
  3150.         }
  3151.         
  3152.         while(*nump != '\0'){
  3153.             *bufp++ = *nump++;
  3154.         }
  3155.         *bufp++ = 'e';
  3156.         PR_snprintf(bufp,bufsz - (bufp - buf), "%+d",decpt-1);
  3157.     }
  3158.     else if(decpt >= 0){
  3159.         while(decpt--){
  3160.             if(*nump != '\0'){
  3161.                 *bufp++ = *nump++;
  3162.             }
  3163.             else {
  3164.                 *bufp++ = '0';
  3165.             }
  3166.         }
  3167.         if(*nump != '\0'){
  3168.             *bufp++ = '.';
  3169.             while(*nump != '\0'){
  3170.                 *bufp++ = *nump++;
  3171.             }
  3172.         }
  3173.         *bufp++ = '\0';
  3174.     }
  3175.     else if(decpt < 0){
  3176.         *bufp++ = '0';
  3177.         *bufp++ = '.';
  3178.         while(decpt++){
  3179.             *bufp++ = '0';
  3180.         }
  3181.         
  3182.         while(*nump != '\0'){
  3183.             *bufp++ = *nump++;
  3184.         }
  3185.         *bufp++ = '\0';
  3186.     }
  3187. done:
  3188.     PR_DELETE(num);
  3189. }
  3190.