LCOV - code coverage report
Current view: top level - js/src - dtoa.c (source / functions) Hit Total Coverage
Test: output.info Lines: 193 991 19.5 %
Date: 2017-07-14 16:53:18 Functions: 9 24 37.5 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.13