PageRenderTime 72ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/util.c

https://github.com/diabolo/ruby
C | 4014 lines | 3273 code | 233 blank | 508 comment | 857 complexity | 625cd7273605632358bd8f6c349a0b10 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause

Large files files are truncated, but you can click here to view the full file

  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. * foo.$$$ => foo.~~~ (fallback)
  208. * makefile => makefile.bak
  209. *
  210. * suffix = "~" (style 2)
  211. * foo.c => foo.c~
  212. * foo.c~ => foo.c~~
  213. * foo.c~~ => foo~.c~~
  214. * foo~.c~~ => foo~~.c~~
  215. * foo~~~~~.c~~ => foo~~~~~.$$$ (fallback)
  216. *
  217. * foo.pas => foo~.pas
  218. * makefile => makefile.~
  219. * longname.fil => longname.fi~
  220. * longname.fi~ => longnam~.fi~
  221. * longnam~.fi~ => longnam~.$$$
  222. *
  223. */
  224. static int valid_filename(const char *s);
  225. static const char suffix1[] = ".$$$";
  226. static const char suffix2[] = ".~~~";
  227. #define strEQ(s1,s2) (strcmp(s1,s2) == 0)
  228. extern const char *ruby_find_basename(const char *, long *, long *);
  229. extern const char *ruby_find_extname(const char *, long *);
  230. void
  231. ruby_add_suffix(VALUE str, const char *suffix)
  232. {
  233. int baselen;
  234. int extlen = strlen(suffix);
  235. char *p, *q;
  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 > 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)) goto fallback;
  256. slen = ext - name;
  257. }
  258. rb_str_resize(str, slen);
  259. rb_str_cat(str, suffix, extlen);
  260. }
  261. else {
  262. strncpy(buf, name, slen);
  263. if (ext)
  264. p = buf + (ext - name);
  265. else
  266. p = buf + slen;
  267. p[len] = '\0';
  268. if (suffix[1] == '\0') { /* Style 2 */
  269. if (len <= 3) {
  270. p[len] = *suffix;
  271. p[++len] = '\0';
  272. }
  273. else if ((q = (char *)ruby_find_basename(buf, &baselen, 0)) &&
  274. baselen < 8) {
  275. q += baselen;
  276. *q++ = *suffix;
  277. if (ext) {
  278. strncpy(q, ext, ext - name);
  279. q[ext - name + 1] = '\0';
  280. }
  281. else
  282. *q = '\0';
  283. }
  284. else if (len == 4 && p[3] != *suffix)
  285. p[3] = *suffix;
  286. else if (baselen == 8 && q[7] != *suffix)
  287. q[7] = *suffix;
  288. else
  289. goto fallback;
  290. }
  291. else { /* Style 3: Panic */
  292. fallback:
  293. (void)memcpy(p, !ext || strEQ(ext, suffix1) ? suffix2 : suffix1, 5);
  294. }
  295. }
  296. rb_str_resize(str, strlen(buf));
  297. memcpy(RSTRING_PTR(str), buf, RSTRING_LEN(str));
  298. }
  299. static int
  300. valid_filename(const char *s)
  301. {
  302. int fd;
  303. /*
  304. // It doesn't exist, so see if we can open it.
  305. */
  306. if ((fd = open(s, O_CREAT|O_EXCL, 0666)) >= 0) {
  307. close(fd);
  308. unlink(s); /* don't leave it laying around */
  309. return 1;
  310. }
  311. else if (errno == EEXIST) {
  312. /* if the file exists, then it's a valid filename! */
  313. return 1;
  314. }
  315. return 0;
  316. }
  317. #endif
  318. /* mm.c */
  319. #define A ((int*)a)
  320. #define B ((int*)b)
  321. #define C ((int*)c)
  322. #define D ((int*)d)
  323. #define mmprepare(base, size) do {\
  324. if (((long)base & (0x3)) == 0)\
  325. if (size >= 16) mmkind = 1;\
  326. else mmkind = 0;\
  327. else mmkind = -1;\
  328. high = (size & (~0xf));\
  329. low = (size & 0x0c);\
  330. } while (0)\
  331. #define mmarg mmkind, size, high, low
  332. static void mmswap_(register char *a, register char *b, int mmkind, size_t size, size_t high, size_t low)
  333. {
  334. register int s;
  335. if (a == b) return;
  336. if (mmkind >= 0) {
  337. if (mmkind > 0) {
  338. register char *t = a + high;
  339. do {
  340. s = A[0]; A[0] = B[0]; B[0] = s;
  341. s = A[1]; A[1] = B[1]; B[1] = s;
  342. s = A[2]; A[2] = B[2]; B[2] = s;
  343. s = A[3]; A[3] = B[3]; B[3] = s; a += 16; b += 16;
  344. } while (a < t);
  345. }
  346. if (low != 0) { s = A[0]; A[0] = B[0]; B[0] = s;
  347. if (low >= 8) { s = A[1]; A[1] = B[1]; B[1] = s;
  348. if (low == 12) {s = A[2]; A[2] = B[2]; B[2] = s;}}}
  349. }
  350. else {
  351. register char *t = a + size;
  352. do {s = *a; *a++ = *b; *b++ = s;} while (a < t);
  353. }
  354. }
  355. #define mmswap(a,b) mmswap_((a),(b),mmarg)
  356. static void mmrot3_(register char *a, register char *b, register char *c, int mmkind, size_t size, size_t high, size_t low)
  357. {
  358. register int s;
  359. if (mmkind >= 0) {
  360. if (mmkind > 0) {
  361. register char *t = a + high;
  362. do {
  363. s = A[0]; A[0] = B[0]; B[0] = C[0]; C[0] = s;
  364. s = A[1]; A[1] = B[1]; B[1] = C[1]; C[1] = s;
  365. s = A[2]; A[2] = B[2]; B[2] = C[2]; C[2] = s;
  366. s = A[3]; A[3] = B[3]; B[3] = C[3]; C[3] = s; a += 16; b += 16; c += 16;
  367. } while (a < t);
  368. }
  369. if (low != 0) { s = A[0]; A[0] = B[0]; B[0] = C[0]; C[0] = s;
  370. if (low >= 8) { s = A[1]; A[1] = B[1]; B[1] = C[1]; C[1] = s;
  371. if (low == 12) {s = A[2]; A[2] = B[2]; B[2] = C[2]; C[2] = s;}}}
  372. }
  373. else {
  374. register char *t = a + size;
  375. do {s = *a; *a++ = *b; *b++ = *c; *c++ = s;} while (a < t);
  376. }
  377. }
  378. #define mmrot3(a,b,c) mmrot3_((a),(b),(c),mmarg)
  379. /* qs6.c */
  380. /*****************************************************/
  381. /* */
  382. /* qs6 (Quick sort function) */
  383. /* */
  384. /* by Tomoyuki Kawamura 1995.4.21 */
  385. /* kawamura@tokuyama.ac.jp */
  386. /*****************************************************/
  387. typedef struct { char *LL, *RR; } stack_node; /* Stack structure for L,l,R,r */
  388. #define PUSH(ll,rr) do { top->LL = (ll); top->RR = (rr); ++top; } while (0) /* Push L,l,R,r */
  389. #define POP(ll,rr) do { --top; ll = top->LL; rr = top->RR; } while (0) /* Pop L,l,R,r */
  390. #define med3(a,b,c) ((*cmp)(a,b,d)<0 ? \
  391. ((*cmp)(b,c,d)<0 ? b : ((*cmp)(a,c,d)<0 ? c : a)) : \
  392. ((*cmp)(b,c,d)>0 ? b : ((*cmp)(a,c,d)<0 ? a : c)))
  393. void
  394. ruby_qsort(void* base, const size_t nel, const size_t size,
  395. int (*cmp)(const void*, const void*, void*), void *d)
  396. {
  397. register char *l, *r, *m; /* l,r:left,right group m:median point */
  398. register int t, eq_l, eq_r; /* eq_l: all items in left group are equal to S */
  399. char *L = base; /* left end of current region */
  400. char *R = (char*)base + size*(nel-1); /* right end of current region */
  401. size_t chklim = 63; /* threshold of ordering element check */
  402. stack_node stack[32], *top = stack; /* 32 is enough for 32bit CPU */
  403. int mmkind;
  404. size_t high, low, n;
  405. if (nel <= 1) return; /* need not to sort */
  406. mmprepare(base, size);
  407. goto start;
  408. nxt:
  409. if (stack == top) return; /* return if stack is empty */
  410. POP(L,R);
  411. for (;;) {
  412. start:
  413. if (L + size == R) { /* 2 elements */
  414. if ((*cmp)(L,R,d) > 0) mmswap(L,R); goto nxt;
  415. }
  416. l = L; r = R;
  417. n = (r - l + size) / size; /* number of elements */
  418. m = l + size * (n >> 1); /* calculate median value */
  419. if (n >= 60) {
  420. register char *m1;
  421. register char *m3;
  422. if (n >= 200) {
  423. n = size*(n>>3); /* number of bytes in splitting 8 */
  424. {
  425. register char *p1 = l + n;
  426. register char *p2 = p1 + n;
  427. register char *p3 = p2 + n;
  428. m1 = med3(p1, p2, p3);
  429. p1 = m + n;
  430. p2 = p1 + n;
  431. p3 = p2 + n;
  432. m3 = med3(p1, p2, p3);
  433. }
  434. }
  435. else {
  436. n = size*(n>>2); /* number of bytes in splitting 4 */
  437. m1 = l + n;
  438. m3 = m + n;
  439. }
  440. m = med3(m1, m, m3);
  441. }
  442. if ((t = (*cmp)(l,m,d)) < 0) { /*3-5-?*/
  443. if ((t = (*cmp)(m,r,d)) < 0) { /*3-5-7*/
  444. if (chklim && nel >= chklim) { /* check if already ascending order */
  445. char *p;
  446. chklim = 0;
  447. for (p=l; p<r; p+=size) if ((*cmp)(p,p+size,d) > 0) goto fail;
  448. goto nxt;
  449. }
  450. fail: goto loopA; /*3-5-7*/
  451. }
  452. if (t > 0) {
  453. if ((*cmp)(l,r,d) <= 0) {mmswap(m,r); goto loopA;} /*3-5-4*/
  454. mmrot3(r,m,l); goto loopA; /*3-5-2*/
  455. }
  456. goto loopB; /*3-5-5*/
  457. }
  458. if (t > 0) { /*7-5-?*/
  459. if ((t = (*cmp)(m,r,d)) > 0) { /*7-5-3*/
  460. if (chklim && nel >= chklim) { /* check if already ascending order */
  461. char *p;
  462. chklim = 0;
  463. for (p=l; p<r; p+=size) if ((*cmp)(p,p+size,d) < 0) goto fail2;
  464. while (l<r) {mmswap(l,r); l+=size; r-=size;} /* reverse region */
  465. goto nxt;
  466. }
  467. fail2: mmswap(l,r); goto loopA; /*7-5-3*/
  468. }
  469. if (t < 0) {
  470. if ((*cmp)(l,r,d) <= 0) {mmswap(l,m); goto loopB;} /*7-5-8*/
  471. mmrot3(l,m,r); goto loopA; /*7-5-6*/
  472. }
  473. mmswap(l,r); goto loopA; /*7-5-5*/
  474. }
  475. if ((t = (*cmp)(m,r,d)) < 0) {goto loopA;} /*5-5-7*/
  476. if (t > 0) {mmswap(l,r); goto loopB;} /*5-5-3*/
  477. /* determining splitting type in case 5-5-5 */ /*5-5-5*/
  478. for (;;) {
  479. if ((l += size) == r) goto nxt; /*5-5-5*/
  480. if (l == m) continue;
  481. if ((t = (*cmp)(l,m,d)) > 0) {mmswap(l,r); l = L; goto loopA;}/*575-5*/
  482. if (t < 0) {mmswap(L,l); l = L; goto loopB;} /*535-5*/
  483. }
  484. loopA: eq_l = 1; eq_r = 1; /* splitting type A */ /* left <= median < right */
  485. for (;;) {
  486. for (;;) {
  487. if ((l += size) == r)
  488. {l -= size; if (l != m) mmswap(m,l); l -= size; goto fin;}
  489. if (l == m) continue;
  490. if ((t = (*cmp)(l,m,d)) > 0) {eq_r = 0; break;}
  491. if (t < 0) eq_l = 0;
  492. }
  493. for (;;) {
  494. if (l == (r -= size))
  495. {l -= size; if (l != m) mmswap(m,l); l -= size; goto fin;}
  496. if (r == m) {m = l; break;}
  497. if ((t = (*cmp)(r,m,d)) < 0) {eq_l = 0; break;}
  498. if (t == 0) break;
  499. }
  500. mmswap(l,r); /* swap left and right */
  501. }
  502. loopB: eq_l = 1; eq_r = 1; /* splitting type B */ /* left < median <= right */
  503. for (;;) {
  504. for (;;) {
  505. if (l == (r -= size))
  506. {r += size; if (r != m) mmswap(r,m); r += size; goto fin;}
  507. if (r == m) continue;
  508. if ((t = (*cmp)(r,m,d)) < 0) {eq_l = 0; break;}
  509. if (t > 0) eq_r = 0;
  510. }
  511. for (;;) {
  512. if ((l += size) == r)
  513. {r += size; if (r != m) mmswap(r,m); r += size; goto fin;}
  514. if (l == m) {m = r; break;}
  515. if ((t = (*cmp)(l,m,d)) > 0) {eq_r = 0; break;}
  516. if (t == 0) break;
  517. }
  518. mmswap(l,r); /* swap left and right */
  519. }
  520. fin:
  521. if (eq_l == 0) /* need to sort left side */
  522. if (eq_r == 0) /* need to sort right side */
  523. if (l-L < R-r) {PUSH(r,R); R = l;} /* sort left side first */
  524. else {PUSH(L,l); L = r;} /* sort right side first */
  525. else R = l; /* need to sort left side only */
  526. else if (eq_r == 0) L = r; /* need to sort right side only */
  527. else goto nxt; /* need not to sort both sides */
  528. }
  529. }
  530. char *
  531. ruby_strdup(const char *str)
  532. {
  533. char *tmp;
  534. size_t len = strlen(str) + 1;
  535. tmp = xmalloc(len);
  536. memcpy(tmp, str, len);
  537. return tmp;
  538. }
  539. char *
  540. ruby_getcwd(void)
  541. {
  542. #ifdef HAVE_GETCWD
  543. int size = 200;
  544. char *buf = xmalloc(size);
  545. while (!getcwd(buf, size)) {
  546. if (errno != ERANGE) {
  547. xfree(buf);
  548. rb_sys_fail("getcwd");
  549. }
  550. size *= 2;
  551. buf = xrealloc(buf, size);
  552. }
  553. #else
  554. # ifndef PATH_MAX
  555. # define PATH_MAX 8192
  556. # endif
  557. char *buf = xmalloc(PATH_MAX+1);
  558. if (!getwd(buf)) {
  559. xfree(buf);
  560. rb_sys_fail("getwd");
  561. }
  562. #endif
  563. return buf;
  564. }
  565. /****************************************************************
  566. *
  567. * The author of this software is David M. Gay.
  568. *
  569. * Copyright (c) 1991, 2000, 2001 by Lucent Technologies.
  570. *
  571. * Permission to use, copy, modify, and distribute this software for any
  572. * purpose without fee is hereby granted, provided that this entire notice
  573. * is included in all copies of any software which is or includes a copy
  574. * or modification of this software and in all copies of the supporting
  575. * documentation for such software.
  576. *
  577. * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
  578. * WARRANTY. IN PARTICULAR, NEITHER THE AUTHOR NOR LUCENT MAKES ANY
  579. * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
  580. * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
  581. *
  582. ***************************************************************/
  583. /* Please send bug reports to David M. Gay (dmg at acm dot org,
  584. * with " at " changed at "@" and " dot " changed to "."). */
  585. /* On a machine with IEEE extended-precision registers, it is
  586. * necessary to specify double-precision (53-bit) rounding precision
  587. * before invoking strtod or dtoa. If the machine uses (the equivalent
  588. * of) Intel 80x87 arithmetic, the call
  589. * _control87(PC_53, MCW_PC);
  590. * does this with many compilers. Whether this or another call is
  591. * appropriate depends on the compiler; for this to work, it may be
  592. * necessary to #include "float.h" or another system-dependent header
  593. * file.
  594. */
  595. /* strtod for IEEE-, VAX-, and IBM-arithmetic machines.
  596. *
  597. * This strtod returns a nearest machine number to the input decimal
  598. * string (or sets errno to ERANGE). With IEEE arithmetic, ties are
  599. * broken by the IEEE round-even rule. Otherwise ties are broken by
  600. * biased rounding (add half and chop).
  601. *
  602. * Inspired loosely by William D. Clinger's paper "How to Read Floating
  603. * Point Numbers Accurately" [Proc. ACM SIGPLAN '90, pp. 92-101].
  604. *
  605. * Modifications:
  606. *
  607. * 1. We only require IEEE, IBM, or VAX double-precision
  608. * arithmetic (not IEEE double-extended).
  609. * 2. We get by with floating-point arithmetic in a case that
  610. * Clinger missed -- when we're computing d * 10^n
  611. * for a small integer d and the integer n is not too
  612. * much larger than 22 (the maximum integer k for which
  613. * we can represent 10^k exactly), we may be able to
  614. * compute (d*10^k) * 10^(e-k) with just one roundoff.
  615. * 3. Rather than a bit-at-a-time adjustment of the binary
  616. * result in the hard case, we use floating-point
  617. * arithmetic to determine the adjustment to within
  618. * one bit; only in really hard cases do we need to
  619. * compute a second residual.
  620. * 4. Because of 3., we don't need a large table of powers of 10
  621. * for ten-to-e (just some small tables, e.g. of 10^k
  622. * for 0 <= k <= 22).
  623. */
  624. /*
  625. * #define IEEE_LITTLE_ENDIAN for IEEE-arithmetic machines where the least
  626. * significant byte has the lowest address.
  627. * #define IEEE_BIG_ENDIAN for IEEE-arithmetic machines where the most
  628. * significant byte has the lowest address.
  629. * #define Long int on machines with 32-bit ints and 64-bit longs.
  630. * #define IBM for IBM mainframe-style floating-point arithmetic.
  631. * #define VAX for VAX-style floating-point arithmetic (D_floating).
  632. * #define No_leftright to omit left-right logic in fast floating-point
  633. * computation of dtoa.
  634. * #define Honor_FLT_ROUNDS if FLT_ROUNDS can assume the values 2 or 3
  635. * and strtod and dtoa should round accordingly.
  636. * #define Check_FLT_ROUNDS if FLT_ROUNDS can assume the values 2 or 3
  637. * and Honor_FLT_ROUNDS is not #defined.
  638. * #define RND_PRODQUOT to use rnd_prod and rnd_quot (assembly routines
  639. * that use extended-precision instructions to compute rounded
  640. * products and quotients) with IBM.
  641. * #define ROUND_BIASED for IEEE-format with biased rounding.
  642. * #define Inaccurate_Divide for IEEE-format with correctly rounded
  643. * products but inaccurate quotients, e.g., for Intel i860.
  644. * #define NO_LONG_LONG on machines that do not have a "long long"
  645. * integer type (of >= 64 bits). On such machines, you can
  646. * #define Just_16 to store 16 bits per 32-bit Long when doing
  647. * high-precision integer arithmetic. Whether this speeds things
  648. * up or slows things down depends on the machine and the number
  649. * being converted. If long long is available and the name is
  650. * something other than "long long", #define Llong to be the name,
  651. * and if "unsigned Llong" does not work as an unsigned version of
  652. * Llong, #define #ULLong to be the corresponding unsigned type.
  653. * #define KR_headers for old-style C function headers.
  654. * #define Bad_float_h if your system lacks a float.h or if it does not
  655. * define some or all of DBL_DIG, DBL_MAX_10_EXP, DBL_MAX_EXP,
  656. * FLT_RADIX, FLT_ROUNDS, and DBL_MAX.
  657. * #define MALLOC your_malloc, where your_malloc(n) acts like malloc(n)
  658. * if memory is available and otherwise does something you deem
  659. * appropriate. If MALLOC is undefined, malloc will be invoked
  660. * directly -- and assumed always to succeed.
  661. * #define Omit_Private_Memory to omit logic (added Jan. 1998) for making
  662. * memory allocations from a private pool of memory when possible.
  663. * When used, the private pool is PRIVATE_MEM bytes long: 2304 bytes,
  664. * unless #defined to be a different length. This default length
  665. * suffices to get rid of MALLOC calls except for unusual cases,
  666. * such as decimal-to-binary conversion of a very long string of
  667. * digits. The longest string dtoa can return is about 751 bytes
  668. * long. For conversions by strtod of strings of 800 digits and
  669. * all dtoa conversions in single-threaded executions with 8-byte
  670. * pointers, PRIVATE_MEM >= 7400 appears to suffice; with 4-byte
  671. * pointers, PRIVATE_MEM >= 7112 appears adequate.
  672. * #define INFNAN_CHECK on IEEE systems to cause strtod to check for
  673. * Infinity and NaN (case insensitively). On some systems (e.g.,
  674. * some HP systems), it may be necessary to #define NAN_WORD0
  675. * appropriately -- to the most significant word of a quiet NaN.
  676. * (On HP Series 700/800 machines, -DNAN_WORD0=0x7ff40000 works.)
  677. * When INFNAN_CHECK is #defined and No_Hex_NaN is not #defined,
  678. * strtod also accepts (case insensitively) strings of the form
  679. * NaN(x), where x is a string of hexadecimal digits and spaces;
  680. * if there is only one string of hexadecimal digits, it is taken
  681. * for the 52 fraction bits of the resulting NaN; if there are two
  682. * or more strings of hex digits, the first is for the high 20 bits,
  683. * the second and subsequent for the low 32 bits, with intervening
  684. * white space ignored; but if this results in none of the 52
  685. * fraction bits being on (an IEEE Infinity symbol), then NAN_WORD0
  686. * and NAN_WORD1 are used instead.
  687. * #define MULTIPLE_THREADS if the system offers preemptively scheduled
  688. * multiple threads. In this case, you must provide (or suitably
  689. * #define) two locks, acquired by ACQUIRE_DTOA_LOCK(n) and freed
  690. * by FREE_DTOA_LOCK(n) for n = 0 or 1. (The second lock, accessed
  691. * in pow5mult, ensures lazy evaluation of only one copy of high
  692. * powers of 5; omitting this lock would introduce a small
  693. * probability of wasting memory, but would otherwise be harmless.)
  694. * You must also invoke freedtoa(s) to free the value s returned by
  695. * dtoa. You may do so whether or not MULTIPLE_THREADS is #defined.
  696. * #define NO_IEEE_Scale to disable new (Feb. 1997) logic in strtod that
  697. * avoids underflows on inputs whose result does not underflow.
  698. * If you #define NO_IEEE_Scale on a machine that uses IEEE-format
  699. * floating-point numbers and flushes underflows to zero rather
  700. * than implementing gradual underflow, then you must also #define
  701. * Sudden_Underflow.
  702. * #define YES_ALIAS to permit aliasing certain double values with
  703. * arrays of ULongs. This leads to slightly better code with
  704. * some compilers and was always used prior to 19990916, but it
  705. * is not strictly legal and can cause trouble with aggressively
  706. * optimizing compilers (e.g., gcc 2.95.1 under -O2).
  707. * #define USE_LOCALE to use the current locale's decimal_point value.
  708. * #define SET_INEXACT if IEEE arithmetic is being used and extra
  709. * computation should be done to set the inexact flag when the
  710. * result is inexact and avoid setting inexact when the result
  711. * is exact. In this case, dtoa.c must be compiled in
  712. * an environment, perhaps provided by #include "dtoa.c" in a
  713. * suitable wrapper, that defines two functions,
  714. * int get_inexact(void);
  715. * void clear_inexact(void);
  716. * such that get_inexact() returns a nonzero value if the
  717. * inexact bit is already set, and clear_inexact() sets the
  718. * inexact bit to 0. When SET_INEXACT is #defined, strtod
  719. * also does extra computations to set the underflow and overflow
  720. * flags when appropriate (i.e., when the result is tiny and
  721. * inexact or when it is a numeric value rounded to +-infinity).
  722. * #define NO_ERRNO if strtod should not assign errno = ERANGE when
  723. * the result overflows to +-Infinity or underflows to 0.
  724. */
  725. #ifdef WORDS_BIGENDIAN
  726. #define IEEE_BIG_ENDIAN
  727. #else
  728. #define IEEE_LITTLE_ENDIAN
  729. #endif
  730. #ifdef __vax__
  731. #define VAX
  732. #undef IEEE_BIG_ENDIAN
  733. #undef IEEE_LITTLE_ENDIAN
  734. #endif
  735. #if defined(__arm__) && !defined(__VFP_FP__)
  736. #define IEEE_BIG_ENDIAN
  737. #undef IEEE_LITTLE_ENDIAN
  738. #endif
  739. #undef Long
  740. #undef ULong
  741. #if SIZEOF_INT == 4
  742. #define Long int
  743. #define ULong unsigned int
  744. #elif SIZEOF_LONG == 4
  745. #define Long long int
  746. #define ULong unsigned long int
  747. #endif
  748. #if HAVE_LONG_LONG
  749. #define Llong LONG_LONG
  750. #endif
  751. #ifdef DEBUG
  752. #include "stdio.h"
  753. #define Bug(x) {fprintf(stderr, "%s\n", x); exit(1);}
  754. #endif
  755. #include "stdlib.h"
  756. #include "string.h"
  757. #ifdef USE_LOCALE
  758. #include "locale.h"
  759. #endif
  760. #ifdef MALLOC
  761. extern void *MALLOC(size_t);
  762. #else
  763. #define MALLOC malloc
  764. #endif
  765. #ifndef Omit_Private_Memory
  766. #ifndef PRIVATE_MEM
  767. #define PRIVATE_MEM 2304
  768. #endif
  769. #define PRIVATE_mem ((PRIVATE_MEM+sizeof(double)-1)/sizeof(double))
  770. static double private_mem[PRIVATE_mem], *pmem_next = private_mem;
  771. #endif
  772. #undef IEEE_Arith
  773. #undef Avoid_Underflow
  774. #ifdef IEEE_BIG_ENDIAN
  775. #define IEEE_Arith
  776. #endif
  777. #ifdef IEEE_LITTLE_ENDIAN
  778. #define IEEE_Arith
  779. #endif
  780. #ifdef Bad_float_h
  781. #ifdef IEEE_Arith
  782. #define DBL_DIG 15
  783. #define DBL_MAX_10_EXP 308
  784. #define DBL_MAX_EXP 1024
  785. #define FLT_RADIX 2
  786. #endif /*IEEE_Arith*/
  787. #ifdef IBM
  788. #define DBL_DIG 16
  789. #define DBL_MAX_10_EXP 75
  790. #define DBL_MAX_EXP 63
  791. #define FLT_RADIX 16
  792. #define DBL_MAX 7.2370055773322621e+75
  793. #endif
  794. #ifdef VAX
  795. #define DBL_DIG 16
  796. #define DBL_MAX_10_EXP 38
  797. #define DBL_MAX_EXP 127
  798. #define FLT_RADIX 2
  799. #define DBL_MAX 1.7014118346046923e+38
  800. #endif
  801. #ifndef LONG_MAX
  802. #define LONG_MAX 2147483647
  803. #endif
  804. #else /* ifndef Bad_float_h */
  805. #include "float.h"
  806. #endif /* Bad_float_h */
  807. #ifndef __MATH_H__
  808. #include "math.h"
  809. #endif
  810. #ifdef __cplusplus
  811. extern "C" {
  812. #if 0
  813. }
  814. #endif
  815. #endif
  816. #if defined(IEEE_LITTLE_ENDIAN) + defined(IEEE_BIG_ENDIAN) + defined(VAX) + defined(IBM) != 1
  817. Exactly one of IEEE_LITTLE_ENDIAN, IEEE_BIG_ENDIAN, VAX, or IBM should be defined.
  818. #endif
  819. typedef union { double d; ULong L[2]; } U;
  820. #ifdef YES_ALIAS
  821. typedef double double_u;
  822. # define dval(x) x
  823. # ifdef IEEE_LITTLE_ENDIAN
  824. # define word0(x) (((ULong *)&x)[1])
  825. # define word1(x) (((ULong *)&x)[0])
  826. # else
  827. # define word0(x) (((ULong *)&x)[0])
  828. # define word1(x) (((ULong *)&x)[1])
  829. # endif
  830. #else
  831. typedef U double_u;
  832. # ifdef IEEE_LITTLE_ENDIAN
  833. # define word0(x) (x.L[1])
  834. # define word1(x) (x.L[0])
  835. # else
  836. # define word0(x) (x.L[0])
  837. # define word1(x) (x.L[1])
  838. # endif
  839. # define dval(x) (x.d)
  840. #endif
  841. /* The following definition of Storeinc is appropriate for MIPS processors.
  842. * An alternative that might be better on some machines is
  843. * #define Storeinc(a,b,c) (*a++ = b << 16 | c & 0xffff)
  844. */
  845. #if defined(IEEE_LITTLE_ENDIAN) + defined(VAX) + defined(__arm__)
  846. #define Storeinc(a,b,c) (((unsigned short *)a)[1] = (unsigned short)b, \
  847. ((unsigned short *)a)[0] = (unsigned short)c, a++)
  848. #else
  849. #define Storeinc(a,b,c) (((unsigned short *)a)[0] = (unsigned short)b, \
  850. ((unsigned short *)a)[1] = (unsigned short)c, a++)
  851. #endif
  852. /* #define P DBL_MANT_DIG */
  853. /* Ten_pmax = floor(P*log(2)/log(5)) */
  854. /* Bletch = (highest power of 2 < DBL_MAX_10_EXP) / 16 */
  855. /* Quick_max = floor((P-1)*log(FLT_RADIX)/log(10) - 1) */
  856. /* Int_max = floor(P*log(FLT_RADIX)/log(10) - 1) */
  857. #ifdef IEEE_Arith
  858. #define Exp_shift 20
  859. #define Exp_shift1 20
  860. #define Exp_msk1 0x100000
  861. #define Exp_msk11 0x100000
  862. #define Exp_mask 0x7ff00000
  863. #define P 53
  864. #define Bias 1023
  865. #define Emin (-1022)
  866. #define Exp_1 0x3ff00000
  867. #define Exp_11 0x3ff00000
  868. #define Ebits 11
  869. #define Frac_mask 0xfffff
  870. #define Frac_mask1 0xfffff
  871. #define Ten_pmax 22
  872. #define Bletch 0x10
  873. #define Bndry_mask 0xfffff
  874. #define Bndry_mask1 0xfffff
  875. #define LSB 1
  876. #define Sign_bit 0x80000000
  877. #define Log2P 1
  878. #define Tiny0 0
  879. #define Tiny1 1
  880. #define Quick_max 14
  881. #define Int_max 14
  882. #ifndef NO_IEEE_Scale
  883. #define Avoid_Underflow
  884. #ifdef Flush_Denorm /* debugging option */
  885. #undef Sudden_Underflow
  886. #endif
  887. #endif
  888. #ifndef Flt_Rounds
  889. #ifdef FLT_ROUNDS
  890. #define Flt_Rounds FLT_ROUNDS
  891. #else
  892. #define Flt_Rounds 1
  893. #endif
  894. #endif /*Flt_Rounds*/
  895. #ifdef Honor_FLT_ROUNDS
  896. #define Rounding rounding
  897. #undef Check_FLT_ROUNDS
  898. #define Check_FLT_ROUNDS
  899. #else
  900. #define Rounding Flt_Rounds
  901. #endif
  902. #else /* ifndef IEEE_Arith */
  903. #undef Check_FLT_ROUNDS
  904. #undef Honor_FLT_ROUNDS
  905. #undef SET_INEXACT
  906. #undef Sudden_Underflow
  907. #define Sudden_Underflow
  908. #ifdef IBM
  909. #undef Flt_Rounds
  910. #define Flt_Rounds 0
  911. #define Exp_shift 24
  912. #define Exp_shift1 24
  913. #define Exp_msk1 0x1000000
  914. #define Exp_msk11 0x1000000
  915. #define Exp_mask 0x7f000000
  916. #define P 14
  917. #define Bias 65
  918. #define Exp_1 0x41000000
  919. #define Exp_11 0x41000000
  920. #define Ebits 8 /* exponent has 7 bits, but 8 is the right value in b2d */
  921. #define Frac_mask 0xffffff
  922. #define Frac_mask1 0xffffff
  923. #define Bletch 4
  924. #define Ten_pmax 22
  925. #define Bndry_mask 0xefffff
  926. #define Bndry_mask1 0xffffff
  927. #define LSB 1
  928. #define Sign_bit 0x80000000
  929. #define Log2P 4
  930. #define Tiny0 0x100000
  931. #define Tiny1 0
  932. #define Quick_max 14
  933. #define Int_max 15
  934. #else /* VAX */
  935. #undef Flt_Rounds
  936. #define Flt_Rounds 1
  937. #define Exp_shift 23
  938. #define Exp_shift1 7
  939. #define Exp_msk1 0x80
  940. #define Exp_msk11 0x800000
  941. #define Exp_mask 0x7f80
  942. #define P 56
  943. #define Bias 129
  944. #define Exp_1 0x40800000
  945. #define Exp_11 0x4080
  946. #define Ebits 8
  947. #define Frac_mask 0x7fffff
  948. #define Frac_mask1 0xffff007f
  949. #define Ten_pmax 24
  950. #define Bletch 2
  951. #define Bndry_mask 0xffff007f
  952. #define Bndry_mask1 0xffff007f
  953. #define LSB 0x10000
  954. #define Sign_bit 0x8000
  955. #define Log2P 1
  956. #define Tiny0 0x80
  957. #define Tiny1 0
  958. #define Quick_max 15
  959. #define Int_max 15
  960. #endif /* IBM, VAX */
  961. #endif /* IEEE_Arith */
  962. #ifndef IEEE_Arith
  963. #define ROUND_BIASED
  964. #endif
  965. #ifdef RND_PRODQUOT
  966. #define rounded_product(a,b) a = rnd_prod(a, b)
  967. #define rounded_quotient(a,b) a = rnd_quot(a, b)
  968. extern double rnd_prod(double, double), rnd_quot(double, double);
  969. #else
  970. #define rounded_product(a,b) a *= b
  971. #define rounded_quotient(a,b) a /= b
  972. #endif
  973. #define Big0 (Frac_mask1 | Exp_msk1*(DBL_MAX_EXP+Bias-1))
  974. #define Big1 0xffffffff
  975. #ifndef Pack_32
  976. #define Pack_32
  977. #endif
  978. #define FFFFFFFF 0xffffffffUL
  979. #ifdef NO_LONG_LONG
  980. #undef ULLong
  981. #ifdef Just_16
  982. #undef Pack_32
  983. /* When Pack_32 is not defined, we store 16 bits per 32-bit Long.
  984. * This makes some inner loops simpler and sometimes saves work
  985. * during multiplications, but it often seems to make things slightly
  986. * slower. Hence the default is now to store 32 bits per Long.
  987. */
  988. #endif
  989. #else /* long long available */
  990. #ifndef Llong
  991. #define Llong long long
  992. #endif
  993. #ifndef ULLong
  994. #define ULLong unsigned Llong
  995. #endif
  996. #endif /* NO_LONG_LONG */
  997. #define MULTIPLE_THREADS 1
  998. #ifndef MULTIPLE_THREADS
  999. #define ACQUIRE_DTOA_LOCK(n) /*nothing*/
  1000. #define FREE_DTOA_LOCK(n) /*nothing*/
  1001. #else
  1002. #define ACQUIRE_DTOA_LOCK(n) /*unused right now*/
  1003. #define FREE_DTOA_LOCK(n) /*unused right now*/
  1004. #endif
  1005. #define Kmax 15
  1006. struct Bigint {
  1007. struct Bigint *next;
  1008. int k, maxwds, sign, wds;
  1009. ULong x[1];
  1010. };
  1011. typedef struct Bigint Bigint;
  1012. static Bigint *freelist[Kmax+1];
  1013. static Bigint *
  1014. Balloc(int k)
  1015. {
  1016. int x;
  1017. Bigint *rv;
  1018. #ifndef Omit_Private_Memory
  1019. size_t len;
  1020. #endif
  1021. ACQUIRE_DTOA_LOCK(0);
  1022. if ((rv = freelist[k]) != 0) {
  1023. freelist[k] = rv->next;
  1024. }
  1025. else {
  1026. x = 1 << k;
  1027. #ifdef Omit_Private_Memory
  1028. rv = (Bigint *)MALLOC(sizeof(Bigint) + (x-1)*sizeof(ULong));
  1029. #else
  1030. len = (sizeof(Bigint) + (x-1)*sizeof(ULong) + sizeof(double) - 1)
  1031. /sizeof(double);
  1032. if (pmem_next - private_mem + len <= PRIVATE_mem) {
  1033. rv = (Bigint*)pmem_next;
  1034. pmem_next += len;
  1035. }
  1036. else
  1037. rv = (Bigint*)MALLOC(len*sizeof(double));
  1038. #endif
  1039. rv->k = k;
  1040. rv->maxwds = x;
  1041. }
  1042. FREE_DTOA_LOCK(0);
  1043. rv->sign = rv->wds = 0;
  1044. return rv;
  1045. }
  1046. static void
  1047. Bfree(Bigint *v)
  1048. {
  1049. if (v) {
  1050. ACQUIRE_DTOA_LOCK(0);
  1051. v->next = freelist[v->k];
  1052. freelist[v->k] = v;
  1053. FREE_DTOA_LOCK(0);
  1054. }
  1055. }
  1056. #define Bcopy(x,y) memcpy((char *)&x->sign, (char *)&y->sign, \
  1057. y->wds*sizeof(Long) + 2*sizeof(int))
  1058. static Bigint *
  1059. multadd(Bigint *b, int m, int a) /* multiply by m and add a */
  1060. {
  1061. int i, wds;
  1062. ULong *x;
  1063. #ifdef ULLong
  1064. ULLong carry, y;
  1065. #else
  1066. ULong carry, y;
  1067. #ifdef Pack_32
  1068. ULong xi, z;
  1069. #endif
  1070. #endif
  1071. Bigint *b1;
  1072. wds = b->wds;
  1073. x = b->x;
  1074. i = 0;
  1075. carry = a;
  1076. do {
  1077. #ifdef ULLong
  1078. y = *x * (ULLong)m + carry;
  1079. carry = y >> 32;
  1080. *x++ = (ULong)(y & FFFFFFFF);
  1081. #else
  1082. #ifdef Pack_32
  1083. xi = *x;
  1084. y = (xi & 0xffff) * m + carry;
  1085. z = (xi >> 16) * m + (y >> 16);
  1086. carry = z >> 16;
  1087. *x++ = (z << 16) + (y & 0xffff);
  1088. #else
  1089. y = *x * m + carry;
  1090. carry = y >> 16;
  1091. *x++ = y & 0xffff;
  1092. #endif
  1093. #endif
  1094. } while (++i < wds);
  1095. if (carry) {
  1096. if (wds >= b->maxwds) {
  1097. b1 = Balloc(b->k+1);
  1098. Bcopy(b1, b);
  1099. Bfree(b);
  1100. b = b1;
  1101. }
  1102. b->x[wds++] = (ULong)carry;
  1103. b->wds = wds;
  1104. }
  1105. return b;
  1106. }
  1107. static Bigint *
  1108. s2b(const char *s, int nd0, int nd, ULong y9)
  1109. {
  1110. Bigint *b;
  1111. int i, k;
  1112. Long x, y;
  1113. x = (nd + 8) / 9;
  1114. for (k = 0, y = 1; x > y; y <<= 1, k++) ;
  1115. #ifdef Pack_32
  1116. b = Balloc(k);
  1117. b->x[0] = y9;
  1118. b->wds = 1;
  1119. #else
  1120. b = Balloc(k+1);
  1121. b->x[0] = y9 & 0xffff;
  1122. b->wds = (b->x[1] = y9 >> 16) ? 2 : 1;
  1123. #endif
  1124. i = 9;
  1125. if (9 < nd0) {
  1126. s += 9;
  1127. do {
  1128. b = multadd(b, 10, *s++ - '0');
  1129. } while (++i < nd0);
  1130. s++;
  1131. }
  1132. else
  1133. s += 10;
  1134. for (; i < nd; i++)
  1135. b = multadd(b, 10, *s++ - '0');
  1136. return b;
  1137. }
  1138. static int
  1139. hi0bits(register ULong x)
  1140. {
  1141. register int k = 0;
  1142. if (!(x & 0xffff0000)) {
  1143. k = 16;
  1144. x <<= 16;
  1145. }
  1146. if (!(x & 0xff000000)) {
  1147. k += 8;
  1148. x <<= 8;
  1149. }
  1150. if (!(x & 0xf0000000)) {
  1151. k += 4;
  1152. x <<= 4;
  1153. }
  1154. if (!(x & 0xc0000000)) {
  1155. k += 2;
  1156. x <<= 2;
  1157. }
  1158. if (!(x & 0x80000000)) {
  1159. k++;
  1160. if (!(x & 0x40000000))
  1161. return 32;
  1162. }
  1163. return k;
  1164. }
  1165. static int
  1166. lo0bits(ULong *y)
  1167. {
  1168. register int k;
  1169. register ULong x = *y;
  1170. if (x & 7) {
  1171. if (x & 1)
  1172. return 0;
  1173. if (x & 2) {
  1174. *y = x >> 1;
  1175. return 1;
  1176. }
  1177. *y = x >> 2;
  1178. return 2;
  1179. }
  1180. k = 0;
  1181. if (!(x & 0xffff)) {
  1182. k = 16;
  1183. x >>= 16;
  1184. }
  1185. if (!(x & 0xff)) {
  1186. k += 8;
  1187. x >>= 8;
  1188. }
  1189. if (!(x & 0xf)) {
  1190. k += 4;
  1191. x >>= 4;
  1192. }
  1193. if (!(x & 0x3)) {
  1194. k += 2;
  1195. x >>= 2;
  1196. }
  1197. if (!(x & 1)) {
  1198. k++;
  1199. x >>= 1;
  1200. if (!x)
  1201. return 32;
  1202. }
  1203. *y = x;
  1204. return k;
  1205. }
  1206. static Bigint *
  1207. i2b(int i)
  1208. {
  1209. Bigint *b;
  1210. b = Balloc(1);
  1211. b->x[0] = i;
  1212. b->wds = 1;
  1213. return b;
  1214. }
  1215. static Bigint *
  1216. mult(Bigint *a, Bigint *b)
  1217. {
  1218. Bigint *c;
  1219. int k, wa, wb, wc;
  1220. ULong *x, *xa, *xae, *xb, *xbe, *xc, *xc0;
  1221. ULong y;
  1222. #ifdef ULLong
  1223. ULLong carry, z;
  1224. #else
  1225. ULong carry, z;
  1226. #ifdef Pack_32
  1227. ULong z2;
  1228. #endif
  1229. #endif
  1230. if (a->wds < b->wds) {
  1231. c = a;
  1232. a = b;
  1233. b = c;
  1234. }
  1235. k = a->k;
  1236. wa = a->wds;
  1237. wb = b->wds;
  1238. wc = wa + wb;
  1239. if (wc > a->maxwds)
  1240. k++;
  1241. c = Balloc(k);
  1242. for (x = c->x, xa = x + wc; x < xa; x++)
  1243. *x = 0;
  1244. xa = a->x;
  1245. xae = xa + wa;
  1246. xb = b->x;
  1247. xbe = xb + wb;
  1248. xc0 = c->x;
  1249. #ifdef ULLong
  1250. for (; xb < xbe; xc0++) {
  1251. if ((y = *xb++) != 0) {
  1252. x = xa;
  1253. xc = xc0;
  1254. carry = 0;
  1255. do {
  1256. z = *x++ * (ULLong)y + *xc + carry;
  1257. carry = z >> 32;
  1258. *xc++ = (ULong)(z & FFFFFFFF);
  1259. } while (x < xae);
  1260. *xc = (ULong)carry;
  1261. }
  1262. }
  1263. #else
  1264. #ifdef Pack_32
  1265. for (; xb < xbe; xb++, xc0++) {
  1266. if (y = *xb & 0xffff) {
  1267. x = xa;
  1268. xc = xc0;
  1269. carry = 0;
  1270. do {
  1271. z = (*x & 0xffff) * y + (*xc & 0xffff) + carry;
  1272. carry = z >> 16;
  1273. z2 = (*x++ >> 16) * y + (*xc >> 16) + carry;
  1274. carry = z2 >> 16;
  1275. Storeinc(xc, z2, z);
  1276. } while (x < xae);
  1277. *xc = (ULong)carry;
  1278. }
  1279. if (y = *xb >> 16) {
  1280. x = xa;
  1281. xc = xc0;
  1282. carry = 0;
  1283. z2 = *xc;
  1284. do {
  1285. z = (*x & 0xffff) * y + (*xc >> 16) + carry;
  1286. carry = z >> 16;
  1287. Storeinc(xc, z, z2);
  1288. z2 = (*x++ >> 16) * y + (*xc & 0xffff) + carry;
  1289. carry = z2 >> 16;
  1290. } while (x < xae);
  1291. *xc = z2;
  1292. }
  1293. }
  1294. #else
  1295. for (; xb < xbe; xc0++) {
  1296. if (y = *xb++) {
  1297. x = xa;
  1298. xc = xc0;
  1299. carry = 0;
  1300. do {
  1301. z = *x++ * y + *xc + carry;
  1302. carry = z >> 16;
  1303. *xc++ = z & 0xffff;
  1304. } while (x < xae);
  1305. *xc = (ULong)carry;
  1306. }
  1307. }
  1308. #endif
  1309. #endif
  1310. for (xc0 = c->x, xc = xc0 + wc; wc > 0 && !*--xc; --wc) ;
  1311. c->wds = wc;
  1312. return c;
  1313. }
  1314. static Bigint *p5s;
  1315. static Bigint *
  1316. pow5mult(Bigint *b, int k)
  1317. {
  1318. Bigint *b1, *p5, *p51;
  1319. int i;
  1320. static int p05[3] = { 5, 25, 125 };
  1321. if ((i = k & 3) != 0)
  1322. b = multadd(b, p05[i-1], 0);
  1323. if (!(k >>= 2))
  1324. return b;
  1325. if (!(p5 = p5s)) {
  1326. /* first time */
  1327. #ifdef MULTIPLE_THREADS
  1328. ACQUIRE_DTOA_LOCK(1);
  1329. if (!(p5 = p5s)) {
  1330. p5 = p5s = i2b(625);
  1331. p5->next = 0;
  1332. }
  1333. FREE_DTOA_LOCK(1);
  1334. #else
  1335. p5 = p5s = i2b(625);
  1336. p5->next = 0;
  1337. #endif
  1338. }
  1339. for (;;) {
  1340. if (k & 1) {
  1341. b1 = mult(b, p5);
  1342. Bfree(b);
  1343. b = b1;
  1344. }
  1345. if (!(k >>= 1))
  1346. break;
  1347. if (!(p51 = p5->next)) {
  1348. #ifdef MULTIPLE_THREADS
  1349. ACQUIRE_DTOA_LOCK(1);
  1350. if (!(p51 = p5->next)) {
  1351. p51 = p5->next = mult(p5,p5);
  1352. p51->next = 0;
  1353. }
  1354. FREE_DTOA_LOCK(1);
  1355. #else
  1356. p51 = p5->next = mult(p5,p5);
  1357. p51->next = 0;
  1358. #endif
  1359. }
  1360. p5 = p51;
  1361. }
  1362. return b;
  1363. }
  1364. static Bigint *
  1365. lshift(Bigint *b, int k)
  1366. {
  1367. int i, k1, n, n1;
  1368. Bigint *b1;
  1369. ULong *x, *x1, *xe, z;
  1370. #ifdef Pack_32
  1371. n = k >> 5;
  1372. #else
  1373. n = k >> 4;
  1374. #endif
  1375. k1 = b->k;
  1376. n1 = n + b->wds + 1;
  1377. for (i = b->maxwds; n1 > i; i <<= 1)
  1378. k1++;
  1379. b1 = Balloc(k1);
  1380. x1 = b1->x;
  1381. for (i = 0; i < n; i++)
  1382. *x1++ = 0;
  1383. x = b->x;
  1384. xe = x + b->wds;
  1385. #ifdef Pack_32
  1386. if (k &= 0x1f) {
  1387. k1 = 32 - k;
  1388. z = 0;
  1389. do {
  1390. *x1++ = *x << k | z;
  1391. z = *x++ >> k1;
  1392. } while (x < xe);
  1393. if ((*x1 = z) != 0)
  1394. ++n1;
  1395. }
  1396. #else
  1397. if (k &= 0xf) {
  1398. k1 = 16 - k;
  1399. z = 0;
  1400. do {
  1401. *x1++ = *x << k & 0xffff | z;
  1402. z = *x++ >> k1;
  1403. } while (x < xe);
  1404. if (*x1 = z)
  1405. ++n1;
  1406. }
  1407. #endif
  1408. else
  1409. do {
  1410. *x1++ = *x++;
  1411. } while (x < xe);
  1412. b1->wds = n1 - 1;
  1413. Bfree(b);
  1414. return b1;
  1415. }
  1416. static int
  1417. cmp(Bigint *a, Bigint *b)
  1418. {
  1419. ULong *xa, *xa0, *xb, *xb0;
  1420. int i, j;
  1421. i = a->wds;
  1422. j = b->wds;
  1423. #ifdef DEBUG
  1424. if (i > 1 && !a->x[i-1])
  1425. Bug("cmp called with a->x[a->wds-1] == 0");
  1426. if (j > 1 && !b->x[j-1])
  1427. Bug("cmp called with b->x[b->wds-1] == 0");
  1428. #endif
  1429. if (i -= j)
  1430. return i;
  1431. xa0 = a->x;
  1432. xa = xa0 + j;
  1433. xb0 = b->x;
  1434. xb = xb0 + j;
  1435. for (;;) {
  1436. if (*--xa != *--xb)
  1437. return *xa < *xb ? -1 : 1;
  1438. if (xa <= xa0)
  1439. break;
  1440. }
  1441. return 0;
  1442. }
  1443. static Bigint *
  1444. diff(Bigint *a, Bigint *b)
  1445. {
  1446. Bigint *c;
  1447. int i, wa, wb;
  1448. ULong *xa, *xae, *xb, *xbe, *xc;
  1449. #ifdef ULLong
  1450. ULLong borrow, y;
  1451. #else
  1452. ULong borrow, y;
  1453. #ifdef Pack_32
  1454. ULong z;
  1455. #endif
  1456. #endif
  1457. i = cmp(a,b);
  1458. if (!i) {
  1459. c = Balloc(0);
  1460. c->wds = 1;
  1461. c->x[0] = 0;
  1462. return c;
  1463. }
  1464. if (i < 0) {
  1465. c = a;
  1466. a = b;
  1467. b = c;
  1468. i = 1;
  1469. }
  1470. else
  1471. i = 0;
  1472. c = Balloc(a->k);
  1473. c->sign = i;
  1474. wa = a->wds;
  1475. xa = a->x;
  1476. xae = xa + wa;
  1477. wb = b->wds;
  1478. xb = b->x;
  1479. xbe = xb + wb;
  1480. xc = c->x;
  1481. borrow = 0;
  1482. #ifdef ULLong
  1483. do {
  1484. y = (ULLong)*xa++ - *xb++ - borrow;
  1485. borrow = y >> 32 & (ULong)1;
  1486. *xc++ = (ULong)(y & FFFFFFFF);
  1487. } while (xb < xbe);
  1488. while (xa < xae) {
  1489. y = *xa++ - borrow;
  1490. borrow = y >> 32 & (ULong)1;
  1491. *xc++ = (ULong)(y & FFFFFFFF);
  1492. }
  1493. #else
  1494. #ifdef Pack_32
  1495. do {
  1496. y = (*xa & 0xffff) - (*xb & 0xffff) - borrow;
  1497. borrow = (y & 0x10000) >> 16;
  1498. z = (*xa++ >> 16) - (*xb++ >> 16) - borrow;
  1499. borrow = (z & 0x10000) >> 16;
  1500. Storeinc(xc, z, y);
  1501. } while (xb < xbe);
  1502. while (xa < xae) {
  1503. y = (*xa & 0xffff) - borrow;
  1504. borrow = (y & 0x10000) >> 16;
  1505. z = (*xa++ >> 16) - borrow;
  1506. borrow = (z & 0x10000) >> 16;
  1507. Storeinc(xc, z, y);
  1508. }
  1509. #else
  1510. do {
  1511. y = *xa++ - *xb++ - borrow;
  1512. borrow = (y & 0x10000) >> 16;
  1513. *xc++ = y & 0xffff;
  1514. } while (xb < xbe);
  1515. while (xa < xae) {
  1516. y = *xa++ - borrow;
  1517. borrow = (y & 0x10000) >> 16;
  1518. *xc++ = y & 0xffff;
  1519. }
  1520. #endif
  1521. #endif
  1522. while (!*--xc)
  1523. wa--;
  1524. c->wds = wa;
  1525. return c;
  1526. }
  1527. static double
  1528. ulp(double x_)
  1529. {
  1530. register Long L;
  1531. double_u x, a;
  1532. dval(x) = x_;
  1533. L = (word0(x) & Exp_mask) - (P-1)*Exp_msk1;
  1534. #ifndef Avoid_Underflow
  1535. #ifndef Sudden_Underflow
  1536. if (L > 0) {
  1537. #endif
  1538. #endif
  1539. #ifdef IBM
  1540. L |= Exp_msk1 >> 4;
  1541. #endif
  1542. word0(a) = L;
  1543. word1(a) = 0;
  1544. #ifndef Avoid_Underflow
  1545. #ifndef Sudden_Underflow
  1546. }
  1547. else {
  1548. L = -L >> Exp_shift;
  1549. if (L < Exp_shift) {
  1550. word0(a) = 0x80000 >> L;
  1551. word1(a) = 0;
  1552. }
  1553. else {
  1554. word0(a) = 0;
  1555. L -= Exp_shift;
  1556. word1(a) = L >= 31 ? 1 : 1 << 31 - L;
  1557. }
  1558. }
  1559. #endif
  1560. #endif
  1561. return dval(a);
  1562. }
  1563. static double
  1564. b2d(Bigint *a, int *e)
  1565. {
  1566. ULong *xa, *xa0, w, y, z;
  1567. int k;
  1568. double_u d;
  1569. #ifdef VAX
  1570. ULong d0, d1;
  1571. #else
  1572. #define d0 word0(d)
  1573. #define d1 word1(d)
  1574. #endif
  1575. xa0 = a->x;
  1576. xa = xa0 + a->wds;
  1577. y = *--xa;
  1578. #ifdef DEBUG
  1579. if (!y) Bug("zero y in b2d");
  1580. #endif
  1581. k = hi0bits(y);
  1582. *e = 32 - k;
  1583. #ifdef Pack_32
  1584. if (k < Ebits) {
  1585. d0 = Exp_1 | y >> (Ebits - k);
  1586. w = xa > xa0 ? *--xa : 0;
  1587. d1 = y << ((32-Ebits) + k) | w >> (Ebits - k);
  1588. goto ret_d;
  1589. }
  1590. z = xa > xa0 ? *--xa : 0;
  1591. if (k -= Ebits) {
  1592. d0 = Exp_1 | y << k | z >> (32 - k);
  1593. y = xa > xa0 ? *--xa : 0;
  1594. d1 = z << k | y >> (32 - k);
  1595. }
  1596. else {
  1597. d0 = Exp_1 | y;
  1598. d1 = z;
  1599. }
  1600. #else
  1601. if (k < Ebits + 16) {
  1602. z = xa > xa0 ? *--xa : 0;
  1603. d0 = Exp_1 | y << k - Ebits | z >> Ebits + 16 - k;
  1604. w = xa > xa0 ? *--xa : 0;
  1605. y = xa > xa0 ? *--xa : 0;
  1606. d1 = z << k + 16 - Ebits | w << k - Ebits | y >> 16 + Ebits - k;
  1607. goto ret_d;
  1608. }
  1609. z = xa > xa0 ? *--xa : 0;
  1610. w = xa > xa0 ? *--xa : 0;
  1611. k -= Ebits + 16;
  1612. d0 = Exp_1 | y << k + 16 | z << k | w >> 16 - k;
  1613. y = xa > xa0 ? *--xa : 0;
  1614. d1 = w << k + 16 | y << k;
  1615. #endif
  1616. ret_d:
  1617. #ifdef VAX
  1618. word0(d) = d0 >> 16 | d0 << 16;
  1619. word1(d) = d1 >> 16 | d1 << 16;
  1620. #else
  1621. #undef d0
  1622. #undef d1
  1623. #endif
  1624. return dval(d);
  1625. }
  1626. static Bigint *
  1627. d2b(double d_, int *e, int *bits)
  1628. {
  1629. double_u d;
  1630. Bigint *b;
  1631. int de, k;
  1632. ULong *x, y, z;
  1633. #ifndef Sudden_Underflow
  1634. int i;
  1635. #endif
  1636. #ifdef VAX
  1637. ULong d0, d1;
  1638. #endif
  1639. dval(d) = d_;
  1640. #ifdef VAX
  1641. d0 = word0(d) >> 16 | word0(d) << 16;
  1642. d1 = word1(d) >> 16 | word1(d) << 16;
  1643. #else
  1644. #define d0 word0(d)
  1645. #define d1 word1(d)
  1646. #endif
  1647. #ifdef Pack_32
  1648. b = Balloc(1);
  1649. #else
  1650. b = Balloc(2);
  1651. #endif
  1652. x = b->x;
  1653. z = d0 & Frac_mask;
  1654. d0 &= 0x7fffffff; /* clear sign bit, which we ignore */
  1655. #ifdef Sudden_Underflow
  1656. de = (int)(d0 >> Exp_shift);
  1657. #ifndef IBM
  1658. z |= Exp_msk11;
  1659. #endif
  1660. #else
  1661. if ((de = (int)(d0 >> Exp_shift)) != 0)
  1662. z |= Exp_msk1;
  1663. #endif
  1664. #ifdef Pack_32
  1665. if ((y = d1) != 0) {
  1666. if ((k = lo0bits(&y)) != 0) {
  1667. x[0] = y | z << (32 - k);
  1668. z >>= k;
  1669. }
  1670. else
  1671. x[0] = y;
  1672. #ifndef Sudden_Underflow
  1673. i =
  1674. #endif
  1675. b->wds = (x[1] = z) ? 2 : 1;
  1676. }
  1677. else {
  1678. #ifdef DEBUG
  1679. if (!z)
  1680. Bug("Zero passed to d2b");
  1681. #endif
  1682. k = lo0bits(&z);
  1683. x[0] = z;
  1684. #ifndef Sudden_Underflow
  1685. i =
  1686. #endif
  1687. b->wds = 1;
  1688. k += 32;
  1689. }
  1690. #else
  1691. if (y = d1) {
  1692. if (k = lo0bits(&y))
  1693. if (k >= 16) {
  1694. x[0] = y | z << 32 - k & 0xffff;
  1695. x[1] = z >> k - 16 & 0xffff;
  1696. x[2] = z >> k;
  1697. i = 2;
  1698. }
  1699. else {
  1700. x[0] = y & 0xffff;
  1701. x[1] = y >> 16 | z << 16 - k & 0xffff;
  1702. x[2] = z >> k & 0xffff;
  1703. x[3] = z >> k+16;
  1704. i = 3;
  1705. }
  1706. else {
  1707. x[0] = y & 0xffff;
  1708. x[1] = y >> 16;
  1709. x[2] = z & 0xffff;
  1710. x[3] = z >> 16;
  1711. i = 3;
  1712. }
  1713. }
  1714. else {
  1715. #ifdef DEBUG
  1716. if (!z)
  1717. Bug("Zero passed to d2b");
  1718. #endif
  1719. k = lo0bits(&z);
  1720. if (k >= 16) {
  1721. x[0] = z;
  1722. i = 0;
  1723. }
  1724. else {
  1725. x[0] = z & 0xffff;
  1726. x[1] = z >> 16;
  1727. i = 1;
  1728. }
  1729. k += 32;
  1730. }
  1731. while (!x[i])
  1732. --i;
  1733. b->wds = i + 1;
  1734. #endif
  1735. #ifndef Sudden_Underflow
  1736. if (de) {
  1737. #endif
  1738. #ifdef IBM
  1739. *e = (de - Bias - (P-1) << 2) + k;
  1740. *bits = 4*P + 8 - k - hi0bits(word0(d) & Frac_mask);
  1741. #else
  1742. *e = de - Bias - (P-1) + k;
  1743. *bits = P - k;
  1744. #endif
  1745. #ifndef Sudden_Underflow
  1746. }
  1747. else {
  1748. *e = de - Bias - (P-1) + 1 + k;
  1749. #ifdef Pack_32
  1750. *bits = 32*i - hi0bits(x[i-1]);
  1751. #else
  1752. *bits = (i+2)*16 - hi0bits(x[i]);
  1753. #endif
  1754. }
  1755. #endif
  1756. return b;
  1757. }
  1758. #undef d0
  1759. #undef d1
  1760. static double
  1761. ratio(Bigint *a, Bigint *b)
  1762. {
  1763. double_u da, db;
  1764. int k, ka, kb;
  1765. dval(da) = b2d(a, &ka);
  1766. dval(db) = b2d(b, &kb);
  1767. #ifdef Pack_32
  1768. k = ka - kb + 32*(a->wds - b->wds);
  1769. #else
  1770. k = ka - kb + 16*(a->wds - b->wds);
  1771. #endif
  1772. #ifdef IBM
  1773. if (k > 0) {
  1774. word0(da) += (k >> 2)*Exp_msk1;
  1775. if (k &= 3)
  1776. dval(da) *= 1 << k;
  1777. }
  1778. else {
  1779. k = -k;
  1780. word0(db) += (k >> 2)*Exp_msk1;
  1781. if (k &= 3)
  1782. dval(db) *= 1 << k;
  1783. }
  1784. #else
  1785. if (k > 0)
  1786. word0(da) += k*Exp_msk1;
  1787. else {
  1788. k = -k;
  1789. word0(db) += k*Exp_msk1;
  1790. }
  1791. #endif
  1792. return dval(da) / dval(db);
  1793. }
  1794. static const double
  1795. tens[] = {
  1796. 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9,
  1797. 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19,
  1798. 1e20, 1e21, 1e22
  1799. #ifdef VAX
  1800. , 1e23, 1e24
  1801. #endif
  1802. };
  1803. static const double
  1804. #ifdef IEEE_Arith
  1805. bigtens[] = { 1e16, 1e32, 1e64, 1e128, 1e256 };
  1806. static const double tinytens[] = { 1e-16, 1e-32, 1e-64, 1e-128,
  1807. #ifdef Avoid_Underflow
  1808. 9007199254740992.*9007199254740992.e-256
  1809. /* = 2^106 * 1e-53 */
  1810. #else
  1811. 1e-256
  1812. #endif
  1813. };
  1814. /* The fact…

Large files files are truncated, but you can click here to view the full file