dtoa.c 18 KB

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