PageRenderTime 65ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/js/lib/Socket.IO-node/support/expresso/deps/jscoverage/js/dtoa.c

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
C | 3165 lines | 2670 code | 139 blank | 356 comment | 505 complexity | 6dcfb87deaf161136497bc671dec37e8 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, MPL-2.0-no-copyleft-exception, BSD-3-Clause

Large files files are truncated, but you can click here to view the full file

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

Large files files are truncated, but you can click here to view the full file