dtoa.c 23 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. /* derived from /netlib/fp/dtoa.c assuming IEEE, Standard C */
  10. /* kudos to dmg@bell-labs.com, gripes to ehg@bell-labs.com */
  11. /* Let x be the exact mathematical number defined by a decimal
  12. * string s. Then atof(s) is the round-nearest-even IEEE
  13. * floating point value.
  14. * Let y be an IEEE floating point value and let s be the string
  15. * printed as %.17g. Then atof(s) is exactly y.
  16. */
  17. #include <u.h>
  18. #include <libc.h>
  19. static Lock _dtoalk[2];
  20. #define ACQUIRE_DTOA_LOCK(n) lock(&_dtoalk[n])
  21. #define FREE_DTOA_LOCK(n) unlock(&_dtoalk[n])
  22. #define PRIVATE_mem ((2000+sizeof(double)-1)/sizeof(double))
  23. static double private_mem[PRIVATE_mem], *pmem_next = private_mem;
  24. #define FLT_ROUNDS 1
  25. #define DBL_DIG 15
  26. #define DBL_MAX_10_EXP 308
  27. #define DBL_MAX_EXP 1024
  28. #define FLT_RADIX 2
  29. #define Storeinc(a,b,c) (*a++ = ((b << 16) | (c & 0xffff)))
  30. /* Ten_pmax = floor(P*log(2)/log(5)) */
  31. /* Bletch = (highest power of 2 < DBL_MAX_10_EXP) / 16 */
  32. /* Quick_max = floor((P-1)*log(FLT_RADIX)/log(10) - 1) */
  33. /* Int_max = floor(P*log(FLT_RADIX)/log(10) - 1) */
  34. #define Exp_shift 20
  35. #define Exp_shift1 20
  36. #define Exp_msk1 0x100000
  37. #define Exp_msk11 0x100000
  38. #define Exp_mask 0x7ff00000
  39. #define P 53
  40. #define Bias 1023
  41. #define Emin (-1022)
  42. #define Exp_1 0x3ff00000
  43. #define Exp_11 0x3ff00000
  44. #define Ebits 11
  45. #define Frac_mask 0xfffff
  46. #define Frac_mask1 0xfffff
  47. #define Ten_pmax 22
  48. #define Bletch 0x10
  49. #define Bndry_mask 0xfffff
  50. #define Bndry_mask1 0xfffff
  51. #define LSB 1
  52. #define Sign_bit 0x80000000
  53. #define Log2P 1
  54. #define Tiny0 0
  55. #define Tiny1 1
  56. #define Quick_max 14
  57. #define Int_max 14
  58. #define Avoid_Underflow
  59. #define rounded_product(a,b) a *= b
  60. #define rounded_quotient(a,b) a /= b
  61. #define Big0 (Frac_mask1 | Exp_msk1*(DBL_MAX_EXP+Bias-1))
  62. #define Big1 0xffffffff
  63. #define FFFFFFFF 0xffffffffUL
  64. #define Kmax 15
  65. typedef struct Bigint Bigint;
  66. typedef struct Ulongs Ulongs;
  67. struct Bigint {
  68. Bigint *next;
  69. int k, maxwds, sign, wds;
  70. unsigned x[1];
  71. };
  72. struct Ulongs {
  73. uint32_t hi;
  74. uint32_t lo;
  75. };
  76. static Bigint *freelist[Kmax+1];
  77. Ulongs
  78. double2ulongs(double d)
  79. {
  80. FPdbleword dw;
  81. Ulongs uls;
  82. dw.x = d;
  83. uls.hi = dw.hi;
  84. uls.lo = dw.lo;
  85. return uls;
  86. }
  87. double
  88. ulongs2double(Ulongs uls)
  89. {
  90. FPdbleword dw;
  91. dw.hi = uls.hi;
  92. dw.lo = uls.lo;
  93. return dw.x;
  94. }
  95. static Bigint *
  96. Balloc(int k)
  97. {
  98. int x;
  99. Bigint * rv;
  100. unsigned int len;
  101. ACQUIRE_DTOA_LOCK(0);
  102. if ((rv = freelist[k]) != nil) {
  103. freelist[k] = rv->next;
  104. } else {
  105. x = 1 << k;
  106. len = (sizeof(Bigint) + (x - 1) * sizeof(unsigned int) + sizeof(double) -1)
  107. / sizeof(double);
  108. if (pmem_next - private_mem + len <= PRIVATE_mem) {
  109. rv = (Bigint * )pmem_next;
  110. pmem_next += len;
  111. } else
  112. rv = (Bigint * )malloc(len * sizeof(Bigint));
  113. rv->k = k;
  114. rv->maxwds = x;
  115. }
  116. FREE_DTOA_LOCK(0);
  117. rv->sign = rv->wds = 0;
  118. return rv;
  119. }
  120. static void
  121. Bfree(Bigint *v)
  122. {
  123. if (v) {
  124. ACQUIRE_DTOA_LOCK(0);
  125. v->next = freelist[v->k];
  126. freelist[v->k] = v;
  127. FREE_DTOA_LOCK(0);
  128. }
  129. }
  130. #define Bcopy(x,y) memcpy((char *)&x->sign, (char *)&y->sign, \
  131. y->wds*sizeof(int) + 2*sizeof(int))
  132. static Bigint *
  133. multadd(Bigint *b, int m, int a) /* multiply by m and add a */
  134. {
  135. int i, wds;
  136. unsigned int carry, *x, y;
  137. unsigned int xi, z;
  138. Bigint * b1;
  139. wds = b->wds;
  140. x = b->x;
  141. i = 0;
  142. carry = a;
  143. do {
  144. xi = *x;
  145. y = (xi & 0xffff) * m + carry;
  146. z = (xi >> 16) * m + (y >> 16);
  147. carry = z >> 16;
  148. *x++ = (z << 16) + (y & 0xffff);
  149. } while (++i < wds);
  150. if (carry) {
  151. if (wds >= b->maxwds) {
  152. b1 = Balloc(b->k + 1);
  153. Bcopy(b1, b);
  154. Bfree(b);
  155. b = b1;
  156. }
  157. b->x[wds++] = carry;
  158. b->wds = wds;
  159. }
  160. return b;
  161. }
  162. static int
  163. hi0bits(register unsigned int x)
  164. {
  165. register int k = 0;
  166. if (!(x & 0xffff0000)) {
  167. k = 16;
  168. x <<= 16;
  169. }
  170. if (!(x & 0xff000000)) {
  171. k += 8;
  172. x <<= 8;
  173. }
  174. if (!(x & 0xf0000000)) {
  175. k += 4;
  176. x <<= 4;
  177. }
  178. if (!(x & 0xc0000000)) {
  179. k += 2;
  180. x <<= 2;
  181. }
  182. if (!(x & 0x80000000)) {
  183. k++;
  184. if (!(x & 0x40000000))
  185. return 32;
  186. }
  187. return k;
  188. }
  189. static int
  190. lo0bits(unsigned int *y)
  191. {
  192. register int k;
  193. register unsigned int x = *y;
  194. if (x & 7) {
  195. if (x & 1)
  196. return 0;
  197. if (x & 2) {
  198. *y = x >> 1;
  199. return 1;
  200. }
  201. *y = x >> 2;
  202. return 2;
  203. }
  204. k = 0;
  205. if (!(x & 0xffff)) {
  206. k = 16;
  207. x >>= 16;
  208. }
  209. if (!(x & 0xff)) {
  210. k += 8;
  211. x >>= 8;
  212. }
  213. if (!(x & 0xf)) {
  214. k += 4;
  215. x >>= 4;
  216. }
  217. if (!(x & 0x3)) {
  218. k += 2;
  219. x >>= 2;
  220. }
  221. if (!(x & 1)) {
  222. k++;
  223. x >>= 1;
  224. if (!(x & 1))
  225. return 32;
  226. }
  227. *y = x;
  228. return k;
  229. }
  230. static Bigint *
  231. i2b(int i)
  232. {
  233. Bigint * b;
  234. b = Balloc(1);
  235. b->x[0] = i;
  236. b->wds = 1;
  237. return b;
  238. }
  239. static Bigint *
  240. mult(Bigint *a, Bigint *b)
  241. {
  242. Bigint * c;
  243. int k, wa, wb, wc;
  244. unsigned int * x, *xa, *xae, *xb, *xbe, *xc, *xc0;
  245. unsigned int y;
  246. unsigned int carry, z;
  247. unsigned int z2;
  248. if (a->wds < b->wds) {
  249. c = a;
  250. a = b;
  251. b = c;
  252. }
  253. k = a->k;
  254. wa = a->wds;
  255. wb = b->wds;
  256. wc = wa + wb;
  257. if (wc > a->maxwds)
  258. k++;
  259. c = Balloc(k);
  260. for (x = c->x, xa = x + wc; x < xa; x++)
  261. *x = 0;
  262. xa = a->x;
  263. xae = xa + wa;
  264. xb = b->x;
  265. xbe = xb + wb;
  266. xc0 = c->x;
  267. for (; xb < xbe; xb++, xc0++) {
  268. if ((y = *xb & 0xffff) != 0) {
  269. x = xa;
  270. xc = xc0;
  271. carry = 0;
  272. do {
  273. z = (*x & 0xffff) * y + (*xc & 0xffff) + carry;
  274. carry = z >> 16;
  275. z2 = (*x++ >> 16) * y + (*xc >> 16) + carry;
  276. carry = z2 >> 16;
  277. Storeinc(xc, z2, z);
  278. } while (x < xae);
  279. *xc = carry;
  280. }
  281. if ((y = (*xb >> 16)) != 0) {
  282. x = xa;
  283. xc = xc0;
  284. carry = 0;
  285. z2 = *xc;
  286. do {
  287. z = (*x & 0xffff) * y + (*xc >> 16) + carry;
  288. carry = z >> 16;
  289. Storeinc(xc, z, z2);
  290. z2 = (*x++ >> 16) * y + (*xc & 0xffff) + carry;
  291. carry = z2 >> 16;
  292. } while (x < xae);
  293. *xc = z2;
  294. }
  295. }
  296. for (xc0 = c->x, xc = xc0 + wc; wc > 0 && !*--xc; --wc)
  297. ;
  298. c->wds = wc;
  299. return c;
  300. }
  301. static Bigint *p5s;
  302. static Bigint *
  303. pow5mult(Bigint *b, int k)
  304. {
  305. Bigint * b1, *p5, *p51;
  306. int i;
  307. static int p05[3] = {
  308. 5, 25, 125 };
  309. if ((i = (k & 3)) != 0)
  310. b = multadd(b, p05[i-1], 0);
  311. if (!(k >>= 2))
  312. return b;
  313. if (!(p5 = p5s)) {
  314. /* first time */
  315. ACQUIRE_DTOA_LOCK(1);
  316. if (!(p5 = p5s)) {
  317. p5 = p5s = i2b(625);
  318. p5->next = 0;
  319. }
  320. FREE_DTOA_LOCK(1);
  321. }
  322. for (; ; ) {
  323. if (k & 1) {
  324. b1 = mult(b, p5);
  325. Bfree(b);
  326. b = b1;
  327. }
  328. if (!(k >>= 1))
  329. break;
  330. if (!(p51 = p5->next)) {
  331. ACQUIRE_DTOA_LOCK(1);
  332. if (!(p51 = p5->next)) {
  333. p51 = p5->next = mult(p5, p5);
  334. p51->next = 0;
  335. }
  336. FREE_DTOA_LOCK(1);
  337. }
  338. p5 = p51;
  339. }
  340. return b;
  341. }
  342. static Bigint *
  343. lshift(Bigint *b, int k)
  344. {
  345. int i, k1, n, n1;
  346. Bigint * b1;
  347. unsigned int * x, *x1, *xe, z;
  348. n = k >> 5;
  349. k1 = b->k;
  350. n1 = n + b->wds + 1;
  351. for (i = b->maxwds; n1 > i; i <<= 1)
  352. k1++;
  353. b1 = Balloc(k1);
  354. x1 = b1->x;
  355. for (i = 0; i < n; i++)
  356. *x1++ = 0;
  357. x = b->x;
  358. xe = x + b->wds;
  359. if (k &= 0x1f) {
  360. k1 = 32 - k;
  361. z = 0;
  362. do {
  363. *x1++ = *x << k | z;
  364. z = *x++ >> k1;
  365. } while (x < xe);
  366. if ((*x1 = z) != 0)
  367. ++n1;
  368. } else
  369. do
  370. *x1++ = *x++;
  371. while (x < xe);
  372. b1->wds = n1 - 1;
  373. Bfree(b);
  374. return b1;
  375. }
  376. static int
  377. cmp(Bigint *a, Bigint *b)
  378. {
  379. unsigned int * xa, *xa0, *xb, *xb0;
  380. int i, j;
  381. i = a->wds;
  382. j = b->wds;
  383. if (i -= j)
  384. return i;
  385. xa0 = a->x;
  386. xa = xa0 + j;
  387. xb0 = b->x;
  388. xb = xb0 + j;
  389. for (; ; ) {
  390. if (*--xa != *--xb)
  391. return * xa < *xb ? -1 : 1;
  392. if (xa <= xa0)
  393. break;
  394. }
  395. return 0;
  396. }
  397. static Bigint *
  398. diff(Bigint *a, Bigint *b)
  399. {
  400. Bigint * c;
  401. int i, wa, wb;
  402. unsigned int * xa, *xae, *xb, *xbe, *xc;
  403. unsigned int borrow, y;
  404. unsigned int z;
  405. i = cmp(a, b);
  406. if (!i) {
  407. c = Balloc(0);
  408. c->wds = 1;
  409. c->x[0] = 0;
  410. return c;
  411. }
  412. if (i < 0) {
  413. c = a;
  414. a = b;
  415. b = c;
  416. i = 1;
  417. } else
  418. i = 0;
  419. c = Balloc(a->k);
  420. c->sign = i;
  421. wa = a->wds;
  422. xa = a->x;
  423. xae = xa + wa;
  424. wb = b->wds;
  425. xb = b->x;
  426. xbe = xb + wb;
  427. xc = c->x;
  428. borrow = 0;
  429. do {
  430. y = (*xa & 0xffff) - (*xb & 0xffff) - borrow;
  431. borrow = (y & 0x10000) >> 16;
  432. z = (*xa++ >> 16) - (*xb++ >> 16) - borrow;
  433. borrow = (z & 0x10000) >> 16;
  434. Storeinc(xc, z, y);
  435. } while (xb < xbe);
  436. while (xa < xae) {
  437. y = (*xa & 0xffff) - borrow;
  438. borrow = (y & 0x10000) >> 16;
  439. z = (*xa++ >> 16) - borrow;
  440. borrow = (z & 0x10000) >> 16;
  441. Storeinc(xc, z, y);
  442. }
  443. while (!*--xc)
  444. wa--;
  445. c->wds = wa;
  446. return c;
  447. }
  448. static Bigint *
  449. d2b(double d, int *e, int *bits)
  450. {
  451. Bigint * b;
  452. int de, i, k;
  453. unsigned *x, y, z;
  454. Ulongs uls;
  455. b = Balloc(1);
  456. x = b->x;
  457. uls = double2ulongs(d);
  458. z = uls.hi & Frac_mask;
  459. uls.hi &= 0x7fffffff; /* clear sign bit, which we ignore */
  460. de = (int)(uls.hi >> Exp_shift);
  461. z |= Exp_msk11;
  462. if ((y = uls.lo) != 0) {
  463. if ((k = lo0bits(&y)) != 0) {
  464. x[0] = y | z << (32 - k);
  465. z >>= k;
  466. } else
  467. x[0] = y;
  468. i = b->wds = (x[1] = z) ? 2 : 1;
  469. } else {
  470. k = lo0bits(&z);
  471. x[0] = z;
  472. i = b->wds = 1;
  473. k += 32;
  474. }
  475. USED(i);
  476. *e = de - Bias - (P - 1) + k;
  477. *bits = P - k;
  478. return b;
  479. }
  480. static const double
  481. tens[] = {
  482. 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9,
  483. 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19,
  484. 1e20, 1e21, 1e22
  485. };
  486. static const double
  487. bigtens[] = {
  488. 1e16, 1e32, 1e64, 1e128, 1e256 };
  489. #define Scale_Bit 0x10
  490. #define n_bigtens 5
  491. #define NAN_WORD0 0x7ff80000
  492. #define NAN_WORD1 0
  493. static int
  494. quorem(Bigint *b, Bigint *S)
  495. {
  496. int n;
  497. unsigned int * bx, *bxe, q, *sx, *sxe;
  498. unsigned int borrow, carry, y, ys;
  499. unsigned int si, z, zs;
  500. n = S->wds;
  501. if (b->wds < n)
  502. return 0;
  503. sx = S->x;
  504. sxe = sx + --n;
  505. bx = b->x;
  506. bxe = bx + n;
  507. q = *bxe / (*sxe + 1); /* ensure q <= true quotient */
  508. if (q) {
  509. borrow = 0;
  510. carry = 0;
  511. do {
  512. si = *sx++;
  513. ys = (si & 0xffff) * q + carry;
  514. zs = (si >> 16) * q + (ys >> 16);
  515. carry = zs >> 16;
  516. y = (*bx & 0xffff) - (ys & 0xffff) - borrow;
  517. borrow = (y & 0x10000) >> 16;
  518. z = (*bx >> 16) - (zs & 0xffff) - borrow;
  519. borrow = (z & 0x10000) >> 16;
  520. Storeinc(bx, z, y);
  521. } while (sx <= sxe);
  522. if (!*bxe) {
  523. bx = b->x;
  524. while (--bxe > bx && !*bxe)
  525. --n;
  526. b->wds = n;
  527. }
  528. }
  529. if (cmp(b, S) >= 0) {
  530. q++;
  531. borrow = 0;
  532. carry = 0;
  533. bx = b->x;
  534. sx = S->x;
  535. do {
  536. si = *sx++;
  537. ys = (si & 0xffff) + carry;
  538. zs = (si >> 16) + (ys >> 16);
  539. carry = zs >> 16;
  540. y = (*bx & 0xffff) - (ys & 0xffff) - borrow;
  541. borrow = (y & 0x10000) >> 16;
  542. z = (*bx >> 16) - (zs & 0xffff) - borrow;
  543. borrow = (z & 0x10000) >> 16;
  544. Storeinc(bx, z, y);
  545. } while (sx <= sxe);
  546. bx = b->x;
  547. bxe = bx + n;
  548. if (!*bxe) {
  549. while (--bxe > bx && !*bxe)
  550. --n;
  551. b->wds = n;
  552. }
  553. }
  554. return q;
  555. }
  556. static char *
  557. rv_alloc(int i)
  558. {
  559. int j, k, *r;
  560. j = sizeof(unsigned int);
  561. for (k = 0;
  562. sizeof(Bigint) - sizeof(unsigned int) - sizeof(int) + j <= i;
  563. j <<= 1)
  564. k++;
  565. r = (int * )Balloc(k);
  566. *r = k;
  567. return
  568. (char *)(r + 1);
  569. }
  570. static char *
  571. nrv_alloc(char *s, char **rve, int n)
  572. {
  573. char *rv, *t;
  574. t = rv = rv_alloc(n);
  575. while ((*t = *s++) != 0)
  576. t++;
  577. if (rve)
  578. *rve = t;
  579. return rv;
  580. }
  581. /* freedtoa(s) must be used to free values s returned by dtoa
  582. * when MULTIPLE_THREADS is #defined. It should be used in all cases,
  583. * but for consistency with earlier versions of dtoa, it is optional
  584. * when MULTIPLE_THREADS is not defined.
  585. */
  586. void
  587. freedtoa(char *s)
  588. {
  589. Bigint * b = (Bigint * )((int *)s - 1);
  590. b->maxwds = 1 << (b->k = *(int * )b);
  591. Bfree(b);
  592. }
  593. /* dtoa for IEEE arithmetic (dmg): convert double to ASCII string.
  594. *
  595. * Inspired by "How to Print Floating-Point Numbers Accurately" by
  596. * Guy L. Steele, Jr. and Jon L. White [Proc. ACM SIGPLAN '90, pp. 92-101].
  597. *
  598. * Modifications:
  599. * 1. Rather than iterating, we use a simple numeric overestimate
  600. * to determine k = floor(log10(d)). We scale relevant
  601. * quantities using O(log2(k)) rather than O(k) multiplications.
  602. * 2. For some modes > 2 (corresponding to ecvt and fcvt), we don't
  603. * try to generate digits strictly left to right. Instead, we
  604. * compute with fewer bits and propagate the carry if necessary
  605. * when rounding the final digit up. This is often faster.
  606. * 3. Under the assumption that input will be rounded nearest,
  607. * mode 0 renders 1e23 as 1e23 rather than 9.999999999999999e22.
  608. * That is, we allow equality in stopping tests when the
  609. * round-nearest rule will give the same floating-point value
  610. * as would satisfaction of the stopping test with strict
  611. * inequality.
  612. * 4. We remove common factors of powers of 2 from relevant
  613. * quantities.
  614. * 5. When converting floating-point integers less than 1e16,
  615. * we use floating-point arithmetic rather than resorting
  616. * to multiple-precision integers.
  617. * 6. When asked to produce fewer than 15 digits, we first try
  618. * to get by with floating-point arithmetic; we resort to
  619. * multiple-precision integer arithmetic only if we cannot
  620. * guarantee that the floating-point calculation has given
  621. * the correctly rounded result. For k requested digits and
  622. * "uniformly" distributed input, the probability is
  623. * something like 10^(k-15) that we must resort to the int
  624. * calculation.
  625. */
  626. char *
  627. dtoa(double d, int mode, int ndigits, int *decpt, int *sign, char **rve)
  628. {
  629. /* Arguments ndigits, decpt, sign are similar to those
  630. of ecvt and fcvt; trailing zeros are suppressed from
  631. the returned string. If not null, *rve is set to point
  632. to the end of the return value. If d is +-Infinity or NaN,
  633. then *decpt is set to 9999.
  634. mode:
  635. 0 ==> shortest string that yields d when read in
  636. and rounded to nearest.
  637. 1 ==> like 0, but with Steele & White stopping rule;
  638. e.g. with IEEE P754 arithmetic , mode 0 gives
  639. 1e23 whereas mode 1 gives 9.999999999999999e22.
  640. 2 ==> max(1,ndigits) significant digits. This gives a
  641. return value similar to that of ecvt, except
  642. that trailing zeros are suppressed.
  643. 3 ==> through ndigits past the decimal point. This
  644. gives a return value similar to that from fcvt,
  645. except that trailing zeros are suppressed, and
  646. ndigits can be negative.
  647. 4-9 should give the same return values as 2-3, i.e.,
  648. 4 <= mode <= 9 ==> same return as mode
  649. 2 + (mode & 1). These modes are mainly for
  650. debugging; often they run slower but sometimes
  651. faster than modes 2-3.
  652. 4,5,8,9 ==> left-to-right digit generation.
  653. 6-9 ==> don't try fast floating-point estimate
  654. (if applicable).
  655. Values of mode other than 0-9 are treated as mode 0.
  656. Sufficient space is allocated to the return value
  657. to hold the suppressed trailing zeros.
  658. */
  659. int bbits, b2, b5, be, dig, i, ieps, ilim, ilim0, ilim1,
  660. j, j1, k, k0, k_check, L, leftright, m2, m5, s2, s5,
  661. spec_case, try_quick;
  662. Bigint * b, *b1, *delta, *mlo=nil, *mhi, *S;
  663. double d2, ds, eps;
  664. char *s, *s0;
  665. Ulongs ulsd, ulsd2;
  666. ulsd = double2ulongs(d);
  667. if (ulsd.hi & Sign_bit) {
  668. /* set sign for everything, including 0's and NaNs */
  669. *sign = 1;
  670. ulsd.hi &= ~Sign_bit; /* clear sign bit */
  671. } else
  672. *sign = 0;
  673. if ((ulsd.hi & Exp_mask) == Exp_mask) {
  674. /* Infinity or NaN */
  675. *decpt = 9999;
  676. if (!ulsd.lo && !(ulsd.hi & 0xfffff))
  677. return nrv_alloc("Infinity", rve, 8);
  678. return nrv_alloc("NaN", rve, 3);
  679. }
  680. d = ulongs2double(ulsd);
  681. if (!d) {
  682. *decpt = 1;
  683. return nrv_alloc("0", rve, 1);
  684. }
  685. b = d2b(d, &be, &bbits);
  686. i = (int)(ulsd.hi >> Exp_shift1 & (Exp_mask >> Exp_shift1));
  687. ulsd2 = ulsd;
  688. ulsd2.hi &= Frac_mask1;
  689. ulsd2.hi |= Exp_11;
  690. d2 = ulongs2double(ulsd2);
  691. /* log(x) ~=~ log(1.5) + (x-1.5)/1.5
  692. * log10(x) = log(x) / log(10)
  693. * ~=~ log(1.5)/log(10) + (x-1.5)/(1.5*log(10))
  694. * log10(d) = (i-Bias)*log(2)/log(10) + log10(d2)
  695. *
  696. * This suggests computing an approximation k to log10(d) by
  697. *
  698. * k = (i - Bias)*0.301029995663981
  699. * + ( (d2-1.5)*0.289529654602168 + 0.176091259055681 );
  700. *
  701. * We want k to be too large rather than too small.
  702. * The error in the first-order Taylor series approximation
  703. * is in our favor, so we just round up the constant enough
  704. * to compensate for any error in the multiplication of
  705. * (i - Bias) by 0.301029995663981; since |i - Bias| <= 1077,
  706. * and 1077 * 0.30103 * 2^-52 ~=~ 7.2e-14,
  707. * adding 1e-13 to the constant term more than suffices.
  708. * Hence we adjust the constant term to 0.1760912590558.
  709. * (We could get a more accurate k by invoking log10,
  710. * but this is probably not worthwhile.)
  711. */
  712. i -= Bias;
  713. ds = (d2 - 1.5) * 0.289529654602168 + 0.1760912590558 + i * 0.301029995663981;
  714. k = (int)ds;
  715. if (ds < 0. && ds != k)
  716. k--; /* want k = floor(ds) */
  717. k_check = 1;
  718. if (k >= 0 && k <= Ten_pmax) {
  719. if (d < tens[k])
  720. k--;
  721. k_check = 0;
  722. }
  723. j = bbits - i - 1;
  724. if (j >= 0) {
  725. b2 = 0;
  726. s2 = j;
  727. } else {
  728. b2 = -j;
  729. s2 = 0;
  730. }
  731. if (k >= 0) {
  732. b5 = 0;
  733. s5 = k;
  734. s2 += k;
  735. } else {
  736. b2 -= k;
  737. b5 = -k;
  738. s5 = 0;
  739. }
  740. if (mode < 0 || mode > 9)
  741. mode = 0;
  742. try_quick = 1;
  743. if (mode > 5) {
  744. mode -= 4;
  745. try_quick = 0;
  746. }
  747. leftright = 1;
  748. switch (mode) {
  749. case 0:
  750. case 1:
  751. default:
  752. ilim = ilim1 = -1;
  753. i = 18;
  754. ndigits = 0;
  755. break;
  756. case 2:
  757. leftright = 0;
  758. /* no break */
  759. case 4:
  760. if (ndigits <= 0)
  761. ndigits = 1;
  762. ilim = ilim1 = i = ndigits;
  763. break;
  764. case 3:
  765. leftright = 0;
  766. /* no break */
  767. case 5:
  768. i = ndigits + k + 1;
  769. ilim = i;
  770. ilim1 = i - 1;
  771. if (i <= 0)
  772. i = 1;
  773. }
  774. s = s0 = rv_alloc(i);
  775. if (ilim >= 0 && ilim <= Quick_max && try_quick) {
  776. /* Try to get by with floating-point arithmetic. */
  777. i = 0;
  778. d2 = d;
  779. k0 = k;
  780. ilim0 = ilim;
  781. ieps = 2; /* conservative */
  782. if (k > 0) {
  783. ds = tens[k&0xf];
  784. j = k >> 4;
  785. if (j & Bletch) {
  786. /* prevent overflows */
  787. j &= Bletch - 1;
  788. d /= bigtens[n_bigtens-1];
  789. ieps++;
  790. }
  791. for (; j; j >>= 1, i++)
  792. if (j & 1) {
  793. ieps++;
  794. ds *= bigtens[i];
  795. }
  796. d /= ds;
  797. } else if ((j1 = -k) != 0) {
  798. d *= tens[j1 & 0xf];
  799. for (j = j1 >> 4; j; j >>= 1, i++)
  800. if (j & 1) {
  801. ieps++;
  802. d *= bigtens[i];
  803. }
  804. }
  805. if (k_check && d < 1. && ilim > 0) {
  806. if (ilim1 <= 0)
  807. goto fast_failed;
  808. ilim = ilim1;
  809. k--;
  810. d *= 10.;
  811. ieps++;
  812. }
  813. eps = ieps * d + 7.;
  814. ulsd = double2ulongs(eps);
  815. ulsd.hi -= (P - 1) * Exp_msk1;
  816. eps = ulongs2double(ulsd);
  817. if (ilim == 0) {
  818. S = mhi = 0;
  819. d -= 5.;
  820. if (d > eps)
  821. goto one_digit;
  822. if (d < -eps)
  823. goto no_digits;
  824. goto fast_failed;
  825. }
  826. /* Generate ilim digits, then fix them up. */
  827. eps *= tens[ilim-1];
  828. for (i = 1; ; i++, d *= 10.) {
  829. L = d;
  830. // assert(L < 10);
  831. d -= L;
  832. *s++ = '0' + (int)L;
  833. if (i == ilim) {
  834. if (d > 0.5 + eps)
  835. goto bump_up;
  836. else if (d < 0.5 - eps) {
  837. while (*--s == '0')
  838. ;
  839. s++;
  840. goto ret1;
  841. }
  842. break;
  843. }
  844. }
  845. fast_failed:
  846. s = s0;
  847. d = d2;
  848. k = k0;
  849. ilim = ilim0;
  850. }
  851. /* Do we have a "small" integer? */
  852. if (be >= 0 && k <= Int_max) {
  853. /* Yes. */
  854. ds = tens[k];
  855. if (ndigits < 0 && ilim <= 0) {
  856. S = mhi = 0;
  857. if (ilim < 0 || d <= 5 * ds)
  858. goto no_digits;
  859. goto one_digit;
  860. }
  861. for (i = 1; ; i++) {
  862. L = d / ds;
  863. d -= L * ds;
  864. *s++ = '0' + (int)L;
  865. if (i == ilim) {
  866. d += d;
  867. if (d > ds || (d == ds && L & 1)) {
  868. bump_up:
  869. while (*--s == '9')
  870. if (s == s0) {
  871. k++;
  872. *s = '0';
  873. break;
  874. }
  875. ++ * s++;
  876. }
  877. break;
  878. }
  879. if (!(d *= 10.))
  880. break;
  881. }
  882. goto ret1;
  883. }
  884. m2 = b2;
  885. m5 = b5;
  886. mhi = mlo = 0;
  887. if (leftright) {
  888. if (mode < 2) {
  889. i =
  890. 1 + P - bbits;
  891. } else {
  892. j = ilim - 1;
  893. if (m5 >= j)
  894. m5 -= j;
  895. else {
  896. s5 += j -= m5;
  897. b5 += j;
  898. m5 = 0;
  899. }
  900. if ((i = ilim) < 0) {
  901. m2 -= i;
  902. i = 0;
  903. }
  904. }
  905. b2 += i;
  906. s2 += i;
  907. mhi = i2b(1);
  908. }
  909. if (m2 > 0 && s2 > 0) {
  910. i = m2 < s2 ? m2 : s2;
  911. b2 -= i;
  912. m2 -= i;
  913. s2 -= i;
  914. }
  915. if (b5 > 0) {
  916. if (leftright) {
  917. if (m5 > 0) {
  918. mhi = pow5mult(mhi, m5);
  919. b1 = mult(mhi, b);
  920. Bfree(b);
  921. b = b1;
  922. }
  923. if ((j = b5 - m5) != 0)
  924. b = pow5mult(b, j);
  925. } else
  926. b = pow5mult(b, b5);
  927. }
  928. S = i2b(1);
  929. if (s5 > 0)
  930. S = pow5mult(S, s5);
  931. /* Check for special case that d is a normalized power of 2. */
  932. spec_case = 0;
  933. if (mode < 2) {
  934. ulsd = double2ulongs(d);
  935. if (!ulsd.lo && !(ulsd.hi & Bndry_mask)) {
  936. /* The special case */
  937. b2 += Log2P;
  938. s2 += Log2P;
  939. spec_case = 1;
  940. }
  941. }
  942. /* Arrange for convenient computation of quotients:
  943. * shift left if necessary so divisor has 4 leading 0 bits.
  944. *
  945. * Perhaps we should just compute leading 28 bits of S once
  946. * and for all and pass them and a shift to quorem, so it
  947. * can do shifts and ors to compute the numerator for q.
  948. */
  949. if ((i = ((s5 ? 32 - hi0bits(S->x[S->wds-1]) : 1) + s2) & 0x1f) != 0)
  950. i = 32 - i;
  951. if (i > 4) {
  952. i -= 4;
  953. b2 += i;
  954. m2 += i;
  955. s2 += i;
  956. } else if (i < 4) {
  957. i += 28;
  958. b2 += i;
  959. m2 += i;
  960. s2 += i;
  961. }
  962. if (b2 > 0)
  963. b = lshift(b, b2);
  964. if (s2 > 0)
  965. S = lshift(S, s2);
  966. if (k_check) {
  967. if (cmp(b, S) < 0) {
  968. k--;
  969. b = multadd(b, 10, 0); /* we botched the k estimate */
  970. if (leftright)
  971. mhi = multadd(mhi, 10, 0);
  972. ilim = ilim1;
  973. }
  974. }
  975. if (ilim <= 0 && mode > 2) {
  976. if (ilim < 0 || cmp(b, S = multadd(S, 5, 0)) <= 0) {
  977. /* no digits, fcvt style */
  978. no_digits:
  979. k = -1 - ndigits;
  980. goto ret;
  981. }
  982. one_digit:
  983. *s++ = '1';
  984. k++;
  985. goto ret;
  986. }
  987. if (leftright) {
  988. if (m2 > 0)
  989. mhi = lshift(mhi, m2);
  990. /* Compute mlo -- check for special case
  991. * that d is a normalized power of 2.
  992. */
  993. mlo = mhi;
  994. if (spec_case) {
  995. mhi = Balloc(mhi->k);
  996. Bcopy(mhi, mlo);
  997. mhi = lshift(mhi, Log2P);
  998. }
  999. for (i = 1; ; i++) {
  1000. dig = quorem(b, S) + '0';
  1001. /* Do we yet have the shortest decimal string
  1002. * that will round to d?
  1003. */
  1004. j = cmp(b, mlo);
  1005. delta = diff(S, mhi);
  1006. j1 = delta->sign ? 1 : cmp(b, delta);
  1007. Bfree(delta);
  1008. ulsd = double2ulongs(d);
  1009. if (j1 == 0 && !mode && !(ulsd.lo & 1)) {
  1010. if (dig == '9')
  1011. goto round_9_up;
  1012. if (j > 0)
  1013. dig++;
  1014. *s++ = dig;
  1015. goto ret;
  1016. }
  1017. if (j < 0 || (j == 0 && !mode && !(ulsd.lo & 1))) {
  1018. if (j1 > 0) {
  1019. b = lshift(b, 1);
  1020. j1 = cmp(b, S);
  1021. if ((j1 > 0 || (j1 == 0 && dig & 1))
  1022. && dig++ == '9')
  1023. goto round_9_up;
  1024. }
  1025. *s++ = dig;
  1026. goto ret;
  1027. }
  1028. if (j1 > 0) {
  1029. if (dig == '9') { /* possible if i == 1 */
  1030. round_9_up:
  1031. *s++ = '9';
  1032. goto roundoff;
  1033. }
  1034. *s++ = dig + 1;
  1035. goto ret;
  1036. }
  1037. *s++ = dig;
  1038. if (i == ilim)
  1039. break;
  1040. b = multadd(b, 10, 0);
  1041. if (mlo == mhi)
  1042. mlo = mhi = multadd(mhi, 10, 0);
  1043. else {
  1044. mlo = multadd(mlo, 10, 0);
  1045. mhi = multadd(mhi, 10, 0);
  1046. }
  1047. }
  1048. } else
  1049. for (i = 1; ; i++) {
  1050. *s++ = dig = quorem(b, S) + '0';
  1051. if (i >= ilim)
  1052. break;
  1053. b = multadd(b, 10, 0);
  1054. }
  1055. /* Round off last digit */
  1056. b = lshift(b, 1);
  1057. j = cmp(b, S);
  1058. if (j > 0 || (j == 0 && dig & 1)) {
  1059. roundoff:
  1060. while (*--s == '9')
  1061. if (s == s0) {
  1062. k++;
  1063. *s++ = '1';
  1064. goto ret;
  1065. }
  1066. ++ * s++;
  1067. } else {
  1068. while (*--s == '0')
  1069. ;
  1070. s++;
  1071. }
  1072. ret:
  1073. Bfree(S);
  1074. if (mhi) {
  1075. if (mlo && mlo != mhi)
  1076. Bfree(mlo);
  1077. Bfree(mhi);
  1078. }
  1079. ret1:
  1080. Bfree(b);
  1081. *s = 0;
  1082. *decpt = k + 1;
  1083. if (rve)
  1084. *rve = s;
  1085. return s0;
  1086. }