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