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