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