home *** CD-ROM | disk | FTP | other *** search
/ Aminet 18 / aminetcdnumber181997.iso / Aminet / dev / gcc / ixemulsrc.lha / ixemul / stdlib / strtod.c < prev    next >
C/C++ Source or Header  |  1996-12-11  |  49KB  |  2,501 lines

  1. /****************************************************************
  2.  *
  3.  * The author of this software is David M. Gay.
  4.  *
  5.  * Copyright (c) 1991 by AT&T.
  6.  *
  7.  * Permission to use, copy, modify, and distribute this software for any
  8.  * purpose without fee is hereby granted, provided that this entire notice
  9.  * is included in all copies of any software which is or includes a copy
  10.  * or modification of this software and in all copies of the supporting
  11.  * documentation for such software.
  12.  *
  13.  * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
  14.  * WARRANTY.  IN PARTICULAR, NEITHER THE AUTHOR NOR AT&T MAKES ANY
  15.  * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
  16.  * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
  17.  *
  18.  ***************************************************************/
  19.  
  20. /* Please send bug reports to
  21.     David M. Gay
  22.     AT&T Bell Laboratories, Room 2C-463
  23.     600 Mountain Avenue
  24.     Murray Hill, NJ 07974-2070
  25.     U.S.A.
  26.     dmg@research.att.com or research!dmg
  27.  */
  28.  
  29. /* strtod for IEEE-, VAX-, and IBM-arithmetic machines.
  30.  *
  31.  * This strtod returns a nearest machine number to the input decimal
  32.  * string (or sets errno to ERANGE).  With IEEE arithmetic, ties are
  33.  * broken by the IEEE round-even rule.  Otherwise ties are broken by
  34.  * biased rounding (add half and chop).
  35.  *
  36.  * Inspired loosely by William D. Clinger's paper "How to Read Floating
  37.  * Point Numbers Accurately" [Proc. ACM SIGPLAN '90, pp. 92-101].
  38.  *
  39.  * Modifications:
  40.  *
  41.  *    1. We only require IEEE, IBM, or VAX double-precision
  42.  *        arithmetic (not IEEE double-extended).
  43.  *    2. We get by with floating-point arithmetic in a case that
  44.  *        Clinger missed -- when we're computing d * 10^n
  45.  *        for a small integer d and the integer n is not too
  46.  *        much larger than 22 (the maximum integer k for which
  47.  *        we can represent 10^k exactly), we may be able to
  48.  *        compute (d*10^k) * 10^(e-k) with just one roundoff.
  49.  *    3. Rather than a bit-at-a-time adjustment of the binary
  50.  *        result in the hard case, we use floating-point
  51.  *        arithmetic to determine the adjustment to within
  52.  *        one bit; only in really hard cases do we need to
  53.  *        compute a second residual.
  54.  *    4. Because of 3., we don't need a large table of powers of 10
  55.  *        for ten-to-e (just some small tables, e.g. of 10^k
  56.  *        for 0 <= k <= 22).
  57.  */
  58.  
  59. /*
  60.  * #define IEEE_LITTLE_ENDIAN for IEEE-arithmetic machines where the least
  61.  *    significant byte has the lowest address.
  62.  * #define IEEE_BIG_ENDIAN for IEEE-arithmetic machines where the most
  63.  *    significant byte has the lowest address.
  64.  * #define Long int on machines with 32-bit ints and 64-bit longs.
  65.  * #define Sudden_Underflow for IEEE-format machines without gradual
  66.  *    underflow (i.e., that flush to zero on underflow).
  67.  * #define IBM for IBM mainframe-style floating-point arithmetic.
  68.  * #define VAX for VAX-style floating-point arithmetic.
  69.  * #define Unsigned_Shifts if >> does treats its left operand as unsigned.
  70.  * #define No_leftright to omit left-right logic in fast floating-point
  71.  *    computation of dtoa.
  72.  * #define Check_FLT_ROUNDS if FLT_ROUNDS can assume the values 2 or 3.
  73.  * #define RND_PRODQUOT to use rnd_prod and rnd_quot (assembly routines
  74.  *    that use extended-precision instructions to compute rounded
  75.  *    products and quotients) with IBM.
  76.  * #define ROUND_BIASED for IEEE-format with biased rounding.
  77.  * #define Inaccurate_Divide for IEEE-format with correctly rounded
  78.  *    products but inaccurate quotients, e.g., for Intel i860.
  79.  * #define Just_16 to store 16 bits per 32-bit Long when doing high-precision
  80.  *    integer arithmetic.  Whether this speeds things up or slows things
  81.  *    down depends on the machine and the number being converted.
  82.  * #define KR_headers for old-style C function headers.
  83.  * #define Bad_float_h if your system lacks a float.h or if it does not
  84.  *    define some or all of DBL_DIG, DBL_MAX_10_EXP, DBL_MAX_EXP,
  85.  *    FLT_RADIX, FLT_ROUNDS, and DBL_MAX.
  86.  * #define MALLOC your_malloc, where your_malloc(n) acts like malloc(n)
  87.  *    if memory is available and otherwise does something you deem
  88.  *    appropriate.  If MALLOC is undefined, malloc will be invoked
  89.  *    directly -- and assumed always to succeed.
  90.  */
  91.  
  92. #if defined(LIBC_SCCS) && !defined(lint)
  93. static char *rcsid = "$Id: strtod.c,v 1.19 1994/12/23 22:50:19 jtc Exp $";
  94. #endif /* LIBC_SCCS and not lint */
  95.  
  96. #define _KERNEL
  97. #include "ixemul.h"
  98.  
  99. #ifndef __m68k__
  100. #define __m68k__
  101. #endif
  102.  
  103. #if defined(__m68k__) || defined(__sparc__) || defined(__i386__) || \
  104.     defined(__mips__) || defined(__ns32k__) || defined(__alpha__)
  105. #include <machine/endian.h>
  106. #if BYTE_ORDER == BIG_ENDIAN
  107. #define IEEE_BIG_ENDIAN
  108. #else
  109. #define IEEE_LITTLE_ENDIAN
  110. #endif
  111. #endif
  112.  
  113. #ifdef vax
  114. #define VAX
  115. #endif
  116.  
  117. #define Long    int32_t
  118. #define ULong    u_int32_t
  119.  
  120. #ifdef DEBUG
  121. #include "stdio.h"
  122. #define Bug(x) {fprintf(stderr, "%s\n", x); exit(1);}
  123. #endif
  124.  
  125. #ifdef __cplusplus
  126. #include "malloc.h"
  127. #include "memory.h"
  128. #else
  129. #ifndef KR_headers
  130. #include "stdlib.h"
  131. #include "string.h"
  132. #include "locale.h"
  133. #else
  134. #include "malloc.h"
  135. #include "memory.h"
  136. #endif
  137. #endif
  138.  
  139. #ifdef MALLOC
  140. #ifdef KR_headers
  141. extern char *MALLOC();
  142. #else
  143. extern void *MALLOC(size_t);
  144. #endif
  145. #else
  146. #define MALLOC malloc
  147. #endif
  148.  
  149. #include "ctype.h"
  150. #include "errno.h"
  151.  
  152. #ifdef Bad_float_h
  153. #undef __STDC__
  154. #ifdef IEEE_BIG_ENDIAN
  155. #define IEEE_ARITHMETIC
  156. #endif
  157. #ifdef IEEE_LITTLE_ENDIAN
  158. #define IEEE_ARITHMETIC
  159. #endif
  160.  
  161. #ifdef IEEE_ARITHMETIC
  162. #define DBL_DIG 15
  163. #define DBL_MAX_10_EXP 308
  164. #define DBL_MAX_EXP 1024
  165. #define FLT_RADIX 2
  166. #define FLT_ROUNDS 1
  167. #define DBL_MAX 1.7976931348623157e+308
  168. #endif
  169.  
  170. #ifdef IBM
  171. #define DBL_DIG 16
  172. #define DBL_MAX_10_EXP 75
  173. #define DBL_MAX_EXP 63
  174. #define FLT_RADIX 16
  175. #define FLT_ROUNDS 0
  176. #define DBL_MAX 7.2370055773322621e+75
  177. #endif
  178.  
  179. #ifdef VAX
  180. #define DBL_DIG 16
  181. #define DBL_MAX_10_EXP 38
  182. #define DBL_MAX_EXP 127
  183. #define FLT_RADIX 2
  184. #define FLT_ROUNDS 1
  185. #define DBL_MAX 1.7014118346046923e+38
  186. #endif
  187.  
  188. #ifndef LONG_MAX
  189. #define LONG_MAX 2147483647
  190. #endif
  191. #else
  192. #include "float.h"
  193. #endif
  194. #ifndef __MATH_H__
  195. #include "math.h"
  196. #endif
  197.  
  198. #ifdef __cplusplus
  199. extern "C" {
  200. #endif
  201.  
  202. #ifndef CONST
  203. #ifdef KR_headers
  204. #define CONST /* blank */
  205. #else
  206. #define CONST const
  207. #endif
  208. #endif
  209.  
  210. #ifdef Unsigned_Shifts
  211. #define Sign_Extend(a,b) if (b < 0) a |= 0xffff0000;
  212. #else
  213. #define Sign_Extend(a,b) /*no-op*/
  214. #endif
  215.  
  216. #if defined(IEEE_LITTLE_ENDIAN) + defined(IEEE_BIG_ENDIAN) + defined(VAX) + defined(IBM) != 1
  217. Exactly one of IEEE_LITTLE_ENDIAN IEEE_BIG_ENDIAN, VAX, or
  218. IBM should be defined.
  219. #endif
  220.  
  221. #ifdef IEEE_LITTLE_ENDIAN
  222. #define word0(x) ((ULong *)&x)[1]
  223. #define word1(x) ((ULong *)&x)[0]
  224. #else
  225. #define word0(x) ((ULong *)&x)[0]
  226. #define word1(x) ((ULong *)&x)[1]
  227. #endif
  228.  
  229. /* The following definition of Storeinc is appropriate for MIPS processors.
  230.  * An alternative that might be better on some machines is
  231.  * #define Storeinc(a,b,c) (*a++ = b << 16 | c & 0xffff)
  232.  */
  233. #if defined(IEEE_LITTLE_ENDIAN) + defined(VAX)
  234. #define Storeinc(a,b,c) (((unsigned short *)a)[1] = (unsigned short)b, \
  235. ((unsigned short *)a)[0] = (unsigned short)c, a++)
  236. #else
  237. #define Storeinc(a,b,c) (((unsigned short *)a)[0] = (unsigned short)b, \
  238. ((unsigned short *)a)[1] = (unsigned short)c, a++)
  239. #endif
  240.  
  241. /* #define P DBL_MANT_DIG */
  242. /* Ten_pmax = floor(P*log(2)/log(5)) */
  243. /* Bletch = (highest power of 2 < DBL_MAX_10_EXP) / 16 */
  244. /* Quick_max = floor((P-1)*log(FLT_RADIX)/log(10) - 1) */
  245. /* Int_max = floor(P*log(FLT_RADIX)/log(10) - 1) */
  246.  
  247. #if defined(IEEE_LITTLE_ENDIAN) + defined(IEEE_BIG_ENDIAN)
  248. #define Exp_shift  20
  249. #define Exp_shift1 20
  250. #define Exp_msk1    0x100000
  251. #define Exp_msk11   0x100000
  252. #define Exp_mask  0x7ff00000
  253. #define P 53
  254. #define Bias 1023
  255. #define IEEE_Arith
  256. #define Emin (-1022)
  257. #define Exp_1  0x3ff00000
  258. #define Exp_11 0x3ff00000
  259. #define Ebits 11
  260. #define Frac_mask  0xfffff
  261. #define Frac_mask1 0xfffff
  262. #define Ten_pmax 22
  263. #define Bletch 0x10
  264. #define Bndry_mask  0xfffff
  265. #define Bndry_mask1 0xfffff
  266. #define LSB 1
  267. #define Sign_bit 0x80000000
  268. #define Log2P 1
  269. #define Tiny0 0
  270. #define Tiny1 1
  271. #define Quick_max 14
  272. #define Int_max 14
  273. #define Infinite(x) (word0(x) == 0x7ff00000) /* sufficient test for here */
  274. #else
  275. #undef  Sudden_Underflow
  276. #define Sudden_Underflow
  277. #ifdef IBM
  278. #define Exp_shift  24
  279. #define Exp_shift1 24
  280. #define Exp_msk1   0x1000000
  281. #define Exp_msk11  0x1000000
  282. #define Exp_mask  0x7f000000
  283. #define P 14
  284. #define Bias 65
  285. #define Exp_1  0x41000000
  286. #define Exp_11 0x41000000
  287. #define Ebits 8    /* exponent has 7 bits, but 8 is the right value in b2d */
  288. #define Frac_mask  0xffffff
  289. #define Frac_mask1 0xffffff
  290. #define Bletch 4
  291. #define Ten_pmax 22
  292. #define Bndry_mask  0xefffff
  293. #define Bndry_mask1 0xffffff
  294. #define LSB 1
  295. #define Sign_bit 0x80000000
  296. #define Log2P 4
  297. #define Tiny0 0x100000
  298. #define Tiny1 0
  299. #define Quick_max 14
  300. #define Int_max 15
  301. #else /* VAX */
  302. #define Exp_shift  23
  303. #define Exp_shift1 7
  304. #define Exp_msk1    0x80
  305. #define Exp_msk11   0x800000
  306. #define Exp_mask  0x7f80
  307. #define P 56
  308. #define Bias 129
  309. #define Exp_1  0x40800000
  310. #define Exp_11 0x4080
  311. #define Ebits 8
  312. #define Frac_mask  0x7fffff
  313. #define Frac_mask1 0xffff007f
  314. #define Ten_pmax 24
  315. #define Bletch 2
  316. #define Bndry_mask  0xffff007f
  317. #define Bndry_mask1 0xffff007f
  318. #define LSB 0x10000
  319. #define Sign_bit 0x8000
  320. #define Log2P 1
  321. #define Tiny0 0x80
  322. #define Tiny1 0
  323. #define Quick_max 15
  324. #define Int_max 15
  325. #endif
  326. #endif
  327.  
  328. #ifndef IEEE_Arith
  329. #define ROUND_BIASED
  330. #endif
  331.  
  332. #ifdef RND_PRODQUOT
  333. #define rounded_product(a,b) a = rnd_prod(a, b)
  334. #define rounded_quotient(a,b) a = rnd_quot(a, b)
  335. #ifdef KR_headers
  336. extern double rnd_prod(), rnd_quot();
  337. #else
  338. extern double rnd_prod(double, double), rnd_quot(double, double);
  339. #endif
  340. #else
  341. #define rounded_product(a,b) a *= b
  342. #define rounded_quotient(a,b) a /= b
  343. #endif
  344.  
  345. #define Big0 (Frac_mask1 | Exp_msk1*(DBL_MAX_EXP+Bias-1))
  346. #define Big1 0xffffffff
  347.  
  348. #ifndef Just_16
  349. /* When Pack_32 is not defined, we store 16 bits per 32-bit Long.
  350.  * This makes some inner loops simpler and sometimes saves work
  351.  * during multiplications, but it often seems to make things slightly
  352.  * slower.  Hence the default is now to store 32 bits per Long.
  353.  */
  354. #ifndef Pack_32
  355. #define Pack_32
  356. #endif
  357. #endif
  358.  
  359. #define Kmax 15
  360.  
  361. #ifdef __cplusplus
  362. extern "C" double strtod(const char *s00, char **se);
  363. extern "C" char *__dtoa(double d, int mode, int ndigits,
  364.             int *decpt, int *sign, char **rve);
  365. #endif
  366.  
  367.  struct
  368. Bigint {
  369.     struct Bigint *next;
  370.     int k, maxwds, sign, wds;
  371.     ULong x[1];
  372.     };
  373.  
  374.  typedef struct Bigint Bigint;
  375.  
  376.  static Bigint *
  377. Balloc
  378. #ifdef KR_headers
  379.     (k) int k;
  380. #else
  381.     (int k)
  382. #endif
  383. {
  384.     int x;
  385.     Bigint *rv;
  386.  
  387.     if ((rv = u.u_freelist[k])) {
  388.         u.u_freelist[k] = rv->next;
  389.         }
  390.     else {
  391.         x = 1 << k;
  392.         rv = (Bigint *)MALLOC(sizeof(Bigint) + (x-1)*sizeof(Long));
  393.         rv->k = k;
  394.         rv->maxwds = x;
  395.         }
  396.     rv->sign = rv->wds = 0;
  397.     return rv;
  398.     }
  399.  
  400.  static void
  401. Bfree
  402. #ifdef KR_headers
  403.     (v) Bigint *v;
  404. #else
  405.     (Bigint *v)
  406. #endif
  407. {
  408.     if (v) {
  409.         v->next = u.u_freelist[v->k];
  410.         u.u_freelist[v->k] = v;
  411.         }
  412.     }
  413.  
  414. #define Bcopy(x,y) memcpy((char *)&x->sign, (char *)&y->sign, \
  415. y->wds*sizeof(Long) + 2*sizeof(int))
  416.  
  417.  static Bigint *
  418. multadd
  419. #ifdef KR_headers
  420.     (b, m, a) Bigint *b; int m, a;
  421. #else
  422.     (Bigint *b, int m, int a)    /* multiply by m and add a */
  423. #endif
  424. {
  425.     int i, wds;
  426.     ULong *x, y;
  427. #ifdef Pack_32
  428.     ULong xi, z;
  429. #endif
  430.     Bigint *b1;
  431.  
  432.     wds = b->wds;
  433.     x = b->x;
  434.     i = 0;
  435.     do {
  436. #ifdef Pack_32
  437.         xi = *x;
  438.         y = (xi & 0xffff) * m + a;
  439.         z = (xi >> 16) * m + (y >> 16);
  440.         a = (int)(z >> 16);
  441.         *x++ = (z << 16) + (y & 0xffff);
  442. #else
  443.         y = *x * m + a;
  444.         a = (int)(y >> 16);
  445.         *x++ = y & 0xffff;
  446. #endif
  447.         }
  448.         while(++i < wds);
  449.     if (a) {
  450.         if (wds >= b->maxwds) {
  451.             b1 = Balloc(b->k+1);
  452.             Bcopy(b1, b);
  453.             Bfree(b);
  454.             b = b1;
  455.             }
  456.         b->x[wds++] = a;
  457.         b->wds = wds;
  458.         }
  459.     return b;
  460.     }
  461.  
  462.  static Bigint *
  463. s2b
  464. #ifdef KR_headers
  465.     (s, nd0, nd, y9) CONST char *s; int nd0, nd; ULong y9;
  466. #else
  467.     (CONST char *s, int nd0, int nd, ULong y9)
  468. #endif
  469. {
  470.     Bigint *b;
  471.     int i, k;
  472.     Long x, y;
  473.  
  474.     x = (nd + 8) / 9;
  475.     for(k = 0, y = 1; x > y; y <<= 1, k++) ;
  476. #ifdef Pack_32
  477.     b = Balloc(k);
  478.     b->x[0] = y9;
  479.     b->wds = 1;
  480. #else
  481.     b = Balloc(k+1);
  482.     b->x[0] = y9 & 0xffff;
  483.     b->wds = (b->x[1] = y9 >> 16) ? 2 : 1;
  484. #endif
  485.  
  486.     i = 9;
  487.     if (9 < nd0) {
  488.         s += 9;
  489.         do b = multadd(b, 10, *s++ - '0');
  490.             while(++i < nd0);
  491.         s++;
  492.         }
  493.     else
  494.         s += 10;
  495.     for(; i < nd; i++)
  496.         b = multadd(b, 10, *s++ - '0');
  497.     return b;
  498.     }
  499.  
  500.  static int
  501. hi0bits
  502. #ifdef KR_headers
  503.     (x) register ULong x;
  504. #else
  505.     (register ULong x)
  506. #endif
  507. {
  508.     register int k = 0;
  509.  
  510.     if (!(x & 0xffff0000)) {
  511.         k = 16;
  512.         x <<= 16;
  513.         }
  514.     if (!(x & 0xff000000)) {
  515.         k += 8;
  516.         x <<= 8;
  517.         }
  518.     if (!(x & 0xf0000000)) {
  519.         k += 4;
  520.         x <<= 4;
  521.         }
  522.     if (!(x & 0xc0000000)) {
  523.         k += 2;
  524.         x <<= 2;
  525.         }
  526.     if (!(x & 0x80000000)) {
  527.         k++;
  528.         if (!(x & 0x40000000))
  529.             return 32;
  530.         }
  531.     return k;
  532.     }
  533.  
  534.  static int
  535. lo0bits
  536. #ifdef KR_headers
  537.     (y) ULong *y;
  538. #else
  539.     (ULong *y)
  540. #endif
  541. {
  542.     register int k;
  543.     register ULong x = *y;
  544.  
  545.     if (x & 7) {
  546.         if (x & 1)
  547.             return 0;
  548.         if (x & 2) {
  549.             *y = x >> 1;
  550.             return 1;
  551.             }
  552.         *y = x >> 2;
  553.         return 2;
  554.         }
  555.     k = 0;
  556.     if (!(x & 0xffff)) {
  557.         k = 16;
  558.         x >>= 16;
  559.         }
  560.     if (!(x & 0xff)) {
  561.         k += 8;
  562.         x >>= 8;
  563.         }
  564.     if (!(x & 0xf)) {
  565.         k += 4;
  566.         x >>= 4;
  567.         }
  568.     if (!(x & 0x3)) {
  569.         k += 2;
  570.         x >>= 2;
  571.         }
  572.     if (!(x & 1)) {
  573.         k++;
  574.         x >>= 1;
  575.         if (!x & 1)
  576.             return 32;
  577.         }
  578.     *y = x;
  579.     return k;
  580.     }
  581.  
  582.  static Bigint *
  583. i2b
  584. #ifdef KR_headers
  585.     (i) int i;
  586. #else
  587.     (int i)
  588. #endif
  589. {
  590.     Bigint *b;
  591.  
  592.     b = Balloc(1);
  593.     b->x[0] = i;
  594.     b->wds = 1;
  595.     return b;
  596.     }
  597.  
  598.  static Bigint *
  599. mult
  600. #ifdef KR_headers
  601.     (a, b) Bigint *a, *b;
  602. #else
  603.     (Bigint *a, Bigint *b)
  604. #endif
  605. {
  606.     Bigint *c;
  607.     int k, wa, wb, wc;
  608.     ULong carry, y, z;
  609.     ULong *x, *xa, *xae, *xb, *xbe, *xc, *xc0;
  610. #ifdef Pack_32
  611.     ULong z2;
  612. #endif
  613.  
  614.     if (a->wds < b->wds) {
  615.         c = a;
  616.         a = b;
  617.         b = c;
  618.         }
  619.     k = a->k;
  620.     wa = a->wds;
  621.     wb = b->wds;
  622.     wc = wa + wb;
  623.     if (wc > a->maxwds)
  624.         k++;
  625.     c = Balloc(k);
  626.     for(x = c->x, xa = x + wc; x < xa; x++)
  627.         *x = 0;
  628.     xa = a->x;
  629.     xae = xa + wa;
  630.     xb = b->x;
  631.     xbe = xb + wb;
  632.     xc0 = c->x;
  633. #ifdef Pack_32
  634.     for(; xb < xbe; xb++, xc0++) {
  635.         if ((y = *xb & 0xffff)) {
  636.             x = xa;
  637.             xc = xc0;
  638.             carry = 0;
  639.             do {
  640.                 z = (*x & 0xffff) * y + (*xc & 0xffff) + carry;
  641.                 carry = z >> 16;
  642.                 z2 = (*x++ >> 16) * y + (*xc >> 16) + carry;
  643.                 carry = z2 >> 16;
  644.                 Storeinc(xc, z2, z);
  645.                 }
  646.                 while(x < xae);
  647.             *xc = carry;
  648.             }
  649.         if ((y = *xb >> 16)) {
  650.             x = xa;
  651.             xc = xc0;
  652.             carry = 0;
  653.             z2 = *xc;
  654.             do {
  655.                 z = (*x & 0xffff) * y + (*xc >> 16) + carry;
  656.                 carry = z >> 16;
  657.                 Storeinc(xc, z, z2);
  658.                 z2 = (*x++ >> 16) * y + (*xc & 0xffff) + carry;
  659.                 carry = z2 >> 16;
  660.                 }
  661.                 while(x < xae);
  662.             *xc = z2;
  663.             }
  664.         }
  665. #else
  666.     for(; xb < xbe; xc0++) {
  667.         if (y = *xb++) {
  668.             x = xa;
  669.             xc = xc0;
  670.             carry = 0;
  671.             do {
  672.                 z = *x++ * y + *xc + carry;
  673.                 carry = z >> 16;
  674.                 *xc++ = z & 0xffff;
  675.                 }
  676.                 while(x < xae);
  677.             *xc = carry;
  678.             }
  679.         }
  680. #endif
  681.     for(xc0 = c->x, xc = xc0 + wc; wc > 0 && !*--xc; --wc) ;
  682.     c->wds = wc;
  683.     return c;
  684.     }
  685.  
  686.  static Bigint *
  687. pow5mult
  688. #ifdef KR_headers
  689.     (b, k) Bigint *b; int k;
  690. #else
  691.     (Bigint *b, int k)
  692. #endif
  693. {
  694.     Bigint *b1, *p5, *p51;
  695.     int i;
  696.     static CONST int p05[3] = { 5, 25, 125 };
  697.  
  698.     if ((i = k & 3))
  699.         b = multadd(b, p05[i-1], 0);
  700.  
  701.     if (!(k >>= 2))
  702.         return b;
  703.     if (!(p5 = u.u_p5s)) {
  704.         /* first time */
  705.         p5 = u.u_p5s = i2b(625);
  706.         p5->next = 0;
  707.         }
  708.     for(;;) {
  709.         if (k & 1) {
  710.             b1 = mult(b, p5);
  711.             Bfree(b);
  712.             b = b1;
  713.             }
  714.         if (!(k >>= 1))
  715.             break;
  716.         if (!(p51 = p5->next)) {
  717.             p51 = p5->next = mult(p5,p5);
  718.             p51->next = 0;
  719.             }
  720.         p5 = p51;
  721.         }
  722.     return b;
  723.     }
  724.  
  725.  static Bigint *
  726. lshift
  727. #ifdef KR_headers
  728.     (b, k) Bigint *b; int k;
  729. #else
  730.     (Bigint *b, int k)
  731. #endif
  732. {
  733.     int i, k1, n, n1;
  734.     Bigint *b1;
  735.     ULong *x, *x1, *xe, z;
  736.  
  737. #ifdef Pack_32
  738.     n = k >> 5;
  739. #else
  740.     n = k >> 4;
  741. #endif
  742.     k1 = b->k;
  743.     n1 = n + b->wds + 1;
  744.     for(i = b->maxwds; n1 > i; i <<= 1)
  745.         k1++;
  746.     b1 = Balloc(k1);
  747.     x1 = b1->x;
  748.     for(i = 0; i < n; i++)
  749.         *x1++ = 0;
  750.     x = b->x;
  751.     xe = x + b->wds;
  752. #ifdef Pack_32
  753.     if (k &= 0x1f) {
  754.         k1 = 32 - k;
  755.         z = 0;
  756.         do {
  757.             *x1++ = *x << k | z;
  758.             z = *x++ >> k1;
  759.             }
  760.             while(x < xe);
  761.         if ((*x1 = z))
  762.             ++n1;
  763.         }
  764. #else
  765.     if (k &= 0xf) {
  766.         k1 = 16 - k;
  767.         z = 0;
  768.         do {
  769.             *x1++ = *x << k  & 0xffff | z;
  770.             z = *x++ >> k1;
  771.             }
  772.             while(x < xe);
  773.         if ((*x1 = z))
  774.             ++n1;
  775.         }
  776. #endif
  777.     else do
  778.         *x1++ = *x++;
  779.         while(x < xe);
  780.     b1->wds = n1 - 1;
  781.     Bfree(b);
  782.     return b1;
  783.     }
  784.  
  785.  static int
  786. cmp
  787. #ifdef KR_headers
  788.     (a, b) Bigint *a, *b;
  789. #else
  790.     (Bigint *a, Bigint *b)
  791. #endif
  792. {
  793.     ULong *xa, *xa0, *xb, *xb0;
  794.     int i, j;
  795.  
  796.     i = a->wds;
  797.     j = b->wds;
  798. #ifdef DEBUG
  799.     if (i > 1 && !a->x[i-1])
  800.         Bug("cmp called with a->x[a->wds-1] == 0");
  801.     if (j > 1 && !b->x[j-1])
  802.         Bug("cmp called with b->x[b->wds-1] == 0");
  803. #endif
  804.     if (i -= j)
  805.         return i;
  806.     xa0 = a->x;
  807.     xa = xa0 + j;
  808.     xb0 = b->x;
  809.     xb = xb0 + j;
  810.     for(;;) {
  811.         if (*--xa != *--xb)
  812.             return *xa < *xb ? -1 : 1;
  813.         if (xa <= xa0)
  814.             break;
  815.         }
  816.     return 0;
  817.     }
  818.  
  819.  static Bigint *
  820. diff
  821. #ifdef KR_headers
  822.     (a, b) Bigint *a, *b;
  823. #else
  824.     (Bigint *a, Bigint *b)
  825. #endif
  826. {
  827.     Bigint *c;
  828.     int i, wa, wb;
  829.     Long borrow, y;    /* We need signed shifts here. */
  830.     ULong *xa, *xae, *xb, *xbe, *xc;
  831. #ifdef Pack_32
  832.     Long z;
  833. #endif
  834.  
  835.     i = cmp(a,b);
  836.     if (!i) {
  837.         c = Balloc(0);
  838.         c->wds = 1;
  839.         c->x[0] = 0;
  840.         return c;
  841.         }
  842.     if (i < 0) {
  843.         c = a;
  844.         a = b;
  845.         b = c;
  846.         i = 1;
  847.         }
  848.     else
  849.         i = 0;
  850.     c = Balloc(a->k);
  851.     c->sign = i;
  852.     wa = a->wds;
  853.     xa = a->x;
  854.     xae = xa + wa;
  855.     wb = b->wds;
  856.     xb = b->x;
  857.     xbe = xb + wb;
  858.     xc = c->x;
  859.     borrow = 0;
  860. #ifdef Pack_32
  861.     do {
  862.         y = (*xa & 0xffff) - (*xb & 0xffff) + borrow;
  863.         borrow = y >> 16;
  864.         Sign_Extend(borrow, y);
  865.         z = (*xa++ >> 16) - (*xb++ >> 16) + borrow;
  866.         borrow = z >> 16;
  867.         Sign_Extend(borrow, z);
  868.         Storeinc(xc, z, y);
  869.         }
  870.         while(xb < xbe);
  871.     while(xa < xae) {
  872.         y = (*xa & 0xffff) + borrow;
  873.         borrow = y >> 16;
  874.         Sign_Extend(borrow, y);
  875.         z = (*xa++ >> 16) + borrow;
  876.         borrow = z >> 16;
  877.         Sign_Extend(borrow, z);
  878.         Storeinc(xc, z, y);
  879.         }
  880. #else
  881.     do {
  882.         y = *xa++ - *xb++ + borrow;
  883.         borrow = y >> 16;
  884.         Sign_Extend(borrow, y);
  885.         *xc++ = y & 0xffff;
  886.         }
  887.         while(xb < xbe);
  888.     while(xa < xae) {
  889.         y = *xa++ + borrow;
  890.         borrow = y >> 16;
  891.         Sign_Extend(borrow, y);
  892.         *xc++ = y & 0xffff;
  893.         }
  894. #endif
  895.     while(!*--xc)
  896.         wa--;
  897.     c->wds = wa;
  898.     return c;
  899.     }
  900.  
  901.  static double
  902. ulp
  903. #ifdef KR_headers
  904.     (x) double x;
  905. #else
  906.     (double x)
  907. #endif
  908. {
  909.     register Long L;
  910.     double a;
  911.  
  912.     L = (word0(x) & Exp_mask) - (P-1)*Exp_msk1;
  913. #ifndef Sudden_Underflow
  914.     if (L > 0) {
  915. #endif
  916. #ifdef IBM
  917.         L |= Exp_msk1 >> 4;
  918. #endif
  919.         word0(a) = L;
  920.         word1(a) = 0;
  921. #ifndef Sudden_Underflow
  922.         }
  923.     else {
  924.         L = -L >> Exp_shift;
  925.         if (L < Exp_shift) {
  926.             word0(a) = 0x80000 >> L;
  927.             word1(a) = 0;
  928.             }
  929.         else {
  930.             word0(a) = 0;
  931.             L -= Exp_shift;
  932.             word1(a) = L >= 31 ? 1 : 1 << (31 - L);
  933.             }
  934.         }
  935. #endif
  936.     return a;
  937.     }
  938.  
  939.  static double
  940. b2d
  941. #ifdef KR_headers
  942.     (a, e) Bigint *a; int *e;
  943. #else
  944.     (Bigint *a, int *e)
  945. #endif
  946. {
  947.     ULong *xa, *xa0, w, y, z;
  948.     int k;
  949.     double d;
  950. #ifdef VAX
  951.     ULong d0, d1;
  952. #else
  953. #define d0 word0(d)
  954. #define d1 word1(d)
  955. #endif
  956.  
  957.     xa0 = a->x;
  958.     xa = xa0 + a->wds;
  959.     y = *--xa;
  960. #ifdef DEBUG
  961.     if (!y) Bug("zero y in b2d");
  962. #endif
  963.     k = hi0bits(y);
  964.     *e = 32 - k;
  965. #ifdef Pack_32
  966.     if (k < Ebits) {
  967.         d0 = Exp_1 | y >> (Ebits - k);
  968.         w = xa > xa0 ? *--xa : 0;
  969.         d1 = y << ((32-Ebits) + k) | w >> (Ebits - k);
  970.         goto ret_d;
  971.         }
  972.     z = xa > xa0 ? *--xa : 0;
  973.     if (k -= Ebits) {
  974.         d0 = Exp_1 | y << k | z >> (32 - k);
  975.         y = xa > xa0 ? *--xa : 0;
  976.         d1 = z << k | y >> (32 - k);
  977.         }
  978.     else {
  979.         d0 = Exp_1 | y;
  980.         d1 = z;
  981.         }
  982. #else
  983.     if (k < Ebits + 16) {
  984.         z = xa > xa0 ? *--xa : 0;
  985.         d0 = Exp_1 | y << k - Ebits | z >> Ebits + 16 - k;
  986.         w = xa > xa0 ? *--xa : 0;
  987.         y = xa > xa0 ? *--xa : 0;
  988.         d1 = z << k + 16 - Ebits | w << k - Ebits | y >> 16 + Ebits - k;
  989.         goto ret_d;
  990.         }
  991.     z = xa > xa0 ? *--xa : 0;
  992.     w = xa > xa0 ? *--xa : 0;
  993.     k -= Ebits + 16;
  994.     d0 = Exp_1 | y << k + 16 | z << k | w >> 16 - k;
  995.     y = xa > xa0 ? *--xa : 0;
  996.     d1 = w << k + 16 | y << k;
  997. #endif
  998.  ret_d:
  999. #ifdef VAX
  1000.     word0(d) = d0 >> 16 | d0 << 16;
  1001.     word1(d) = d1 >> 16 | d1 << 16;
  1002. #else
  1003. #undef d0
  1004. #undef d1
  1005. #endif
  1006.     return d;
  1007.     }
  1008.  
  1009.  static Bigint *
  1010. d2b
  1011. #ifdef KR_headers
  1012.     (d, e, bits) double d; int *e, *bits;
  1013. #else
  1014.     (double d, int *e, int *bits)
  1015. #endif
  1016. {
  1017.     Bigint *b;
  1018.     int de, i, k;
  1019.     ULong *x, y, z;
  1020. #ifdef VAX
  1021.     ULong d0, d1;
  1022.     d0 = word0(d) >> 16 | word0(d) << 16;
  1023.     d1 = word1(d) >> 16 | word1(d) << 16;
  1024. #else
  1025. #define d0 word0(d)
  1026. #define d1 word1(d)
  1027. #endif
  1028.  
  1029. #ifdef Pack_32
  1030.     b = Balloc(1);
  1031. #else
  1032.     b = Balloc(2);
  1033. #endif
  1034.     x = b->x;
  1035.  
  1036.     z = d0 & Frac_mask;
  1037.     d0 &= 0x7fffffff;    /* clear sign bit, which we ignore */
  1038. #ifdef Sudden_Underflow
  1039.     de = (int)(d0 >> Exp_shift);
  1040. #ifndef IBM
  1041.     z |= Exp_msk11;
  1042. #endif
  1043. #else
  1044.     if ((de = (int)(d0 >> Exp_shift)))
  1045.         z |= Exp_msk1;
  1046. #endif
  1047. #ifdef Pack_32
  1048.     if ((y = d1)) {
  1049.         if ((k = lo0bits(&y))) {
  1050.             x[0] = y | z << (32 - k);
  1051.             z >>= k;
  1052.             }
  1053.         else
  1054.             x[0] = y;
  1055.         i = b->wds = (x[1] = z) ? 2 : 1;
  1056.         }
  1057.     else {
  1058. #ifdef DEBUG
  1059.         if (!z)
  1060.             Bug("Zero passed to d2b");
  1061. #endif
  1062.         k = lo0bits(&z);
  1063.         x[0] = z;
  1064.         i = b->wds = 1;
  1065.         k += 32;
  1066.         }
  1067. #else
  1068.     if (y = d1) {
  1069.         if (k = lo0bits(&y))
  1070.             if (k >= 16) {
  1071.                 x[0] = y | z << 32 - k & 0xffff;
  1072.                 x[1] = z >> k - 16 & 0xffff;
  1073.                 x[2] = z >> k;
  1074.                 i = 2;
  1075.                 }
  1076.             else {
  1077.                 x[0] = y & 0xffff;
  1078.                 x[1] = y >> 16 | z << 16 - k & 0xffff;
  1079.                 x[2] = z >> k & 0xffff;
  1080.                 x[3] = z >> k+16;
  1081.                 i = 3;
  1082.                 }
  1083.         else {
  1084.             x[0] = y & 0xffff;
  1085.             x[1] = y >> 16;
  1086.             x[2] = z & 0xffff;
  1087.             x[3] = z >> 16;
  1088.             i = 3;
  1089.             }
  1090.         }
  1091.     else {
  1092. #ifdef DEBUG
  1093.         if (!z)
  1094.             Bug("Zero passed to d2b");
  1095. #endif
  1096.         k = lo0bits(&z);
  1097.         if (k >= 16) {
  1098.             x[0] = z;
  1099.             i = 0;
  1100.             }
  1101.         else {
  1102.             x[0] = z & 0xffff;
  1103.             x[1] = z >> 16;
  1104.             i = 1;
  1105.             }
  1106.         k += 32;
  1107.         }
  1108.     while(!x[i])
  1109.         --i;
  1110.     b->wds = i + 1;
  1111. #endif
  1112. #ifndef Sudden_Underflow
  1113.     if (de) {
  1114. #endif
  1115. #ifdef IBM
  1116.         *e = (de - Bias - (P-1) << 2) + k;
  1117.         *bits = 4*P + 8 - k - hi0bits(word0(d) & Frac_mask);
  1118. #else
  1119.         *e = de - Bias - (P-1) + k;
  1120.         *bits = P - k;
  1121. #endif
  1122. #ifndef Sudden_Underflow
  1123.         }
  1124.     else {
  1125.         *e = de - Bias - (P-1) + 1 + k;
  1126. #ifdef Pack_32
  1127.         *bits = 32*i - hi0bits(x[i-1]);
  1128. #else
  1129.         *bits = (i+2)*16 - hi0bits(x[i]);
  1130. #endif
  1131.         }
  1132. #endif
  1133.     return b;
  1134.     }
  1135. #undef d0
  1136. #undef d1
  1137.  
  1138.  static double
  1139. ratio
  1140. #ifdef KR_headers
  1141.     (a, b) Bigint *a, *b;
  1142. #else
  1143.     (Bigint *a, Bigint *b)
  1144. #endif
  1145. {
  1146.     double da, db;
  1147.     int k, ka, kb;
  1148.  
  1149.     da = b2d(a, &ka);
  1150.     db = b2d(b, &kb);
  1151. #ifdef Pack_32
  1152.     k = ka - kb + 32*(a->wds - b->wds);
  1153. #else
  1154.     k = ka - kb + 16*(a->wds - b->wds);
  1155. #endif
  1156. #ifdef IBM
  1157.     if (k > 0) {
  1158.         word0(da) += (k >> 2)*Exp_msk1;
  1159.         if (k &= 3)
  1160.             da *= 1 << k;
  1161.         }
  1162.     else {
  1163.         k = -k;
  1164.         word0(db) += (k >> 2)*Exp_msk1;
  1165.         if (k &= 3)
  1166.             db *= 1 << k;
  1167.         }
  1168. #else
  1169.     if (k > 0)
  1170.         word0(da) += k*Exp_msk1;
  1171.     else {
  1172.         k = -k;
  1173.         word0(db) += k*Exp_msk1;
  1174.         }
  1175. #endif
  1176.     return da / db;
  1177.     }
  1178.  
  1179. static CONST double
  1180. tens[] = {
  1181.         1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9,
  1182.         1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19,
  1183.         1e20, 1e21, 1e22
  1184. #ifdef VAX
  1185.         , 1e23, 1e24
  1186. #endif
  1187.         };
  1188.  
  1189. #ifdef IEEE_Arith
  1190. static CONST double bigtens[] = { 1e16, 1e32, 1e64, 1e128, 1e256 };
  1191. static CONST double tinytens[] = { 1e-16, 1e-32, 1e-64, 1e-128, 1e-256 };
  1192. #define n_bigtens 5
  1193. #else
  1194. #ifdef IBM
  1195. static CONST double bigtens[] = { 1e16, 1e32, 1e64 };
  1196. static CONST double tinytens[] = { 1e-16, 1e-32, 1e-64 };
  1197. #define n_bigtens 3
  1198. #else
  1199. static CONST double bigtens[] = { 1e16, 1e32 };
  1200. static CONST double tinytens[] = { 1e-16, 1e-32 };
  1201. #define n_bigtens 2
  1202. #endif
  1203. #endif
  1204.  
  1205.  double
  1206. strtod
  1207. #ifdef KR_headers
  1208.     (s00, se) CONST char *s00; char **se;
  1209. #else
  1210.     (CONST char *s00, char **se)
  1211. #endif
  1212. {
  1213.     int bb2, bb5, bbe, bd2, bd5, bbbits, bs2, c, dsign,
  1214.          e, e1, esign, i, j, k, nd, nd0, nf, nz, nz0, sign;
  1215.     CONST char *s, *s0, *s1;
  1216.     double aadj, aadj1, adj, rv, rv0;
  1217.     Long L;
  1218.     ULong y, z;
  1219.     Bigint *bb = 0, *bb1, *bd = 0, *bd0, *bs = 0, *delta = 0;
  1220.  
  1221. #ifndef KR_headers
  1222.     CONST char decimal_point = localeconv()->decimal_point[0];
  1223. #else
  1224.     CONST char decimal_point = '.';
  1225. #endif
  1226.  
  1227.     sign = nz0 = nz = 0;
  1228.     rv = 0.;
  1229.  
  1230.  
  1231.     for(s = s00; isspace(*s); s++)
  1232.         ;
  1233.  
  1234.     if (*s == '-') {
  1235.         sign = 1;
  1236.         s++;
  1237.     } else if (*s == '+') {
  1238.         s++;
  1239.     }
  1240.  
  1241.     if (*s == '\0') {
  1242.         s = s00;
  1243.         goto ret;
  1244.     }
  1245.  
  1246.     if (*s == '0') {
  1247.         nz0 = 1;
  1248.         while(*++s == '0') ;
  1249.         if (!*s)
  1250.             goto ret;
  1251.         }
  1252.     s0 = s;
  1253.     y = z = 0;
  1254.     for(nd = nf = 0; (c = *s) >= '0' && c <= '9'; nd++, s++)
  1255.         if (nd < 9)
  1256.             y = 10*y + c - '0';
  1257.         else if (nd < 16)
  1258.             z = 10*z + c - '0';
  1259.     nd0 = nd;
  1260.     if (c == decimal_point) {
  1261.         c = *++s;
  1262.         if (!nd) {
  1263.             for(; c == '0'; c = *++s)
  1264.                 nz++;
  1265.             if (c > '0' && c <= '9') {
  1266.                 s0 = s;
  1267.                 nf += nz;
  1268.                 nz = 0;
  1269.                 goto have_dig;
  1270.                 }
  1271.             goto dig_done;
  1272.             }
  1273.         for(; c >= '0' && c <= '9'; c = *++s) {
  1274.  have_dig:
  1275.             nz++;
  1276.             if (c -= '0') {
  1277.                 nf += nz;
  1278.                 for(i = 1; i < nz; i++)
  1279.                     if (nd++ < 9)
  1280.                         y *= 10;
  1281.                     else if (nd <= DBL_DIG + 1)
  1282.                         z *= 10;
  1283.                 if (nd++ < 9)
  1284.                     y = 10*y + c;
  1285.                 else if (nd <= DBL_DIG + 1)
  1286.                     z = 10*z + c;
  1287.                 nz = 0;
  1288.                 }
  1289.             }
  1290.         }
  1291.  dig_done:
  1292.     e = 0;
  1293.     if (c == 'e' || c == 'E') {
  1294.         if (!nd && !nz && !nz0) {
  1295.             s = s00;
  1296.             goto ret;
  1297.             }
  1298.         s00 = s;
  1299.         esign = 0;
  1300.         switch(c = *++s) {
  1301.             case '-':
  1302.                 esign = 1;
  1303.             case '+':
  1304.                 c = *++s;
  1305.             }
  1306.         if (c >= '0' && c <= '9') {
  1307.             while(c == '0')
  1308.                 c = *++s;
  1309.             if (c > '0' && c <= '9') {
  1310.                 L = c - '0';
  1311.                 s1 = s;
  1312.                 while((c = *++s) >= '0' && c <= '9')
  1313.                     L = 10*L + c - '0';
  1314.                 if (s - s1 > 8 || L > 19999)
  1315.                     /* Avoid confusion from exponents
  1316.                      * so large that e might overflow.
  1317.                      */
  1318.                     e = 19999; /* safe for 16 bit ints */
  1319.                 else
  1320.                     e = (int)L;
  1321.                 if (esign)
  1322.                     e = -e;
  1323.                 }
  1324.             else
  1325.                 e = 0;
  1326.             }
  1327.         else
  1328.             s = s00;
  1329.         }
  1330.     if (!nd) {
  1331.         if (!nz && !nz0)
  1332.             s = s00;
  1333.         goto ret;
  1334.         }
  1335.     e1 = e -= nf;
  1336.  
  1337.     /* Now we have nd0 digits, starting at s0, followed by a
  1338.      * decimal point, followed by nd-nd0 digits.  The number we're
  1339.      * after is the integer represented by those digits times
  1340.      * 10**e */
  1341.  
  1342.     if (!nd0)
  1343.         nd0 = nd;
  1344.     k = nd < DBL_DIG + 1 ? nd : DBL_DIG + 1;
  1345.     rv = y;
  1346.     if (k > 9)
  1347.         rv = tens[k - 9] * rv + z;
  1348.     bd0 = 0;
  1349.     if (nd <= DBL_DIG
  1350. #ifndef RND_PRODQUOT
  1351.         && FLT_ROUNDS == 1
  1352. #endif
  1353.             ) {
  1354.         if (!e)
  1355.             goto ret;
  1356.         if (e > 0) {
  1357.             if (e <= Ten_pmax) {
  1358. #ifdef VAX
  1359.                 goto vax_ovfl_check;
  1360. #else
  1361.                 /* rv = */ rounded_product(rv, tens[e]);
  1362.                 goto ret;
  1363. #endif
  1364.                 }
  1365.             i = DBL_DIG - nd;
  1366.             if (e <= Ten_pmax + i) {
  1367.                 /* A fancier test would sometimes let us do
  1368.                  * this for larger i values.
  1369.                  */
  1370.                 e -= i;
  1371.                 rv *= tens[i];
  1372. #ifdef VAX
  1373.                 /* VAX exponent range is so narrow we must
  1374.                  * worry about overflow here...
  1375.                  */
  1376.  vax_ovfl_check:
  1377.                 word0(rv) -= P*Exp_msk1;
  1378.                 /* rv = */ rounded_product(rv, tens[e]);
  1379.                 if ((word0(rv) & Exp_mask)
  1380.                  > Exp_msk1*(DBL_MAX_EXP+Bias-1-P))
  1381.                     goto ovfl;
  1382.                 word0(rv) += P*Exp_msk1;
  1383. #else
  1384.                 /* rv = */ rounded_product(rv, tens[e]);
  1385. #endif
  1386.                 goto ret;
  1387.                 }
  1388.             }
  1389. #ifndef Inaccurate_Divide
  1390.         else if (e >= -Ten_pmax) {
  1391.             /* rv = */ rounded_quotient(rv, tens[-e]);
  1392.             goto ret;
  1393.             }
  1394. #endif
  1395.         }
  1396.     e1 += nd - k;
  1397.  
  1398.     /* Get starting approximation = rv * 10**e1 */
  1399.  
  1400.     if (e1 > 0) {
  1401.         if ((i = e1 & 15))
  1402.             rv *= tens[i];
  1403.         if (e1 &= ~15) {
  1404.             if (e1 > DBL_MAX_10_EXP) {
  1405.  ovfl:
  1406.                 errno = ERANGE;
  1407. #ifdef __STDC__
  1408.                 rv = HUGE_VAL;
  1409. #else
  1410.                 /* Can't trust HUGE_VAL */
  1411. #ifdef IEEE_Arith
  1412.                 word0(rv) = Exp_mask;
  1413.                 word1(rv) = 0;
  1414. #else
  1415.                 word0(rv) = Big0;
  1416.                 word1(rv) = Big1;
  1417. #endif
  1418. #endif
  1419.                 if (bd0)
  1420.                     goto retfree;
  1421.                 goto ret;
  1422.                 }
  1423.             if (e1 >>= 4) {
  1424.                 for(j = 0; e1 > 1; j++, e1 >>= 1)
  1425.                     if (e1 & 1)
  1426.                         rv *= bigtens[j];
  1427.             /* The last multiplication could overflow. */
  1428.                 word0(rv) -= P*Exp_msk1;
  1429.                 rv *= bigtens[j];
  1430.                 if ((z = word0(rv) & Exp_mask)
  1431.                  > Exp_msk1*(DBL_MAX_EXP+Bias-P))
  1432.                     goto ovfl;
  1433.                 if (z > Exp_msk1*(DBL_MAX_EXP+Bias-1-P)) {
  1434.                     /* set to largest number */
  1435.                     /* (Can't trust DBL_MAX) */
  1436.                     word0(rv) = Big0;
  1437.                     word1(rv) = Big1;
  1438.                     }
  1439.                 else
  1440.                     word0(rv) += P*Exp_msk1;
  1441.                 }
  1442.  
  1443.             }
  1444.         }
  1445.     else if (e1 < 0) {
  1446.         e1 = -e1;
  1447.         if ((i = e1 & 15))
  1448.             rv /= tens[i];
  1449.         if (e1 &= ~15) {
  1450.             e1 >>= 4;
  1451.             if (e1 >= 1 << n_bigtens)
  1452.                 goto undfl;
  1453.             for(j = 0; e1 > 1; j++, e1 >>= 1)
  1454.                 if (e1 & 1)
  1455.                     rv *= tinytens[j];
  1456.             /* The last multiplication could underflow. */
  1457.             rv0 = rv;
  1458.             rv *= tinytens[j];
  1459.             if (!rv) {
  1460.                 rv = 2.*rv0;
  1461.                 rv *= tinytens[j];
  1462.                 if (!rv) {
  1463.  undfl:
  1464.                     rv = 0.;
  1465.                     errno = ERANGE;
  1466.                     if (bd0)
  1467.                         goto retfree;
  1468.                     goto ret;
  1469.                     }
  1470.                 word0(rv) = Tiny0;
  1471.                 word1(rv) = Tiny1;
  1472.                 /* The refinement below will clean
  1473.                  * this approximation up.
  1474.                  */
  1475.                 }
  1476.             }
  1477.         }
  1478.  
  1479.     /* Now the hard part -- adjusting rv to the correct value.*/
  1480.  
  1481.     /* Put digits into bd: true value = bd * 10^e */
  1482.  
  1483.     bd0 = s2b(s0, nd0, nd, y);
  1484.  
  1485.     for(;;) {
  1486.         bd = Balloc(bd0->k);
  1487.         Bcopy(bd, bd0);
  1488.         bb = d2b(rv, &bbe, &bbbits);    /* rv = bb * 2^bbe */
  1489.         bs = i2b(1);
  1490.  
  1491.         if (e >= 0) {
  1492.             bb2 = bb5 = 0;
  1493.             bd2 = bd5 = e;
  1494.             }
  1495.         else {
  1496.             bb2 = bb5 = -e;
  1497.             bd2 = bd5 = 0;
  1498.             }
  1499.         if (bbe >= 0)
  1500.             bb2 += bbe;
  1501.         else
  1502.             bd2 -= bbe;
  1503.         bs2 = bb2;
  1504. #ifdef Sudden_Underflow
  1505. #ifdef IBM
  1506.         j = 1 + 4*P - 3 - bbbits + ((bbe + bbbits - 1) & 3);
  1507. #else
  1508.         j = P + 1 - bbbits;
  1509. #endif
  1510. #else
  1511.         i = bbe + bbbits - 1;    /* logb(rv) */
  1512.         if (i < Emin)    /* denormal */
  1513.             j = bbe + (P-Emin);
  1514.         else
  1515.             j = P + 1 - bbbits;
  1516. #endif
  1517.         bb2 += j;
  1518.         bd2 += j;
  1519.         i = bb2 < bd2 ? bb2 : bd2;
  1520.         if (i > bs2)
  1521.             i = bs2;
  1522.         if (i > 0) {
  1523.             bb2 -= i;
  1524.             bd2 -= i;
  1525.             bs2 -= i;
  1526.             }
  1527.         if (bb5 > 0) {
  1528.             bs = pow5mult(bs, bb5);
  1529.             bb1 = mult(bs, bb);
  1530.             Bfree(bb);
  1531.             bb = bb1;
  1532.             }
  1533.         if (bb2 > 0)
  1534.             bb = lshift(bb, bb2);
  1535.         if (bd5 > 0)
  1536.             bd = pow5mult(bd, bd5);
  1537.         if (bd2 > 0)
  1538.             bd = lshift(bd, bd2);
  1539.         if (bs2 > 0)
  1540.             bs = lshift(bs, bs2);
  1541.         delta = diff(bb, bd);
  1542.         dsign = delta->sign;
  1543.         delta->sign = 0;
  1544.         i = cmp(delta, bs);
  1545.         if (i < 0) {
  1546.             /* Error is less than half an ulp -- check for
  1547.              * special case of mantissa a power of two.
  1548.              */
  1549.             if (dsign || word1(rv) || word0(rv) & Bndry_mask)
  1550.                 break;
  1551.             delta = lshift(delta,Log2P);
  1552.             if (cmp(delta, bs) > 0)
  1553.                 goto drop_down;
  1554.             break;
  1555.             }
  1556.         if (i == 0) {
  1557.             /* exactly half-way between */
  1558.             if (dsign) {
  1559.                 if ((word0(rv) & Bndry_mask1) == Bndry_mask1
  1560.                  &&  word1(rv) == 0xffffffff) {
  1561.                     /*boundary case -- increment exponent*/
  1562.                     word0(rv) = (word0(rv) & Exp_mask)
  1563.                         + Exp_msk1
  1564. #ifdef IBM
  1565.                         | Exp_msk1 >> 4
  1566. #endif
  1567.                         ;
  1568.                     word1(rv) = 0;
  1569.                     break;
  1570.                     }
  1571.                 }
  1572.             else if (!(word0(rv) & Bndry_mask) && !word1(rv)) {
  1573.  drop_down:
  1574.                 /* boundary case -- decrement exponent */
  1575. #ifdef Sudden_Underflow
  1576.                 L = word0(rv) & Exp_mask;
  1577. #ifdef IBM
  1578.                 if (L <  Exp_msk1)
  1579. #else
  1580.                 if (L <= Exp_msk1)
  1581. #endif
  1582.                     goto undfl;
  1583.                 L -= Exp_msk1;
  1584. #else
  1585.                 L = (word0(rv) & Exp_mask) - Exp_msk1;
  1586. #endif
  1587.                 word0(rv) = L | Bndry_mask1;
  1588.                 word1(rv) = 0xffffffff;
  1589. #ifdef IBM
  1590.                 goto cont;
  1591. #else
  1592.                 break;
  1593. #endif
  1594.                 }
  1595. #ifndef ROUND_BIASED
  1596.             if (!(word1(rv) & LSB))
  1597.                 break;
  1598. #endif
  1599.             if (dsign)
  1600.                 rv += ulp(rv);
  1601. #ifndef ROUND_BIASED
  1602.             else {
  1603.                 rv -= ulp(rv);
  1604. #ifndef Sudden_Underflow
  1605.                 if (!rv)
  1606.                     goto undfl;
  1607. #endif
  1608.                 }
  1609. #endif
  1610.             break;
  1611.             }
  1612.         if ((aadj = ratio(delta, bs)) <= 2.) {
  1613.             if (dsign)
  1614.                 aadj = aadj1 = 1.;
  1615.             else if (word1(rv) || word0(rv) & Bndry_mask) {
  1616. #ifndef Sudden_Underflow
  1617.                 if (word1(rv) == Tiny1 && !word0(rv))
  1618.                     goto undfl;
  1619. #endif
  1620.                 aadj = 1.;
  1621.                 aadj1 = -1.;
  1622.                 }
  1623.             else {
  1624.                 /* special case -- power of FLT_RADIX to be */
  1625.                 /* rounded down... */
  1626.  
  1627.                 if (aadj < 2./FLT_RADIX)
  1628.                     aadj = 1./FLT_RADIX;
  1629.                 else
  1630.                     aadj *= 0.5;
  1631.                 aadj1 = -aadj;
  1632.                 }
  1633.             }
  1634.         else {
  1635.             aadj *= 0.5;
  1636.             aadj1 = dsign ? aadj : -aadj;
  1637. #ifdef Check_FLT_ROUNDS
  1638.             switch(FLT_ROUNDS) {
  1639.                 case 2: /* towards +infinity */
  1640.                     aadj1 -= 0.5;
  1641.                     break;
  1642.                 case 0: /* towards 0 */
  1643.                 case 3: /* towards -infinity */
  1644.                     aadj1 += 0.5;
  1645.                 }
  1646. #else
  1647.             if (FLT_ROUNDS == 0)
  1648.                 aadj1 += 0.5;
  1649. #endif
  1650.             }
  1651.         y = word0(rv) & Exp_mask;
  1652.  
  1653.         /* Check for overflow */
  1654.  
  1655.         if (y == Exp_msk1*(DBL_MAX_EXP+Bias-1)) {
  1656.             rv0 = rv;
  1657.             word0(rv) -= P*Exp_msk1;
  1658.             adj = aadj1 * ulp(rv);
  1659.             rv += adj;
  1660.             if ((word0(rv) & Exp_mask) >=
  1661.                     Exp_msk1*(DBL_MAX_EXP+Bias-P)) {
  1662.                 if (word0(rv0) == Big0 && word1(rv0) == Big1)
  1663.                     goto ovfl;
  1664.                 word0(rv) = Big0;
  1665.                 word1(rv) = Big1;
  1666.                 goto cont;
  1667.                 }
  1668.             else
  1669.                 word0(rv) += P*Exp_msk1;
  1670.             }
  1671.         else {
  1672. #ifdef Sudden_Underflow
  1673.             if ((word0(rv) & Exp_mask) <= P*Exp_msk1) {
  1674.                 rv0 = rv;
  1675.                 word0(rv) += P*Exp_msk1;
  1676.                 adj = aadj1 * ulp(rv);
  1677.                 rv += adj;
  1678. #ifdef IBM
  1679.                 if ((word0(rv) & Exp_mask) <  P*Exp_msk1)
  1680. #else
  1681.                 if ((word0(rv) & Exp_mask) <= P*Exp_msk1)
  1682. #endif
  1683.                     {
  1684.                     if (word0(rv0) == Tiny0
  1685.                      && word1(rv0) == Tiny1)
  1686.                         goto undfl;
  1687.                     word0(rv) = Tiny0;
  1688.                     word1(rv) = Tiny1;
  1689.                     goto cont;
  1690.                     }
  1691.                 else
  1692.                     word0(rv) -= P*Exp_msk1;
  1693.                 }
  1694.             else {
  1695.                 adj = aadj1 * ulp(rv);
  1696.                 rv += adj;
  1697.                 }
  1698. #else
  1699.             /* Compute adj so that the IEEE rounding rules will
  1700.              * correctly round rv + adj in some half-way cases.
  1701.              * If rv * ulp(rv) is denormalized (i.e.,
  1702.              * y <= (P-1)*Exp_msk1), we must adjust aadj to avoid
  1703.              * trouble from bits lost to denormalization;
  1704.              * example: 1.2e-307 .
  1705.              */
  1706.             if (y <= (P-1)*Exp_msk1 && aadj >= 1.) {
  1707.                 aadj1 = (double)(int)(aadj + 0.5);
  1708.                 if (!dsign)
  1709.                     aadj1 = -aadj1;
  1710.                 }
  1711.             adj = aadj1 * ulp(rv);
  1712.             rv += adj;
  1713. #endif
  1714.             }
  1715.         z = word0(rv) & Exp_mask;
  1716.         if (y == z) {
  1717.             /* Can we stop now? */
  1718.             L = aadj;
  1719.             aadj -= L;
  1720.             /* The tolerances below are conservative. */
  1721.             if (dsign || word1(rv) || word0(rv) & Bndry_mask) {
  1722.                 if (aadj < .4999999 || aadj > .5000001)
  1723.                     break;
  1724.                 }
  1725.             else if (aadj < .4999999/FLT_RADIX)
  1726.                 break;
  1727.             }
  1728.  cont:
  1729.         Bfree(bb);
  1730.         Bfree(bd);
  1731.         Bfree(bs);
  1732.         Bfree(delta);
  1733.         }
  1734.  retfree:
  1735.     Bfree(bb);
  1736.     Bfree(bd);
  1737.     Bfree(bs);
  1738.     Bfree(bd0);
  1739.     Bfree(delta);
  1740.  ret:
  1741.     if (se)
  1742.         *se = (char *)s;
  1743.     return sign ? -rv : rv;
  1744.     }
  1745.  
  1746.  static int
  1747. quorem
  1748. #ifdef KR_headers
  1749.     (b, S) Bigint *b, *S;
  1750. #else
  1751.     (Bigint *b, Bigint *S)
  1752. #endif
  1753. {
  1754.     int n;
  1755.     Long borrow, y;
  1756.     ULong carry, q, ys;
  1757.     ULong *bx, *bxe, *sx, *sxe;
  1758. #ifdef Pack_32
  1759.     Long z;
  1760.     ULong si, zs;
  1761. #endif
  1762.  
  1763.     n = S->wds;
  1764. #ifdef DEBUG
  1765.     /*debug*/ if (b->wds > n)
  1766.     /*debug*/    Bug("oversize b in quorem");
  1767. #endif
  1768.     if (b->wds < n)
  1769.         return 0;
  1770.     sx = S->x;
  1771.     sxe = sx + --n;
  1772.     bx = b->x;
  1773.     bxe = bx + n;
  1774.     q = *bxe / (*sxe + 1);    /* ensure q <= true quotient */
  1775. #ifdef DEBUG
  1776.     /*debug*/ if (q > 9)
  1777.     /*debug*/    Bug("oversized quotient in quorem");
  1778. #endif
  1779.     if (q) {
  1780.         borrow = 0;
  1781.         carry = 0;
  1782.         do {
  1783. #ifdef Pack_32
  1784.             si = *sx++;
  1785.             ys = (si & 0xffff) * q + carry;
  1786.             zs = (si >> 16) * q + (ys >> 16);
  1787.             carry = zs >> 16;
  1788.             y = (*bx & 0xffff) - (ys & 0xffff) + borrow;
  1789.             borrow = y >> 16;
  1790.             Sign_Extend(borrow, y);
  1791.             z = (*bx >> 16) - (zs & 0xffff) + borrow;
  1792.             borrow = z >> 16;
  1793.             Sign_Extend(borrow, z);
  1794.             Storeinc(bx, z, y);
  1795. #else
  1796.             ys = *sx++ * q + carry;
  1797.             carry = ys >> 16;
  1798.             y = *bx - (ys & 0xffff) + borrow;
  1799.             borrow = y >> 16;
  1800.             Sign_Extend(borrow, y);
  1801.             *bx++ = y & 0xffff;
  1802. #endif
  1803.             }
  1804.             while(sx <= sxe);
  1805.         if (!*bxe) {
  1806.             bx = b->x;
  1807.             while(--bxe > bx && !*bxe)
  1808.                 --n;
  1809.             b->wds = n;
  1810.             }
  1811.         }
  1812.     if (cmp(b, S) >= 0) {
  1813.         q++;
  1814.         borrow = 0;
  1815.         carry = 0;
  1816.         bx = b->x;
  1817.         sx = S->x;
  1818.         do {
  1819. #ifdef Pack_32
  1820.             si = *sx++;
  1821.             ys = (si & 0xffff) + carry;
  1822.             zs = (si >> 16) + (ys >> 16);
  1823.             carry = zs >> 16;
  1824.             y = (*bx & 0xffff) - (ys & 0xffff) + borrow;
  1825.             borrow = y >> 16;
  1826.             Sign_Extend(borrow, y);
  1827.             z = (*bx >> 16) - (zs & 0xffff) + borrow;
  1828.             borrow = z >> 16;
  1829.             Sign_Extend(borrow, z);
  1830.             Storeinc(bx, z, y);
  1831. #else
  1832.             ys = *sx++ + carry;
  1833.             carry = ys >> 16;
  1834.             y = *bx - (ys & 0xffff) + borrow;
  1835.             borrow = y >> 16;
  1836.             Sign_Extend(borrow, y);
  1837.             *bx++ = y & 0xffff;
  1838. #endif
  1839.             }
  1840.             while(sx <= sxe);
  1841.         bx = b->x;
  1842.         bxe = bx + n;
  1843.         if (!*bxe) {
  1844.             while(--bxe > bx && !*bxe)
  1845.                 --n;
  1846.             b->wds = n;
  1847.             }
  1848.         }
  1849.     return q;
  1850.     }
  1851.  
  1852. /* dtoa for IEEE arithmetic (dmg): convert double to ASCII string.
  1853.  *
  1854.  * Inspired by "How to Print Floating-Point Numbers Accurately" by
  1855.  * Guy L. Steele, Jr. and Jon L. White [Proc. ACM SIGPLAN '90, pp. 92-101].
  1856.  *
  1857.  * Modifications:
  1858.  *    1. Rather than iterating, we use a simple numeric overestimate
  1859.  *       to determine k = floor(log10(d)).  We scale relevant
  1860.  *       quantities using O(log2(k)) rather than O(k) multiplications.
  1861.  *    2. For some modes > 2 (corresponding to ecvt and fcvt), we don't
  1862.  *       try to generate digits strictly left to right.  Instead, we
  1863.  *       compute with fewer bits and propagate the carry if necessary
  1864.  *       when rounding the final digit up.  This is often faster.
  1865.  *    3. Under the assumption that input will be rounded nearest,
  1866.  *       mode 0 renders 1e23 as 1e23 rather than 9.999999999999999e22.
  1867.  *       That is, we allow equality in stopping tests when the
  1868.  *       round-nearest rule will give the same floating-point value
  1869.  *       as would satisfaction of the stopping test with strict
  1870.  *       inequality.
  1871.  *    4. We remove common factors of powers of 2 from relevant
  1872.  *       quantities.
  1873.  *    5. When converting floating-point integers less than 1e16,
  1874.  *       we use floating-point arithmetic rather than resorting
  1875.  *       to multiple-precision integers.
  1876.  *    6. When asked to produce fewer than 15 digits, we first try
  1877.  *       to get by with floating-point arithmetic; we resort to
  1878.  *       multiple-precision integer arithmetic only if we cannot
  1879.  *       guarantee that the floating-point calculation has given
  1880.  *       the correctly rounded result.  For k requested digits and
  1881.  *       "uniformly" distributed input, the probability is
  1882.  *       something like 10^(k-15) that we must resort to the Long
  1883.  *       calculation.
  1884.  */
  1885.  
  1886.  char *
  1887. __dtoa
  1888. #ifdef KR_headers
  1889.     (d, mode, ndigits, decpt, sign, rve)
  1890.     double d; int mode, ndigits, *decpt, *sign; char **rve;
  1891. #else
  1892.     (double d, int mode, int ndigits, int *decpt, int *sign, char **rve)
  1893. #endif
  1894. {
  1895.  /*    Arguments ndigits, decpt, sign are similar to those
  1896.     of ecvt and fcvt; trailing zeros are suppressed from
  1897.     the returned string.  If not null, *rve is set to point
  1898.     to the end of the return value.  If d is +-Infinity or NaN,
  1899.     then *decpt is set to 9999.
  1900.  
  1901.     mode:
  1902.         0 ==> shortest string that yields d when read in
  1903.             and rounded to nearest.
  1904.         1 ==> like 0, but with Steele & White stopping rule;
  1905.             e.g. with IEEE P754 arithmetic , mode 0 gives
  1906.             1e23 whereas mode 1 gives 9.999999999999999e22.
  1907.         2 ==> max(1,ndigits) significant digits.  This gives a
  1908.             return value similar to that of ecvt, except
  1909.             that trailing zeros are suppressed.
  1910.         3 ==> through ndigits past the decimal point.  This
  1911.             gives a return value similar to that from fcvt,
  1912.             except that trailing zeros are suppressed, and
  1913.             ndigits can be negative.
  1914.         4-9 should give the same return values as 2-3, i.e.,
  1915.             4 <= mode <= 9 ==> same return as mode
  1916.             2 + (mode & 1).  These modes are mainly for
  1917.             debugging; often they run slower but sometimes
  1918.             faster than modes 2-3.
  1919.         4,5,8,9 ==> left-to-right digit generation.
  1920.         6-9 ==> don't try fast floating-point estimate
  1921.             (if applicable).
  1922.  
  1923.         Values of mode other than 0-9 are treated as mode 0.
  1924.  
  1925.         Sufficient space is allocated to the return value
  1926.         to hold the suppressed trailing zeros.
  1927.     */
  1928.  
  1929.     int bbits, b2, b5, be, dig, i, ieps, ilim = 0, ilim0, ilim1 = 0,
  1930.         j, j1, k, k0, k_check, leftright, m2, m5, s2, s5,
  1931.         spec_case = 0, try_quick;
  1932.     Long L;
  1933. #ifndef Sudden_Underflow
  1934.     int denorm;
  1935.     ULong x;
  1936. #endif
  1937.     Bigint *b, *b1, *delta, *mlo = 0, *mhi, *S;
  1938.     double d2, ds, eps;
  1939.     char *s, *s0;
  1940.  
  1941.     if (u.u_result) {
  1942.         ((Bigint *)u.u_result)->k = u.u_result_k;
  1943.         ((Bigint *)u.u_result)->maxwds = 1 << u.u_result_k;
  1944.         Bfree(u.u_result);
  1945.         u.u_result = 0;
  1946.         }
  1947.     if (word0(d) & Sign_bit) {
  1948.         /* set sign for everything, including 0's and NaNs */
  1949.         *sign = 1;
  1950.         word0(d) &= ~Sign_bit;    /* clear sign bit */
  1951.         }
  1952.     else
  1953.         *sign = 0;
  1954.  
  1955. #if defined(IEEE_Arith) + defined(VAX)
  1956. #ifdef IEEE_Arith
  1957.     if ((word0(d) & Exp_mask) == Exp_mask)
  1958. #else
  1959.     if (word0(d)  == 0x8000)
  1960. #endif
  1961.         {
  1962.         /* Infinity or NaN */
  1963.         *decpt = 9999;
  1964.         s =
  1965. #ifdef IEEE_Arith
  1966.             !word1(d) && !(word0(d) & 0xfffff) ? "Infinity" :
  1967. #endif
  1968.                 "NaN";
  1969.         if (rve)
  1970.             *rve =
  1971. #ifdef IEEE_Arith
  1972.                 s[3] ? s + 8 :
  1973. #endif
  1974.                         s + 3;
  1975.         return s;
  1976.         }
  1977. #endif
  1978. #ifdef IBM
  1979.     d += 0; /* normalize */
  1980. #endif
  1981.     if (!d) {
  1982.         *decpt = 1;
  1983.         s = "0";
  1984.         if (rve)
  1985.             *rve = s + 1;
  1986.         return s;
  1987.         }
  1988.  
  1989.     b = d2b(d, &be, &bbits);
  1990. #ifdef Sudden_Underflow
  1991.     i = (int)(word0(d) >> Exp_shift1 & (Exp_mask>>Exp_shift1));
  1992. #else
  1993.     if ((i = (int)(word0(d) >> Exp_shift1 & (Exp_mask>>Exp_shift1)))) {
  1994. #endif
  1995.         d2 = d;
  1996.         word0(d2) &= Frac_mask1;
  1997.         word0(d2) |= Exp_11;
  1998. #ifdef IBM
  1999.         if (j = 11 - hi0bits(word0(d2) & Frac_mask))
  2000.             d2 /= 1 << j;
  2001. #endif
  2002.  
  2003.         /* log(x)    ~=~ log(1.5) + (x-1.5)/1.5
  2004.          * log10(x)     =  log(x) / log(10)
  2005.          *        ~=~ log(1.5)/log(10) + (x-1.5)/(1.5*log(10))
  2006.          * log10(d) = (i-Bias)*log(2)/log(10) + log10(d2)
  2007.          *
  2008.          * This suggests computing an approximation k to log10(d) by
  2009.          *
  2010.          * k = (i - Bias)*0.301029995663981
  2011.          *    + ( (d2-1.5)*0.289529654602168 + 0.176091259055681 );
  2012.          *
  2013.          * We want k to be too large rather than too small.
  2014.          * The error in the first-order Taylor series approximation
  2015.          * is in our favor, so we just round up the constant enough
  2016.          * to compensate for any error in the multiplication of
  2017.          * (i - Bias) by 0.301029995663981; since |i - Bias| <= 1077,
  2018.          * and 1077 * 0.30103 * 2^-52 ~=~ 7.2e-14,
  2019.          * adding 1e-13 to the constant term more than suffices.
  2020.          * Hence we adjust the constant term to 0.1760912590558.
  2021.          * (We could get a more accurate k by invoking log10,
  2022.          *  but this is probably not worthwhile.)
  2023.          */
  2024.  
  2025.         i -= Bias;
  2026. #ifdef IBM
  2027.         i <<= 2;
  2028.         i += j;
  2029. #endif
  2030. #ifndef Sudden_Underflow
  2031.         denorm = 0;
  2032.         }
  2033.     else {
  2034.         /* d is denormalized */
  2035.  
  2036.         i = bbits + be + (Bias + (P-1) - 1);
  2037.         x = i > 32  ? word0(d) << (64 - i) | word1(d) >> (i - 32)
  2038.                 : word1(d) << (32 - i);
  2039.         d2 = x;
  2040.         word0(d2) -= 31*Exp_msk1; /* adjust exponent */
  2041.         i -= (Bias + (P-1) - 1) + 1;
  2042.         denorm = 1;
  2043.         }
  2044. #endif
  2045.     ds = (d2-1.5)*0.289529654602168 + 0.1760912590558 + i*0.301029995663981;
  2046.     k = (int)ds;
  2047.     if (ds < 0. && ds != k)
  2048.         k--;    /* want k = floor(ds) */
  2049.     k_check = 1;
  2050.     if (k >= 0 && k <= Ten_pmax) {
  2051.         if (d < tens[k])
  2052.             k--;
  2053.         k_check = 0;
  2054.         }
  2055.     j = bbits - i - 1;
  2056.     if (j >= 0) {
  2057.         b2 = 0;
  2058.         s2 = j;
  2059.         }
  2060.     else {
  2061.         b2 = -j;
  2062.         s2 = 0;
  2063.         }
  2064.     if (k >= 0) {
  2065.         b5 = 0;
  2066.         s5 = k;
  2067.         s2 += k;
  2068.         }
  2069.     else {
  2070.         b2 -= k;
  2071.         b5 = -k;
  2072.         s5 = 0;
  2073.         }
  2074.     if (mode < 0 || mode > 9)
  2075.         mode = 0;
  2076.     try_quick = 1;
  2077.     if (mode > 5) {
  2078.         mode -= 4;
  2079.         try_quick = 0;
  2080.         }
  2081.     leftright = 1;
  2082.     switch(mode) {
  2083.         case 0:
  2084.         case 1:
  2085.             ilim = ilim1 = -1;
  2086.             i = 18;
  2087.             ndigits = 0;
  2088.             break;
  2089.         case 2:
  2090.             leftright = 0;
  2091.             /* no break */
  2092.         case 4:
  2093.             if (ndigits <= 0)
  2094.                 ndigits = 1;
  2095.             ilim = ilim1 = i = ndigits;
  2096.             break;
  2097.         case 3:
  2098.             leftright = 0;
  2099.             /* no break */
  2100.         case 5:
  2101.             i = ndigits + k + 1;
  2102.             ilim = i;
  2103.             ilim1 = i - 1;
  2104.             if (i <= 0)
  2105.                 i = 1;
  2106.         }
  2107.     j = sizeof(ULong);
  2108.     for(u.u_result_k = 0; sizeof(Bigint) - sizeof(ULong) + j <= i;
  2109.         j <<= 1) u.u_result_k++;
  2110.     u.u_result = Balloc(u.u_result_k);
  2111.     s = s0 = (char *)u.u_result;
  2112.  
  2113.     if (ilim >= 0 && ilim <= Quick_max && try_quick) {
  2114.  
  2115.         /* Try to get by with floating-point arithmetic. */
  2116.  
  2117.         i = 0;
  2118.         d2 = d;
  2119.         k0 = k;
  2120.         ilim0 = ilim;
  2121.         ieps = 2; /* conservative */
  2122.         if (k > 0) {
  2123.             ds = tens[k&0xf];
  2124.             j = k >> 4;
  2125.             if (j & Bletch) {
  2126.                 /* prevent overflows */
  2127.                 j &= Bletch - 1;
  2128.                 d /= bigtens[n_bigtens-1];
  2129.                 ieps++;
  2130.                 }
  2131.             for(; j; j >>= 1, i++)
  2132.                 if (j & 1) {
  2133.                     ieps++;
  2134.                     ds *= bigtens[i];
  2135.                     }
  2136.             d /= ds;
  2137.             }
  2138.         else if ((j1 = -k)) {
  2139.             d *= tens[j1 & 0xf];
  2140.             for(j = j1 >> 4; j; j >>= 1, i++)
  2141.                 if (j & 1) {
  2142.                     ieps++;
  2143.                     d *= bigtens[i];
  2144.                     }
  2145.             }
  2146.         if (k_check && d < 1. && ilim > 0) {
  2147.             if (ilim1 <= 0)
  2148.                 goto fast_failed;
  2149.             ilim = ilim1;
  2150.             k--;
  2151.             d *= 10.;
  2152.             ieps++;
  2153.             }
  2154.         eps = ieps*d + 7.;
  2155.         word0(eps) -= (P-1)*Exp_msk1;
  2156.         if (ilim == 0) {
  2157.             S = mhi = 0;
  2158.             d -= 5.;
  2159.             if (d > eps)
  2160.                 goto one_digit;
  2161.             if (d < -eps)
  2162.                 goto no_digits;
  2163.             goto fast_failed;
  2164.             }
  2165. #ifndef No_leftright
  2166.         if (leftright) {
  2167.             /* Use Steele & White method of only
  2168.              * generating digits needed.
  2169.              */
  2170.             eps = 0.5/tens[ilim-1] - eps;
  2171.             for(i = 0;;) {
  2172.                 L = d;
  2173.                 d -= L;
  2174.                 *s++ = '0' + (int)L;
  2175.                 if (d < eps)
  2176.                     goto ret1;
  2177.                 if (1. - d < eps)
  2178.                     goto bump_up;
  2179.                 if (++i >= ilim)
  2180.                     break;
  2181.                 eps *= 10.;
  2182.                 d *= 10.;
  2183.                 }
  2184.             }
  2185.         else {
  2186. #endif
  2187.             /* Generate ilim digits, then fix them up. */
  2188.             eps *= tens[ilim-1];
  2189.             for(i = 1;; i++, d *= 10.) {
  2190.                 L = d;
  2191.                 d -= L;
  2192.                 *s++ = '0' + (int)L;
  2193.                 if (i == ilim) {
  2194.                     if (d > 0.5 + eps)
  2195.                         goto bump_up;
  2196.                     else if (d < 0.5 - eps) {
  2197.                         while(*--s == '0');
  2198.                         s++;
  2199.                         goto ret1;
  2200.                         }
  2201.                     break;
  2202.                     }
  2203.                 }
  2204. #ifndef No_leftright
  2205.             }
  2206. #endif
  2207.  fast_failed:
  2208.         s = s0;
  2209.         d = d2;
  2210.         k = k0;
  2211.         ilim = ilim0;
  2212.         }
  2213.  
  2214.     /* Do we have a "small" integer? */
  2215.  
  2216.     if (be >= 0 && k <= Int_max) {
  2217.         /* Yes. */
  2218.         ds = tens[k];
  2219.         if (ndigits < 0 && ilim <= 0) {
  2220.             S = mhi = 0;
  2221.             if (ilim < 0 || d <= 5*ds)
  2222.                 goto no_digits;
  2223.             goto one_digit;
  2224.             }
  2225.         for(i = 1;; i++) {
  2226.             L = d / ds;
  2227.             d -= L*ds;
  2228. #ifdef Check_FLT_ROUNDS
  2229.             /* If FLT_ROUNDS == 2, L will usually be high by 1 */
  2230.             if (d < 0) {
  2231.                 L--;
  2232.                 d += ds;
  2233.                 }
  2234. #endif
  2235.             *s++ = '0' + (int)L;
  2236.             if (i == ilim) {
  2237.                 d += d;
  2238.                 if (d > ds || (d == ds && L & 1)) {
  2239.  bump_up:
  2240.                     while(*--s == '9')
  2241.                         if (s == s0) {
  2242.                             k++;
  2243.                             *s = '0';
  2244.                             break;
  2245.                             }
  2246.                     ++*s++;
  2247.                     }
  2248.                 break;
  2249.                 }
  2250.             if (!(d *= 10.))
  2251.                 break;
  2252.             }
  2253.         goto ret1;
  2254.         }
  2255.  
  2256.     m2 = b2;
  2257.     m5 = b5;
  2258.     mhi = mlo = 0;
  2259.     if (leftright) {
  2260.         if (mode < 2) {
  2261.             i =
  2262. #ifndef Sudden_Underflow
  2263.                 denorm ? be + (Bias + (P-1) - 1 + 1) :
  2264. #endif
  2265. #ifdef IBM
  2266.                 1 + 4*P - 3 - bbits + ((bbits + be - 1) & 3);
  2267. #else
  2268.                 1 + P - bbits;
  2269. #endif
  2270.             }
  2271.         else {
  2272.             j = ilim - 1;
  2273.             if (m5 >= j)
  2274.                 m5 -= j;
  2275.             else {
  2276.                 s5 += j -= m5;
  2277.                 b5 += j;
  2278.                 m5 = 0;
  2279.                 }
  2280.             if ((i = ilim) < 0) {
  2281.                 m2 -= i;
  2282.                 i = 0;
  2283.                 }
  2284.             }
  2285.         b2 += i;
  2286.         s2 += i;
  2287.         mhi = i2b(1);
  2288.         }
  2289.     if (m2 > 0 && s2 > 0) {
  2290.         i = m2 < s2 ? m2 : s2;
  2291.         b2 -= i;
  2292.         m2 -= i;
  2293.         s2 -= i;
  2294.         }
  2295.     if (b5 > 0) {
  2296.         if (leftright) {
  2297.             if (m5 > 0) {
  2298.                 mhi = pow5mult(mhi, m5);
  2299.                 b1 = mult(mhi, b);
  2300.                 Bfree(b);
  2301.                 b = b1;
  2302.                 }
  2303.             if ((j = b5 - m5))
  2304.                 b = pow5mult(b, j);
  2305.             }
  2306.         else
  2307.             b = pow5mult(b, b5);
  2308.         }
  2309.     S = i2b(1);
  2310.     if (s5 > 0)
  2311.         S = pow5mult(S, s5);
  2312.  
  2313.     /* Check for special case that d is a normalized power of 2. */
  2314.  
  2315.     if (mode < 2) {
  2316.         if (!word1(d) && !(word0(d) & Bndry_mask)
  2317. #ifndef Sudden_Underflow
  2318.          && word0(d) & Exp_mask
  2319. #endif
  2320.                 ) {
  2321.             /* The special case */
  2322.             b2 += Log2P;
  2323.             s2 += Log2P;
  2324.             spec_case = 1;
  2325.             }
  2326.         else
  2327.             spec_case = 0;
  2328.         }
  2329.  
  2330.     /* Arrange for convenient computation of quotients:
  2331.      * shift left if necessary so divisor has 4 leading 0 bits.
  2332.      *
  2333.      * Perhaps we should just compute leading 28 bits of S once
  2334.      * and for all and pass them and a shift to quorem, so it
  2335.      * can do shifts and ors to compute the numerator for q.
  2336.      */
  2337. #ifdef Pack_32
  2338.     if ((i = ((s5 ? 32 - hi0bits(S->x[S->wds-1]) : 1) + s2) & 0x1f))
  2339.         i = 32 - i;
  2340. #else
  2341.     if ((i = ((s5 ? 32 - hi0bits(S->x[S->wds-1]) : 1) + s2) & 0xf))
  2342.         i = 16 - i;
  2343. #endif
  2344.     if (i > 4) {
  2345.         i -= 4;
  2346.         b2 += i;
  2347.         m2 += i;
  2348.         s2 += i;
  2349.         }
  2350.     else if (i < 4) {
  2351.         i += 28;
  2352.         b2 += i;
  2353.         m2 += i;
  2354.         s2 += i;
  2355.         }
  2356.     if (b2 > 0)
  2357.         b = lshift(b, b2);
  2358.     if (s2 > 0)
  2359.         S = lshift(S, s2);
  2360.     if (k_check) {
  2361.         if (cmp(b,S) < 0) {
  2362.             k--;
  2363.             b = multadd(b, 10, 0);    /* we botched the k estimate */
  2364.             if (leftright)
  2365.                 mhi = multadd(mhi, 10, 0);
  2366.             ilim = ilim1;
  2367.             }
  2368.         }
  2369.     if (ilim <= 0 && mode > 2) {
  2370.         if (ilim < 0 || cmp(b,S = multadd(S,5,0)) <= 0) {
  2371.             /* no digits, fcvt style */
  2372.  no_digits:
  2373.             k = -1 - ndigits;
  2374.             goto ret;
  2375.             }
  2376.  one_digit:
  2377.         *s++ = '1';
  2378.         k++;
  2379.         goto ret;
  2380.         }
  2381.     if (leftright) {
  2382.         if (m2 > 0)
  2383.             mhi = lshift(mhi, m2);
  2384.  
  2385.         /* Compute mlo -- check for special case
  2386.          * that d is a normalized power of 2.
  2387.          */
  2388.  
  2389.         mlo = mhi;
  2390.         if (spec_case) {
  2391.             mhi = Balloc(mhi->k);
  2392.             Bcopy(mhi, mlo);
  2393.             mhi = lshift(mhi, Log2P);
  2394.             }
  2395.  
  2396.         for(i = 1;;i++) {
  2397.             dig = quorem(b,S) + '0';
  2398.             /* Do we yet have the shortest decimal string
  2399.              * that will round to d?
  2400.              */
  2401.             j = cmp(b, mlo);
  2402.             delta = diff(S, mhi);
  2403.             j1 = delta->sign ? 1 : cmp(b, delta);
  2404.             Bfree(delta);
  2405. #ifndef ROUND_BIASED
  2406.             if (j1 == 0 && !mode && !(word1(d) & 1)) {
  2407.                 if (dig == '9')
  2408.                     goto round_9_up;
  2409.                 if (j > 0)
  2410.                     dig++;
  2411.                 *s++ = dig;
  2412.                 goto ret;
  2413.                 }
  2414. #endif
  2415.             if (j < 0 || (j == 0 && !mode
  2416. #ifndef ROUND_BIASED
  2417.                             && !(word1(d) & 1)
  2418. #endif
  2419.                     )) {
  2420.                 if (j1 > 0) {
  2421.                     b = lshift(b, 1);
  2422.                     j1 = cmp(b, S);
  2423.                     if ((j1 > 0 || (j1 == 0 && dig & 1))
  2424.                     && dig++ == '9')
  2425.                         goto round_9_up;
  2426.                     }
  2427.                 *s++ = dig;
  2428.                 goto ret;
  2429.                 }
  2430.             if (j1 > 0) {
  2431.                 if (dig == '9') { /* possible if i == 1 */
  2432.  round_9_up:
  2433.                     *s++ = '9';
  2434.                     goto roundoff;
  2435.                     }
  2436.                 *s++ = dig + 1;
  2437.                 goto ret;
  2438.                 }
  2439.             *s++ = dig;
  2440.             if (i == ilim)
  2441.                 break;
  2442.             b = multadd(b, 10, 0);
  2443.             if (mlo == mhi)
  2444.                 mlo = mhi = multadd(mhi, 10, 0);
  2445.             else {
  2446.                 mlo = multadd(mlo, 10, 0);
  2447.                 mhi = multadd(mhi, 10, 0);
  2448.                 }
  2449.             }
  2450.         }
  2451.     else
  2452.         for(i = 1;; i++) {
  2453.             *s++ = dig = quorem(b,S) + '0';
  2454.             if (i >= ilim)
  2455.                 break;
  2456.             b = multadd(b, 10, 0);
  2457.             }
  2458.  
  2459.     /* Round off last digit */
  2460.  
  2461.     b = lshift(b, 1);
  2462.     j = cmp(b, S);
  2463.     if (j > 0 || (j == 0 && dig & 1)) {
  2464.  roundoff:
  2465.         while(*--s == '9')
  2466.             if (s == s0) {
  2467.                 k++;
  2468.                 *s++ = '1';
  2469.                 goto ret;
  2470.                 }
  2471.         ++*s++;
  2472.         }
  2473.     else {
  2474.         while(*--s == '0');
  2475.         s++;
  2476.         }
  2477.  ret:
  2478.     Bfree(S);
  2479.     if (mhi) {
  2480.         if (mlo && mlo != mhi)
  2481.             Bfree(mlo);
  2482.         Bfree(mhi);
  2483.         }
  2484.  ret1:
  2485.     Bfree(b);
  2486.     if (s == s0) {                /* don't return empty string */
  2487.         *s++ = '0';
  2488.         k = 0;
  2489.     }
  2490.     *s = 0;
  2491.     *decpt = k + 1;
  2492.     if (rve)
  2493.         *rve = s;
  2494.     return s0;
  2495.     }
  2496. #ifdef __cplusplus
  2497. }
  2498. #endif
  2499.  
  2500. #undef P
  2501.