home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / libg++-2.7.1-base.tgz / libg++-2.7.1-src.tar / fsf / libg++ / libio / floatconv.c < prev    next >
C/C++ Source or Header  |  1995-06-15  |  73KB  |  2,350 lines

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