dtoa.c 23 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181
  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 <lib9.h>
  19. static Lock _dtoalk[2];
  20. #define ACQUIRE_DTOA_LOCK(n) jehanne_lock(&_dtoalk[n])
  21. #define FREE_DTOA_LOCK(n) jehanne_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]) {
  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(double));
  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) {
  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) {
  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)
  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)
  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) { /* assignment = */
  463. if (k = lo0bits(&y)) { /* assignment = */
  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. /*
  490. static const double tinytens[] = {
  491. 1e-16, 1e-32, 1e-64, 1e-128,
  492. 9007199254740992.e-256
  493. };
  494. */
  495. /* The factor of 2^53 in tinytens[4] helps us avoid setting the underflow */
  496. /* flag unnecessarily. It leads to a song and dance at the end of strtod. */
  497. #define Scale_Bit 0x10
  498. #define n_bigtens 5
  499. #define NAN_WORD0 0x7ff80000
  500. #define NAN_WORD1 0
  501. static int
  502. quorem(Bigint *b, Bigint *S)
  503. {
  504. int n;
  505. unsigned int * bx, *bxe, q, *sx, *sxe;
  506. unsigned int borrow, carry, y, ys;
  507. unsigned int si, z, zs;
  508. n = S->wds;
  509. if (b->wds < n)
  510. return 0;
  511. sx = S->x;
  512. sxe = sx + --n;
  513. bx = b->x;
  514. bxe = bx + n;
  515. q = *bxe / (*sxe + 1); /* ensure q <= true quotient */
  516. if (q) {
  517. borrow = 0;
  518. carry = 0;
  519. do {
  520. si = *sx++;
  521. ys = (si & 0xffff) * q + carry;
  522. zs = (si >> 16) * q + (ys >> 16);
  523. carry = zs >> 16;
  524. y = (*bx & 0xffff) - (ys & 0xffff) - borrow;
  525. borrow = (y & 0x10000) >> 16;
  526. z = (*bx >> 16) - (zs & 0xffff) - borrow;
  527. borrow = (z & 0x10000) >> 16;
  528. Storeinc(bx, z, y);
  529. } while (sx <= sxe);
  530. if (!*bxe) {
  531. bx = b->x;
  532. while (--bxe > bx && !*bxe)
  533. --n;
  534. b->wds = n;
  535. }
  536. }
  537. if (cmp(b, S) >= 0) {
  538. q++;
  539. borrow = 0;
  540. carry = 0;
  541. bx = b->x;
  542. sx = S->x;
  543. do {
  544. si = *sx++;
  545. ys = (si & 0xffff) + carry;
  546. zs = (si >> 16) + (ys >> 16);
  547. carry = zs >> 16;
  548. y = (*bx & 0xffff) - (ys & 0xffff) - borrow;
  549. borrow = (y & 0x10000) >> 16;
  550. z = (*bx >> 16) - (zs & 0xffff) - borrow;
  551. borrow = (z & 0x10000) >> 16;
  552. Storeinc(bx, z, y);
  553. } while (sx <= sxe);
  554. bx = b->x;
  555. bxe = bx + n;
  556. if (!*bxe) {
  557. while (--bxe > bx && !*bxe)
  558. --n;
  559. b->wds = n;
  560. }
  561. }
  562. return q;
  563. }
  564. static char *
  565. rv_alloc(int i)
  566. {
  567. int j, k, *r;
  568. j = sizeof(unsigned int);
  569. for (k = 0;
  570. sizeof(Bigint) - sizeof(unsigned int) - sizeof(int) + j <= i;
  571. j <<= 1)
  572. k++;
  573. r = (int * )Balloc(k);
  574. *r = k;
  575. return
  576. (char *)(r + 1);
  577. }
  578. static char *
  579. nrv_alloc(char *s, char **rve, int n)
  580. {
  581. char *rv, *t;
  582. t = rv = rv_alloc(n);
  583. while (*t = *s++)
  584. t++;
  585. if (rve)
  586. *rve = t;
  587. return rv;
  588. }
  589. /* freedtoa(s) must be used to free values s returned by dtoa
  590. * when MULTIPLE_THREADS is #defined. It should be used in all cases,
  591. * but for consistency with earlier versions of dtoa, it is optional
  592. * when MULTIPLE_THREADS is not defined.
  593. */
  594. void
  595. freedtoa(char *s)
  596. {
  597. Bigint * b = (Bigint * )((int *)s - 1);
  598. b->maxwds = 1 << (b->k = *(int * )b);
  599. Bfree(b);
  600. }
  601. /* dtoa for IEEE arithmetic (dmg): convert double to ASCII string.
  602. *
  603. * Inspired by "How to Print Floating-Point Numbers Accurately" by
  604. * Guy L. Steele, Jr. and Jon L. White [Proc. ACM SIGPLAN '90, pp. 92-101].
  605. *
  606. * Modifications:
  607. * 1. Rather than iterating, we use a simple numeric overestimate
  608. * to determine k = floor(log10(d)). We scale relevant
  609. * quantities using O(log2(k)) rather than O(k) multiplications.
  610. * 2. For some modes > 2 (corresponding to ecvt and fcvt), we don't
  611. * try to generate digits strictly left to right. Instead, we
  612. * compute with fewer bits and propagate the carry if necessary
  613. * when rounding the final digit up. This is often faster.
  614. * 3. Under the assumption that input will be rounded nearest,
  615. * mode 0 renders 1e23 as 1e23 rather than 9.999999999999999e22.
  616. * That is, we allow equality in stopping tests when the
  617. * round-nearest rule will give the same floating-point value
  618. * as would satisfaction of the stopping test with strict
  619. * inequality.
  620. * 4. We remove common factors of powers of 2 from relevant
  621. * quantities.
  622. * 5. When converting floating-point integers less than 1e16,
  623. * we use floating-point arithmetic rather than resorting
  624. * to multiple-precision integers.
  625. * 6. When asked to produce fewer than 15 digits, we first try
  626. * to get by with floating-point arithmetic; we resort to
  627. * multiple-precision integer arithmetic only if we cannot
  628. * guarantee that the floating-point calculation has given
  629. * the correctly rounded result. For k requested digits and
  630. * "uniformly" distributed input, the probability is
  631. * something like 10^(k-15) that we must resort to the int
  632. * calculation.
  633. */
  634. char *
  635. dtoa(double d, int mode, int ndigits, int *decpt, int *sign, char **rve)
  636. {
  637. /* Arguments ndigits, decpt, sign are similar to those
  638. of ecvt and fcvt; trailing zeros are suppressed from
  639. the returned string. If not null, *rve is set to point
  640. to the end of the return value. If d is +-Infinity or NaN,
  641. then *decpt is set to 9999.
  642. mode:
  643. 0 ==> shortest string that yields d when read in
  644. and rounded to nearest.
  645. 1 ==> like 0, but with Steele & White stopping rule;
  646. e.g. with IEEE P754 arithmetic , mode 0 gives
  647. 1e23 whereas mode 1 gives 9.999999999999999e22.
  648. 2 ==> max(1,ndigits) significant digits. This gives a
  649. return value similar to that of ecvt, except
  650. that trailing zeros are suppressed.
  651. 3 ==> through ndigits past the decimal point. This
  652. gives a return value similar to that from fcvt,
  653. except that trailing zeros are suppressed, and
  654. ndigits can be negative.
  655. 4-9 should give the same return values as 2-3, i.e.,
  656. 4 <= mode <= 9 ==> same return as mode
  657. 2 + (mode & 1). These modes are mainly for
  658. debugging; often they run slower but sometimes
  659. faster than modes 2-3.
  660. 4,5,8,9 ==> left-to-right digit generation.
  661. 6-9 ==> don't try fast floating-point estimate
  662. (if applicable).
  663. Values of mode other than 0-9 are treated as mode 0.
  664. Sufficient space is allocated to the return value
  665. to hold the suppressed trailing zeros.
  666. */
  667. int bbits, b2, b5, be, dig, i, ieps, ilim, ilim0, ilim1,
  668. j, j1, k, k0, k_check, L, leftright, m2, m5, s2, s5,
  669. spec_case, try_quick;
  670. Bigint * b, *b1, *delta, *mlo=nil, *mhi, *S;
  671. double d2, ds, eps;
  672. char *s, *s0;
  673. Ulongs ulsd, ulsd2;
  674. ulsd = double2ulongs(d);
  675. if (ulsd.hi & Sign_bit) {
  676. /* set sign for everything, including 0's and NaNs */
  677. *sign = 1;
  678. ulsd.hi &= ~Sign_bit; /* clear sign bit */
  679. } else
  680. *sign = 0;
  681. if ((ulsd.hi & Exp_mask) == Exp_mask) {
  682. /* Infinity or NaN */
  683. *decpt = 9999;
  684. if (!ulsd.lo && !(ulsd.hi & 0xfffff))
  685. return nrv_alloc("Infinity", rve, 8);
  686. return nrv_alloc("NaN", rve, 3);
  687. }
  688. d = ulongs2double(ulsd);
  689. if (!d) {
  690. *decpt = 1;
  691. return nrv_alloc("0", rve, 1);
  692. }
  693. b = d2b(d, &be, &bbits);
  694. i = (int)(ulsd.hi >> Exp_shift1 & (Exp_mask >> Exp_shift1));
  695. ulsd2 = ulsd;
  696. ulsd2.hi &= Frac_mask1;
  697. ulsd2.hi |= Exp_11;
  698. d2 = ulongs2double(ulsd2);
  699. /* log(x) ~=~ log(1.5) + (x-1.5)/1.5
  700. * log10(x) = log(x) / log(10)
  701. * ~=~ log(1.5)/log(10) + (x-1.5)/(1.5*log(10))
  702. * log10(d) = (i-Bias)*log(2)/log(10) + log10(d2)
  703. *
  704. * This suggests computing an approximation k to log10(d) by
  705. *
  706. * k = (i - Bias)*0.301029995663981
  707. * + ( (d2-1.5)*0.289529654602168 + 0.176091259055681 );
  708. *
  709. * We want k to be too large rather than too small.
  710. * The error in the first-order Taylor series approximation
  711. * is in our favor, so we just round up the constant enough
  712. * to compensate for any error in the multiplication of
  713. * (i - Bias) by 0.301029995663981; since |i - Bias| <= 1077,
  714. * and 1077 * 0.30103 * 2^-52 ~=~ 7.2e-14,
  715. * adding 1e-13 to the constant term more than suffices.
  716. * Hence we adjust the constant term to 0.1760912590558.
  717. * (We could get a more accurate k by invoking log10,
  718. * but this is probably not worthwhile.)
  719. */
  720. i -= Bias;
  721. ds = (d2 - 1.5) * 0.289529654602168 + 0.1760912590558 + i * 0.301029995663981;
  722. k = (int)ds;
  723. if (ds < 0. && ds != k)
  724. k--; /* want k = floor(ds) */
  725. k_check = 1;
  726. if (k >= 0 && k <= Ten_pmax) {
  727. if (d < tens[k])
  728. k--;
  729. k_check = 0;
  730. }
  731. j = bbits - i - 1;
  732. if (j >= 0) {
  733. b2 = 0;
  734. s2 = j;
  735. } else {
  736. b2 = -j;
  737. s2 = 0;
  738. }
  739. if (k >= 0) {
  740. b5 = 0;
  741. s5 = k;
  742. s2 += k;
  743. } else {
  744. b2 -= k;
  745. b5 = -k;
  746. s5 = 0;
  747. }
  748. if (mode < 0 || mode > 9)
  749. mode = 0;
  750. try_quick = 1;
  751. if (mode > 5) {
  752. mode -= 4;
  753. try_quick = 0;
  754. }
  755. leftright = 1;
  756. switch (mode) {
  757. case 0:
  758. case 1:
  759. default:
  760. ilim = ilim1 = -1;
  761. i = 18;
  762. ndigits = 0;
  763. break;
  764. case 2:
  765. leftright = 0;
  766. /* no break */
  767. case 4:
  768. if (ndigits <= 0)
  769. ndigits = 1;
  770. ilim = ilim1 = i = ndigits;
  771. break;
  772. case 3:
  773. leftright = 0;
  774. /* no break */
  775. case 5:
  776. i = ndigits + k + 1;
  777. ilim = i;
  778. ilim1 = i - 1;
  779. if (i <= 0)
  780. i = 1;
  781. }
  782. s = s0 = rv_alloc(i);
  783. if (ilim >= 0 && ilim <= Quick_max && try_quick) {
  784. /* Try to get by with floating-point arithmetic. */
  785. i = 0;
  786. d2 = d;
  787. k0 = k;
  788. ilim0 = ilim;
  789. ieps = 2; /* conservative */
  790. if (k > 0) {
  791. ds = tens[k&0xf];
  792. j = k >> 4;
  793. if (j & Bletch) {
  794. /* prevent overflows */
  795. j &= Bletch - 1;
  796. d /= bigtens[n_bigtens-1];
  797. ieps++;
  798. }
  799. for (; j; j >>= 1, i++)
  800. if (j & 1) {
  801. ieps++;
  802. ds *= bigtens[i];
  803. }
  804. d /= ds;
  805. } else if (j1 = -k) {
  806. d *= tens[j1 & 0xf];
  807. for (j = j1 >> 4; j; j >>= 1, i++)
  808. if (j & 1) {
  809. ieps++;
  810. d *= bigtens[i];
  811. }
  812. }
  813. if (k_check && d < 1. && ilim > 0) {
  814. if (ilim1 <= 0)
  815. goto fast_failed;
  816. ilim = ilim1;
  817. k--;
  818. d *= 10.;
  819. ieps++;
  820. }
  821. eps = ieps * d + 7.;
  822. ulsd = double2ulongs(eps);
  823. ulsd.hi -= (P - 1) * Exp_msk1;
  824. eps = ulongs2double(ulsd);
  825. if (ilim == 0) {
  826. S = mhi = 0;
  827. d -= 5.;
  828. if (d > eps)
  829. goto one_digit;
  830. if (d < -eps)
  831. goto no_digits;
  832. goto fast_failed;
  833. }
  834. /* Generate ilim digits, then fix them up. */
  835. eps *= tens[ilim-1];
  836. for (i = 1; ; i++, d *= 10.) {
  837. L = d;
  838. // assert(L < 10);
  839. d -= L;
  840. *s++ = '0' + (int)L;
  841. if (i == ilim) {
  842. if (d > 0.5 + eps)
  843. goto bump_up;
  844. else if (d < 0.5 - eps) {
  845. while (*--s == '0')
  846. ;
  847. s++;
  848. goto ret1;
  849. }
  850. break;
  851. }
  852. }
  853. fast_failed:
  854. s = s0;
  855. d = d2;
  856. k = k0;
  857. ilim = ilim0;
  858. }
  859. /* Do we have a "small" integer? */
  860. if (be >= 0 && k <= Int_max) {
  861. /* Yes. */
  862. ds = tens[k];
  863. if (ndigits < 0 && ilim <= 0) {
  864. S = mhi = 0;
  865. if (ilim < 0 || d <= 5 * ds)
  866. goto no_digits;
  867. goto one_digit;
  868. }
  869. for (i = 1; ; i++) {
  870. L = d / ds;
  871. d -= L * ds;
  872. *s++ = '0' + (int)L;
  873. if (i == ilim) {
  874. d += d;
  875. if (d > ds || d == ds && L & 1) {
  876. bump_up:
  877. while (*--s == '9')
  878. if (s == s0) {
  879. k++;
  880. *s = '0';
  881. break;
  882. }
  883. ++ * s++;
  884. }
  885. break;
  886. }
  887. if (!(d *= 10.))
  888. break;
  889. }
  890. goto ret1;
  891. }
  892. m2 = b2;
  893. m5 = b5;
  894. mhi = mlo = 0;
  895. if (leftright) {
  896. if (mode < 2) {
  897. i =
  898. 1 + P - bbits;
  899. } else {
  900. j = ilim - 1;
  901. if (m5 >= j)
  902. m5 -= j;
  903. else {
  904. s5 += j -= m5;
  905. b5 += j;
  906. m5 = 0;
  907. }
  908. if ((i = ilim) < 0) {
  909. m2 -= i;
  910. i = 0;
  911. }
  912. }
  913. b2 += i;
  914. s2 += i;
  915. mhi = i2b(1);
  916. }
  917. if (m2 > 0 && s2 > 0) {
  918. i = m2 < s2 ? m2 : s2;
  919. b2 -= i;
  920. m2 -= i;
  921. s2 -= i;
  922. }
  923. if (b5 > 0) {
  924. if (leftright) {
  925. if (m5 > 0) {
  926. mhi = pow5mult(mhi, m5);
  927. b1 = mult(mhi, b);
  928. Bfree(b);
  929. b = b1;
  930. }
  931. if (j = b5 - m5)
  932. b = pow5mult(b, j);
  933. } else
  934. b = pow5mult(b, b5);
  935. }
  936. S = i2b(1);
  937. if (s5 > 0)
  938. S = pow5mult(S, s5);
  939. /* Check for special case that d is a normalized power of 2. */
  940. spec_case = 0;
  941. if (mode < 2) {
  942. ulsd = double2ulongs(d);
  943. if (!ulsd.lo && !(ulsd.hi & Bndry_mask)) {
  944. /* The special case */
  945. b2 += Log2P;
  946. s2 += Log2P;
  947. spec_case = 1;
  948. }
  949. }
  950. /* Arrange for convenient computation of quotients:
  951. * shift left if necessary so divisor has 4 leading 0 bits.
  952. *
  953. * Perhaps we should just compute leading 28 bits of S once
  954. * and for all and pass them and a shift to quorem, so it
  955. * can do shifts and ors to compute the numerator for q.
  956. */
  957. if (i = ((s5 ? 32 - hi0bits(S->x[S->wds-1]) : 1) + s2) & 0x1f)
  958. i = 32 - i;
  959. if (i > 4) {
  960. i -= 4;
  961. b2 += i;
  962. m2 += i;
  963. s2 += i;
  964. } else if (i < 4) {
  965. i += 28;
  966. b2 += i;
  967. m2 += i;
  968. s2 += i;
  969. }
  970. if (b2 > 0)
  971. b = lshift(b, b2);
  972. if (s2 > 0)
  973. S = lshift(S, s2);
  974. if (k_check) {
  975. if (cmp(b, S) < 0) {
  976. k--;
  977. b = multadd(b, 10, 0); /* we botched the k estimate */
  978. if (leftright)
  979. mhi = multadd(mhi, 10, 0);
  980. ilim = ilim1;
  981. }
  982. }
  983. if (ilim <= 0 && mode > 2) {
  984. if (ilim < 0 || cmp(b, S = multadd(S, 5, 0)) <= 0) {
  985. /* no digits, fcvt style */
  986. no_digits:
  987. k = -1 - ndigits;
  988. goto ret;
  989. }
  990. one_digit:
  991. *s++ = '1';
  992. k++;
  993. goto ret;
  994. }
  995. if (leftright) {
  996. if (m2 > 0)
  997. mhi = lshift(mhi, m2);
  998. /* Compute mlo -- check for special case
  999. * that d is a normalized power of 2.
  1000. */
  1001. mlo = mhi;
  1002. if (spec_case) {
  1003. mhi = Balloc(mhi->k);
  1004. Bcopy(mhi, mlo);
  1005. mhi = lshift(mhi, Log2P);
  1006. }
  1007. for (i = 1; ; i++) {
  1008. dig = quorem(b, S) + '0';
  1009. /* Do we yet have the shortest decimal string
  1010. * that will round to d?
  1011. */
  1012. j = cmp(b, mlo);
  1013. delta = diff(S, mhi);
  1014. j1 = delta->sign ? 1 : cmp(b, delta);
  1015. Bfree(delta);
  1016. ulsd = double2ulongs(d);
  1017. if (j1 == 0 && !mode && !(ulsd.lo & 1)) {
  1018. if (dig == '9')
  1019. goto round_9_up;
  1020. if (j > 0)
  1021. dig++;
  1022. *s++ = dig;
  1023. goto ret;
  1024. }
  1025. if (j < 0 || j == 0 && !mode && !(ulsd.lo & 1)) {
  1026. if (j1 > 0) {
  1027. b = lshift(b, 1);
  1028. j1 = cmp(b, S);
  1029. if ((j1 > 0 || j1 == 0 && dig & 1)
  1030. && dig++ == '9')
  1031. goto round_9_up;
  1032. }
  1033. *s++ = dig;
  1034. goto ret;
  1035. }
  1036. if (j1 > 0) {
  1037. if (dig == '9') { /* possible if i == 1 */
  1038. round_9_up:
  1039. *s++ = '9';
  1040. goto roundoff;
  1041. }
  1042. *s++ = dig + 1;
  1043. goto ret;
  1044. }
  1045. *s++ = dig;
  1046. if (i == ilim)
  1047. break;
  1048. b = multadd(b, 10, 0);
  1049. if (mlo == mhi)
  1050. mlo = mhi = multadd(mhi, 10, 0);
  1051. else {
  1052. mlo = multadd(mlo, 10, 0);
  1053. mhi = multadd(mhi, 10, 0);
  1054. }
  1055. }
  1056. } else
  1057. for (i = 1; ; i++) {
  1058. *s++ = dig = quorem(b, S) + '0';
  1059. if (i >= ilim)
  1060. break;
  1061. b = multadd(b, 10, 0);
  1062. }
  1063. /* Round off last digit */
  1064. b = lshift(b, 1);
  1065. j = cmp(b, S);
  1066. if (j > 0 || j == 0 && dig & 1) {
  1067. roundoff:
  1068. while (*--s == '9')
  1069. if (s == s0) {
  1070. k++;
  1071. *s++ = '1';
  1072. goto ret;
  1073. }
  1074. ++ * s++;
  1075. } else {
  1076. while (*--s == '0')
  1077. ;
  1078. s++;
  1079. }
  1080. ret:
  1081. Bfree(S);
  1082. if (mhi) {
  1083. if (mlo && mlo != mhi)
  1084. Bfree(mlo);
  1085. Bfree(mhi);
  1086. }
  1087. ret1:
  1088. Bfree(b);
  1089. *s = 0;
  1090. *decpt = k + 1;
  1091. if (rve)
  1092. *rve = s;
  1093. return s0;
  1094. }