dtoa.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922
  1. /****************************************************************
  2. *
  3. * The author of this software is David M. Gay.
  4. *
  5. * Copyright (c) 1991, 2006 by AT&T.
  6. *
  7. * Permission to use, copy, modify, and distribute this software for any
  8. * purpose without fee is hereby granted, provided that this entire notice
  9. * is included in all copies of any software which is or includes a copy
  10. * or modification of this software and in all copies of the supporting
  11. * documentation for such software.
  12. *
  13. * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
  14. * WARRANTY. IN PARTICULAR, NEITHER THE AUTHOR NOR AT&T MAKES ANY
  15. * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
  16. * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
  17. *
  18. ***************************************************************/
  19. /* Please send bug reports to
  20. David M. Gay
  21. AT&T Bell Laboratories, Room 2C-463
  22. 600 Mountain Avenue
  23. Murray Hill, NJ 07974-2070
  24. U.S.A.
  25. dmg@research.att.com or research!dmg
  26. */
  27. #include <string.h>
  28. #include <stdlib.h>
  29. #include "mprec.h"
  30. #include <stdlib.h>
  31. static int
  32. _DEFUN (quorem,
  33. (b, S),
  34. _Jv_Bigint * b _AND _Jv_Bigint * S)
  35. {
  36. int n;
  37. long borrow, y;
  38. unsigned long carry, q, ys;
  39. unsigned long *bx, *bxe, *sx, *sxe;
  40. #ifdef Pack_32
  41. long z;
  42. unsigned long si, zs;
  43. #endif
  44. n = S->_wds;
  45. #ifdef DEBUG
  46. /*debug*/ if (b->_wds > n)
  47. /*debug*/ Bug ("oversize b in quorem");
  48. #endif
  49. if (b->_wds < n)
  50. return 0;
  51. sx = S->_x;
  52. sxe = sx + --n;
  53. bx = b->_x;
  54. bxe = bx + n;
  55. q = *bxe / (*sxe + 1); /* ensure q <= true quotient */
  56. #ifdef DEBUG
  57. /*debug*/ if (q > 9)
  58. /*debug*/ Bug ("oversized quotient in quorem");
  59. #endif
  60. if (q)
  61. {
  62. borrow = 0;
  63. carry = 0;
  64. do
  65. {
  66. #ifdef Pack_32
  67. si = *sx++;
  68. ys = (si & 0xffff) * q + carry;
  69. zs = (si >> 16) * q + (ys >> 16);
  70. carry = zs >> 16;
  71. y = (*bx & 0xffff) - (ys & 0xffff) + borrow;
  72. borrow = y >> 16;
  73. Sign_Extend (borrow, y);
  74. z = (*bx >> 16) - (zs & 0xffff) + borrow;
  75. borrow = z >> 16;
  76. Sign_Extend (borrow, z);
  77. Storeinc (bx, z, y);
  78. #else
  79. ys = *sx++ * q + carry;
  80. carry = ys >> 16;
  81. y = *bx - (ys & 0xffff) + borrow;
  82. borrow = y >> 16;
  83. Sign_Extend (borrow, y);
  84. *bx++ = y & 0xffff;
  85. #endif
  86. }
  87. while (sx <= sxe);
  88. if (!*bxe)
  89. {
  90. bx = b->_x;
  91. while (--bxe > bx && !*bxe)
  92. --n;
  93. b->_wds = n;
  94. }
  95. }
  96. if (cmp (b, S) >= 0)
  97. {
  98. q++;
  99. borrow = 0;
  100. carry = 0;
  101. bx = b->_x;
  102. sx = S->_x;
  103. do
  104. {
  105. #ifdef Pack_32
  106. si = *sx++;
  107. ys = (si & 0xffff) + carry;
  108. zs = (si >> 16) + (ys >> 16);
  109. carry = zs >> 16;
  110. y = (*bx & 0xffff) - (ys & 0xffff) + borrow;
  111. borrow = y >> 16;
  112. Sign_Extend (borrow, y);
  113. z = (*bx >> 16) - (zs & 0xffff) + borrow;
  114. borrow = z >> 16;
  115. Sign_Extend (borrow, z);
  116. Storeinc (bx, z, y);
  117. #else
  118. ys = *sx++ + carry;
  119. carry = ys >> 16;
  120. y = *bx - (ys & 0xffff) + borrow;
  121. borrow = y >> 16;
  122. Sign_Extend (borrow, y);
  123. *bx++ = y & 0xffff;
  124. #endif
  125. }
  126. while (sx <= sxe);
  127. bx = b->_x;
  128. bxe = bx + n;
  129. if (!*bxe)
  130. {
  131. while (--bxe > bx && !*bxe)
  132. --n;
  133. b->_wds = n;
  134. }
  135. }
  136. return q;
  137. }
  138. #ifdef DEBUG
  139. #include <stdio.h>
  140. void
  141. print (_Jv_Bigint * b)
  142. {
  143. int i, wds;
  144. unsigned long *x, y;
  145. wds = b->_wds;
  146. x = b->_x+wds;
  147. i = 0;
  148. do
  149. {
  150. x--;
  151. fprintf (stderr, "%08x", *x);
  152. }
  153. while (++i < wds);
  154. fprintf (stderr, "\n");
  155. }
  156. #endif
  157. /* dtoa for IEEE arithmetic (dmg): convert double to ASCII string.
  158. *
  159. * Inspired by "How to Print Floating-Point Numbers Accurately" by
  160. * Guy L. Steele, Jr. and Jon L. White [Proc. ACM SIGPLAN '90, pp. 92-101].
  161. *
  162. * Modifications:
  163. * 1. Rather than iterating, we use a simple numeric overestimate
  164. * to determine k = floor(log10(d)). We scale relevant
  165. * quantities using O(log2(k)) rather than O(k) multiplications.
  166. * 2. For some modes > 2 (corresponding to ecvt and fcvt), we don't
  167. * try to generate digits strictly left to right. Instead, we
  168. * compute with fewer bits and propagate the carry if necessary
  169. * when rounding the final digit up. This is often faster.
  170. * 3. Under the assumption that input will be rounded nearest,
  171. * mode 0 renders 1e23 as 1e23 rather than 9.999999999999999e22.
  172. * That is, we allow equality in stopping tests when the
  173. * round-nearest rule will give the same floating-point value
  174. * as would satisfaction of the stopping test with strict
  175. * inequality.
  176. * 4. We remove common factors of powers of 2 from relevant
  177. * quantities.
  178. * 5. When converting floating-point integers less than 1e16,
  179. * we use floating-point arithmetic rather than resorting
  180. * to multiple-precision integers.
  181. * 6. When asked to produce fewer than 15 digits, we first try
  182. * to get by with floating-point arithmetic; we resort to
  183. * multiple-precision integer arithmetic only if we cannot
  184. * guarantee that the floating-point calculation has given
  185. * the correctly rounded result. For k requested digits and
  186. * "uniformly" distributed input, the probability is
  187. * something like 10^(k-15) that we must resort to the long
  188. * calculation.
  189. */
  190. char *
  191. _DEFUN (_dtoa_r,
  192. (ptr, _d, mode, ndigits, decpt, sign, rve, float_type),
  193. struct _Jv_reent *ptr _AND
  194. double _d _AND
  195. int mode _AND
  196. int ndigits _AND
  197. int *decpt _AND
  198. int *sign _AND
  199. char **rve _AND
  200. int float_type)
  201. {
  202. /*
  203. float_type == 0 for double precision, 1 for float.
  204. Arguments ndigits, decpt, sign are similar to those
  205. of ecvt and fcvt; trailing zeros are suppressed from
  206. the returned string. If not null, *rve is set to point
  207. to the end of the return value. If d is +-Infinity or NaN,
  208. then *decpt is set to 9999.
  209. mode:
  210. 0 ==> shortest string that yields d when read in
  211. and rounded to nearest.
  212. 1 ==> like 0, but with Steele & White stopping rule;
  213. e.g. with IEEE P754 arithmetic , mode 0 gives
  214. 1e23 whereas mode 1 gives 9.999999999999999e22.
  215. 2 ==> max(1,ndigits) significant digits. This gives a
  216. return value similar to that of ecvt, except
  217. that trailing zeros are suppressed.
  218. 3 ==> through ndigits past the decimal point. This
  219. gives a return value similar to that from fcvt,
  220. except that trailing zeros are suppressed, and
  221. ndigits can be negative.
  222. 4-9 should give the same return values as 2-3, i.e.,
  223. 4 <= mode <= 9 ==> same return as mode
  224. 2 + (mode & 1). These modes are mainly for
  225. debugging; often they run slower but sometimes
  226. faster than modes 2-3.
  227. 4,5,8,9 ==> left-to-right digit generation.
  228. 6-9 ==> don't try fast floating-point estimate
  229. (if applicable).
  230. > 16 ==> Floating-point arg is treated as single precision.
  231. Values of mode other than 0-9 are treated as mode 0.
  232. Sufficient space is allocated to the return value
  233. to hold the suppressed trailing zeros.
  234. */
  235. int bbits, b2, b5, be, dig, i, ieps, ilim0, j, j1, k, k0,
  236. k_check, leftright, m2, m5, s2, s5, try_quick;
  237. int ilim = 0, ilim1 = 0, spec_case = 0;
  238. union double_union d, d2, eps;
  239. long L;
  240. #ifndef Sudden_Underflow
  241. int denorm;
  242. unsigned long x;
  243. #endif
  244. _Jv_Bigint *b, *b1, *delta, *mlo = NULL, *mhi, *S;
  245. double ds;
  246. char *s, *s0;
  247. d.d = _d;
  248. if (ptr->_result)
  249. {
  250. ptr->_result->_k = ptr->_result_k;
  251. ptr->_result->_maxwds = 1 << ptr->_result_k;
  252. Bfree (ptr, ptr->_result);
  253. ptr->_result = 0;
  254. }
  255. if (word0 (d) & Sign_bit)
  256. {
  257. /* set sign for everything, including 0's and NaNs */
  258. *sign = 1;
  259. word0 (d) &= ~Sign_bit; /* clear sign bit */
  260. }
  261. else
  262. *sign = 0;
  263. #if defined(IEEE_Arith) + defined(VAX)
  264. #ifdef IEEE_Arith
  265. if ((word0 (d) & Exp_mask) == Exp_mask)
  266. #else
  267. if (word0 (d) == 0x8000)
  268. #endif
  269. {
  270. /* Infinity or NaN */
  271. *decpt = 9999;
  272. s =
  273. #ifdef IEEE_Arith
  274. !word1 (d) && !(word0 (d) & 0xfffff) ? "Infinity" :
  275. #endif
  276. "NaN";
  277. if (rve)
  278. *rve =
  279. #ifdef IEEE_Arith
  280. s[3] ? s + 8 :
  281. #endif
  282. s + 3;
  283. return s;
  284. }
  285. #endif
  286. #ifdef IBM
  287. d.d += 0; /* normalize */
  288. #endif
  289. if (!d.d)
  290. {
  291. *decpt = 1;
  292. s = "0";
  293. if (rve)
  294. *rve = s + 1;
  295. return s;
  296. }
  297. b = d2b (ptr, d.d, &be, &bbits);
  298. #ifdef Sudden_Underflow
  299. i = (int) (word0 (d) >> Exp_shift1 & (Exp_mask >> Exp_shift1));
  300. #else
  301. if ((i = (int) (word0 (d) >> Exp_shift1 & (Exp_mask >> Exp_shift1))))
  302. {
  303. #endif
  304. d2.d = d.d;
  305. word0 (d2) &= Frac_mask1;
  306. word0 (d2) |= Exp_11;
  307. #ifdef IBM
  308. if (j = 11 - hi0bits (word0 (d2) & Frac_mask))
  309. d2.d /= 1 << j;
  310. #endif
  311. /* log(x) ~=~ log(1.5) + (x-1.5)/1.5
  312. * log10(x) = log(x) / log(10)
  313. * ~=~ log(1.5)/log(10) + (x-1.5)/(1.5*log(10))
  314. * log10(d) = (i-Bias)*log(2)/log(10) + log10(d2)
  315. *
  316. * This suggests computing an approximation k to log10(d) by
  317. *
  318. * k = (i - Bias)*0.301029995663981
  319. * + ( (d2-1.5)*0.289529654602168 + 0.176091259055681 );
  320. *
  321. * We want k to be too large rather than too small.
  322. * The error in the first-order Taylor series approximation
  323. * is in our favor, so we just round up the constant enough
  324. * to compensate for any error in the multiplication of
  325. * (i - Bias) by 0.301029995663981; since |i - Bias| <= 1077,
  326. * and 1077 * 0.30103 * 2^-52 ~=~ 7.2e-14,
  327. * adding 1e-13 to the constant term more than suffices.
  328. * Hence we adjust the constant term to 0.1760912590558.
  329. * (We could get a more accurate k by invoking log10,
  330. * but this is probably not worthwhile.)
  331. */
  332. i -= Bias;
  333. #ifdef IBM
  334. i <<= 2;
  335. i += j;
  336. #endif
  337. #ifndef Sudden_Underflow
  338. denorm = 0;
  339. }
  340. else
  341. {
  342. /* d is denormalized */
  343. i = bbits + be + (Bias + (P - 1) - 1);
  344. x = i > 32 ? word0 (d) << (64 - i) | word1 (d) >> (i - 32)
  345. : word1 (d) << (32 - i);
  346. d2.d = x;
  347. word0 (d2) -= 31 * Exp_msk1; /* adjust exponent */
  348. i -= (Bias + (P - 1) - 1) + 1;
  349. denorm = 1;
  350. }
  351. #endif
  352. ds = (d2.d - 1.5) * 0.289529654602168 + 0.1760912590558 + i * 0.301029995663981;
  353. k = (int) ds;
  354. if (ds < 0. && ds != k)
  355. k--; /* want k = floor(ds) */
  356. k_check = 1;
  357. if (k >= 0 && k <= Ten_pmax)
  358. {
  359. if (d.d < tens[k])
  360. k--;
  361. k_check = 0;
  362. }
  363. j = bbits - i - 1;
  364. if (j >= 0)
  365. {
  366. b2 = 0;
  367. s2 = j;
  368. }
  369. else
  370. {
  371. b2 = -j;
  372. s2 = 0;
  373. }
  374. if (k >= 0)
  375. {
  376. b5 = 0;
  377. s5 = k;
  378. s2 += k;
  379. }
  380. else
  381. {
  382. b2 -= k;
  383. b5 = -k;
  384. s5 = 0;
  385. }
  386. if (mode < 0 || mode > 9)
  387. mode = 0;
  388. try_quick = 1;
  389. if (mode > 5)
  390. {
  391. mode -= 4;
  392. try_quick = 0;
  393. }
  394. leftright = 1;
  395. switch (mode)
  396. {
  397. case 0:
  398. case 1:
  399. ilim = ilim1 = -1;
  400. i = 18;
  401. ndigits = 0;
  402. break;
  403. case 2:
  404. leftright = 0;
  405. /* no break */
  406. case 4:
  407. if (ndigits <= 0)
  408. ndigits = 1;
  409. ilim = ilim1 = i = ndigits;
  410. break;
  411. case 3:
  412. leftright = 0;
  413. /* no break */
  414. case 5:
  415. i = ndigits + k + 1;
  416. ilim = i;
  417. ilim1 = i - 1;
  418. if (i <= 0)
  419. i = 1;
  420. }
  421. j = sizeof (unsigned long);
  422. for (ptr->_result_k = 0; (int) (sizeof (_Jv_Bigint) - sizeof (unsigned long)) + j <= i;
  423. j <<= 1)
  424. ptr->_result_k++;
  425. ptr->_result = Balloc (ptr, ptr->_result_k);
  426. s = s0 = (char *) ptr->_result;
  427. if (ilim >= 0 && ilim <= Quick_max && try_quick)
  428. {
  429. /* Try to get by with floating-point arithmetic. */
  430. i = 0;
  431. d2.d = d.d;
  432. k0 = k;
  433. ilim0 = ilim;
  434. ieps = 2; /* conservative */
  435. if (k > 0)
  436. {
  437. ds = tens[k & 0xf];
  438. j = k >> 4;
  439. if (j & Bletch)
  440. {
  441. /* prevent overflows */
  442. j &= Bletch - 1;
  443. d.d /= bigtens[n_bigtens - 1];
  444. ieps++;
  445. }
  446. for (; j; j >>= 1, i++)
  447. if (j & 1)
  448. {
  449. ieps++;
  450. ds *= bigtens[i];
  451. }
  452. d.d /= ds;
  453. }
  454. else if ((j1 = -k))
  455. {
  456. d.d *= tens[j1 & 0xf];
  457. for (j = j1 >> 4; j; j >>= 1, i++)
  458. if (j & 1)
  459. {
  460. ieps++;
  461. d.d *= bigtens[i];
  462. }
  463. }
  464. if (k_check && d.d < 1. && ilim > 0)
  465. {
  466. if (ilim1 <= 0)
  467. goto fast_failed;
  468. ilim = ilim1;
  469. k--;
  470. d.d *= 10.;
  471. ieps++;
  472. }
  473. eps.d = ieps * d.d + 7.;
  474. word0 (eps) -= (P - 1) * Exp_msk1;
  475. if (ilim == 0)
  476. {
  477. S = mhi = 0;
  478. d.d -= 5.;
  479. if (d.d > eps.d)
  480. goto one_digit;
  481. if (d.d < -eps.d)
  482. goto no_digits;
  483. goto fast_failed;
  484. }
  485. #ifndef No_leftright
  486. if (leftright)
  487. {
  488. /* Use Steele & White method of only
  489. * generating digits needed.
  490. */
  491. eps.d = 0.5 / tens[ilim - 1] - eps.d;
  492. for (i = 0;;)
  493. {
  494. L = d.d;
  495. d.d -= L;
  496. *s++ = '0' + (int) L;
  497. if (d.d < eps.d)
  498. goto ret1;
  499. if (1. - d.d < eps.d)
  500. goto bump_up;
  501. if (++i >= ilim)
  502. break;
  503. eps.d *= 10.;
  504. d.d *= 10.;
  505. }
  506. }
  507. else
  508. {
  509. #endif
  510. /* Generate ilim digits, then fix them up. */
  511. eps.d *= tens[ilim - 1];
  512. for (i = 1;; i++, d.d *= 10.)
  513. {
  514. L = d.d;
  515. d.d -= L;
  516. *s++ = '0' + (int) L;
  517. if (i == ilim)
  518. {
  519. if (d.d > 0.5 + eps.d)
  520. goto bump_up;
  521. else if (d.d < 0.5 - eps.d)
  522. {
  523. while (*--s == '0');
  524. s++;
  525. goto ret1;
  526. }
  527. break;
  528. }
  529. }
  530. #ifndef No_leftright
  531. }
  532. #endif
  533. fast_failed:
  534. s = s0;
  535. d.d = d2.d;
  536. k = k0;
  537. ilim = ilim0;
  538. }
  539. /* Do we have a "small" integer? */
  540. if (be >= 0 && k <= Int_max)
  541. {
  542. /* Yes. */
  543. ds = tens[k];
  544. if (ndigits < 0 && ilim <= 0)
  545. {
  546. S = mhi = 0;
  547. if (ilim < 0 || d.d <= 5 * ds)
  548. goto no_digits;
  549. goto one_digit;
  550. }
  551. for (i = 1;; i++)
  552. {
  553. L = d.d / ds;
  554. d.d -= L * ds;
  555. #ifdef Check_FLT_ROUNDS
  556. /* If FLT_ROUNDS == 2, L will usually be high by 1 */
  557. if (d.d < 0)
  558. {
  559. L--;
  560. d.d += ds;
  561. }
  562. #endif
  563. *s++ = '0' + (int) L;
  564. if (i == ilim)
  565. {
  566. d.d += d.d;
  567. if (d.d > ds || (d.d == ds && L & 1))
  568. {
  569. bump_up:
  570. while (*--s == '9')
  571. if (s == s0)
  572. {
  573. k++;
  574. *s = '0';
  575. break;
  576. }
  577. ++*s++;
  578. }
  579. break;
  580. }
  581. if (!(d.d *= 10.))
  582. break;
  583. }
  584. goto ret1;
  585. }
  586. m2 = b2;
  587. m5 = b5;
  588. mhi = mlo = 0;
  589. if (leftright)
  590. {
  591. if (mode < 2)
  592. {
  593. i =
  594. #ifndef Sudden_Underflow
  595. denorm ? be + (Bias + (P - 1) - 1 + 1) :
  596. #endif
  597. #ifdef IBM
  598. 1 + 4 * P - 3 - bbits + ((bbits + be - 1) & 3);
  599. #else
  600. 1 + P - bbits;
  601. #endif
  602. }
  603. else
  604. {
  605. j = ilim - 1;
  606. if (m5 >= j)
  607. m5 -= j;
  608. else
  609. {
  610. s5 += j -= m5;
  611. b5 += j;
  612. m5 = 0;
  613. }
  614. if ((i = ilim) < 0)
  615. {
  616. m2 -= i;
  617. i = 0;
  618. }
  619. }
  620. b2 += i;
  621. s2 += i;
  622. mhi = i2b (ptr, 1);
  623. }
  624. if (m2 > 0 && s2 > 0)
  625. {
  626. i = m2 < s2 ? m2 : s2;
  627. b2 -= i;
  628. m2 -= i;
  629. s2 -= i;
  630. }
  631. if (b5 > 0)
  632. {
  633. if (leftright)
  634. {
  635. if (m5 > 0)
  636. {
  637. mhi = pow5mult (ptr, mhi, m5);
  638. b1 = mult (ptr, mhi, b);
  639. Bfree (ptr, b);
  640. b = b1;
  641. }
  642. if ((j = b5 - m5))
  643. b = pow5mult (ptr, b, j);
  644. }
  645. else
  646. b = pow5mult (ptr, b, b5);
  647. }
  648. S = i2b (ptr, 1);
  649. if (s5 > 0)
  650. S = pow5mult (ptr, S, s5);
  651. /* Check for special case that d is a normalized power of 2. */
  652. if (mode < 2)
  653. {
  654. if (!word1 (d) && !(word0 (d) & Bndry_mask)
  655. #ifndef Sudden_Underflow
  656. && word0(d) & Exp_mask
  657. #endif
  658. )
  659. {
  660. /* The special case */
  661. b2 += Log2P;
  662. s2 += Log2P;
  663. spec_case = 1;
  664. }
  665. else
  666. spec_case = 0;
  667. }
  668. /* Arrange for convenient computation of quotients:
  669. * shift left if necessary so divisor has 4 leading 0 bits.
  670. *
  671. * Perhaps we should just compute leading 28 bits of S once
  672. * and for all and pass them and a shift to quorem, so it
  673. * can do shifts and ors to compute the numerator for q.
  674. */
  675. #ifdef Pack_32
  676. if ((i = ((s5 ? 32 - hi0bits (S->_x[S->_wds - 1]) : 1) + s2) & 0x1f))
  677. i = 32 - i;
  678. #else
  679. if ((i = ((s5 ? 32 - hi0bits (S->_x[S->_wds - 1]) : 1) + s2) & 0xf))
  680. i = 16 - i;
  681. #endif
  682. if (i > 4)
  683. {
  684. i -= 4;
  685. b2 += i;
  686. m2 += i;
  687. s2 += i;
  688. }
  689. else if (i < 4)
  690. {
  691. i += 28;
  692. b2 += i;
  693. m2 += i;
  694. s2 += i;
  695. }
  696. if (b2 > 0)
  697. b = lshift (ptr, b, b2);
  698. if (s2 > 0)
  699. S = lshift (ptr, S, s2);
  700. if (k_check)
  701. {
  702. if (cmp (b, S) < 0)
  703. {
  704. k--;
  705. b = multadd (ptr, b, 10, 0); /* we botched the k estimate */
  706. if (leftright)
  707. mhi = multadd (ptr, mhi, 10, 0);
  708. ilim = ilim1;
  709. }
  710. }
  711. if (ilim <= 0 && mode > 2)
  712. {
  713. if (ilim < 0 || cmp (b, S = multadd (ptr, S, 5, 0)) <= 0)
  714. {
  715. /* no digits, fcvt style */
  716. no_digits:
  717. k = -1 - ndigits;
  718. goto ret;
  719. }
  720. one_digit:
  721. *s++ = '1';
  722. k++;
  723. goto ret;
  724. }
  725. if (leftright)
  726. {
  727. if (m2 > 0)
  728. mhi = lshift (ptr, mhi, m2);
  729. /* Single precision case, */
  730. if (float_type)
  731. mhi = lshift (ptr, mhi, 29);
  732. /* Compute mlo -- check for special case
  733. * that d is a normalized power of 2.
  734. */
  735. mlo = mhi;
  736. if (spec_case)
  737. {
  738. mhi = Balloc (ptr, mhi->_k);
  739. Bcopy (mhi, mlo);
  740. mhi = lshift (ptr, mhi, Log2P);
  741. }
  742. for (i = 1;; i++)
  743. {
  744. dig = quorem (b, S) + '0';
  745. /* Do we yet have the shortest decimal string
  746. * that will round to d?
  747. */
  748. j = cmp (b, mlo);
  749. delta = diff (ptr, S, mhi);
  750. j1 = delta->_sign ? 1 : cmp (b, delta);
  751. Bfree (ptr, delta);
  752. #ifndef ROUND_BIASED
  753. if (j1 == 0 && !mode && !(word1 (d) & 1))
  754. {
  755. if (dig == '9')
  756. goto round_9_up;
  757. if (j > 0)
  758. dig++;
  759. *s++ = dig;
  760. goto ret;
  761. }
  762. #endif
  763. if (j < 0 || (j == 0 && !mode
  764. #ifndef ROUND_BIASED
  765. && !(word1 (d) & 1)
  766. #endif
  767. ))
  768. {
  769. if (j1 > 0)
  770. {
  771. b = lshift (ptr, b, 1);
  772. j1 = cmp (b, S);
  773. if ((j1 > 0 || (j1 == 0 && dig & 1))
  774. && dig++ == '9')
  775. goto round_9_up;
  776. }
  777. *s++ = dig;
  778. goto ret;
  779. }
  780. if (j1 > 0)
  781. {
  782. if (dig == '9')
  783. { /* possible if i == 1 */
  784. round_9_up:
  785. *s++ = '9';
  786. goto roundoff;
  787. }
  788. *s++ = dig + 1;
  789. goto ret;
  790. }
  791. *s++ = dig;
  792. if (i == ilim)
  793. break;
  794. b = multadd (ptr, b, 10, 0);
  795. if (mlo == mhi)
  796. mlo = mhi = multadd (ptr, mhi, 10, 0);
  797. else
  798. {
  799. mlo = multadd (ptr, mlo, 10, 0);
  800. mhi = multadd (ptr, mhi, 10, 0);
  801. }
  802. }
  803. }
  804. else
  805. for (i = 1;; i++)
  806. {
  807. *s++ = dig = quorem (b, S) + '0';
  808. if (i >= ilim)
  809. break;
  810. b = multadd (ptr, b, 10, 0);
  811. }
  812. /* Round off last digit */
  813. b = lshift (ptr, b, 1);
  814. j = cmp (b, S);
  815. if (j > 0 || (j == 0 && dig & 1))
  816. {
  817. roundoff:
  818. while (*--s == '9')
  819. if (s == s0)
  820. {
  821. k++;
  822. *s++ = '1';
  823. goto ret;
  824. }
  825. ++*s++;
  826. }
  827. else
  828. {
  829. while (*--s == '0');
  830. s++;
  831. }
  832. ret:
  833. Bfree (ptr, S);
  834. if (mhi)
  835. {
  836. if (mlo && mlo != mhi)
  837. Bfree (ptr, mlo);
  838. Bfree (ptr, mhi);
  839. }
  840. ret1:
  841. Bfree (ptr, b);
  842. *s = 0;
  843. *decpt = k + 1;
  844. if (rve)
  845. *rve = s;
  846. return s0;
  847. }
  848. _VOID
  849. _DEFUN (_dtoa,
  850. (_d, mode, ndigits, decpt, sign, rve, buf, float_type),
  851. double _d _AND
  852. int mode _AND
  853. int ndigits _AND
  854. int *decpt _AND
  855. int *sign _AND
  856. char **rve _AND
  857. char *buf _AND
  858. int float_type)
  859. {
  860. struct _Jv_reent reent;
  861. char *p;
  862. int i;
  863. memset (&reent, 0, sizeof reent);
  864. p = _dtoa_r (&reent, _d, mode, ndigits, decpt, sign, rve, float_type);
  865. strcpy (buf, p);
  866. for (i = 0; i < reent._result_k; ++i)
  867. {
  868. struct _Jv_Bigint *l = reent._freelist[i];
  869. while (l)
  870. {
  871. struct _Jv_Bigint *next = l->_next;
  872. free (l);
  873. l = next;
  874. }
  875. }
  876. if (reent._freelist)
  877. free (reent._freelist);
  878. }