dtoa.c 25 KB

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