|
Ruby
2.0.0p353(2013-11-22revision43784)
|
00001 /********************************************************************** 00002 00003 util.c - 00004 00005 $Author: nagachika $ 00006 created at: Fri Mar 10 17:22:34 JST 1995 00007 00008 Copyright (C) 1993-2008 Yukihiro Matsumoto 00009 00010 **********************************************************************/ 00011 00012 #include "ruby/ruby.h" 00013 #include "internal.h" 00014 00015 #include <ctype.h> 00016 #include <stdio.h> 00017 #include <errno.h> 00018 #include <math.h> 00019 #include <float.h> 00020 00021 #ifdef _WIN32 00022 #include "missing/file.h" 00023 #endif 00024 00025 #include "ruby/util.h" 00026 00027 unsigned long 00028 ruby_scan_oct(const char *start, size_t len, size_t *retlen) 00029 { 00030 register const char *s = start; 00031 register unsigned long retval = 0; 00032 00033 while (len-- && *s >= '0' && *s <= '7') { 00034 retval <<= 3; 00035 retval |= *s++ - '0'; 00036 } 00037 *retlen = (int)(s - start); /* less than len */ 00038 return retval; 00039 } 00040 00041 unsigned long 00042 ruby_scan_hex(const char *start, size_t len, size_t *retlen) 00043 { 00044 static const char hexdigit[] = "0123456789abcdef0123456789ABCDEF"; 00045 register const char *s = start; 00046 register unsigned long retval = 0; 00047 const char *tmp; 00048 00049 while (len-- && *s && (tmp = strchr(hexdigit, *s))) { 00050 retval <<= 4; 00051 retval |= (tmp - hexdigit) & 15; 00052 s++; 00053 } 00054 *retlen = (int)(s - start); /* less than len */ 00055 return retval; 00056 } 00057 00058 static unsigned long 00059 scan_digits(const char *str, int base, size_t *retlen, int *overflow) 00060 { 00061 static signed char table[] = { 00062 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */ 00063 /*0*/ -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, 00064 /*1*/ -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, 00065 /*2*/ -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, 00066 /*3*/ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,-1,-1,-1,-1,-1,-1, 00067 /*4*/ -1,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24, 00068 /*5*/ 25,26,27,28,29,30,31,32,33,34,35,-1,-1,-1,-1,-1, 00069 /*6*/ -1,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24, 00070 /*7*/ 25,26,27,28,29,30,31,32,33,34,35,-1,-1,-1,-1,-1, 00071 /*8*/ -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, 00072 /*9*/ -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, 00073 /*a*/ -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, 00074 /*b*/ -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, 00075 /*c*/ -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, 00076 /*d*/ -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, 00077 /*e*/ -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, 00078 /*f*/ -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, 00079 }; 00080 00081 const char *start = str; 00082 unsigned long ret = 0, x; 00083 unsigned long mul_overflow = (~(unsigned long)0) / base; 00084 int c; 00085 *overflow = 0; 00086 00087 while ((c = (unsigned char)*str++) != '\0') { 00088 int d = table[c]; 00089 if (d == -1 || base <= d) { 00090 *retlen = (str-1) - start; 00091 return ret; 00092 } 00093 if (mul_overflow < ret) 00094 *overflow = 1; 00095 ret *= base; 00096 x = ret; 00097 ret += d; 00098 if (ret < x) 00099 *overflow = 1; 00100 } 00101 *retlen = (str-1) - start; 00102 return ret; 00103 } 00104 00105 unsigned long 00106 ruby_strtoul(const char *str, char **endptr, int base) 00107 { 00108 int c, b, overflow; 00109 int sign = 0; 00110 size_t len; 00111 unsigned long ret; 00112 const char *subject_found = str; 00113 00114 if (base == 1 || 36 < base) { 00115 errno = EINVAL; 00116 return 0; 00117 } 00118 00119 while ((c = *str) && ISSPACE(c)) 00120 str++; 00121 00122 if (c == '+') { 00123 sign = 1; 00124 str++; 00125 } 00126 else if (c == '-') { 00127 sign = -1; 00128 str++; 00129 } 00130 00131 if (str[0] == '0') { 00132 subject_found = str+1; 00133 if (base == 0 || base == 16) { 00134 if (str[1] == 'x' || str[1] == 'X') { 00135 b = 16; 00136 str += 2; 00137 } 00138 else { 00139 b = base == 0 ? 8 : 16; 00140 str++; 00141 } 00142 } 00143 else { 00144 b = base; 00145 str++; 00146 } 00147 } 00148 else { 00149 b = base == 0 ? 10 : base; 00150 } 00151 00152 ret = scan_digits(str, b, &len, &overflow); 00153 00154 if (0 < len) 00155 subject_found = str+len; 00156 00157 if (endptr) 00158 *endptr = (char*)subject_found; 00159 00160 if (overflow) { 00161 errno = ERANGE; 00162 return ULONG_MAX; 00163 } 00164 00165 if (sign < 0) { 00166 ret = (unsigned long)(-(long)ret); 00167 return ret; 00168 } 00169 else { 00170 return ret; 00171 } 00172 } 00173 00174 #include <sys/types.h> 00175 #include <sys/stat.h> 00176 #ifdef HAVE_UNISTD_H 00177 #include <unistd.h> 00178 #endif 00179 #if defined(HAVE_FCNTL_H) 00180 #include <fcntl.h> 00181 #endif 00182 00183 #ifndef S_ISDIR 00184 # define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR) 00185 #endif 00186 00187 00188 /* mm.c */ 00189 00190 #define mmtype long 00191 #define mmcount (16 / SIZEOF_LONG) 00192 #define A ((mmtype*)a) 00193 #define B ((mmtype*)b) 00194 #define C ((mmtype*)c) 00195 #define D ((mmtype*)d) 00196 00197 #define mmstep (sizeof(mmtype) * mmcount) 00198 #define mmprepare(base, size) do {\ 00199 if (((VALUE)(base) % sizeof(mmtype)) == 0 && ((size) % sizeof(mmtype)) == 0) \ 00200 if ((size) >= mmstep) mmkind = 1;\ 00201 else mmkind = 0;\ 00202 else mmkind = -1;\ 00203 high = ((size) / mmstep) * mmstep;\ 00204 low = ((size) % mmstep);\ 00205 } while (0)\ 00206 00207 #define mmarg mmkind, size, high, low 00208 #define mmargdecl int mmkind, size_t size, size_t high, size_t low 00209 00210 static void mmswap_(register char *a, register char *b, mmargdecl) 00211 { 00212 if (a == b) return; 00213 if (mmkind >= 0) { 00214 register mmtype s; 00215 #if mmcount > 1 00216 if (mmkind > 0) { 00217 register char *t = a + high; 00218 do { 00219 s = A[0]; A[0] = B[0]; B[0] = s; 00220 s = A[1]; A[1] = B[1]; B[1] = s; 00221 #if mmcount > 2 00222 s = A[2]; A[2] = B[2]; B[2] = s; 00223 #if mmcount > 3 00224 s = A[3]; A[3] = B[3]; B[3] = s; 00225 #endif 00226 #endif 00227 a += mmstep; b += mmstep; 00228 } while (a < t); 00229 } 00230 #endif 00231 if (low != 0) { s = A[0]; A[0] = B[0]; B[0] = s; 00232 #if mmcount > 2 00233 if (low >= 2 * sizeof(mmtype)) { s = A[1]; A[1] = B[1]; B[1] = s; 00234 #if mmcount > 3 00235 if (low >= 3 * sizeof(mmtype)) {s = A[2]; A[2] = B[2]; B[2] = s;} 00236 #endif 00237 } 00238 #endif 00239 } 00240 } 00241 else { 00242 register char *t = a + size, s; 00243 do {s = *a; *a++ = *b; *b++ = s;} while (a < t); 00244 } 00245 } 00246 #define mmswap(a,b) mmswap_((a),(b),mmarg) 00247 00248 /* a, b, c = b, c, a */ 00249 static void mmrot3_(register char *a, register char *b, register char *c, mmargdecl) 00250 { 00251 if (mmkind >= 0) { 00252 register mmtype s; 00253 #if mmcount > 1 00254 if (mmkind > 0) { 00255 register char *t = a + high; 00256 do { 00257 s = A[0]; A[0] = B[0]; B[0] = C[0]; C[0] = s; 00258 s = A[1]; A[1] = B[1]; B[1] = C[1]; C[1] = s; 00259 #if mmcount > 2 00260 s = A[2]; A[2] = B[2]; B[2] = C[2]; C[2] = s; 00261 #if mmcount > 3 00262 s = A[3]; A[3] = B[3]; B[3] = C[3]; C[3] = s; 00263 #endif 00264 #endif 00265 a += mmstep; b += mmstep; c += mmstep; 00266 } while (a < t); 00267 } 00268 #endif 00269 if (low != 0) { s = A[0]; A[0] = B[0]; B[0] = C[0]; C[0] = s; 00270 #if mmcount > 2 00271 if (low >= 2 * sizeof(mmtype)) { s = A[1]; A[1] = B[1]; B[1] = C[1]; C[1] = s; 00272 #if mmcount > 3 00273 if (low == 3 * sizeof(mmtype)) {s = A[2]; A[2] = B[2]; B[2] = C[2]; C[2] = s;} 00274 #endif 00275 } 00276 #endif 00277 } 00278 } 00279 else { 00280 register char *t = a + size, s; 00281 do {s = *a; *a++ = *b; *b++ = *c; *c++ = s;} while (a < t); 00282 } 00283 } 00284 #define mmrot3(a,b,c) mmrot3_((a),(b),(c),mmarg) 00285 00286 /* qs6.c */ 00287 /*****************************************************/ 00288 /* */ 00289 /* qs6 (Quick sort function) */ 00290 /* */ 00291 /* by Tomoyuki Kawamura 1995.4.21 */ 00292 /* kawamura@tokuyama.ac.jp */ 00293 /*****************************************************/ 00294 00295 typedef struct { char *LL, *RR; } stack_node; /* Stack structure for L,l,R,r */ 00296 #define PUSH(ll,rr) do { top->LL = (ll); top->RR = (rr); ++top; } while (0) /* Push L,l,R,r */ 00297 #define POP(ll,rr) do { --top; (ll) = top->LL; (rr) = top->RR; } while (0) /* Pop L,l,R,r */ 00298 00299 #define med3(a,b,c) ((*cmp)((a),(b),d)<0 ? \ 00300 ((*cmp)((b),(c),d)<0 ? (b) : ((*cmp)((a),(c),d)<0 ? (c) : (a))) : \ 00301 ((*cmp)((b),(c),d)>0 ? (b) : ((*cmp)((a),(c),d)<0 ? (a) : (c)))) 00302 00303 typedef int (cmpfunc_t)(const void*, const void*, void*); 00304 void 00305 ruby_qsort(void* base, const size_t nel, const size_t size, cmpfunc_t *cmp, void *d) 00306 { 00307 register char *l, *r, *m; /* l,r:left,right group m:median point */ 00308 register int t, eq_l, eq_r; /* eq_l: all items in left group are equal to S */ 00309 char *L = base; /* left end of current region */ 00310 char *R = (char*)base + size*(nel-1); /* right end of current region */ 00311 size_t chklim = 63; /* threshold of ordering element check */ 00312 stack_node stack[32], *top = stack; /* 32 is enough for 32bit CPU */ 00313 int mmkind; 00314 size_t high, low, n; 00315 00316 if (nel <= 1) return; /* need not to sort */ 00317 mmprepare(base, size); 00318 goto start; 00319 00320 nxt: 00321 if (stack == top) return; /* return if stack is empty */ 00322 POP(L,R); 00323 00324 for (;;) { 00325 start: 00326 if (L + size == R) { /* 2 elements */ 00327 if ((*cmp)(L,R,d) > 0) mmswap(L,R); goto nxt; 00328 } 00329 00330 l = L; r = R; 00331 n = (r - l + size) / size; /* number of elements */ 00332 m = l + size * (n >> 1); /* calculate median value */ 00333 00334 if (n >= 60) { 00335 register char *m1; 00336 register char *m3; 00337 if (n >= 200) { 00338 n = size*(n>>3); /* number of bytes in splitting 8 */ 00339 { 00340 register char *p1 = l + n; 00341 register char *p2 = p1 + n; 00342 register char *p3 = p2 + n; 00343 m1 = med3(p1, p2, p3); 00344 p1 = m + n; 00345 p2 = p1 + n; 00346 p3 = p2 + n; 00347 m3 = med3(p1, p2, p3); 00348 } 00349 } 00350 else { 00351 n = size*(n>>2); /* number of bytes in splitting 4 */ 00352 m1 = l + n; 00353 m3 = m + n; 00354 } 00355 m = med3(m1, m, m3); 00356 } 00357 00358 if ((t = (*cmp)(l,m,d)) < 0) { /*3-5-?*/ 00359 if ((t = (*cmp)(m,r,d)) < 0) { /*3-5-7*/ 00360 if (chklim && nel >= chklim) { /* check if already ascending order */ 00361 char *p; 00362 chklim = 0; 00363 for (p=l; p<r; p+=size) if ((*cmp)(p,p+size,d) > 0) goto fail; 00364 goto nxt; 00365 } 00366 fail: goto loopA; /*3-5-7*/ 00367 } 00368 if (t > 0) { 00369 if ((*cmp)(l,r,d) <= 0) {mmswap(m,r); goto loopA;} /*3-5-4*/ 00370 mmrot3(r,m,l); goto loopA; /*3-5-2*/ 00371 } 00372 goto loopB; /*3-5-5*/ 00373 } 00374 00375 if (t > 0) { /*7-5-?*/ 00376 if ((t = (*cmp)(m,r,d)) > 0) { /*7-5-3*/ 00377 if (chklim && nel >= chklim) { /* check if already ascending order */ 00378 char *p; 00379 chklim = 0; 00380 for (p=l; p<r; p+=size) if ((*cmp)(p,p+size,d) < 0) goto fail2; 00381 while (l<r) {mmswap(l,r); l+=size; r-=size;} /* reverse region */ 00382 goto nxt; 00383 } 00384 fail2: mmswap(l,r); goto loopA; /*7-5-3*/ 00385 } 00386 if (t < 0) { 00387 if ((*cmp)(l,r,d) <= 0) {mmswap(l,m); goto loopB;} /*7-5-8*/ 00388 mmrot3(l,m,r); goto loopA; /*7-5-6*/ 00389 } 00390 mmswap(l,r); goto loopA; /*7-5-5*/ 00391 } 00392 00393 if ((t = (*cmp)(m,r,d)) < 0) {goto loopA;} /*5-5-7*/ 00394 if (t > 0) {mmswap(l,r); goto loopB;} /*5-5-3*/ 00395 00396 /* determining splitting type in case 5-5-5 */ /*5-5-5*/ 00397 for (;;) { 00398 if ((l += size) == r) goto nxt; /*5-5-5*/ 00399 if (l == m) continue; 00400 if ((t = (*cmp)(l,m,d)) > 0) {mmswap(l,r); l = L; goto loopA;}/*575-5*/ 00401 if (t < 0) {mmswap(L,l); l = L; goto loopB;} /*535-5*/ 00402 } 00403 00404 loopA: eq_l = 1; eq_r = 1; /* splitting type A */ /* left <= median < right */ 00405 for (;;) { 00406 for (;;) { 00407 if ((l += size) == r) 00408 {l -= size; if (l != m) mmswap(m,l); l -= size; goto fin;} 00409 if (l == m) continue; 00410 if ((t = (*cmp)(l,m,d)) > 0) {eq_r = 0; break;} 00411 if (t < 0) eq_l = 0; 00412 } 00413 for (;;) { 00414 if (l == (r -= size)) 00415 {l -= size; if (l != m) mmswap(m,l); l -= size; goto fin;} 00416 if (r == m) {m = l; break;} 00417 if ((t = (*cmp)(r,m,d)) < 0) {eq_l = 0; break;} 00418 if (t == 0) break; 00419 } 00420 mmswap(l,r); /* swap left and right */ 00421 } 00422 00423 loopB: eq_l = 1; eq_r = 1; /* splitting type B */ /* left < median <= right */ 00424 for (;;) { 00425 for (;;) { 00426 if (l == (r -= size)) 00427 {r += size; if (r != m) mmswap(r,m); r += size; goto fin;} 00428 if (r == m) continue; 00429 if ((t = (*cmp)(r,m,d)) < 0) {eq_l = 0; break;} 00430 if (t > 0) eq_r = 0; 00431 } 00432 for (;;) { 00433 if ((l += size) == r) 00434 {r += size; if (r != m) mmswap(r,m); r += size; goto fin;} 00435 if (l == m) {m = r; break;} 00436 if ((t = (*cmp)(l,m,d)) > 0) {eq_r = 0; break;} 00437 if (t == 0) break; 00438 } 00439 mmswap(l,r); /* swap left and right */ 00440 } 00441 00442 fin: 00443 if (eq_l == 0) /* need to sort left side */ 00444 if (eq_r == 0) /* need to sort right side */ 00445 if (l-L < R-r) {PUSH(r,R); R = l;} /* sort left side first */ 00446 else {PUSH(L,l); L = r;} /* sort right side first */ 00447 else R = l; /* need to sort left side only */ 00448 else if (eq_r == 0) L = r; /* need to sort right side only */ 00449 else goto nxt; /* need not to sort both sides */ 00450 } 00451 } 00452 00453 char * 00454 ruby_strdup(const char *str) 00455 { 00456 char *tmp; 00457 size_t len = strlen(str) + 1; 00458 00459 tmp = xmalloc(len); 00460 memcpy(tmp, str, len); 00461 00462 return tmp; 00463 } 00464 00465 #ifdef __native_client__ 00466 char * 00467 ruby_getcwd(void) 00468 { 00469 char *buf = xmalloc(2); 00470 strcpy(buf, "."); 00471 return buf; 00472 } 00473 #else 00474 char * 00475 ruby_getcwd(void) 00476 { 00477 #ifdef HAVE_GETCWD 00478 int size = 200; 00479 char *buf = xmalloc(size); 00480 00481 while (!getcwd(buf, size)) { 00482 if (errno != ERANGE) { 00483 xfree(buf); 00484 rb_sys_fail("getcwd"); 00485 } 00486 size *= 2; 00487 buf = xrealloc(buf, size); 00488 } 00489 #else 00490 # ifndef PATH_MAX 00491 # define PATH_MAX 8192 00492 # endif 00493 char *buf = xmalloc(PATH_MAX+1); 00494 00495 if (!getwd(buf)) { 00496 xfree(buf); 00497 rb_sys_fail("getwd"); 00498 } 00499 #endif 00500 return buf; 00501 } 00502 #endif 00503 00504 /**************************************************************** 00505 * 00506 * The author of this software is David M. Gay. 00507 * 00508 * Copyright (c) 1991, 2000, 2001 by Lucent Technologies. 00509 * 00510 * Permission to use, copy, modify, and distribute this software for any 00511 * purpose without fee is hereby granted, provided that this entire notice 00512 * is included in all copies of any software which is or includes a copy 00513 * or modification of this software and in all copies of the supporting 00514 * documentation for such software. 00515 * 00516 * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED 00517 * WARRANTY. IN PARTICULAR, NEITHER THE AUTHOR NOR LUCENT MAKES ANY 00518 * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY 00519 * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE. 00520 * 00521 ***************************************************************/ 00522 00523 /* Please send bug reports to David M. Gay (dmg at acm dot org, 00524 * with " at " changed at "@" and " dot " changed to "."). */ 00525 00526 /* On a machine with IEEE extended-precision registers, it is 00527 * necessary to specify double-precision (53-bit) rounding precision 00528 * before invoking strtod or dtoa. If the machine uses (the equivalent 00529 * of) Intel 80x87 arithmetic, the call 00530 * _control87(PC_53, MCW_PC); 00531 * does this with many compilers. Whether this or another call is 00532 * appropriate depends on the compiler; for this to work, it may be 00533 * necessary to #include "float.h" or another system-dependent header 00534 * file. 00535 */ 00536 00537 /* strtod for IEEE-, VAX-, and IBM-arithmetic machines. 00538 * 00539 * This strtod returns a nearest machine number to the input decimal 00540 * string (or sets errno to ERANGE). With IEEE arithmetic, ties are 00541 * broken by the IEEE round-even rule. Otherwise ties are broken by 00542 * biased rounding (add half and chop). 00543 * 00544 * Inspired loosely by William D. Clinger's paper "How to Read Floating 00545 * Point Numbers Accurately" [Proc. ACM SIGPLAN '90, pp. 92-101]. 00546 * 00547 * Modifications: 00548 * 00549 * 1. We only require IEEE, IBM, or VAX double-precision 00550 * arithmetic (not IEEE double-extended). 00551 * 2. We get by with floating-point arithmetic in a case that 00552 * Clinger missed -- when we're computing d * 10^n 00553 * for a small integer d and the integer n is not too 00554 * much larger than 22 (the maximum integer k for which 00555 * we can represent 10^k exactly), we may be able to 00556 * compute (d*10^k) * 10^(e-k) with just one roundoff. 00557 * 3. Rather than a bit-at-a-time adjustment of the binary 00558 * result in the hard case, we use floating-point 00559 * arithmetic to determine the adjustment to within 00560 * one bit; only in really hard cases do we need to 00561 * compute a second residual. 00562 * 4. Because of 3., we don't need a large table of powers of 10 00563 * for ten-to-e (just some small tables, e.g. of 10^k 00564 * for 0 <= k <= 22). 00565 */ 00566 00567 /* 00568 * #define IEEE_LITTLE_ENDIAN for IEEE-arithmetic machines where the least 00569 * significant byte has the lowest address. 00570 * #define IEEE_BIG_ENDIAN for IEEE-arithmetic machines where the most 00571 * significant byte has the lowest address. 00572 * #define Long int on machines with 32-bit ints and 64-bit longs. 00573 * #define IBM for IBM mainframe-style floating-point arithmetic. 00574 * #define VAX for VAX-style floating-point arithmetic (D_floating). 00575 * #define No_leftright to omit left-right logic in fast floating-point 00576 * computation of dtoa. 00577 * #define Honor_FLT_ROUNDS if FLT_ROUNDS can assume the values 2 or 3 00578 * and strtod and dtoa should round accordingly. 00579 * #define Check_FLT_ROUNDS if FLT_ROUNDS can assume the values 2 or 3 00580 * and Honor_FLT_ROUNDS is not #defined. 00581 * #define RND_PRODQUOT to use rnd_prod and rnd_quot (assembly routines 00582 * that use extended-precision instructions to compute rounded 00583 * products and quotients) with IBM. 00584 * #define ROUND_BIASED for IEEE-format with biased rounding. 00585 * #define Inaccurate_Divide for IEEE-format with correctly rounded 00586 * products but inaccurate quotients, e.g., for Intel i860. 00587 * #define NO_LONG_LONG on machines that do not have a "long long" 00588 * integer type (of >= 64 bits). On such machines, you can 00589 * #define Just_16 to store 16 bits per 32-bit Long when doing 00590 * high-precision integer arithmetic. Whether this speeds things 00591 * up or slows things down depends on the machine and the number 00592 * being converted. If long long is available and the name is 00593 * something other than "long long", #define Llong to be the name, 00594 * and if "unsigned Llong" does not work as an unsigned version of 00595 * Llong, #define #ULLong to be the corresponding unsigned type. 00596 * #define KR_headers for old-style C function headers. 00597 * #define Bad_float_h if your system lacks a float.h or if it does not 00598 * define some or all of DBL_DIG, DBL_MAX_10_EXP, DBL_MAX_EXP, 00599 * FLT_RADIX, FLT_ROUNDS, and DBL_MAX. 00600 * #define MALLOC your_malloc, where your_malloc(n) acts like malloc(n) 00601 * if memory is available and otherwise does something you deem 00602 * appropriate. If MALLOC is undefined, malloc will be invoked 00603 * directly -- and assumed always to succeed. 00604 * #define Omit_Private_Memory to omit logic (added Jan. 1998) for making 00605 * memory allocations from a private pool of memory when possible. 00606 * When used, the private pool is PRIVATE_MEM bytes long: 2304 bytes, 00607 * unless #defined to be a different length. This default length 00608 * suffices to get rid of MALLOC calls except for unusual cases, 00609 * such as decimal-to-binary conversion of a very long string of 00610 * digits. The longest string dtoa can return is about 751 bytes 00611 * long. For conversions by strtod of strings of 800 digits and 00612 * all dtoa conversions in single-threaded executions with 8-byte 00613 * pointers, PRIVATE_MEM >= 7400 appears to suffice; with 4-byte 00614 * pointers, PRIVATE_MEM >= 7112 appears adequate. 00615 * #define INFNAN_CHECK on IEEE systems to cause strtod to check for 00616 * Infinity and NaN (case insensitively). On some systems (e.g., 00617 * some HP systems), it may be necessary to #define NAN_WORD0 00618 * appropriately -- to the most significant word of a quiet NaN. 00619 * (On HP Series 700/800 machines, -DNAN_WORD0=0x7ff40000 works.) 00620 * When INFNAN_CHECK is #defined and No_Hex_NaN is not #defined, 00621 * strtod also accepts (case insensitively) strings of the form 00622 * NaN(x), where x is a string of hexadecimal digits and spaces; 00623 * if there is only one string of hexadecimal digits, it is taken 00624 * for the 52 fraction bits of the resulting NaN; if there are two 00625 * or more strings of hex digits, the first is for the high 20 bits, 00626 * the second and subsequent for the low 32 bits, with intervening 00627 * white space ignored; but if this results in none of the 52 00628 * fraction bits being on (an IEEE Infinity symbol), then NAN_WORD0 00629 * and NAN_WORD1 are used instead. 00630 * #define MULTIPLE_THREADS if the system offers preemptively scheduled 00631 * multiple threads. In this case, you must provide (or suitably 00632 * #define) two locks, acquired by ACQUIRE_DTOA_LOCK(n) and freed 00633 * by FREE_DTOA_LOCK(n) for n = 0 or 1. (The second lock, accessed 00634 * in pow5mult, ensures lazy evaluation of only one copy of high 00635 * powers of 5; omitting this lock would introduce a small 00636 * probability of wasting memory, but would otherwise be harmless.) 00637 * You must also invoke freedtoa(s) to free the value s returned by 00638 * dtoa. You may do so whether or not MULTIPLE_THREADS is #defined. 00639 * #define NO_IEEE_Scale to disable new (Feb. 1997) logic in strtod that 00640 * avoids underflows on inputs whose result does not underflow. 00641 * If you #define NO_IEEE_Scale on a machine that uses IEEE-format 00642 * floating-point numbers and flushes underflows to zero rather 00643 * than implementing gradual underflow, then you must also #define 00644 * Sudden_Underflow. 00645 * #define YES_ALIAS to permit aliasing certain double values with 00646 * arrays of ULongs. This leads to slightly better code with 00647 * some compilers and was always used prior to 19990916, but it 00648 * is not strictly legal and can cause trouble with aggressively 00649 * optimizing compilers (e.g., gcc 2.95.1 under -O2). 00650 * #define USE_LOCALE to use the current locale's decimal_point value. 00651 * #define SET_INEXACT if IEEE arithmetic is being used and extra 00652 * computation should be done to set the inexact flag when the 00653 * result is inexact and avoid setting inexact when the result 00654 * is exact. In this case, dtoa.c must be compiled in 00655 * an environment, perhaps provided by #include "dtoa.c" in a 00656 * suitable wrapper, that defines two functions, 00657 * int get_inexact(void); 00658 * void clear_inexact(void); 00659 * such that get_inexact() returns a nonzero value if the 00660 * inexact bit is already set, and clear_inexact() sets the 00661 * inexact bit to 0. When SET_INEXACT is #defined, strtod 00662 * also does extra computations to set the underflow and overflow 00663 * flags when appropriate (i.e., when the result is tiny and 00664 * inexact or when it is a numeric value rounded to +-infinity). 00665 * #define NO_ERRNO if strtod should not assign errno = ERANGE when 00666 * the result overflows to +-Infinity or underflows to 0. 00667 */ 00668 00669 #ifdef WORDS_BIGENDIAN 00670 #define IEEE_BIG_ENDIAN 00671 #else 00672 #define IEEE_LITTLE_ENDIAN 00673 #endif 00674 00675 #ifdef __vax__ 00676 #define VAX 00677 #undef IEEE_BIG_ENDIAN 00678 #undef IEEE_LITTLE_ENDIAN 00679 #endif 00680 00681 #if defined(__arm__) && !defined(__VFP_FP__) 00682 #define IEEE_BIG_ENDIAN 00683 #undef IEEE_LITTLE_ENDIAN 00684 #endif 00685 00686 #undef Long 00687 #undef ULong 00688 00689 #if SIZEOF_INT == 4 00690 #define Long int 00691 #define ULong unsigned int 00692 #elif SIZEOF_LONG == 4 00693 #define Long long int 00694 #define ULong unsigned long int 00695 #endif 00696 00697 #if HAVE_LONG_LONG 00698 #define Llong LONG_LONG 00699 #endif 00700 00701 #ifdef DEBUG 00702 #include "stdio.h" 00703 #define Bug(x) {fprintf(stderr, "%s\n", (x)); exit(EXIT_FAILURE);} 00704 #endif 00705 00706 #include "stdlib.h" 00707 #include "string.h" 00708 00709 #ifdef USE_LOCALE 00710 #include "locale.h" 00711 #endif 00712 00713 #ifdef MALLOC 00714 extern void *MALLOC(size_t); 00715 #else 00716 #define MALLOC malloc 00717 #endif 00718 #ifdef FREE 00719 extern void FREE(void*); 00720 #else 00721 #define FREE free 00722 #endif 00723 00724 #ifndef Omit_Private_Memory 00725 #ifndef PRIVATE_MEM 00726 #define PRIVATE_MEM 2304 00727 #endif 00728 #define PRIVATE_mem ((PRIVATE_MEM+sizeof(double)-1)/sizeof(double)) 00729 static double private_mem[PRIVATE_mem], *pmem_next = private_mem; 00730 #endif 00731 00732 #undef IEEE_Arith 00733 #undef Avoid_Underflow 00734 #ifdef IEEE_BIG_ENDIAN 00735 #define IEEE_Arith 00736 #endif 00737 #ifdef IEEE_LITTLE_ENDIAN 00738 #define IEEE_Arith 00739 #endif 00740 00741 #ifdef Bad_float_h 00742 00743 #ifdef IEEE_Arith 00744 #define DBL_DIG 15 00745 #define DBL_MAX_10_EXP 308 00746 #define DBL_MAX_EXP 1024 00747 #define FLT_RADIX 2 00748 #endif /*IEEE_Arith*/ 00749 00750 #ifdef IBM 00751 #define DBL_DIG 16 00752 #define DBL_MAX_10_EXP 75 00753 #define DBL_MAX_EXP 63 00754 #define FLT_RADIX 16 00755 #define DBL_MAX 7.2370055773322621e+75 00756 #endif 00757 00758 #ifdef VAX 00759 #define DBL_DIG 16 00760 #define DBL_MAX_10_EXP 38 00761 #define DBL_MAX_EXP 127 00762 #define FLT_RADIX 2 00763 #define DBL_MAX 1.7014118346046923e+38 00764 #endif 00765 00766 #ifndef LONG_MAX 00767 #define LONG_MAX 2147483647 00768 #endif 00769 00770 #else /* ifndef Bad_float_h */ 00771 #include "float.h" 00772 #endif /* Bad_float_h */ 00773 00774 #ifndef __MATH_H__ 00775 #include "math.h" 00776 #endif 00777 00778 #ifdef __cplusplus 00779 extern "C" { 00780 #if 0 00781 } /* satisfy cc-mode */ 00782 #endif 00783 #endif 00784 00785 #if defined(IEEE_LITTLE_ENDIAN) + defined(IEEE_BIG_ENDIAN) + defined(VAX) + defined(IBM) != 1 00786 Exactly one of IEEE_LITTLE_ENDIAN, IEEE_BIG_ENDIAN, VAX, or IBM should be defined. 00787 #endif 00788 00789 typedef union { double d; ULong L[2]; } U; 00790 00791 #ifdef YES_ALIAS 00792 typedef double double_u; 00793 # define dval(x) (x) 00794 # ifdef IEEE_LITTLE_ENDIAN 00795 # define word0(x) (((ULong *)&(x))[1]) 00796 # define word1(x) (((ULong *)&(x))[0]) 00797 # else 00798 # define word0(x) (((ULong *)&(x))[0]) 00799 # define word1(x) (((ULong *)&(x))[1]) 00800 # endif 00801 #else 00802 typedef U double_u; 00803 # ifdef IEEE_LITTLE_ENDIAN 00804 # define word0(x) ((x).L[1]) 00805 # define word1(x) ((x).L[0]) 00806 # else 00807 # define word0(x) ((x).L[0]) 00808 # define word1(x) ((x).L[1]) 00809 # endif 00810 # define dval(x) ((x).d) 00811 #endif 00812 00813 /* The following definition of Storeinc is appropriate for MIPS processors. 00814 * An alternative that might be better on some machines is 00815 * #define Storeinc(a,b,c) (*a++ = b << 16 | c & 0xffff) 00816 */ 00817 #if defined(IEEE_LITTLE_ENDIAN) + defined(VAX) + defined(__arm__) 00818 #define Storeinc(a,b,c) (((unsigned short *)(a))[1] = (unsigned short)(b), \ 00819 ((unsigned short *)(a))[0] = (unsigned short)(c), (a)++) 00820 #else 00821 #define Storeinc(a,b,c) (((unsigned short *)(a))[0] = (unsigned short)(b), \ 00822 ((unsigned short *)(a))[1] = (unsigned short)(c), (a)++) 00823 #endif 00824 00825 /* #define P DBL_MANT_DIG */ 00826 /* Ten_pmax = floor(P*log(2)/log(5)) */ 00827 /* Bletch = (highest power of 2 < DBL_MAX_10_EXP) / 16 */ 00828 /* Quick_max = floor((P-1)*log(FLT_RADIX)/log(10) - 1) */ 00829 /* Int_max = floor(P*log(FLT_RADIX)/log(10) - 1) */ 00830 00831 #ifdef IEEE_Arith 00832 #define Exp_shift 20 00833 #define Exp_shift1 20 00834 #define Exp_msk1 0x100000 00835 #define Exp_msk11 0x100000 00836 #define Exp_mask 0x7ff00000 00837 #define P 53 00838 #define Bias 1023 00839 #define Emin (-1022) 00840 #define Exp_1 0x3ff00000 00841 #define Exp_11 0x3ff00000 00842 #define Ebits 11 00843 #define Frac_mask 0xfffff 00844 #define Frac_mask1 0xfffff 00845 #define Ten_pmax 22 00846 #define Bletch 0x10 00847 #define Bndry_mask 0xfffff 00848 #define Bndry_mask1 0xfffff 00849 #define LSB 1 00850 #define Sign_bit 0x80000000 00851 #define Log2P 1 00852 #define Tiny0 0 00853 #define Tiny1 1 00854 #define Quick_max 14 00855 #define Int_max 14 00856 #ifndef NO_IEEE_Scale 00857 #define Avoid_Underflow 00858 #ifdef Flush_Denorm /* debugging option */ 00859 #undef Sudden_Underflow 00860 #endif 00861 #endif 00862 00863 #ifndef Flt_Rounds 00864 #ifdef FLT_ROUNDS 00865 #define Flt_Rounds FLT_ROUNDS 00866 #else 00867 #define Flt_Rounds 1 00868 #endif 00869 #endif /*Flt_Rounds*/ 00870 00871 #ifdef Honor_FLT_ROUNDS 00872 #define Rounding rounding 00873 #undef Check_FLT_ROUNDS 00874 #define Check_FLT_ROUNDS 00875 #else 00876 #define Rounding Flt_Rounds 00877 #endif 00878 00879 #else /* ifndef IEEE_Arith */ 00880 #undef Check_FLT_ROUNDS 00881 #undef Honor_FLT_ROUNDS 00882 #undef SET_INEXACT 00883 #undef Sudden_Underflow 00884 #define Sudden_Underflow 00885 #ifdef IBM 00886 #undef Flt_Rounds 00887 #define Flt_Rounds 0 00888 #define Exp_shift 24 00889 #define Exp_shift1 24 00890 #define Exp_msk1 0x1000000 00891 #define Exp_msk11 0x1000000 00892 #define Exp_mask 0x7f000000 00893 #define P 14 00894 #define Bias 65 00895 #define Exp_1 0x41000000 00896 #define Exp_11 0x41000000 00897 #define Ebits 8 /* exponent has 7 bits, but 8 is the right value in b2d */ 00898 #define Frac_mask 0xffffff 00899 #define Frac_mask1 0xffffff 00900 #define Bletch 4 00901 #define Ten_pmax 22 00902 #define Bndry_mask 0xefffff 00903 #define Bndry_mask1 0xffffff 00904 #define LSB 1 00905 #define Sign_bit 0x80000000 00906 #define Log2P 4 00907 #define Tiny0 0x100000 00908 #define Tiny1 0 00909 #define Quick_max 14 00910 #define Int_max 15 00911 #else /* VAX */ 00912 #undef Flt_Rounds 00913 #define Flt_Rounds 1 00914 #define Exp_shift 23 00915 #define Exp_shift1 7 00916 #define Exp_msk1 0x80 00917 #define Exp_msk11 0x800000 00918 #define Exp_mask 0x7f80 00919 #define P 56 00920 #define Bias 129 00921 #define Exp_1 0x40800000 00922 #define Exp_11 0x4080 00923 #define Ebits 8 00924 #define Frac_mask 0x7fffff 00925 #define Frac_mask1 0xffff007f 00926 #define Ten_pmax 24 00927 #define Bletch 2 00928 #define Bndry_mask 0xffff007f 00929 #define Bndry_mask1 0xffff007f 00930 #define LSB 0x10000 00931 #define Sign_bit 0x8000 00932 #define Log2P 1 00933 #define Tiny0 0x80 00934 #define Tiny1 0 00935 #define Quick_max 15 00936 #define Int_max 15 00937 #endif /* IBM, VAX */ 00938 #endif /* IEEE_Arith */ 00939 00940 #ifndef IEEE_Arith 00941 #define ROUND_BIASED 00942 #endif 00943 00944 #ifdef RND_PRODQUOT 00945 #define rounded_product(a,b) ((a) = rnd_prod((a), (b))) 00946 #define rounded_quotient(a,b) ((a) = rnd_quot((a), (b))) 00947 extern double rnd_prod(double, double), rnd_quot(double, double); 00948 #else 00949 #define rounded_product(a,b) ((a) *= (b)) 00950 #define rounded_quotient(a,b) ((a) /= (b)) 00951 #endif 00952 00953 #define Big0 (Frac_mask1 | Exp_msk1*(DBL_MAX_EXP+Bias-1)) 00954 #define Big1 0xffffffff 00955 00956 #ifndef Pack_32 00957 #define Pack_32 00958 #endif 00959 00960 #define FFFFFFFF 0xffffffffUL 00961 00962 #ifdef NO_LONG_LONG 00963 #undef ULLong 00964 #ifdef Just_16 00965 #undef Pack_32 00966 /* When Pack_32 is not defined, we store 16 bits per 32-bit Long. 00967 * This makes some inner loops simpler and sometimes saves work 00968 * during multiplications, but it often seems to make things slightly 00969 * slower. Hence the default is now to store 32 bits per Long. 00970 */ 00971 #endif 00972 #else /* long long available */ 00973 #ifndef Llong 00974 #define Llong long long 00975 #endif 00976 #ifndef ULLong 00977 #define ULLong unsigned Llong 00978 #endif 00979 #endif /* NO_LONG_LONG */ 00980 00981 #define MULTIPLE_THREADS 1 00982 00983 #ifndef MULTIPLE_THREADS 00984 #define ACQUIRE_DTOA_LOCK(n) /*nothing*/ 00985 #define FREE_DTOA_LOCK(n) /*nothing*/ 00986 #else 00987 #define ACQUIRE_DTOA_LOCK(n) /*unused right now*/ 00988 #define FREE_DTOA_LOCK(n) /*unused right now*/ 00989 #endif 00990 00991 #define Kmax 15 00992 00993 struct Bigint { 00994 struct Bigint *next; 00995 int k, maxwds, sign, wds; 00996 ULong x[1]; 00997 }; 00998 00999 typedef struct Bigint Bigint; 01000 01001 static Bigint *freelist[Kmax+1]; 01002 01003 static Bigint * 01004 Balloc(int k) 01005 { 01006 int x; 01007 Bigint *rv; 01008 #ifndef Omit_Private_Memory 01009 size_t len; 01010 #endif 01011 01012 ACQUIRE_DTOA_LOCK(0); 01013 if (k <= Kmax && (rv = freelist[k]) != 0) { 01014 freelist[k] = rv->next; 01015 } 01016 else { 01017 x = 1 << k; 01018 #ifdef Omit_Private_Memory 01019 rv = (Bigint *)MALLOC(sizeof(Bigint) + (x-1)*sizeof(ULong)); 01020 #else 01021 len = (sizeof(Bigint) + (x-1)*sizeof(ULong) + sizeof(double) - 1) 01022 /sizeof(double); 01023 if (k <= Kmax && pmem_next - private_mem + len <= PRIVATE_mem) { 01024 rv = (Bigint*)pmem_next; 01025 pmem_next += len; 01026 } 01027 else 01028 rv = (Bigint*)MALLOC(len*sizeof(double)); 01029 #endif 01030 rv->k = k; 01031 rv->maxwds = x; 01032 } 01033 FREE_DTOA_LOCK(0); 01034 rv->sign = rv->wds = 0; 01035 return rv; 01036 } 01037 01038 static void 01039 Bfree(Bigint *v) 01040 { 01041 if (v) { 01042 if (v->k > Kmax) { 01043 FREE(v); 01044 return; 01045 } 01046 ACQUIRE_DTOA_LOCK(0); 01047 v->next = freelist[v->k]; 01048 freelist[v->k] = v; 01049 FREE_DTOA_LOCK(0); 01050 } 01051 } 01052 01053 #define Bcopy(x,y) memcpy((char *)&(x)->sign, (char *)&(y)->sign, \ 01054 (y)->wds*sizeof(Long) + 2*sizeof(int)) 01055 01056 static Bigint * 01057 multadd(Bigint *b, int m, int a) /* multiply by m and add a */ 01058 { 01059 int i, wds; 01060 ULong *x; 01061 #ifdef ULLong 01062 ULLong carry, y; 01063 #else 01064 ULong carry, y; 01065 #ifdef Pack_32 01066 ULong xi, z; 01067 #endif 01068 #endif 01069 Bigint *b1; 01070 01071 wds = b->wds; 01072 x = b->x; 01073 i = 0; 01074 carry = a; 01075 do { 01076 #ifdef ULLong 01077 y = *x * (ULLong)m + carry; 01078 carry = y >> 32; 01079 *x++ = (ULong)(y & FFFFFFFF); 01080 #else 01081 #ifdef Pack_32 01082 xi = *x; 01083 y = (xi & 0xffff) * m + carry; 01084 z = (xi >> 16) * m + (y >> 16); 01085 carry = z >> 16; 01086 *x++ = (z << 16) + (y & 0xffff); 01087 #else 01088 y = *x * m + carry; 01089 carry = y >> 16; 01090 *x++ = y & 0xffff; 01091 #endif 01092 #endif 01093 } while (++i < wds); 01094 if (carry) { 01095 if (wds >= b->maxwds) { 01096 b1 = Balloc(b->k+1); 01097 Bcopy(b1, b); 01098 Bfree(b); 01099 b = b1; 01100 } 01101 b->x[wds++] = (ULong)carry; 01102 b->wds = wds; 01103 } 01104 return b; 01105 } 01106 01107 static Bigint * 01108 s2b(const char *s, int nd0, int nd, ULong y9) 01109 { 01110 Bigint *b; 01111 int i, k; 01112 Long x, y; 01113 01114 x = (nd + 8) / 9; 01115 for (k = 0, y = 1; x > y; y <<= 1, k++) ; 01116 #ifdef Pack_32 01117 b = Balloc(k); 01118 b->x[0] = y9; 01119 b->wds = 1; 01120 #else 01121 b = Balloc(k+1); 01122 b->x[0] = y9 & 0xffff; 01123 b->wds = (b->x[1] = y9 >> 16) ? 2 : 1; 01124 #endif 01125 01126 i = 9; 01127 if (9 < nd0) { 01128 s += 9; 01129 do { 01130 b = multadd(b, 10, *s++ - '0'); 01131 } while (++i < nd0); 01132 s++; 01133 } 01134 else 01135 s += 10; 01136 for (; i < nd; i++) 01137 b = multadd(b, 10, *s++ - '0'); 01138 return b; 01139 } 01140 01141 static int 01142 hi0bits(register ULong x) 01143 { 01144 register int k = 0; 01145 01146 if (!(x & 0xffff0000)) { 01147 k = 16; 01148 x <<= 16; 01149 } 01150 if (!(x & 0xff000000)) { 01151 k += 8; 01152 x <<= 8; 01153 } 01154 if (!(x & 0xf0000000)) { 01155 k += 4; 01156 x <<= 4; 01157 } 01158 if (!(x & 0xc0000000)) { 01159 k += 2; 01160 x <<= 2; 01161 } 01162 if (!(x & 0x80000000)) { 01163 k++; 01164 if (!(x & 0x40000000)) 01165 return 32; 01166 } 01167 return k; 01168 } 01169 01170 static int 01171 lo0bits(ULong *y) 01172 { 01173 register int k; 01174 register ULong x = *y; 01175 01176 if (x & 7) { 01177 if (x & 1) 01178 return 0; 01179 if (x & 2) { 01180 *y = x >> 1; 01181 return 1; 01182 } 01183 *y = x >> 2; 01184 return 2; 01185 } 01186 k = 0; 01187 if (!(x & 0xffff)) { 01188 k = 16; 01189 x >>= 16; 01190 } 01191 if (!(x & 0xff)) { 01192 k += 8; 01193 x >>= 8; 01194 } 01195 if (!(x & 0xf)) { 01196 k += 4; 01197 x >>= 4; 01198 } 01199 if (!(x & 0x3)) { 01200 k += 2; 01201 x >>= 2; 01202 } 01203 if (!(x & 1)) { 01204 k++; 01205 x >>= 1; 01206 if (!x) 01207 return 32; 01208 } 01209 *y = x; 01210 return k; 01211 } 01212 01213 static Bigint * 01214 i2b(int i) 01215 { 01216 Bigint *b; 01217 01218 b = Balloc(1); 01219 b->x[0] = i; 01220 b->wds = 1; 01221 return b; 01222 } 01223 01224 static Bigint * 01225 mult(Bigint *a, Bigint *b) 01226 { 01227 Bigint *c; 01228 int k, wa, wb, wc; 01229 ULong *x, *xa, *xae, *xb, *xbe, *xc, *xc0; 01230 ULong y; 01231 #ifdef ULLong 01232 ULLong carry, z; 01233 #else 01234 ULong carry, z; 01235 #ifdef Pack_32 01236 ULong z2; 01237 #endif 01238 #endif 01239 01240 if (a->wds < b->wds) { 01241 c = a; 01242 a = b; 01243 b = c; 01244 } 01245 k = a->k; 01246 wa = a->wds; 01247 wb = b->wds; 01248 wc = wa + wb; 01249 if (wc > a->maxwds) 01250 k++; 01251 c = Balloc(k); 01252 for (x = c->x, xa = x + wc; x < xa; x++) 01253 *x = 0; 01254 xa = a->x; 01255 xae = xa + wa; 01256 xb = b->x; 01257 xbe = xb + wb; 01258 xc0 = c->x; 01259 #ifdef ULLong 01260 for (; xb < xbe; xc0++) { 01261 if ((y = *xb++) != 0) { 01262 x = xa; 01263 xc = xc0; 01264 carry = 0; 01265 do { 01266 z = *x++ * (ULLong)y + *xc + carry; 01267 carry = z >> 32; 01268 *xc++ = (ULong)(z & FFFFFFFF); 01269 } while (x < xae); 01270 *xc = (ULong)carry; 01271 } 01272 } 01273 #else 01274 #ifdef Pack_32 01275 for (; xb < xbe; xb++, xc0++) { 01276 if (y = *xb & 0xffff) { 01277 x = xa; 01278 xc = xc0; 01279 carry = 0; 01280 do { 01281 z = (*x & 0xffff) * y + (*xc & 0xffff) + carry; 01282 carry = z >> 16; 01283 z2 = (*x++ >> 16) * y + (*xc >> 16) + carry; 01284 carry = z2 >> 16; 01285 Storeinc(xc, z2, z); 01286 } while (x < xae); 01287 *xc = (ULong)carry; 01288 } 01289 if (y = *xb >> 16) { 01290 x = xa; 01291 xc = xc0; 01292 carry = 0; 01293 z2 = *xc; 01294 do { 01295 z = (*x & 0xffff) * y + (*xc >> 16) + carry; 01296 carry = z >> 16; 01297 Storeinc(xc, z, z2); 01298 z2 = (*x++ >> 16) * y + (*xc & 0xffff) + carry; 01299 carry = z2 >> 16; 01300 } while (x < xae); 01301 *xc = z2; 01302 } 01303 } 01304 #else 01305 for (; xb < xbe; xc0++) { 01306 if (y = *xb++) { 01307 x = xa; 01308 xc = xc0; 01309 carry = 0; 01310 do { 01311 z = *x++ * y + *xc + carry; 01312 carry = z >> 16; 01313 *xc++ = z & 0xffff; 01314 } while (x < xae); 01315 *xc = (ULong)carry; 01316 } 01317 } 01318 #endif 01319 #endif 01320 for (xc0 = c->x, xc = xc0 + wc; wc > 0 && !*--xc; --wc) ; 01321 c->wds = wc; 01322 return c; 01323 } 01324 01325 static Bigint *p5s; 01326 01327 static Bigint * 01328 pow5mult(Bigint *b, int k) 01329 { 01330 Bigint *b1, *p5, *p51; 01331 int i; 01332 static int p05[3] = { 5, 25, 125 }; 01333 01334 if ((i = k & 3) != 0) 01335 b = multadd(b, p05[i-1], 0); 01336 01337 if (!(k >>= 2)) 01338 return b; 01339 if (!(p5 = p5s)) { 01340 /* first time */ 01341 #ifdef MULTIPLE_THREADS 01342 ACQUIRE_DTOA_LOCK(1); 01343 if (!(p5 = p5s)) { 01344 p5 = p5s = i2b(625); 01345 p5->next = 0; 01346 } 01347 FREE_DTOA_LOCK(1); 01348 #else 01349 p5 = p5s = i2b(625); 01350 p5->next = 0; 01351 #endif 01352 } 01353 for (;;) { 01354 if (k & 1) { 01355 b1 = mult(b, p5); 01356 Bfree(b); 01357 b = b1; 01358 } 01359 if (!(k >>= 1)) 01360 break; 01361 if (!(p51 = p5->next)) { 01362 #ifdef MULTIPLE_THREADS 01363 ACQUIRE_DTOA_LOCK(1); 01364 if (!(p51 = p5->next)) { 01365 p51 = p5->next = mult(p5,p5); 01366 p51->next = 0; 01367 } 01368 FREE_DTOA_LOCK(1); 01369 #else 01370 p51 = p5->next = mult(p5,p5); 01371 p51->next = 0; 01372 #endif 01373 } 01374 p5 = p51; 01375 } 01376 return b; 01377 } 01378 01379 static Bigint * 01380 lshift(Bigint *b, int k) 01381 { 01382 int i, k1, n, n1; 01383 Bigint *b1; 01384 ULong *x, *x1, *xe, z; 01385 01386 #ifdef Pack_32 01387 n = k >> 5; 01388 #else 01389 n = k >> 4; 01390 #endif 01391 k1 = b->k; 01392 n1 = n + b->wds + 1; 01393 for (i = b->maxwds; n1 > i; i <<= 1) 01394 k1++; 01395 b1 = Balloc(k1); 01396 x1 = b1->x; 01397 for (i = 0; i < n; i++) 01398 *x1++ = 0; 01399 x = b->x; 01400 xe = x + b->wds; 01401 #ifdef Pack_32 01402 if (k &= 0x1f) { 01403 k1 = 32 - k; 01404 z = 0; 01405 do { 01406 *x1++ = *x << k | z; 01407 z = *x++ >> k1; 01408 } while (x < xe); 01409 if ((*x1 = z) != 0) 01410 ++n1; 01411 } 01412 #else 01413 if (k &= 0xf) { 01414 k1 = 16 - k; 01415 z = 0; 01416 do { 01417 *x1++ = *x << k & 0xffff | z; 01418 z = *x++ >> k1; 01419 } while (x < xe); 01420 if (*x1 = z) 01421 ++n1; 01422 } 01423 #endif 01424 else 01425 do { 01426 *x1++ = *x++; 01427 } while (x < xe); 01428 b1->wds = n1 - 1; 01429 Bfree(b); 01430 return b1; 01431 } 01432 01433 static int 01434 cmp(Bigint *a, Bigint *b) 01435 { 01436 ULong *xa, *xa0, *xb, *xb0; 01437 int i, j; 01438 01439 i = a->wds; 01440 j = b->wds; 01441 #ifdef DEBUG 01442 if (i > 1 && !a->x[i-1]) 01443 Bug("cmp called with a->x[a->wds-1] == 0"); 01444 if (j > 1 && !b->x[j-1]) 01445 Bug("cmp called with b->x[b->wds-1] == 0"); 01446 #endif 01447 if (i -= j) 01448 return i; 01449 xa0 = a->x; 01450 xa = xa0 + j; 01451 xb0 = b->x; 01452 xb = xb0 + j; 01453 for (;;) { 01454 if (*--xa != *--xb) 01455 return *xa < *xb ? -1 : 1; 01456 if (xa <= xa0) 01457 break; 01458 } 01459 return 0; 01460 } 01461 01462 static Bigint * 01463 diff(Bigint *a, Bigint *b) 01464 { 01465 Bigint *c; 01466 int i, wa, wb; 01467 ULong *xa, *xae, *xb, *xbe, *xc; 01468 #ifdef ULLong 01469 ULLong borrow, y; 01470 #else 01471 ULong borrow, y; 01472 #ifdef Pack_32 01473 ULong z; 01474 #endif 01475 #endif 01476 01477 i = cmp(a,b); 01478 if (!i) { 01479 c = Balloc(0); 01480 c->wds = 1; 01481 c->x[0] = 0; 01482 return c; 01483 } 01484 if (i < 0) { 01485 c = a; 01486 a = b; 01487 b = c; 01488 i = 1; 01489 } 01490 else 01491 i = 0; 01492 c = Balloc(a->k); 01493 c->sign = i; 01494 wa = a->wds; 01495 xa = a->x; 01496 xae = xa + wa; 01497 wb = b->wds; 01498 xb = b->x; 01499 xbe = xb + wb; 01500 xc = c->x; 01501 borrow = 0; 01502 #ifdef ULLong 01503 do { 01504 y = (ULLong)*xa++ - *xb++ - borrow; 01505 borrow = y >> 32 & (ULong)1; 01506 *xc++ = (ULong)(y & FFFFFFFF); 01507 } while (xb < xbe); 01508 while (xa < xae) { 01509 y = *xa++ - borrow; 01510 borrow = y >> 32 & (ULong)1; 01511 *xc++ = (ULong)(y & FFFFFFFF); 01512 } 01513 #else 01514 #ifdef Pack_32 01515 do { 01516 y = (*xa & 0xffff) - (*xb & 0xffff) - borrow; 01517 borrow = (y & 0x10000) >> 16; 01518 z = (*xa++ >> 16) - (*xb++ >> 16) - borrow; 01519 borrow = (z & 0x10000) >> 16; 01520 Storeinc(xc, z, y); 01521 } while (xb < xbe); 01522 while (xa < xae) { 01523 y = (*xa & 0xffff) - borrow; 01524 borrow = (y & 0x10000) >> 16; 01525 z = (*xa++ >> 16) - borrow; 01526 borrow = (z & 0x10000) >> 16; 01527 Storeinc(xc, z, y); 01528 } 01529 #else 01530 do { 01531 y = *xa++ - *xb++ - borrow; 01532 borrow = (y & 0x10000) >> 16; 01533 *xc++ = y & 0xffff; 01534 } while (xb < xbe); 01535 while (xa < xae) { 01536 y = *xa++ - borrow; 01537 borrow = (y & 0x10000) >> 16; 01538 *xc++ = y & 0xffff; 01539 } 01540 #endif 01541 #endif 01542 while (!*--xc) 01543 wa--; 01544 c->wds = wa; 01545 return c; 01546 } 01547 01548 static double 01549 ulp(double x_) 01550 { 01551 register Long L; 01552 double_u x, a; 01553 dval(x) = x_; 01554 01555 L = (word0(x) & Exp_mask) - (P-1)*Exp_msk1; 01556 #ifndef Avoid_Underflow 01557 #ifndef Sudden_Underflow 01558 if (L > 0) { 01559 #endif 01560 #endif 01561 #ifdef IBM 01562 L |= Exp_msk1 >> 4; 01563 #endif 01564 word0(a) = L; 01565 word1(a) = 0; 01566 #ifndef Avoid_Underflow 01567 #ifndef Sudden_Underflow 01568 } 01569 else { 01570 L = -L >> Exp_shift; 01571 if (L < Exp_shift) { 01572 word0(a) = 0x80000 >> L; 01573 word1(a) = 0; 01574 } 01575 else { 01576 word0(a) = 0; 01577 L -= Exp_shift; 01578 word1(a) = L >= 31 ? 1 : 1 << 31 - L; 01579 } 01580 } 01581 #endif 01582 #endif 01583 return dval(a); 01584 } 01585 01586 static double 01587 b2d(Bigint *a, int *e) 01588 { 01589 ULong *xa, *xa0, w, y, z; 01590 int k; 01591 double_u d; 01592 #ifdef VAX 01593 ULong d0, d1; 01594 #else 01595 #define d0 word0(d) 01596 #define d1 word1(d) 01597 #endif 01598 01599 xa0 = a->x; 01600 xa = xa0 + a->wds; 01601 y = *--xa; 01602 #ifdef DEBUG 01603 if (!y) Bug("zero y in b2d"); 01604 #endif 01605 k = hi0bits(y); 01606 *e = 32 - k; 01607 #ifdef Pack_32 01608 if (k < Ebits) { 01609 d0 = Exp_1 | y >> (Ebits - k); 01610 w = xa > xa0 ? *--xa : 0; 01611 d1 = y << ((32-Ebits) + k) | w >> (Ebits - k); 01612 goto ret_d; 01613 } 01614 z = xa > xa0 ? *--xa : 0; 01615 if (k -= Ebits) { 01616 d0 = Exp_1 | y << k | z >> (32 - k); 01617 y = xa > xa0 ? *--xa : 0; 01618 d1 = z << k | y >> (32 - k); 01619 } 01620 else { 01621 d0 = Exp_1 | y; 01622 d1 = z; 01623 } 01624 #else 01625 if (k < Ebits + 16) { 01626 z = xa > xa0 ? *--xa : 0; 01627 d0 = Exp_1 | y << k - Ebits | z >> Ebits + 16 - k; 01628 w = xa > xa0 ? *--xa : 0; 01629 y = xa > xa0 ? *--xa : 0; 01630 d1 = z << k + 16 - Ebits | w << k - Ebits | y >> 16 + Ebits - k; 01631 goto ret_d; 01632 } 01633 z = xa > xa0 ? *--xa : 0; 01634 w = xa > xa0 ? *--xa : 0; 01635 k -= Ebits + 16; 01636 d0 = Exp_1 | y << k + 16 | z << k | w >> 16 - k; 01637 y = xa > xa0 ? *--xa : 0; 01638 d1 = w << k + 16 | y << k; 01639 #endif 01640 ret_d: 01641 #ifdef VAX 01642 word0(d) = d0 >> 16 | d0 << 16; 01643 word1(d) = d1 >> 16 | d1 << 16; 01644 #else 01645 #undef d0 01646 #undef d1 01647 #endif 01648 return dval(d); 01649 } 01650 01651 static Bigint * 01652 d2b(double d_, int *e, int *bits) 01653 { 01654 double_u d; 01655 Bigint *b; 01656 int de, k; 01657 ULong *x, y, z; 01658 #ifndef Sudden_Underflow 01659 int i; 01660 #endif 01661 #ifdef VAX 01662 ULong d0, d1; 01663 #endif 01664 dval(d) = d_; 01665 #ifdef VAX 01666 d0 = word0(d) >> 16 | word0(d) << 16; 01667 d1 = word1(d) >> 16 | word1(d) << 16; 01668 #else 01669 #define d0 word0(d) 01670 #define d1 word1(d) 01671 #endif 01672 01673 #ifdef Pack_32 01674 b = Balloc(1); 01675 #else 01676 b = Balloc(2); 01677 #endif 01678 x = b->x; 01679 01680 z = d0 & Frac_mask; 01681 d0 &= 0x7fffffff; /* clear sign bit, which we ignore */ 01682 #ifdef Sudden_Underflow 01683 de = (int)(d0 >> Exp_shift); 01684 #ifndef IBM 01685 z |= Exp_msk11; 01686 #endif 01687 #else 01688 if ((de = (int)(d0 >> Exp_shift)) != 0) 01689 z |= Exp_msk1; 01690 #endif 01691 #ifdef Pack_32 01692 if ((y = d1) != 0) { 01693 if ((k = lo0bits(&y)) != 0) { 01694 x[0] = y | z << (32 - k); 01695 z >>= k; 01696 } 01697 else 01698 x[0] = y; 01699 #ifndef Sudden_Underflow 01700 i = 01701 #endif 01702 b->wds = (x[1] = z) ? 2 : 1; 01703 } 01704 else { 01705 #ifdef DEBUG 01706 if (!z) 01707 Bug("Zero passed to d2b"); 01708 #endif 01709 k = lo0bits(&z); 01710 x[0] = z; 01711 #ifndef Sudden_Underflow 01712 i = 01713 #endif 01714 b->wds = 1; 01715 k += 32; 01716 } 01717 #else 01718 if (y = d1) { 01719 if (k = lo0bits(&y)) 01720 if (k >= 16) { 01721 x[0] = y | z << 32 - k & 0xffff; 01722 x[1] = z >> k - 16 & 0xffff; 01723 x[2] = z >> k; 01724 i = 2; 01725 } 01726 else { 01727 x[0] = y & 0xffff; 01728 x[1] = y >> 16 | z << 16 - k & 0xffff; 01729 x[2] = z >> k & 0xffff; 01730 x[3] = z >> k+16; 01731 i = 3; 01732 } 01733 else { 01734 x[0] = y & 0xffff; 01735 x[1] = y >> 16; 01736 x[2] = z & 0xffff; 01737 x[3] = z >> 16; 01738 i = 3; 01739 } 01740 } 01741 else { 01742 #ifdef DEBUG 01743 if (!z) 01744 Bug("Zero passed to d2b"); 01745 #endif 01746 k = lo0bits(&z); 01747 if (k >= 16) { 01748 x[0] = z; 01749 i = 0; 01750 } 01751 else { 01752 x[0] = z & 0xffff; 01753 x[1] = z >> 16; 01754 i = 1; 01755 } 01756 k += 32; 01757 } 01758 while (!x[i]) 01759 --i; 01760 b->wds = i + 1; 01761 #endif 01762 #ifndef Sudden_Underflow 01763 if (de) { 01764 #endif 01765 #ifdef IBM 01766 *e = (de - Bias - (P-1) << 2) + k; 01767 *bits = 4*P + 8 - k - hi0bits(word0(d) & Frac_mask); 01768 #else 01769 *e = de - Bias - (P-1) + k; 01770 *bits = P - k; 01771 #endif 01772 #ifndef Sudden_Underflow 01773 } 01774 else { 01775 *e = de - Bias - (P-1) + 1 + k; 01776 #ifdef Pack_32 01777 *bits = 32*i - hi0bits(x[i-1]); 01778 #else 01779 *bits = (i+2)*16 - hi0bits(x[i]); 01780 #endif 01781 } 01782 #endif 01783 return b; 01784 } 01785 #undef d0 01786 #undef d1 01787 01788 static double 01789 ratio(Bigint *a, Bigint *b) 01790 { 01791 double_u da, db; 01792 int k, ka, kb; 01793 01794 dval(da) = b2d(a, &ka); 01795 dval(db) = b2d(b, &kb); 01796 #ifdef Pack_32 01797 k = ka - kb + 32*(a->wds - b->wds); 01798 #else 01799 k = ka - kb + 16*(a->wds - b->wds); 01800 #endif 01801 #ifdef IBM 01802 if (k > 0) { 01803 word0(da) += (k >> 2)*Exp_msk1; 01804 if (k &= 3) 01805 dval(da) *= 1 << k; 01806 } 01807 else { 01808 k = -k; 01809 word0(db) += (k >> 2)*Exp_msk1; 01810 if (k &= 3) 01811 dval(db) *= 1 << k; 01812 } 01813 #else 01814 if (k > 0) 01815 word0(da) += k*Exp_msk1; 01816 else { 01817 k = -k; 01818 word0(db) += k*Exp_msk1; 01819 } 01820 #endif 01821 return dval(da) / dval(db); 01822 } 01823 01824 static const double 01825 tens[] = { 01826 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 01827 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19, 01828 1e20, 1e21, 1e22 01829 #ifdef VAX 01830 , 1e23, 1e24 01831 #endif 01832 }; 01833 01834 static const double 01835 #ifdef IEEE_Arith 01836 bigtens[] = { 1e16, 1e32, 1e64, 1e128, 1e256 }; 01837 static const double tinytens[] = { 1e-16, 1e-32, 1e-64, 1e-128, 01838 #ifdef Avoid_Underflow 01839 9007199254740992.*9007199254740992.e-256 01840 /* = 2^106 * 1e-53 */ 01841 #else 01842 1e-256 01843 #endif 01844 }; 01845 /* The factor of 2^53 in tinytens[4] helps us avoid setting the underflow */ 01846 /* flag unnecessarily. It leads to a song and dance at the end of strtod. */ 01847 #define Scale_Bit 0x10 01848 #define n_bigtens 5 01849 #else 01850 #ifdef IBM 01851 bigtens[] = { 1e16, 1e32, 1e64 }; 01852 static const double tinytens[] = { 1e-16, 1e-32, 1e-64 }; 01853 #define n_bigtens 3 01854 #else 01855 bigtens[] = { 1e16, 1e32 }; 01856 static const double tinytens[] = { 1e-16, 1e-32 }; 01857 #define n_bigtens 2 01858 #endif 01859 #endif 01860 01861 #ifndef IEEE_Arith 01862 #undef INFNAN_CHECK 01863 #endif 01864 01865 #ifdef INFNAN_CHECK 01866 01867 #ifndef NAN_WORD0 01868 #define NAN_WORD0 0x7ff80000 01869 #endif 01870 01871 #ifndef NAN_WORD1 01872 #define NAN_WORD1 0 01873 #endif 01874 01875 static int 01876 match(const char **sp, char *t) 01877 { 01878 int c, d; 01879 const char *s = *sp; 01880 01881 while (d = *t++) { 01882 if ((c = *++s) >= 'A' && c <= 'Z') 01883 c += 'a' - 'A'; 01884 if (c != d) 01885 return 0; 01886 } 01887 *sp = s + 1; 01888 return 1; 01889 } 01890 01891 #ifndef No_Hex_NaN 01892 static void 01893 hexnan(double *rvp, const char **sp) 01894 { 01895 ULong c, x[2]; 01896 const char *s; 01897 int havedig, udx0, xshift; 01898 01899 x[0] = x[1] = 0; 01900 havedig = xshift = 0; 01901 udx0 = 1; 01902 s = *sp; 01903 while (c = *(const unsigned char*)++s) { 01904 if (c >= '0' && c <= '9') 01905 c -= '0'; 01906 else if (c >= 'a' && c <= 'f') 01907 c += 10 - 'a'; 01908 else if (c >= 'A' && c <= 'F') 01909 c += 10 - 'A'; 01910 else if (c <= ' ') { 01911 if (udx0 && havedig) { 01912 udx0 = 0; 01913 xshift = 1; 01914 } 01915 continue; 01916 } 01917 else if (/*(*/ c == ')' && havedig) { 01918 *sp = s + 1; 01919 break; 01920 } 01921 else 01922 return; /* invalid form: don't change *sp */ 01923 havedig = 1; 01924 if (xshift) { 01925 xshift = 0; 01926 x[0] = x[1]; 01927 x[1] = 0; 01928 } 01929 if (udx0) 01930 x[0] = (x[0] << 4) | (x[1] >> 28); 01931 x[1] = (x[1] << 4) | c; 01932 } 01933 if ((x[0] &= 0xfffff) || x[1]) { 01934 word0(*rvp) = Exp_mask | x[0]; 01935 word1(*rvp) = x[1]; 01936 } 01937 } 01938 #endif /*No_Hex_NaN*/ 01939 #endif /* INFNAN_CHECK */ 01940 01941 double 01942 ruby_strtod(const char *s00, char **se) 01943 { 01944 #ifdef Avoid_Underflow 01945 int scale; 01946 #endif 01947 int bb2, bb5, bbe, bd2, bd5, bbbits, bs2, c, dsign, 01948 e, e1, esign, i, j, k, nd, nd0, nf, nz, nz0, sign; 01949 const char *s, *s0, *s1; 01950 double aadj, adj; 01951 double_u aadj1, rv, rv0; 01952 Long L; 01953 ULong y, z; 01954 Bigint *bb, *bb1, *bd, *bd0, *bs, *delta; 01955 #ifdef SET_INEXACT 01956 int inexact, oldinexact; 01957 #endif 01958 #ifdef Honor_FLT_ROUNDS 01959 int rounding; 01960 #endif 01961 #ifdef USE_LOCALE 01962 const char *s2; 01963 #endif 01964 01965 errno = 0; 01966 sign = nz0 = nz = 0; 01967 dval(rv) = 0.; 01968 for (s = s00;;s++) 01969 switch (*s) { 01970 case '-': 01971 sign = 1; 01972 /* no break */ 01973 case '+': 01974 if (*++s) 01975 goto break2; 01976 /* no break */ 01977 case 0: 01978 goto ret0; 01979 case '\t': 01980 case '\n': 01981 case '\v': 01982 case '\f': 01983 case '\r': 01984 case ' ': 01985 continue; 01986 default: 01987 goto break2; 01988 } 01989 break2: 01990 if (*s == '0') { 01991 if (s[1] == 'x' || s[1] == 'X') { 01992 static const char hexdigit[] = "0123456789abcdef0123456789ABCDEF"; 01993 s0 = ++s; 01994 adj = 0; 01995 aadj = 1.0; 01996 nd0 = -4; 01997 01998 if (!*++s || !(s1 = strchr(hexdigit, *s))) goto ret0; 01999 if (*s == '0') { 02000 while (*++s == '0'); 02001 s1 = strchr(hexdigit, *s); 02002 } 02003 if (s1 != NULL) { 02004 do { 02005 adj += aadj * ((s1 - hexdigit) & 15); 02006 nd0 += 4; 02007 aadj /= 16; 02008 } while (*++s && (s1 = strchr(hexdigit, *s))); 02009 } 02010 02011 if (*s == '.') { 02012 dsign = 1; 02013 if (!*++s || !(s1 = strchr(hexdigit, *s))) goto ret0; 02014 if (nd0 < 0) { 02015 while (*s == '0') { 02016 s++; 02017 nd0 -= 4; 02018 } 02019 } 02020 for (; *s && (s1 = strchr(hexdigit, *s)); ++s) { 02021 adj += aadj * ((s1 - hexdigit) & 15); 02022 if ((aadj /= 16) == 0.0) { 02023 while (strchr(hexdigit, *++s)); 02024 break; 02025 } 02026 } 02027 } 02028 else { 02029 dsign = 0; 02030 } 02031 02032 if (*s == 'P' || *s == 'p') { 02033 dsign = 0x2C - *++s; /* +: 2B, -: 2D */ 02034 if (abs(dsign) == 1) s++; 02035 else dsign = 1; 02036 02037 nd = 0; 02038 c = *s; 02039 if (c < '0' || '9' < c) goto ret0; 02040 do { 02041 nd *= 10; 02042 nd += c; 02043 nd -= '0'; 02044 c = *++s; 02045 /* Float("0x0."+("0"*267)+"1fp2095") */ 02046 if (nd + dsign * nd0 > 2095) { 02047 while ('0' <= c && c <= '9') c = *++s; 02048 break; 02049 } 02050 } while ('0' <= c && c <= '9'); 02051 nd0 += nd * dsign; 02052 } 02053 else { 02054 if (dsign) goto ret0; 02055 } 02056 dval(rv) = ldexp(adj, nd0); 02057 goto ret; 02058 } 02059 nz0 = 1; 02060 while (*++s == '0') ; 02061 if (!*s) 02062 goto ret; 02063 } 02064 s0 = s; 02065 y = z = 0; 02066 for (nd = nf = 0; (c = *s) >= '0' && c <= '9'; nd++, s++) 02067 if (nd < 9) 02068 y = 10*y + c - '0'; 02069 else if (nd < 16) 02070 z = 10*z + c - '0'; 02071 nd0 = nd; 02072 #ifdef USE_LOCALE 02073 s1 = localeconv()->decimal_point; 02074 if (c == *s1) { 02075 c = '.'; 02076 if (*++s1) { 02077 s2 = s; 02078 for (;;) { 02079 if (*++s2 != *s1) { 02080 c = 0; 02081 break; 02082 } 02083 if (!*++s1) { 02084 s = s2; 02085 break; 02086 } 02087 } 02088 } 02089 } 02090 #endif 02091 if (c == '.') { 02092 if (!ISDIGIT(s[1])) 02093 goto dig_done; 02094 c = *++s; 02095 if (!nd) { 02096 for (; c == '0'; c = *++s) 02097 nz++; 02098 if (c > '0' && c <= '9') { 02099 s0 = s; 02100 nf += nz; 02101 nz = 0; 02102 goto have_dig; 02103 } 02104 goto dig_done; 02105 } 02106 for (; c >= '0' && c <= '9'; c = *++s) { 02107 have_dig: 02108 nz++; 02109 if (nf > DBL_DIG * 4) continue; 02110 if (c -= '0') { 02111 nf += nz; 02112 for (i = 1; i < nz; i++) 02113 if (nd++ < 9) 02114 y *= 10; 02115 else if (nd <= DBL_DIG + 1) 02116 z *= 10; 02117 if (nd++ < 9) 02118 y = 10*y + c; 02119 else if (nd <= DBL_DIG + 1) 02120 z = 10*z + c; 02121 nz = 0; 02122 } 02123 } 02124 } 02125 dig_done: 02126 e = 0; 02127 if (c == 'e' || c == 'E') { 02128 if (!nd && !nz && !nz0) { 02129 goto ret0; 02130 } 02131 s00 = s; 02132 esign = 0; 02133 switch (c = *++s) { 02134 case '-': 02135 esign = 1; 02136 case '+': 02137 c = *++s; 02138 } 02139 if (c >= '0' && c <= '9') { 02140 while (c == '0') 02141 c = *++s; 02142 if (c > '0' && c <= '9') { 02143 L = c - '0'; 02144 s1 = s; 02145 while ((c = *++s) >= '0' && c <= '9') 02146 L = 10*L + c - '0'; 02147 if (s - s1 > 8 || L > 19999) 02148 /* Avoid confusion from exponents 02149 * so large that e might overflow. 02150 */ 02151 e = 19999; /* safe for 16 bit ints */ 02152 else 02153 e = (int)L; 02154 if (esign) 02155 e = -e; 02156 } 02157 else 02158 e = 0; 02159 } 02160 else 02161 s = s00; 02162 } 02163 if (!nd) { 02164 if (!nz && !nz0) { 02165 #ifdef INFNAN_CHECK 02166 /* Check for Nan and Infinity */ 02167 switch (c) { 02168 case 'i': 02169 case 'I': 02170 if (match(&s,"nf")) { 02171 --s; 02172 if (!match(&s,"inity")) 02173 ++s; 02174 word0(rv) = 0x7ff00000; 02175 word1(rv) = 0; 02176 goto ret; 02177 } 02178 break; 02179 case 'n': 02180 case 'N': 02181 if (match(&s, "an")) { 02182 word0(rv) = NAN_WORD0; 02183 word1(rv) = NAN_WORD1; 02184 #ifndef No_Hex_NaN 02185 if (*s == '(') /*)*/ 02186 hexnan(&rv, &s); 02187 #endif 02188 goto ret; 02189 } 02190 } 02191 #endif /* INFNAN_CHECK */ 02192 ret0: 02193 s = s00; 02194 sign = 0; 02195 } 02196 goto ret; 02197 } 02198 e1 = e -= nf; 02199 02200 /* Now we have nd0 digits, starting at s0, followed by a 02201 * decimal point, followed by nd-nd0 digits. The number we're 02202 * after is the integer represented by those digits times 02203 * 10**e */ 02204 02205 if (!nd0) 02206 nd0 = nd; 02207 k = nd < DBL_DIG + 1 ? nd : DBL_DIG + 1; 02208 dval(rv) = y; 02209 if (k > 9) { 02210 #ifdef SET_INEXACT 02211 if (k > DBL_DIG) 02212 oldinexact = get_inexact(); 02213 #endif 02214 dval(rv) = tens[k - 9] * dval(rv) + z; 02215 } 02216 bd0 = bb = bd = bs = delta = 0; 02217 if (nd <= DBL_DIG 02218 #ifndef RND_PRODQUOT 02219 #ifndef Honor_FLT_ROUNDS 02220 && Flt_Rounds == 1 02221 #endif 02222 #endif 02223 ) { 02224 if (!e) 02225 goto ret; 02226 if (e > 0) { 02227 if (e <= Ten_pmax) { 02228 #ifdef VAX 02229 goto vax_ovfl_check; 02230 #else 02231 #ifdef Honor_FLT_ROUNDS 02232 /* round correctly FLT_ROUNDS = 2 or 3 */ 02233 if (sign) { 02234 dval(rv) = -dval(rv); 02235 sign = 0; 02236 } 02237 #endif 02238 /* rv = */ rounded_product(dval(rv), tens[e]); 02239 goto ret; 02240 #endif 02241 } 02242 i = DBL_DIG - nd; 02243 if (e <= Ten_pmax + i) { 02244 /* A fancier test would sometimes let us do 02245 * this for larger i values. 02246 */ 02247 #ifdef Honor_FLT_ROUNDS 02248 /* round correctly FLT_ROUNDS = 2 or 3 */ 02249 if (sign) { 02250 dval(rv) = -dval(rv); 02251 sign = 0; 02252 } 02253 #endif 02254 e -= i; 02255 dval(rv) *= tens[i]; 02256 #ifdef VAX 02257 /* VAX exponent range is so narrow we must 02258 * worry about overflow here... 02259 */ 02260 vax_ovfl_check: 02261 word0(rv) -= P*Exp_msk1; 02262 /* rv = */ rounded_product(dval(rv), tens[e]); 02263 if ((word0(rv) & Exp_mask) 02264 > Exp_msk1*(DBL_MAX_EXP+Bias-1-P)) 02265 goto ovfl; 02266 word0(rv) += P*Exp_msk1; 02267 #else 02268 /* rv = */ rounded_product(dval(rv), tens[e]); 02269 #endif 02270 goto ret; 02271 } 02272 } 02273 #ifndef Inaccurate_Divide 02274 else if (e >= -Ten_pmax) { 02275 #ifdef Honor_FLT_ROUNDS 02276 /* round correctly FLT_ROUNDS = 2 or 3 */ 02277 if (sign) { 02278 dval(rv) = -dval(rv); 02279 sign = 0; 02280 } 02281 #endif 02282 /* rv = */ rounded_quotient(dval(rv), tens[-e]); 02283 goto ret; 02284 } 02285 #endif 02286 } 02287 e1 += nd - k; 02288 02289 #ifdef IEEE_Arith 02290 #ifdef SET_INEXACT 02291 inexact = 1; 02292 if (k <= DBL_DIG) 02293 oldinexact = get_inexact(); 02294 #endif 02295 #ifdef Avoid_Underflow 02296 scale = 0; 02297 #endif 02298 #ifdef Honor_FLT_ROUNDS 02299 if ((rounding = Flt_Rounds) >= 2) { 02300 if (sign) 02301 rounding = rounding == 2 ? 0 : 2; 02302 else 02303 if (rounding != 2) 02304 rounding = 0; 02305 } 02306 #endif 02307 #endif /*IEEE_Arith*/ 02308 02309 /* Get starting approximation = rv * 10**e1 */ 02310 02311 if (e1 > 0) { 02312 if ((i = e1 & 15) != 0) 02313 dval(rv) *= tens[i]; 02314 if (e1 &= ~15) { 02315 if (e1 > DBL_MAX_10_EXP) { 02316 ovfl: 02317 #ifndef NO_ERRNO 02318 errno = ERANGE; 02319 #endif 02320 /* Can't trust HUGE_VAL */ 02321 #ifdef IEEE_Arith 02322 #ifdef Honor_FLT_ROUNDS 02323 switch (rounding) { 02324 case 0: /* toward 0 */ 02325 case 3: /* toward -infinity */ 02326 word0(rv) = Big0; 02327 word1(rv) = Big1; 02328 break; 02329 default: 02330 word0(rv) = Exp_mask; 02331 word1(rv) = 0; 02332 } 02333 #else /*Honor_FLT_ROUNDS*/ 02334 word0(rv) = Exp_mask; 02335 word1(rv) = 0; 02336 #endif /*Honor_FLT_ROUNDS*/ 02337 #ifdef SET_INEXACT 02338 /* set overflow bit */ 02339 dval(rv0) = 1e300; 02340 dval(rv0) *= dval(rv0); 02341 #endif 02342 #else /*IEEE_Arith*/ 02343 word0(rv) = Big0; 02344 word1(rv) = Big1; 02345 #endif /*IEEE_Arith*/ 02346 if (bd0) 02347 goto retfree; 02348 goto ret; 02349 } 02350 e1 >>= 4; 02351 for (j = 0; e1 > 1; j++, e1 >>= 1) 02352 if (e1 & 1) 02353 dval(rv) *= bigtens[j]; 02354 /* The last multiplication could overflow. */ 02355 word0(rv) -= P*Exp_msk1; 02356 dval(rv) *= bigtens[j]; 02357 if ((z = word0(rv) & Exp_mask) 02358 > Exp_msk1*(DBL_MAX_EXP+Bias-P)) 02359 goto ovfl; 02360 if (z > Exp_msk1*(DBL_MAX_EXP+Bias-1-P)) { 02361 /* set to largest number */ 02362 /* (Can't trust DBL_MAX) */ 02363 word0(rv) = Big0; 02364 word1(rv) = Big1; 02365 } 02366 else 02367 word0(rv) += P*Exp_msk1; 02368 } 02369 } 02370 else if (e1 < 0) { 02371 e1 = -e1; 02372 if ((i = e1 & 15) != 0) 02373 dval(rv) /= tens[i]; 02374 if (e1 >>= 4) { 02375 if (e1 >= 1 << n_bigtens) 02376 goto undfl; 02377 #ifdef Avoid_Underflow 02378 if (e1 & Scale_Bit) 02379 scale = 2*P; 02380 for (j = 0; e1 > 0; j++, e1 >>= 1) 02381 if (e1 & 1) 02382 dval(rv) *= tinytens[j]; 02383 if (scale && (j = 2*P + 1 - ((word0(rv) & Exp_mask) 02384 >> Exp_shift)) > 0) { 02385 /* scaled rv is denormal; zap j low bits */ 02386 if (j >= 32) { 02387 word1(rv) = 0; 02388 if (j >= 53) 02389 word0(rv) = (P+2)*Exp_msk1; 02390 else 02391 word0(rv) &= 0xffffffff << (j-32); 02392 } 02393 else 02394 word1(rv) &= 0xffffffff << j; 02395 } 02396 #else 02397 for (j = 0; e1 > 1; j++, e1 >>= 1) 02398 if (e1 & 1) 02399 dval(rv) *= tinytens[j]; 02400 /* The last multiplication could underflow. */ 02401 dval(rv0) = dval(rv); 02402 dval(rv) *= tinytens[j]; 02403 if (!dval(rv)) { 02404 dval(rv) = 2.*dval(rv0); 02405 dval(rv) *= tinytens[j]; 02406 #endif 02407 if (!dval(rv)) { 02408 undfl: 02409 dval(rv) = 0.; 02410 #ifndef NO_ERRNO 02411 errno = ERANGE; 02412 #endif 02413 if (bd0) 02414 goto retfree; 02415 goto ret; 02416 } 02417 #ifndef Avoid_Underflow 02418 word0(rv) = Tiny0; 02419 word1(rv) = Tiny1; 02420 /* The refinement below will clean 02421 * this approximation up. 02422 */ 02423 } 02424 #endif 02425 } 02426 } 02427 02428 /* Now the hard part -- adjusting rv to the correct value.*/ 02429 02430 /* Put digits into bd: true value = bd * 10^e */ 02431 02432 bd0 = s2b(s0, nd0, nd, y); 02433 02434 for (;;) { 02435 bd = Balloc(bd0->k); 02436 Bcopy(bd, bd0); 02437 bb = d2b(dval(rv), &bbe, &bbbits); /* rv = bb * 2^bbe */ 02438 bs = i2b(1); 02439 02440 if (e >= 0) { 02441 bb2 = bb5 = 0; 02442 bd2 = bd5 = e; 02443 } 02444 else { 02445 bb2 = bb5 = -e; 02446 bd2 = bd5 = 0; 02447 } 02448 if (bbe >= 0) 02449 bb2 += bbe; 02450 else 02451 bd2 -= bbe; 02452 bs2 = bb2; 02453 #ifdef Honor_FLT_ROUNDS 02454 if (rounding != 1) 02455 bs2++; 02456 #endif 02457 #ifdef Avoid_Underflow 02458 j = bbe - scale; 02459 i = j + bbbits - 1; /* logb(rv) */ 02460 if (i < Emin) /* denormal */ 02461 j += P - Emin; 02462 else 02463 j = P + 1 - bbbits; 02464 #else /*Avoid_Underflow*/ 02465 #ifdef Sudden_Underflow 02466 #ifdef IBM 02467 j = 1 + 4*P - 3 - bbbits + ((bbe + bbbits - 1) & 3); 02468 #else 02469 j = P + 1 - bbbits; 02470 #endif 02471 #else /*Sudden_Underflow*/ 02472 j = bbe; 02473 i = j + bbbits - 1; /* logb(rv) */ 02474 if (i < Emin) /* denormal */ 02475 j += P - Emin; 02476 else 02477 j = P + 1 - bbbits; 02478 #endif /*Sudden_Underflow*/ 02479 #endif /*Avoid_Underflow*/ 02480 bb2 += j; 02481 bd2 += j; 02482 #ifdef Avoid_Underflow 02483 bd2 += scale; 02484 #endif 02485 i = bb2 < bd2 ? bb2 : bd2; 02486 if (i > bs2) 02487 i = bs2; 02488 if (i > 0) { 02489 bb2 -= i; 02490 bd2 -= i; 02491 bs2 -= i; 02492 } 02493 if (bb5 > 0) { 02494 bs = pow5mult(bs, bb5); 02495 bb1 = mult(bs, bb); 02496 Bfree(bb); 02497 bb = bb1; 02498 } 02499 if (bb2 > 0) 02500 bb = lshift(bb, bb2); 02501 if (bd5 > 0) 02502 bd = pow5mult(bd, bd5); 02503 if (bd2 > 0) 02504 bd = lshift(bd, bd2); 02505 if (bs2 > 0) 02506 bs = lshift(bs, bs2); 02507 delta = diff(bb, bd); 02508 dsign = delta->sign; 02509 delta->sign = 0; 02510 i = cmp(delta, bs); 02511 #ifdef Honor_FLT_ROUNDS 02512 if (rounding != 1) { 02513 if (i < 0) { 02514 /* Error is less than an ulp */ 02515 if (!delta->x[0] && delta->wds <= 1) { 02516 /* exact */ 02517 #ifdef SET_INEXACT 02518 inexact = 0; 02519 #endif 02520 break; 02521 } 02522 if (rounding) { 02523 if (dsign) { 02524 adj = 1.; 02525 goto apply_adj; 02526 } 02527 } 02528 else if (!dsign) { 02529 adj = -1.; 02530 if (!word1(rv) 02531 && !(word0(rv) & Frac_mask)) { 02532 y = word0(rv) & Exp_mask; 02533 #ifdef Avoid_Underflow 02534 if (!scale || y > 2*P*Exp_msk1) 02535 #else 02536 if (y) 02537 #endif 02538 { 02539 delta = lshift(delta,Log2P); 02540 if (cmp(delta, bs) <= 0) 02541 adj = -0.5; 02542 } 02543 } 02544 apply_adj: 02545 #ifdef Avoid_Underflow 02546 if (scale && (y = word0(rv) & Exp_mask) 02547 <= 2*P*Exp_msk1) 02548 word0(adj) += (2*P+1)*Exp_msk1 - y; 02549 #else 02550 #ifdef Sudden_Underflow 02551 if ((word0(rv) & Exp_mask) <= 02552 P*Exp_msk1) { 02553 word0(rv) += P*Exp_msk1; 02554 dval(rv) += adj*ulp(dval(rv)); 02555 word0(rv) -= P*Exp_msk1; 02556 } 02557 else 02558 #endif /*Sudden_Underflow*/ 02559 #endif /*Avoid_Underflow*/ 02560 dval(rv) += adj*ulp(dval(rv)); 02561 } 02562 break; 02563 } 02564 adj = ratio(delta, bs); 02565 if (adj < 1.) 02566 adj = 1.; 02567 if (adj <= 0x7ffffffe) { 02568 /* adj = rounding ? ceil(adj) : floor(adj); */ 02569 y = adj; 02570 if (y != adj) { 02571 if (!((rounding>>1) ^ dsign)) 02572 y++; 02573 adj = y; 02574 } 02575 } 02576 #ifdef Avoid_Underflow 02577 if (scale && (y = word0(rv) & Exp_mask) <= 2*P*Exp_msk1) 02578 word0(adj) += (2*P+1)*Exp_msk1 - y; 02579 #else 02580 #ifdef Sudden_Underflow 02581 if ((word0(rv) & Exp_mask) <= P*Exp_msk1) { 02582 word0(rv) += P*Exp_msk1; 02583 adj *= ulp(dval(rv)); 02584 if (dsign) 02585 dval(rv) += adj; 02586 else 02587 dval(rv) -= adj; 02588 word0(rv) -= P*Exp_msk1; 02589 goto cont; 02590 } 02591 #endif /*Sudden_Underflow*/ 02592 #endif /*Avoid_Underflow*/ 02593 adj *= ulp(dval(rv)); 02594 if (dsign) 02595 dval(rv) += adj; 02596 else 02597 dval(rv) -= adj; 02598 goto cont; 02599 } 02600 #endif /*Honor_FLT_ROUNDS*/ 02601 02602 if (i < 0) { 02603 /* Error is less than half an ulp -- check for 02604 * special case of mantissa a power of two. 02605 */ 02606 if (dsign || word1(rv) || word0(rv) & Bndry_mask 02607 #ifdef IEEE_Arith 02608 #ifdef Avoid_Underflow 02609 || (word0(rv) & Exp_mask) <= (2*P+1)*Exp_msk1 02610 #else 02611 || (word0(rv) & Exp_mask) <= Exp_msk1 02612 #endif 02613 #endif 02614 ) { 02615 #ifdef SET_INEXACT 02616 if (!delta->x[0] && delta->wds <= 1) 02617 inexact = 0; 02618 #endif 02619 break; 02620 } 02621 if (!delta->x[0] && delta->wds <= 1) { 02622 /* exact result */ 02623 #ifdef SET_INEXACT 02624 inexact = 0; 02625 #endif 02626 break; 02627 } 02628 delta = lshift(delta,Log2P); 02629 if (cmp(delta, bs) > 0) 02630 goto drop_down; 02631 break; 02632 } 02633 if (i == 0) { 02634 /* exactly half-way between */ 02635 if (dsign) { 02636 if ((word0(rv) & Bndry_mask1) == Bndry_mask1 02637 && word1(rv) == ( 02638 #ifdef Avoid_Underflow 02639 (scale && (y = word0(rv) & Exp_mask) <= 2*P*Exp_msk1) 02640 ? (0xffffffff & (0xffffffff << (2*P+1-(y>>Exp_shift)))) : 02641 #endif 02642 0xffffffff)) { 02643 /*boundary case -- increment exponent*/ 02644 word0(rv) = (word0(rv) & Exp_mask) 02645 + Exp_msk1 02646 #ifdef IBM 02647 | Exp_msk1 >> 4 02648 #endif 02649 ; 02650 word1(rv) = 0; 02651 #ifdef Avoid_Underflow 02652 dsign = 0; 02653 #endif 02654 break; 02655 } 02656 } 02657 else if (!(word0(rv) & Bndry_mask) && !word1(rv)) { 02658 drop_down: 02659 /* boundary case -- decrement exponent */ 02660 #ifdef Sudden_Underflow /*{{*/ 02661 L = word0(rv) & Exp_mask; 02662 #ifdef IBM 02663 if (L < Exp_msk1) 02664 #else 02665 #ifdef Avoid_Underflow 02666 if (L <= (scale ? (2*P+1)*Exp_msk1 : Exp_msk1)) 02667 #else 02668 if (L <= Exp_msk1) 02669 #endif /*Avoid_Underflow*/ 02670 #endif /*IBM*/ 02671 goto undfl; 02672 L -= Exp_msk1; 02673 #else /*Sudden_Underflow}{*/ 02674 #ifdef Avoid_Underflow 02675 if (scale) { 02676 L = word0(rv) & Exp_mask; 02677 if (L <= (2*P+1)*Exp_msk1) { 02678 if (L > (P+2)*Exp_msk1) 02679 /* round even ==> */ 02680 /* accept rv */ 02681 break; 02682 /* rv = smallest denormal */ 02683 goto undfl; 02684 } 02685 } 02686 #endif /*Avoid_Underflow*/ 02687 L = (word0(rv) & Exp_mask) - Exp_msk1; 02688 #endif /*Sudden_Underflow}}*/ 02689 word0(rv) = L | Bndry_mask1; 02690 word1(rv) = 0xffffffff; 02691 #ifdef IBM 02692 goto cont; 02693 #else 02694 break; 02695 #endif 02696 } 02697 #ifndef ROUND_BIASED 02698 if (!(word1(rv) & LSB)) 02699 break; 02700 #endif 02701 if (dsign) 02702 dval(rv) += ulp(dval(rv)); 02703 #ifndef ROUND_BIASED 02704 else { 02705 dval(rv) -= ulp(dval(rv)); 02706 #ifndef Sudden_Underflow 02707 if (!dval(rv)) 02708 goto undfl; 02709 #endif 02710 } 02711 #ifdef Avoid_Underflow 02712 dsign = 1 - dsign; 02713 #endif 02714 #endif 02715 break; 02716 } 02717 if ((aadj = ratio(delta, bs)) <= 2.) { 02718 if (dsign) 02719 aadj = dval(aadj1) = 1.; 02720 else if (word1(rv) || word0(rv) & Bndry_mask) { 02721 #ifndef Sudden_Underflow 02722 if (word1(rv) == Tiny1 && !word0(rv)) 02723 goto undfl; 02724 #endif 02725 aadj = 1.; 02726 dval(aadj1) = -1.; 02727 } 02728 else { 02729 /* special case -- power of FLT_RADIX to be */ 02730 /* rounded down... */ 02731 02732 if (aadj < 2./FLT_RADIX) 02733 aadj = 1./FLT_RADIX; 02734 else 02735 aadj *= 0.5; 02736 dval(aadj1) = -aadj; 02737 } 02738 } 02739 else { 02740 aadj *= 0.5; 02741 dval(aadj1) = dsign ? aadj : -aadj; 02742 #ifdef Check_FLT_ROUNDS 02743 switch (Rounding) { 02744 case 2: /* towards +infinity */ 02745 dval(aadj1) -= 0.5; 02746 break; 02747 case 0: /* towards 0 */ 02748 case 3: /* towards -infinity */ 02749 dval(aadj1) += 0.5; 02750 } 02751 #else 02752 if (Flt_Rounds == 0) 02753 dval(aadj1) += 0.5; 02754 #endif /*Check_FLT_ROUNDS*/ 02755 } 02756 y = word0(rv) & Exp_mask; 02757 02758 /* Check for overflow */ 02759 02760 if (y == Exp_msk1*(DBL_MAX_EXP+Bias-1)) { 02761 dval(rv0) = dval(rv); 02762 word0(rv) -= P*Exp_msk1; 02763 adj = dval(aadj1) * ulp(dval(rv)); 02764 dval(rv) += adj; 02765 if ((word0(rv) & Exp_mask) >= 02766 Exp_msk1*(DBL_MAX_EXP+Bias-P)) { 02767 if (word0(rv0) == Big0 && word1(rv0) == Big1) 02768 goto ovfl; 02769 word0(rv) = Big0; 02770 word1(rv) = Big1; 02771 goto cont; 02772 } 02773 else 02774 word0(rv) += P*Exp_msk1; 02775 } 02776 else { 02777 #ifdef Avoid_Underflow 02778 if (scale && y <= 2*P*Exp_msk1) { 02779 if (aadj <= 0x7fffffff) { 02780 if ((z = (int)aadj) <= 0) 02781 z = 1; 02782 aadj = z; 02783 dval(aadj1) = dsign ? aadj : -aadj; 02784 } 02785 word0(aadj1) += (2*P+1)*Exp_msk1 - y; 02786 } 02787 adj = dval(aadj1) * ulp(dval(rv)); 02788 dval(rv) += adj; 02789 #else 02790 #ifdef Sudden_Underflow 02791 if ((word0(rv) & Exp_mask) <= P*Exp_msk1) { 02792 dval(rv0) = dval(rv); 02793 word0(rv) += P*Exp_msk1; 02794 adj = dval(aadj1) * ulp(dval(rv)); 02795 dval(rv) += adj; 02796 #ifdef IBM 02797 if ((word0(rv) & Exp_mask) < P*Exp_msk1) 02798 #else 02799 if ((word0(rv) & Exp_mask) <= P*Exp_msk1) 02800 #endif 02801 { 02802 if (word0(rv0) == Tiny0 && word1(rv0) == Tiny1) 02803 goto undfl; 02804 word0(rv) = Tiny0; 02805 word1(rv) = Tiny1; 02806 goto cont; 02807 } 02808 else 02809 word0(rv) -= P*Exp_msk1; 02810 } 02811 else { 02812 adj = dval(aadj1) * ulp(dval(rv)); 02813 dval(rv) += adj; 02814 } 02815 #else /*Sudden_Underflow*/ 02816 /* Compute adj so that the IEEE rounding rules will 02817 * correctly round rv + adj in some half-way cases. 02818 * If rv * ulp(rv) is denormalized (i.e., 02819 * y <= (P-1)*Exp_msk1), we must adjust aadj to avoid 02820 * trouble from bits lost to denormalization; 02821 * example: 1.2e-307 . 02822 */ 02823 if (y <= (P-1)*Exp_msk1 && aadj > 1.) { 02824 dval(aadj1) = (double)(int)(aadj + 0.5); 02825 if (!dsign) 02826 dval(aadj1) = -dval(aadj1); 02827 } 02828 adj = dval(aadj1) * ulp(dval(rv)); 02829 dval(rv) += adj; 02830 #endif /*Sudden_Underflow*/ 02831 #endif /*Avoid_Underflow*/ 02832 } 02833 z = word0(rv) & Exp_mask; 02834 #ifndef SET_INEXACT 02835 #ifdef Avoid_Underflow 02836 if (!scale) 02837 #endif 02838 if (y == z) { 02839 /* Can we stop now? */ 02840 L = (Long)aadj; 02841 aadj -= L; 02842 /* The tolerances below are conservative. */ 02843 if (dsign || word1(rv) || word0(rv) & Bndry_mask) { 02844 if (aadj < .4999999 || aadj > .5000001) 02845 break; 02846 } 02847 else if (aadj < .4999999/FLT_RADIX) 02848 break; 02849 } 02850 #endif 02851 cont: 02852 Bfree(bb); 02853 Bfree(bd); 02854 Bfree(bs); 02855 Bfree(delta); 02856 } 02857 #ifdef SET_INEXACT 02858 if (inexact) { 02859 if (!oldinexact) { 02860 word0(rv0) = Exp_1 + (70 << Exp_shift); 02861 word1(rv0) = 0; 02862 dval(rv0) += 1.; 02863 } 02864 } 02865 else if (!oldinexact) 02866 clear_inexact(); 02867 #endif 02868 #ifdef Avoid_Underflow 02869 if (scale) { 02870 word0(rv0) = Exp_1 - 2*P*Exp_msk1; 02871 word1(rv0) = 0; 02872 dval(rv) *= dval(rv0); 02873 #ifndef NO_ERRNO 02874 /* try to avoid the bug of testing an 8087 register value */ 02875 if (word0(rv) == 0 && word1(rv) == 0) 02876 errno = ERANGE; 02877 #endif 02878 } 02879 #endif /* Avoid_Underflow */ 02880 #ifdef SET_INEXACT 02881 if (inexact && !(word0(rv) & Exp_mask)) { 02882 /* set underflow bit */ 02883 dval(rv0) = 1e-300; 02884 dval(rv0) *= dval(rv0); 02885 } 02886 #endif 02887 retfree: 02888 Bfree(bb); 02889 Bfree(bd); 02890 Bfree(bs); 02891 Bfree(bd0); 02892 Bfree(delta); 02893 ret: 02894 if (se) 02895 *se = (char *)s; 02896 return sign ? -dval(rv) : dval(rv); 02897 } 02898 02899 static int 02900 quorem(Bigint *b, Bigint *S) 02901 { 02902 int n; 02903 ULong *bx, *bxe, q, *sx, *sxe; 02904 #ifdef ULLong 02905 ULLong borrow, carry, y, ys; 02906 #else 02907 ULong borrow, carry, y, ys; 02908 #ifdef Pack_32 02909 ULong si, z, zs; 02910 #endif 02911 #endif 02912 02913 n = S->wds; 02914 #ifdef DEBUG 02915 /*debug*/ if (b->wds > n) 02916 /*debug*/ Bug("oversize b in quorem"); 02917 #endif 02918 if (b->wds < n) 02919 return 0; 02920 sx = S->x; 02921 sxe = sx + --n; 02922 bx = b->x; 02923 bxe = bx + n; 02924 q = *bxe / (*sxe + 1); /* ensure q <= true quotient */ 02925 #ifdef DEBUG 02926 /*debug*/ if (q > 9) 02927 /*debug*/ Bug("oversized quotient in quorem"); 02928 #endif 02929 if (q) { 02930 borrow = 0; 02931 carry = 0; 02932 do { 02933 #ifdef ULLong 02934 ys = *sx++ * (ULLong)q + carry; 02935 carry = ys >> 32; 02936 y = *bx - (ys & FFFFFFFF) - borrow; 02937 borrow = y >> 32 & (ULong)1; 02938 *bx++ = (ULong)(y & FFFFFFFF); 02939 #else 02940 #ifdef Pack_32 02941 si = *sx++; 02942 ys = (si & 0xffff) * q + carry; 02943 zs = (si >> 16) * q + (ys >> 16); 02944 carry = zs >> 16; 02945 y = (*bx & 0xffff) - (ys & 0xffff) - borrow; 02946 borrow = (y & 0x10000) >> 16; 02947 z = (*bx >> 16) - (zs & 0xffff) - borrow; 02948 borrow = (z & 0x10000) >> 16; 02949 Storeinc(bx, z, y); 02950 #else 02951 ys = *sx++ * q + carry; 02952 carry = ys >> 16; 02953 y = *bx - (ys & 0xffff) - borrow; 02954 borrow = (y & 0x10000) >> 16; 02955 *bx++ = y & 0xffff; 02956 #endif 02957 #endif 02958 } while (sx <= sxe); 02959 if (!*bxe) { 02960 bx = b->x; 02961 while (--bxe > bx && !*bxe) 02962 --n; 02963 b->wds = n; 02964 } 02965 } 02966 if (cmp(b, S) >= 0) { 02967 q++; 02968 borrow = 0; 02969 carry = 0; 02970 bx = b->x; 02971 sx = S->x; 02972 do { 02973 #ifdef ULLong 02974 ys = *sx++ + carry; 02975 carry = ys >> 32; 02976 y = *bx - (ys & FFFFFFFF) - borrow; 02977 borrow = y >> 32 & (ULong)1; 02978 *bx++ = (ULong)(y & FFFFFFFF); 02979 #else 02980 #ifdef Pack_32 02981 si = *sx++; 02982 ys = (si & 0xffff) + carry; 02983 zs = (si >> 16) + (ys >> 16); 02984 carry = zs >> 16; 02985 y = (*bx & 0xffff) - (ys & 0xffff) - borrow; 02986 borrow = (y & 0x10000) >> 16; 02987 z = (*bx >> 16) - (zs & 0xffff) - borrow; 02988 borrow = (z & 0x10000) >> 16; 02989 Storeinc(bx, z, y); 02990 #else 02991 ys = *sx++ + carry; 02992 carry = ys >> 16; 02993 y = *bx - (ys & 0xffff) - borrow; 02994 borrow = (y & 0x10000) >> 16; 02995 *bx++ = y & 0xffff; 02996 #endif 02997 #endif 02998 } while (sx <= sxe); 02999 bx = b->x; 03000 bxe = bx + n; 03001 if (!*bxe) { 03002 while (--bxe > bx && !*bxe) 03003 --n; 03004 b->wds = n; 03005 } 03006 } 03007 return q; 03008 } 03009 03010 #ifndef MULTIPLE_THREADS 03011 static char *dtoa_result; 03012 #endif 03013 03014 #ifndef MULTIPLE_THREADS 03015 static char * 03016 rv_alloc(int i) 03017 { 03018 return dtoa_result = xmalloc(i); 03019 } 03020 #else 03021 #define rv_alloc(i) xmalloc(i) 03022 #endif 03023 03024 static char * 03025 nrv_alloc(const char *s, char **rve, size_t n) 03026 { 03027 char *rv, *t; 03028 03029 t = rv = rv_alloc(n); 03030 while ((*t = *s++) != 0) t++; 03031 if (rve) 03032 *rve = t; 03033 return rv; 03034 } 03035 03036 #define rv_strdup(s, rve) nrv_alloc((s), (rve), strlen(s)+1) 03037 03038 #ifndef MULTIPLE_THREADS 03039 /* freedtoa(s) must be used to free values s returned by dtoa 03040 * when MULTIPLE_THREADS is #defined. It should be used in all cases, 03041 * but for consistency with earlier versions of dtoa, it is optional 03042 * when MULTIPLE_THREADS is not defined. 03043 */ 03044 03045 static void 03046 freedtoa(char *s) 03047 { 03048 xfree(s); 03049 } 03050 #endif 03051 03052 static const char INFSTR[] = "Infinity"; 03053 static const char NANSTR[] = "NaN"; 03054 static const char ZEROSTR[] = "0"; 03055 03056 /* dtoa for IEEE arithmetic (dmg): convert double to ASCII string. 03057 * 03058 * Inspired by "How to Print Floating-Point Numbers Accurately" by 03059 * Guy L. Steele, Jr. and Jon L. White [Proc. ACM SIGPLAN '90, pp. 112-126]. 03060 * 03061 * Modifications: 03062 * 1. Rather than iterating, we use a simple numeric overestimate 03063 * to determine k = floor(log10(d)). We scale relevant 03064 * quantities using O(log2(k)) rather than O(k) multiplications. 03065 * 2. For some modes > 2 (corresponding to ecvt and fcvt), we don't 03066 * try to generate digits strictly left to right. Instead, we 03067 * compute with fewer bits and propagate the carry if necessary 03068 * when rounding the final digit up. This is often faster. 03069 * 3. Under the assumption that input will be rounded nearest, 03070 * mode 0 renders 1e23 as 1e23 rather than 9.999999999999999e22. 03071 * That is, we allow equality in stopping tests when the 03072 * round-nearest rule will give the same floating-point value 03073 * as would satisfaction of the stopping test with strict 03074 * inequality. 03075 * 4. We remove common factors of powers of 2 from relevant 03076 * quantities. 03077 * 5. When converting floating-point integers less than 1e16, 03078 * we use floating-point arithmetic rather than resorting 03079 * to multiple-precision integers. 03080 * 6. When asked to produce fewer than 15 digits, we first try 03081 * to get by with floating-point arithmetic; we resort to 03082 * multiple-precision integer arithmetic only if we cannot 03083 * guarantee that the floating-point calculation has given 03084 * the correctly rounded result. For k requested digits and 03085 * "uniformly" distributed input, the probability is 03086 * something like 10^(k-15) that we must resort to the Long 03087 * calculation. 03088 */ 03089 03090 char * 03091 ruby_dtoa(double d_, int mode, int ndigits, int *decpt, int *sign, char **rve) 03092 { 03093 /* Arguments ndigits, decpt, sign are similar to those 03094 of ecvt and fcvt; trailing zeros are suppressed from 03095 the returned string. If not null, *rve is set to point 03096 to the end of the return value. If d is +-Infinity or NaN, 03097 then *decpt is set to 9999. 03098 03099 mode: 03100 0 ==> shortest string that yields d when read in 03101 and rounded to nearest. 03102 1 ==> like 0, but with Steele & White stopping rule; 03103 e.g. with IEEE P754 arithmetic , mode 0 gives 03104 1e23 whereas mode 1 gives 9.999999999999999e22. 03105 2 ==> max(1,ndigits) significant digits. This gives a 03106 return value similar to that of ecvt, except 03107 that trailing zeros are suppressed. 03108 3 ==> through ndigits past the decimal point. This 03109 gives a return value similar to that from fcvt, 03110 except that trailing zeros are suppressed, and 03111 ndigits can be negative. 03112 4,5 ==> similar to 2 and 3, respectively, but (in 03113 round-nearest mode) with the tests of mode 0 to 03114 possibly return a shorter string that rounds to d. 03115 With IEEE arithmetic and compilation with 03116 -DHonor_FLT_ROUNDS, modes 4 and 5 behave the same 03117 as modes 2 and 3 when FLT_ROUNDS != 1. 03118 6-9 ==> Debugging modes similar to mode - 4: don't try 03119 fast floating-point estimate (if applicable). 03120 03121 Values of mode other than 0-9 are treated as mode 0. 03122 03123 Sufficient space is allocated to the return value 03124 to hold the suppressed trailing zeros. 03125 */ 03126 03127 int bbits, b2, b5, be, dig, i, ieps, ilim, ilim0, ilim1, 03128 j, j1, k, k0, k_check, leftright, m2, m5, s2, s5, 03129 spec_case, try_quick; 03130 Long L; 03131 #ifndef Sudden_Underflow 03132 int denorm; 03133 ULong x; 03134 #endif 03135 Bigint *b, *b1, *delta, *mlo = 0, *mhi = 0, *S; 03136 double ds; 03137 double_u d, d2, eps; 03138 char *s, *s0; 03139 #ifdef Honor_FLT_ROUNDS 03140 int rounding; 03141 #endif 03142 #ifdef SET_INEXACT 03143 int inexact, oldinexact; 03144 #endif 03145 03146 dval(d) = d_; 03147 03148 #ifndef MULTIPLE_THREADS 03149 if (dtoa_result) { 03150 freedtoa(dtoa_result); 03151 dtoa_result = 0; 03152 } 03153 #endif 03154 03155 if (word0(d) & Sign_bit) { 03156 /* set sign for everything, including 0's and NaNs */ 03157 *sign = 1; 03158 word0(d) &= ~Sign_bit; /* clear sign bit */ 03159 } 03160 else 03161 *sign = 0; 03162 03163 #if defined(IEEE_Arith) + defined(VAX) 03164 #ifdef IEEE_Arith 03165 if ((word0(d) & Exp_mask) == Exp_mask) 03166 #else 03167 if (word0(d) == 0x8000) 03168 #endif 03169 { 03170 /* Infinity or NaN */ 03171 *decpt = 9999; 03172 #ifdef IEEE_Arith 03173 if (!word1(d) && !(word0(d) & 0xfffff)) 03174 return rv_strdup(INFSTR, rve); 03175 #endif 03176 return rv_strdup(NANSTR, rve); 03177 } 03178 #endif 03179 #ifdef IBM 03180 dval(d) += 0; /* normalize */ 03181 #endif 03182 if (!dval(d)) { 03183 *decpt = 1; 03184 return rv_strdup(ZEROSTR, rve); 03185 } 03186 03187 #ifdef SET_INEXACT 03188 try_quick = oldinexact = get_inexact(); 03189 inexact = 1; 03190 #endif 03191 #ifdef Honor_FLT_ROUNDS 03192 if ((rounding = Flt_Rounds) >= 2) { 03193 if (*sign) 03194 rounding = rounding == 2 ? 0 : 2; 03195 else 03196 if (rounding != 2) 03197 rounding = 0; 03198 } 03199 #endif 03200 03201 b = d2b(dval(d), &be, &bbits); 03202 #ifdef Sudden_Underflow 03203 i = (int)(word0(d) >> Exp_shift1 & (Exp_mask>>Exp_shift1)); 03204 #else 03205 if ((i = (int)(word0(d) >> Exp_shift1 & (Exp_mask>>Exp_shift1))) != 0) { 03206 #endif 03207 dval(d2) = dval(d); 03208 word0(d2) &= Frac_mask1; 03209 word0(d2) |= Exp_11; 03210 #ifdef IBM 03211 if (j = 11 - hi0bits(word0(d2) & Frac_mask)) 03212 dval(d2) /= 1 << j; 03213 #endif 03214 03215 /* log(x) ~=~ log(1.5) + (x-1.5)/1.5 03216 * log10(x) = log(x) / log(10) 03217 * ~=~ log(1.5)/log(10) + (x-1.5)/(1.5*log(10)) 03218 * log10(d) = (i-Bias)*log(2)/log(10) + log10(d2) 03219 * 03220 * This suggests computing an approximation k to log10(d) by 03221 * 03222 * k = (i - Bias)*0.301029995663981 03223 * + ( (d2-1.5)*0.289529654602168 + 0.176091259055681 ); 03224 * 03225 * We want k to be too large rather than too small. 03226 * The error in the first-order Taylor series approximation 03227 * is in our favor, so we just round up the constant enough 03228 * to compensate for any error in the multiplication of 03229 * (i - Bias) by 0.301029995663981; since |i - Bias| <= 1077, 03230 * and 1077 * 0.30103 * 2^-52 ~=~ 7.2e-14, 03231 * adding 1e-13 to the constant term more than suffices. 03232 * Hence we adjust the constant term to 0.1760912590558. 03233 * (We could get a more accurate k by invoking log10, 03234 * but this is probably not worthwhile.) 03235 */ 03236 03237 i -= Bias; 03238 #ifdef IBM 03239 i <<= 2; 03240 i += j; 03241 #endif 03242 #ifndef Sudden_Underflow 03243 denorm = 0; 03244 } 03245 else { 03246 /* d is denormalized */ 03247 03248 i = bbits + be + (Bias + (P-1) - 1); 03249 x = i > 32 ? word0(d) << (64 - i) | word1(d) >> (i - 32) 03250 : word1(d) << (32 - i); 03251 dval(d2) = x; 03252 word0(d2) -= 31*Exp_msk1; /* adjust exponent */ 03253 i -= (Bias + (P-1) - 1) + 1; 03254 denorm = 1; 03255 } 03256 #endif 03257 ds = (dval(d2)-1.5)*0.289529654602168 + 0.1760912590558 + i*0.301029995663981; 03258 k = (int)ds; 03259 if (ds < 0. && ds != k) 03260 k--; /* want k = floor(ds) */ 03261 k_check = 1; 03262 if (k >= 0 && k <= Ten_pmax) { 03263 if (dval(d) < tens[k]) 03264 k--; 03265 k_check = 0; 03266 } 03267 j = bbits - i - 1; 03268 if (j >= 0) { 03269 b2 = 0; 03270 s2 = j; 03271 } 03272 else { 03273 b2 = -j; 03274 s2 = 0; 03275 } 03276 if (k >= 0) { 03277 b5 = 0; 03278 s5 = k; 03279 s2 += k; 03280 } 03281 else { 03282 b2 -= k; 03283 b5 = -k; 03284 s5 = 0; 03285 } 03286 if (mode < 0 || mode > 9) 03287 mode = 0; 03288 03289 #ifndef SET_INEXACT 03290 #ifdef Check_FLT_ROUNDS 03291 try_quick = Rounding == 1; 03292 #else 03293 try_quick = 1; 03294 #endif 03295 #endif /*SET_INEXACT*/ 03296 03297 if (mode > 5) { 03298 mode -= 4; 03299 try_quick = 0; 03300 } 03301 leftright = 1; 03302 ilim = ilim1 = -1; 03303 switch (mode) { 03304 case 0: 03305 case 1: 03306 i = 18; 03307 ndigits = 0; 03308 break; 03309 case 2: 03310 leftright = 0; 03311 /* no break */ 03312 case 4: 03313 if (ndigits <= 0) 03314 ndigits = 1; 03315 ilim = ilim1 = i = ndigits; 03316 break; 03317 case 3: 03318 leftright = 0; 03319 /* no break */ 03320 case 5: 03321 i = ndigits + k + 1; 03322 ilim = i; 03323 ilim1 = i - 1; 03324 if (i <= 0) 03325 i = 1; 03326 } 03327 s = s0 = rv_alloc(i+1); 03328 03329 #ifdef Honor_FLT_ROUNDS 03330 if (mode > 1 && rounding != 1) 03331 leftright = 0; 03332 #endif 03333 03334 if (ilim >= 0 && ilim <= Quick_max && try_quick) { 03335 03336 /* Try to get by with floating-point arithmetic. */ 03337 03338 i = 0; 03339 dval(d2) = dval(d); 03340 k0 = k; 03341 ilim0 = ilim; 03342 ieps = 2; /* conservative */ 03343 if (k > 0) { 03344 ds = tens[k&0xf]; 03345 j = k >> 4; 03346 if (j & Bletch) { 03347 /* prevent overflows */ 03348 j &= Bletch - 1; 03349 dval(d) /= bigtens[n_bigtens-1]; 03350 ieps++; 03351 } 03352 for (; j; j >>= 1, i++) 03353 if (j & 1) { 03354 ieps++; 03355 ds *= bigtens[i]; 03356 } 03357 dval(d) /= ds; 03358 } 03359 else if ((j1 = -k) != 0) { 03360 dval(d) *= tens[j1 & 0xf]; 03361 for (j = j1 >> 4; j; j >>= 1, i++) 03362 if (j & 1) { 03363 ieps++; 03364 dval(d) *= bigtens[i]; 03365 } 03366 } 03367 if (k_check && dval(d) < 1. && ilim > 0) { 03368 if (ilim1 <= 0) 03369 goto fast_failed; 03370 ilim = ilim1; 03371 k--; 03372 dval(d) *= 10.; 03373 ieps++; 03374 } 03375 dval(eps) = ieps*dval(d) + 7.; 03376 word0(eps) -= (P-1)*Exp_msk1; 03377 if (ilim == 0) { 03378 S = mhi = 0; 03379 dval(d) -= 5.; 03380 if (dval(d) > dval(eps)) 03381 goto one_digit; 03382 if (dval(d) < -dval(eps)) 03383 goto no_digits; 03384 goto fast_failed; 03385 } 03386 #ifndef No_leftright 03387 if (leftright) { 03388 /* Use Steele & White method of only 03389 * generating digits needed. 03390 */ 03391 dval(eps) = 0.5/tens[ilim-1] - dval(eps); 03392 for (i = 0;;) { 03393 L = (int)dval(d); 03394 dval(d) -= L; 03395 *s++ = '0' + (int)L; 03396 if (dval(d) < dval(eps)) 03397 goto ret1; 03398 if (1. - dval(d) < dval(eps)) 03399 goto bump_up; 03400 if (++i >= ilim) 03401 break; 03402 dval(eps) *= 10.; 03403 dval(d) *= 10.; 03404 } 03405 } 03406 else { 03407 #endif 03408 /* Generate ilim digits, then fix them up. */ 03409 dval(eps) *= tens[ilim-1]; 03410 for (i = 1;; i++, dval(d) *= 10.) { 03411 L = (Long)(dval(d)); 03412 if (!(dval(d) -= L)) 03413 ilim = i; 03414 *s++ = '0' + (int)L; 03415 if (i == ilim) { 03416 if (dval(d) > 0.5 + dval(eps)) 03417 goto bump_up; 03418 else if (dval(d) < 0.5 - dval(eps)) { 03419 while (*--s == '0') ; 03420 s++; 03421 goto ret1; 03422 } 03423 break; 03424 } 03425 } 03426 #ifndef No_leftright 03427 } 03428 #endif 03429 fast_failed: 03430 s = s0; 03431 dval(d) = dval(d2); 03432 k = k0; 03433 ilim = ilim0; 03434 } 03435 03436 /* Do we have a "small" integer? */ 03437 03438 if (be >= 0 && k <= Int_max) { 03439 /* Yes. */ 03440 ds = tens[k]; 03441 if (ndigits < 0 && ilim <= 0) { 03442 S = mhi = 0; 03443 if (ilim < 0 || dval(d) <= 5*ds) 03444 goto no_digits; 03445 goto one_digit; 03446 } 03447 for (i = 1;; i++, dval(d) *= 10.) { 03448 L = (Long)(dval(d) / ds); 03449 dval(d) -= L*ds; 03450 #ifdef Check_FLT_ROUNDS 03451 /* If FLT_ROUNDS == 2, L will usually be high by 1 */ 03452 if (dval(d) < 0) { 03453 L--; 03454 dval(d) += ds; 03455 } 03456 #endif 03457 *s++ = '0' + (int)L; 03458 if (!dval(d)) { 03459 #ifdef SET_INEXACT 03460 inexact = 0; 03461 #endif 03462 break; 03463 } 03464 if (i == ilim) { 03465 #ifdef Honor_FLT_ROUNDS 03466 if (mode > 1) 03467 switch (rounding) { 03468 case 0: goto ret1; 03469 case 2: goto bump_up; 03470 } 03471 #endif 03472 dval(d) += dval(d); 03473 if (dval(d) > ds || (dval(d) == ds && (L & 1))) { 03474 bump_up: 03475 while (*--s == '9') 03476 if (s == s0) { 03477 k++; 03478 *s = '0'; 03479 break; 03480 } 03481 ++*s++; 03482 } 03483 break; 03484 } 03485 } 03486 goto ret1; 03487 } 03488 03489 m2 = b2; 03490 m5 = b5; 03491 if (leftright) { 03492 i = 03493 #ifndef Sudden_Underflow 03494 denorm ? be + (Bias + (P-1) - 1 + 1) : 03495 #endif 03496 #ifdef IBM 03497 1 + 4*P - 3 - bbits + ((bbits + be - 1) & 3); 03498 #else 03499 1 + P - bbits; 03500 #endif 03501 b2 += i; 03502 s2 += i; 03503 mhi = i2b(1); 03504 } 03505 if (m2 > 0 && s2 > 0) { 03506 i = m2 < s2 ? m2 : s2; 03507 b2 -= i; 03508 m2 -= i; 03509 s2 -= i; 03510 } 03511 if (b5 > 0) { 03512 if (leftright) { 03513 if (m5 > 0) { 03514 mhi = pow5mult(mhi, m5); 03515 b1 = mult(mhi, b); 03516 Bfree(b); 03517 b = b1; 03518 } 03519 if ((j = b5 - m5) != 0) 03520 b = pow5mult(b, j); 03521 } 03522 else 03523 b = pow5mult(b, b5); 03524 } 03525 S = i2b(1); 03526 if (s5 > 0) 03527 S = pow5mult(S, s5); 03528 03529 /* Check for special case that d is a normalized power of 2. */ 03530 03531 spec_case = 0; 03532 if ((mode < 2 || leftright) 03533 #ifdef Honor_FLT_ROUNDS 03534 && rounding == 1 03535 #endif 03536 ) { 03537 if (!word1(d) && !(word0(d) & Bndry_mask) 03538 #ifndef Sudden_Underflow 03539 && word0(d) & (Exp_mask & ~Exp_msk1) 03540 #endif 03541 ) { 03542 /* The special case */ 03543 b2 += Log2P; 03544 s2 += Log2P; 03545 spec_case = 1; 03546 } 03547 } 03548 03549 /* Arrange for convenient computation of quotients: 03550 * shift left if necessary so divisor has 4 leading 0 bits. 03551 * 03552 * Perhaps we should just compute leading 28 bits of S once 03553 * and for all and pass them and a shift to quorem, so it 03554 * can do shifts and ors to compute the numerator for q. 03555 */ 03556 #ifdef Pack_32 03557 if ((i = ((s5 ? 32 - hi0bits(S->x[S->wds-1]) : 1) + s2) & 0x1f) != 0) 03558 i = 32 - i; 03559 #else 03560 if ((i = ((s5 ? 32 - hi0bits(S->x[S->wds-1]) : 1) + s2) & 0xf) != 0) 03561 i = 16 - i; 03562 #endif 03563 if (i > 4) { 03564 i -= 4; 03565 b2 += i; 03566 m2 += i; 03567 s2 += i; 03568 } 03569 else if (i < 4) { 03570 i += 28; 03571 b2 += i; 03572 m2 += i; 03573 s2 += i; 03574 } 03575 if (b2 > 0) 03576 b = lshift(b, b2); 03577 if (s2 > 0) 03578 S = lshift(S, s2); 03579 if (k_check) { 03580 if (cmp(b,S) < 0) { 03581 k--; 03582 b = multadd(b, 10, 0); /* we botched the k estimate */ 03583 if (leftright) 03584 mhi = multadd(mhi, 10, 0); 03585 ilim = ilim1; 03586 } 03587 } 03588 if (ilim <= 0 && (mode == 3 || mode == 5)) { 03589 if (ilim < 0 || cmp(b,S = multadd(S,5,0)) <= 0) { 03590 /* no digits, fcvt style */ 03591 no_digits: 03592 k = -1 - ndigits; 03593 goto ret; 03594 } 03595 one_digit: 03596 *s++ = '1'; 03597 k++; 03598 goto ret; 03599 } 03600 if (leftright) { 03601 if (m2 > 0) 03602 mhi = lshift(mhi, m2); 03603 03604 /* Compute mlo -- check for special case 03605 * that d is a normalized power of 2. 03606 */ 03607 03608 mlo = mhi; 03609 if (spec_case) { 03610 mhi = Balloc(mhi->k); 03611 Bcopy(mhi, mlo); 03612 mhi = lshift(mhi, Log2P); 03613 } 03614 03615 for (i = 1;;i++) { 03616 dig = quorem(b,S) + '0'; 03617 /* Do we yet have the shortest decimal string 03618 * that will round to d? 03619 */ 03620 j = cmp(b, mlo); 03621 delta = diff(S, mhi); 03622 j1 = delta->sign ? 1 : cmp(b, delta); 03623 Bfree(delta); 03624 #ifndef ROUND_BIASED 03625 if (j1 == 0 && mode != 1 && !(word1(d) & 1) 03626 #ifdef Honor_FLT_ROUNDS 03627 && rounding >= 1 03628 #endif 03629 ) { 03630 if (dig == '9') 03631 goto round_9_up; 03632 if (j > 0) 03633 dig++; 03634 #ifdef SET_INEXACT 03635 else if (!b->x[0] && b->wds <= 1) 03636 inexact = 0; 03637 #endif 03638 *s++ = dig; 03639 goto ret; 03640 } 03641 #endif 03642 if (j < 0 || (j == 0 && mode != 1 03643 #ifndef ROUND_BIASED 03644 && !(word1(d) & 1) 03645 #endif 03646 )) { 03647 if (!b->x[0] && b->wds <= 1) { 03648 #ifdef SET_INEXACT 03649 inexact = 0; 03650 #endif 03651 goto accept_dig; 03652 } 03653 #ifdef Honor_FLT_ROUNDS 03654 if (mode > 1) 03655 switch (rounding) { 03656 case 0: goto accept_dig; 03657 case 2: goto keep_dig; 03658 } 03659 #endif /*Honor_FLT_ROUNDS*/ 03660 if (j1 > 0) { 03661 b = lshift(b, 1); 03662 j1 = cmp(b, S); 03663 if ((j1 > 0 || (j1 == 0 && (dig & 1))) && dig++ == '9') 03664 goto round_9_up; 03665 } 03666 accept_dig: 03667 *s++ = dig; 03668 goto ret; 03669 } 03670 if (j1 > 0) { 03671 #ifdef Honor_FLT_ROUNDS 03672 if (!rounding) 03673 goto accept_dig; 03674 #endif 03675 if (dig == '9') { /* possible if i == 1 */ 03676 round_9_up: 03677 *s++ = '9'; 03678 goto roundoff; 03679 } 03680 *s++ = dig + 1; 03681 goto ret; 03682 } 03683 #ifdef Honor_FLT_ROUNDS 03684 keep_dig: 03685 #endif 03686 *s++ = dig; 03687 if (i == ilim) 03688 break; 03689 b = multadd(b, 10, 0); 03690 if (mlo == mhi) 03691 mlo = mhi = multadd(mhi, 10, 0); 03692 else { 03693 mlo = multadd(mlo, 10, 0); 03694 mhi = multadd(mhi, 10, 0); 03695 } 03696 } 03697 } 03698 else 03699 for (i = 1;; i++) { 03700 *s++ = dig = quorem(b,S) + '0'; 03701 if (!b->x[0] && b->wds <= 1) { 03702 #ifdef SET_INEXACT 03703 inexact = 0; 03704 #endif 03705 goto ret; 03706 } 03707 if (i >= ilim) 03708 break; 03709 b = multadd(b, 10, 0); 03710 } 03711 03712 /* Round off last digit */ 03713 03714 #ifdef Honor_FLT_ROUNDS 03715 switch (rounding) { 03716 case 0: goto trimzeros; 03717 case 2: goto roundoff; 03718 } 03719 #endif 03720 b = lshift(b, 1); 03721 j = cmp(b, S); 03722 if (j > 0 || (j == 0 && (dig & 1))) { 03723 roundoff: 03724 while (*--s == '9') 03725 if (s == s0) { 03726 k++; 03727 *s++ = '1'; 03728 goto ret; 03729 } 03730 ++*s++; 03731 } 03732 else { 03733 while (*--s == '0') ; 03734 s++; 03735 } 03736 ret: 03737 Bfree(S); 03738 if (mhi) { 03739 if (mlo && mlo != mhi) 03740 Bfree(mlo); 03741 Bfree(mhi); 03742 } 03743 ret1: 03744 #ifdef SET_INEXACT 03745 if (inexact) { 03746 if (!oldinexact) { 03747 word0(d) = Exp_1 + (70 << Exp_shift); 03748 word1(d) = 0; 03749 dval(d) += 1.; 03750 } 03751 } 03752 else if (!oldinexact) 03753 clear_inexact(); 03754 #endif 03755 Bfree(b); 03756 *s = 0; 03757 *decpt = k + 1; 03758 if (rve) 03759 *rve = s; 03760 return s0; 03761 } 03762 03763 void 03764 ruby_each_words(const char *str, void (*func)(const char*, int, void*), void *arg) 03765 { 03766 const char *end; 03767 int len; 03768 03769 if (!str) return; 03770 for (; *str; str = end) { 03771 while (ISSPACE(*str) || *str == ',') str++; 03772 if (!*str) break; 03773 end = str; 03774 while (*end && !ISSPACE(*end) && *end != ',') end++; 03775 len = (int)(end - str); /* assume no string exceeds INT_MAX */ 03776 (*func)(str, len, arg); 03777 } 03778 } 03779 03780 /*- 03781 * Copyright (c) 2004-2008 David Schultz <das@FreeBSD.ORG> 03782 * All rights reserved. 03783 * 03784 * Redistribution and use in source and binary forms, with or without 03785 * modification, are permitted provided that the following conditions 03786 * are met: 03787 * 1. Redistributions of source code must retain the above copyright 03788 * notice, this list of conditions and the following disclaimer. 03789 * 2. Redistributions in binary form must reproduce the above copyright 03790 * notice, this list of conditions and the following disclaimer in the 03791 * documentation and/or other materials provided with the distribution. 03792 * 03793 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 03794 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 03795 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 03796 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 03797 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 03798 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 03799 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 03800 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 03801 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 03802 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 03803 * SUCH DAMAGE. 03804 */ 03805 03806 #define DBL_MANH_SIZE 20 03807 #define DBL_MANL_SIZE 32 03808 #define DBL_ADJ (DBL_MAX_EXP - 2) 03809 #define SIGFIGS ((DBL_MANT_DIG + 3) / 4 + 1) 03810 #define dexp_get(u) ((int)(word0(u) >> Exp_shift) & ~Exp_msk1) 03811 #define dexp_set(u,v) (word0(u) = (((int)(word0(u)) & ~Exp_mask) | ((v) << Exp_shift))) 03812 #define dmanh_get(u) ((uint32_t)(word0(u) & Frac_mask)) 03813 #define dmanl_get(u) ((uint32_t)word1(u)) 03814 03815 03816 /* 03817 * This procedure converts a double-precision number in IEEE format 03818 * into a string of hexadecimal digits and an exponent of 2. Its 03819 * behavior is bug-for-bug compatible with dtoa() in mode 2, with the 03820 * following exceptions: 03821 * 03822 * - An ndigits < 0 causes it to use as many digits as necessary to 03823 * represent the number exactly. 03824 * - The additional xdigs argument should point to either the string 03825 * "0123456789ABCDEF" or the string "0123456789abcdef", depending on 03826 * which case is desired. 03827 * - This routine does not repeat dtoa's mistake of setting decpt 03828 * to 9999 in the case of an infinity or NaN. INT_MAX is used 03829 * for this purpose instead. 03830 * 03831 * Note that the C99 standard does not specify what the leading digit 03832 * should be for non-zero numbers. For instance, 0x1.3p3 is the same 03833 * as 0x2.6p2 is the same as 0x4.cp3. This implementation always makes 03834 * the leading digit a 1. This ensures that the exponent printed is the 03835 * actual base-2 exponent, i.e., ilogb(d). 03836 * 03837 * Inputs: d, xdigs, ndigits 03838 * Outputs: decpt, sign, rve 03839 */ 03840 char * 03841 ruby_hdtoa(double d, const char *xdigs, int ndigits, int *decpt, int *sign, 03842 char **rve) 03843 { 03844 U u; 03845 char *s, *s0; 03846 int bufsize; 03847 uint32_t manh, manl; 03848 03849 u.d = d; 03850 if (word0(u) & Sign_bit) { 03851 /* set sign for everything, including 0's and NaNs */ 03852 *sign = 1; 03853 word0(u) &= ~Sign_bit; /* clear sign bit */ 03854 } 03855 else 03856 *sign = 0; 03857 03858 if (isinf(d)) { /* FP_INFINITE */ 03859 *decpt = INT_MAX; 03860 return rv_strdup(INFSTR, rve); 03861 } 03862 else if (isnan(d)) { /* FP_NAN */ 03863 *decpt = INT_MAX; 03864 return rv_strdup(NANSTR, rve); 03865 } 03866 else if (d == 0.0) { /* FP_ZERO */ 03867 *decpt = 1; 03868 return rv_strdup(ZEROSTR, rve); 03869 } 03870 else if (dexp_get(u)) { /* FP_NORMAL */ 03871 *decpt = dexp_get(u) - DBL_ADJ; 03872 } 03873 else { /* FP_SUBNORMAL */ 03874 u.d *= 5.363123171977039e+154 /* 0x1p514 */; 03875 *decpt = dexp_get(u) - (514 + DBL_ADJ); 03876 } 03877 03878 if (ndigits == 0) /* dtoa() compatibility */ 03879 ndigits = 1; 03880 03881 /* 03882 * If ndigits < 0, we are expected to auto-size, so we allocate 03883 * enough space for all the digits. 03884 */ 03885 bufsize = (ndigits > 0) ? ndigits : SIGFIGS; 03886 s0 = rv_alloc(bufsize+1); 03887 03888 /* Round to the desired number of digits. */ 03889 if (SIGFIGS > ndigits && ndigits > 0) { 03890 float redux = 1.0f; 03891 volatile double d; 03892 int offset = 4 * ndigits + DBL_MAX_EXP - 4 - DBL_MANT_DIG; 03893 dexp_set(u, offset); 03894 d = u.d; 03895 d += redux; 03896 d -= redux; 03897 u.d = d; 03898 *decpt += dexp_get(u) - offset; 03899 } 03900 03901 manh = dmanh_get(u); 03902 manl = dmanl_get(u); 03903 *s0 = '1'; 03904 for (s = s0 + 1; s < s0 + bufsize; s++) { 03905 *s = xdigs[(manh >> (DBL_MANH_SIZE - 4)) & 0xf]; 03906 manh = (manh << 4) | (manl >> (DBL_MANL_SIZE - 4)); 03907 manl <<= 4; 03908 } 03909 03910 /* If ndigits < 0, we are expected to auto-size the precision. */ 03911 if (ndigits < 0) { 03912 for (ndigits = SIGFIGS; s0[ndigits - 1] == '0'; ndigits--) 03913 ; 03914 } 03915 03916 s = s0 + ndigits; 03917 *s = '\0'; 03918 if (rve != NULL) 03919 *rve = s; 03920 return (s0); 03921 } 03922 03923 #ifdef __cplusplus 03924 #if 0 03925 { /* satisfy cc-mode */ 03926 #endif 03927 } 03928 #endif 03929
1.7.6.1