dtoa.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324
  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]) {
  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 Bigint *
  163. s2b(const char *s, int nd0, int nd, unsigned int y9)
  164. {
  165. Bigint * b;
  166. int i, k;
  167. int x, y;
  168. x = (nd + 8) / 9;
  169. for (k = 0, y = 1; x > y; y <<= 1, k++)
  170. ;
  171. b = Balloc(k);
  172. b->x[0] = y9;
  173. b->wds = 1;
  174. i = 9;
  175. if (9 < nd0) {
  176. s += 9;
  177. do
  178. b = multadd(b, 10, *s++ - '0');
  179. while (++i < nd0);
  180. s++;
  181. } else
  182. s += 10;
  183. for (; i < nd; i++)
  184. b = multadd(b, 10, *s++ - '0');
  185. return b;
  186. }
  187. static int
  188. hi0bits(register unsigned int x)
  189. {
  190. register int k = 0;
  191. if (!(x & 0xffff0000)) {
  192. k = 16;
  193. x <<= 16;
  194. }
  195. if (!(x & 0xff000000)) {
  196. k += 8;
  197. x <<= 8;
  198. }
  199. if (!(x & 0xf0000000)) {
  200. k += 4;
  201. x <<= 4;
  202. }
  203. if (!(x & 0xc0000000)) {
  204. k += 2;
  205. x <<= 2;
  206. }
  207. if (!(x & 0x80000000)) {
  208. k++;
  209. if (!(x & 0x40000000))
  210. return 32;
  211. }
  212. return k;
  213. }
  214. static int
  215. lo0bits(unsigned int *y)
  216. {
  217. register int k;
  218. register unsigned int x = *y;
  219. if (x & 7) {
  220. if (x & 1)
  221. return 0;
  222. if (x & 2) {
  223. *y = x >> 1;
  224. return 1;
  225. }
  226. *y = x >> 2;
  227. return 2;
  228. }
  229. k = 0;
  230. if (!(x & 0xffff)) {
  231. k = 16;
  232. x >>= 16;
  233. }
  234. if (!(x & 0xff)) {
  235. k += 8;
  236. x >>= 8;
  237. }
  238. if (!(x & 0xf)) {
  239. k += 4;
  240. x >>= 4;
  241. }
  242. if (!(x & 0x3)) {
  243. k += 2;
  244. x >>= 2;
  245. }
  246. if (!(x & 1)) {
  247. k++;
  248. x >>= 1;
  249. if (!x & 1)
  250. return 32;
  251. }
  252. *y = x;
  253. return k;
  254. }
  255. static Bigint *
  256. i2b(int i)
  257. {
  258. Bigint * b;
  259. b = Balloc(1);
  260. b->x[0] = i;
  261. b->wds = 1;
  262. return b;
  263. }
  264. static Bigint *
  265. mult(Bigint *a, Bigint *b)
  266. {
  267. Bigint * c;
  268. int k, wa, wb, wc;
  269. unsigned int * x, *xa, *xae, *xb, *xbe, *xc, *xc0;
  270. unsigned int y;
  271. unsigned int carry, z;
  272. unsigned int z2;
  273. if (a->wds < b->wds) {
  274. c = a;
  275. a = b;
  276. b = c;
  277. }
  278. k = a->k;
  279. wa = a->wds;
  280. wb = b->wds;
  281. wc = wa + wb;
  282. if (wc > a->maxwds)
  283. k++;
  284. c = Balloc(k);
  285. for (x = c->x, xa = x + wc; x < xa; x++)
  286. *x = 0;
  287. xa = a->x;
  288. xae = xa + wa;
  289. xb = b->x;
  290. xbe = xb + wb;
  291. xc0 = c->x;
  292. for (; xb < xbe; xb++, xc0++) {
  293. if (y = *xb & 0xffff) {
  294. x = xa;
  295. xc = xc0;
  296. carry = 0;
  297. do {
  298. z = (*x & 0xffff) * y + (*xc & 0xffff) + carry;
  299. carry = z >> 16;
  300. z2 = (*x++ >> 16) * y + (*xc >> 16) + carry;
  301. carry = z2 >> 16;
  302. Storeinc(xc, z2, z);
  303. } while (x < xae);
  304. *xc = carry;
  305. }
  306. if (y = *xb >> 16) {
  307. x = xa;
  308. xc = xc0;
  309. carry = 0;
  310. z2 = *xc;
  311. do {
  312. z = (*x & 0xffff) * y + (*xc >> 16) + carry;
  313. carry = z >> 16;
  314. Storeinc(xc, z, z2);
  315. z2 = (*x++ >> 16) * y + (*xc & 0xffff) + carry;
  316. carry = z2 >> 16;
  317. } while (x < xae);
  318. *xc = z2;
  319. }
  320. }
  321. for (xc0 = c->x, xc = xc0 + wc; wc > 0 && !*--xc; --wc)
  322. ;
  323. c->wds = wc;
  324. return c;
  325. }
  326. static Bigint *p5s;
  327. static Bigint *
  328. pow5mult(Bigint *b, int k)
  329. {
  330. Bigint * b1, *p5, *p51;
  331. int i;
  332. static int p05[3] = {
  333. 5, 25, 125 };
  334. if (i = k & 3)
  335. b = multadd(b, p05[i-1], 0);
  336. if (!(k >>= 2))
  337. return b;
  338. if (!(p5 = p5s)) {
  339. /* first time */
  340. ACQUIRE_DTOA_LOCK(1);
  341. if (!(p5 = p5s)) {
  342. p5 = p5s = i2b(625);
  343. p5->next = 0;
  344. }
  345. FREE_DTOA_LOCK(1);
  346. }
  347. for (; ; ) {
  348. if (k & 1) {
  349. b1 = mult(b, p5);
  350. Bfree(b);
  351. b = b1;
  352. }
  353. if (!(k >>= 1))
  354. break;
  355. if (!(p51 = p5->next)) {
  356. ACQUIRE_DTOA_LOCK(1);
  357. if (!(p51 = p5->next)) {
  358. p51 = p5->next = mult(p5, p5);
  359. p51->next = 0;
  360. }
  361. FREE_DTOA_LOCK(1);
  362. }
  363. p5 = p51;
  364. }
  365. return b;
  366. }
  367. static Bigint *
  368. lshift(Bigint *b, int k)
  369. {
  370. int i, k1, n, n1;
  371. Bigint * b1;
  372. unsigned int * x, *x1, *xe, z;
  373. n = k >> 5;
  374. k1 = b->k;
  375. n1 = n + b->wds + 1;
  376. for (i = b->maxwds; n1 > i; i <<= 1)
  377. k1++;
  378. b1 = Balloc(k1);
  379. x1 = b1->x;
  380. for (i = 0; i < n; i++)
  381. *x1++ = 0;
  382. x = b->x;
  383. xe = x + b->wds;
  384. if (k &= 0x1f) {
  385. k1 = 32 - k;
  386. z = 0;
  387. do {
  388. *x1++ = *x << k | z;
  389. z = *x++ >> k1;
  390. } while (x < xe);
  391. if (*x1 = z)
  392. ++n1;
  393. } else
  394. do
  395. *x1++ = *x++;
  396. while (x < xe);
  397. b1->wds = n1 - 1;
  398. Bfree(b);
  399. return b1;
  400. }
  401. static int
  402. cmp(Bigint *a, Bigint *b)
  403. {
  404. unsigned int * xa, *xa0, *xb, *xb0;
  405. int i, j;
  406. i = a->wds;
  407. j = b->wds;
  408. if (i -= j)
  409. return i;
  410. xa0 = a->x;
  411. xa = xa0 + j;
  412. xb0 = b->x;
  413. xb = xb0 + j;
  414. for (; ; ) {
  415. if (*--xa != *--xb)
  416. return * xa < *xb ? -1 : 1;
  417. if (xa <= xa0)
  418. break;
  419. }
  420. return 0;
  421. }
  422. static Bigint *
  423. diff(Bigint *a, Bigint *b)
  424. {
  425. Bigint * c;
  426. int i, wa, wb;
  427. unsigned int * xa, *xae, *xb, *xbe, *xc;
  428. unsigned int borrow, y;
  429. unsigned int z;
  430. i = cmp(a, b);
  431. if (!i) {
  432. c = Balloc(0);
  433. c->wds = 1;
  434. c->x[0] = 0;
  435. return c;
  436. }
  437. if (i < 0) {
  438. c = a;
  439. a = b;
  440. b = c;
  441. i = 1;
  442. } else
  443. i = 0;
  444. c = Balloc(a->k);
  445. c->sign = i;
  446. wa = a->wds;
  447. xa = a->x;
  448. xae = xa + wa;
  449. wb = b->wds;
  450. xb = b->x;
  451. xbe = xb + wb;
  452. xc = c->x;
  453. borrow = 0;
  454. do {
  455. y = (*xa & 0xffff) - (*xb & 0xffff) - borrow;
  456. borrow = (y & 0x10000) >> 16;
  457. z = (*xa++ >> 16) - (*xb++ >> 16) - borrow;
  458. borrow = (z & 0x10000) >> 16;
  459. Storeinc(xc, z, y);
  460. } while (xb < xbe);
  461. while (xa < xae) {
  462. y = (*xa & 0xffff) - borrow;
  463. borrow = (y & 0x10000) >> 16;
  464. z = (*xa++ >> 16) - borrow;
  465. borrow = (z & 0x10000) >> 16;
  466. Storeinc(xc, z, y);
  467. }
  468. while (!*--xc)
  469. wa--;
  470. c->wds = wa;
  471. return c;
  472. }
  473. static double
  474. ulp(double x)
  475. {
  476. uint32_t L;
  477. Ulongs uls;
  478. uls = double2ulongs(x);
  479. L = (uls.hi & Exp_mask) - (P - 1) * Exp_msk1;
  480. return ulongs2double((Ulongs){L, 0});
  481. }
  482. static double
  483. b2d(Bigint *a, int *e)
  484. {
  485. unsigned *xa, *xa0, w, y, z;
  486. int k;
  487. uint32_t d0, d1;
  488. xa0 = a->x;
  489. xa = xa0 + a->wds;
  490. y = *--xa;
  491. k = hi0bits(y);
  492. *e = 32 - k;
  493. if (k < Ebits) {
  494. w = xa > xa0 ? *--xa : 0;
  495. d1 = y << (32 - Ebits) + k | w >> Ebits - k;
  496. return ulongs2double((Ulongs){Exp_1 | y >> Ebits - k, d1});
  497. }
  498. z = xa > xa0 ? *--xa : 0;
  499. if (k -= Ebits) {
  500. d0 = Exp_1 | y << k | z >> 32 - k;
  501. y = xa > xa0 ? *--xa : 0;
  502. d1 = z << k | y >> 32 - k;
  503. } else {
  504. d0 = Exp_1 | y;
  505. d1 = z;
  506. }
  507. return ulongs2double((Ulongs){d0, d1});
  508. }
  509. static Bigint *
  510. d2b(double d, int *e, int *bits)
  511. {
  512. Bigint * b;
  513. int de, i, k;
  514. unsigned *x, y, z;
  515. Ulongs uls;
  516. b = Balloc(1);
  517. x = b->x;
  518. uls = double2ulongs(d);
  519. z = uls.hi & Frac_mask;
  520. uls.hi &= 0x7fffffff; /* clear sign bit, which we ignore */
  521. de = (int)(uls.hi >> Exp_shift);
  522. z |= Exp_msk11;
  523. if (y = uls.lo) { /* assignment = */
  524. if (k = lo0bits(&y)) { /* assignment = */
  525. x[0] = y | z << 32 - k;
  526. z >>= k;
  527. } else
  528. x[0] = y;
  529. i = b->wds = (x[1] = z) ? 2 : 1;
  530. } else {
  531. k = lo0bits(&z);
  532. x[0] = z;
  533. i = b->wds = 1;
  534. k += 32;
  535. }
  536. USED(i);
  537. *e = de - Bias - (P - 1) + k;
  538. *bits = P - k;
  539. return b;
  540. }
  541. static double
  542. ratio(Bigint *a, Bigint *b)
  543. {
  544. double da, db;
  545. int k, ka, kb;
  546. Ulongs uls;
  547. da = b2d(a, &ka);
  548. db = b2d(b, &kb);
  549. k = ka - kb + 32 * (a->wds - b->wds);
  550. if (k > 0) {
  551. uls = double2ulongs(da);
  552. uls.hi += k * Exp_msk1;
  553. da = ulongs2double(uls);
  554. } else {
  555. k = -k;
  556. uls = double2ulongs(db);
  557. uls.hi += k * Exp_msk1;
  558. db = ulongs2double(uls);
  559. }
  560. return da / db;
  561. }
  562. static const double
  563. tens[] = {
  564. 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9,
  565. 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19,
  566. 1e20, 1e21, 1e22
  567. };
  568. static const double
  569. bigtens[] = {
  570. 1e16, 1e32, 1e64, 1e128, 1e256 };
  571. #define Scale_Bit 0x10
  572. #define n_bigtens 5
  573. #define NAN_WORD0 0x7ff80000
  574. #define NAN_WORD1 0
  575. static int
  576. match(const char **sp, char *t)
  577. {
  578. int c, d;
  579. const char * s = *sp;
  580. while (d = *t++) {
  581. if ((c = *++s) >= 'A' && c <= 'Z')
  582. c += 'a' - 'A';
  583. if (c != d)
  584. return 0;
  585. }
  586. *sp = s + 1;
  587. return 1;
  588. }
  589. static void
  590. gethex(double *rvp, const char **sp)
  591. {
  592. unsigned int c, x[2];
  593. const char * s;
  594. int havedig, udx0, xshift;
  595. x[0] = x[1] = 0;
  596. havedig = xshift = 0;
  597. udx0 = 1;
  598. s = *sp;
  599. while (c = *(const unsigned char * )++s) {
  600. if (c >= '0' && c <= '9')
  601. c -= '0';
  602. else if (c >= 'a' && c <= 'f')
  603. c += 10 - 'a';
  604. else if (c >= 'A' && c <= 'F')
  605. c += 10 - 'A';
  606. else if (c <= ' ') {
  607. if (udx0 && havedig) {
  608. udx0 = 0;
  609. xshift = 1;
  610. }
  611. continue;
  612. } else if (/*(*/ c == ')') {
  613. *sp = s + 1;
  614. break;
  615. } else
  616. return; /* invalid form: don't change *sp */
  617. havedig = 1;
  618. if (xshift) {
  619. xshift = 0;
  620. x[0] = x[1];
  621. x[1] = 0;
  622. }
  623. if (udx0)
  624. x[0] = (x[0] << 4) | (x[1] >> 28);
  625. x[1] = (x[1] << 4) | c;
  626. }
  627. if ((x[0] &= 0xfffff) || x[1])
  628. *rvp = ulongs2double((Ulongs){Exp_mask | x[0], x[1]});
  629. }
  630. static int
  631. quorem(Bigint *b, Bigint *S)
  632. {
  633. int n;
  634. unsigned int * bx, *bxe, q, *sx, *sxe;
  635. unsigned int borrow, carry, y, ys;
  636. unsigned int si, z, zs;
  637. n = S->wds;
  638. if (b->wds < n)
  639. return 0;
  640. sx = S->x;
  641. sxe = sx + --n;
  642. bx = b->x;
  643. bxe = bx + n;
  644. q = *bxe / (*sxe + 1); /* ensure q <= true quotient */
  645. if (q) {
  646. borrow = 0;
  647. carry = 0;
  648. do {
  649. si = *sx++;
  650. ys = (si & 0xffff) * q + carry;
  651. zs = (si >> 16) * q + (ys >> 16);
  652. carry = zs >> 16;
  653. y = (*bx & 0xffff) - (ys & 0xffff) - borrow;
  654. borrow = (y & 0x10000) >> 16;
  655. z = (*bx >> 16) - (zs & 0xffff) - borrow;
  656. borrow = (z & 0x10000) >> 16;
  657. Storeinc(bx, z, y);
  658. } while (sx <= sxe);
  659. if (!*bxe) {
  660. bx = b->x;
  661. while (--bxe > bx && !*bxe)
  662. --n;
  663. b->wds = n;
  664. }
  665. }
  666. if (cmp(b, S) >= 0) {
  667. q++;
  668. borrow = 0;
  669. carry = 0;
  670. bx = b->x;
  671. sx = S->x;
  672. do {
  673. si = *sx++;
  674. ys = (si & 0xffff) + carry;
  675. zs = (si >> 16) + (ys >> 16);
  676. carry = zs >> 16;
  677. y = (*bx & 0xffff) - (ys & 0xffff) - borrow;
  678. borrow = (y & 0x10000) >> 16;
  679. z = (*bx >> 16) - (zs & 0xffff) - borrow;
  680. borrow = (z & 0x10000) >> 16;
  681. Storeinc(bx, z, y);
  682. } while (sx <= sxe);
  683. bx = b->x;
  684. bxe = bx + n;
  685. if (!*bxe) {
  686. while (--bxe > bx && !*bxe)
  687. --n;
  688. b->wds = n;
  689. }
  690. }
  691. return q;
  692. }
  693. static char *
  694. rv_alloc(int i)
  695. {
  696. int j, k, *r;
  697. j = sizeof(unsigned int);
  698. for (k = 0;
  699. sizeof(Bigint) - sizeof(unsigned int) - sizeof(int) + j <= i;
  700. j <<= 1)
  701. k++;
  702. r = (int * )Balloc(k);
  703. *r = k;
  704. return
  705. (char *)(r + 1);
  706. }
  707. static char *
  708. nrv_alloc(char *s, char **rve, int n)
  709. {
  710. char *rv, *t;
  711. t = rv = rv_alloc(n);
  712. while (*t = *s++)
  713. t++;
  714. if (rve)
  715. *rve = t;
  716. return rv;
  717. }
  718. /* freedtoa(s) must be used to free values s returned by dtoa
  719. * when MULTIPLE_THREADS is #defined. It should be used in all cases,
  720. * but for consistency with earlier versions of dtoa, it is optional
  721. * when MULTIPLE_THREADS is not defined.
  722. */
  723. void
  724. freedtoa(char *s)
  725. {
  726. Bigint * b = (Bigint * )((int *)s - 1);
  727. b->maxwds = 1 << (b->k = *(int * )b);
  728. Bfree(b);
  729. }
  730. /* dtoa for IEEE arithmetic (dmg): convert double to ASCII string.
  731. *
  732. * Inspired by "How to Print Floating-Point Numbers Accurately" by
  733. * Guy L. Steele, Jr. and Jon L. White [Proc. ACM SIGPLAN '90, pp. 92-101].
  734. *
  735. * Modifications:
  736. * 1. Rather than iterating, we use a simple numeric overestimate
  737. * to determine k = floor(log10(d)). We scale relevant
  738. * quantities using O(log2(k)) rather than O(k) multiplications.
  739. * 2. For some modes > 2 (corresponding to ecvt and fcvt), we don't
  740. * try to generate digits strictly left to right. Instead, we
  741. * compute with fewer bits and propagate the carry if necessary
  742. * when rounding the final digit up. This is often faster.
  743. * 3. Under the assumption that input will be rounded nearest,
  744. * mode 0 renders 1e23 as 1e23 rather than 9.999999999999999e22.
  745. * That is, we allow equality in stopping tests when the
  746. * round-nearest rule will give the same floating-point value
  747. * as would satisfaction of the stopping test with strict
  748. * inequality.
  749. * 4. We remove common factors of powers of 2 from relevant
  750. * quantities.
  751. * 5. When converting floating-point integers less than 1e16,
  752. * we use floating-point arithmetic rather than resorting
  753. * to multiple-precision integers.
  754. * 6. When asked to produce fewer than 15 digits, we first try
  755. * to get by with floating-point arithmetic; we resort to
  756. * multiple-precision integer arithmetic only if we cannot
  757. * guarantee that the floating-point calculation has given
  758. * the correctly rounded result. For k requested digits and
  759. * "uniformly" distributed input, the probability is
  760. * something like 10^(k-15) that we must resort to the int
  761. * calculation.
  762. */
  763. char *
  764. dtoa(double d, int mode, int ndigits, int *decpt, int *sign, char **rve)
  765. {
  766. /* Arguments ndigits, decpt, sign are similar to those
  767. of ecvt and fcvt; trailing zeros are suppressed from
  768. the returned string. If not null, *rve is set to point
  769. to the end of the return value. If d is +-Infinity or NaN,
  770. then *decpt is set to 9999.
  771. mode:
  772. 0 ==> shortest string that yields d when read in
  773. and rounded to nearest.
  774. 1 ==> like 0, but with Steele & White stopping rule;
  775. e.g. with IEEE P754 arithmetic , mode 0 gives
  776. 1e23 whereas mode 1 gives 9.999999999999999e22.
  777. 2 ==> max(1,ndigits) significant digits. This gives a
  778. return value similar to that of ecvt, except
  779. that trailing zeros are suppressed.
  780. 3 ==> through ndigits past the decimal point. This
  781. gives a return value similar to that from fcvt,
  782. except that trailing zeros are suppressed, and
  783. ndigits can be negative.
  784. 4-9 should give the same return values as 2-3, i.e.,
  785. 4 <= mode <= 9 ==> same return as mode
  786. 2 + (mode & 1). These modes are mainly for
  787. debugging; often they run slower but sometimes
  788. faster than modes 2-3.
  789. 4,5,8,9 ==> left-to-right digit generation.
  790. 6-9 ==> don't try fast floating-point estimate
  791. (if applicable).
  792. Values of mode other than 0-9 are treated as mode 0.
  793. Sufficient space is allocated to the return value
  794. to hold the suppressed trailing zeros.
  795. */
  796. int bbits, b2, b5, be, dig, i, ieps, ilim, ilim0, ilim1,
  797. j, j1, k, k0, k_check, L, leftright, m2, m5, s2, s5,
  798. spec_case, try_quick;
  799. Bigint * b, *b1, *delta, *mlo=nil, *mhi, *S;
  800. double d2, ds, eps;
  801. char *s, *s0;
  802. Ulongs ulsd, ulsd2;
  803. ulsd = double2ulongs(d);
  804. if (ulsd.hi & Sign_bit) {
  805. /* set sign for everything, including 0's and NaNs */
  806. *sign = 1;
  807. ulsd.hi &= ~Sign_bit; /* clear sign bit */
  808. } else
  809. *sign = 0;
  810. if ((ulsd.hi & Exp_mask) == Exp_mask) {
  811. /* Infinity or NaN */
  812. *decpt = 9999;
  813. if (!ulsd.lo && !(ulsd.hi & 0xfffff))
  814. return nrv_alloc("Infinity", rve, 8);
  815. return nrv_alloc("NaN", rve, 3);
  816. }
  817. d = ulongs2double(ulsd);
  818. if (!d) {
  819. *decpt = 1;
  820. return nrv_alloc("0", rve, 1);
  821. }
  822. b = d2b(d, &be, &bbits);
  823. i = (int)(ulsd.hi >> Exp_shift1 & (Exp_mask >> Exp_shift1));
  824. ulsd2 = ulsd;
  825. ulsd2.hi &= Frac_mask1;
  826. ulsd2.hi |= Exp_11;
  827. d2 = ulongs2double(ulsd2);
  828. /* log(x) ~=~ log(1.5) + (x-1.5)/1.5
  829. * log10(x) = log(x) / log(10)
  830. * ~=~ log(1.5)/log(10) + (x-1.5)/(1.5*log(10))
  831. * log10(d) = (i-Bias)*log(2)/log(10) + log10(d2)
  832. *
  833. * This suggests computing an approximation k to log10(d) by
  834. *
  835. * k = (i - Bias)*0.301029995663981
  836. * + ( (d2-1.5)*0.289529654602168 + 0.176091259055681 );
  837. *
  838. * We want k to be too large rather than too small.
  839. * The error in the first-order Taylor series approximation
  840. * is in our favor, so we just round up the constant enough
  841. * to compensate for any error in the multiplication of
  842. * (i - Bias) by 0.301029995663981; since |i - Bias| <= 1077,
  843. * and 1077 * 0.30103 * 2^-52 ~=~ 7.2e-14,
  844. * adding 1e-13 to the constant term more than suffices.
  845. * Hence we adjust the constant term to 0.1760912590558.
  846. * (We could get a more accurate k by invoking log10,
  847. * but this is probably not worthwhile.)
  848. */
  849. i -= Bias;
  850. ds = (d2 - 1.5) * 0.289529654602168 + 0.1760912590558 + i * 0.301029995663981;
  851. k = (int)ds;
  852. if (ds < 0. && ds != k)
  853. k--; /* want k = floor(ds) */
  854. k_check = 1;
  855. if (k >= 0 && k <= Ten_pmax) {
  856. if (d < tens[k])
  857. k--;
  858. k_check = 0;
  859. }
  860. j = bbits - i - 1;
  861. if (j >= 0) {
  862. b2 = 0;
  863. s2 = j;
  864. } else {
  865. b2 = -j;
  866. s2 = 0;
  867. }
  868. if (k >= 0) {
  869. b5 = 0;
  870. s5 = k;
  871. s2 += k;
  872. } else {
  873. b2 -= k;
  874. b5 = -k;
  875. s5 = 0;
  876. }
  877. if (mode < 0 || mode > 9)
  878. mode = 0;
  879. try_quick = 1;
  880. if (mode > 5) {
  881. mode -= 4;
  882. try_quick = 0;
  883. }
  884. leftright = 1;
  885. switch (mode) {
  886. case 0:
  887. case 1:
  888. default:
  889. ilim = ilim1 = -1;
  890. i = 18;
  891. ndigits = 0;
  892. break;
  893. case 2:
  894. leftright = 0;
  895. /* no break */
  896. case 4:
  897. if (ndigits <= 0)
  898. ndigits = 1;
  899. ilim = ilim1 = i = ndigits;
  900. break;
  901. case 3:
  902. leftright = 0;
  903. /* no break */
  904. case 5:
  905. i = ndigits + k + 1;
  906. ilim = i;
  907. ilim1 = i - 1;
  908. if (i <= 0)
  909. i = 1;
  910. }
  911. s = s0 = rv_alloc(i);
  912. if (ilim >= 0 && ilim <= Quick_max && try_quick) {
  913. /* Try to get by with floating-point arithmetic. */
  914. i = 0;
  915. d2 = d;
  916. k0 = k;
  917. ilim0 = ilim;
  918. ieps = 2; /* conservative */
  919. if (k > 0) {
  920. ds = tens[k&0xf];
  921. j = k >> 4;
  922. if (j & Bletch) {
  923. /* prevent overflows */
  924. j &= Bletch - 1;
  925. d /= bigtens[n_bigtens-1];
  926. ieps++;
  927. }
  928. for (; j; j >>= 1, i++)
  929. if (j & 1) {
  930. ieps++;
  931. ds *= bigtens[i];
  932. }
  933. d /= ds;
  934. } else if (j1 = -k) {
  935. d *= tens[j1 & 0xf];
  936. for (j = j1 >> 4; j; j >>= 1, i++)
  937. if (j & 1) {
  938. ieps++;
  939. d *= bigtens[i];
  940. }
  941. }
  942. if (k_check && d < 1. && ilim > 0) {
  943. if (ilim1 <= 0)
  944. goto fast_failed;
  945. ilim = ilim1;
  946. k--;
  947. d *= 10.;
  948. ieps++;
  949. }
  950. eps = ieps * d + 7.;
  951. ulsd = double2ulongs(eps);
  952. ulsd.hi -= (P - 1) * Exp_msk1;
  953. eps = ulongs2double(ulsd);
  954. if (ilim == 0) {
  955. S = mhi = 0;
  956. d -= 5.;
  957. if (d > eps)
  958. goto one_digit;
  959. if (d < -eps)
  960. goto no_digits;
  961. goto fast_failed;
  962. }
  963. /* Generate ilim digits, then fix them up. */
  964. eps *= tens[ilim-1];
  965. for (i = 1; ; i++, d *= 10.) {
  966. L = d;
  967. // assert(L < 10);
  968. d -= L;
  969. *s++ = '0' + (int)L;
  970. if (i == ilim) {
  971. if (d > 0.5 + eps)
  972. goto bump_up;
  973. else if (d < 0.5 - eps) {
  974. while (*--s == '0')
  975. ;
  976. s++;
  977. goto ret1;
  978. }
  979. break;
  980. }
  981. }
  982. fast_failed:
  983. s = s0;
  984. d = d2;
  985. k = k0;
  986. ilim = ilim0;
  987. }
  988. /* Do we have a "small" integer? */
  989. if (be >= 0 && k <= Int_max) {
  990. /* Yes. */
  991. ds = tens[k];
  992. if (ndigits < 0 && ilim <= 0) {
  993. S = mhi = 0;
  994. if (ilim < 0 || d <= 5 * ds)
  995. goto no_digits;
  996. goto one_digit;
  997. }
  998. for (i = 1; ; i++) {
  999. L = d / ds;
  1000. d -= L * ds;
  1001. *s++ = '0' + (int)L;
  1002. if (i == ilim) {
  1003. d += d;
  1004. if (d > ds || d == ds && L & 1) {
  1005. bump_up:
  1006. while (*--s == '9')
  1007. if (s == s0) {
  1008. k++;
  1009. *s = '0';
  1010. break;
  1011. }
  1012. ++ * s++;
  1013. }
  1014. break;
  1015. }
  1016. if (!(d *= 10.))
  1017. break;
  1018. }
  1019. goto ret1;
  1020. }
  1021. m2 = b2;
  1022. m5 = b5;
  1023. mhi = mlo = 0;
  1024. if (leftright) {
  1025. if (mode < 2) {
  1026. i =
  1027. 1 + P - bbits;
  1028. } else {
  1029. j = ilim - 1;
  1030. if (m5 >= j)
  1031. m5 -= j;
  1032. else {
  1033. s5 += j -= m5;
  1034. b5 += j;
  1035. m5 = 0;
  1036. }
  1037. if ((i = ilim) < 0) {
  1038. m2 -= i;
  1039. i = 0;
  1040. }
  1041. }
  1042. b2 += i;
  1043. s2 += i;
  1044. mhi = i2b(1);
  1045. }
  1046. if (m2 > 0 && s2 > 0) {
  1047. i = m2 < s2 ? m2 : s2;
  1048. b2 -= i;
  1049. m2 -= i;
  1050. s2 -= i;
  1051. }
  1052. if (b5 > 0) {
  1053. if (leftright) {
  1054. if (m5 > 0) {
  1055. mhi = pow5mult(mhi, m5);
  1056. b1 = mult(mhi, b);
  1057. Bfree(b);
  1058. b = b1;
  1059. }
  1060. if (j = b5 - m5)
  1061. b = pow5mult(b, j);
  1062. } else
  1063. b = pow5mult(b, b5);
  1064. }
  1065. S = i2b(1);
  1066. if (s5 > 0)
  1067. S = pow5mult(S, s5);
  1068. /* Check for special case that d is a normalized power of 2. */
  1069. spec_case = 0;
  1070. if (mode < 2) {
  1071. ulsd = double2ulongs(d);
  1072. if (!ulsd.lo && !(ulsd.hi & Bndry_mask)) {
  1073. /* The special case */
  1074. b2 += Log2P;
  1075. s2 += Log2P;
  1076. spec_case = 1;
  1077. }
  1078. }
  1079. /* Arrange for convenient computation of quotients:
  1080. * shift left if necessary so divisor has 4 leading 0 bits.
  1081. *
  1082. * Perhaps we should just compute leading 28 bits of S once
  1083. * and for all and pass them and a shift to quorem, so it
  1084. * can do shifts and ors to compute the numerator for q.
  1085. */
  1086. if (i = ((s5 ? 32 - hi0bits(S->x[S->wds-1]) : 1) + s2) & 0x1f)
  1087. i = 32 - i;
  1088. if (i > 4) {
  1089. i -= 4;
  1090. b2 += i;
  1091. m2 += i;
  1092. s2 += i;
  1093. } else if (i < 4) {
  1094. i += 28;
  1095. b2 += i;
  1096. m2 += i;
  1097. s2 += i;
  1098. }
  1099. if (b2 > 0)
  1100. b = lshift(b, b2);
  1101. if (s2 > 0)
  1102. S = lshift(S, s2);
  1103. if (k_check) {
  1104. if (cmp(b, S) < 0) {
  1105. k--;
  1106. b = multadd(b, 10, 0); /* we botched the k estimate */
  1107. if (leftright)
  1108. mhi = multadd(mhi, 10, 0);
  1109. ilim = ilim1;
  1110. }
  1111. }
  1112. if (ilim <= 0 && mode > 2) {
  1113. if (ilim < 0 || cmp(b, S = multadd(S, 5, 0)) <= 0) {
  1114. /* no digits, fcvt style */
  1115. no_digits:
  1116. k = -1 - ndigits;
  1117. goto ret;
  1118. }
  1119. one_digit:
  1120. *s++ = '1';
  1121. k++;
  1122. goto ret;
  1123. }
  1124. if (leftright) {
  1125. if (m2 > 0)
  1126. mhi = lshift(mhi, m2);
  1127. /* Compute mlo -- check for special case
  1128. * that d is a normalized power of 2.
  1129. */
  1130. mlo = mhi;
  1131. if (spec_case) {
  1132. mhi = Balloc(mhi->k);
  1133. Bcopy(mhi, mlo);
  1134. mhi = lshift(mhi, Log2P);
  1135. }
  1136. for (i = 1; ; i++) {
  1137. dig = quorem(b, S) + '0';
  1138. /* Do we yet have the shortest decimal string
  1139. * that will round to d?
  1140. */
  1141. j = cmp(b, mlo);
  1142. delta = diff(S, mhi);
  1143. j1 = delta->sign ? 1 : cmp(b, delta);
  1144. Bfree(delta);
  1145. ulsd = double2ulongs(d);
  1146. if (j1 == 0 && !mode && !(ulsd.lo & 1)) {
  1147. if (dig == '9')
  1148. goto round_9_up;
  1149. if (j > 0)
  1150. dig++;
  1151. *s++ = dig;
  1152. goto ret;
  1153. }
  1154. if (j < 0 || j == 0 && !mode && !(ulsd.lo & 1)) {
  1155. if (j1 > 0) {
  1156. b = lshift(b, 1);
  1157. j1 = cmp(b, S);
  1158. if ((j1 > 0 || j1 == 0 && dig & 1)
  1159. && dig++ == '9')
  1160. goto round_9_up;
  1161. }
  1162. *s++ = dig;
  1163. goto ret;
  1164. }
  1165. if (j1 > 0) {
  1166. if (dig == '9') { /* possible if i == 1 */
  1167. round_9_up:
  1168. *s++ = '9';
  1169. goto roundoff;
  1170. }
  1171. *s++ = dig + 1;
  1172. goto ret;
  1173. }
  1174. *s++ = dig;
  1175. if (i == ilim)
  1176. break;
  1177. b = multadd(b, 10, 0);
  1178. if (mlo == mhi)
  1179. mlo = mhi = multadd(mhi, 10, 0);
  1180. else {
  1181. mlo = multadd(mlo, 10, 0);
  1182. mhi = multadd(mhi, 10, 0);
  1183. }
  1184. }
  1185. } else
  1186. for (i = 1; ; i++) {
  1187. *s++ = dig = quorem(b, S) + '0';
  1188. if (i >= ilim)
  1189. break;
  1190. b = multadd(b, 10, 0);
  1191. }
  1192. /* Round off last digit */
  1193. b = lshift(b, 1);
  1194. j = cmp(b, S);
  1195. if (j > 0 || j == 0 && dig & 1) {
  1196. roundoff:
  1197. while (*--s == '9')
  1198. if (s == s0) {
  1199. k++;
  1200. *s++ = '1';
  1201. goto ret;
  1202. }
  1203. ++ * s++;
  1204. } else {
  1205. while (*--s == '0')
  1206. ;
  1207. s++;
  1208. }
  1209. ret:
  1210. Bfree(S);
  1211. if (mhi) {
  1212. if (mlo && mlo != mhi)
  1213. Bfree(mlo);
  1214. Bfree(mhi);
  1215. }
  1216. ret1:
  1217. Bfree(b);
  1218. *s = 0;
  1219. *decpt = k + 1;
  1220. if (rve)
  1221. *rve = s;
  1222. return s0;
  1223. }