Ruby  2.0.0p195(2013-05-14revision40734)
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 
719 #ifndef Omit_Private_Memory
720 #ifndef PRIVATE_MEM
721 #define PRIVATE_MEM 2304
722 #endif
723 #define PRIVATE_mem ((PRIVATE_MEM+sizeof(double)-1)/sizeof(double))
725 #endif
726 
727 #undef IEEE_Arith
728 #undef Avoid_Underflow
729 #ifdef IEEE_BIG_ENDIAN
730 #define IEEE_Arith
731 #endif
732 #ifdef IEEE_LITTLE_ENDIAN
733 #define IEEE_Arith
734 #endif
735 
736 #ifdef Bad_float_h
737 
738 #ifdef IEEE_Arith
739 #define DBL_DIG 15
740 #define DBL_MAX_10_EXP 308
741 #define DBL_MAX_EXP 1024
742 #define FLT_RADIX 2
743 #endif /*IEEE_Arith*/
744 
745 #ifdef IBM
746 #define DBL_DIG 16
747 #define DBL_MAX_10_EXP 75
748 #define DBL_MAX_EXP 63
749 #define FLT_RADIX 16
750 #define DBL_MAX 7.2370055773322621e+75
751 #endif
752 
753 #ifdef VAX
754 #define DBL_DIG 16
755 #define DBL_MAX_10_EXP 38
756 #define DBL_MAX_EXP 127
757 #define FLT_RADIX 2
758 #define DBL_MAX 1.7014118346046923e+38
759 #endif
760 
761 #ifndef LONG_MAX
762 #define LONG_MAX 2147483647
763 #endif
764 
765 #else /* ifndef Bad_float_h */
766 #include "float.h"
767 #endif /* Bad_float_h */
768 
769 #ifndef __MATH_H__
770 #include "math.h"
771 #endif
772 
773 #ifdef __cplusplus
774 extern "C" {
775 #if 0
776 } /* satisfy cc-mode */
777 #endif
778 #endif
779 
780 #if defined(IEEE_LITTLE_ENDIAN) + defined(IEEE_BIG_ENDIAN) + defined(VAX) + defined(IBM) != 1
781 Exactly one of IEEE_LITTLE_ENDIAN, IEEE_BIG_ENDIAN, VAX, or IBM should be defined.
782 #endif
783 
784 typedef union { double d; ULong L[2]; } U;
785 
786 #ifdef YES_ALIAS
787 typedef double double_u;
788 # define dval(x) (x)
789 # ifdef IEEE_LITTLE_ENDIAN
790 # define word0(x) (((ULong *)&(x))[1])
791 # define word1(x) (((ULong *)&(x))[0])
792 # else
793 # define word0(x) (((ULong *)&(x))[0])
794 # define word1(x) (((ULong *)&(x))[1])
795 # endif
796 #else
797 typedef U double_u;
798 # ifdef IEEE_LITTLE_ENDIAN
799 # define word0(x) ((x).L[1])
800 # define word1(x) ((x).L[0])
801 # else
802 # define word0(x) ((x).L[0])
803 # define word1(x) ((x).L[1])
804 # endif
805 # define dval(x) ((x).d)
806 #endif
807 
808 /* The following definition of Storeinc is appropriate for MIPS processors.
809  * An alternative that might be better on some machines is
810  * #define Storeinc(a,b,c) (*a++ = b << 16 | c & 0xffff)
811  */
812 #if defined(IEEE_LITTLE_ENDIAN) + defined(VAX) + defined(__arm__)
813 #define Storeinc(a,b,c) (((unsigned short *)(a))[1] = (unsigned short)(b), \
814 ((unsigned short *)(a))[0] = (unsigned short)(c), (a)++)
815 #else
816 #define Storeinc(a,b,c) (((unsigned short *)(a))[0] = (unsigned short)(b), \
817 ((unsigned short *)(a))[1] = (unsigned short)(c), (a)++)
818 #endif
819 
820 /* #define P DBL_MANT_DIG */
821 /* Ten_pmax = floor(P*log(2)/log(5)) */
822 /* Bletch = (highest power of 2 < DBL_MAX_10_EXP) / 16 */
823 /* Quick_max = floor((P-1)*log(FLT_RADIX)/log(10) - 1) */
824 /* Int_max = floor(P*log(FLT_RADIX)/log(10) - 1) */
825 
826 #ifdef IEEE_Arith
827 #define Exp_shift 20
828 #define Exp_shift1 20
829 #define Exp_msk1 0x100000
830 #define Exp_msk11 0x100000
831 #define Exp_mask 0x7ff00000
832 #define P 53
833 #define Bias 1023
834 #define Emin (-1022)
835 #define Exp_1 0x3ff00000
836 #define Exp_11 0x3ff00000
837 #define Ebits 11
838 #define Frac_mask 0xfffff
839 #define Frac_mask1 0xfffff
840 #define Ten_pmax 22
841 #define Bletch 0x10
842 #define Bndry_mask 0xfffff
843 #define Bndry_mask1 0xfffff
844 #define LSB 1
845 #define Sign_bit 0x80000000
846 #define Log2P 1
847 #define Tiny0 0
848 #define Tiny1 1
849 #define Quick_max 14
850 #define Int_max 14
851 #ifndef NO_IEEE_Scale
852 #define Avoid_Underflow
853 #ifdef Flush_Denorm /* debugging option */
854 #undef Sudden_Underflow
855 #endif
856 #endif
857 
858 #ifndef Flt_Rounds
859 #ifdef FLT_ROUNDS
860 #define Flt_Rounds FLT_ROUNDS
861 #else
862 #define Flt_Rounds 1
863 #endif
864 #endif /*Flt_Rounds*/
865 
866 #ifdef Honor_FLT_ROUNDS
867 #define Rounding rounding
868 #undef Check_FLT_ROUNDS
869 #define Check_FLT_ROUNDS
870 #else
871 #define Rounding Flt_Rounds
872 #endif
873 
874 #else /* ifndef IEEE_Arith */
875 #undef Check_FLT_ROUNDS
876 #undef Honor_FLT_ROUNDS
877 #undef SET_INEXACT
878 #undef Sudden_Underflow
879 #define Sudden_Underflow
880 #ifdef IBM
881 #undef Flt_Rounds
882 #define Flt_Rounds 0
883 #define Exp_shift 24
884 #define Exp_shift1 24
885 #define Exp_msk1 0x1000000
886 #define Exp_msk11 0x1000000
887 #define Exp_mask 0x7f000000
888 #define P 14
889 #define Bias 65
890 #define Exp_1 0x41000000
891 #define Exp_11 0x41000000
892 #define Ebits 8 /* exponent has 7 bits, but 8 is the right value in b2d */
893 #define Frac_mask 0xffffff
894 #define Frac_mask1 0xffffff
895 #define Bletch 4
896 #define Ten_pmax 22
897 #define Bndry_mask 0xefffff
898 #define Bndry_mask1 0xffffff
899 #define LSB 1
900 #define Sign_bit 0x80000000
901 #define Log2P 4
902 #define Tiny0 0x100000
903 #define Tiny1 0
904 #define Quick_max 14
905 #define Int_max 15
906 #else /* VAX */
907 #undef Flt_Rounds
908 #define Flt_Rounds 1
909 #define Exp_shift 23
910 #define Exp_shift1 7
911 #define Exp_msk1 0x80
912 #define Exp_msk11 0x800000
913 #define Exp_mask 0x7f80
914 #define P 56
915 #define Bias 129
916 #define Exp_1 0x40800000
917 #define Exp_11 0x4080
918 #define Ebits 8
919 #define Frac_mask 0x7fffff
920 #define Frac_mask1 0xffff007f
921 #define Ten_pmax 24
922 #define Bletch 2
923 #define Bndry_mask 0xffff007f
924 #define Bndry_mask1 0xffff007f
925 #define LSB 0x10000
926 #define Sign_bit 0x8000
927 #define Log2P 1
928 #define Tiny0 0x80
929 #define Tiny1 0
930 #define Quick_max 15
931 #define Int_max 15
932 #endif /* IBM, VAX */
933 #endif /* IEEE_Arith */
934 
935 #ifndef IEEE_Arith
936 #define ROUND_BIASED
937 #endif
938 
939 #ifdef RND_PRODQUOT
940 #define rounded_product(a,b) ((a) = rnd_prod((a), (b)))
941 #define rounded_quotient(a,b) ((a) = rnd_quot((a), (b)))
942 extern double rnd_prod(double, double), rnd_quot(double, double);
943 #else
944 #define rounded_product(a,b) ((a) *= (b))
945 #define rounded_quotient(a,b) ((a) /= (b))
946 #endif
947 
948 #define Big0 (Frac_mask1 | Exp_msk1*(DBL_MAX_EXP+Bias-1))
949 #define Big1 0xffffffff
950 
951 #ifndef Pack_32
952 #define Pack_32
953 #endif
954 
955 #define FFFFFFFF 0xffffffffUL
956 
957 #ifdef NO_LONG_LONG
958 #undef ULLong
959 #ifdef Just_16
960 #undef Pack_32
961 /* When Pack_32 is not defined, we store 16 bits per 32-bit Long.
962  * This makes some inner loops simpler and sometimes saves work
963  * during multiplications, but it often seems to make things slightly
964  * slower. Hence the default is now to store 32 bits per Long.
965  */
966 #endif
967 #else /* long long available */
968 #ifndef Llong
969 #define Llong long long
970 #endif
971 #ifndef ULLong
972 #define ULLong unsigned Llong
973 #endif
974 #endif /* NO_LONG_LONG */
975 
976 #define MULTIPLE_THREADS 1
977 
978 #ifndef MULTIPLE_THREADS
979 #define ACQUIRE_DTOA_LOCK(n) /*nothing*/
980 #define FREE_DTOA_LOCK(n) /*nothing*/
981 #else
982 #define ACQUIRE_DTOA_LOCK(n) /*unused right now*/
983 #define FREE_DTOA_LOCK(n) /*unused right now*/
984 #endif
985 
986 #define Kmax 15
987 
988 struct Bigint {
989  struct Bigint *next;
990  int k, maxwds, sign, wds;
991  ULong x[1];
992 };
993 
994 typedef struct Bigint Bigint;
995 
996 static Bigint *freelist[Kmax+1];
997 
998 static Bigint *
999 Balloc(int k)
1000 {
1001  int x;
1002  Bigint *rv;
1003 #ifndef Omit_Private_Memory
1004  size_t len;
1005 #endif
1006 
1007  ACQUIRE_DTOA_LOCK(0);
1008  if ((rv = freelist[k]) != 0) {
1009  freelist[k] = rv->next;
1010  }
1011  else {
1012  x = 1 << k;
1013 #ifdef Omit_Private_Memory
1014  rv = (Bigint *)MALLOC(sizeof(Bigint) + (x-1)*sizeof(ULong));
1015 #else
1016  len = (sizeof(Bigint) + (x-1)*sizeof(ULong) + sizeof(double) - 1)
1017  /sizeof(double);
1018  if (pmem_next - private_mem + len <= PRIVATE_mem) {
1019  rv = (Bigint*)pmem_next;
1020  pmem_next += len;
1021  }
1022  else
1023  rv = (Bigint*)MALLOC(len*sizeof(double));
1024 #endif
1025  rv->k = k;
1026  rv->maxwds = x;
1027  }
1028  FREE_DTOA_LOCK(0);
1029  rv->sign = rv->wds = 0;
1030  return rv;
1031 }
1032 
1033 static void
1035 {
1036  if (v) {
1037  ACQUIRE_DTOA_LOCK(0);
1038  v->next = freelist[v->k];
1039  freelist[v->k] = v;
1040  FREE_DTOA_LOCK(0);
1041  }
1042 }
1043 
1044 #define Bcopy(x,y) memcpy((char *)&(x)->sign, (char *)&(y)->sign, \
1045 (y)->wds*sizeof(Long) + 2*sizeof(int))
1046 
1047 static Bigint *
1048 multadd(Bigint *b, int m, int a) /* multiply by m and add a */
1049 {
1050  int i, wds;
1051  ULong *x;
1052 #ifdef ULLong
1053  ULLong carry, y;
1054 #else
1055  ULong carry, y;
1056 #ifdef Pack_32
1057  ULong xi, z;
1058 #endif
1059 #endif
1060  Bigint *b1;
1061 
1062  wds = b->wds;
1063  x = b->x;
1064  i = 0;
1065  carry = a;
1066  do {
1067 #ifdef ULLong
1068  y = *x * (ULLong)m + carry;
1069  carry = y >> 32;
1070  *x++ = (ULong)(y & FFFFFFFF);
1071 #else
1072 #ifdef Pack_32
1073  xi = *x;
1074  y = (xi & 0xffff) * m + carry;
1075  z = (xi >> 16) * m + (y >> 16);
1076  carry = z >> 16;
1077  *x++ = (z << 16) + (y & 0xffff);
1078 #else
1079  y = *x * m + carry;
1080  carry = y >> 16;
1081  *x++ = y & 0xffff;
1082 #endif
1083 #endif
1084  } while (++i < wds);
1085  if (carry) {
1086  if (wds >= b->maxwds) {
1087  b1 = Balloc(b->k+1);
1088  Bcopy(b1, b);
1089  Bfree(b);
1090  b = b1;
1091  }
1092  b->x[wds++] = (ULong)carry;
1093  b->wds = wds;
1094  }
1095  return b;
1096 }
1097 
1098 static Bigint *
1099 s2b(const char *s, int nd0, int nd, ULong y9)
1100 {
1101  Bigint *b;
1102  int i, k;
1103  Long x, y;
1104 
1105  x = (nd + 8) / 9;
1106  for (k = 0, y = 1; x > y; y <<= 1, k++) ;
1107 #ifdef Pack_32
1108  b = Balloc(k);
1109  b->x[0] = y9;
1110  b->wds = 1;
1111 #else
1112  b = Balloc(k+1);
1113  b->x[0] = y9 & 0xffff;
1114  b->wds = (b->x[1] = y9 >> 16) ? 2 : 1;
1115 #endif
1116 
1117  i = 9;
1118  if (9 < nd0) {
1119  s += 9;
1120  do {
1121  b = multadd(b, 10, *s++ - '0');
1122  } while (++i < nd0);
1123  s++;
1124  }
1125  else
1126  s += 10;
1127  for (; i < nd; i++)
1128  b = multadd(b, 10, *s++ - '0');
1129  return b;
1130 }
1131 
1132 static int
1133 hi0bits(register ULong x)
1134 {
1135  register int k = 0;
1136 
1137  if (!(x & 0xffff0000)) {
1138  k = 16;
1139  x <<= 16;
1140  }
1141  if (!(x & 0xff000000)) {
1142  k += 8;
1143  x <<= 8;
1144  }
1145  if (!(x & 0xf0000000)) {
1146  k += 4;
1147  x <<= 4;
1148  }
1149  if (!(x & 0xc0000000)) {
1150  k += 2;
1151  x <<= 2;
1152  }
1153  if (!(x & 0x80000000)) {
1154  k++;
1155  if (!(x & 0x40000000))
1156  return 32;
1157  }
1158  return k;
1159 }
1160 
1161 static int
1162 lo0bits(ULong *y)
1163 {
1164  register int k;
1165  register ULong x = *y;
1166 
1167  if (x & 7) {
1168  if (x & 1)
1169  return 0;
1170  if (x & 2) {
1171  *y = x >> 1;
1172  return 1;
1173  }
1174  *y = x >> 2;
1175  return 2;
1176  }
1177  k = 0;
1178  if (!(x & 0xffff)) {
1179  k = 16;
1180  x >>= 16;
1181  }
1182  if (!(x & 0xff)) {
1183  k += 8;
1184  x >>= 8;
1185  }
1186  if (!(x & 0xf)) {
1187  k += 4;
1188  x >>= 4;
1189  }
1190  if (!(x & 0x3)) {
1191  k += 2;
1192  x >>= 2;
1193  }
1194  if (!(x & 1)) {
1195  k++;
1196  x >>= 1;
1197  if (!x)
1198  return 32;
1199  }
1200  *y = x;
1201  return k;
1202 }
1203 
1204 static Bigint *
1205 i2b(int i)
1206 {
1207  Bigint *b;
1208 
1209  b = Balloc(1);
1210  b->x[0] = i;
1211  b->wds = 1;
1212  return b;
1213 }
1214 
1215 static Bigint *
1217 {
1218  Bigint *c;
1219  int k, wa, wb, wc;
1220  ULong *x, *xa, *xae, *xb, *xbe, *xc, *xc0;
1221  ULong y;
1222 #ifdef ULLong
1223  ULLong carry, z;
1224 #else
1225  ULong carry, z;
1226 #ifdef Pack_32
1227  ULong z2;
1228 #endif
1229 #endif
1230 
1231  if (a->wds < b->wds) {
1232  c = a;
1233  a = b;
1234  b = c;
1235  }
1236  k = a->k;
1237  wa = a->wds;
1238  wb = b->wds;
1239  wc = wa + wb;
1240  if (wc > a->maxwds)
1241  k++;
1242  c = Balloc(k);
1243  for (x = c->x, xa = x + wc; x < xa; x++)
1244  *x = 0;
1245  xa = a->x;
1246  xae = xa + wa;
1247  xb = b->x;
1248  xbe = xb + wb;
1249  xc0 = c->x;
1250 #ifdef ULLong
1251  for (; xb < xbe; xc0++) {
1252  if ((y = *xb++) != 0) {
1253  x = xa;
1254  xc = xc0;
1255  carry = 0;
1256  do {
1257  z = *x++ * (ULLong)y + *xc + carry;
1258  carry = z >> 32;
1259  *xc++ = (ULong)(z & FFFFFFFF);
1260  } while (x < xae);
1261  *xc = (ULong)carry;
1262  }
1263  }
1264 #else
1265 #ifdef Pack_32
1266  for (; xb < xbe; xb++, xc0++) {
1267  if (y = *xb & 0xffff) {
1268  x = xa;
1269  xc = xc0;
1270  carry = 0;
1271  do {
1272  z = (*x & 0xffff) * y + (*xc & 0xffff) + carry;
1273  carry = z >> 16;
1274  z2 = (*x++ >> 16) * y + (*xc >> 16) + carry;
1275  carry = z2 >> 16;
1276  Storeinc(xc, z2, z);
1277  } while (x < xae);
1278  *xc = (ULong)carry;
1279  }
1280  if (y = *xb >> 16) {
1281  x = xa;
1282  xc = xc0;
1283  carry = 0;
1284  z2 = *xc;
1285  do {
1286  z = (*x & 0xffff) * y + (*xc >> 16) + carry;
1287  carry = z >> 16;
1288  Storeinc(xc, z, z2);
1289  z2 = (*x++ >> 16) * y + (*xc & 0xffff) + carry;
1290  carry = z2 >> 16;
1291  } while (x < xae);
1292  *xc = z2;
1293  }
1294  }
1295 #else
1296  for (; xb < xbe; xc0++) {
1297  if (y = *xb++) {
1298  x = xa;
1299  xc = xc0;
1300  carry = 0;
1301  do {
1302  z = *x++ * y + *xc + carry;
1303  carry = z >> 16;
1304  *xc++ = z & 0xffff;
1305  } while (x < xae);
1306  *xc = (ULong)carry;
1307  }
1308  }
1309 #endif
1310 #endif
1311  for (xc0 = c->x, xc = xc0 + wc; wc > 0 && !*--xc; --wc) ;
1312  c->wds = wc;
1313  return c;
1314 }
1315 
1316 static Bigint *p5s;
1317 
1318 static Bigint *
1320 {
1321  Bigint *b1, *p5, *p51;
1322  int i;
1323  static int p05[3] = { 5, 25, 125 };
1324 
1325  if ((i = k & 3) != 0)
1326  b = multadd(b, p05[i-1], 0);
1327 
1328  if (!(k >>= 2))
1329  return b;
1330  if (!(p5 = p5s)) {
1331  /* first time */
1332 #ifdef MULTIPLE_THREADS
1333  ACQUIRE_DTOA_LOCK(1);
1334  if (!(p5 = p5s)) {
1335  p5 = p5s = i2b(625);
1336  p5->next = 0;
1337  }
1338  FREE_DTOA_LOCK(1);
1339 #else
1340  p5 = p5s = i2b(625);
1341  p5->next = 0;
1342 #endif
1343  }
1344  for (;;) {
1345  if (k & 1) {
1346  b1 = mult(b, p5);
1347  Bfree(b);
1348  b = b1;
1349  }
1350  if (!(k >>= 1))
1351  break;
1352  if (!(p51 = p5->next)) {
1353 #ifdef MULTIPLE_THREADS
1354  ACQUIRE_DTOA_LOCK(1);
1355  if (!(p51 = p5->next)) {
1356  p51 = p5->next = mult(p5,p5);
1357  p51->next = 0;
1358  }
1359  FREE_DTOA_LOCK(1);
1360 #else
1361  p51 = p5->next = mult(p5,p5);
1362  p51->next = 0;
1363 #endif
1364  }
1365  p5 = p51;
1366  }
1367  return b;
1368 }
1369 
1370 static Bigint *
1371 lshift(Bigint *b, int k)
1372 {
1373  int i, k1, n, n1;
1374  Bigint *b1;
1375  ULong *x, *x1, *xe, z;
1376 
1377 #ifdef Pack_32
1378  n = k >> 5;
1379 #else
1380  n = k >> 4;
1381 #endif
1382  k1 = b->k;
1383  n1 = n + b->wds + 1;
1384  for (i = b->maxwds; n1 > i; i <<= 1)
1385  k1++;
1386  b1 = Balloc(k1);
1387  x1 = b1->x;
1388  for (i = 0; i < n; i++)
1389  *x1++ = 0;
1390  x = b->x;
1391  xe = x + b->wds;
1392 #ifdef Pack_32
1393  if (k &= 0x1f) {
1394  k1 = 32 - k;
1395  z = 0;
1396  do {
1397  *x1++ = *x << k | z;
1398  z = *x++ >> k1;
1399  } while (x < xe);
1400  if ((*x1 = z) != 0)
1401  ++n1;
1402  }
1403 #else
1404  if (k &= 0xf) {
1405  k1 = 16 - k;
1406  z = 0;
1407  do {
1408  *x1++ = *x << k & 0xffff | z;
1409  z = *x++ >> k1;
1410  } while (x < xe);
1411  if (*x1 = z)
1412  ++n1;
1413  }
1414 #endif
1415  else
1416  do {
1417  *x1++ = *x++;
1418  } while (x < xe);
1419  b1->wds = n1 - 1;
1420  Bfree(b);
1421  return b1;
1422 }
1423 
1424 static int
1426 {
1427  ULong *xa, *xa0, *xb, *xb0;
1428  int i, j;
1429 
1430  i = a->wds;
1431  j = b->wds;
1432 #ifdef DEBUG
1433  if (i > 1 && !a->x[i-1])
1434  Bug("cmp called with a->x[a->wds-1] == 0");
1435  if (j > 1 && !b->x[j-1])
1436  Bug("cmp called with b->x[b->wds-1] == 0");
1437 #endif
1438  if (i -= j)
1439  return i;
1440  xa0 = a->x;
1441  xa = xa0 + j;
1442  xb0 = b->x;
1443  xb = xb0 + j;
1444  for (;;) {
1445  if (*--xa != *--xb)
1446  return *xa < *xb ? -1 : 1;
1447  if (xa <= xa0)
1448  break;
1449  }
1450  return 0;
1451 }
1452 
1453 static Bigint *
1455 {
1456  Bigint *c;
1457  int i, wa, wb;
1458  ULong *xa, *xae, *xb, *xbe, *xc;
1459 #ifdef ULLong
1460  ULLong borrow, y;
1461 #else
1462  ULong borrow, y;
1463 #ifdef Pack_32
1464  ULong z;
1465 #endif
1466 #endif
1467 
1468  i = cmp(a,b);
1469  if (!i) {
1470  c = Balloc(0);
1471  c->wds = 1;
1472  c->x[0] = 0;
1473  return c;
1474  }
1475  if (i < 0) {
1476  c = a;
1477  a = b;
1478  b = c;
1479  i = 1;
1480  }
1481  else
1482  i = 0;
1483  c = Balloc(a->k);
1484  c->sign = i;
1485  wa = a->wds;
1486  xa = a->x;
1487  xae = xa + wa;
1488  wb = b->wds;
1489  xb = b->x;
1490  xbe = xb + wb;
1491  xc = c->x;
1492  borrow = 0;
1493 #ifdef ULLong
1494  do {
1495  y = (ULLong)*xa++ - *xb++ - borrow;
1496  borrow = y >> 32 & (ULong)1;
1497  *xc++ = (ULong)(y & FFFFFFFF);
1498  } while (xb < xbe);
1499  while (xa < xae) {
1500  y = *xa++ - borrow;
1501  borrow = y >> 32 & (ULong)1;
1502  *xc++ = (ULong)(y & FFFFFFFF);
1503  }
1504 #else
1505 #ifdef Pack_32
1506  do {
1507  y = (*xa & 0xffff) - (*xb & 0xffff) - borrow;
1508  borrow = (y & 0x10000) >> 16;
1509  z = (*xa++ >> 16) - (*xb++ >> 16) - borrow;
1510  borrow = (z & 0x10000) >> 16;
1511  Storeinc(xc, z, y);
1512  } while (xb < xbe);
1513  while (xa < xae) {
1514  y = (*xa & 0xffff) - borrow;
1515  borrow = (y & 0x10000) >> 16;
1516  z = (*xa++ >> 16) - borrow;
1517  borrow = (z & 0x10000) >> 16;
1518  Storeinc(xc, z, y);
1519  }
1520 #else
1521  do {
1522  y = *xa++ - *xb++ - borrow;
1523  borrow = (y & 0x10000) >> 16;
1524  *xc++ = y & 0xffff;
1525  } while (xb < xbe);
1526  while (xa < xae) {
1527  y = *xa++ - borrow;
1528  borrow = (y & 0x10000) >> 16;
1529  *xc++ = y & 0xffff;
1530  }
1531 #endif
1532 #endif
1533  while (!*--xc)
1534  wa--;
1535  c->wds = wa;
1536  return c;
1537 }
1538 
1539 static double
1540 ulp(double x_)
1541 {
1542  register Long L;
1543  double_u x, a;
1544  dval(x) = x_;
1545 
1546  L = (word0(x) & Exp_mask) - (P-1)*Exp_msk1;
1547 #ifndef Avoid_Underflow
1548 #ifndef Sudden_Underflow
1549  if (L > 0) {
1550 #endif
1551 #endif
1552 #ifdef IBM
1553  L |= Exp_msk1 >> 4;
1554 #endif
1555  word0(a) = L;
1556  word1(a) = 0;
1557 #ifndef Avoid_Underflow
1558 #ifndef Sudden_Underflow
1559  }
1560  else {
1561  L = -L >> Exp_shift;
1562  if (L < Exp_shift) {
1563  word0(a) = 0x80000 >> L;
1564  word1(a) = 0;
1565  }
1566  else {
1567  word0(a) = 0;
1568  L -= Exp_shift;
1569  word1(a) = L >= 31 ? 1 : 1 << 31 - L;
1570  }
1571  }
1572 #endif
1573 #endif
1574  return dval(a);
1575 }
1576 
1577 static double
1578 b2d(Bigint *a, int *e)
1579 {
1580  ULong *xa, *xa0, w, y, z;
1581  int k;
1582  double_u d;
1583 #ifdef VAX
1584  ULong d0, d1;
1585 #else
1586 #define d0 word0(d)
1587 #define d1 word1(d)
1588 #endif
1589 
1590  xa0 = a->x;
1591  xa = xa0 + a->wds;
1592  y = *--xa;
1593 #ifdef DEBUG
1594  if (!y) Bug("zero y in b2d");
1595 #endif
1596  k = hi0bits(y);
1597  *e = 32 - k;
1598 #ifdef Pack_32
1599  if (k < Ebits) {
1600  d0 = Exp_1 | y >> (Ebits - k);
1601  w = xa > xa0 ? *--xa : 0;
1602  d1 = y << ((32-Ebits) + k) | w >> (Ebits - k);
1603  goto ret_d;
1604  }
1605  z = xa > xa0 ? *--xa : 0;
1606  if (k -= Ebits) {
1607  d0 = Exp_1 | y << k | z >> (32 - k);
1608  y = xa > xa0 ? *--xa : 0;
1609  d1 = z << k | y >> (32 - k);
1610  }
1611  else {
1612  d0 = Exp_1 | y;
1613  d1 = z;
1614  }
1615 #else
1616  if (k < Ebits + 16) {
1617  z = xa > xa0 ? *--xa : 0;
1618  d0 = Exp_1 | y << k - Ebits | z >> Ebits + 16 - k;
1619  w = xa > xa0 ? *--xa : 0;
1620  y = xa > xa0 ? *--xa : 0;
1621  d1 = z << k + 16 - Ebits | w << k - Ebits | y >> 16 + Ebits - k;
1622  goto ret_d;
1623  }
1624  z = xa > xa0 ? *--xa : 0;
1625  w = xa > xa0 ? *--xa : 0;
1626  k -= Ebits + 16;
1627  d0 = Exp_1 | y << k + 16 | z << k | w >> 16 - k;
1628  y = xa > xa0 ? *--xa : 0;
1629  d1 = w << k + 16 | y << k;
1630 #endif
1631 ret_d:
1632 #ifdef VAX
1633  word0(d) = d0 >> 16 | d0 << 16;
1634  word1(d) = d1 >> 16 | d1 << 16;
1635 #else
1636 #undef d0
1637 #undef d1
1638 #endif
1639  return dval(d);
1640 }
1641 
1642 static Bigint *
1643 d2b(double d_, int *e, int *bits)
1644 {
1645  double_u d;
1646  Bigint *b;
1647  int de, k;
1648  ULong *x, y, z;
1649 #ifndef Sudden_Underflow
1650  int i;
1651 #endif
1652 #ifdef VAX
1653  ULong d0, d1;
1654 #endif
1655  dval(d) = d_;
1656 #ifdef VAX
1657  d0 = word0(d) >> 16 | word0(d) << 16;
1658  d1 = word1(d) >> 16 | word1(d) << 16;
1659 #else
1660 #define d0 word0(d)
1661 #define d1 word1(d)
1662 #endif
1663 
1664 #ifdef Pack_32
1665  b = Balloc(1);
1666 #else
1667  b = Balloc(2);
1668 #endif
1669  x = b->x;
1670 
1671  z = d0 & Frac_mask;
1672  d0 &= 0x7fffffff; /* clear sign bit, which we ignore */
1673 #ifdef Sudden_Underflow
1674  de = (int)(d0 >> Exp_shift);
1675 #ifndef IBM
1676  z |= Exp_msk11;
1677 #endif
1678 #else
1679  if ((de = (int)(d0 >> Exp_shift)) != 0)
1680  z |= Exp_msk1;
1681 #endif
1682 #ifdef Pack_32
1683  if ((y = d1) != 0) {
1684  if ((k = lo0bits(&y)) != 0) {
1685  x[0] = y | z << (32 - k);
1686  z >>= k;
1687  }
1688  else
1689  x[0] = y;
1690 #ifndef Sudden_Underflow
1691  i =
1692 #endif
1693  b->wds = (x[1] = z) ? 2 : 1;
1694  }
1695  else {
1696 #ifdef DEBUG
1697  if (!z)
1698  Bug("Zero passed to d2b");
1699 #endif
1700  k = lo0bits(&z);
1701  x[0] = z;
1702 #ifndef Sudden_Underflow
1703  i =
1704 #endif
1705  b->wds = 1;
1706  k += 32;
1707  }
1708 #else
1709  if (y = d1) {
1710  if (k = lo0bits(&y))
1711  if (k >= 16) {
1712  x[0] = y | z << 32 - k & 0xffff;
1713  x[1] = z >> k - 16 & 0xffff;
1714  x[2] = z >> k;
1715  i = 2;
1716  }
1717  else {
1718  x[0] = y & 0xffff;
1719  x[1] = y >> 16 | z << 16 - k & 0xffff;
1720  x[2] = z >> k & 0xffff;
1721  x[3] = z >> k+16;
1722  i = 3;
1723  }
1724  else {
1725  x[0] = y & 0xffff;
1726  x[1] = y >> 16;
1727  x[2] = z & 0xffff;
1728  x[3] = z >> 16;
1729  i = 3;
1730  }
1731  }
1732  else {
1733 #ifdef DEBUG
1734  if (!z)
1735  Bug("Zero passed to d2b");
1736 #endif
1737  k = lo0bits(&z);
1738  if (k >= 16) {
1739  x[0] = z;
1740  i = 0;
1741  }
1742  else {
1743  x[0] = z & 0xffff;
1744  x[1] = z >> 16;
1745  i = 1;
1746  }
1747  k += 32;
1748  }
1749  while (!x[i])
1750  --i;
1751  b->wds = i + 1;
1752 #endif
1753 #ifndef Sudden_Underflow
1754  if (de) {
1755 #endif
1756 #ifdef IBM
1757  *e = (de - Bias - (P-1) << 2) + k;
1758  *bits = 4*P + 8 - k - hi0bits(word0(d) & Frac_mask);
1759 #else
1760  *e = de - Bias - (P-1) + k;
1761  *bits = P - k;
1762 #endif
1763 #ifndef Sudden_Underflow
1764  }
1765  else {
1766  *e = de - Bias - (P-1) + 1 + k;
1767 #ifdef Pack_32
1768  *bits = 32*i - hi0bits(x[i-1]);
1769 #else
1770  *bits = (i+2)*16 - hi0bits(x[i]);
1771 #endif
1772  }
1773 #endif
1774  return b;
1775 }
1776 #undef d0
1777 #undef d1
1778 
1779 static double
1781 {
1782  double_u da, db;
1783  int k, ka, kb;
1784 
1785  dval(da) = b2d(a, &ka);
1786  dval(db) = b2d(b, &kb);
1787 #ifdef Pack_32
1788  k = ka - kb + 32*(a->wds - b->wds);
1789 #else
1790  k = ka - kb + 16*(a->wds - b->wds);
1791 #endif
1792 #ifdef IBM
1793  if (k > 0) {
1794  word0(da) += (k >> 2)*Exp_msk1;
1795  if (k &= 3)
1796  dval(da) *= 1 << k;
1797  }
1798  else {
1799  k = -k;
1800  word0(db) += (k >> 2)*Exp_msk1;
1801  if (k &= 3)
1802  dval(db) *= 1 << k;
1803  }
1804 #else
1805  if (k > 0)
1806  word0(da) += k*Exp_msk1;
1807  else {
1808  k = -k;
1809  word0(db) += k*Exp_msk1;
1810  }
1811 #endif
1812  return dval(da) / dval(db);
1813 }
1814 
1815 static const double
1816 tens[] = {
1817  1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9,
1818  1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19,
1819  1e20, 1e21, 1e22
1820 #ifdef VAX
1821  , 1e23, 1e24
1822 #endif
1823 };
1824 
1825 static const double
1826 #ifdef IEEE_Arith
1827 bigtens[] = { 1e16, 1e32, 1e64, 1e128, 1e256 };
1828 static const double tinytens[] = { 1e-16, 1e-32, 1e-64, 1e-128,
1829 #ifdef Avoid_Underflow
1830  9007199254740992.*9007199254740992.e-256
1831  /* = 2^106 * 1e-53 */
1832 #else
1833  1e-256
1834 #endif
1835 };
1836 /* The factor of 2^53 in tinytens[4] helps us avoid setting the underflow */
1837 /* flag unnecessarily. It leads to a song and dance at the end of strtod. */
1838 #define Scale_Bit 0x10
1839 #define n_bigtens 5
1840 #else
1841 #ifdef IBM
1842 bigtens[] = { 1e16, 1e32, 1e64 };
1843 static const double tinytens[] = { 1e-16, 1e-32, 1e-64 };
1844 #define n_bigtens 3
1845 #else
1846 bigtens[] = { 1e16, 1e32 };
1847 static const double tinytens[] = { 1e-16, 1e-32 };
1848 #define n_bigtens 2
1849 #endif
1850 #endif
1851 
1852 #ifndef IEEE_Arith
1853 #undef INFNAN_CHECK
1854 #endif
1855 
1856 #ifdef INFNAN_CHECK
1857 
1858 #ifndef NAN_WORD0
1859 #define NAN_WORD0 0x7ff80000
1860 #endif
1861 
1862 #ifndef NAN_WORD1
1863 #define NAN_WORD1 0
1864 #endif
1865 
1866 static int
1867 match(const char **sp, char *t)
1868 {
1869  int c, d;
1870  const char *s = *sp;
1871 
1872  while (d = *t++) {
1873  if ((c = *++s) >= 'A' && c <= 'Z')
1874  c += 'a' - 'A';
1875  if (c != d)
1876  return 0;
1877  }
1878  *sp = s + 1;
1879  return 1;
1880 }
1881 
1882 #ifndef No_Hex_NaN
1883 static void
1884 hexnan(double *rvp, const char **sp)
1885 {
1886  ULong c, x[2];
1887  const char *s;
1888  int havedig, udx0, xshift;
1889 
1890  x[0] = x[1] = 0;
1891  havedig = xshift = 0;
1892  udx0 = 1;
1893  s = *sp;
1894  while (c = *(const unsigned char*)++s) {
1895  if (c >= '0' && c <= '9')
1896  c -= '0';
1897  else if (c >= 'a' && c <= 'f')
1898  c += 10 - 'a';
1899  else if (c >= 'A' && c <= 'F')
1900  c += 10 - 'A';
1901  else if (c <= ' ') {
1902  if (udx0 && havedig) {
1903  udx0 = 0;
1904  xshift = 1;
1905  }
1906  continue;
1907  }
1908  else if (/*(*/ c == ')' && havedig) {
1909  *sp = s + 1;
1910  break;
1911  }
1912  else
1913  return; /* invalid form: don't change *sp */
1914  havedig = 1;
1915  if (xshift) {
1916  xshift = 0;
1917  x[0] = x[1];
1918  x[1] = 0;
1919  }
1920  if (udx0)
1921  x[0] = (x[0] << 4) | (x[1] >> 28);
1922  x[1] = (x[1] << 4) | c;
1923  }
1924  if ((x[0] &= 0xfffff) || x[1]) {
1925  word0(*rvp) = Exp_mask | x[0];
1926  word1(*rvp) = x[1];
1927  }
1928 }
1929 #endif /*No_Hex_NaN*/
1930 #endif /* INFNAN_CHECK */
1931 
1932 double
1933 ruby_strtod(const char *s00, char **se)
1934 {
1935 #ifdef Avoid_Underflow
1936  int scale;
1937 #endif
1938  int bb2, bb5, bbe, bd2, bd5, bbbits, bs2, c, dsign,
1939  e, e1, esign, i, j, k, nd, nd0, nf, nz, nz0, sign;
1940  const char *s, *s0, *s1;
1941  double aadj, adj;
1942  double_u aadj1, rv, rv0;
1943  Long L;
1944  ULong y, z;
1945  Bigint *bb, *bb1, *bd, *bd0, *bs, *delta;
1946 #ifdef SET_INEXACT
1947  int inexact, oldinexact;
1948 #endif
1949 #ifdef Honor_FLT_ROUNDS
1950  int rounding;
1951 #endif
1952 #ifdef USE_LOCALE
1953  const char *s2;
1954 #endif
1955 
1956  errno = 0;
1957  sign = nz0 = nz = 0;
1958  dval(rv) = 0.;
1959  for (s = s00;;s++)
1960  switch (*s) {
1961  case '-':
1962  sign = 1;
1963  /* no break */
1964  case '+':
1965  if (*++s)
1966  goto break2;
1967  /* no break */
1968  case 0:
1969  goto ret0;
1970  case '\t':
1971  case '\n':
1972  case '\v':
1973  case '\f':
1974  case '\r':
1975  case ' ':
1976  continue;
1977  default:
1978  goto break2;
1979  }
1980 break2:
1981  if (*s == '0') {
1982  if (s[1] == 'x' || s[1] == 'X') {
1983  static const char hexdigit[] = "0123456789abcdef0123456789ABCDEF";
1984  s0 = ++s;
1985  adj = 0;
1986  aadj = 1.0;
1987  nd0 = -4;
1988 
1989  if (!*++s || !(s1 = strchr(hexdigit, *s))) goto ret0;
1990  if (*s == '0') {
1991  while (*++s == '0');
1992  s1 = strchr(hexdigit, *s);
1993  }
1994  if (s1 != NULL) {
1995  do {
1996  adj += aadj * ((s1 - hexdigit) & 15);
1997  nd0 += 4;
1998  aadj /= 16;
1999  } while (*++s && (s1 = strchr(hexdigit, *s)));
2000  }
2001 
2002  if (*s == '.') {
2003  dsign = 1;
2004  if (!*++s || !(s1 = strchr(hexdigit, *s))) goto ret0;
2005  if (nd0 < 0) {
2006  while (*s == '0') {
2007  s++;
2008  nd0 -= 4;
2009  }
2010  }
2011  for (; *s && (s1 = strchr(hexdigit, *s)); ++s) {
2012  adj += aadj * ((s1 - hexdigit) & 15);
2013  if ((aadj /= 16) == 0.0) {
2014  while (strchr(hexdigit, *++s));
2015  break;
2016  }
2017  }
2018  }
2019  else {
2020  dsign = 0;
2021  }
2022 
2023  if (*s == 'P' || *s == 'p') {
2024  dsign = 0x2C - *++s; /* +: 2B, -: 2D */
2025  if (abs(dsign) == 1) s++;
2026  else dsign = 1;
2027 
2028  nd = 0;
2029  c = *s;
2030  if (c < '0' || '9' < c) goto ret0;
2031  do {
2032  nd *= 10;
2033  nd += c;
2034  nd -= '0';
2035  c = *++s;
2036  /* Float("0x0."+("0"*267)+"1fp2095") */
2037  if (nd + dsign * nd0 > 2095) {
2038  while ('0' <= c && c <= '9') c = *++s;
2039  break;
2040  }
2041  } while ('0' <= c && c <= '9');
2042  nd0 += nd * dsign;
2043  }
2044  else {
2045  if (dsign) goto ret0;
2046  }
2047  dval(rv) = ldexp(adj, nd0);
2048  goto ret;
2049  }
2050  nz0 = 1;
2051  while (*++s == '0') ;
2052  if (!*s)
2053  goto ret;
2054  }
2055  s0 = s;
2056  y = z = 0;
2057  for (nd = nf = 0; (c = *s) >= '0' && c <= '9'; nd++, s++)
2058  if (nd < 9)
2059  y = 10*y + c - '0';
2060  else if (nd < 16)
2061  z = 10*z + c - '0';
2062  nd0 = nd;
2063 #ifdef USE_LOCALE
2064  s1 = localeconv()->decimal_point;
2065  if (c == *s1) {
2066  c = '.';
2067  if (*++s1) {
2068  s2 = s;
2069  for (;;) {
2070  if (*++s2 != *s1) {
2071  c = 0;
2072  break;
2073  }
2074  if (!*++s1) {
2075  s = s2;
2076  break;
2077  }
2078  }
2079  }
2080  }
2081 #endif
2082  if (c == '.') {
2083  if (!ISDIGIT(s[1]))
2084  goto dig_done;
2085  c = *++s;
2086  if (!nd) {
2087  for (; c == '0'; c = *++s)
2088  nz++;
2089  if (c > '0' && c <= '9') {
2090  s0 = s;
2091  nf += nz;
2092  nz = 0;
2093  goto have_dig;
2094  }
2095  goto dig_done;
2096  }
2097  for (; c >= '0' && c <= '9'; c = *++s) {
2098 have_dig:
2099  nz++;
2100  if (c -= '0') {
2101  nf += nz;
2102  for (i = 1; i < nz; i++)
2103  if (nd++ < 9)
2104  y *= 10;
2105  else if (nd <= DBL_DIG + 1)
2106  z *= 10;
2107  if (nd++ < 9)
2108  y = 10*y + c;
2109  else if (nd <= DBL_DIG + 1)
2110  z = 10*z + c;
2111  nz = 0;
2112  }
2113  }
2114  }
2115 dig_done:
2116  e = 0;
2117  if (c == 'e' || c == 'E') {
2118  if (!nd && !nz && !nz0) {
2119  goto ret0;
2120  }
2121  s00 = s;
2122  esign = 0;
2123  switch (c = *++s) {
2124  case '-':
2125  esign = 1;
2126  case '+':
2127  c = *++s;
2128  }
2129  if (c >= '0' && c <= '9') {
2130  while (c == '0')
2131  c = *++s;
2132  if (c > '0' && c <= '9') {
2133  L = c - '0';
2134  s1 = s;
2135  while ((c = *++s) >= '0' && c <= '9')
2136  L = 10*L + c - '0';
2137  if (s - s1 > 8 || L > 19999)
2138  /* Avoid confusion from exponents
2139  * so large that e might overflow.
2140  */
2141  e = 19999; /* safe for 16 bit ints */
2142  else
2143  e = (int)L;
2144  if (esign)
2145  e = -e;
2146  }
2147  else
2148  e = 0;
2149  }
2150  else
2151  s = s00;
2152  }
2153  if (!nd) {
2154  if (!nz && !nz0) {
2155 #ifdef INFNAN_CHECK
2156  /* Check for Nan and Infinity */
2157  switch (c) {
2158  case 'i':
2159  case 'I':
2160  if (match(&s,"nf")) {
2161  --s;
2162  if (!match(&s,"inity"))
2163  ++s;
2164  word0(rv) = 0x7ff00000;
2165  word1(rv) = 0;
2166  goto ret;
2167  }
2168  break;
2169  case 'n':
2170  case 'N':
2171  if (match(&s, "an")) {
2172  word0(rv) = NAN_WORD0;
2173  word1(rv) = NAN_WORD1;
2174 #ifndef No_Hex_NaN
2175  if (*s == '(') /*)*/
2176  hexnan(&rv, &s);
2177 #endif
2178  goto ret;
2179  }
2180  }
2181 #endif /* INFNAN_CHECK */
2182 ret0:
2183  s = s00;
2184  sign = 0;
2185  }
2186  goto ret;
2187  }
2188  e1 = e -= nf;
2189 
2190  /* Now we have nd0 digits, starting at s0, followed by a
2191  * decimal point, followed by nd-nd0 digits. The number we're
2192  * after is the integer represented by those digits times
2193  * 10**e */
2194 
2195  if (!nd0)
2196  nd0 = nd;
2197  k = nd < DBL_DIG + 1 ? nd : DBL_DIG + 1;
2198  dval(rv) = y;
2199  if (k > 9) {
2200 #ifdef SET_INEXACT
2201  if (k > DBL_DIG)
2202  oldinexact = get_inexact();
2203 #endif
2204  dval(rv) = tens[k - 9] * dval(rv) + z;
2205  }
2206  bd0 = bb = bd = bs = delta = 0;
2207  if (nd <= DBL_DIG
2208 #ifndef RND_PRODQUOT
2209 #ifndef Honor_FLT_ROUNDS
2210  && Flt_Rounds == 1
2211 #endif
2212 #endif
2213  ) {
2214  if (!e)
2215  goto ret;
2216  if (e > 0) {
2217  if (e <= Ten_pmax) {
2218 #ifdef VAX
2219  goto vax_ovfl_check;
2220 #else
2221 #ifdef Honor_FLT_ROUNDS
2222  /* round correctly FLT_ROUNDS = 2 or 3 */
2223  if (sign) {
2224  dval(rv) = -dval(rv);
2225  sign = 0;
2226  }
2227 #endif
2228  /* rv = */ rounded_product(dval(rv), tens[e]);
2229  goto ret;
2230 #endif
2231  }
2232  i = DBL_DIG - nd;
2233  if (e <= Ten_pmax + i) {
2234  /* A fancier test would sometimes let us do
2235  * this for larger i values.
2236  */
2237 #ifdef Honor_FLT_ROUNDS
2238  /* round correctly FLT_ROUNDS = 2 or 3 */
2239  if (sign) {
2240  dval(rv) = -dval(rv);
2241  sign = 0;
2242  }
2243 #endif
2244  e -= i;
2245  dval(rv) *= tens[i];
2246 #ifdef VAX
2247  /* VAX exponent range is so narrow we must
2248  * worry about overflow here...
2249  */
2250 vax_ovfl_check:
2251  word0(rv) -= P*Exp_msk1;
2252  /* rv = */ rounded_product(dval(rv), tens[e]);
2253  if ((word0(rv) & Exp_mask)
2254  > Exp_msk1*(DBL_MAX_EXP+Bias-1-P))
2255  goto ovfl;
2256  word0(rv) += P*Exp_msk1;
2257 #else
2258  /* rv = */ rounded_product(dval(rv), tens[e]);
2259 #endif
2260  goto ret;
2261  }
2262  }
2263 #ifndef Inaccurate_Divide
2264  else if (e >= -Ten_pmax) {
2265 #ifdef Honor_FLT_ROUNDS
2266  /* round correctly FLT_ROUNDS = 2 or 3 */
2267  if (sign) {
2268  dval(rv) = -dval(rv);
2269  sign = 0;
2270  }
2271 #endif
2272  /* rv = */ rounded_quotient(dval(rv), tens[-e]);
2273  goto ret;
2274  }
2275 #endif
2276  }
2277  e1 += nd - k;
2278 
2279 #ifdef IEEE_Arith
2280 #ifdef SET_INEXACT
2281  inexact = 1;
2282  if (k <= DBL_DIG)
2283  oldinexact = get_inexact();
2284 #endif
2285 #ifdef Avoid_Underflow
2286  scale = 0;
2287 #endif
2288 #ifdef Honor_FLT_ROUNDS
2289  if ((rounding = Flt_Rounds) >= 2) {
2290  if (sign)
2291  rounding = rounding == 2 ? 0 : 2;
2292  else
2293  if (rounding != 2)
2294  rounding = 0;
2295  }
2296 #endif
2297 #endif /*IEEE_Arith*/
2298 
2299  /* Get starting approximation = rv * 10**e1 */
2300 
2301  if (e1 > 0) {
2302  if ((i = e1 & 15) != 0)
2303  dval(rv) *= tens[i];
2304  if (e1 &= ~15) {
2305  if (e1 > DBL_MAX_10_EXP) {
2306 ovfl:
2307 #ifndef NO_ERRNO
2308  errno = ERANGE;
2309 #endif
2310  /* Can't trust HUGE_VAL */
2311 #ifdef IEEE_Arith
2312 #ifdef Honor_FLT_ROUNDS
2313  switch (rounding) {
2314  case 0: /* toward 0 */
2315  case 3: /* toward -infinity */
2316  word0(rv) = Big0;
2317  word1(rv) = Big1;
2318  break;
2319  default:
2320  word0(rv) = Exp_mask;
2321  word1(rv) = 0;
2322  }
2323 #else /*Honor_FLT_ROUNDS*/
2324  word0(rv) = Exp_mask;
2325  word1(rv) = 0;
2326 #endif /*Honor_FLT_ROUNDS*/
2327 #ifdef SET_INEXACT
2328  /* set overflow bit */
2329  dval(rv0) = 1e300;
2330  dval(rv0) *= dval(rv0);
2331 #endif
2332 #else /*IEEE_Arith*/
2333  word0(rv) = Big0;
2334  word1(rv) = Big1;
2335 #endif /*IEEE_Arith*/
2336  if (bd0)
2337  goto retfree;
2338  goto ret;
2339  }
2340  e1 >>= 4;
2341  for (j = 0; e1 > 1; j++, e1 >>= 1)
2342  if (e1 & 1)
2343  dval(rv) *= bigtens[j];
2344  /* The last multiplication could overflow. */
2345  word0(rv) -= P*Exp_msk1;
2346  dval(rv) *= bigtens[j];
2347  if ((z = word0(rv) & Exp_mask)
2348  > Exp_msk1*(DBL_MAX_EXP+Bias-P))
2349  goto ovfl;
2350  if (z > Exp_msk1*(DBL_MAX_EXP+Bias-1-P)) {
2351  /* set to largest number */
2352  /* (Can't trust DBL_MAX) */
2353  word0(rv) = Big0;
2354  word1(rv) = Big1;
2355  }
2356  else
2357  word0(rv) += P*Exp_msk1;
2358  }
2359  }
2360  else if (e1 < 0) {
2361  e1 = -e1;
2362  if ((i = e1 & 15) != 0)
2363  dval(rv) /= tens[i];
2364  if (e1 >>= 4) {
2365  if (e1 >= 1 << n_bigtens)
2366  goto undfl;
2367 #ifdef Avoid_Underflow
2368  if (e1 & Scale_Bit)
2369  scale = 2*P;
2370  for (j = 0; e1 > 0; j++, e1 >>= 1)
2371  if (e1 & 1)
2372  dval(rv) *= tinytens[j];
2373  if (scale && (j = 2*P + 1 - ((word0(rv) & Exp_mask)
2374  >> Exp_shift)) > 0) {
2375  /* scaled rv is denormal; zap j low bits */
2376  if (j >= 32) {
2377  word1(rv) = 0;
2378  if (j >= 53)
2379  word0(rv) = (P+2)*Exp_msk1;
2380  else
2381  word0(rv) &= 0xffffffff << (j-32);
2382  }
2383  else
2384  word1(rv) &= 0xffffffff << j;
2385  }
2386 #else
2387  for (j = 0; e1 > 1; j++, e1 >>= 1)
2388  if (e1 & 1)
2389  dval(rv) *= tinytens[j];
2390  /* The last multiplication could underflow. */
2391  dval(rv0) = dval(rv);
2392  dval(rv) *= tinytens[j];
2393  if (!dval(rv)) {
2394  dval(rv) = 2.*dval(rv0);
2395  dval(rv) *= tinytens[j];
2396 #endif
2397  if (!dval(rv)) {
2398 undfl:
2399  dval(rv) = 0.;
2400 #ifndef NO_ERRNO
2401  errno = ERANGE;
2402 #endif
2403  if (bd0)
2404  goto retfree;
2405  goto ret;
2406  }
2407 #ifndef Avoid_Underflow
2408  word0(rv) = Tiny0;
2409  word1(rv) = Tiny1;
2410  /* The refinement below will clean
2411  * this approximation up.
2412  */
2413  }
2414 #endif
2415  }
2416  }
2417 
2418  /* Now the hard part -- adjusting rv to the correct value.*/
2419 
2420  /* Put digits into bd: true value = bd * 10^e */
2421 
2422  bd0 = s2b(s0, nd0, nd, y);
2423 
2424  for (;;) {
2425  bd = Balloc(bd0->k);
2426  Bcopy(bd, bd0);
2427  bb = d2b(dval(rv), &bbe, &bbbits); /* rv = bb * 2^bbe */
2428  bs = i2b(1);
2429 
2430  if (e >= 0) {
2431  bb2 = bb5 = 0;
2432  bd2 = bd5 = e;
2433  }
2434  else {
2435  bb2 = bb5 = -e;
2436  bd2 = bd5 = 0;
2437  }
2438  if (bbe >= 0)
2439  bb2 += bbe;
2440  else
2441  bd2 -= bbe;
2442  bs2 = bb2;
2443 #ifdef Honor_FLT_ROUNDS
2444  if (rounding != 1)
2445  bs2++;
2446 #endif
2447 #ifdef Avoid_Underflow
2448  j = bbe - scale;
2449  i = j + bbbits - 1; /* logb(rv) */
2450  if (i < Emin) /* denormal */
2451  j += P - Emin;
2452  else
2453  j = P + 1 - bbbits;
2454 #else /*Avoid_Underflow*/
2455 #ifdef Sudden_Underflow
2456 #ifdef IBM
2457  j = 1 + 4*P - 3 - bbbits + ((bbe + bbbits - 1) & 3);
2458 #else
2459  j = P + 1 - bbbits;
2460 #endif
2461 #else /*Sudden_Underflow*/
2462  j = bbe;
2463  i = j + bbbits - 1; /* logb(rv) */
2464  if (i < Emin) /* denormal */
2465  j += P - Emin;
2466  else
2467  j = P + 1 - bbbits;
2468 #endif /*Sudden_Underflow*/
2469 #endif /*Avoid_Underflow*/
2470  bb2 += j;
2471  bd2 += j;
2472 #ifdef Avoid_Underflow
2473  bd2 += scale;
2474 #endif
2475  i = bb2 < bd2 ? bb2 : bd2;
2476  if (i > bs2)
2477  i = bs2;
2478  if (i > 0) {
2479  bb2 -= i;
2480  bd2 -= i;
2481  bs2 -= i;
2482  }
2483  if (bb5 > 0) {
2484  bs = pow5mult(bs, bb5);
2485  bb1 = mult(bs, bb);
2486  Bfree(bb);
2487  bb = bb1;
2488  }
2489  if (bb2 > 0)
2490  bb = lshift(bb, bb2);
2491  if (bd5 > 0)
2492  bd = pow5mult(bd, bd5);
2493  if (bd2 > 0)
2494  bd = lshift(bd, bd2);
2495  if (bs2 > 0)
2496  bs = lshift(bs, bs2);
2497  delta = diff(bb, bd);
2498  dsign = delta->sign;
2499  delta->sign = 0;
2500  i = cmp(delta, bs);
2501 #ifdef Honor_FLT_ROUNDS
2502  if (rounding != 1) {
2503  if (i < 0) {
2504  /* Error is less than an ulp */
2505  if (!delta->x[0] && delta->wds <= 1) {
2506  /* exact */
2507 #ifdef SET_INEXACT
2508  inexact = 0;
2509 #endif
2510  break;
2511  }
2512  if (rounding) {
2513  if (dsign) {
2514  adj = 1.;
2515  goto apply_adj;
2516  }
2517  }
2518  else if (!dsign) {
2519  adj = -1.;
2520  if (!word1(rv)
2521  && !(word0(rv) & Frac_mask)) {
2522  y = word0(rv) & Exp_mask;
2523 #ifdef Avoid_Underflow
2524  if (!scale || y > 2*P*Exp_msk1)
2525 #else
2526  if (y)
2527 #endif
2528  {
2529  delta = lshift(delta,Log2P);
2530  if (cmp(delta, bs) <= 0)
2531  adj = -0.5;
2532  }
2533  }
2534 apply_adj:
2535 #ifdef Avoid_Underflow
2536  if (scale && (y = word0(rv) & Exp_mask)
2537  <= 2*P*Exp_msk1)
2538  word0(adj) += (2*P+1)*Exp_msk1 - y;
2539 #else
2540 #ifdef Sudden_Underflow
2541  if ((word0(rv) & Exp_mask) <=
2542  P*Exp_msk1) {
2543  word0(rv) += P*Exp_msk1;
2544  dval(rv) += adj*ulp(dval(rv));
2545  word0(rv) -= P*Exp_msk1;
2546  }
2547  else
2548 #endif /*Sudden_Underflow*/
2549 #endif /*Avoid_Underflow*/
2550  dval(rv) += adj*ulp(dval(rv));
2551  }
2552  break;
2553  }
2554  adj = ratio(delta, bs);
2555  if (adj < 1.)
2556  adj = 1.;
2557  if (adj <= 0x7ffffffe) {
2558  /* adj = rounding ? ceil(adj) : floor(adj); */
2559  y = adj;
2560  if (y != adj) {
2561  if (!((rounding>>1) ^ dsign))
2562  y++;
2563  adj = y;
2564  }
2565  }
2566 #ifdef Avoid_Underflow
2567  if (scale && (y = word0(rv) & Exp_mask) <= 2*P*Exp_msk1)
2568  word0(adj) += (2*P+1)*Exp_msk1 - y;
2569 #else
2570 #ifdef Sudden_Underflow
2571  if ((word0(rv) & Exp_mask) <= P*Exp_msk1) {
2572  word0(rv) += P*Exp_msk1;
2573  adj *= ulp(dval(rv));
2574  if (dsign)
2575  dval(rv) += adj;
2576  else
2577  dval(rv) -= adj;
2578  word0(rv) -= P*Exp_msk1;
2579  goto cont;
2580  }
2581 #endif /*Sudden_Underflow*/
2582 #endif /*Avoid_Underflow*/
2583  adj *= ulp(dval(rv));
2584  if (dsign)
2585  dval(rv) += adj;
2586  else
2587  dval(rv) -= adj;
2588  goto cont;
2589  }
2590 #endif /*Honor_FLT_ROUNDS*/
2591 
2592  if (i < 0) {
2593  /* Error is less than half an ulp -- check for
2594  * special case of mantissa a power of two.
2595  */
2596  if (dsign || word1(rv) || word0(rv) & Bndry_mask
2597 #ifdef IEEE_Arith
2598 #ifdef Avoid_Underflow
2599  || (word0(rv) & Exp_mask) <= (2*P+1)*Exp_msk1
2600 #else
2601  || (word0(rv) & Exp_mask) <= Exp_msk1
2602 #endif
2603 #endif
2604  ) {
2605 #ifdef SET_INEXACT
2606  if (!delta->x[0] && delta->wds <= 1)
2607  inexact = 0;
2608 #endif
2609  break;
2610  }
2611  if (!delta->x[0] && delta->wds <= 1) {
2612  /* exact result */
2613 #ifdef SET_INEXACT
2614  inexact = 0;
2615 #endif
2616  break;
2617  }
2618  delta = lshift(delta,Log2P);
2619  if (cmp(delta, bs) > 0)
2620  goto drop_down;
2621  break;
2622  }
2623  if (i == 0) {
2624  /* exactly half-way between */
2625  if (dsign) {
2626  if ((word0(rv) & Bndry_mask1) == Bndry_mask1
2627  && word1(rv) == (
2628 #ifdef Avoid_Underflow
2629  (scale && (y = word0(rv) & Exp_mask) <= 2*P*Exp_msk1)
2630  ? (0xffffffff & (0xffffffff << (2*P+1-(y>>Exp_shift)))) :
2631 #endif
2632  0xffffffff)) {
2633  /*boundary case -- increment exponent*/
2634  word0(rv) = (word0(rv) & Exp_mask)
2635  + Exp_msk1
2636 #ifdef IBM
2637  | Exp_msk1 >> 4
2638 #endif
2639  ;
2640  word1(rv) = 0;
2641 #ifdef Avoid_Underflow
2642  dsign = 0;
2643 #endif
2644  break;
2645  }
2646  }
2647  else if (!(word0(rv) & Bndry_mask) && !word1(rv)) {
2648 drop_down:
2649  /* boundary case -- decrement exponent */
2650 #ifdef Sudden_Underflow /*{{*/
2651  L = word0(rv) & Exp_mask;
2652 #ifdef IBM
2653  if (L < Exp_msk1)
2654 #else
2655 #ifdef Avoid_Underflow
2656  if (L <= (scale ? (2*P+1)*Exp_msk1 : Exp_msk1))
2657 #else
2658  if (L <= Exp_msk1)
2659 #endif /*Avoid_Underflow*/
2660 #endif /*IBM*/
2661  goto undfl;
2662  L -= Exp_msk1;
2663 #else /*Sudden_Underflow}{*/
2664 #ifdef Avoid_Underflow
2665  if (scale) {
2666  L = word0(rv) & Exp_mask;
2667  if (L <= (2*P+1)*Exp_msk1) {
2668  if (L > (P+2)*Exp_msk1)
2669  /* round even ==> */
2670  /* accept rv */
2671  break;
2672  /* rv = smallest denormal */
2673  goto undfl;
2674  }
2675  }
2676 #endif /*Avoid_Underflow*/
2677  L = (word0(rv) & Exp_mask) - Exp_msk1;
2678 #endif /*Sudden_Underflow}}*/
2679  word0(rv) = L | Bndry_mask1;
2680  word1(rv) = 0xffffffff;
2681 #ifdef IBM
2682  goto cont;
2683 #else
2684  break;
2685 #endif
2686  }
2687 #ifndef ROUND_BIASED
2688  if (!(word1(rv) & LSB))
2689  break;
2690 #endif
2691  if (dsign)
2692  dval(rv) += ulp(dval(rv));
2693 #ifndef ROUND_BIASED
2694  else {
2695  dval(rv) -= ulp(dval(rv));
2696 #ifndef Sudden_Underflow
2697  if (!dval(rv))
2698  goto undfl;
2699 #endif
2700  }
2701 #ifdef Avoid_Underflow
2702  dsign = 1 - dsign;
2703 #endif
2704 #endif
2705  break;
2706  }
2707  if ((aadj = ratio(delta, bs)) <= 2.) {
2708  if (dsign)
2709  aadj = dval(aadj1) = 1.;
2710  else if (word1(rv) || word0(rv) & Bndry_mask) {
2711 #ifndef Sudden_Underflow
2712  if (word1(rv) == Tiny1 && !word0(rv))
2713  goto undfl;
2714 #endif
2715  aadj = 1.;
2716  dval(aadj1) = -1.;
2717  }
2718  else {
2719  /* special case -- power of FLT_RADIX to be */
2720  /* rounded down... */
2721 
2722  if (aadj < 2./FLT_RADIX)
2723  aadj = 1./FLT_RADIX;
2724  else
2725  aadj *= 0.5;
2726  dval(aadj1) = -aadj;
2727  }
2728  }
2729  else {
2730  aadj *= 0.5;
2731  dval(aadj1) = dsign ? aadj : -aadj;
2732 #ifdef Check_FLT_ROUNDS
2733  switch (Rounding) {
2734  case 2: /* towards +infinity */
2735  dval(aadj1) -= 0.5;
2736  break;
2737  case 0: /* towards 0 */
2738  case 3: /* towards -infinity */
2739  dval(aadj1) += 0.5;
2740  }
2741 #else
2742  if (Flt_Rounds == 0)
2743  dval(aadj1) += 0.5;
2744 #endif /*Check_FLT_ROUNDS*/
2745  }
2746  y = word0(rv) & Exp_mask;
2747 
2748  /* Check for overflow */
2749 
2750  if (y == Exp_msk1*(DBL_MAX_EXP+Bias-1)) {
2751  dval(rv0) = dval(rv);
2752  word0(rv) -= P*Exp_msk1;
2753  adj = dval(aadj1) * ulp(dval(rv));
2754  dval(rv) += adj;
2755  if ((word0(rv) & Exp_mask) >=
2756  Exp_msk1*(DBL_MAX_EXP+Bias-P)) {
2757  if (word0(rv0) == Big0 && word1(rv0) == Big1)
2758  goto ovfl;
2759  word0(rv) = Big0;
2760  word1(rv) = Big1;
2761  goto cont;
2762  }
2763  else
2764  word0(rv) += P*Exp_msk1;
2765  }
2766  else {
2767 #ifdef Avoid_Underflow
2768  if (scale && y <= 2*P*Exp_msk1) {
2769  if (aadj <= 0x7fffffff) {
2770  if ((z = (int)aadj) <= 0)
2771  z = 1;
2772  aadj = z;
2773  dval(aadj1) = dsign ? aadj : -aadj;
2774  }
2775  word0(aadj1) += (2*P+1)*Exp_msk1 - y;
2776  }
2777  adj = dval(aadj1) * ulp(dval(rv));
2778  dval(rv) += adj;
2779 #else
2780 #ifdef Sudden_Underflow
2781  if ((word0(rv) & Exp_mask) <= P*Exp_msk1) {
2782  dval(rv0) = dval(rv);
2783  word0(rv) += P*Exp_msk1;
2784  adj = dval(aadj1) * ulp(dval(rv));
2785  dval(rv) += adj;
2786 #ifdef IBM
2787  if ((word0(rv) & Exp_mask) < P*Exp_msk1)
2788 #else
2789  if ((word0(rv) & Exp_mask) <= P*Exp_msk1)
2790 #endif
2791  {
2792  if (word0(rv0) == Tiny0 && word1(rv0) == Tiny1)
2793  goto undfl;
2794  word0(rv) = Tiny0;
2795  word1(rv) = Tiny1;
2796  goto cont;
2797  }
2798  else
2799  word0(rv) -= P*Exp_msk1;
2800  }
2801  else {
2802  adj = dval(aadj1) * ulp(dval(rv));
2803  dval(rv) += adj;
2804  }
2805 #else /*Sudden_Underflow*/
2806  /* Compute adj so that the IEEE rounding rules will
2807  * correctly round rv + adj in some half-way cases.
2808  * If rv * ulp(rv) is denormalized (i.e.,
2809  * y <= (P-1)*Exp_msk1), we must adjust aadj to avoid
2810  * trouble from bits lost to denormalization;
2811  * example: 1.2e-307 .
2812  */
2813  if (y <= (P-1)*Exp_msk1 && aadj > 1.) {
2814  dval(aadj1) = (double)(int)(aadj + 0.5);
2815  if (!dsign)
2816  dval(aadj1) = -dval(aadj1);
2817  }
2818  adj = dval(aadj1) * ulp(dval(rv));
2819  dval(rv) += adj;
2820 #endif /*Sudden_Underflow*/
2821 #endif /*Avoid_Underflow*/
2822  }
2823  z = word0(rv) & Exp_mask;
2824 #ifndef SET_INEXACT
2825 #ifdef Avoid_Underflow
2826  if (!scale)
2827 #endif
2828  if (y == z) {
2829  /* Can we stop now? */
2830  L = (Long)aadj;
2831  aadj -= L;
2832  /* The tolerances below are conservative. */
2833  if (dsign || word1(rv) || word0(rv) & Bndry_mask) {
2834  if (aadj < .4999999 || aadj > .5000001)
2835  break;
2836  }
2837  else if (aadj < .4999999/FLT_RADIX)
2838  break;
2839  }
2840 #endif
2841 cont:
2842  Bfree(bb);
2843  Bfree(bd);
2844  Bfree(bs);
2845  Bfree(delta);
2846  }
2847 #ifdef SET_INEXACT
2848  if (inexact) {
2849  if (!oldinexact) {
2850  word0(rv0) = Exp_1 + (70 << Exp_shift);
2851  word1(rv0) = 0;
2852  dval(rv0) += 1.;
2853  }
2854  }
2855  else if (!oldinexact)
2856  clear_inexact();
2857 #endif
2858 #ifdef Avoid_Underflow
2859  if (scale) {
2860  word0(rv0) = Exp_1 - 2*P*Exp_msk1;
2861  word1(rv0) = 0;
2862  dval(rv) *= dval(rv0);
2863 #ifndef NO_ERRNO
2864  /* try to avoid the bug of testing an 8087 register value */
2865  if (word0(rv) == 0 && word1(rv) == 0)
2866  errno = ERANGE;
2867 #endif
2868  }
2869 #endif /* Avoid_Underflow */
2870 #ifdef SET_INEXACT
2871  if (inexact && !(word0(rv) & Exp_mask)) {
2872  /* set underflow bit */
2873  dval(rv0) = 1e-300;
2874  dval(rv0) *= dval(rv0);
2875  }
2876 #endif
2877 retfree:
2878  Bfree(bb);
2879  Bfree(bd);
2880  Bfree(bs);
2881  Bfree(bd0);
2882  Bfree(delta);
2883 ret:
2884  if (se)
2885  *se = (char *)s;
2886  return sign ? -dval(rv) : dval(rv);
2887 }
2888 
2889 static int
2891 {
2892  int n;
2893  ULong *bx, *bxe, q, *sx, *sxe;
2894 #ifdef ULLong
2895  ULLong borrow, carry, y, ys;
2896 #else
2897  ULong borrow, carry, y, ys;
2898 #ifdef Pack_32
2899  ULong si, z, zs;
2900 #endif
2901 #endif
2902 
2903  n = S->wds;
2904 #ifdef DEBUG
2905  /*debug*/ if (b->wds > n)
2906  /*debug*/ Bug("oversize b in quorem");
2907 #endif
2908  if (b->wds < n)
2909  return 0;
2910  sx = S->x;
2911  sxe = sx + --n;
2912  bx = b->x;
2913  bxe = bx + n;
2914  q = *bxe / (*sxe + 1); /* ensure q <= true quotient */
2915 #ifdef DEBUG
2916  /*debug*/ if (q > 9)
2917  /*debug*/ Bug("oversized quotient in quorem");
2918 #endif
2919  if (q) {
2920  borrow = 0;
2921  carry = 0;
2922  do {
2923 #ifdef ULLong
2924  ys = *sx++ * (ULLong)q + carry;
2925  carry = ys >> 32;
2926  y = *bx - (ys & FFFFFFFF) - borrow;
2927  borrow = y >> 32 & (ULong)1;
2928  *bx++ = (ULong)(y & FFFFFFFF);
2929 #else
2930 #ifdef Pack_32
2931  si = *sx++;
2932  ys = (si & 0xffff) * q + carry;
2933  zs = (si >> 16) * q + (ys >> 16);
2934  carry = zs >> 16;
2935  y = (*bx & 0xffff) - (ys & 0xffff) - borrow;
2936  borrow = (y & 0x10000) >> 16;
2937  z = (*bx >> 16) - (zs & 0xffff) - borrow;
2938  borrow = (z & 0x10000) >> 16;
2939  Storeinc(bx, z, y);
2940 #else
2941  ys = *sx++ * q + carry;
2942  carry = ys >> 16;
2943  y = *bx - (ys & 0xffff) - borrow;
2944  borrow = (y & 0x10000) >> 16;
2945  *bx++ = y & 0xffff;
2946 #endif
2947 #endif
2948  } while (sx <= sxe);
2949  if (!*bxe) {
2950  bx = b->x;
2951  while (--bxe > bx && !*bxe)
2952  --n;
2953  b->wds = n;
2954  }
2955  }
2956  if (cmp(b, S) >= 0) {
2957  q++;
2958  borrow = 0;
2959  carry = 0;
2960  bx = b->x;
2961  sx = S->x;
2962  do {
2963 #ifdef ULLong
2964  ys = *sx++ + carry;
2965  carry = ys >> 32;
2966  y = *bx - (ys & FFFFFFFF) - borrow;
2967  borrow = y >> 32 & (ULong)1;
2968  *bx++ = (ULong)(y & FFFFFFFF);
2969 #else
2970 #ifdef Pack_32
2971  si = *sx++;
2972  ys = (si & 0xffff) + carry;
2973  zs = (si >> 16) + (ys >> 16);
2974  carry = zs >> 16;
2975  y = (*bx & 0xffff) - (ys & 0xffff) - borrow;
2976  borrow = (y & 0x10000) >> 16;
2977  z = (*bx >> 16) - (zs & 0xffff) - borrow;
2978  borrow = (z & 0x10000) >> 16;
2979  Storeinc(bx, z, y);
2980 #else
2981  ys = *sx++ + carry;
2982  carry = ys >> 16;
2983  y = *bx - (ys & 0xffff) - borrow;
2984  borrow = (y & 0x10000) >> 16;
2985  *bx++ = y & 0xffff;
2986 #endif
2987 #endif
2988  } while (sx <= sxe);
2989  bx = b->x;
2990  bxe = bx + n;
2991  if (!*bxe) {
2992  while (--bxe > bx && !*bxe)
2993  --n;
2994  b->wds = n;
2995  }
2996  }
2997  return q;
2998 }
2999 
3000 #ifndef MULTIPLE_THREADS
3001 static char *dtoa_result;
3002 #endif
3003 
3004 #ifndef MULTIPLE_THREADS
3005 static char *
3006 rv_alloc(int i)
3007 {
3008  return dtoa_result = xmalloc(i);
3009 }
3010 #else
3011 #define rv_alloc(i) xmalloc(i)
3012 #endif
3013 
3014 static char *
3015 nrv_alloc(const char *s, char **rve, size_t n)
3016 {
3017  char *rv, *t;
3018 
3019  t = rv = rv_alloc(n);
3020  while ((*t = *s++) != 0) t++;
3021  if (rve)
3022  *rve = t;
3023  return rv;
3024 }
3025 
3026 #define rv_strdup(s, rve) nrv_alloc((s), (rve), strlen(s)+1)
3027 
3028 #ifndef MULTIPLE_THREADS
3029 /* freedtoa(s) must be used to free values s returned by dtoa
3030  * when MULTIPLE_THREADS is #defined. It should be used in all cases,
3031  * but for consistency with earlier versions of dtoa, it is optional
3032  * when MULTIPLE_THREADS is not defined.
3033  */
3034 
3035 static void
3036 freedtoa(char *s)
3037 {
3038  xfree(s);
3039 }
3040 #endif
3041 
3042 static const char INFSTR[] = "Infinity";
3043 static const char NANSTR[] = "NaN";
3044 static const char ZEROSTR[] = "0";
3045 
3046 /* dtoa for IEEE arithmetic (dmg): convert double to ASCII string.
3047  *
3048  * Inspired by "How to Print Floating-Point Numbers Accurately" by
3049  * Guy L. Steele, Jr. and Jon L. White [Proc. ACM SIGPLAN '90, pp. 112-126].
3050  *
3051  * Modifications:
3052  * 1. Rather than iterating, we use a simple numeric overestimate
3053  * to determine k = floor(log10(d)). We scale relevant
3054  * quantities using O(log2(k)) rather than O(k) multiplications.
3055  * 2. For some modes > 2 (corresponding to ecvt and fcvt), we don't
3056  * try to generate digits strictly left to right. Instead, we
3057  * compute with fewer bits and propagate the carry if necessary
3058  * when rounding the final digit up. This is often faster.
3059  * 3. Under the assumption that input will be rounded nearest,
3060  * mode 0 renders 1e23 as 1e23 rather than 9.999999999999999e22.
3061  * That is, we allow equality in stopping tests when the
3062  * round-nearest rule will give the same floating-point value
3063  * as would satisfaction of the stopping test with strict
3064  * inequality.
3065  * 4. We remove common factors of powers of 2 from relevant
3066  * quantities.
3067  * 5. When converting floating-point integers less than 1e16,
3068  * we use floating-point arithmetic rather than resorting
3069  * to multiple-precision integers.
3070  * 6. When asked to produce fewer than 15 digits, we first try
3071  * to get by with floating-point arithmetic; we resort to
3072  * multiple-precision integer arithmetic only if we cannot
3073  * guarantee that the floating-point calculation has given
3074  * the correctly rounded result. For k requested digits and
3075  * "uniformly" distributed input, the probability is
3076  * something like 10^(k-15) that we must resort to the Long
3077  * calculation.
3078  */
3079 
3080 char *
3081 ruby_dtoa(double d_, int mode, int ndigits, int *decpt, int *sign, char **rve)
3082 {
3083  /* Arguments ndigits, decpt, sign are similar to those
3084  of ecvt and fcvt; trailing zeros are suppressed from
3085  the returned string. If not null, *rve is set to point
3086  to the end of the return value. If d is +-Infinity or NaN,
3087  then *decpt is set to 9999.
3088 
3089  mode:
3090  0 ==> shortest string that yields d when read in
3091  and rounded to nearest.
3092  1 ==> like 0, but with Steele & White stopping rule;
3093  e.g. with IEEE P754 arithmetic , mode 0 gives
3094  1e23 whereas mode 1 gives 9.999999999999999e22.
3095  2 ==> max(1,ndigits) significant digits. This gives a
3096  return value similar to that of ecvt, except
3097  that trailing zeros are suppressed.
3098  3 ==> through ndigits past the decimal point. This
3099  gives a return value similar to that from fcvt,
3100  except that trailing zeros are suppressed, and
3101  ndigits can be negative.
3102  4,5 ==> similar to 2 and 3, respectively, but (in
3103  round-nearest mode) with the tests of mode 0 to
3104  possibly return a shorter string that rounds to d.
3105  With IEEE arithmetic and compilation with
3106  -DHonor_FLT_ROUNDS, modes 4 and 5 behave the same
3107  as modes 2 and 3 when FLT_ROUNDS != 1.
3108  6-9 ==> Debugging modes similar to mode - 4: don't try
3109  fast floating-point estimate (if applicable).
3110 
3111  Values of mode other than 0-9 are treated as mode 0.
3112 
3113  Sufficient space is allocated to the return value
3114  to hold the suppressed trailing zeros.
3115  */
3116 
3117  int bbits, b2, b5, be, dig, i, ieps, ilim, ilim0, ilim1,
3118  j, j1, k, k0, k_check, leftright, m2, m5, s2, s5,
3119  spec_case, try_quick;
3120  Long L;
3121 #ifndef Sudden_Underflow
3122  int denorm;
3123  ULong x;
3124 #endif
3125  Bigint *b, *b1, *delta, *mlo = 0, *mhi = 0, *S;
3126  double ds;
3127  double_u d, d2, eps;
3128  char *s, *s0;
3129 #ifdef Honor_FLT_ROUNDS
3130  int rounding;
3131 #endif
3132 #ifdef SET_INEXACT
3133  int inexact, oldinexact;
3134 #endif
3135 
3136  dval(d) = d_;
3137 
3138 #ifndef MULTIPLE_THREADS
3139  if (dtoa_result) {
3140  freedtoa(dtoa_result);
3141  dtoa_result = 0;
3142  }
3143 #endif
3144 
3145  if (word0(d) & Sign_bit) {
3146  /* set sign for everything, including 0's and NaNs */
3147  *sign = 1;
3148  word0(d) &= ~Sign_bit; /* clear sign bit */
3149  }
3150  else
3151  *sign = 0;
3152 
3153 #if defined(IEEE_Arith) + defined(VAX)
3154 #ifdef IEEE_Arith
3155  if ((word0(d) & Exp_mask) == Exp_mask)
3156 #else
3157  if (word0(d) == 0x8000)
3158 #endif
3159  {
3160  /* Infinity or NaN */
3161  *decpt = 9999;
3162 #ifdef IEEE_Arith
3163  if (!word1(d) && !(word0(d) & 0xfffff))
3164  return rv_strdup(INFSTR, rve);
3165 #endif
3166  return rv_strdup(NANSTR, rve);
3167  }
3168 #endif
3169 #ifdef IBM
3170  dval(d) += 0; /* normalize */
3171 #endif
3172  if (!dval(d)) {
3173  *decpt = 1;
3174  return rv_strdup(ZEROSTR, rve);
3175  }
3176 
3177 #ifdef SET_INEXACT
3178  try_quick = oldinexact = get_inexact();
3179  inexact = 1;
3180 #endif
3181 #ifdef Honor_FLT_ROUNDS
3182  if ((rounding = Flt_Rounds) >= 2) {
3183  if (*sign)
3184  rounding = rounding == 2 ? 0 : 2;
3185  else
3186  if (rounding != 2)
3187  rounding = 0;
3188  }
3189 #endif
3190 
3191  b = d2b(dval(d), &be, &bbits);
3192 #ifdef Sudden_Underflow
3193  i = (int)(word0(d) >> Exp_shift1 & (Exp_mask>>Exp_shift1));
3194 #else
3195  if ((i = (int)(word0(d) >> Exp_shift1 & (Exp_mask>>Exp_shift1))) != 0) {
3196 #endif
3197  dval(d2) = dval(d);
3198  word0(d2) &= Frac_mask1;
3199  word0(d2) |= Exp_11;
3200 #ifdef IBM
3201  if (j = 11 - hi0bits(word0(d2) & Frac_mask))
3202  dval(d2) /= 1 << j;
3203 #endif
3204 
3205  /* log(x) ~=~ log(1.5) + (x-1.5)/1.5
3206  * log10(x) = log(x) / log(10)
3207  * ~=~ log(1.5)/log(10) + (x-1.5)/(1.5*log(10))
3208  * log10(d) = (i-Bias)*log(2)/log(10) + log10(d2)
3209  *
3210  * This suggests computing an approximation k to log10(d) by
3211  *
3212  * k = (i - Bias)*0.301029995663981
3213  * + ( (d2-1.5)*0.289529654602168 + 0.176091259055681 );
3214  *
3215  * We want k to be too large rather than too small.
3216  * The error in the first-order Taylor series approximation
3217  * is in our favor, so we just round up the constant enough
3218  * to compensate for any error in the multiplication of
3219  * (i - Bias) by 0.301029995663981; since |i - Bias| <= 1077,
3220  * and 1077 * 0.30103 * 2^-52 ~=~ 7.2e-14,
3221  * adding 1e-13 to the constant term more than suffices.
3222  * Hence we adjust the constant term to 0.1760912590558.
3223  * (We could get a more accurate k by invoking log10,
3224  * but this is probably not worthwhile.)
3225  */
3226 
3227  i -= Bias;
3228 #ifdef IBM
3229  i <<= 2;
3230  i += j;
3231 #endif
3232 #ifndef Sudden_Underflow
3233  denorm = 0;
3234  }
3235  else {
3236  /* d is denormalized */
3237 
3238  i = bbits + be + (Bias + (P-1) - 1);
3239  x = i > 32 ? word0(d) << (64 - i) | word1(d) >> (i - 32)
3240  : word1(d) << (32 - i);
3241  dval(d2) = x;
3242  word0(d2) -= 31*Exp_msk1; /* adjust exponent */
3243  i -= (Bias + (P-1) - 1) + 1;
3244  denorm = 1;
3245  }
3246 #endif
3247  ds = (dval(d2)-1.5)*0.289529654602168 + 0.1760912590558 + i*0.301029995663981;
3248  k = (int)ds;
3249  if (ds < 0. && ds != k)
3250  k--; /* want k = floor(ds) */
3251  k_check = 1;
3252  if (k >= 0 && k <= Ten_pmax) {
3253  if (dval(d) < tens[k])
3254  k--;
3255  k_check = 0;
3256  }
3257  j = bbits - i - 1;
3258  if (j >= 0) {
3259  b2 = 0;
3260  s2 = j;
3261  }
3262  else {
3263  b2 = -j;
3264  s2 = 0;
3265  }
3266  if (k >= 0) {
3267  b5 = 0;
3268  s5 = k;
3269  s2 += k;
3270  }
3271  else {
3272  b2 -= k;
3273  b5 = -k;
3274  s5 = 0;
3275  }
3276  if (mode < 0 || mode > 9)
3277  mode = 0;
3278 
3279 #ifndef SET_INEXACT
3280 #ifdef Check_FLT_ROUNDS
3281  try_quick = Rounding == 1;
3282 #else
3283  try_quick = 1;
3284 #endif
3285 #endif /*SET_INEXACT*/
3286 
3287  if (mode > 5) {
3288  mode -= 4;
3289  try_quick = 0;
3290  }
3291  leftright = 1;
3292  ilim = ilim1 = -1;
3293  switch (mode) {
3294  case 0:
3295  case 1:
3296  i = 18;
3297  ndigits = 0;
3298  break;
3299  case 2:
3300  leftright = 0;
3301  /* no break */
3302  case 4:
3303  if (ndigits <= 0)
3304  ndigits = 1;
3305  ilim = ilim1 = i = ndigits;
3306  break;
3307  case 3:
3308  leftright = 0;
3309  /* no break */
3310  case 5:
3311  i = ndigits + k + 1;
3312  ilim = i;
3313  ilim1 = i - 1;
3314  if (i <= 0)
3315  i = 1;
3316  }
3317  s = s0 = rv_alloc(i+1);
3318 
3319 #ifdef Honor_FLT_ROUNDS
3320  if (mode > 1 && rounding != 1)
3321  leftright = 0;
3322 #endif
3323 
3324  if (ilim >= 0 && ilim <= Quick_max && try_quick) {
3325 
3326  /* Try to get by with floating-point arithmetic. */
3327 
3328  i = 0;
3329  dval(d2) = dval(d);
3330  k0 = k;
3331  ilim0 = ilim;
3332  ieps = 2; /* conservative */
3333  if (k > 0) {
3334  ds = tens[k&0xf];
3335  j = k >> 4;
3336  if (j & Bletch) {
3337  /* prevent overflows */
3338  j &= Bletch - 1;
3339  dval(d) /= bigtens[n_bigtens-1];
3340  ieps++;
3341  }
3342  for (; j; j >>= 1, i++)
3343  if (j & 1) {
3344  ieps++;
3345  ds *= bigtens[i];
3346  }
3347  dval(d) /= ds;
3348  }
3349  else if ((j1 = -k) != 0) {
3350  dval(d) *= tens[j1 & 0xf];
3351  for (j = j1 >> 4; j; j >>= 1, i++)
3352  if (j & 1) {
3353  ieps++;
3354  dval(d) *= bigtens[i];
3355  }
3356  }
3357  if (k_check && dval(d) < 1. && ilim > 0) {
3358  if (ilim1 <= 0)
3359  goto fast_failed;
3360  ilim = ilim1;
3361  k--;
3362  dval(d) *= 10.;
3363  ieps++;
3364  }
3365  dval(eps) = ieps*dval(d) + 7.;
3366  word0(eps) -= (P-1)*Exp_msk1;
3367  if (ilim == 0) {
3368  S = mhi = 0;
3369  dval(d) -= 5.;
3370  if (dval(d) > dval(eps))
3371  goto one_digit;
3372  if (dval(d) < -dval(eps))
3373  goto no_digits;
3374  goto fast_failed;
3375  }
3376 #ifndef No_leftright
3377  if (leftright) {
3378  /* Use Steele & White method of only
3379  * generating digits needed.
3380  */
3381  dval(eps) = 0.5/tens[ilim-1] - dval(eps);
3382  for (i = 0;;) {
3383  L = (int)dval(d);
3384  dval(d) -= L;
3385  *s++ = '0' + (int)L;
3386  if (dval(d) < dval(eps))
3387  goto ret1;
3388  if (1. - dval(d) < dval(eps))
3389  goto bump_up;
3390  if (++i >= ilim)
3391  break;
3392  dval(eps) *= 10.;
3393  dval(d) *= 10.;
3394  }
3395  }
3396  else {
3397 #endif
3398  /* Generate ilim digits, then fix them up. */
3399  dval(eps) *= tens[ilim-1];
3400  for (i = 1;; i++, dval(d) *= 10.) {
3401  L = (Long)(dval(d));
3402  if (!(dval(d) -= L))
3403  ilim = i;
3404  *s++ = '0' + (int)L;
3405  if (i == ilim) {
3406  if (dval(d) > 0.5 + dval(eps))
3407  goto bump_up;
3408  else if (dval(d) < 0.5 - dval(eps)) {
3409  while (*--s == '0') ;
3410  s++;
3411  goto ret1;
3412  }
3413  break;
3414  }
3415  }
3416 #ifndef No_leftright
3417  }
3418 #endif
3419 fast_failed:
3420  s = s0;
3421  dval(d) = dval(d2);
3422  k = k0;
3423  ilim = ilim0;
3424  }
3425 
3426  /* Do we have a "small" integer? */
3427 
3428  if (be >= 0 && k <= Int_max) {
3429  /* Yes. */
3430  ds = tens[k];
3431  if (ndigits < 0 && ilim <= 0) {
3432  S = mhi = 0;
3433  if (ilim < 0 || dval(d) <= 5*ds)
3434  goto no_digits;
3435  goto one_digit;
3436  }
3437  for (i = 1;; i++, dval(d) *= 10.) {
3438  L = (Long)(dval(d) / ds);
3439  dval(d) -= L*ds;
3440 #ifdef Check_FLT_ROUNDS
3441  /* If FLT_ROUNDS == 2, L will usually be high by 1 */
3442  if (dval(d) < 0) {
3443  L--;
3444  dval(d) += ds;
3445  }
3446 #endif
3447  *s++ = '0' + (int)L;
3448  if (!dval(d)) {
3449 #ifdef SET_INEXACT
3450  inexact = 0;
3451 #endif
3452  break;
3453  }
3454  if (i == ilim) {
3455 #ifdef Honor_FLT_ROUNDS
3456  if (mode > 1)
3457  switch (rounding) {
3458  case 0: goto ret1;
3459  case 2: goto bump_up;
3460  }
3461 #endif
3462  dval(d) += dval(d);
3463  if (dval(d) > ds || (dval(d) == ds && (L & 1))) {
3464 bump_up:
3465  while (*--s == '9')
3466  if (s == s0) {
3467  k++;
3468  *s = '0';
3469  break;
3470  }
3471  ++*s++;
3472  }
3473  break;
3474  }
3475  }
3476  goto ret1;
3477  }
3478 
3479  m2 = b2;
3480  m5 = b5;
3481  if (leftright) {
3482  i =
3483 #ifndef Sudden_Underflow
3484  denorm ? be + (Bias + (P-1) - 1 + 1) :
3485 #endif
3486 #ifdef IBM
3487  1 + 4*P - 3 - bbits + ((bbits + be - 1) & 3);
3488 #else
3489  1 + P - bbits;
3490 #endif
3491  b2 += i;
3492  s2 += i;
3493  mhi = i2b(1);
3494  }
3495  if (m2 > 0 && s2 > 0) {
3496  i = m2 < s2 ? m2 : s2;
3497  b2 -= i;
3498  m2 -= i;
3499  s2 -= i;
3500  }
3501  if (b5 > 0) {
3502  if (leftright) {
3503  if (m5 > 0) {
3504  mhi = pow5mult(mhi, m5);
3505  b1 = mult(mhi, b);
3506  Bfree(b);
3507  b = b1;
3508  }
3509  if ((j = b5 - m5) != 0)
3510  b = pow5mult(b, j);
3511  }
3512  else
3513  b = pow5mult(b, b5);
3514  }
3515  S = i2b(1);
3516  if (s5 > 0)
3517  S = pow5mult(S, s5);
3518 
3519  /* Check for special case that d is a normalized power of 2. */
3520 
3521  spec_case = 0;
3522  if ((mode < 2 || leftright)
3523 #ifdef Honor_FLT_ROUNDS
3524  && rounding == 1
3525 #endif
3526  ) {
3527  if (!word1(d) && !(word0(d) & Bndry_mask)
3528 #ifndef Sudden_Underflow
3529  && word0(d) & (Exp_mask & ~Exp_msk1)
3530 #endif
3531  ) {
3532  /* The special case */
3533  b2 += Log2P;
3534  s2 += Log2P;
3535  spec_case = 1;
3536  }
3537  }
3538 
3539  /* Arrange for convenient computation of quotients:
3540  * shift left if necessary so divisor has 4 leading 0 bits.
3541  *
3542  * Perhaps we should just compute leading 28 bits of S once
3543  * and for all and pass them and a shift to quorem, so it
3544  * can do shifts and ors to compute the numerator for q.
3545  */
3546 #ifdef Pack_32
3547  if ((i = ((s5 ? 32 - hi0bits(S->x[S->wds-1]) : 1) + s2) & 0x1f) != 0)
3548  i = 32 - i;
3549 #else
3550  if ((i = ((s5 ? 32 - hi0bits(S->x[S->wds-1]) : 1) + s2) & 0xf) != 0)
3551  i = 16 - i;
3552 #endif
3553  if (i > 4) {
3554  i -= 4;
3555  b2 += i;
3556  m2 += i;
3557  s2 += i;
3558  }
3559  else if (i < 4) {
3560  i += 28;
3561  b2 += i;
3562  m2 += i;
3563  s2 += i;
3564  }
3565  if (b2 > 0)
3566  b = lshift(b, b2);
3567  if (s2 > 0)
3568  S = lshift(S, s2);
3569  if (k_check) {
3570  if (cmp(b,S) < 0) {
3571  k--;
3572  b = multadd(b, 10, 0); /* we botched the k estimate */
3573  if (leftright)
3574  mhi = multadd(mhi, 10, 0);
3575  ilim = ilim1;
3576  }
3577  }
3578  if (ilim <= 0 && (mode == 3 || mode == 5)) {
3579  if (ilim < 0 || cmp(b,S = multadd(S,5,0)) <= 0) {
3580  /* no digits, fcvt style */
3581 no_digits:
3582  k = -1 - ndigits;
3583  goto ret;
3584  }
3585 one_digit:
3586  *s++ = '1';
3587  k++;
3588  goto ret;
3589  }
3590  if (leftright) {
3591  if (m2 > 0)
3592  mhi = lshift(mhi, m2);
3593 
3594  /* Compute mlo -- check for special case
3595  * that d is a normalized power of 2.
3596  */
3597 
3598  mlo = mhi;
3599  if (spec_case) {
3600  mhi = Balloc(mhi->k);
3601  Bcopy(mhi, mlo);
3602  mhi = lshift(mhi, Log2P);
3603  }
3604 
3605  for (i = 1;;i++) {
3606  dig = quorem(b,S) + '0';
3607  /* Do we yet have the shortest decimal string
3608  * that will round to d?
3609  */
3610  j = cmp(b, mlo);
3611  delta = diff(S, mhi);
3612  j1 = delta->sign ? 1 : cmp(b, delta);
3613  Bfree(delta);
3614 #ifndef ROUND_BIASED
3615  if (j1 == 0 && mode != 1 && !(word1(d) & 1)
3616 #ifdef Honor_FLT_ROUNDS
3617  && rounding >= 1
3618 #endif
3619  ) {
3620  if (dig == '9')
3621  goto round_9_up;
3622  if (j > 0)
3623  dig++;
3624 #ifdef SET_INEXACT
3625  else if (!b->x[0] && b->wds <= 1)
3626  inexact = 0;
3627 #endif
3628  *s++ = dig;
3629  goto ret;
3630  }
3631 #endif
3632  if (j < 0 || (j == 0 && mode != 1
3633 #ifndef ROUND_BIASED
3634  && !(word1(d) & 1)
3635 #endif
3636  )) {
3637  if (!b->x[0] && b->wds <= 1) {
3638 #ifdef SET_INEXACT
3639  inexact = 0;
3640 #endif
3641  goto accept_dig;
3642  }
3643 #ifdef Honor_FLT_ROUNDS
3644  if (mode > 1)
3645  switch (rounding) {
3646  case 0: goto accept_dig;
3647  case 2: goto keep_dig;
3648  }
3649 #endif /*Honor_FLT_ROUNDS*/
3650  if (j1 > 0) {
3651  b = lshift(b, 1);
3652  j1 = cmp(b, S);
3653  if ((j1 > 0 || (j1 == 0 && (dig & 1))) && dig++ == '9')
3654  goto round_9_up;
3655  }
3656 accept_dig:
3657  *s++ = dig;
3658  goto ret;
3659  }
3660  if (j1 > 0) {
3661 #ifdef Honor_FLT_ROUNDS
3662  if (!rounding)
3663  goto accept_dig;
3664 #endif
3665  if (dig == '9') { /* possible if i == 1 */
3666 round_9_up:
3667  *s++ = '9';
3668  goto roundoff;
3669  }
3670  *s++ = dig + 1;
3671  goto ret;
3672  }
3673 #ifdef Honor_FLT_ROUNDS
3674 keep_dig:
3675 #endif
3676  *s++ = dig;
3677  if (i == ilim)
3678  break;
3679  b = multadd(b, 10, 0);
3680  if (mlo == mhi)
3681  mlo = mhi = multadd(mhi, 10, 0);
3682  else {
3683  mlo = multadd(mlo, 10, 0);
3684  mhi = multadd(mhi, 10, 0);
3685  }
3686  }
3687  }
3688  else
3689  for (i = 1;; i++) {
3690  *s++ = dig = quorem(b,S) + '0';
3691  if (!b->x[0] && b->wds <= 1) {
3692 #ifdef SET_INEXACT
3693  inexact = 0;
3694 #endif
3695  goto ret;
3696  }
3697  if (i >= ilim)
3698  break;
3699  b = multadd(b, 10, 0);
3700  }
3701 
3702  /* Round off last digit */
3703 
3704 #ifdef Honor_FLT_ROUNDS
3705  switch (rounding) {
3706  case 0: goto trimzeros;
3707  case 2: goto roundoff;
3708  }
3709 #endif
3710  b = lshift(b, 1);
3711  j = cmp(b, S);
3712  if (j > 0 || (j == 0 && (dig & 1))) {
3713  roundoff:
3714  while (*--s == '9')
3715  if (s == s0) {
3716  k++;
3717  *s++ = '1';
3718  goto ret;
3719  }
3720  ++*s++;
3721  }
3722  else {
3723  while (*--s == '0') ;
3724  s++;
3725  }
3726 ret:
3727  Bfree(S);
3728  if (mhi) {
3729  if (mlo && mlo != mhi)
3730  Bfree(mlo);
3731  Bfree(mhi);
3732  }
3733 ret1:
3734 #ifdef SET_INEXACT
3735  if (inexact) {
3736  if (!oldinexact) {
3737  word0(d) = Exp_1 + (70 << Exp_shift);
3738  word1(d) = 0;
3739  dval(d) += 1.;
3740  }
3741  }
3742  else if (!oldinexact)
3743  clear_inexact();
3744 #endif
3745  Bfree(b);
3746  *s = 0;
3747  *decpt = k + 1;
3748  if (rve)
3749  *rve = s;
3750  return s0;
3751 }
3752 
3753 void
3754 ruby_each_words(const char *str, void (*func)(const char*, int, void*), void *arg)
3755 {
3756  const char *end;
3757  int len;
3758 
3759  if (!str) return;
3760  for (; *str; str = end) {
3761  while (ISSPACE(*str) || *str == ',') str++;
3762  if (!*str) break;
3763  end = str;
3764  while (*end && !ISSPACE(*end) && *end != ',') end++;
3765  len = (int)(end - str); /* assume no string exceeds INT_MAX */
3766  (*func)(str, len, arg);
3767  }
3768 }
3769 
3770 /*-
3771  * Copyright (c) 2004-2008 David Schultz <das@FreeBSD.ORG>
3772  * All rights reserved.
3773  *
3774  * Redistribution and use in source and binary forms, with or without
3775  * modification, are permitted provided that the following conditions
3776  * are met:
3777  * 1. Redistributions of source code must retain the above copyright
3778  * notice, this list of conditions and the following disclaimer.
3779  * 2. Redistributions in binary form must reproduce the above copyright
3780  * notice, this list of conditions and the following disclaimer in the
3781  * documentation and/or other materials provided with the distribution.
3782  *
3783  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
3784  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
3785  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
3786  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
3787  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3788  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3789  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3790  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3791  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3792  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3793  * SUCH DAMAGE.
3794  */
3795 
3796 #define DBL_MANH_SIZE 20
3797 #define DBL_MANL_SIZE 32
3798 #define DBL_ADJ (DBL_MAX_EXP - 2)
3799 #define SIGFIGS ((DBL_MANT_DIG + 3) / 4 + 1)
3800 #define dexp_get(u) ((int)(word0(u) >> Exp_shift) & ~Exp_msk1)
3801 #define dexp_set(u,v) (word0(u) = (((int)(word0(u)) & ~Exp_mask) | ((v) << Exp_shift)))
3802 #define dmanh_get(u) ((uint32_t)(word0(u) & Frac_mask))
3803 #define dmanl_get(u) ((uint32_t)word1(u))
3804 
3805 
3806 /*
3807  * This procedure converts a double-precision number in IEEE format
3808  * into a string of hexadecimal digits and an exponent of 2. Its
3809  * behavior is bug-for-bug compatible with dtoa() in mode 2, with the
3810  * following exceptions:
3811  *
3812  * - An ndigits < 0 causes it to use as many digits as necessary to
3813  * represent the number exactly.
3814  * - The additional xdigs argument should point to either the string
3815  * "0123456789ABCDEF" or the string "0123456789abcdef", depending on
3816  * which case is desired.
3817  * - This routine does not repeat dtoa's mistake of setting decpt
3818  * to 9999 in the case of an infinity or NaN. INT_MAX is used
3819  * for this purpose instead.
3820  *
3821  * Note that the C99 standard does not specify what the leading digit
3822  * should be for non-zero numbers. For instance, 0x1.3p3 is the same
3823  * as 0x2.6p2 is the same as 0x4.cp3. This implementation always makes
3824  * the leading digit a 1. This ensures that the exponent printed is the
3825  * actual base-2 exponent, i.e., ilogb(d).
3826  *
3827  * Inputs: d, xdigs, ndigits
3828  * Outputs: decpt, sign, rve
3829  */
3830 char *
3831 ruby_hdtoa(double d, const char *xdigs, int ndigits, int *decpt, int *sign,
3832  char **rve)
3833 {
3834  U u;
3835  char *s, *s0;
3836  int bufsize;
3837  uint32_t manh, manl;
3838 
3839  u.d = d;
3840  if (word0(u) & Sign_bit) {
3841  /* set sign for everything, including 0's and NaNs */
3842  *sign = 1;
3843  word0(u) &= ~Sign_bit; /* clear sign bit */
3844  }
3845  else
3846  *sign = 0;
3847 
3848  if (isinf(d)) { /* FP_INFINITE */
3849  *decpt = INT_MAX;
3850  return rv_strdup(INFSTR, rve);
3851  }
3852  else if (isnan(d)) { /* FP_NAN */
3853  *decpt = INT_MAX;
3854  return rv_strdup(NANSTR, rve);
3855  }
3856  else if (d == 0.0) { /* FP_ZERO */
3857  *decpt = 1;
3858  return rv_strdup(ZEROSTR, rve);
3859  }
3860  else if (dexp_get(u)) { /* FP_NORMAL */
3861  *decpt = dexp_get(u) - DBL_ADJ;
3862  }
3863  else { /* FP_SUBNORMAL */
3864  u.d *= 5.363123171977039e+154 /* 0x1p514 */;
3865  *decpt = dexp_get(u) - (514 + DBL_ADJ);
3866  }
3867 
3868  if (ndigits == 0) /* dtoa() compatibility */
3869  ndigits = 1;
3870 
3871  /*
3872  * If ndigits < 0, we are expected to auto-size, so we allocate
3873  * enough space for all the digits.
3874  */
3875  bufsize = (ndigits > 0) ? ndigits : SIGFIGS;
3876  s0 = rv_alloc(bufsize+1);
3877 
3878  /* Round to the desired number of digits. */
3879  if (SIGFIGS > ndigits && ndigits > 0) {
3880  float redux = 1.0f;
3881  volatile double d;
3882  int offset = 4 * ndigits + DBL_MAX_EXP - 4 - DBL_MANT_DIG;
3883  dexp_set(u, offset);
3884  d = u.d;
3885  d += redux;
3886  d -= redux;
3887  u.d = d;
3888  *decpt += dexp_get(u) - offset;
3889  }
3890 
3891  manh = dmanh_get(u);
3892  manl = dmanl_get(u);
3893  *s0 = '1';
3894  for (s = s0 + 1; s < s0 + bufsize; s++) {
3895  *s = xdigs[(manh >> (DBL_MANH_SIZE - 4)) & 0xf];
3896  manh = (manh << 4) | (manl >> (DBL_MANL_SIZE - 4));
3897  manl <<= 4;
3898  }
3899 
3900  /* If ndigits < 0, we are expected to auto-size the precision. */
3901  if (ndigits < 0) {
3902  for (ndigits = SIGFIGS; s0[ndigits - 1] == '0'; ndigits--)
3903  ;
3904  }
3905 
3906  s = s0 + ndigits;
3907  *s = '\0';
3908  if (rve != NULL)
3909  *rve = s;
3910  return (s0);
3911 }
3912 
3913 #ifdef __cplusplus
3914 #if 0
3915 { /* satisfy cc-mode */
3916 #endif
3917 }
3918 #endif
3919