gdtoa.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829
  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. static Bigint *
  27. #ifdef KR_headers
  28. bitstob(bits, nbits, bbits) ULong *bits; int nbits; int *bbits;
  29. #else
  30. bitstob(ULong *bits, int nbits, int *bbits)
  31. #endif
  32. {
  33. int i, k;
  34. Bigint *b;
  35. ULong *be, *x, *x0;
  36. i = ULbits;
  37. k = 0;
  38. while(i < nbits) {
  39. i <<= 1;
  40. k++;
  41. }
  42. #ifndef Pack_32
  43. if (!k)
  44. k = 1;
  45. #endif
  46. b = Balloc(k);
  47. if (b == NULL)
  48. return (NULL);
  49. be = bits + ((nbits - 1) >> kshift);
  50. x = x0 = b->x;
  51. do {
  52. *x++ = *bits & ALL_ON;
  53. #ifdef Pack_16
  54. *x++ = (*bits >> 16) & ALL_ON;
  55. #endif
  56. } while(++bits <= be);
  57. i = x - x0;
  58. while(!x0[--i])
  59. if (!i) {
  60. b->wds = 0;
  61. *bbits = 0;
  62. goto ret;
  63. }
  64. b->wds = i + 1;
  65. *bbits = i*ULbits + 32 - hi0bits(b->x[i]);
  66. ret:
  67. return b;
  68. }
  69. /* dtoa for IEEE arithmetic (dmg): convert double to ASCII string.
  70. *
  71. * Inspired by "How to Print Floating-Point Numbers Accurately" by
  72. * Guy L. Steele, Jr. and Jon L. White [Proc. ACM SIGPLAN '90, pp. 112-126].
  73. *
  74. * Modifications:
  75. * 1. Rather than iterating, we use a simple numeric overestimate
  76. * to determine k = floor(log10(d)). We scale relevant
  77. * quantities using O(log2(k)) rather than O(k) multiplications.
  78. * 2. For some modes > 2 (corresponding to ecvt and fcvt), we don't
  79. * try to generate digits strictly left to right. Instead, we
  80. * compute with fewer bits and propagate the carry if necessary
  81. * when rounding the final digit up. This is often faster.
  82. * 3. Under the assumption that input will be rounded nearest,
  83. * mode 0 renders 1e23 as 1e23 rather than 9.999999999999999e22.
  84. * That is, we allow equality in stopping tests when the
  85. * round-nearest rule will give the same floating-point value
  86. * as would satisfaction of the stopping test with strict
  87. * inequality.
  88. * 4. We remove common factors of powers of 2 from relevant
  89. * quantities.
  90. * 5. When converting floating-point integers less than 1e16,
  91. * we use floating-point arithmetic rather than resorting
  92. * to multiple-precision integers.
  93. * 6. When asked to produce fewer than 15 digits, we first try
  94. * to get by with floating-point arithmetic; we resort to
  95. * multiple-precision integer arithmetic only if we cannot
  96. * guarantee that the floating-point calculation has given
  97. * the correctly rounded result. For k requested digits and
  98. * "uniformly" distributed input, the probability is
  99. * something like 10^(k-15) that we must resort to the Long
  100. * calculation.
  101. */
  102. char *
  103. gdtoa
  104. #ifdef KR_headers
  105. (fpi, be, bits, kindp, mode, ndigits, decpt, rve)
  106. FPI *fpi; int be; ULong *bits;
  107. int *kindp, mode, ndigits, *decpt; char **rve;
  108. #else
  109. (FPI *fpi, int be, ULong *bits, int *kindp, int mode, int ndigits, int *decpt, char **rve)
  110. #endif
  111. {
  112. /* Arguments ndigits and decpt are similar to the second and third
  113. arguments of ecvt and fcvt; trailing zeros are suppressed from
  114. the returned string. If not null, *rve is set to point
  115. to the end of the return value. If d is +-Infinity or NaN,
  116. then *decpt is set to 9999.
  117. be = exponent: value = (integer represented by bits) * (2 to the power of be).
  118. mode:
  119. 0 ==> shortest string that yields d when read in
  120. and rounded to nearest.
  121. 1 ==> like 0, but with Steele & White stopping rule;
  122. e.g. with IEEE P754 arithmetic , mode 0 gives
  123. 1e23 whereas mode 1 gives 9.999999999999999e22.
  124. 2 ==> max(1,ndigits) significant digits. This gives a
  125. return value similar to that of ecvt, except
  126. that trailing zeros are suppressed.
  127. 3 ==> through ndigits past the decimal point. This
  128. gives a return value similar to that from fcvt,
  129. except that trailing zeros are suppressed, and
  130. ndigits can be negative.
  131. 4-9 should give the same return values as 2-3, i.e.,
  132. 4 <= mode <= 9 ==> same return as mode
  133. 2 + (mode & 1). These modes are mainly for
  134. debugging; often they run slower but sometimes
  135. faster than modes 2-3.
  136. 4,5,8,9 ==> left-to-right digit generation.
  137. 6-9 ==> don't try fast floating-point estimate
  138. (if applicable).
  139. Values of mode other than 0-9 are treated as mode 0.
  140. Sufficient space is allocated to the return value
  141. to hold the suppressed trailing zeros.
  142. */
  143. int bbits, b2, b5, be0, dig, i, ieps, ilim, ilim0, ilim1, inex;
  144. int j, j1, k, k0, k_check, kind, leftright, m2, m5, nbits;
  145. int rdir, s2, s5, spec_case, try_quick;
  146. Long L;
  147. Bigint *b, *b1, *delta, *mlo, *mhi, *mhi1, *S;
  148. double d2, ds;
  149. char *s, *s0;
  150. U d, eps;
  151. #ifndef MULTIPLE_THREADS
  152. if (dtoa_result) {
  153. freedtoa(dtoa_result);
  154. dtoa_result = 0;
  155. }
  156. #endif
  157. inex = 0;
  158. kind = *kindp &= ~STRTOG_Inexact;
  159. switch(kind & STRTOG_Retmask) {
  160. case STRTOG_Zero:
  161. goto ret_zero;
  162. case STRTOG_Normal:
  163. case STRTOG_Denormal:
  164. break;
  165. case STRTOG_Infinite:
  166. *decpt = -32768;
  167. return nrv_alloc("Infinity", rve, 8);
  168. case STRTOG_NaN:
  169. *decpt = -32768;
  170. return nrv_alloc("NaN", rve, 3);
  171. default:
  172. return 0;
  173. }
  174. b = bitstob(bits, nbits = fpi->nbits, &bbits);
  175. if (b == NULL)
  176. return (NULL);
  177. be0 = be;
  178. if ( (i = trailz(b)) !=0) {
  179. rshift(b, i);
  180. be += i;
  181. bbits -= i;
  182. }
  183. if (!b->wds) {
  184. Bfree(b);
  185. ret_zero:
  186. *decpt = 1;
  187. return nrv_alloc("0", rve, 1);
  188. }
  189. dval(&d) = b2d(b, &i);
  190. i = be + bbits - 1;
  191. word0(&d) &= Frac_mask1;
  192. word0(&d) |= Exp_11;
  193. #ifdef IBM
  194. if ( (j = 11 - hi0bits(word0(&d) & Frac_mask)) !=0)
  195. dval(&d) /= 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. #ifdef IBM
  219. i <<= 2;
  220. i += j;
  221. #endif
  222. ds = (dval(&d)-1.5)*0.289529654602168 + 0.1760912590558 + i*0.301029995663981;
  223. /* correct assumption about exponent range */
  224. if ((j = i) < 0)
  225. j = -j;
  226. if ((j -= 1077) > 0)
  227. ds += j * 7e-17;
  228. k = (int)ds;
  229. if (ds < 0. && ds != k)
  230. k--; /* want k = floor(ds) */
  231. k_check = 1;
  232. #ifdef IBM
  233. j = be + bbits - 1;
  234. if ( (j1 = j & 3) !=0)
  235. dval(&d) *= 1 << j1;
  236. word0(&d) += j << Exp_shift - 2 & Exp_mask;
  237. #else
  238. word0(&d) += (be + bbits - 1) << Exp_shift;
  239. #endif
  240. if (k >= 0 && k <= Ten_pmax) {
  241. if (dval(&d) < tens[k])
  242. k--;
  243. k_check = 0;
  244. }
  245. j = bbits - i - 1;
  246. if (j >= 0) {
  247. b2 = 0;
  248. s2 = j;
  249. }
  250. else {
  251. b2 = -j;
  252. s2 = 0;
  253. }
  254. if (k >= 0) {
  255. b5 = 0;
  256. s5 = k;
  257. s2 += k;
  258. }
  259. else {
  260. b2 -= k;
  261. b5 = -k;
  262. s5 = 0;
  263. }
  264. if (mode < 0 || mode > 9)
  265. mode = 0;
  266. try_quick = 1;
  267. if (mode > 5) {
  268. mode -= 4;
  269. try_quick = 0;
  270. }
  271. else if (i >= -4 - Emin || i < Emin)
  272. try_quick = 0;
  273. leftright = 1;
  274. ilim = ilim1 = -1; /* Values for cases 0 and 1; done here to */
  275. /* silence erroneous "gcc -Wall" warning. */
  276. switch(mode) {
  277. case 0:
  278. case 1:
  279. i = (int)(nbits * .30103) + 3;
  280. ndigits = 0;
  281. break;
  282. case 2:
  283. leftright = 0;
  284. /* no break */
  285. case 4:
  286. if (ndigits <= 0)
  287. ndigits = 1;
  288. ilim = ilim1 = i = ndigits;
  289. break;
  290. case 3:
  291. leftright = 0;
  292. /* no break */
  293. case 5:
  294. i = ndigits + k + 1;
  295. ilim = i;
  296. ilim1 = i - 1;
  297. if (i <= 0)
  298. i = 1;
  299. }
  300. s = s0 = rv_alloc(i);
  301. if (s == NULL)
  302. return (NULL);
  303. if ( (rdir = fpi->rounding - 1) !=0) {
  304. if (rdir < 0)
  305. rdir = 2;
  306. if (kind & STRTOG_Neg)
  307. rdir = 3 - rdir;
  308. }
  309. /* Now rdir = 0 ==> round near, 1 ==> round up, 2 ==> round down. */
  310. if (ilim >= 0 && ilim <= Quick_max && try_quick && !rdir
  311. #ifndef IMPRECISE_INEXACT
  312. && k == 0
  313. #endif
  314. ) {
  315. /* Try to get by with floating-point arithmetic. */
  316. i = 0;
  317. d2 = dval(&d);
  318. #ifdef IBM
  319. if ( (j = 11 - hi0bits(word0(&d) & Frac_mask)) !=0)
  320. dval(&d) /= 1 << j;
  321. #endif
  322. k0 = k;
  323. ilim0 = ilim;
  324. ieps = 2; /* conservative */
  325. if (k > 0) {
  326. ds = tens[k&0xf];
  327. j = k >> 4;
  328. if (j & Bletch) {
  329. /* prevent overflows */
  330. j &= Bletch - 1;
  331. dval(&d) /= bigtens[n_bigtens-1];
  332. ieps++;
  333. }
  334. for(; j; j >>= 1, i++)
  335. if (j & 1) {
  336. ieps++;
  337. ds *= bigtens[i];
  338. }
  339. }
  340. else {
  341. ds = 1.;
  342. if ( (j1 = -k) !=0) {
  343. dval(&d) *= tens[j1 & 0xf];
  344. for(j = j1 >> 4; j; j >>= 1, i++)
  345. if (j & 1) {
  346. ieps++;
  347. dval(&d) *= bigtens[i];
  348. }
  349. }
  350. }
  351. if (k_check && dval(&d) < 1. && ilim > 0) {
  352. if (ilim1 <= 0)
  353. goto fast_failed;
  354. ilim = ilim1;
  355. k--;
  356. dval(&d) *= 10.;
  357. ieps++;
  358. }
  359. dval(&eps) = ieps*dval(&d) + 7.;
  360. word0(&eps) -= (P-1)*Exp_msk1;
  361. if (ilim == 0) {
  362. S = mhi = 0;
  363. dval(&d) -= 5.;
  364. if (dval(&d) > dval(&eps))
  365. goto one_digit;
  366. if (dval(&d) < -dval(&eps))
  367. goto no_digits;
  368. goto fast_failed;
  369. }
  370. #ifndef No_leftright
  371. if (leftright) {
  372. /* Use Steele & White method of only
  373. * generating digits needed.
  374. */
  375. dval(&eps) = ds*0.5/tens[ilim-1] - dval(&eps);
  376. for(i = 0;;) {
  377. L = (Long)(dval(&d)/ds);
  378. dval(&d) -= L*ds;
  379. *s++ = '0' + (int)L;
  380. if (dval(&d) < dval(&eps)) {
  381. if (dval(&d))
  382. inex = STRTOG_Inexlo;
  383. goto ret1;
  384. }
  385. if (ds - dval(&d) < dval(&eps))
  386. goto bump_up;
  387. if (++i >= ilim)
  388. break;
  389. dval(&eps) *= 10.;
  390. dval(&d) *= 10.;
  391. }
  392. }
  393. else {
  394. #endif
  395. /* Generate ilim digits, then fix them up. */
  396. dval(&eps) *= tens[ilim-1];
  397. for(i = 1;; i++, dval(&d) *= 10.) {
  398. if ( (L = (Long)(dval(&d)/ds)) !=0)
  399. dval(&d) -= L*ds;
  400. *s++ = '0' + (int)L;
  401. if (i == ilim) {
  402. ds *= 0.5;
  403. if (dval(&d) > ds + dval(&eps))
  404. goto bump_up;
  405. else if (dval(&d) < ds - dval(&eps)) {
  406. if (dval(&d))
  407. inex = STRTOG_Inexlo;
  408. goto clear_trailing0;
  409. }
  410. break;
  411. }
  412. }
  413. #ifndef No_leftright
  414. }
  415. #endif
  416. fast_failed:
  417. s = s0;
  418. dval(&d) = d2;
  419. k = k0;
  420. ilim = ilim0;
  421. }
  422. /* Do we have a "small" integer? */
  423. if (be >= 0 && k <= Int_max) {
  424. /* Yes. */
  425. ds = tens[k];
  426. if (ndigits < 0 && ilim <= 0) {
  427. S = mhi = 0;
  428. if (ilim < 0 || dval(&d) <= 5*ds)
  429. goto no_digits;
  430. goto one_digit;
  431. }
  432. for(i = 1;; i++, dval(&d) *= 10.) {
  433. L = dval(&d) / ds;
  434. dval(&d) -= L*ds;
  435. #ifdef Check_FLT_ROUNDS
  436. /* If FLT_ROUNDS == 2, L will usually be high by 1 */
  437. if (dval(&d) < 0) {
  438. L--;
  439. dval(&d) += ds;
  440. }
  441. #endif
  442. *s++ = '0' + (int)L;
  443. if (dval(&d) == 0.)
  444. break;
  445. if (i == ilim) {
  446. if (rdir) {
  447. if (rdir == 1)
  448. goto bump_up;
  449. inex = STRTOG_Inexlo;
  450. goto ret1;
  451. }
  452. dval(&d) += dval(&d);
  453. #ifdef ROUND_BIASED
  454. if (dval(&d) >= ds)
  455. #else
  456. if (dval(&d) > ds || (dval(&d) == ds && L & 1))
  457. #endif
  458. {
  459. bump_up:
  460. inex = STRTOG_Inexhi;
  461. while(*--s == '9')
  462. if (s == s0) {
  463. k++;
  464. *s = '0';
  465. break;
  466. }
  467. ++*s++;
  468. }
  469. else {
  470. inex = STRTOG_Inexlo;
  471. clear_trailing0:
  472. while(*--s == '0'){}
  473. ++s;
  474. }
  475. break;
  476. }
  477. }
  478. goto ret1;
  479. }
  480. m2 = b2;
  481. m5 = b5;
  482. mhi = mlo = 0;
  483. if (leftright) {
  484. i = nbits - bbits;
  485. if (be - i++ < fpi->emin && mode != 3 && mode != 5) {
  486. /* denormal */
  487. i = be - fpi->emin + 1;
  488. if (mode >= 2 && ilim > 0 && ilim < i)
  489. goto small_ilim;
  490. }
  491. else if (mode >= 2) {
  492. small_ilim:
  493. j = ilim - 1;
  494. if (m5 >= j)
  495. m5 -= j;
  496. else {
  497. s5 += j -= m5;
  498. b5 += j;
  499. m5 = 0;
  500. }
  501. if ((i = ilim) < 0) {
  502. m2 -= i;
  503. i = 0;
  504. }
  505. }
  506. b2 += i;
  507. s2 += i;
  508. mhi = i2b(1);
  509. if (mhi == NULL)
  510. return (NULL);
  511. }
  512. if (m2 > 0 && s2 > 0) {
  513. i = m2 < s2 ? m2 : s2;
  514. b2 -= i;
  515. m2 -= i;
  516. s2 -= i;
  517. }
  518. if (b5 > 0) {
  519. if (leftright) {
  520. if (m5 > 0) {
  521. mhi = pow5mult(mhi, m5);
  522. if (mhi == NULL)
  523. return (NULL);
  524. b1 = mult(mhi, b);
  525. if (b1 == NULL)
  526. return (NULL);
  527. Bfree(b);
  528. b = b1;
  529. }
  530. if ( (j = b5 - m5) !=0) {
  531. b = pow5mult(b, j);
  532. if (b == NULL)
  533. return (NULL);
  534. }
  535. }
  536. else {
  537. b = pow5mult(b, b5);
  538. if (b == NULL)
  539. return (NULL);
  540. }
  541. }
  542. S = i2b(1);
  543. if (S == NULL)
  544. return (NULL);
  545. if (s5 > 0) {
  546. S = pow5mult(S, s5);
  547. if (S == NULL)
  548. return (NULL);
  549. }
  550. /* Check for special case that d is a normalized power of 2. */
  551. spec_case = 0;
  552. if (mode < 2) {
  553. if (bbits == 1 && be0 > fpi->emin + 1) {
  554. /* The special case */
  555. b2++;
  556. s2++;
  557. spec_case = 1;
  558. }
  559. }
  560. /* Arrange for convenient computation of quotients:
  561. * shift left if necessary so divisor has 4 leading 0 bits.
  562. *
  563. * Perhaps we should just compute leading 28 bits of S once
  564. * and for all and pass them and a shift to quorem, so it
  565. * can do shifts and ors to compute the numerator for q.
  566. */
  567. i = ((s5 ? hi0bits(S->x[S->wds-1]) : ULbits - 1) - s2 - 4) & kmask;
  568. m2 += i;
  569. if ((b2 += i) > 0) {
  570. b = lshift(b, b2);
  571. if (b == NULL)
  572. return (NULL);
  573. }
  574. if ((s2 += i) > 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 > 2) {
  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. inex = STRTOG_Inexlo;
  602. goto ret;
  603. }
  604. one_digit:
  605. inex = STRTOG_Inexhi;
  606. *s++ = '1';
  607. k++;
  608. goto ret;
  609. }
  610. if (leftright) {
  611. if (m2 > 0) {
  612. mhi = lshift(mhi, m2);
  613. if (mhi == NULL)
  614. return (NULL);
  615. }
  616. /* Compute mlo -- check for special case
  617. * that d is a normalized power of 2.
  618. */
  619. mlo = mhi;
  620. if (spec_case) {
  621. mhi = Balloc(mhi->k);
  622. if (mhi == NULL)
  623. return (NULL);
  624. Bcopy(mhi, mlo);
  625. mhi = lshift(mhi, 1);
  626. if (mhi == NULL)
  627. return (NULL);
  628. }
  629. for(i = 1;;i++) {
  630. dig = quorem(b,S) + '0';
  631. /* Do we yet have the shortest decimal string
  632. * that will round to d?
  633. */
  634. j = cmp(b, mlo);
  635. delta = diff(S, mhi);
  636. if (delta == NULL)
  637. return (NULL);
  638. j1 = delta->sign ? 1 : cmp(b, delta);
  639. Bfree(delta);
  640. #ifndef ROUND_BIASED
  641. if (j1 == 0 && !mode && !(bits[0] & 1) && !rdir) {
  642. if (dig == '9')
  643. goto round_9_up;
  644. if (j <= 0) {
  645. if (b->wds > 1 || b->x[0])
  646. inex = STRTOG_Inexlo;
  647. }
  648. else {
  649. dig++;
  650. inex = STRTOG_Inexhi;
  651. }
  652. *s++ = dig;
  653. goto ret;
  654. }
  655. #endif
  656. if (j < 0 || (j == 0 && !mode
  657. #ifndef ROUND_BIASED
  658. && !(bits[0] & 1)
  659. #endif
  660. )) {
  661. if (rdir && (b->wds > 1 || b->x[0])) {
  662. if (rdir == 2) {
  663. inex = STRTOG_Inexlo;
  664. goto accept;
  665. }
  666. while (cmp(S,mhi) > 0) {
  667. *s++ = dig;
  668. mhi1 = multadd(mhi, 10, 0);
  669. if (mhi1 == NULL)
  670. return (NULL);
  671. if (mlo == mhi)
  672. mlo = mhi1;
  673. mhi = mhi1;
  674. b = multadd(b, 10, 0);
  675. if (b == NULL)
  676. return (NULL);
  677. dig = quorem(b,S) + '0';
  678. }
  679. if (dig++ == '9')
  680. goto round_9_up;
  681. inex = STRTOG_Inexhi;
  682. goto accept;
  683. }
  684. if (j1 > 0) {
  685. b = lshift(b, 1);
  686. if (b == NULL)
  687. return (NULL);
  688. j1 = cmp(b, S);
  689. #ifdef ROUND_BIASED
  690. if (j1 >= 0 /*)*/
  691. #else
  692. if ((j1 > 0 || (j1 == 0 && dig & 1))
  693. #endif
  694. && dig++ == '9')
  695. goto round_9_up;
  696. inex = STRTOG_Inexhi;
  697. }
  698. if (b->wds > 1 || b->x[0])
  699. inex = STRTOG_Inexlo;
  700. accept:
  701. *s++ = dig;
  702. goto ret;
  703. }
  704. if (j1 > 0 && rdir != 2) {
  705. if (dig == '9') { /* possible if i == 1 */
  706. round_9_up:
  707. *s++ = '9';
  708. inex = STRTOG_Inexhi;
  709. goto roundoff;
  710. }
  711. inex = STRTOG_Inexhi;
  712. *s++ = dig + 1;
  713. goto ret;
  714. }
  715. *s++ = dig;
  716. if (i == ilim)
  717. break;
  718. b = multadd(b, 10, 0);
  719. if (b == NULL)
  720. return (NULL);
  721. if (mlo == mhi) {
  722. mlo = mhi = multadd(mhi, 10, 0);
  723. if (mlo == NULL)
  724. return (NULL);
  725. }
  726. else {
  727. mlo = multadd(mlo, 10, 0);
  728. if (mlo == NULL)
  729. return (NULL);
  730. mhi = multadd(mhi, 10, 0);
  731. if (mhi == NULL)
  732. return (NULL);
  733. }
  734. }
  735. }
  736. else
  737. for(i = 1;; i++) {
  738. *s++ = dig = quorem(b,S) + '0';
  739. if (i >= ilim)
  740. break;
  741. b = multadd(b, 10, 0);
  742. if (b == NULL)
  743. return (NULL);
  744. }
  745. /* Round off last digit */
  746. if (rdir) {
  747. if (rdir == 2 || (b->wds <= 1 && !b->x[0]))
  748. goto chopzeros;
  749. goto roundoff;
  750. }
  751. b = lshift(b, 1);
  752. if (b == NULL)
  753. return (NULL);
  754. j = cmp(b, S);
  755. #ifdef ROUND_BIASED
  756. if (j >= 0)
  757. #else
  758. if (j > 0 || (j == 0 && dig & 1))
  759. #endif
  760. {
  761. roundoff:
  762. inex = STRTOG_Inexhi;
  763. while(*--s == '9')
  764. if (s == s0) {
  765. k++;
  766. *s++ = '1';
  767. goto ret;
  768. }
  769. ++*s++;
  770. }
  771. else {
  772. chopzeros:
  773. if (b->wds > 1 || b->x[0])
  774. inex = STRTOG_Inexlo;
  775. while(*--s == '0'){}
  776. ++s;
  777. }
  778. ret:
  779. Bfree(S);
  780. if (mhi) {
  781. if (mlo && mlo != mhi)
  782. Bfree(mlo);
  783. Bfree(mhi);
  784. }
  785. ret1:
  786. Bfree(b);
  787. *s = 0;
  788. *decpt = k + 1;
  789. if (rve)
  790. *rve = s;
  791. *kindp |= inex;
  792. return s0;
  793. }