dtoa.c 24 KB

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