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