PageRenderTime 77ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/util.c

https://github.com/nazy/ruby
C | 4033 lines | 3289 code | 234 blank | 510 comment | 871 complexity | 726a532a856582a5caa8d63d711efa02 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, AGPL-3.0, 0BSD, Unlicense
  1. /**********************************************************************
  2. util.c -
  3. $Author$
  4. created at: Fri Mar 10 17:22:34 JST 1995
  5. Copyright (C) 1993-2008 Yukihiro Matsumoto
  6. **********************************************************************/
  7. #include "ruby/ruby.h"
  8. #include <ctype.h>
  9. #include <stdio.h>
  10. #include <errno.h>
  11. #include <math.h>
  12. #include <float.h>
  13. #ifdef _WIN32
  14. #include "missing/file.h"
  15. #endif
  16. #include "ruby/util.h"
  17. unsigned long
  18. ruby_scan_oct(const char *start, size_t len, size_t *retlen)
  19. {
  20. register const char *s = start;
  21. register unsigned long retval = 0;
  22. while (len-- && *s >= '0' && *s <= '7') {
  23. retval <<= 3;
  24. retval |= *s++ - '0';
  25. }
  26. *retlen = (int)(s - start); /* less than len */
  27. return retval;
  28. }
  29. unsigned long
  30. ruby_scan_hex(const char *start, size_t len, size_t *retlen)
  31. {
  32. static const char hexdigit[] = "0123456789abcdef0123456789ABCDEF";
  33. register const char *s = start;
  34. register unsigned long retval = 0;
  35. const char *tmp;
  36. while (len-- && *s && (tmp = strchr(hexdigit, *s))) {
  37. retval <<= 4;
  38. retval |= (tmp - hexdigit) & 15;
  39. s++;
  40. }
  41. *retlen = (int)(s - start); /* less than len */
  42. return retval;
  43. }
  44. static unsigned long
  45. scan_digits(const char *str, int base, size_t *retlen, int *overflow)
  46. {
  47. static signed char table[] = {
  48. /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
  49. /*0*/ -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  50. /*1*/ -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  51. /*2*/ -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  52. /*3*/ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,-1,-1,-1,-1,-1,-1,
  53. /*4*/ -1,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,
  54. /*5*/ 25,26,27,28,29,30,31,32,33,34,35,-1,-1,-1,-1,-1,
  55. /*6*/ -1,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,
  56. /*7*/ 25,26,27,28,29,30,31,32,33,34,35,-1,-1,-1,-1,-1,
  57. /*8*/ -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  58. /*9*/ -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  59. /*a*/ -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  60. /*b*/ -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  61. /*c*/ -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  62. /*d*/ -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  63. /*e*/ -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  64. /*f*/ -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
  65. };
  66. const char *start = str;
  67. unsigned long ret = 0, x;
  68. unsigned long mul_overflow = (~(unsigned long)0) / base;
  69. int c;
  70. *overflow = 0;
  71. while ((c = (unsigned char)*str++) != '\0') {
  72. int d = table[c];
  73. if (d == -1 || base <= d) {
  74. *retlen = (str-1) - start;
  75. return ret;
  76. }
  77. if (mul_overflow < ret)
  78. *overflow = 1;
  79. ret *= base;
  80. x = ret;
  81. ret += d;
  82. if (ret < x)
  83. *overflow = 1;
  84. }
  85. *retlen = (str-1) - start;
  86. return ret;
  87. }
  88. unsigned long
  89. ruby_strtoul(const char *str, char **endptr, int base)
  90. {
  91. int c, b, overflow;
  92. int sign = 0;
  93. size_t len;
  94. unsigned long ret;
  95. const char *subject_found = str;
  96. if (base == 1 || 36 < base) {
  97. errno = EINVAL;
  98. return 0;
  99. }
  100. while ((c = *str) && ISSPACE(c))
  101. str++;
  102. if (c == '+') {
  103. sign = 1;
  104. str++;
  105. }
  106. else if (c == '-') {
  107. sign = -1;
  108. str++;
  109. }
  110. if (str[0] == '0') {
  111. subject_found = str+1;
  112. if (base == 0 || base == 16) {
  113. if (str[1] == 'x' || str[1] == 'X') {
  114. b = 16;
  115. str += 2;
  116. }
  117. else {
  118. b = base == 0 ? 8 : 16;
  119. str++;
  120. }
  121. }
  122. else {
  123. b = base;
  124. str++;
  125. }
  126. }
  127. else {
  128. b = base == 0 ? 10 : base;
  129. }
  130. ret = scan_digits(str, b, &len, &overflow);
  131. if (0 < len)
  132. subject_found = str+len;
  133. if (endptr)
  134. *endptr = (char*)subject_found;
  135. if (overflow) {
  136. errno = ERANGE;
  137. return ULONG_MAX;
  138. }
  139. if (sign < 0) {
  140. ret = (unsigned long)(-(long)ret);
  141. return ret;
  142. }
  143. else {
  144. return ret;
  145. }
  146. }
  147. #include <sys/types.h>
  148. #include <sys/stat.h>
  149. #ifdef HAVE_UNISTD_H
  150. #include <unistd.h>
  151. #endif
  152. #if defined(HAVE_FCNTL_H)
  153. #include <fcntl.h>
  154. #endif
  155. #ifndef S_ISDIR
  156. # define S_ISDIR(m) ((m & S_IFMT) == S_IFDIR)
  157. #endif
  158. #if defined(__CYGWIN32__) || defined(_WIN32)
  159. /*
  160. * Copyright (c) 1993, Intergraph Corporation
  161. *
  162. * You may distribute under the terms of either the GNU General Public
  163. * License or the Artistic License, as specified in the perl README file.
  164. *
  165. * Various Unix compatibility functions and NT specific functions.
  166. *
  167. * Some of this code was derived from the MSDOS port(s) and the OS/2 port.
  168. *
  169. */
  170. /*
  171. * Suffix appending for in-place editing under MS-DOS and OS/2 (and now NT!).
  172. *
  173. * Here are the rules:
  174. *
  175. * Style 0: Append the suffix exactly as standard perl would do it.
  176. * If the filesystem groks it, use it. (HPFS will always
  177. * grok it. So will NTFS. FAT will rarely accept it.)
  178. *
  179. * Style 1: The suffix begins with a '.'. The extension is replaced.
  180. * If the name matches the original name, use the fallback method.
  181. *
  182. * Style 2: The suffix is a single character, not a '.'. Try to add the
  183. * suffix to the following places, using the first one that works.
  184. * [1] Append to extension.
  185. * [2] Append to filename,
  186. * [3] Replace end of extension,
  187. * [4] Replace end of filename.
  188. * If the name matches the original name, use the fallback method.
  189. *
  190. * Style 3: Any other case: Ignore the suffix completely and use the
  191. * fallback method.
  192. *
  193. * Fallback method: Change the extension to ".$$$". If that matches the
  194. * original name, then change the extension to ".~~~".
  195. *
  196. * If filename is more than 1000 characters long, we die a horrible
  197. * death. Sorry.
  198. *
  199. * The filename restriction is a cheat so that we can use buf[] to store
  200. * assorted temporary goo.
  201. *
  202. * Examples, assuming style 0 failed.
  203. *
  204. * suffix = ".bak" (style 1)
  205. * foo.bar => foo.bak
  206. * foo.bak => foo.$$$ (fallback)
  207. * makefile => makefile.bak
  208. * suffix = ".$$$" (style 1)
  209. * foo.$$$ => foo.~~~ (fallback)
  210. *
  211. * suffix = "~" (style 2)
  212. * foo.c => foo.c~
  213. * foo.c~ => foo.c~~
  214. * foo.c~~ => foo~.c~~
  215. * foo~.c~~ => foo~~.c~~
  216. * foo~~~~~.c~~ => foo~~~~~.$$$ (fallback)
  217. *
  218. * foo.pas => foo~.pas
  219. * makefile => makefile.~
  220. * longname.fil => longname.fi~
  221. * longname.fi~ => longnam~.fi~
  222. * longnam~.fi~ => longnam~.$$$
  223. *
  224. */
  225. static int valid_filename(const char *s);
  226. static const char suffix1[] = ".$$$";
  227. static const char suffix2[] = ".~~~";
  228. #define strEQ(s1,s2) (strcmp(s1,s2) == 0)
  229. extern const char *ruby_find_basename(const char *, long *, long *);
  230. extern const char *ruby_find_extname(const char *, long *);
  231. void
  232. ruby_add_suffix(VALUE str, const char *suffix)
  233. {
  234. long baselen;
  235. long extlen = strlen(suffix);
  236. long slen;
  237. char buf[1024];
  238. const char *name;
  239. const char *ext;
  240. long len;
  241. name = StringValueCStr(str);
  242. slen = strlen(name);
  243. if (slen > (long)(sizeof(buf) - 1))
  244. rb_fatal("Cannot do inplace edit on long filename (%ld characters)",
  245. slen);
  246. /* Style 0 */
  247. rb_str_cat(str, suffix, extlen);
  248. if (valid_filename(RSTRING_PTR(str))) return;
  249. /* Fooey, style 0 failed. Fix str before continuing. */
  250. rb_str_resize(str, slen);
  251. name = StringValueCStr(str);
  252. ext = ruby_find_extname(name, &len);
  253. if (*suffix == '.') { /* Style 1 */
  254. if (ext) {
  255. if (strEQ(ext, suffix)) {
  256. extlen = sizeof(suffix1) - 1; /* suffix2 must be same length */
  257. suffix = strEQ(suffix, suffix1) ? suffix2 : suffix1;
  258. }
  259. slen = ext - name;
  260. }
  261. rb_str_resize(str, slen);
  262. rb_str_cat(str, suffix, extlen);
  263. }
  264. else {
  265. char *p = buf, *q;
  266. strncpy(buf, name, slen);
  267. if (ext)
  268. p += (ext - name);
  269. else
  270. p += slen;
  271. p[len] = '\0';
  272. if (suffix[1] == '\0') { /* Style 2 */
  273. q = (char *)ruby_find_basename(buf, &baselen, 0);
  274. if (len <= 3) {
  275. if (len == 0 && baselen >= 8 && p + 3 <= buf + sizeof(buf)) p[len++] = '.'; /* DOSISH */
  276. p[len] = *suffix;
  277. p[++len] = '\0';
  278. }
  279. else if (q && baselen < 8) {
  280. q += baselen;
  281. *q++ = *suffix;
  282. if (ext) {
  283. strncpy(q, ext, ext - name);
  284. q[ext - name + 1] = '\0';
  285. }
  286. else
  287. *q = '\0';
  288. }
  289. else if (len == 4 && p[3] != *suffix)
  290. p[3] = *suffix;
  291. else if (baselen == 8 && q[7] != *suffix)
  292. q[7] = *suffix;
  293. else
  294. goto fallback;
  295. }
  296. else { /* Style 3: Panic */
  297. fallback:
  298. (void)memcpy(p, !ext || strEQ(ext, suffix1) ? suffix2 : suffix1, 5);
  299. }
  300. rb_str_resize(str, strlen(buf));
  301. memcpy(RSTRING_PTR(str), buf, RSTRING_LEN(str));
  302. }
  303. }
  304. static int
  305. valid_filename(const char *s)
  306. {
  307. int fd;
  308. /*
  309. // It doesn't exist, so see if we can open it.
  310. */
  311. if ((fd = open(s, O_CREAT|O_EXCL, 0666)) >= 0) {
  312. close(fd);
  313. unlink(s); /* don't leave it laying around */
  314. return 1;
  315. }
  316. else if (errno == EEXIST) {
  317. /* if the file exists, then it's a valid filename! */
  318. return 1;
  319. }
  320. return 0;
  321. }
  322. #endif
  323. /* mm.c */
  324. #define A ((int*)a)
  325. #define B ((int*)b)
  326. #define C ((int*)c)
  327. #define D ((int*)d)
  328. #define mmprepare(base, size) do {\
  329. if (((long)base & (0x3)) == 0)\
  330. if (size >= 16) mmkind = 1;\
  331. else mmkind = 0;\
  332. else mmkind = -1;\
  333. high = (size & (~0xf));\
  334. low = (size & 0x0c);\
  335. } while (0)\
  336. #define mmarg mmkind, size, high, low
  337. static void mmswap_(register char *a, register char *b, int mmkind, size_t size, size_t high, size_t low)
  338. {
  339. register int s;
  340. if (a == b) return;
  341. if (mmkind >= 0) {
  342. if (mmkind > 0) {
  343. register char *t = a + high;
  344. do {
  345. s = A[0]; A[0] = B[0]; B[0] = s;
  346. s = A[1]; A[1] = B[1]; B[1] = s;
  347. s = A[2]; A[2] = B[2]; B[2] = s;
  348. s = A[3]; A[3] = B[3]; B[3] = s; a += 16; b += 16;
  349. } while (a < t);
  350. }
  351. if (low != 0) { s = A[0]; A[0] = B[0]; B[0] = s;
  352. if (low >= 8) { s = A[1]; A[1] = B[1]; B[1] = s;
  353. if (low == 12) {s = A[2]; A[2] = B[2]; B[2] = s;}}}
  354. }
  355. else {
  356. register char *t = a + size;
  357. do {s = *a; *a++ = *b; *b++ = s;} while (a < t);
  358. }
  359. }
  360. #define mmswap(a,b) mmswap_((a),(b),mmarg)
  361. static void mmrot3_(register char *a, register char *b, register char *c, int mmkind, size_t size, size_t high, size_t low)
  362. {
  363. register int s;
  364. if (mmkind >= 0) {
  365. if (mmkind > 0) {
  366. register char *t = a + high;
  367. do {
  368. s = A[0]; A[0] = B[0]; B[0] = C[0]; C[0] = s;
  369. s = A[1]; A[1] = B[1]; B[1] = C[1]; C[1] = s;
  370. s = A[2]; A[2] = B[2]; B[2] = C[2]; C[2] = s;
  371. s = A[3]; A[3] = B[3]; B[3] = C[3]; C[3] = s; a += 16; b += 16; c += 16;
  372. } while (a < t);
  373. }
  374. if (low != 0) { s = A[0]; A[0] = B[0]; B[0] = C[0]; C[0] = s;
  375. if (low >= 8) { s = A[1]; A[1] = B[1]; B[1] = C[1]; C[1] = s;
  376. if (low == 12) {s = A[2]; A[2] = B[2]; B[2] = C[2]; C[2] = s;}}}
  377. }
  378. else {
  379. register char *t = a + size;
  380. do {s = *a; *a++ = *b; *b++ = *c; *c++ = s;} while (a < t);
  381. }
  382. }
  383. #define mmrot3(a,b,c) mmrot3_((a),(b),(c),mmarg)
  384. /* qs6.c */
  385. /*****************************************************/
  386. /* */
  387. /* qs6 (Quick sort function) */
  388. /* */
  389. /* by Tomoyuki Kawamura 1995.4.21 */
  390. /* kawamura@tokuyama.ac.jp */
  391. /*****************************************************/
  392. typedef struct { char *LL, *RR; } stack_node; /* Stack structure for L,l,R,r */
  393. #define PUSH(ll,rr) do { top->LL = (ll); top->RR = (rr); ++top; } while (0) /* Push L,l,R,r */
  394. #define POP(ll,rr) do { --top; ll = top->LL; rr = top->RR; } while (0) /* Pop L,l,R,r */
  395. #define med3(a,b,c) ((*cmp)(a,b,d)<0 ? \
  396. ((*cmp)(b,c,d)<0 ? b : ((*cmp)(a,c,d)<0 ? c : a)) : \
  397. ((*cmp)(b,c,d)>0 ? b : ((*cmp)(a,c,d)<0 ? a : c)))
  398. void
  399. ruby_qsort(void* base, const size_t nel, const size_t size,
  400. int (*cmp)(const void*, const void*, void*), void *d)
  401. {
  402. register char *l, *r, *m; /* l,r:left,right group m:median point */
  403. register int t, eq_l, eq_r; /* eq_l: all items in left group are equal to S */
  404. char *L = base; /* left end of current region */
  405. char *R = (char*)base + size*(nel-1); /* right end of current region */
  406. size_t chklim = 63; /* threshold of ordering element check */
  407. stack_node stack[32], *top = stack; /* 32 is enough for 32bit CPU */
  408. int mmkind;
  409. size_t high, low, n;
  410. if (nel <= 1) return; /* need not to sort */
  411. mmprepare(base, size);
  412. goto start;
  413. nxt:
  414. if (stack == top) return; /* return if stack is empty */
  415. POP(L,R);
  416. for (;;) {
  417. start:
  418. if (L + size == R) { /* 2 elements */
  419. if ((*cmp)(L,R,d) > 0) mmswap(L,R); goto nxt;
  420. }
  421. l = L; r = R;
  422. n = (r - l + size) / size; /* number of elements */
  423. m = l + size * (n >> 1); /* calculate median value */
  424. if (n >= 60) {
  425. register char *m1;
  426. register char *m3;
  427. if (n >= 200) {
  428. n = size*(n>>3); /* number of bytes in splitting 8 */
  429. {
  430. register char *p1 = l + n;
  431. register char *p2 = p1 + n;
  432. register char *p3 = p2 + n;
  433. m1 = med3(p1, p2, p3);
  434. p1 = m + n;
  435. p2 = p1 + n;
  436. p3 = p2 + n;
  437. m3 = med3(p1, p2, p3);
  438. }
  439. }
  440. else {
  441. n = size*(n>>2); /* number of bytes in splitting 4 */
  442. m1 = l + n;
  443. m3 = m + n;
  444. }
  445. m = med3(m1, m, m3);
  446. }
  447. if ((t = (*cmp)(l,m,d)) < 0) { /*3-5-?*/
  448. if ((t = (*cmp)(m,r,d)) < 0) { /*3-5-7*/
  449. if (chklim && nel >= chklim) { /* check if already ascending order */
  450. char *p;
  451. chklim = 0;
  452. for (p=l; p<r; p+=size) if ((*cmp)(p,p+size,d) > 0) goto fail;
  453. goto nxt;
  454. }
  455. fail: goto loopA; /*3-5-7*/
  456. }
  457. if (t > 0) {
  458. if ((*cmp)(l,r,d) <= 0) {mmswap(m,r); goto loopA;} /*3-5-4*/
  459. mmrot3(r,m,l); goto loopA; /*3-5-2*/
  460. }
  461. goto loopB; /*3-5-5*/
  462. }
  463. if (t > 0) { /*7-5-?*/
  464. if ((t = (*cmp)(m,r,d)) > 0) { /*7-5-3*/
  465. if (chklim && nel >= chklim) { /* check if already ascending order */
  466. char *p;
  467. chklim = 0;
  468. for (p=l; p<r; p+=size) if ((*cmp)(p,p+size,d) < 0) goto fail2;
  469. while (l<r) {mmswap(l,r); l+=size; r-=size;} /* reverse region */
  470. goto nxt;
  471. }
  472. fail2: mmswap(l,r); goto loopA; /*7-5-3*/
  473. }
  474. if (t < 0) {
  475. if ((*cmp)(l,r,d) <= 0) {mmswap(l,m); goto loopB;} /*7-5-8*/
  476. mmrot3(l,m,r); goto loopA; /*7-5-6*/
  477. }
  478. mmswap(l,r); goto loopA; /*7-5-5*/
  479. }
  480. if ((t = (*cmp)(m,r,d)) < 0) {goto loopA;} /*5-5-7*/
  481. if (t > 0) {mmswap(l,r); goto loopB;} /*5-5-3*/
  482. /* determining splitting type in case 5-5-5 */ /*5-5-5*/
  483. for (;;) {
  484. if ((l += size) == r) goto nxt; /*5-5-5*/
  485. if (l == m) continue;
  486. if ((t = (*cmp)(l,m,d)) > 0) {mmswap(l,r); l = L; goto loopA;}/*575-5*/
  487. if (t < 0) {mmswap(L,l); l = L; goto loopB;} /*535-5*/
  488. }
  489. loopA: eq_l = 1; eq_r = 1; /* splitting type A */ /* left <= median < right */
  490. for (;;) {
  491. for (;;) {
  492. if ((l += size) == r)
  493. {l -= size; if (l != m) mmswap(m,l); l -= size; goto fin;}
  494. if (l == m) continue;
  495. if ((t = (*cmp)(l,m,d)) > 0) {eq_r = 0; break;}
  496. if (t < 0) eq_l = 0;
  497. }
  498. for (;;) {
  499. if (l == (r -= size))
  500. {l -= size; if (l != m) mmswap(m,l); l -= size; goto fin;}
  501. if (r == m) {m = l; break;}
  502. if ((t = (*cmp)(r,m,d)) < 0) {eq_l = 0; break;}
  503. if (t == 0) break;
  504. }
  505. mmswap(l,r); /* swap left and right */
  506. }
  507. loopB: eq_l = 1; eq_r = 1; /* splitting type B */ /* left < median <= right */
  508. for (;;) {
  509. for (;;) {
  510. if (l == (r -= size))
  511. {r += size; if (r != m) mmswap(r,m); r += size; goto fin;}
  512. if (r == m) continue;
  513. if ((t = (*cmp)(r,m,d)) < 0) {eq_l = 0; break;}
  514. if (t > 0) eq_r = 0;
  515. }
  516. for (;;) {
  517. if ((l += size) == r)
  518. {r += size; if (r != m) mmswap(r,m); r += size; goto fin;}
  519. if (l == m) {m = r; break;}
  520. if ((t = (*cmp)(l,m,d)) > 0) {eq_r = 0; break;}
  521. if (t == 0) break;
  522. }
  523. mmswap(l,r); /* swap left and right */
  524. }
  525. fin:
  526. if (eq_l == 0) /* need to sort left side */
  527. if (eq_r == 0) /* need to sort right side */
  528. if (l-L < R-r) {PUSH(r,R); R = l;} /* sort left side first */
  529. else {PUSH(L,l); L = r;} /* sort right side first */
  530. else R = l; /* need to sort left side only */
  531. else if (eq_r == 0) L = r; /* need to sort right side only */
  532. else goto nxt; /* need not to sort both sides */
  533. }
  534. }
  535. char *
  536. ruby_strdup(const char *str)
  537. {
  538. char *tmp;
  539. size_t len = strlen(str) + 1;
  540. tmp = xmalloc(len);
  541. memcpy(tmp, str, len);
  542. return tmp;
  543. }
  544. char *
  545. ruby_getcwd(void)
  546. {
  547. #ifdef HAVE_GETCWD
  548. int size = 200;
  549. char *buf = xmalloc(size);
  550. while (!getcwd(buf, size)) {
  551. if (errno != ERANGE) {
  552. xfree(buf);
  553. rb_sys_fail("getcwd");
  554. }
  555. size *= 2;
  556. buf = xrealloc(buf, size);
  557. }
  558. #else
  559. # ifndef PATH_MAX
  560. # define PATH_MAX 8192
  561. # endif
  562. char *buf = xmalloc(PATH_MAX+1);
  563. if (!getwd(buf)) {
  564. xfree(buf);
  565. rb_sys_fail("getwd");
  566. }
  567. #endif
  568. return buf;
  569. }
  570. /****************************************************************
  571. *
  572. * The author of this software is David M. Gay.
  573. *
  574. * Copyright (c) 1991, 2000, 2001 by Lucent Technologies.
  575. *
  576. * Permission to use, copy, modify, and distribute this software for any
  577. * purpose without fee is hereby granted, provided that this entire notice
  578. * is included in all copies of any software which is or includes a copy
  579. * or modification of this software and in all copies of the supporting
  580. * documentation for such software.
  581. *
  582. * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
  583. * WARRANTY. IN PARTICULAR, NEITHER THE AUTHOR NOR LUCENT MAKES ANY
  584. * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
  585. * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
  586. *
  587. ***************************************************************/
  588. /* Please send bug reports to David M. Gay (dmg at acm dot org,
  589. * with " at " changed at "@" and " dot " changed to "."). */
  590. /* On a machine with IEEE extended-precision registers, it is
  591. * necessary to specify double-precision (53-bit) rounding precision
  592. * before invoking strtod or dtoa. If the machine uses (the equivalent
  593. * of) Intel 80x87 arithmetic, the call
  594. * _control87(PC_53, MCW_PC);
  595. * does this with many compilers. Whether this or another call is
  596. * appropriate depends on the compiler; for this to work, it may be
  597. * necessary to #include "float.h" or another system-dependent header
  598. * file.
  599. */
  600. /* strtod for IEEE-, VAX-, and IBM-arithmetic machines.
  601. *
  602. * This strtod returns a nearest machine number to the input decimal
  603. * string (or sets errno to ERANGE). With IEEE arithmetic, ties are
  604. * broken by the IEEE round-even rule. Otherwise ties are broken by
  605. * biased rounding (add half and chop).
  606. *
  607. * Inspired loosely by William D. Clinger's paper "How to Read Floating
  608. * Point Numbers Accurately" [Proc. ACM SIGPLAN '90, pp. 92-101].
  609. *
  610. * Modifications:
  611. *
  612. * 1. We only require IEEE, IBM, or VAX double-precision
  613. * arithmetic (not IEEE double-extended).
  614. * 2. We get by with floating-point arithmetic in a case that
  615. * Clinger missed -- when we're computing d * 10^n
  616. * for a small integer d and the integer n is not too
  617. * much larger than 22 (the maximum integer k for which
  618. * we can represent 10^k exactly), we may be able to
  619. * compute (d*10^k) * 10^(e-k) with just one roundoff.
  620. * 3. Rather than a bit-at-a-time adjustment of the binary
  621. * result in the hard case, we use floating-point
  622. * arithmetic to determine the adjustment to within
  623. * one bit; only in really hard cases do we need to
  624. * compute a second residual.
  625. * 4. Because of 3., we don't need a large table of powers of 10
  626. * for ten-to-e (just some small tables, e.g. of 10^k
  627. * for 0 <= k <= 22).
  628. */
  629. /*
  630. * #define IEEE_LITTLE_ENDIAN for IEEE-arithmetic machines where the least
  631. * significant byte has the lowest address.
  632. * #define IEEE_BIG_ENDIAN for IEEE-arithmetic machines where the most
  633. * significant byte has the lowest address.
  634. * #define Long int on machines with 32-bit ints and 64-bit longs.
  635. * #define IBM for IBM mainframe-style floating-point arithmetic.
  636. * #define VAX for VAX-style floating-point arithmetic (D_floating).
  637. * #define No_leftright to omit left-right logic in fast floating-point
  638. * computation of dtoa.
  639. * #define Honor_FLT_ROUNDS if FLT_ROUNDS can assume the values 2 or 3
  640. * and strtod and dtoa should round accordingly.
  641. * #define Check_FLT_ROUNDS if FLT_ROUNDS can assume the values 2 or 3
  642. * and Honor_FLT_ROUNDS is not #defined.
  643. * #define RND_PRODQUOT to use rnd_prod and rnd_quot (assembly routines
  644. * that use extended-precision instructions to compute rounded
  645. * products and quotients) with IBM.
  646. * #define ROUND_BIASED for IEEE-format with biased rounding.
  647. * #define Inaccurate_Divide for IEEE-format with correctly rounded
  648. * products but inaccurate quotients, e.g., for Intel i860.
  649. * #define NO_LONG_LONG on machines that do not have a "long long"
  650. * integer type (of >= 64 bits). On such machines, you can
  651. * #define Just_16 to store 16 bits per 32-bit Long when doing
  652. * high-precision integer arithmetic. Whether this speeds things
  653. * up or slows things down depends on the machine and the number
  654. * being converted. If long long is available and the name is
  655. * something other than "long long", #define Llong to be the name,
  656. * and if "unsigned Llong" does not work as an unsigned version of
  657. * Llong, #define #ULLong to be the corresponding unsigned type.
  658. * #define KR_headers for old-style C function headers.
  659. * #define Bad_float_h if your system lacks a float.h or if it does not
  660. * define some or all of DBL_DIG, DBL_MAX_10_EXP, DBL_MAX_EXP,
  661. * FLT_RADIX, FLT_ROUNDS, and DBL_MAX.
  662. * #define MALLOC your_malloc, where your_malloc(n) acts like malloc(n)
  663. * if memory is available and otherwise does something you deem
  664. * appropriate. If MALLOC is undefined, malloc will be invoked
  665. * directly -- and assumed always to succeed.
  666. * #define Omit_Private_Memory to omit logic (added Jan. 1998) for making
  667. * memory allocations from a private pool of memory when possible.
  668. * When used, the private pool is PRIVATE_MEM bytes long: 2304 bytes,
  669. * unless #defined to be a different length. This default length
  670. * suffices to get rid of MALLOC calls except for unusual cases,
  671. * such as decimal-to-binary conversion of a very long string of
  672. * digits. The longest string dtoa can return is about 751 bytes
  673. * long. For conversions by strtod of strings of 800 digits and
  674. * all dtoa conversions in single-threaded executions with 8-byte
  675. * pointers, PRIVATE_MEM >= 7400 appears to suffice; with 4-byte
  676. * pointers, PRIVATE_MEM >= 7112 appears adequate.
  677. * #define INFNAN_CHECK on IEEE systems to cause strtod to check for
  678. * Infinity and NaN (case insensitively). On some systems (e.g.,
  679. * some HP systems), it may be necessary to #define NAN_WORD0
  680. * appropriately -- to the most significant word of a quiet NaN.
  681. * (On HP Series 700/800 machines, -DNAN_WORD0=0x7ff40000 works.)
  682. * When INFNAN_CHECK is #defined and No_Hex_NaN is not #defined,
  683. * strtod also accepts (case insensitively) strings of the form
  684. * NaN(x), where x is a string of hexadecimal digits and spaces;
  685. * if there is only one string of hexadecimal digits, it is taken
  686. * for the 52 fraction bits of the resulting NaN; if there are two
  687. * or more strings of hex digits, the first is for the high 20 bits,
  688. * the second and subsequent for the low 32 bits, with intervening
  689. * white space ignored; but if this results in none of the 52
  690. * fraction bits being on (an IEEE Infinity symbol), then NAN_WORD0
  691. * and NAN_WORD1 are used instead.
  692. * #define MULTIPLE_THREADS if the system offers preemptively scheduled
  693. * multiple threads. In this case, you must provide (or suitably
  694. * #define) two locks, acquired by ACQUIRE_DTOA_LOCK(n) and freed
  695. * by FREE_DTOA_LOCK(n) for n = 0 or 1. (The second lock, accessed
  696. * in pow5mult, ensures lazy evaluation of only one copy of high
  697. * powers of 5; omitting this lock would introduce a small
  698. * probability of wasting memory, but would otherwise be harmless.)
  699. * You must also invoke freedtoa(s) to free the value s returned by
  700. * dtoa. You may do so whether or not MULTIPLE_THREADS is #defined.
  701. * #define NO_IEEE_Scale to disable new (Feb. 1997) logic in strtod that
  702. * avoids underflows on inputs whose result does not underflow.
  703. * If you #define NO_IEEE_Scale on a machine that uses IEEE-format
  704. * floating-point numbers and flushes underflows to zero rather
  705. * than implementing gradual underflow, then you must also #define
  706. * Sudden_Underflow.
  707. * #define YES_ALIAS to permit aliasing certain double values with
  708. * arrays of ULongs. This leads to slightly better code with
  709. * some compilers and was always used prior to 19990916, but it
  710. * is not strictly legal and can cause trouble with aggressively
  711. * optimizing compilers (e.g., gcc 2.95.1 under -O2).
  712. * #define USE_LOCALE to use the current locale's decimal_point value.
  713. * #define SET_INEXACT if IEEE arithmetic is being used and extra
  714. * computation should be done to set the inexact flag when the
  715. * result is inexact and avoid setting inexact when the result
  716. * is exact. In this case, dtoa.c must be compiled in
  717. * an environment, perhaps provided by #include "dtoa.c" in a
  718. * suitable wrapper, that defines two functions,
  719. * int get_inexact(void);
  720. * void clear_inexact(void);
  721. * such that get_inexact() returns a nonzero value if the
  722. * inexact bit is already set, and clear_inexact() sets the
  723. * inexact bit to 0. When SET_INEXACT is #defined, strtod
  724. * also does extra computations to set the underflow and overflow
  725. * flags when appropriate (i.e., when the result is tiny and
  726. * inexact or when it is a numeric value rounded to +-infinity).
  727. * #define NO_ERRNO if strtod should not assign errno = ERANGE when
  728. * the result overflows to +-Infinity or underflows to 0.
  729. */
  730. #ifdef WORDS_BIGENDIAN
  731. #define IEEE_BIG_ENDIAN
  732. #else
  733. #define IEEE_LITTLE_ENDIAN
  734. #endif
  735. #ifdef __vax__
  736. #define VAX
  737. #undef IEEE_BIG_ENDIAN
  738. #undef IEEE_LITTLE_ENDIAN
  739. #endif
  740. #if defined(__arm__) && !defined(__VFP_FP__)
  741. #define IEEE_BIG_ENDIAN
  742. #undef IEEE_LITTLE_ENDIAN
  743. #endif
  744. #undef Long
  745. #undef ULong
  746. #if SIZEOF_INT == 4
  747. #define Long int
  748. #define ULong unsigned int
  749. #elif SIZEOF_LONG == 4
  750. #define Long long int
  751. #define ULong unsigned long int
  752. #endif
  753. #if HAVE_LONG_LONG
  754. #define Llong LONG_LONG
  755. #endif
  756. #ifdef DEBUG
  757. #include "stdio.h"
  758. #define Bug(x) {fprintf(stderr, "%s\n", x); exit(1);}
  759. #endif
  760. #include "stdlib.h"
  761. #include "string.h"
  762. #ifdef USE_LOCALE
  763. #include "locale.h"
  764. #endif
  765. #ifdef MALLOC
  766. extern void *MALLOC(size_t);
  767. #else
  768. #define MALLOC malloc
  769. #endif
  770. #ifndef Omit_Private_Memory
  771. #ifndef PRIVATE_MEM
  772. #define PRIVATE_MEM 2304
  773. #endif
  774. #define PRIVATE_mem ((PRIVATE_MEM+sizeof(double)-1)/sizeof(double))
  775. static double private_mem[PRIVATE_mem], *pmem_next = private_mem;
  776. #endif
  777. #undef IEEE_Arith
  778. #undef Avoid_Underflow
  779. #ifdef IEEE_BIG_ENDIAN
  780. #define IEEE_Arith
  781. #endif
  782. #ifdef IEEE_LITTLE_ENDIAN
  783. #define IEEE_Arith
  784. #endif
  785. #ifdef Bad_float_h
  786. #ifdef IEEE_Arith
  787. #define DBL_DIG 15
  788. #define DBL_MAX_10_EXP 308
  789. #define DBL_MAX_EXP 1024
  790. #define FLT_RADIX 2
  791. #endif /*IEEE_Arith*/
  792. #ifdef IBM
  793. #define DBL_DIG 16
  794. #define DBL_MAX_10_EXP 75
  795. #define DBL_MAX_EXP 63
  796. #define FLT_RADIX 16
  797. #define DBL_MAX 7.2370055773322621e+75
  798. #endif
  799. #ifdef VAX
  800. #define DBL_DIG 16
  801. #define DBL_MAX_10_EXP 38
  802. #define DBL_MAX_EXP 127
  803. #define FLT_RADIX 2
  804. #define DBL_MAX 1.7014118346046923e+38
  805. #endif
  806. #ifndef LONG_MAX
  807. #define LONG_MAX 2147483647
  808. #endif
  809. #else /* ifndef Bad_float_h */
  810. #include "float.h"
  811. #endif /* Bad_float_h */
  812. #ifndef __MATH_H__
  813. #include "math.h"
  814. #endif
  815. #ifdef __cplusplus
  816. extern "C" {
  817. #if 0
  818. }
  819. #endif
  820. #endif
  821. #if defined(IEEE_LITTLE_ENDIAN) + defined(IEEE_BIG_ENDIAN) + defined(VAX) + defined(IBM) != 1
  822. Exactly one of IEEE_LITTLE_ENDIAN, IEEE_BIG_ENDIAN, VAX, or IBM should be defined.
  823. #endif
  824. typedef union { double d; ULong L[2]; } U;
  825. #ifdef YES_ALIAS
  826. typedef double double_u;
  827. # define dval(x) x
  828. # ifdef IEEE_LITTLE_ENDIAN
  829. # define word0(x) (((ULong *)&x)[1])
  830. # define word1(x) (((ULong *)&x)[0])
  831. # else
  832. # define word0(x) (((ULong *)&x)[0])
  833. # define word1(x) (((ULong *)&x)[1])
  834. # endif
  835. #else
  836. typedef U double_u;
  837. # ifdef IEEE_LITTLE_ENDIAN
  838. # define word0(x) (x.L[1])
  839. # define word1(x) (x.L[0])
  840. # else
  841. # define word0(x) (x.L[0])
  842. # define word1(x) (x.L[1])
  843. # endif
  844. # define dval(x) (x.d)
  845. #endif
  846. /* The following definition of Storeinc is appropriate for MIPS processors.
  847. * An alternative that might be better on some machines is
  848. * #define Storeinc(a,b,c) (*a++ = b << 16 | c & 0xffff)
  849. */
  850. #if defined(IEEE_LITTLE_ENDIAN) + defined(VAX) + defined(__arm__)
  851. #define Storeinc(a,b,c) (((unsigned short *)a)[1] = (unsigned short)b, \
  852. ((unsigned short *)a)[0] = (unsigned short)c, a++)
  853. #else
  854. #define Storeinc(a,b,c) (((unsigned short *)a)[0] = (unsigned short)b, \
  855. ((unsigned short *)a)[1] = (unsigned short)c, a++)
  856. #endif
  857. /* #define P DBL_MANT_DIG */
  858. /* Ten_pmax = floor(P*log(2)/log(5)) */
  859. /* Bletch = (highest power of 2 < DBL_MAX_10_EXP) / 16 */
  860. /* Quick_max = floor((P-1)*log(FLT_RADIX)/log(10) - 1) */
  861. /* Int_max = floor(P*log(FLT_RADIX)/log(10) - 1) */
  862. #ifdef IEEE_Arith
  863. #define Exp_shift 20
  864. #define Exp_shift1 20
  865. #define Exp_msk1 0x100000
  866. #define Exp_msk11 0x100000
  867. #define Exp_mask 0x7ff00000
  868. #define P 53
  869. #define Bias 1023
  870. #define Emin (-1022)
  871. #define Exp_1 0x3ff00000
  872. #define Exp_11 0x3ff00000
  873. #define Ebits 11
  874. #define Frac_mask 0xfffff
  875. #define Frac_mask1 0xfffff
  876. #define Ten_pmax 22
  877. #define Bletch 0x10
  878. #define Bndry_mask 0xfffff
  879. #define Bndry_mask1 0xfffff
  880. #define LSB 1
  881. #define Sign_bit 0x80000000
  882. #define Log2P 1
  883. #define Tiny0 0
  884. #define Tiny1 1
  885. #define Quick_max 14
  886. #define Int_max 14
  887. #ifndef NO_IEEE_Scale
  888. #define Avoid_Underflow
  889. #ifdef Flush_Denorm /* debugging option */
  890. #undef Sudden_Underflow
  891. #endif
  892. #endif
  893. #ifndef Flt_Rounds
  894. #ifdef FLT_ROUNDS
  895. #define Flt_Rounds FLT_ROUNDS
  896. #else
  897. #define Flt_Rounds 1
  898. #endif
  899. #endif /*Flt_Rounds*/
  900. #ifdef Honor_FLT_ROUNDS
  901. #define Rounding rounding
  902. #undef Check_FLT_ROUNDS
  903. #define Check_FLT_ROUNDS
  904. #else
  905. #define Rounding Flt_Rounds
  906. #endif
  907. #else /* ifndef IEEE_Arith */
  908. #undef Check_FLT_ROUNDS
  909. #undef Honor_FLT_ROUNDS
  910. #undef SET_INEXACT
  911. #undef Sudden_Underflow
  912. #define Sudden_Underflow
  913. #ifdef IBM
  914. #undef Flt_Rounds
  915. #define Flt_Rounds 0
  916. #define Exp_shift 24
  917. #define Exp_shift1 24
  918. #define Exp_msk1 0x1000000
  919. #define Exp_msk11 0x1000000
  920. #define Exp_mask 0x7f000000
  921. #define P 14
  922. #define Bias 65
  923. #define Exp_1 0x41000000
  924. #define Exp_11 0x41000000
  925. #define Ebits 8 /* exponent has 7 bits, but 8 is the right value in b2d */
  926. #define Frac_mask 0xffffff
  927. #define Frac_mask1 0xffffff
  928. #define Bletch 4
  929. #define Ten_pmax 22
  930. #define Bndry_mask 0xefffff
  931. #define Bndry_mask1 0xffffff
  932. #define LSB 1
  933. #define Sign_bit 0x80000000
  934. #define Log2P 4
  935. #define Tiny0 0x100000
  936. #define Tiny1 0
  937. #define Quick_max 14
  938. #define Int_max 15
  939. #else /* VAX */
  940. #undef Flt_Rounds
  941. #define Flt_Rounds 1
  942. #define Exp_shift 23
  943. #define Exp_shift1 7
  944. #define Exp_msk1 0x80
  945. #define Exp_msk11 0x800000
  946. #define Exp_mask 0x7f80
  947. #define P 56
  948. #define Bias 129
  949. #define Exp_1 0x40800000
  950. #define Exp_11 0x4080
  951. #define Ebits 8
  952. #define Frac_mask 0x7fffff
  953. #define Frac_mask1 0xffff007f
  954. #define Ten_pmax 24
  955. #define Bletch 2
  956. #define Bndry_mask 0xffff007f
  957. #define Bndry_mask1 0xffff007f
  958. #define LSB 0x10000
  959. #define Sign_bit 0x8000
  960. #define Log2P 1
  961. #define Tiny0 0x80
  962. #define Tiny1 0
  963. #define Quick_max 15
  964. #define Int_max 15
  965. #endif /* IBM, VAX */
  966. #endif /* IEEE_Arith */
  967. #ifndef IEEE_Arith
  968. #define ROUND_BIASED
  969. #endif
  970. #ifdef RND_PRODQUOT
  971. #define rounded_product(a,b) a = rnd_prod(a, b)
  972. #define rounded_quotient(a,b) a = rnd_quot(a, b)
  973. extern double rnd_prod(double, double), rnd_quot(double, double);
  974. #else
  975. #define rounded_product(a,b) a *= b
  976. #define rounded_quotient(a,b) a /= b
  977. #endif
  978. #define Big0 (Frac_mask1 | Exp_msk1*(DBL_MAX_EXP+Bias-1))
  979. #define Big1 0xffffffff
  980. #ifndef Pack_32
  981. #define Pack_32
  982. #endif
  983. #define FFFFFFFF 0xffffffffUL
  984. #ifdef NO_LONG_LONG
  985. #undef ULLong
  986. #ifdef Just_16
  987. #undef Pack_32
  988. /* When Pack_32 is not defined, we store 16 bits per 32-bit Long.
  989. * This makes some inner loops simpler and sometimes saves work
  990. * during multiplications, but it often seems to make things slightly
  991. * slower. Hence the default is now to store 32 bits per Long.
  992. */
  993. #endif
  994. #else /* long long available */
  995. #ifndef Llong
  996. #define Llong long long
  997. #endif
  998. #ifndef ULLong
  999. #define ULLong unsigned Llong
  1000. #endif
  1001. #endif /* NO_LONG_LONG */
  1002. #define MULTIPLE_THREADS 1
  1003. #ifndef MULTIPLE_THREADS
  1004. #define ACQUIRE_DTOA_LOCK(n) /*nothing*/
  1005. #define FREE_DTOA_LOCK(n) /*nothing*/
  1006. #else
  1007. #define ACQUIRE_DTOA_LOCK(n) /*unused right now*/
  1008. #define FREE_DTOA_LOCK(n) /*unused right now*/
  1009. #endif
  1010. #define Kmax 15
  1011. struct Bigint {
  1012. struct Bigint *next;
  1013. int k, maxwds, sign, wds;
  1014. ULong x[1];
  1015. };
  1016. typedef struct Bigint Bigint;
  1017. static Bigint *freelist[Kmax+1];
  1018. static Bigint *
  1019. Balloc(int k)
  1020. {
  1021. int x;
  1022. Bigint *rv;
  1023. #ifndef Omit_Private_Memory
  1024. size_t len;
  1025. #endif
  1026. ACQUIRE_DTOA_LOCK(0);
  1027. if ((rv = freelist[k]) != 0) {
  1028. freelist[k] = rv->next;
  1029. }
  1030. else {
  1031. x = 1 << k;
  1032. #ifdef Omit_Private_Memory
  1033. rv = (Bigint *)MALLOC(sizeof(Bigint) + (x-1)*sizeof(ULong));
  1034. #else
  1035. len = (sizeof(Bigint) + (x-1)*sizeof(ULong) + sizeof(double) - 1)
  1036. /sizeof(double);
  1037. if (pmem_next - private_mem + len <= PRIVATE_mem) {
  1038. rv = (Bigint*)pmem_next;
  1039. pmem_next += len;
  1040. }
  1041. else
  1042. rv = (Bigint*)MALLOC(len*sizeof(double));
  1043. #endif
  1044. rv->k = k;
  1045. rv->maxwds = x;
  1046. }
  1047. FREE_DTOA_LOCK(0);
  1048. rv->sign = rv->wds = 0;
  1049. return rv;
  1050. }
  1051. static void
  1052. Bfree(Bigint *v)
  1053. {
  1054. if (v) {
  1055. ACQUIRE_DTOA_LOCK(0);
  1056. v->next = freelist[v->k];
  1057. freelist[v->k] = v;
  1058. FREE_DTOA_LOCK(0);
  1059. }
  1060. }
  1061. #define Bcopy(x,y) memcpy((char *)&x->sign, (char *)&y->sign, \
  1062. y->wds*sizeof(Long) + 2*sizeof(int))
  1063. static Bigint *
  1064. multadd(Bigint *b, int m, int a) /* multiply by m and add a */
  1065. {
  1066. int i, wds;
  1067. ULong *x;
  1068. #ifdef ULLong
  1069. ULLong carry, y;
  1070. #else
  1071. ULong carry, y;
  1072. #ifdef Pack_32
  1073. ULong xi, z;
  1074. #endif
  1075. #endif
  1076. Bigint *b1;
  1077. wds = b->wds;
  1078. x = b->x;
  1079. i = 0;
  1080. carry = a;
  1081. do {
  1082. #ifdef ULLong
  1083. y = *x * (ULLong)m + carry;
  1084. carry = y >> 32;
  1085. *x++ = (ULong)(y & FFFFFFFF);
  1086. #else
  1087. #ifdef Pack_32
  1088. xi = *x;
  1089. y = (xi & 0xffff) * m + carry;
  1090. z = (xi >> 16) * m + (y >> 16);
  1091. carry = z >> 16;
  1092. *x++ = (z << 16) + (y & 0xffff);
  1093. #else
  1094. y = *x * m + carry;
  1095. carry = y >> 16;
  1096. *x++ = y & 0xffff;
  1097. #endif
  1098. #endif
  1099. } while (++i < wds);
  1100. if (carry) {
  1101. if (wds >= b->maxwds) {
  1102. b1 = Balloc(b->k+1);
  1103. Bcopy(b1, b);
  1104. Bfree(b);
  1105. b = b1;
  1106. }
  1107. b->x[wds++] = (ULong)carry;
  1108. b->wds = wds;
  1109. }
  1110. return b;
  1111. }
  1112. static Bigint *
  1113. s2b(const char *s, int nd0, int nd, ULong y9)
  1114. {
  1115. Bigint *b;
  1116. int i, k;
  1117. Long x, y;
  1118. x = (nd + 8) / 9;
  1119. for (k = 0, y = 1; x > y; y <<= 1, k++) ;
  1120. #ifdef Pack_32
  1121. b = Balloc(k);
  1122. b->x[0] = y9;
  1123. b->wds = 1;
  1124. #else
  1125. b = Balloc(k+1);
  1126. b->x[0] = y9 & 0xffff;
  1127. b->wds = (b->x[1] = y9 >> 16) ? 2 : 1;
  1128. #endif
  1129. i = 9;
  1130. if (9 < nd0) {
  1131. s += 9;
  1132. do {
  1133. b = multadd(b, 10, *s++ - '0');
  1134. } while (++i < nd0);
  1135. s++;
  1136. }
  1137. else
  1138. s += 10;
  1139. for (; i < nd; i++)
  1140. b = multadd(b, 10, *s++ - '0');
  1141. return b;
  1142. }
  1143. static int
  1144. hi0bits(register ULong x)
  1145. {
  1146. register int k = 0;
  1147. if (!(x & 0xffff0000)) {
  1148. k = 16;
  1149. x <<= 16;
  1150. }
  1151. if (!(x & 0xff000000)) {
  1152. k += 8;
  1153. x <<= 8;
  1154. }
  1155. if (!(x & 0xf0000000)) {
  1156. k += 4;
  1157. x <<= 4;
  1158. }
  1159. if (!(x & 0xc0000000)) {
  1160. k += 2;
  1161. x <<= 2;
  1162. }
  1163. if (!(x & 0x80000000)) {
  1164. k++;
  1165. if (!(x & 0x40000000))
  1166. return 32;
  1167. }
  1168. return k;
  1169. }
  1170. static int
  1171. lo0bits(ULong *y)
  1172. {
  1173. register int k;
  1174. register ULong x = *y;
  1175. if (x & 7) {
  1176. if (x & 1)
  1177. return 0;
  1178. if (x & 2) {
  1179. *y = x >> 1;
  1180. return 1;
  1181. }
  1182. *y = x >> 2;
  1183. return 2;
  1184. }
  1185. k = 0;
  1186. if (!(x & 0xffff)) {
  1187. k = 16;
  1188. x >>= 16;
  1189. }
  1190. if (!(x & 0xff)) {
  1191. k += 8;
  1192. x >>= 8;
  1193. }
  1194. if (!(x & 0xf)) {
  1195. k += 4;
  1196. x >>= 4;
  1197. }
  1198. if (!(x & 0x3)) {
  1199. k += 2;
  1200. x >>= 2;
  1201. }
  1202. if (!(x & 1)) {
  1203. k++;
  1204. x >>= 1;
  1205. if (!x)
  1206. return 32;
  1207. }
  1208. *y = x;
  1209. return k;
  1210. }
  1211. static Bigint *
  1212. i2b(int i)
  1213. {
  1214. Bigint *b;
  1215. b = Balloc(1);
  1216. b->x[0] = i;
  1217. b->wds = 1;
  1218. return b;
  1219. }
  1220. static Bigint *
  1221. mult(Bigint *a, Bigint *b)
  1222. {
  1223. Bigint *c;
  1224. int k, wa, wb, wc;
  1225. ULong *x, *xa, *xae, *xb, *xbe, *xc, *xc0;
  1226. ULong y;
  1227. #ifdef ULLong
  1228. ULLong carry, z;
  1229. #else
  1230. ULong carry, z;
  1231. #ifdef Pack_32
  1232. ULong z2;
  1233. #endif
  1234. #endif
  1235. if (a->wds < b->wds) {
  1236. c = a;
  1237. a = b;
  1238. b = c;
  1239. }
  1240. k = a->k;
  1241. wa = a->wds;
  1242. wb = b->wds;
  1243. wc = wa + wb;
  1244. if (wc > a->maxwds)
  1245. k++;
  1246. c = Balloc(k);
  1247. for (x = c->x, xa = x + wc; x < xa; x++)
  1248. *x = 0;
  1249. xa = a->x;
  1250. xae = xa + wa;
  1251. xb = b->x;
  1252. xbe = xb + wb;
  1253. xc0 = c->x;
  1254. #ifdef ULLong
  1255. for (; xb < xbe; xc0++) {
  1256. if ((y = *xb++) != 0) {
  1257. x = xa;
  1258. xc = xc0;
  1259. carry = 0;
  1260. do {
  1261. z = *x++ * (ULLong)y + *xc + carry;
  1262. carry = z >> 32;
  1263. *xc++ = (ULong)(z & FFFFFFFF);
  1264. } while (x < xae);
  1265. *xc = (ULong)carry;
  1266. }
  1267. }
  1268. #else
  1269. #ifdef Pack_32
  1270. for (; xb < xbe; xb++, xc0++) {
  1271. if (y = *xb & 0xffff) {
  1272. x = xa;
  1273. xc = xc0;
  1274. carry = 0;
  1275. do {
  1276. z = (*x & 0xffff) * y + (*xc & 0xffff) + carry;
  1277. carry = z >> 16;
  1278. z2 = (*x++ >> 16) * y + (*xc >> 16) + carry;
  1279. carry = z2 >> 16;
  1280. Storeinc(xc, z2, z);
  1281. } while (x < xae);
  1282. *xc = (ULong)carry;
  1283. }
  1284. if (y = *xb >> 16) {
  1285. x = xa;
  1286. xc = xc0;
  1287. carry = 0;
  1288. z2 = *xc;
  1289. do {
  1290. z = (*x & 0xffff) * y + (*xc >> 16) + carry;
  1291. carry = z >> 16;
  1292. Storeinc(xc, z, z2);
  1293. z2 = (*x++ >> 16) * y + (*xc & 0xffff) + carry;
  1294. carry = z2 >> 16;
  1295. } while (x < xae);
  1296. *xc = z2;
  1297. }
  1298. }
  1299. #else
  1300. for (; xb < xbe; xc0++) {
  1301. if (y = *xb++) {
  1302. x = xa;
  1303. xc = xc0;
  1304. carry = 0;
  1305. do {
  1306. z = *x++ * y + *xc + carry;
  1307. carry = z >> 16;
  1308. *xc++ = z & 0xffff;
  1309. } while (x < xae);
  1310. *xc = (ULong)carry;
  1311. }
  1312. }
  1313. #endif
  1314. #endif
  1315. for (xc0 = c->x, xc = xc0 + wc; wc > 0 && !*--xc; --wc) ;
  1316. c->wds = wc;
  1317. return c;
  1318. }
  1319. static Bigint *p5s;
  1320. static Bigint *
  1321. pow5mult(Bigint *b, int k)
  1322. {
  1323. Bigint *b1, *p5, *p51;
  1324. int i;
  1325. static int p05[3] = { 5, 25, 125 };
  1326. if ((i = k & 3) != 0)
  1327. b = multadd(b, p05[i-1], 0);
  1328. if (!(k >>= 2))
  1329. return b;
  1330. if (!(p5 = p5s)) {
  1331. /* first time */
  1332. #ifdef MULTIPLE_THREADS
  1333. ACQUIRE_DTOA_LOCK(1);
  1334. if (!(p5 = p5s)) {
  1335. p5 = p5s = i2b(625);
  1336. p5->next = 0;
  1337. }
  1338. FREE_DTOA_LOCK(1);
  1339. #else
  1340. p5 = p5s = i2b(625);
  1341. p5->next = 0;
  1342. #endif
  1343. }
  1344. for (;;) {
  1345. if (k & 1) {
  1346. b1 = mult(b, p5);
  1347. Bfree(b);
  1348. b = b1;
  1349. }
  1350. if (!(k >>= 1))
  1351. break;
  1352. if (!(p51 = p5->next)) {
  1353. #ifdef MULTIPLE_THREADS
  1354. ACQUIRE_DTOA_LOCK(1);
  1355. if (!(p51 = p5->next)) {
  1356. p51 = p5->next = mult(p5,p5);
  1357. p51->next = 0;
  1358. }
  1359. FREE_DTOA_LOCK(1);
  1360. #else
  1361. p51 = p5->next = mult(p5,p5);
  1362. p51->next = 0;
  1363. #endif
  1364. }
  1365. p5 = p51;
  1366. }
  1367. return b;
  1368. }
  1369. static Bigint *
  1370. lshift(Bigint *b, int k)
  1371. {
  1372. int i, k1, n, n1;
  1373. Bigint *b1;
  1374. ULong *x, *x1, *xe, z;
  1375. #ifdef Pack_32
  1376. n = k >> 5;
  1377. #else
  1378. n = k >> 4;
  1379. #endif
  1380. k1 = b->k;
  1381. n1 = n + b->wds + 1;
  1382. for (i = b->maxwds; n1 > i; i <<= 1)
  1383. k1++;
  1384. b1 = Balloc(k1);
  1385. x1 = b1->x;
  1386. for (i = 0; i < n; i++)
  1387. *x1++ = 0;
  1388. x = b->x;
  1389. xe = x + b->wds;
  1390. #ifdef Pack_32
  1391. if (k &= 0x1f) {
  1392. k1 = 32 - k;
  1393. z = 0;
  1394. do {
  1395. *x1++ = *x << k | z;
  1396. z = *x++ >> k1;
  1397. } while (x < xe);
  1398. if ((*x1 = z) != 0)
  1399. ++n1;
  1400. }
  1401. #else
  1402. if (k &= 0xf) {
  1403. k1 = 16 - k;
  1404. z = 0;
  1405. do {
  1406. *x1++ = *x << k & 0xffff | z;
  1407. z = *x++ >> k1;
  1408. } while (x < xe);
  1409. if (*x1 = z)
  1410. ++n1;
  1411. }
  1412. #endif
  1413. else
  1414. do {
  1415. *x1++ = *x++;
  1416. } while (x < xe);
  1417. b1->wds = n1 - 1;
  1418. Bfree(b);
  1419. return b1;
  1420. }
  1421. static int
  1422. cmp(Bigint *a, Bigint *b)
  1423. {
  1424. ULong *xa, *xa0, *xb, *xb0;
  1425. int i, j;
  1426. i = a->wds;
  1427. j = b->wds;
  1428. #ifdef DEBUG
  1429. if (i > 1 && !a->x[i-1])
  1430. Bug("cmp called with a->x[a->wds-1] == 0");
  1431. if (j > 1 && !b->x[j-1])
  1432. Bug("cmp called with b->x[b->wds-1] == 0");
  1433. #endif
  1434. if (i -= j)
  1435. return i;
  1436. xa0 = a->x;
  1437. xa = xa0 + j;
  1438. xb0 = b->x;
  1439. xb = xb0 + j;
  1440. for (;;) {
  1441. if (*--xa != *--xb)
  1442. return *xa < *xb ? -1 : 1;
  1443. if (xa <= xa0)
  1444. break;
  1445. }
  1446. return 0;
  1447. }
  1448. static Bigint *
  1449. diff(Bigint *a, Bigint *b)
  1450. {
  1451. Bigint *c;
  1452. int i, wa, wb;
  1453. ULong *xa, *xae, *xb, *xbe, *xc;
  1454. #ifdef ULLong
  1455. ULLong borrow, y;
  1456. #else
  1457. ULong borrow, y;
  1458. #ifdef Pack_32
  1459. ULong z;
  1460. #endif
  1461. #endif
  1462. i = cmp(a,b);
  1463. if (!i) {
  1464. c = Balloc(0);
  1465. c->wds = 1;
  1466. c->x[0] = 0;
  1467. return c;
  1468. }
  1469. if (i < 0) {
  1470. c = a;
  1471. a = b;
  1472. b = c;
  1473. i = 1;
  1474. }
  1475. else
  1476. i = 0;
  1477. c = Balloc(a->k);
  1478. c->sign = i;
  1479. wa = a->wds;
  1480. xa = a->x;
  1481. xae = xa + wa;
  1482. wb = b->wds;
  1483. xb = b->x;
  1484. xbe = xb + wb;
  1485. xc = c->x;
  1486. borrow = 0;
  1487. #ifdef ULLong
  1488. do {
  1489. y = (ULLong)*xa++ - *xb++ - borrow;
  1490. borrow = y >> 32 & (ULong)1;
  1491. *xc++ = (ULong)(y & FFFFFFFF);
  1492. } while (xb < xbe);
  1493. while (xa < xae) {
  1494. y = *xa++ - borrow;
  1495. borrow = y >> 32 & (ULong)1;
  1496. *xc++ = (ULong)(y & FFFFFFFF);
  1497. }
  1498. #else
  1499. #ifdef Pack_32
  1500. do {
  1501. y = (*xa & 0xffff) - (*xb & 0xffff) - borrow;
  1502. borrow = (y & 0x10000) >> 16;
  1503. z = (*xa++ >> 16) - (*xb++ >> 16) - borrow;
  1504. borrow = (z & 0x10000) >> 16;
  1505. Storeinc(xc, z, y);
  1506. } while (xb < xbe);
  1507. while (xa < xae) {
  1508. y = (*xa & 0xffff) - borrow;
  1509. borrow = (y & 0x10000) >> 16;
  1510. z = (*xa++ >> 16) - borrow;
  1511. borrow = (z & 0x10000) >> 16;
  1512. Storeinc(xc, z, y);
  1513. }
  1514. #else
  1515. do {
  1516. y = *xa++ - *xb++ - borrow;
  1517. borrow = (y & 0x10000) >> 16;
  1518. *xc++ = y & 0xffff;
  1519. } while (xb < xbe);
  1520. while (xa < xae) {
  1521. y = *xa++ - borrow;
  1522. borrow = (y & 0x10000) >> 16;
  1523. *xc++ = y & 0xffff;
  1524. }
  1525. #endif
  1526. #endif
  1527. while (!*--xc)
  1528. wa--;
  1529. c->wds = wa;
  1530. return c;
  1531. }
  1532. static double
  1533. ulp(double x_)
  1534. {
  1535. register Long L;
  1536. double_u x, a;
  1537. dval(x) = x_;
  1538. L = (word0(x) & Exp_mask) - (P-1)*Exp_msk1;
  1539. #ifndef Avoid_Underflow
  1540. #ifndef Sudden_Underflow
  1541. if (L > 0) {
  1542. #endif
  1543. #endif
  1544. #ifdef IBM
  1545. L |= Exp_msk1 >> 4;
  1546. #endif
  1547. word0(a) = L;
  1548. word1(a) = 0;
  1549. #ifndef Avoid_Underflow
  1550. #ifndef Sudden_Underflow
  1551. }
  1552. else {
  1553. L = -L >> Exp_shift;
  1554. if (L < Exp_shift) {
  1555. word0(a) = 0x80000 >> L;
  1556. word1(a) = 0;
  1557. }
  1558. else {
  1559. word0(a) = 0;
  1560. L -= Exp_shift;
  1561. word1(a) = L >= 31 ? 1 : 1 << 31 - L;
  1562. }
  1563. }
  1564. #endif
  1565. #endif
  1566. return dval(a);
  1567. }
  1568. static double
  1569. b2d(Bigint *a, int *e)
  1570. {
  1571. ULong *xa, *xa0, w, y, z;
  1572. int k;
  1573. double_u d;
  1574. #ifdef VAX
  1575. ULong d0, d1;
  1576. #else
  1577. #define d0 word0(d)
  1578. #define d1 word1(d)
  1579. #endif
  1580. xa0 = a->x;
  1581. xa = xa0 + a->wds;
  1582. y = *--xa;
  1583. #ifdef DEBUG
  1584. if (!y) Bug("zero y in b2d");
  1585. #endif
  1586. k = hi0bits(y);
  1587. *e = 32 - k;
  1588. #ifdef Pack_32
  1589. if (k < Ebits) {
  1590. d0 = Exp_1 | y >> (Ebits - k);
  1591. w = xa > xa0 ? *--xa : 0;
  1592. d1 = y << ((32-Ebits) + k) | w >> (Ebits - k);
  1593. goto ret_d;
  1594. }
  1595. z = xa > xa0 ? *--xa : 0;
  1596. if (k -= Ebits) {
  1597. d0 = Exp_1 | y << k | z >> (32 - k);
  1598. y = xa > xa0 ? *--xa : 0;
  1599. d1 = z << k | y >> (32 - k);
  1600. }
  1601. else {
  1602. d0 = Exp_1 | y;
  1603. d1 = z;
  1604. }
  1605. #else
  1606. if (k < Ebits + 16) {
  1607. z = xa > xa0 ? *--xa : 0;
  1608. d0 = Exp_1 | y << k - Ebits | z >> Ebits + 16 - k;
  1609. w = xa > xa0 ? *--xa : 0;
  1610. y = xa > xa0 ? *--xa : 0;
  1611. d1 = z << k + 16 - Ebits | w << k - Ebits | y >> 16 + Ebits - k;
  1612. goto ret_d;
  1613. }
  1614. z = xa > xa0 ? *--xa : 0;
  1615. w = xa > xa0 ? *--xa : 0;
  1616. k -= Ebits + 16;
  1617. d0 = Exp_1 | y << k + 16 | z << k | w >> 16 - k;
  1618. y = xa > xa0 ? *--xa : 0;
  1619. d1 = w << k + 16 | y << k;
  1620. #endif
  1621. ret_d:
  1622. #ifdef VAX
  1623. word0(d) = d0 >> 16 | d0 << 16;
  1624. word1(d) = d1 >> 16 | d1 << 16;
  1625. #else
  1626. #undef d0
  1627. #undef d1
  1628. #endif
  1629. return dval(d);
  1630. }
  1631. static Bigint *
  1632. d2b(double d_, int *e, int *bits)
  1633. {
  1634. double_u d;
  1635. Bigint *b;
  1636. int de, k;
  1637. ULong *x, y, z;
  1638. #ifndef Sudden_Underflow
  1639. int i;
  1640. #endif
  1641. #ifdef VAX
  1642. ULong d0, d1;
  1643. #endif
  1644. dval(d) = d_;
  1645. #ifdef VAX
  1646. d0 = word0(d) >> 16 | word0(d) << 16;
  1647. d1 = word1(d) >> 16 | word1(d) << 16;
  1648. #else
  1649. #define d0 word0(d)
  1650. #define d1 word1(d)
  1651. #endif
  1652. #ifdef Pack_32
  1653. b = Balloc(1);
  1654. #else
  1655. b = Balloc(2);
  1656. #endif
  1657. x = b->x;
  1658. z = d0 & Frac_mask;
  1659. d0 &= 0x7fffffff; /* clear sign bit, which we ignore */
  1660. #ifdef Sudden_Underflow
  1661. de = (int)(d0 >> Exp_shift);
  1662. #ifndef IBM
  1663. z |= Exp_msk11;
  1664. #endif
  1665. #else
  1666. if ((de = (int)(d0 >> Exp_shift)) != 0)
  1667. z |= Exp_msk1;
  1668. #endif
  1669. #ifdef Pack_32
  1670. if ((y = d1) != 0) {
  1671. if ((k = lo0bits(&y)) != 0) {
  1672. x[0] = y | z << (32 - k);
  1673. z >>= k;
  1674. }
  1675. else
  1676. x[0] = y;
  1677. #ifndef Sudden_Underflow
  1678. i =
  1679. #endif
  1680. b->wds = (x[1] = z) ? 2 : 1;
  1681. }
  1682. else {
  1683. #ifdef DEBUG
  1684. if (!z)
  1685. Bug("Zero passed to d2b");
  1686. #endif
  1687. k = lo0bits(&z);
  1688. x[0] = z;
  1689. #ifndef Sudden_Underflow
  1690. i =
  1691. #endif
  1692. b->wds = 1;
  1693. k += 32;
  1694. }
  1695. #else
  1696. if (y = d1) {
  1697. if (k = lo0bits(&y))
  1698. if (k >= 16) {
  1699. x[0] = y | z << 32 - k & 0xffff;
  1700. x[1] = z >> k - 16 & 0xffff;
  1701. x[2] = z >> k;
  1702. i = 2;
  1703. }
  1704. else {
  1705. x[0] = y & 0xffff;
  1706. x[1] = y >> 16 | z << 16 - k & 0xffff;
  1707. x[2] = z >> k & 0xffff;
  1708. x[3] = z >> k+16;
  1709. i = 3;
  1710. }
  1711. else {
  1712. x[0] = y & 0xffff;
  1713. x[1] = y >> 16;
  1714. x[2] = z & 0xffff;
  1715. x[3] = z >> 16;
  1716. i = 3;
  1717. }
  1718. }
  1719. else {
  1720. #ifdef DEBUG
  1721. if (!z)
  1722. Bug("Zero passed to d2b");
  1723. #endif
  1724. k = lo0bits(&z);
  1725. if (k >= 16) {
  1726. x[0] = z;
  1727. i = 0;
  1728. }
  1729. else {
  1730. x[0] = z & 0xffff;
  1731. x[1] = z >> 16;
  1732. i = 1;
  1733. }
  1734. k += 32;
  1735. }
  1736. while (!x[i])
  1737. --i;
  1738. b->wds = i + 1;
  1739. #endif
  1740. #ifndef Sudden_Underflow
  1741. if (de) {
  1742. #endif
  1743. #ifdef IBM
  1744. *e = (de - Bias - (P-1) << 2) + k;
  1745. *bits = 4*P + 8 - k - hi0bits(word0(d) & Frac_mask);
  1746. #else
  1747. *e = de - Bias - (P-1) + k;
  1748. *bits = P - k;
  1749. #endif
  1750. #ifndef Sudden_Underflow
  1751. }
  1752. else {
  1753. *e = de - Bias - (P-1) + 1 + k;
  1754. #ifdef Pack_32
  1755. *bits = 32*i - hi0bits(x[i-1]);
  1756. #else
  1757. *bits = (i+2)*16 - hi0bits(x[i]);
  1758. #endif
  1759. }
  1760. #endif
  1761. return b;
  1762. }
  1763. #undef d0
  1764. #undef d1
  1765. static double
  1766. ratio(Bigint *a, Bigint *b)
  1767. {
  1768. double_u da, db;
  1769. int k, ka, kb;
  1770. dval(da) = b2d(a, &ka);
  1771. dval(db) = b2d(b, &kb);
  1772. #ifdef Pack_32
  1773. k = ka - kb + 32*(a->wds - b->wds);
  1774. #else
  1775. k = ka - kb + 16*(a->wds - b->wds);
  1776. #endif
  1777. #ifdef IBM
  1778. if (k > 0) {
  1779. word0(da) += (k >> 2)*Exp_msk1;
  1780. if (k &= 3)
  1781. dval(da) *= 1 << k;
  1782. }
  1783. else {
  1784. k = -k;
  1785. word0(db) += (k >> 2)*Exp_msk1;
  1786. if (k &= 3)
  1787. dval(db) *= 1 << k;
  1788. }
  1789. #else
  1790. if (k > 0)
  1791. word0(da) += k*Exp_msk1;
  1792. else {
  1793. k = -k;
  1794. word0(db) += k*Exp_msk1;
  1795. }
  1796. #endif
  1797. return dval(da) / dval(db);
  1798. }
  1799. static const double
  1800. tens[] = {
  1801. 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9,
  1802. 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19,
  1803. 1e20, 1e21, 1e22
  1804. #ifdef VAX
  1805. , 1e23, 1e24
  1806. #endif
  1807. };
  1808. static const double
  1809. #ifdef IEEE_Arith
  1810. bigtens[] = { 1e16, 1e32, 1e64, 1e128, 1e256 };
  1811. static const double tinytens[] = { 1e-16, 1e-32, 1e-64, 1e-128,
  1812. #ifdef Avoid_Underflow
  1813. 9007199254740992.*9007199254740992.e-256
  1814. /* = 2^106 * 1e-53 */
  1815. #else
  1816. 1e-256
  1817. #endif
  1818. };
  1819. /* The factor of 2^53 in tinytens[4] helps us avoid setting the underflow */
  1820. /* flag unnecessarily. It leads to a song and dance at the end of strtod. */
  1821. #define Scale_Bit 0x10
  1822. #define n_bigtens 5
  1823. #else
  1824. #ifdef IBM
  1825. bigtens[] = { 1e16, 1e32, 1e64 };
  1826. static const double tinytens[] = { 1e-16, 1e-32, 1e-64 };
  1827. #define n_bigtens 3
  1828. #else
  1829. bigtens[] = { 1e16, 1e32 };
  1830. static const double tinytens[] = { 1e-16, 1e-32 };
  1831. #define n_bigtens 2
  1832. #endif
  1833. #endif
  1834. #ifndef IEEE_Arith
  1835. #undef INFNAN_CHECK
  1836. #endif
  1837. #ifdef INFNAN_CHECK
  1838. #ifndef NAN_WORD0
  1839. #define NAN_WORD0 0x7ff80000
  1840. #endif
  1841. #ifndef NAN_WORD1
  1842. #define NAN_WORD1 0
  1843. #endif
  1844. static int
  1845. match(const char **sp, char *t)
  1846. {
  1847. int c, d;
  1848. const char *s = *sp;
  1849. while (d = *t++) {
  1850. if ((c = *++s) >= 'A' && c <= 'Z')
  1851. c += 'a' - 'A';
  1852. if (c != d)
  1853. return 0;
  1854. }
  1855. *sp = s + 1;
  1856. return 1;
  1857. }
  1858. #ifndef No_Hex_NaN
  1859. static void
  1860. hexnan(double *rvp, const char **sp)
  1861. {
  1862. ULong c, x[2];
  1863. const char *s;
  1864. int havedig, udx0, xshift;
  1865. x[0] = x[1] = 0;
  1866. havedig = xshift = 0;
  1867. udx0 = 1;
  1868. s = *sp;
  1869. while (c = *(const unsigned char*)++s) {
  1870. if (c >= '0' && c <= '9')
  1871. c -= '0';
  1872. else if (c >= 'a' && c <= 'f')
  1873. c += 10 - 'a';
  1874. else if (c >= 'A' && c <= 'F')
  1875. c += 10 - 'A';
  1876. else if (c <= ' ') {
  1877. if (udx0 && havedig) {
  1878. udx0 = 0;
  1879. xshift = 1;
  1880. }
  1881. continue;
  1882. }
  1883. else if (/*(*/ c == ')' && havedig) {
  1884. *sp = s + 1;
  1885. break;
  1886. }
  1887. else
  1888. return; /* invalid form: don't change *sp */
  1889. havedig = 1;
  1890. if (xshift) {
  1891. xshift = 0;
  1892. x[0] = x[1];
  1893. x[1] = 0;
  1894. }
  1895. if (udx0)
  1896. x[0] = (x[0] << 4) | (x[1] >> 28);
  1897. x[1] = (x[1] << 4) | c;
  1898. }
  1899. if ((x[0] &= 0xfffff) || x[1]) {
  1900. word0(*rvp) = Exp_mask | x[0];
  1901. word1(*rvp) = x[1];
  1902. }
  1903. }
  1904. #endif /*No_Hex_NaN*/
  1905. #endif /* INFNAN_CHECK */
  1906. double
  1907. ruby_strtod(const char *s00, char **se)
  1908. {
  1909. #ifdef Avoid_Underflow
  1910. int scale;
  1911. #endif
  1912. int bb2, bb5, bbe, bd2, bd5, bbbits, bs2, c, dsign,
  1913. e, e1, esign, i, j, k, nd, nd0, nf, nz, nz0, sign;
  1914. const char *s, *s0, *s1;
  1915. double aadj, adj;
  1916. double_u aadj1, rv, rv0;
  1917. Long L;
  1918. ULong y, z;
  1919. Bigint *bb, *bb1, *bd, *bd0, *bs, *delta;
  1920. #ifdef SET_INEXACT
  1921. int inexact, oldinexact;
  1922. #endif
  1923. #ifdef Honor_FLT_ROUNDS
  1924. int rounding;
  1925. #endif
  1926. #ifdef USE_LOCALE
  1927. const char *s2;
  1928. #endif
  1929. errno = 0;
  1930. sign = nz0 = nz = 0;
  1931. dval(rv) = 0.;
  1932. for (s = s00;;s++)
  1933. switch (*s) {
  1934. case '-':
  1935. sign = 1;
  1936. /* no break */
  1937. case '+':
  1938. if (*++s)
  1939. goto break2;
  1940. /* no break */
  1941. case 0:
  1942. goto ret0;
  1943. case '\t':
  1944. case '\n':
  1945. case '\v':
  1946. case '\f':
  1947. case '\r':
  1948. case ' ':
  1949. continue;
  1950. default:
  1951. goto break2;
  1952. }
  1953. break2:
  1954. if (*s == '0') {
  1955. if (s[1] == 'x' || s[1] == 'X') {
  1956. static const char hexdigit[] = "0123456789abcdef0123456789ABCDEF";
  1957. s0 = ++s;
  1958. adj = 0;
  1959. aadj = -1;
  1960. if (!*++s || !(s1 = strchr(hexdigit, *s))) goto ret0;
  1961. do {
  1962. adj *= 16;
  1963. adj += (s1 - hexdigit) & 15;
  1964. } while (*++s && (s1 = strchr(hexdigit, *s)));
  1965. if (*s == '.') {
  1966. aadj = 1.;
  1967. while (*++s && (s1 = strchr(hexdigit, *s))) {
  1968. aadj /= 16;
  1969. adj += aadj * ((s1 - hexdigit) & 15);
  1970. }
  1971. }
  1972. if (*s == 'P' || *s == 'p') {
  1973. dsign = 0x2C - *++s; /* +: 2B, -: 2D */
  1974. if (abs(dsign) == 1) s++;
  1975. else dsign = 1;
  1976. nd = 0;
  1977. c = *s;
  1978. if (c < '0' || '9' < c) goto ret0;
  1979. do {
  1980. nd *= 10;
  1981. nd += c;
  1982. nd -= '0';
  1983. c = *++s;
  1984. /* Float("0x0."+("0"*267)+"1fp2095") */
  1985. if (abs(nd) > 2095) {
  1986. while ('0' <= c && c <= '9') c = *++s;
  1987. break;
  1988. }
  1989. } while ('0' <= c && c <= '9');
  1990. dval(rv) = ldexp(adj, nd * dsign);
  1991. }
  1992. else {
  1993. if (aadj != -1) goto ret0;
  1994. dval(rv) = adj;
  1995. }
  1996. goto ret;
  1997. }
  1998. nz0 = 1;
  1999. while (*++s == '0') ;
  2000. if (!*s)
  2001. goto ret;
  2002. }
  2003. s0 = s;
  2004. y = z = 0;
  2005. for (nd = nf = 0; (c = *s) >= '0' && c <= '9'; nd++, s++)
  2006. if (nd < 9)
  2007. y = 10*y + c - '0';
  2008. else if (nd < 16)
  2009. z = 10*z + c - '0';
  2010. nd0 = nd;
  2011. #ifdef USE_LOCALE
  2012. s1 = localeconv()->decimal_point;
  2013. if (c == *s1) {
  2014. c = '.';
  2015. if (*++s1) {
  2016. s2 = s;
  2017. for (;;) {
  2018. if (*++s2 != *s1) {
  2019. c = 0;
  2020. break;
  2021. }
  2022. if (!*++s1) {
  2023. s = s2;
  2024. break;
  2025. }
  2026. }
  2027. }
  2028. }
  2029. #endif
  2030. if (c == '.') {
  2031. if (!ISDIGIT(s[1]))
  2032. goto dig_done;
  2033. c = *++s;
  2034. if (!nd) {
  2035. for (; c == '0'; c = *++s)
  2036. nz++;
  2037. if (c > '0' && c <= '9') {
  2038. s0 = s;
  2039. nf += nz;
  2040. nz = 0;
  2041. goto have_dig;
  2042. }
  2043. goto dig_done;
  2044. }
  2045. for (; c >= '0' && c <= '9'; c = *++s) {
  2046. have_dig:
  2047. nz++;
  2048. if (c -= '0') {
  2049. nf += nz;
  2050. for (i = 1; i < nz; i++)
  2051. if (nd++ < 9)
  2052. y *= 10;
  2053. else if (nd <= DBL_DIG + 1)
  2054. z *= 10;
  2055. if (nd++ < 9)
  2056. y = 10*y + c;
  2057. else if (nd <= DBL_DIG + 1)
  2058. z = 10*z + c;
  2059. nz = 0;
  2060. }
  2061. }
  2062. }
  2063. dig_done:
  2064. e = 0;
  2065. if (c == 'e' || c == 'E') {
  2066. if (!nd && !nz && !nz0) {
  2067. goto ret0;
  2068. }
  2069. s00 = s;
  2070. esign = 0;
  2071. switch (c = *++s) {
  2072. case '-':
  2073. esign = 1;
  2074. case '+':
  2075. c = *++s;
  2076. }
  2077. if (c >= '0' && c <= '9') {
  2078. while (c == '0')
  2079. c = *++s;
  2080. if (c > '0' && c <= '9') {
  2081. L = c - '0';
  2082. s1 = s;
  2083. while ((c = *++s) >= '0' && c <= '9')
  2084. L = 10*L + c - '0';
  2085. if (s - s1 > 8 || L > 19999)
  2086. /* Avoid confusion from exponents
  2087. * so large that e might overflow.
  2088. */
  2089. e = 19999; /* safe for 16 bit ints */
  2090. else
  2091. e = (int)L;
  2092. if (esign)
  2093. e = -e;
  2094. }
  2095. else
  2096. e = 0;
  2097. }
  2098. else
  2099. s = s00;
  2100. }
  2101. if (!nd) {
  2102. if (!nz && !nz0) {
  2103. #ifdef INFNAN_CHECK
  2104. /* Check for Nan and Infinity */
  2105. switch (c) {
  2106. case 'i':
  2107. case 'I':
  2108. if (match(&s,"nf")) {
  2109. --s;
  2110. if (!match(&s,"inity"))
  2111. ++s;
  2112. word0(rv) = 0x7ff00000;
  2113. word1(rv) = 0;
  2114. goto ret;
  2115. }
  2116. break;
  2117. case 'n':
  2118. case 'N':
  2119. if (match(&s, "an")) {
  2120. word0(rv) = NAN_WORD0;
  2121. word1(rv) = NAN_WORD1;
  2122. #ifndef No_Hex_NaN
  2123. if (*s == '(') /*)*/
  2124. hexnan(&rv, &s);
  2125. #endif
  2126. goto ret;
  2127. }
  2128. }
  2129. #endif /* INFNAN_CHECK */
  2130. ret0:
  2131. s = s00;
  2132. sign = 0;
  2133. }
  2134. goto ret;
  2135. }
  2136. e1 = e -= nf;
  2137. /* Now we have nd0 digits, starting at s0, followed by a
  2138. * decimal point, followed by nd-nd0 digits. The number we're
  2139. * after is the integer represented by those digits times
  2140. * 10**e */
  2141. if (!nd0)
  2142. nd0 = nd;
  2143. k = nd < DBL_DIG + 1 ? nd : DBL_DIG + 1;
  2144. dval(rv) = y;
  2145. if (k > 9) {
  2146. #ifdef SET_INEXACT
  2147. if (k > DBL_DIG)
  2148. oldinexact = get_inexact();
  2149. #endif
  2150. dval(rv) = tens[k - 9] * dval(rv) + z;
  2151. }
  2152. bd0 = bb = bd = bs = delta = 0;
  2153. if (nd <= DBL_DIG
  2154. #ifndef RND_PRODQUOT
  2155. #ifndef Honor_FLT_ROUNDS
  2156. && Flt_Rounds == 1
  2157. #endif
  2158. #endif
  2159. ) {
  2160. if (!e)
  2161. goto ret;
  2162. if (e > 0) {
  2163. if (e <= Ten_pmax) {
  2164. #ifdef VAX
  2165. goto vax_ovfl_check;
  2166. #else
  2167. #ifdef Honor_FLT_ROUNDS
  2168. /* round correctly FLT_ROUNDS = 2 or 3 */
  2169. if (sign) {
  2170. dval(rv) = -dval(rv);
  2171. sign = 0;
  2172. }
  2173. #endif
  2174. /* rv = */ rounded_product(dval(rv), tens[e]);
  2175. goto ret;
  2176. #endif
  2177. }
  2178. i = DBL_DIG - nd;
  2179. if (e <= Ten_pmax + i) {
  2180. /* A fancier test would sometimes let us do
  2181. * this for larger i values.
  2182. */
  2183. #ifdef Honor_FLT_ROUNDS
  2184. /* round correctly FLT_ROUNDS = 2 or 3 */
  2185. if (sign) {
  2186. dval(rv) = -dval(rv);
  2187. sign = 0;
  2188. }
  2189. #endif
  2190. e -= i;
  2191. dval(rv) *= tens[i];
  2192. #ifdef VAX
  2193. /* VAX exponent range is so narrow we must
  2194. * worry about overflow here...
  2195. */
  2196. vax_ovfl_check:
  2197. word0(rv) -= P*Exp_msk1;
  2198. /* rv = */ rounded_product(dval(rv), tens[e]);
  2199. if ((word0(rv) & Exp_mask)
  2200. > Exp_msk1*(DBL_MAX_EXP+Bias-1-P))
  2201. goto ovfl;
  2202. word0(rv) += P*Exp_msk1;
  2203. #else
  2204. /* rv = */ rounded_product(dval(rv), tens[e]);
  2205. #endif
  2206. goto ret;
  2207. }
  2208. }
  2209. #ifndef Inaccurate_Divide
  2210. else if (e >= -Ten_pmax) {
  2211. #ifdef Honor_FLT_ROUNDS
  2212. /* round correctly FLT_ROUNDS = 2 or 3 */
  2213. if (sign) {
  2214. dval(rv) = -dval(rv);
  2215. sign = 0;
  2216. }
  2217. #endif
  2218. /* rv = */ rounded_quotient(dval(rv), tens[-e]);
  2219. goto ret;
  2220. }
  2221. #endif
  2222. }
  2223. e1 += nd - k;
  2224. #ifdef IEEE_Arith
  2225. #ifdef SET_INEXACT
  2226. inexact = 1;
  2227. if (k <= DBL_DIG)
  2228. oldinexact = get_inexact();
  2229. #endif
  2230. #ifdef Avoid_Underflow
  2231. scale = 0;
  2232. #endif
  2233. #ifdef Honor_FLT_ROUNDS
  2234. if ((rounding = Flt_Rounds) >= 2) {
  2235. if (sign)
  2236. rounding = rounding == 2 ? 0 : 2;
  2237. else
  2238. if (rounding != 2)
  2239. rounding = 0;
  2240. }
  2241. #endif
  2242. #endif /*IEEE_Arith*/
  2243. /* Get starting approximation = rv * 10**e1 */
  2244. if (e1 > 0) {
  2245. if ((i = e1 & 15) != 0)
  2246. dval(rv) *= tens[i];
  2247. if (e1 &= ~15) {
  2248. if (e1 > DBL_MAX_10_EXP) {
  2249. ovfl:
  2250. #ifndef NO_ERRNO
  2251. errno = ERANGE;
  2252. #endif
  2253. /* Can't trust HUGE_VAL */
  2254. #ifdef IEEE_Arith
  2255. #ifdef Honor_FLT_ROUNDS
  2256. switch (rounding) {
  2257. case 0: /* toward 0 */
  2258. case 3: /* toward -infinity */
  2259. word0(rv) = Big0;
  2260. word1(rv) = Big1;
  2261. break;
  2262. default:
  2263. word0(rv) = Exp_mask;
  2264. word1(rv) = 0;
  2265. }
  2266. #else /*Honor_FLT_ROUNDS*/
  2267. word0(rv) = Exp_mask;
  2268. word1(rv) = 0;
  2269. #endif /*Honor_FLT_ROUNDS*/
  2270. #ifdef SET_INEXACT
  2271. /* set overflow bit */
  2272. dval(rv0) = 1e300;
  2273. dval(rv0) *= dval(rv0);
  2274. #endif
  2275. #else /*IEEE_Arith*/
  2276. word0(rv) = Big0;
  2277. word1(rv) = Big1;
  2278. #endif /*IEEE_Arith*/
  2279. if (bd0)
  2280. goto retfree;
  2281. goto ret;
  2282. }
  2283. e1 >>= 4;
  2284. for (j = 0; e1 > 1; j++, e1 >>= 1)
  2285. if (e1 & 1)
  2286. dval(rv) *= bigtens[j];
  2287. /* The last multiplication could overflow. */
  2288. word0(rv) -= P*Exp_msk1;
  2289. dval(rv) *= bigtens[j];
  2290. if ((z = word0(rv) & Exp_mask)
  2291. > Exp_msk1*(DBL_MAX_EXP+Bias-P))
  2292. goto ovfl;
  2293. if (z > Exp_msk1*(DBL_MAX_EXP+Bias-1-P)) {
  2294. /* set to largest number */
  2295. /* (Can't trust DBL_MAX) */
  2296. word0(rv) = Big0;
  2297. word1(rv) = Big1;
  2298. }
  2299. else
  2300. word0(rv) += P*Exp_msk1;
  2301. }
  2302. }
  2303. else if (e1 < 0) {
  2304. e1 = -e1;
  2305. if ((i = e1 & 15) != 0)
  2306. dval(rv) /= tens[i];
  2307. if (e1 >>= 4) {
  2308. if (e1 >= 1 << n_bigtens)
  2309. goto undfl;
  2310. #ifdef Avoid_Underflow
  2311. if (e1 & Scale_Bit)
  2312. scale = 2*P;
  2313. for (j = 0; e1 > 0; j++, e1 >>= 1)
  2314. if (e1 & 1)
  2315. dval(rv) *= tinytens[j];
  2316. if (scale && (j = 2*P + 1 - ((word0(rv) & Exp_mask)
  2317. >> Exp_shift)) > 0) {
  2318. /* scaled rv is denormal; zap j low bits */
  2319. if (j >= 32) {
  2320. word1(rv) = 0;
  2321. if (j >= 53)
  2322. word0(rv) = (P+2)*Exp_msk1;
  2323. else
  2324. word0(rv) &= 0xffffffff << (j-32);
  2325. }
  2326. else
  2327. word1(rv) &= 0xffffffff << j;
  2328. }
  2329. #else
  2330. for (j = 0; e1 > 1; j++, e1 >>= 1)
  2331. if (e1 & 1)
  2332. dval(rv) *= tinytens[j];
  2333. /* The last multiplication could underflow. */
  2334. dval(rv0) = dval(rv);
  2335. dval(rv) *= tinytens[j];
  2336. if (!dval(rv)) {
  2337. dval(rv) = 2.*dval(rv0);
  2338. dval(rv) *= tinytens[j];
  2339. #endif
  2340. if (!dval(rv)) {
  2341. undfl:
  2342. dval(rv) = 0.;
  2343. #ifndef NO_ERRNO
  2344. errno = ERANGE;
  2345. #endif
  2346. if (bd0)
  2347. goto retfree;
  2348. goto ret;
  2349. }
  2350. #ifndef Avoid_Underflow
  2351. word0(rv) = Tiny0;
  2352. word1(rv) = Tiny1;
  2353. /* The refinement below will clean
  2354. * this approximation up.
  2355. */
  2356. }
  2357. #endif
  2358. }
  2359. }
  2360. /* Now the hard part -- adjusting rv to the correct value.*/
  2361. /* Put digits into bd: true value = bd * 10^e */
  2362. bd0 = s2b(s0, nd0, nd, y);
  2363. for (;;) {
  2364. bd = Balloc(bd0->k);
  2365. Bcopy(bd, bd0);
  2366. bb = d2b(dval(rv), &bbe, &bbbits); /* rv = bb * 2^bbe */
  2367. bs = i2b(1);
  2368. if (e >= 0) {
  2369. bb2 = bb5 = 0;
  2370. bd2 = bd5 = e;
  2371. }
  2372. else {
  2373. bb2 = bb5 = -e;
  2374. bd2 = bd5 = 0;
  2375. }
  2376. if (bbe >= 0)
  2377. bb2 += bbe;
  2378. else
  2379. bd2 -= bbe;
  2380. bs2 = bb2;
  2381. #ifdef Honor_FLT_ROUNDS
  2382. if (rounding != 1)
  2383. bs2++;
  2384. #endif
  2385. #ifdef Avoid_Underflow
  2386. j = bbe - scale;
  2387. i = j + bbbits - 1; /* logb(rv) */
  2388. if (i < Emin) /* denormal */
  2389. j += P - Emin;
  2390. else
  2391. j = P + 1 - bbbits;
  2392. #else /*Avoid_Underflow*/
  2393. #ifdef Sudden_Underflow
  2394. #ifdef IBM
  2395. j = 1 + 4*P - 3 - bbbits + ((bbe + bbbits - 1) & 3);
  2396. #else
  2397. j = P + 1 - bbbits;
  2398. #endif
  2399. #else /*Sudden_Underflow*/
  2400. j = bbe;
  2401. i = j + bbbits - 1; /* logb(rv) */
  2402. if (i < Emin) /* denormal */
  2403. j += P - Emin;
  2404. else
  2405. j = P + 1 - bbbits;
  2406. #endif /*Sudden_Underflow*/
  2407. #endif /*Avoid_Underflow*/
  2408. bb2 += j;
  2409. bd2 += j;
  2410. #ifdef Avoid_Underflow
  2411. bd2 += scale;
  2412. #endif
  2413. i = bb2 < bd2 ? bb2 : bd2;
  2414. if (i > bs2)
  2415. i = bs2;
  2416. if (i > 0) {
  2417. bb2 -= i;
  2418. bd2 -= i;
  2419. bs2 -= i;
  2420. }
  2421. if (bb5 > 0) {
  2422. bs = pow5mult(bs, bb5);
  2423. bb1 = mult(bs, bb);
  2424. Bfree(bb);
  2425. bb = bb1;
  2426. }
  2427. if (bb2 > 0)
  2428. bb = lshift(bb, bb2);
  2429. if (bd5 > 0)
  2430. bd = pow5mult(bd, bd5);
  2431. if (bd2 > 0)
  2432. bd = lshift(bd, bd2);
  2433. if (bs2 > 0)
  2434. bs = lshift(bs, bs2);
  2435. delta = diff(bb, bd);
  2436. dsign = delta->sign;
  2437. delta->sign = 0;
  2438. i = cmp(delta, bs);
  2439. #ifdef Honor_FLT_ROUNDS
  2440. if (rounding != 1) {
  2441. if (i < 0) {
  2442. /* Error is less than an ulp */
  2443. if (!delta->x[0] && delta->wds <= 1) {
  2444. /* exact */
  2445. #ifdef SET_INEXACT
  2446. inexact = 0;
  2447. #endif
  2448. break;
  2449. }
  2450. if (rounding) {
  2451. if (dsign) {
  2452. adj = 1.;
  2453. goto apply_adj;
  2454. }
  2455. }
  2456. else if (!dsign) {
  2457. adj = -1.;
  2458. if (!word1(rv)
  2459. && !(word0(rv) & Frac_mask)) {
  2460. y = word0(rv) & Exp_mask;
  2461. #ifdef Avoid_Underflow
  2462. if (!scale || y > 2*P*Exp_msk1)
  2463. #else
  2464. if (y)
  2465. #endif
  2466. {
  2467. delta = lshift(delta,Log2P);
  2468. if (cmp(delta, bs) <= 0)
  2469. adj = -0.5;
  2470. }
  2471. }
  2472. apply_adj:
  2473. #ifdef Avoid_Underflow
  2474. if (scale && (y = word0(rv) & Exp_mask)
  2475. <= 2*P*Exp_msk1)
  2476. word0(adj) += (2*P+1)*Exp_msk1 - y;
  2477. #else
  2478. #ifdef Sudden_Underflow
  2479. if ((word0(rv) & Exp_mask) <=
  2480. P*Exp_msk1) {
  2481. word0(rv) += P*Exp_msk1;
  2482. dval(rv) += adj*ulp(dval(rv));
  2483. word0(rv) -= P*Exp_msk1;
  2484. }
  2485. else
  2486. #endif /*Sudden_Underflow*/
  2487. #endif /*Avoid_Underflow*/
  2488. dval(rv) += adj*ulp(dval(rv));
  2489. }
  2490. break;
  2491. }
  2492. adj = ratio(delta, bs);
  2493. if (adj < 1.)
  2494. adj = 1.;
  2495. if (adj <= 0x7ffffffe) {
  2496. /* adj = rounding ? ceil(adj) : floor(adj); */
  2497. y = adj;
  2498. if (y != adj) {
  2499. if (!((rounding>>1) ^ dsign))
  2500. y++;
  2501. adj = y;
  2502. }
  2503. }
  2504. #ifdef Avoid_Underflow
  2505. if (scale && (y = word0(rv) & Exp_mask) <= 2*P*Exp_msk1)
  2506. word0(adj) += (2*P+1)*Exp_msk1 - y;
  2507. #else
  2508. #ifdef Sudden_Underflow
  2509. if ((word0(rv) & Exp_mask) <= P*Exp_msk1) {
  2510. word0(rv) += P*Exp_msk1;
  2511. adj *= ulp(dval(rv));
  2512. if (dsign)
  2513. dval(rv) += adj;
  2514. else
  2515. dval(rv) -= adj;
  2516. word0(rv) -= P*Exp_msk1;
  2517. goto cont;
  2518. }
  2519. #endif /*Sudden_Underflow*/
  2520. #endif /*Avoid_Underflow*/
  2521. adj *= ulp(dval(rv));
  2522. if (dsign)
  2523. dval(rv) += adj;
  2524. else
  2525. dval(rv) -= adj;
  2526. goto cont;
  2527. }
  2528. #endif /*Honor_FLT_ROUNDS*/
  2529. if (i < 0) {
  2530. /* Error is less than half an ulp -- check for
  2531. * special case of mantissa a power of two.
  2532. */
  2533. if (dsign || word1(rv) || word0(rv) & Bndry_mask
  2534. #ifdef IEEE_Arith
  2535. #ifdef Avoid_Underflow
  2536. || (word0(rv) & Exp_mask) <= (2*P+1)*Exp_msk1
  2537. #else
  2538. || (word0(rv) & Exp_mask) <= Exp_msk1
  2539. #endif
  2540. #endif
  2541. ) {
  2542. #ifdef SET_INEXACT
  2543. if (!delta->x[0] && delta->wds <= 1)
  2544. inexact = 0;
  2545. #endif
  2546. break;
  2547. }
  2548. if (!delta->x[0] && delta->wds <= 1) {
  2549. /* exact result */
  2550. #ifdef SET_INEXACT
  2551. inexact = 0;
  2552. #endif
  2553. break;
  2554. }
  2555. delta = lshift(delta,Log2P);
  2556. if (cmp(delta, bs) > 0)
  2557. goto drop_down;
  2558. break;
  2559. }
  2560. if (i == 0) {
  2561. /* exactly half-way between */
  2562. if (dsign) {
  2563. if ((word0(rv) & Bndry_mask1) == Bndry_mask1
  2564. && word1(rv) == (
  2565. #ifdef Avoid_Underflow
  2566. (scale && (y = word0(rv) & Exp_mask) <= 2*P*Exp_msk1)
  2567. ? (0xffffffff & (0xffffffff << (2*P+1-(y>>Exp_shift)))) :
  2568. #endif
  2569. 0xffffffff)) {
  2570. /*boundary case -- increment exponent*/
  2571. word0(rv) = (word0(rv) & Exp_mask)
  2572. + Exp_msk1
  2573. #ifdef IBM
  2574. | Exp_msk1 >> 4
  2575. #endif
  2576. ;
  2577. word1(rv) = 0;
  2578. #ifdef Avoid_Underflow
  2579. dsign = 0;
  2580. #endif
  2581. break;
  2582. }
  2583. }
  2584. else if (!(word0(rv) & Bndry_mask) && !word1(rv)) {
  2585. drop_down:
  2586. /* boundary case -- decrement exponent */
  2587. #ifdef Sudden_Underflow /*{{*/
  2588. L = word0(rv) & Exp_mask;
  2589. #ifdef IBM
  2590. if (L < Exp_msk1)
  2591. #else
  2592. #ifdef Avoid_Underflow
  2593. if (L <= (scale ? (2*P+1)*Exp_msk1 : Exp_msk1))
  2594. #else
  2595. if (L <= Exp_msk1)
  2596. #endif /*Avoid_Underflow*/
  2597. #endif /*IBM*/
  2598. goto undfl;
  2599. L -= Exp_msk1;
  2600. #else /*Sudden_Underflow}{*/
  2601. #ifdef Avoid_Underflow
  2602. if (scale) {
  2603. L = word0(rv) & Exp_mask;
  2604. if (L <= (2*P+1)*Exp_msk1) {
  2605. if (L > (P+2)*Exp_msk1)
  2606. /* round even ==> */
  2607. /* accept rv */
  2608. break;
  2609. /* rv = smallest denormal */
  2610. goto undfl;
  2611. }
  2612. }
  2613. #endif /*Avoid_Underflow*/
  2614. L = (word0(rv) & Exp_mask) - Exp_msk1;
  2615. #endif /*Sudden_Underflow}}*/
  2616. word0(rv) = L | Bndry_mask1;
  2617. word1(rv) = 0xffffffff;
  2618. #ifdef IBM
  2619. goto cont;
  2620. #else
  2621. break;
  2622. #endif
  2623. }
  2624. #ifndef ROUND_BIASED
  2625. if (!(word1(rv) & LSB))
  2626. break;
  2627. #endif
  2628. if (dsign)
  2629. dval(rv) += ulp(dval(rv));
  2630. #ifndef ROUND_BIASED
  2631. else {
  2632. dval(rv) -= ulp(dval(rv));
  2633. #ifndef Sudden_Underflow
  2634. if (!dval(rv))
  2635. goto undfl;
  2636. #endif
  2637. }
  2638. #ifdef Avoid_Underflow
  2639. dsign = 1 - dsign;
  2640. #endif
  2641. #endif
  2642. break;
  2643. }
  2644. if ((aadj = ratio(delta, bs)) <= 2.) {
  2645. if (dsign)
  2646. aadj = dval(aadj1) = 1.;
  2647. else if (word1(rv) || word0(rv) & Bndry_mask) {
  2648. #ifndef Sudden_Underflow
  2649. if (word1(rv) == Tiny1 && !word0(rv))
  2650. goto undfl;
  2651. #endif
  2652. aadj = 1.;
  2653. dval(aadj1) = -1.;
  2654. }
  2655. else {
  2656. /* special case -- power of FLT_RADIX to be */
  2657. /* rounded down... */
  2658. if (aadj < 2./FLT_RADIX)
  2659. aadj = 1./FLT_RADIX;
  2660. else
  2661. aadj *= 0.5;
  2662. dval(aadj1) = -aadj;
  2663. }
  2664. }
  2665. else {
  2666. aadj *= 0.5;
  2667. dval(aadj1) = dsign ? aadj : -aadj;
  2668. #ifdef Check_FLT_ROUNDS
  2669. switch (Rounding) {
  2670. case 2: /* towards +infinity */
  2671. dval(aadj1) -= 0.5;
  2672. break;
  2673. case 0: /* towards 0 */
  2674. case 3: /* towards -infinity */
  2675. dval(aadj1) += 0.5;
  2676. }
  2677. #else
  2678. if (Flt_Rounds == 0)
  2679. dval(aadj1) += 0.5;
  2680. #endif /*Check_FLT_ROUNDS*/
  2681. }
  2682. y = word0(rv) & Exp_mask;
  2683. /* Check for overflow */
  2684. if (y == Exp_msk1*(DBL_MAX_EXP+Bias-1)) {
  2685. dval(rv0) = dval(rv);
  2686. word0(rv) -= P*Exp_msk1;
  2687. adj = dval(aadj1) * ulp(dval(rv));
  2688. dval(rv) += adj;
  2689. if ((word0(rv) & Exp_mask) >=
  2690. Exp_msk1*(DBL_MAX_EXP+Bias-P)) {
  2691. if (word0(rv0) == Big0 && word1(rv0) == Big1)
  2692. goto ovfl;
  2693. word0(rv) = Big0;
  2694. word1(rv) = Big1;
  2695. goto cont;
  2696. }
  2697. else
  2698. word0(rv) += P*Exp_msk1;
  2699. }
  2700. else {
  2701. #ifdef Avoid_Underflow
  2702. if (scale && y <= 2*P*Exp_msk1) {
  2703. if (aadj <= 0x7fffffff) {
  2704. if ((z = (int)aadj) <= 0)
  2705. z = 1;
  2706. aadj = z;
  2707. dval(aadj1) = dsign ? aadj : -aadj;
  2708. }
  2709. word0(aadj1) += (2*P+1)*Exp_msk1 - y;
  2710. }
  2711. adj = dval(aadj1) * ulp(dval(rv));
  2712. dval(rv) += adj;
  2713. #else
  2714. #ifdef Sudden_Underflow
  2715. if ((word0(rv) & Exp_mask) <= P*Exp_msk1) {
  2716. dval(rv0) = dval(rv);
  2717. word0(rv) += P*Exp_msk1;
  2718. adj = dval(aadj1) * ulp(dval(rv));
  2719. dval(rv) += adj;
  2720. #ifdef IBM
  2721. if ((word0(rv) & Exp_mask) < P*Exp_msk1)
  2722. #else
  2723. if ((word0(rv) & Exp_mask) <= P*Exp_msk1)
  2724. #endif
  2725. {
  2726. if (word0(rv0) == Tiny0 && word1(rv0) == Tiny1)
  2727. goto undfl;
  2728. word0(rv) = Tiny0;
  2729. word1(rv) = Tiny1;
  2730. goto cont;
  2731. }
  2732. else
  2733. word0(rv) -= P*Exp_msk1;
  2734. }
  2735. else {
  2736. adj = dval(aadj1) * ulp(dval(rv));
  2737. dval(rv) += adj;
  2738. }
  2739. #else /*Sudden_Underflow*/
  2740. /* Compute adj so that the IEEE rounding rules will
  2741. * correctly round rv + adj in some half-way cases.
  2742. * If rv * ulp(rv) is denormalized (i.e.,
  2743. * y <= (P-1)*Exp_msk1), we must adjust aadj to avoid
  2744. * trouble from bits lost to denormalization;
  2745. * example: 1.2e-307 .
  2746. */
  2747. if (y <= (P-1)*Exp_msk1 && aadj > 1.) {
  2748. dval(aadj1) = (double)(int)(aadj + 0.5);
  2749. if (!dsign)
  2750. dval(aadj1) = -dval(aadj1);
  2751. }
  2752. adj = dval(aadj1) * ulp(dval(rv));
  2753. dval(rv) += adj;
  2754. #endif /*Sudden_Underflow*/
  2755. #endif /*Avoid_Underflow*/
  2756. }
  2757. z = word0(rv) & Exp_mask;
  2758. #ifndef SET_INEXACT
  2759. #ifdef Avoid_Underflow
  2760. if (!scale)
  2761. #endif
  2762. if (y == z) {
  2763. /* Can we stop now? */
  2764. L = (Long)aadj;
  2765. aadj -= L;
  2766. /* The tolerances below are conservative. */
  2767. if (dsign || word1(rv) || word0(rv) & Bndry_mask) {
  2768. if (aadj < .4999999 || aadj > .5000001)
  2769. break;
  2770. }
  2771. else if (aadj < .4999999/FLT_RADIX)
  2772. break;
  2773. }
  2774. #endif
  2775. cont:
  2776. Bfree(bb);
  2777. Bfree(bd);
  2778. Bfree(bs);
  2779. Bfree(delta);
  2780. }
  2781. #ifdef SET_INEXACT
  2782. if (inexact) {
  2783. if (!oldinexact) {
  2784. word0(rv0) = Exp_1 + (70 << Exp_shift);
  2785. word1(rv0) = 0;
  2786. dval(rv0) += 1.;
  2787. }
  2788. }
  2789. else if (!oldinexact)
  2790. clear_inexact();
  2791. #endif
  2792. #ifdef Avoid_Underflow
  2793. if (scale) {
  2794. word0(rv0) = Exp_1 - 2*P*Exp_msk1;
  2795. word1(rv0) = 0;
  2796. dval(rv) *= dval(rv0);
  2797. #ifndef NO_ERRNO
  2798. /* try to avoid the bug of testing an 8087 register value */
  2799. if (word0(rv) == 0 && word1(rv) == 0)
  2800. errno = ERANGE;
  2801. #endif
  2802. }
  2803. #endif /* Avoid_Underflow */
  2804. #ifdef SET_INEXACT
  2805. if (inexact && !(word0(rv) & Exp_mask)) {
  2806. /* set underflow bit */
  2807. dval(rv0) = 1e-300;
  2808. dval(rv0) *= dval(rv0);
  2809. }
  2810. #endif
  2811. retfree:
  2812. Bfree(bb);
  2813. Bfree(bd);
  2814. Bfree(bs);
  2815. Bfree(bd0);
  2816. Bfree(delta);
  2817. ret:
  2818. if (se)
  2819. *se = (char *)s;
  2820. return sign ? -dval(rv) : dval(rv);
  2821. }
  2822. static int
  2823. quorem(Bigint *b, Bigint *S)
  2824. {
  2825. int n;
  2826. ULong *bx, *bxe, q, *sx, *sxe;
  2827. #ifdef ULLong
  2828. ULLong borrow, carry, y, ys;
  2829. #else
  2830. ULong borrow, carry, y, ys;
  2831. #ifdef Pack_32
  2832. ULong si, z, zs;
  2833. #endif
  2834. #endif
  2835. n = S->wds;
  2836. #ifdef DEBUG
  2837. /*debug*/ if (b->wds > n)
  2838. /*debug*/ Bug("oversize b in quorem");
  2839. #endif
  2840. if (b->wds < n)
  2841. return 0;
  2842. sx = S->x;
  2843. sxe = sx + --n;
  2844. bx = b->x;
  2845. bxe = bx + n;
  2846. q = *bxe / (*sxe + 1); /* ensure q <= true quotient */
  2847. #ifdef DEBUG
  2848. /*debug*/ if (q > 9)
  2849. /*debug*/ Bug("oversized quotient in quorem");
  2850. #endif
  2851. if (q) {
  2852. borrow = 0;
  2853. carry = 0;
  2854. do {
  2855. #ifdef ULLong
  2856. ys = *sx++ * (ULLong)q + carry;
  2857. carry = ys >> 32;
  2858. y = *bx - (ys & FFFFFFFF) - borrow;
  2859. borrow = y >> 32 & (ULong)1;
  2860. *bx++ = (ULong)(y & FFFFFFFF);
  2861. #else
  2862. #ifdef Pack_32
  2863. si = *sx++;
  2864. ys = (si & 0xffff) * q + carry;
  2865. zs = (si >> 16) * q + (ys >> 16);
  2866. carry = zs >> 16;
  2867. y = (*bx & 0xffff) - (ys & 0xffff) - borrow;
  2868. borrow = (y & 0x10000) >> 16;
  2869. z = (*bx >> 16) - (zs & 0xffff) - borrow;
  2870. borrow = (z & 0x10000) >> 16;
  2871. Storeinc(bx, z, y);
  2872. #else
  2873. ys = *sx++ * q + carry;
  2874. carry = ys >> 16;
  2875. y = *bx - (ys & 0xffff) - borrow;
  2876. borrow = (y & 0x10000) >> 16;
  2877. *bx++ = y & 0xffff;
  2878. #endif
  2879. #endif
  2880. } while (sx <= sxe);
  2881. if (!*bxe) {
  2882. bx = b->x;
  2883. while (--bxe > bx && !*bxe)
  2884. --n;
  2885. b->wds = n;
  2886. }
  2887. }
  2888. if (cmp(b, S) >= 0) {
  2889. q++;
  2890. borrow = 0;
  2891. carry = 0;
  2892. bx = b->x;
  2893. sx = S->x;
  2894. do {
  2895. #ifdef ULLong
  2896. ys = *sx++ + carry;
  2897. carry = ys >> 32;
  2898. y = *bx - (ys & FFFFFFFF) - borrow;
  2899. borrow = y >> 32 & (ULong)1;
  2900. *bx++ = (ULong)(y & FFFFFFFF);
  2901. #else
  2902. #ifdef Pack_32
  2903. si = *sx++;
  2904. ys = (si & 0xffff) + carry;
  2905. zs = (si >> 16) + (ys >> 16);
  2906. carry = zs >> 16;
  2907. y = (*bx & 0xffff) - (ys & 0xffff) - borrow;
  2908. borrow = (y & 0x10000) >> 16;
  2909. z = (*bx >> 16) - (zs & 0xffff) - borrow;
  2910. borrow = (z & 0x10000) >> 16;
  2911. Storeinc(bx, z, y);
  2912. #else
  2913. ys = *sx++ + carry;
  2914. carry = ys >> 16;
  2915. y = *bx - (ys & 0xffff) - borrow;
  2916. borrow = (y & 0x10000) >> 16;
  2917. *bx++ = y & 0xffff;
  2918. #endif
  2919. #endif
  2920. } while (sx <= sxe);
  2921. bx = b->x;
  2922. bxe = bx + n;
  2923. if (!*bxe) {
  2924. while (--bxe > bx && !*bxe)
  2925. --n;
  2926. b->wds = n;
  2927. }
  2928. }
  2929. return q;
  2930. }
  2931. #ifndef MULTIPLE_THREADS
  2932. static char *dtoa_result;
  2933. #endif
  2934. #ifndef MULTIPLE_THREADS
  2935. static char *
  2936. rv_alloc(int i)
  2937. {
  2938. return dtoa_result = xmalloc(i);
  2939. }
  2940. #else
  2941. #define rv_alloc(i) xmalloc(i)
  2942. #endif
  2943. static char *
  2944. nrv_alloc(const char *s, char **rve, size_t n)
  2945. {
  2946. char *rv, *t;
  2947. t = rv = rv_alloc(n);
  2948. while ((*t = *s++) != 0) t++;
  2949. if (rve)
  2950. *rve = t;
  2951. return rv;
  2952. }
  2953. #define rv_strdup(s, rve) nrv_alloc(s, rve, strlen(s)+1)
  2954. #ifndef MULTIPLE_THREADS
  2955. /* freedtoa(s) must be used to free values s returned by dtoa
  2956. * when MULTIPLE_THREADS is #defined. It should be used in all cases,
  2957. * but for consistency with earlier versions of dtoa, it is optional
  2958. * when MULTIPLE_THREADS is not defined.
  2959. */
  2960. static void
  2961. freedtoa(char *s)
  2962. {
  2963. xfree(s);
  2964. }
  2965. #endif
  2966. static const char INFSTR[] = "Infinity";
  2967. static const char NANSTR[] = "NaN";
  2968. static const char ZEROSTR[] = "0";
  2969. /* dtoa for IEEE arithmetic (dmg): convert double to ASCII string.
  2970. *
  2971. * Inspired by "How to Print Floating-Point Numbers Accurately" by
  2972. * Guy L. Steele, Jr. and Jon L. White [Proc. ACM SIGPLAN '90, pp. 112-126].
  2973. *
  2974. * Modifications:
  2975. * 1. Rather than iterating, we use a simple numeric overestimate
  2976. * to determine k = floor(log10(d)). We scale relevant
  2977. * quantities using O(log2(k)) rather than O(k) multiplications.
  2978. * 2. For some modes > 2 (corresponding to ecvt and fcvt), we don't
  2979. * try to generate digits strictly left to right. Instead, we
  2980. * compute with fewer bits and propagate the carry if necessary
  2981. * when rounding the final digit up. This is often faster.
  2982. * 3. Under the assumption that input will be rounded nearest,
  2983. * mode 0 renders 1e23 as 1e23 rather than 9.999999999999999e22.
  2984. * That is, we allow equality in stopping tests when the
  2985. * round-nearest rule will give the same floating-point value
  2986. * as would satisfaction of the stopping test with strict
  2987. * inequality.
  2988. * 4. We remove common factors of powers of 2 from relevant
  2989. * quantities.
  2990. * 5. When converting floating-point integers less than 1e16,
  2991. * we use floating-point arithmetic rather than resorting
  2992. * to multiple-precision integers.
  2993. * 6. When asked to produce fewer than 15 digits, we first try
  2994. * to get by with floating-point arithmetic; we resort to
  2995. * multiple-precision integer arithmetic only if we cannot
  2996. * guarantee that the floating-point calculation has given
  2997. * the correctly rounded result. For k requested digits and
  2998. * "uniformly" distributed input, the probability is
  2999. * something like 10^(k-15) that we must resort to the Long
  3000. * calculation.
  3001. */
  3002. char *
  3003. ruby_dtoa(double d_, int mode, int ndigits, int *decpt, int *sign, char **rve)
  3004. {
  3005. /* Arguments ndigits, decpt, sign are similar to those
  3006. of ecvt and fcvt; trailing zeros are suppressed from
  3007. the returned string. If not null, *rve is set to point
  3008. to the end of the return value. If d is +-Infinity or NaN,
  3009. then *decpt is set to 9999.
  3010. mode:
  3011. 0 ==> shortest string that yields d when read in
  3012. and rounded to nearest.
  3013. 1 ==> like 0, but with Steele & White stopping rule;
  3014. e.g. with IEEE P754 arithmetic , mode 0 gives
  3015. 1e23 whereas mode 1 gives 9.999999999999999e22.
  3016. 2 ==> max(1,ndigits) significant digits. This gives a
  3017. return value similar to that of ecvt, except
  3018. that trailing zeros are suppressed.
  3019. 3 ==> through ndigits past the decimal point. This
  3020. gives a return value similar to that from fcvt,
  3021. except that trailing zeros are suppressed, and
  3022. ndigits can be negative.
  3023. 4,5 ==> similar to 2 and 3, respectively, but (in
  3024. round-nearest mode) with the tests of mode 0 to
  3025. possibly return a shorter string that rounds to d.
  3026. With IEEE arithmetic and compilation with
  3027. -DHonor_FLT_ROUNDS, modes 4 and 5 behave the same
  3028. as modes 2 and 3 when FLT_ROUNDS != 1.
  3029. 6-9 ==> Debugging modes similar to mode - 4: don't try
  3030. fast floating-point estimate (if applicable).
  3031. Values of mode other than 0-9 are treated as mode 0.
  3032. Sufficient space is allocated to the return value
  3033. to hold the suppressed trailing zeros.
  3034. */
  3035. int bbits, b2, b5, be, dig, i, ieps, ilim, ilim0, ilim1,
  3036. j, j1, k, k0, k_check, leftright, m2, m5, s2, s5,
  3037. spec_case, try_quick;
  3038. Long L;
  3039. #ifndef Sudden_Underflow
  3040. int denorm;
  3041. ULong x;
  3042. #endif
  3043. Bigint *b, *b1, *delta, *mlo = 0, *mhi = 0, *S;
  3044. double ds;
  3045. double_u d, d2, eps;
  3046. char *s, *s0;
  3047. #ifdef Honor_FLT_ROUNDS
  3048. int rounding;
  3049. #endif
  3050. #ifdef SET_INEXACT
  3051. int inexact, oldinexact;
  3052. #endif
  3053. dval(d) = d_;
  3054. #ifndef MULTIPLE_THREADS
  3055. if (dtoa_result) {
  3056. freedtoa(dtoa_result);
  3057. dtoa_result = 0;
  3058. }
  3059. #endif
  3060. if (word0(d) & Sign_bit) {
  3061. /* set sign for everything, including 0's and NaNs */
  3062. *sign = 1;
  3063. word0(d) &= ~Sign_bit; /* clear sign bit */
  3064. }
  3065. else
  3066. *sign = 0;
  3067. #if defined(IEEE_Arith) + defined(VAX)
  3068. #ifdef IEEE_Arith
  3069. if ((word0(d) & Exp_mask) == Exp_mask)
  3070. #else
  3071. if (word0(d) == 0x8000)
  3072. #endif
  3073. {
  3074. /* Infinity or NaN */
  3075. *decpt = 9999;
  3076. #ifdef IEEE_Arith
  3077. if (!word1(d) && !(word0(d) & 0xfffff))
  3078. return rv_strdup(INFSTR, rve);
  3079. #endif
  3080. return rv_strdup(NANSTR, rve);
  3081. }
  3082. #endif
  3083. #ifdef IBM
  3084. dval(d) += 0; /* normalize */
  3085. #endif
  3086. if (!dval(d)) {
  3087. *decpt = 1;
  3088. return rv_strdup(ZEROSTR, rve);
  3089. }
  3090. #ifdef SET_INEXACT
  3091. try_quick = oldinexact = get_inexact();
  3092. inexact = 1;
  3093. #endif
  3094. #ifdef Honor_FLT_ROUNDS
  3095. if ((rounding = Flt_Rounds) >= 2) {
  3096. if (*sign)
  3097. rounding = rounding == 2 ? 0 : 2;
  3098. else
  3099. if (rounding != 2)
  3100. rounding = 0;
  3101. }
  3102. #endif
  3103. b = d2b(dval(d), &be, &bbits);
  3104. #ifdef Sudden_Underflow
  3105. i = (int)(word0(d) >> Exp_shift1 & (Exp_mask>>Exp_shift1));
  3106. #else
  3107. if ((i = (int)(word0(d) >> Exp_shift1 & (Exp_mask>>Exp_shift1))) != 0) {
  3108. #endif
  3109. dval(d2) = dval(d);
  3110. word0(d2) &= Frac_mask1;
  3111. word0(d2) |= Exp_11;
  3112. #ifdef IBM
  3113. if (j = 11 - hi0bits(word0(d2) & Frac_mask))
  3114. dval(d2) /= 1 << j;
  3115. #endif
  3116. /* log(x) ~=~ log(1.5) + (x-1.5)/1.5
  3117. * log10(x) = log(x) / log(10)
  3118. * ~=~ log(1.5)/log(10) + (x-1.5)/(1.5*log(10))
  3119. * log10(d) = (i-Bias)*log(2)/log(10) + log10(d2)
  3120. *
  3121. * This suggests computing an approximation k to log10(d) by
  3122. *
  3123. * k = (i - Bias)*0.301029995663981
  3124. * + ( (d2-1.5)*0.289529654602168 + 0.176091259055681 );
  3125. *
  3126. * We want k to be too large rather than too small.
  3127. * The error in the first-order Taylor series approximation
  3128. * is in our favor, so we just round up the constant enough
  3129. * to compensate for any error in the multiplication of
  3130. * (i - Bias) by 0.301029995663981; since |i - Bias| <= 1077,
  3131. * and 1077 * 0.30103 * 2^-52 ~=~ 7.2e-14,
  3132. * adding 1e-13 to the constant term more than suffices.
  3133. * Hence we adjust the constant term to 0.1760912590558.
  3134. * (We could get a more accurate k by invoking log10,
  3135. * but this is probably not worthwhile.)
  3136. */
  3137. i -= Bias;
  3138. #ifdef IBM
  3139. i <<= 2;
  3140. i += j;
  3141. #endif
  3142. #ifndef Sudden_Underflow
  3143. denorm = 0;
  3144. }
  3145. else {
  3146. /* d is denormalized */
  3147. i = bbits + be + (Bias + (P-1) - 1);
  3148. x = i > 32 ? word0(d) << (64 - i) | word1(d) >> (i - 32)
  3149. : word1(d) << (32 - i);
  3150. dval(d2) = x;
  3151. word0(d2) -= 31*Exp_msk1; /* adjust exponent */
  3152. i -= (Bias + (P-1) - 1) + 1;
  3153. denorm = 1;
  3154. }
  3155. #endif
  3156. ds = (dval(d2)-1.5)*0.289529654602168 + 0.1760912590558 + i*0.301029995663981;
  3157. k = (int)ds;
  3158. if (ds < 0. && ds != k)
  3159. k--; /* want k = floor(ds) */
  3160. k_check = 1;
  3161. if (k >= 0 && k <= Ten_pmax) {
  3162. if (dval(d) < tens[k])
  3163. k--;
  3164. k_check = 0;
  3165. }
  3166. j = bbits - i - 1;
  3167. if (j >= 0) {
  3168. b2 = 0;
  3169. s2 = j;
  3170. }
  3171. else {
  3172. b2 = -j;
  3173. s2 = 0;
  3174. }
  3175. if (k >= 0) {
  3176. b5 = 0;
  3177. s5 = k;
  3178. s2 += k;
  3179. }
  3180. else {
  3181. b2 -= k;
  3182. b5 = -k;
  3183. s5 = 0;
  3184. }
  3185. if (mode < 0 || mode > 9)
  3186. mode = 0;
  3187. #ifndef SET_INEXACT
  3188. #ifdef Check_FLT_ROUNDS
  3189. try_quick = Rounding == 1;
  3190. #else
  3191. try_quick = 1;
  3192. #endif
  3193. #endif /*SET_INEXACT*/
  3194. if (mode > 5) {
  3195. mode -= 4;
  3196. try_quick = 0;
  3197. }
  3198. leftright = 1;
  3199. ilim = ilim1 = -1;
  3200. switch (mode) {
  3201. case 0:
  3202. case 1:
  3203. i = 18;
  3204. ndigits = 0;
  3205. break;
  3206. case 2:
  3207. leftright = 0;
  3208. /* no break */
  3209. case 4:
  3210. if (ndigits <= 0)
  3211. ndigits = 1;
  3212. ilim = ilim1 = i = ndigits;
  3213. break;
  3214. case 3:
  3215. leftright = 0;
  3216. /* no break */
  3217. case 5:
  3218. i = ndigits + k + 1;
  3219. ilim = i;
  3220. ilim1 = i - 1;
  3221. if (i <= 0)
  3222. i = 1;
  3223. }
  3224. s = s0 = rv_alloc(i+1);
  3225. #ifdef Honor_FLT_ROUNDS
  3226. if (mode > 1 && rounding != 1)
  3227. leftright = 0;
  3228. #endif
  3229. if (ilim >= 0 && ilim <= Quick_max && try_quick) {
  3230. /* Try to get by with floating-point arithmetic. */
  3231. i = 0;
  3232. dval(d2) = dval(d);
  3233. k0 = k;
  3234. ilim0 = ilim;
  3235. ieps = 2; /* conservative */
  3236. if (k > 0) {
  3237. ds = tens[k&0xf];
  3238. j = k >> 4;
  3239. if (j & Bletch) {
  3240. /* prevent overflows */
  3241. j &= Bletch - 1;
  3242. dval(d) /= bigtens[n_bigtens-1];
  3243. ieps++;
  3244. }
  3245. for (; j; j >>= 1, i++)
  3246. if (j & 1) {
  3247. ieps++;
  3248. ds *= bigtens[i];
  3249. }
  3250. dval(d) /= ds;
  3251. }
  3252. else if ((j1 = -k) != 0) {
  3253. dval(d) *= tens[j1 & 0xf];
  3254. for (j = j1 >> 4; j; j >>= 1, i++)
  3255. if (j & 1) {
  3256. ieps++;
  3257. dval(d) *= bigtens[i];
  3258. }
  3259. }
  3260. if (k_check && dval(d) < 1. && ilim > 0) {
  3261. if (ilim1 <= 0)
  3262. goto fast_failed;
  3263. ilim = ilim1;
  3264. k--;
  3265. dval(d) *= 10.;
  3266. ieps++;
  3267. }
  3268. dval(eps) = ieps*dval(d) + 7.;
  3269. word0(eps) -= (P-1)*Exp_msk1;
  3270. if (ilim == 0) {
  3271. S = mhi = 0;
  3272. dval(d) -= 5.;
  3273. if (dval(d) > dval(eps))
  3274. goto one_digit;
  3275. if (dval(d) < -dval(eps))
  3276. goto no_digits;
  3277. goto fast_failed;
  3278. }
  3279. #ifndef No_leftright
  3280. if (leftright) {
  3281. /* Use Steele & White method of only
  3282. * generating digits needed.
  3283. */
  3284. dval(eps) = 0.5/tens[ilim-1] - dval(eps);
  3285. for (i = 0;;) {
  3286. L = (int)dval(d);
  3287. dval(d) -= L;
  3288. *s++ = '0' + (int)L;
  3289. if (dval(d) < dval(eps))
  3290. goto ret1;
  3291. if (1. - dval(d) < dval(eps))
  3292. goto bump_up;
  3293. if (++i >= ilim)
  3294. break;
  3295. dval(eps) *= 10.;
  3296. dval(d) *= 10.;
  3297. }
  3298. }
  3299. else {
  3300. #endif
  3301. /* Generate ilim digits, then fix them up. */
  3302. dval(eps) *= tens[ilim-1];
  3303. for (i = 1;; i++, dval(d) *= 10.) {
  3304. L = (Long)(dval(d));
  3305. if (!(dval(d) -= L))
  3306. ilim = i;
  3307. *s++ = '0' + (int)L;
  3308. if (i == ilim) {
  3309. if (dval(d) > 0.5 + dval(eps))
  3310. goto bump_up;
  3311. else if (dval(d) < 0.5 - dval(eps)) {
  3312. while (*--s == '0') ;
  3313. s++;
  3314. goto ret1;
  3315. }
  3316. break;
  3317. }
  3318. }
  3319. #ifndef No_leftright
  3320. }
  3321. #endif
  3322. fast_failed:
  3323. s = s0;
  3324. dval(d) = dval(d2);
  3325. k = k0;
  3326. ilim = ilim0;
  3327. }
  3328. /* Do we have a "small" integer? */
  3329. if (be >= 0 && k <= Int_max) {
  3330. /* Yes. */
  3331. ds = tens[k];
  3332. if (ndigits < 0 && ilim <= 0) {
  3333. S = mhi = 0;
  3334. if (ilim < 0 || dval(d) <= 5*ds)
  3335. goto no_digits;
  3336. goto one_digit;
  3337. }
  3338. for (i = 1;; i++, dval(d) *= 10.) {
  3339. L = (Long)(dval(d) / ds);
  3340. dval(d) -= L*ds;
  3341. #ifdef Check_FLT_ROUNDS
  3342. /* If FLT_ROUNDS == 2, L will usually be high by 1 */
  3343. if (dval(d) < 0) {
  3344. L--;
  3345. dval(d) += ds;
  3346. }
  3347. #endif
  3348. *s++ = '0' + (int)L;
  3349. if (!dval(d)) {
  3350. #ifdef SET_INEXACT
  3351. inexact = 0;
  3352. #endif
  3353. break;
  3354. }
  3355. if (i == ilim) {
  3356. #ifdef Honor_FLT_ROUNDS
  3357. if (mode > 1)
  3358. switch (rounding) {
  3359. case 0: goto ret1;
  3360. case 2: goto bump_up;
  3361. }
  3362. #endif
  3363. dval(d) += dval(d);
  3364. if (dval(d) > ds || (dval(d) == ds && (L & 1))) {
  3365. bump_up:
  3366. while (*--s == '9')
  3367. if (s == s0) {
  3368. k++;
  3369. *s = '0';
  3370. break;
  3371. }
  3372. ++*s++;
  3373. }
  3374. break;
  3375. }
  3376. }
  3377. goto ret1;
  3378. }
  3379. m2 = b2;
  3380. m5 = b5;
  3381. if (leftright) {
  3382. i =
  3383. #ifndef Sudden_Underflow
  3384. denorm ? be + (Bias + (P-1) - 1 + 1) :
  3385. #endif
  3386. #ifdef IBM
  3387. 1 + 4*P - 3 - bbits + ((bbits + be - 1) & 3);
  3388. #else
  3389. 1 + P - bbits;
  3390. #endif
  3391. b2 += i;
  3392. s2 += i;
  3393. mhi = i2b(1);
  3394. }
  3395. if (m2 > 0 && s2 > 0) {
  3396. i = m2 < s2 ? m2 : s2;
  3397. b2 -= i;
  3398. m2 -= i;
  3399. s2 -= i;
  3400. }
  3401. if (b5 > 0) {
  3402. if (leftright) {
  3403. if (m5 > 0) {
  3404. mhi = pow5mult(mhi, m5);
  3405. b1 = mult(mhi, b);
  3406. Bfree(b);
  3407. b = b1;
  3408. }
  3409. if ((j = b5 - m5) != 0)
  3410. b = pow5mult(b, j);
  3411. }
  3412. else
  3413. b = pow5mult(b, b5);
  3414. }
  3415. S = i2b(1);
  3416. if (s5 > 0)
  3417. S = pow5mult(S, s5);
  3418. /* Check for special case that d is a normalized power of 2. */
  3419. spec_case = 0;
  3420. if ((mode < 2 || leftright)
  3421. #ifdef Honor_FLT_ROUNDS
  3422. && rounding == 1
  3423. #endif
  3424. ) {
  3425. if (!word1(d) && !(word0(d) & Bndry_mask)
  3426. #ifndef Sudden_Underflow
  3427. && word0(d) & (Exp_mask & ~Exp_msk1)
  3428. #endif
  3429. ) {
  3430. /* The special case */
  3431. b2 += Log2P;
  3432. s2 += Log2P;
  3433. spec_case = 1;
  3434. }
  3435. }
  3436. /* Arrange for convenient computation of quotients:
  3437. * shift left if necessary so divisor has 4 leading 0 bits.
  3438. *
  3439. * Perhaps we should just compute leading 28 bits of S once
  3440. * and for all and pass them and a shift to quorem, so it
  3441. * can do shifts and ors to compute the numerator for q.
  3442. */
  3443. #ifdef Pack_32
  3444. if ((i = ((s5 ? 32 - hi0bits(S->x[S->wds-1]) : 1) + s2) & 0x1f) != 0)
  3445. i = 32 - i;
  3446. #else
  3447. if ((i = ((s5 ? 32 - hi0bits(S->x[S->wds-1]) : 1) + s2) & 0xf) != 0)
  3448. i = 16 - i;
  3449. #endif
  3450. if (i > 4) {
  3451. i -= 4;
  3452. b2 += i;
  3453. m2 += i;
  3454. s2 += i;
  3455. }
  3456. else if (i < 4) {
  3457. i += 28;
  3458. b2 += i;
  3459. m2 += i;
  3460. s2 += i;
  3461. }
  3462. if (b2 > 0)
  3463. b = lshift(b, b2);
  3464. if (s2 > 0)
  3465. S = lshift(S, s2);
  3466. if (k_check) {
  3467. if (cmp(b,S) < 0) {
  3468. k--;
  3469. b = multadd(b, 10, 0); /* we botched the k estimate */
  3470. if (leftright)
  3471. mhi = multadd(mhi, 10, 0);
  3472. ilim = ilim1;
  3473. }
  3474. }
  3475. if (ilim <= 0 && (mode == 3 || mode == 5)) {
  3476. if (ilim < 0 || cmp(b,S = multadd(S,5,0)) <= 0) {
  3477. /* no digits, fcvt style */
  3478. no_digits:
  3479. k = -1 - ndigits;
  3480. goto ret;
  3481. }
  3482. one_digit:
  3483. *s++ = '1';
  3484. k++;
  3485. goto ret;
  3486. }
  3487. if (leftright) {
  3488. if (m2 > 0)
  3489. mhi = lshift(mhi, m2);
  3490. /* Compute mlo -- check for special case
  3491. * that d is a normalized power of 2.
  3492. */
  3493. mlo = mhi;
  3494. if (spec_case) {
  3495. mhi = Balloc(mhi->k);
  3496. Bcopy(mhi, mlo);
  3497. mhi = lshift(mhi, Log2P);
  3498. }
  3499. for (i = 1;;i++) {
  3500. dig = quorem(b,S) + '0';
  3501. /* Do we yet have the shortest decimal string
  3502. * that will round to d?
  3503. */
  3504. j = cmp(b, mlo);
  3505. delta = diff(S, mhi);
  3506. j1 = delta->sign ? 1 : cmp(b, delta);
  3507. Bfree(delta);
  3508. #ifndef ROUND_BIASED
  3509. if (j1 == 0 && mode != 1 && !(word1(d) & 1)
  3510. #ifdef Honor_FLT_ROUNDS
  3511. && rounding >= 1
  3512. #endif
  3513. ) {
  3514. if (dig == '9')
  3515. goto round_9_up;
  3516. if (j > 0)
  3517. dig++;
  3518. #ifdef SET_INEXACT
  3519. else if (!b->x[0] && b->wds <= 1)
  3520. inexact = 0;
  3521. #endif
  3522. *s++ = dig;
  3523. goto ret;
  3524. }
  3525. #endif
  3526. if (j < 0 || (j == 0 && mode != 1
  3527. #ifndef ROUND_BIASED
  3528. && !(word1(d) & 1)
  3529. #endif
  3530. )) {
  3531. if (!b->x[0] && b->wds <= 1) {
  3532. #ifdef SET_INEXACT
  3533. inexact = 0;
  3534. #endif
  3535. goto accept_dig;
  3536. }
  3537. #ifdef Honor_FLT_ROUNDS
  3538. if (mode > 1)
  3539. switch (rounding) {
  3540. case 0: goto accept_dig;
  3541. case 2: goto keep_dig;
  3542. }
  3543. #endif /*Honor_FLT_ROUNDS*/
  3544. if (j1 > 0) {
  3545. b = lshift(b, 1);
  3546. j1 = cmp(b, S);
  3547. if ((j1 > 0 || (j1 == 0 && (dig & 1))) && dig++ == '9')
  3548. goto round_9_up;
  3549. }
  3550. accept_dig:
  3551. *s++ = dig;
  3552. goto ret;
  3553. }
  3554. if (j1 > 0) {
  3555. #ifdef Honor_FLT_ROUNDS
  3556. if (!rounding)
  3557. goto accept_dig;
  3558. #endif
  3559. if (dig == '9') { /* possible if i == 1 */
  3560. round_9_up:
  3561. *s++ = '9';
  3562. goto roundoff;
  3563. }
  3564. *s++ = dig + 1;
  3565. goto ret;
  3566. }
  3567. #ifdef Honor_FLT_ROUNDS
  3568. keep_dig:
  3569. #endif
  3570. *s++ = dig;
  3571. if (i == ilim)
  3572. break;
  3573. b = multadd(b, 10, 0);
  3574. if (mlo == mhi)
  3575. mlo = mhi = multadd(mhi, 10, 0);
  3576. else {
  3577. mlo = multadd(mlo, 10, 0);
  3578. mhi = multadd(mhi, 10, 0);
  3579. }
  3580. }
  3581. }
  3582. else
  3583. for (i = 1;; i++) {
  3584. *s++ = dig = quorem(b,S) + '0';
  3585. if (!b->x[0] && b->wds <= 1) {
  3586. #ifdef SET_INEXACT
  3587. inexact = 0;
  3588. #endif
  3589. goto ret;
  3590. }
  3591. if (i >= ilim)
  3592. break;
  3593. b = multadd(b, 10, 0);
  3594. }
  3595. /* Round off last digit */
  3596. #ifdef Honor_FLT_ROUNDS
  3597. switch (rounding) {
  3598. case 0: goto trimzeros;
  3599. case 2: goto roundoff;
  3600. }
  3601. #endif
  3602. b = lshift(b, 1);
  3603. j = cmp(b, S);
  3604. if (j > 0 || (j == 0 && (dig & 1))) {
  3605. roundoff:
  3606. while (*--s == '9')
  3607. if (s == s0) {
  3608. k++;
  3609. *s++ = '1';
  3610. goto ret;
  3611. }
  3612. ++*s++;
  3613. }
  3614. else {
  3615. while (*--s == '0') ;
  3616. s++;
  3617. }
  3618. ret:
  3619. Bfree(S);
  3620. if (mhi) {
  3621. if (mlo && mlo != mhi)
  3622. Bfree(mlo);
  3623. Bfree(mhi);
  3624. }
  3625. ret1:
  3626. #ifdef SET_INEXACT
  3627. if (inexact) {
  3628. if (!oldinexact) {
  3629. word0(d) = Exp_1 + (70 << Exp_shift);
  3630. word1(d) = 0;
  3631. dval(d) += 1.;
  3632. }
  3633. }
  3634. else if (!oldinexact)
  3635. clear_inexact();
  3636. #endif
  3637. Bfree(b);
  3638. *s = 0;
  3639. *decpt = k + 1;
  3640. if (rve)
  3641. *rve = s;
  3642. return s0;
  3643. }
  3644. void
  3645. ruby_each_words(const char *str, void (*func)(const char*, int, void*), void *arg)
  3646. {
  3647. const char *end;
  3648. int len;
  3649. if (!str) return;
  3650. for (; *str; str = end) {
  3651. while (ISSPACE(*str) || *str == ',') str++;
  3652. if (!*str) break;
  3653. end = str;
  3654. while (*end && !ISSPACE(*end) && *end != ',') end++;
  3655. len = (int)(end - str); /* assume no string exceeds INT_MAX */
  3656. (*func)(str, len, arg);
  3657. }
  3658. }
  3659. /*-
  3660. * Copyright (c) 2004-2008 David Schultz <das@FreeBSD.ORG>
  3661. * All rights reserved.
  3662. *
  3663. * Redistribution and use in source and binary forms, with or without
  3664. * modification, are permitted provided that the following conditions
  3665. * are met:
  3666. * 1. Redistributions of source code must retain the above copyright
  3667. * notice, this list of conditions and the following disclaimer.
  3668. * 2. Redistributions in binary form must reproduce the above copyright
  3669. * notice, this list of conditions and the following disclaimer in the
  3670. * documentation and/or other materials provided with the distribution.
  3671. *
  3672. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  3673. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  3674. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  3675. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  3676. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  3677. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  3678. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  3679. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  3680. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  3681. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  3682. * SUCH DAMAGE.
  3683. */
  3684. #define DBL_MANH_SIZE 20
  3685. #define DBL_MANL_SIZE 32
  3686. #define DBL_ADJ (DBL_MAX_EXP - 2)
  3687. #define SIGFIGS ((DBL_MANT_DIG + 3) / 4 + 1)
  3688. #define dexp_get(u) ((int)(word0(u) >> Exp_shift) & ~Exp_msk1)
  3689. #define dexp_set(u,v) (word0(u) = (((int)(word0(u)) & ~Exp_mask) | (v << Exp_shift)))
  3690. #define dmanh_get(u) ((int)(word0(u) & Frac_mask))
  3691. #define dmanl_get(u) ((int)word1(u))
  3692. /*
  3693. * This procedure converts a double-precision number in IEEE format
  3694. * into a string of hexadecimal digits and an exponent of 2. Its
  3695. * behavior is bug-for-bug compatible with dtoa() in mode 2, with the
  3696. * following exceptions:
  3697. *
  3698. * - An ndigits < 0 causes it to use as many digits as necessary to
  3699. * represent the number exactly.
  3700. * - The additional xdigs argument should point to either the string
  3701. * "0123456789ABCDEF" or the string "0123456789abcdef", depending on
  3702. * which case is desired.
  3703. * - This routine does not repeat dtoa's mistake of setting decpt
  3704. * to 9999 in the case of an infinity or NaN. INT_MAX is used
  3705. * for this purpose instead.
  3706. *
  3707. * Note that the C99 standard does not specify what the leading digit
  3708. * should be for non-zero numbers. For instance, 0x1.3p3 is the same
  3709. * as 0x2.6p2 is the same as 0x4.cp3. This implementation always makes
  3710. * the leading digit a 1. This ensures that the exponent printed is the
  3711. * actual base-2 exponent, i.e., ilogb(d).
  3712. *
  3713. * Inputs: d, xdigs, ndigits
  3714. * Outputs: decpt, sign, rve
  3715. */
  3716. char *
  3717. ruby_hdtoa(double d, const char *xdigs, int ndigits, int *decpt, int *sign,
  3718. char **rve)
  3719. {
  3720. U u;
  3721. char *s, *s0;
  3722. int bufsize;
  3723. uint32_t manh, manl;
  3724. u.d = d;
  3725. if (word0(u) & Sign_bit) {
  3726. /* set sign for everything, including 0's and NaNs */
  3727. *sign = 1;
  3728. word0(u) &= ~Sign_bit; /* clear sign bit */
  3729. }
  3730. else
  3731. *sign = 0;
  3732. if (isinf(d)) { /* FP_INFINITE */
  3733. *decpt = INT_MAX;
  3734. return rv_strdup(INFSTR, rve);
  3735. }
  3736. else if (isnan(d)) { /* FP_NAN */
  3737. *decpt = INT_MAX;
  3738. return rv_strdup(NANSTR, rve);
  3739. }
  3740. else if (d == 0.0) { /* FP_ZERO */
  3741. *decpt = 1;
  3742. return rv_strdup(ZEROSTR, rve);
  3743. }
  3744. else if (dexp_get(u)) { /* FP_NORMAL */
  3745. *decpt = dexp_get(u) - DBL_ADJ;
  3746. }
  3747. else { /* FP_SUBNORMAL */
  3748. u.d *= 5.363123171977039e+154 /* 0x1p514 */;
  3749. *decpt = dexp_get(u) - (514 + DBL_ADJ);
  3750. }
  3751. if (ndigits == 0) /* dtoa() compatibility */
  3752. ndigits = 1;
  3753. /*
  3754. * If ndigits < 0, we are expected to auto-size, so we allocate
  3755. * enough space for all the digits.
  3756. */
  3757. bufsize = (ndigits > 0) ? ndigits : SIGFIGS;
  3758. s0 = rv_alloc(bufsize);
  3759. /* Round to the desired number of digits. */
  3760. if (SIGFIGS > ndigits && ndigits > 0) {
  3761. float redux = 1.0f;
  3762. int offset = 4 * ndigits + DBL_MAX_EXP - 4 - DBL_MANT_DIG;
  3763. dexp_set(u, offset);
  3764. u.d += redux;
  3765. u.d -= redux;
  3766. *decpt += dexp_get(u) - offset;
  3767. }
  3768. manh = dmanh_get(u);
  3769. manl = dmanl_get(u);
  3770. *s0 = '1';
  3771. for (s = s0 + 1; s < s0 + bufsize; s++) {
  3772. *s = xdigs[(manh >> (DBL_MANH_SIZE - 4)) & 0xf];
  3773. manh = (manh << 4) | (manl >> (DBL_MANL_SIZE - 4));
  3774. manl <<= 4;
  3775. }
  3776. /* If ndigits < 0, we are expected to auto-size the precision. */
  3777. if (ndigits < 0) {
  3778. for (ndigits = SIGFIGS; s0[ndigits - 1] == '0'; ndigits--)
  3779. ;
  3780. }
  3781. s = s0 + ndigits;
  3782. *s = '\0';
  3783. if (rve != NULL)
  3784. *rve = s;
  3785. return (s0);
  3786. }
  3787. #ifdef __cplusplus
  3788. #if 0
  3789. {
  3790. #endif
  3791. }
  3792. #endif