CUBRID Engine  latest
mprec.c
Go to the documentation of this file.
1 /****************************************************************
2  *
3  * The author of this software is David M. Gay.
4  *
5  * Copyright (c) 1991 by AT&T.
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose without fee is hereby granted, provided that this entire notice
9  * is included in all copies of any software which is or includes a copy
10  * or modification of this software and in all copies of the supporting
11  * documentation for such software.
12  *
13  * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
14  * WARRANTY. IN PARTICULAR, NEITHER THE AUTHOR NOR AT&T MAKES ANY
15  * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
16  * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
17  *
18  ***************************************************************/
19 
20 /* Please send bug reports to
21  David M. Gay
22  AT&T Bell Laboratories, Room 2C-463
23  600 Mountain Avenue
24  Murray Hill, NJ 07974-2070
25  U.S.A.
26  dmg@research.att.com or research!dmg
27  */
28 
29 /* strtod for IEEE-, VAX-, and IBM-arithmetic machines.
30  *
31  * This strtod returns a nearest machine number to the input decimal
32  * string (or sets errno to ERANGE). With IEEE arithmetic, ties are
33  * broken by the IEEE round-even rule. Otherwise ties are broken by
34  * biased rounding (add half and chop).
35  *
36  * Inspired loosely by William D. Clinger's paper "How to Read Floating
37  * Point Numbers Accurately" [Proc. ACM SIGPLAN '90, pp. 92-101].
38  *
39  * Modifications:
40  *
41  * 1. We only require IEEE, IBM, or VAX double-precision
42  * arithmetic (not IEEE double-extended).
43  * 2. We get by with floating-point arithmetic in a case that
44  * Clinger missed -- when we're computing d * 10^n
45  * for a small integer d and the integer n is not too
46  * much larger than 22 (the maximum integer k for which
47  * we can represent 10^k exactly), we may be able to
48  * compute (d*10^k) * 10^(e-k) with just one roundoff.
49  * 3. Rather than a bit-at-a-time adjustment of the binary
50  * result in the hard case, we use floating-point
51  * arithmetic to determine the adjustment to within
52  * one bit; only in really hard cases do we need to
53  * compute a second residual.
54  * 4. Because of 3., we don't need a large table of powers of 10
55  * for ten-to-e (just some small tables, e.g. of 10^k
56  * for 0 <= k <= 22).
57  */
58 
59 /*
60  * #define IEEE_8087 for IEEE-arithmetic machines where the least
61  * significant byte has the lowest address.
62  * #define IEEE_MC68k for IEEE-arithmetic machines where the most
63  * significant byte has the lowest address.
64  * #define Sudden_Underflow for IEEE-format machines without gradual
65  * underflow (i.e., that flush to zero on underflow).
66  * #define IBM for IBM mainframe-style floating-point arithmetic.
67  * #define VAX for VAX-style floating-point arithmetic.
68  * #define Unsigned_Shifts if >> does treats its left operand as unsigned.
69  * #define No_leftright to omit left-right logic in fast floating-point
70  * computation of dtoa.
71  * #define Check_FLT_ROUNDS if FLT_ROUNDS can assume the values 2 or 3.
72  * #define RND_PRODQUOT to use rnd_prod and rnd_quot (assembly routines
73  * that use extended-precision instructions to compute rounded
74  * products and quotients) with IBM.
75  * #define ROUND_BIASED for IEEE-format with biased rounding.
76  * #define Inaccurate_Divide for IEEE-format with correctly rounded
77  * products but inaccurate quotients, e.g., for Intel i860.
78  * #define Just_16 to store 16 bits per 32-bit long when doing high-precision
79  * integer arithmetic. Whether this speeds things up or slows things
80  * down depends on the machine and the number being converted.
81  */
82 
83 /*#include <_ansi.h>*/
84 #include <assert.h>
85 #include <stdlib.h>
86 #include <string.h>
87 /* #include <reent.h> */
88 #include "mprec.h"
89 
90 /* reent.c knows this value */
91 /* #define _Kmax 15 */
92 
93 #define _reent _Jv_reent
94 #define _Bigint _Jv_Bigint
95 
96 #define _REENT_CHECK_MP(x)
97 #define _REENT_MP_FREELIST(x) ((x)->_freelist)
98 #define _REENT_MP_P5S(x) ((x)->_p5s)
99 
100 typedef unsigned long __ULong;
101 typedef long __Long;
102 
103 static void *
104 mprec_calloc (void *ignore, size_t x1, size_t x2)
105 {
106  char *result = (char *) malloc (x1 * x2);
107  memset (result, 0, x1 * x2);
108  return result;
109 }
110 
111 _Bigint *
112 _DEFUN (Balloc, (ptr, k), struct _reent * ptr _AND int k)
113 {
114  int x;
115  _Bigint *rv;
116  int new_k = k + 1;
117 
118  _REENT_CHECK_MP (ptr);
119  if (_REENT_MP_FREELIST (ptr) == NULL)
120  {
121  /* Allocate a list of pointers to the mprec objects */
122  _REENT_MP_FREELIST (ptr) = (struct _Bigint **) mprec_calloc (ptr, sizeof (struct _Bigint *), new_k);
123  if (_REENT_MP_FREELIST (ptr) == NULL)
124  {
125  return NULL;
126  }
127  ptr->_max_k = new_k;
128  }
129  else if (new_k > ptr->_max_k)
130  {
131  struct _Bigint **new_list = (struct _Bigint **) realloc (ptr->_freelist,
132  new_k * sizeof (struct _Bigint *));
133  memset (&new_list[ptr->_max_k], 0, (new_k - ptr->_max_k) * sizeof (struct _Bigint *));
134  ptr->_freelist = new_list;
135  ptr->_max_k = new_k;
136 
137  }
138 
139  assert (k <= ptr->_max_k);
140 
141  if ((rv = _REENT_MP_FREELIST (ptr)[k]) != 0)
142  {
143  _REENT_MP_FREELIST (ptr)[k] = rv->_next;
144  }
145  else
146  {
147  x = 1 << k;
148  /* Allocate an mprec Bigint and stick in in the freelist */
149  rv = (_Bigint *) mprec_calloc (ptr, 1, sizeof (_Bigint) + (x - 1) * sizeof (rv->_x));
150  if (rv == NULL)
151  return NULL;
152  rv->_k = k;
153  rv->_maxwds = x;
154  }
155  rv->_sign = rv->_wds = 0;
156  return rv;
157 }
158 
159 void
160 _DEFUN (Bfree, (ptr, v), struct _reent *ptr _AND _Bigint * v)
161 {
162  _REENT_CHECK_MP (ptr);
163  if (v)
164  {
165  v->_next = _REENT_MP_FREELIST (ptr)[v->_k];
166  _REENT_MP_FREELIST (ptr)[v->_k] = v;
167  }
168 }
169 
170 _Bigint *
171 _DEFUN (multadd, (ptr, b, m, a), struct _reent *ptr _AND _Bigint * b _AND int m _AND int a)
172 {
173  int i, wds;
174  __ULong *x, y;
175 #ifdef Pack_32
176  __ULong xi, z;
177 #endif
178  _Bigint *b1;
179 
180  wds = b->_wds;
181  x = b->_x;
182  i = 0;
183  do
184  {
185 #ifdef Pack_32
186  xi = *x;
187  y = (xi & 0xffff) * m + a;
188  z = (xi >> 16) * m + (y >> 16);
189  a = (int) (z >> 16);
190  *x++ = (z << 16) + (y & 0xffff);
191 #else
192  y = *x * m + a;
193  a = (int) (y >> 16);
194  *x++ = y & 0xffff;
195 #endif
196  }
197  while (++i < wds);
198  if (a)
199  {
200  if (wds >= b->_maxwds)
201  {
202  b1 = Balloc (ptr, b->_k + 1);
203  Bcopy (b1, b);
204  Bfree (ptr, b);
205  b = b1;
206  }
207  b->_x[wds++] = a;
208  b->_wds = wds;
209  }
210  return b;
211 }
212 
213 #if defined (ENABLE_UNUSED_FUNCTION)
214 _Bigint *
215 _DEFUN (s2b, (ptr, s, nd0, nd, y9), struct _reent * ptr _AND _CONST char *s _AND int nd0 _AND int nd _AND __ULong y9)
216 {
217  _Bigint *b;
218  int i, k;
219  __Long x, y;
220 
221  x = (nd + 8) / 9;
222  for (k = 0, y = 1; x > y; y <<= 1, k++);
223 #ifdef Pack_32
224  b = Balloc (ptr, k);
225  b->_x[0] = y9;
226  b->_wds = 1;
227 #else
228  b = Balloc (ptr, k + 1);
229  b->_x[0] = y9 & 0xffff;
230  b->_wds = (b->_x[1] = y9 >> 16) ? 2 : 1;
231 #endif
232 
233  i = 9;
234  if (9 < nd0)
235  {
236  s += 9;
237  do
238  b = multadd (ptr, b, 10, *s++ - '0');
239  while (++i < nd0);
240  s++;
241  }
242  else
243  s += 10;
244  for (; i < nd; i++)
245  b = multadd (ptr, b, 10, *s++ - '0');
246  return b;
247 }
248 
249 #endif /* ENABLE_UNUSED_FUNCTION */
250 int
252 {
253  int k = 0;
254 
255  if (!(x & 0xffff0000))
256  {
257  k = 16;
258  x <<= 16;
259  }
260  if (!(x & 0xff000000))
261  {
262  k += 8;
263  x <<= 8;
264  }
265  if (!(x & 0xf0000000))
266  {
267  k += 4;
268  x <<= 4;
269  }
270  if (!(x & 0xc0000000))
271  {
272  k += 2;
273  x <<= 2;
274  }
275  if (!(x & 0x80000000))
276  {
277  k++;
278  if (!(x & 0x40000000))
279  return 32;
280  }
281  return k;
282 }
283 
284 int
285 _DEFUN (lo0bits, (y), __ULong * y)
286 {
287  int k;
288  __ULong x = *y;
289 
290  if (x & 7)
291  {
292  if (x & 1)
293  return 0;
294  if (x & 2)
295  {
296  *y = x >> 1;
297  return 1;
298  }
299  *y = x >> 2;
300  return 2;
301  }
302  k = 0;
303  if (!(x & 0xffff))
304  {
305  k = 16;
306  x >>= 16;
307  }
308  if (!(x & 0xff))
309  {
310  k += 8;
311  x >>= 8;
312  }
313  if (!(x & 0xf))
314  {
315  k += 4;
316  x >>= 4;
317  }
318  if (!(x & 0x3))
319  {
320  k += 2;
321  x >>= 2;
322  }
323  if (!(x & 1))
324  {
325  k++;
326  x >>= 1;
327  if (!x & 1)
328  return 32;
329  }
330  *y = x;
331  return k;
332 }
333 
334 _Bigint *
335 _DEFUN (i2b, (ptr, i), struct _reent * ptr _AND int i)
336 {
337  _Bigint *b;
338 
339  b = Balloc (ptr, 1);
340  b->_x[0] = i;
341  b->_wds = 1;
342  return b;
343 }
344 
345 _Bigint *
346 _DEFUN (mult, (ptr, a, b), struct _reent * ptr _AND _Bigint * a _AND _Bigint * b)
347 {
348  _Bigint *c;
349  int k, wa, wb, wc;
350  __ULong carry, y, z;
351  __ULong *x, *xa, *xae, *xb, *xbe, *xc, *xc0;
352 #ifdef Pack_32
353  __ULong z2;
354 #endif
355 
356  if (a->_wds < b->_wds)
357  {
358  c = a;
359  a = b;
360  b = c;
361  }
362  k = a->_k;
363  wa = a->_wds;
364  wb = b->_wds;
365  wc = wa + wb;
366  if (wc > a->_maxwds)
367  k++;
368  c = Balloc (ptr, k);
369  for (x = c->_x, xa = x + wc; x < xa; x++)
370  *x = 0;
371  xa = a->_x;
372  xae = xa + wa;
373  xb = b->_x;
374  xbe = xb + wb;
375  xc0 = c->_x;
376 #ifdef Pack_32
377  for (; xb < xbe; xb++, xc0++)
378  {
379  if ((y = *xb & 0xffff) != 0)
380  {
381  x = xa;
382  xc = xc0;
383  carry = 0;
384  do
385  {
386  z = (*x & 0xffff) * y + (*xc & 0xffff) + carry;
387  carry = z >> 16;
388  z2 = (*x++ >> 16) * y + (*xc >> 16) + carry;
389  carry = z2 >> 16;
390  Storeinc (xc, z2, z);
391  }
392  while (x < xae);
393  *xc = carry;
394  }
395  if ((y = *xb >> 16) != 0)
396  {
397  x = xa;
398  xc = xc0;
399  carry = 0;
400  z2 = *xc;
401  do
402  {
403  z = (*x & 0xffff) * y + (*xc >> 16) + carry;
404  carry = z >> 16;
405  Storeinc (xc, z, z2);
406  z2 = (*x++ >> 16) * y + (*xc & 0xffff) + carry;
407  carry = z2 >> 16;
408  }
409  while (x < xae);
410  *xc = z2;
411  }
412  }
413 #else
414  for (; xb < xbe; xc0++)
415  {
416  if ((y = *xb++))
417  {
418  x = xa;
419  xc = xc0;
420  carry = 0;
421  do
422  {
423  z = *x++ * y + *xc + carry;
424  carry = z >> 16;
425  *xc++ = z & 0xffff;
426  }
427  while (x < xae);
428  *xc = carry;
429  }
430  }
431 #endif
432  for (xc0 = c->_x, xc = xc0 + wc; wc > 0 && !*--xc; --wc);
433  c->_wds = wc;
434  return c;
435 }
436 
437 _Bigint *
438 _DEFUN (pow5mult, (ptr, b, k), struct _reent * ptr _AND _Bigint * b _AND int k)
439 {
440  _Bigint *b1, *p5, *p51;
441  int i;
442  static _CONST int p05[3] = { 5, 25, 125 };
443 
444  if ((i = k & 3) != 0)
445  b = multadd (ptr, b, p05[i - 1], 0);
446 
447  if (!(k >>= 2))
448  return b;
449  _REENT_CHECK_MP (ptr);
450  if (!(p5 = _REENT_MP_P5S (ptr)))
451  {
452  /* first time */
453  p5 = _REENT_MP_P5S (ptr) = i2b (ptr, 625);
454  p5->_next = 0;
455  }
456  for (;;)
457  {
458  if (k & 1)
459  {
460  b1 = mult (ptr, b, p5);
461  Bfree (ptr, b);
462  b = b1;
463  }
464  if (!(k >>= 1))
465  break;
466  if (!(p51 = p5->_next))
467  {
468  p51 = p5->_next = mult (ptr, p5, p5);
469  p51->_next = 0;
470  }
471  p5 = p51;
472  }
473  return b;
474 }
475 
476 _Bigint *
477 _DEFUN (lshift, (ptr, b, k), struct _reent * ptr _AND _Bigint * b _AND int k)
478 {
479  int i, k1, n, n1;
480  _Bigint *b1;
481  __ULong *x, *x1, *xe, z;
482 
483 #ifdef Pack_32
484  n = k >> 5;
485 #else
486  n = k >> 4;
487 #endif
488  k1 = b->_k;
489  n1 = n + b->_wds + 1;
490  for (i = b->_maxwds; n1 > i; i <<= 1)
491  k1++;
492  b1 = Balloc (ptr, k1);
493  x1 = b1->_x;
494  for (i = 0; i < n; i++)
495  *x1++ = 0;
496  x = b->_x;
497  xe = x + b->_wds;
498 #ifdef Pack_32
499  if (k &= 0x1f)
500  {
501  k1 = 32 - k;
502  z = 0;
503  do
504  {
505  *x1++ = *x << k | z;
506  z = *x++ >> k1;
507  }
508  while (x < xe);
509  if ((*x1 = z) != 0)
510  ++n1;
511  }
512 #else
513  if (k &= 0xf)
514  {
515  k1 = 16 - k;
516  z = 0;
517  do
518  {
519  *x1++ = (*x << k & 0xffff) | z;
520  z = *x++ >> k1;
521  }
522  while (x < xe);
523  if ((*x1 = z))
524  ++n1;
525  }
526 #endif
527  else
528  do
529  *x1++ = *x++;
530  while (x < xe);
531  b1->_wds = n1 - 1;
532  Bfree (ptr, b);
533  return b1;
534 }
535 
536 int
537 _DEFUN (cmp, (a, b), _Bigint * a _AND _Bigint * b)
538 {
539  __ULong *xa, *xa0, *xb, *xb0;
540  int i, j;
541 
542  i = a->_wds;
543  j = b->_wds;
544 #ifdef DEBUG
545  if (i > 1 && !a->_x[i - 1])
546  Bug ("cmp called with a->_x[a->_wds-1] == 0");
547  if (j > 1 && !b->_x[j - 1])
548  Bug ("cmp called with b->_x[b->_wds-1] == 0");
549 #endif
550  if (i -= j)
551  return i;
552  xa0 = a->_x;
553  xa = xa0 + j;
554  xb0 = b->_x;
555  xb = xb0 + j;
556  for (;;)
557  {
558  if (*--xa != *--xb)
559  return *xa < *xb ? -1 : 1;
560  if (xa <= xa0)
561  break;
562  }
563  return 0;
564 }
565 
566 _Bigint *
567 _DEFUN (diff, (ptr, a, b), struct _reent * ptr _AND _Bigint * a _AND _Bigint * b)
568 {
569  _Bigint *c;
570  int i, wa, wb;
571  __Long borrow, y; /* We need signed shifts here. */
572  __ULong *xa, *xae, *xb, *xbe, *xc;
573 #ifdef Pack_32
574  __Long z;
575 #endif
576 
577  i = cmp (a, b);
578  if (!i)
579  {
580  c = Balloc (ptr, 0);
581  c->_wds = 1;
582  c->_x[0] = 0;
583  return c;
584  }
585  if (i < 0)
586  {
587  c = a;
588  a = b;
589  b = c;
590  i = 1;
591  }
592  else
593  i = 0;
594  c = Balloc (ptr, a->_k);
595  c->_sign = i;
596  wa = a->_wds;
597  xa = a->_x;
598  xae = xa + wa;
599  wb = b->_wds;
600  xb = b->_x;
601  xbe = xb + wb;
602  xc = c->_x;
603  borrow = 0;
604 #ifdef Pack_32
605  do
606  {
607  y = (*xa & 0xffff) - (*xb & 0xffff) + borrow;
608  borrow = y >> 16;
609  Sign_Extend (borrow, y);
610  z = (*xa++ >> 16) - (*xb++ >> 16) + borrow;
611  borrow = z >> 16;
612  Sign_Extend (borrow, z);
613  Storeinc (xc, z, y);
614  }
615  while (xb < xbe);
616  while (xa < xae)
617  {
618  y = (*xa & 0xffff) + borrow;
619  borrow = y >> 16;
620  Sign_Extend (borrow, y);
621  z = (*xa++ >> 16) + borrow;
622  borrow = z >> 16;
623  Sign_Extend (borrow, z);
624  Storeinc (xc, z, y);
625  }
626 #else
627  do
628  {
629  y = *xa++ - *xb++ + borrow;
630  borrow = y >> 16;
631  Sign_Extend (borrow, y);
632  *xc++ = y & 0xffff;
633  }
634  while (xb < xbe);
635  while (xa < xae)
636  {
637  y = *xa++ + borrow;
638  borrow = y >> 16;
639  Sign_Extend (borrow, y);
640  *xc++ = y & 0xffff;
641  }
642 #endif
643  while (!*--xc)
644  wa--;
645  c->_wds = wa;
646  return c;
647 }
648 
649 #if defined (ENABLE_UNUSED_FUNCTION)
650 double
651 _DEFUN (ulp, (_x), double _x)
652 {
653  union double_union x, a;
654  register int32_t L;
655 
656  x.d = _x;
657 
658  L = (word0 (x) & Exp_mask) - (PREC - 1) * Exp_msk1;
659 #ifndef Sudden_Underflow
660  if (L > 0)
661  {
662 #endif
663 #ifdef IBM
664  L |= Exp_msk1 >> 4;
665 #endif
666  word0 (a) = L;
667 #ifndef _DOUBLE_IS_32BITS
668  word1 (a) = 0;
669 #endif
670 
671 #ifndef Sudden_Underflow
672  }
673  else
674  {
675  L = -L >> Exp_shift;
676  if (L < Exp_shift)
677  {
678  word0 (a) = 0x80000 >> L;
679 #ifndef _DOUBLE_IS_32BITS
680  word1 (a) = 0;
681 #endif
682  }
683  else
684  {
685  word0 (a) = 0;
686  L -= Exp_shift;
687 #ifndef _DOUBLE_IS_32BITS
688  word1 (a) = L >= 31 ? 1 : 1 << (31 - L);
689 #endif
690  }
691  }
692 #endif
693  return a.d;
694 }
695 
696 double
697 _DEFUN (b2d, (a, e), _Bigint * a _AND int *e)
698 {
699  __ULong *xa, *xa0, w, y, z;
700  int k;
701  union double_union d;
702 #ifdef VAX
703  __ULong d0, d1;
704 #else
705 #define d0 word0(d)
706 #define d1 word1(d)
707 #endif
708 
709  xa0 = a->_x;
710  xa = xa0 + a->_wds;
711  y = *--xa;
712 #ifdef DEBUG
713  if (!y)
714  Bug ("zero y in b2d");
715 #endif
716  k = hi0bits (y);
717  *e = 32 - k;
718 #ifdef Pack_32
719  if (k < Ebits)
720  {
721  d0 = Exp_1 | y >> (Ebits - k);
722  w = xa > xa0 ? *--xa : 0;
723 #ifndef _DOUBLE_IS_32BITS
724  d1 = y << ((32 - Ebits) + k) | w >> (Ebits - k);
725 #endif
726  goto ret_d;
727  }
728  z = xa > xa0 ? *--xa : 0;
729  if (k -= Ebits)
730  {
731  d0 = Exp_1 | y << k | z >> (32 - k);
732  y = xa > xa0 ? *--xa : 0;
733 #ifndef _DOUBLE_IS_32BITS
734  d1 = z << k | y >> (32 - k);
735 #endif
736  }
737  else
738  {
739  d0 = Exp_1 | y;
740 #ifndef _DOUBLE_IS_32BITS
741  d1 = z;
742 #endif
743  }
744 #else
745  if (k < Ebits + 16)
746  {
747  z = xa > xa0 ? *--xa : 0;
748  d0 = Exp_1 | y << (k - Ebits) | z >> (Ebits + 16 - k);
749  w = xa > xa0 ? *--xa : 0;
750  y = xa > xa0 ? *--xa : 0;
751  d1 = z << (k + 16 - Ebits) | w << (k - Ebits) | y >> (16 + Ebits - k);
752  goto ret_d;
753  }
754  z = xa > xa0 ? *--xa : 0;
755  w = xa > xa0 ? *--xa : 0;
756  k -= Ebits + 16;
757  d0 = Exp_1 | y << (k + 16) | z << k | w >> (16 - k);
758  y = xa > xa0 ? *--xa : 0;
759  d1 = w << (k + 16) | y << k;
760 #endif
761 ret_d:
762 #ifdef VAX
763  word0 (d) = d0 >> 16 | d0 << 16;
764  word1 (d) = d1 >> 16 | d1 << 16;
765 #else
766 #undef d0
767 #undef d1
768 #endif
769  return d.d;
770 }
771 
772 #endif /* ENABLE_UNUSED_FUNCTION */
773 _Bigint *
774 _DEFUN (d2b, (ptr, _d, e, bits), struct _reent * ptr _AND double _d _AND int *e _AND int *bits)
775 {
776  union double_union d;
777  _Bigint *b;
778  int de, i, k;
779  __ULong *x, y, z;
780 #ifdef VAX
781  __ULong d0, d1;
782 #endif
783  d.d = _d;
784 #ifdef VAX
785  d0 = word0 (d) >> 16 | word0 (d) << 16;
786  d1 = word1 (d) >> 16 | word1 (d) << 16;
787 #else
788 #define d0 word0(d)
789 #define d1 word1(d)
790  d.d = _d;
791 #endif
792 
793 #ifdef Pack_32
794  b = Balloc (ptr, 1);
795 #else
796  b = Balloc (ptr, 2);
797 #endif
798  x = b->_x;
799 
800  z = d0 & Frac_mask;
801  d0 &= 0x7fffffff; /* clear sign bit, which we ignore */
802 #ifdef Sudden_Underflow
803  de = (int) (d0 >> Exp_shift);
804 #ifndef IBM
805  z |= Exp_msk11;
806 #endif
807 #else
808  if ((de = (int) (d0 >> Exp_shift)) != 0)
809  z |= Exp_msk1;
810 #endif
811 #ifdef Pack_32
812 #ifndef _DOUBLE_IS_32BITS
813  if (d1)
814  {
815  y = d1;
816  k = lo0bits (&y);
817  if (k)
818  {
819  x[0] = y | z << (32 - k);
820  z >>= k;
821  }
822  else
823  x[0] = y;
824  i = b->_wds = (x[1] = z) ? 2 : 1;
825  }
826  else
827 #endif
828  {
829 #ifdef DEBUG
830  if (!z)
831  Bug ("Zero passed to d2b");
832 #endif
833  k = lo0bits (&z);
834  x[0] = z;
835  i = b->_wds = 1;
836 #ifndef _DOUBLE_IS_32BITS
837  k += 32;
838 #endif
839  }
840 #else
841  if (d1)
842  {
843  y = d1;
844  k = lo0bits (&y);
845  if (k)
846  if (k >= 16)
847  {
848  x[0] = y | (z << (32 - k) & 0xffff);
849  x[1] = z >> (k - 16) & 0xffff;
850  x[2] = z >> k;
851  i = 2;
852  }
853  else
854  {
855  x[0] = y & 0xffff;
856  x[1] = y >> 16 | (z << (16 - k) & 0xffff);
857  x[2] = z >> k & 0xffff;
858  x[3] = z >> (k + 16);
859  i = 3;
860  }
861  else
862  {
863  x[0] = y & 0xffff;
864  x[1] = y >> 16;
865  x[2] = z & 0xffff;
866  x[3] = z >> 16;
867  i = 3;
868  }
869  }
870  else
871  {
872 #ifdef DEBUG
873  if (!z)
874  Bug ("Zero passed to d2b");
875 #endif
876  k = lo0bits (&z);
877  if (k >= 16)
878  {
879  x[0] = z;
880  i = 0;
881  }
882  else
883  {
884  x[0] = z & 0xffff;
885  x[1] = z >> 16;
886  i = 1;
887  }
888  k += 32;
889  }
890  while (!x[i])
891  --i;
892  b->_wds = i + 1;
893 #endif
894 #ifndef Sudden_Underflow
895  if (de)
896  {
897 #endif
898 #ifdef IBM
899  *e = (de - Bias - (PREC - 1) << 2) + k;
900  *bits = 4 * PREC + 8 - k - hi0bits (word0 (d) & Frac_mask);
901 #else
902  *e = de - Bias - (PREC - 1) + k;
903  *bits = PREC - k;
904 #endif
905 #ifndef Sudden_Underflow
906  }
907  else
908  {
909  *e = de - Bias - (PREC - 1) + 1 + k;
910 #ifdef Pack_32
911  *bits = 32 * i - hi0bits (x[i - 1]);
912 #else
913  *bits = (i + 2) * 16 - hi0bits (x[i]);
914 #endif
915  }
916 #endif
917  return b;
918 }
919 
920 #undef d0
921 #undef d1
922 
923 #if defined (ENABLE_UNUSED_FUNCTION)
924 double
925 _DEFUN (ratio, (a, b), _Bigint * a _AND _Bigint * b)
926 {
927  union double_union da, db;
928  int k, ka, kb;
929 
930  da.d = b2d (a, &ka);
931  db.d = b2d (b, &kb);
932 #ifdef Pack_32
933  k = ka - kb + 32 * (a->_wds - b->_wds);
934 #else
935  k = ka - kb + 16 * (a->_wds - b->_wds);
936 #endif
937 #ifdef IBM
938  if (k > 0)
939  {
940  word0 (da) += (k >> 2) * Exp_msk1;
941  if (k &= 3)
942  da.d *= 1 << k;
943  }
944  else
945  {
946  k = -k;
947  word0 (db) += (k >> 2) * Exp_msk1;
948  if (k &= 3)
949  db.d *= 1 << k;
950  }
951 #else
952  if (k > 0)
953  word0 (da) += k * Exp_msk1;
954  else
955  {
956  k = -k;
957  word0 (db) += k * Exp_msk1;
958  }
959 #endif
960  return da.d / db.d;
961 }
962 #endif /* ENABLE_UNUSED_FUNCTION */
963 
964 _CONST double tens[] = {
965  1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9,
966  1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19,
967  1e20, 1e21, 1e22, 1e23, 1e24
968 };
969 
970 #if !defined(_DOUBLE_IS_32BITS) && !defined(__v800)
971 _CONST double bigtens[] = { 1e16, 1e32, 1e64, 1e128, 1e256 };
972 
973 _CONST double tinytens[] = { 1e-16, 1e-32, 1e-64, 1e-128, 1e-256 };
974 #else
975 _CONST double bigtens[] = { 1e16, 1e32 };
976 
977 _CONST double tinytens[] = { 1e-16, 1e-32 };
978 #endif
979 
980 #if defined (ENABLE_UNUSED_FUNCTION)
981 
982 double
983 _DEFUN (_mprec_log10, (dig), int dig)
984 {
985  double v = 1.0;
986  if (dig < 24)
987  return tens[dig];
988  while (dig > 0)
989  {
990  v *= 10;
991  dig--;
992  }
993  return v;
994 }
995 #endif /* ENABLE_UNUSED_FUNCTION */
#define Bcopy(x, y)
Definition: mprec.h:390
#define _AND
Definition: mprec.h:309
#define _reent
Definition: mprec.c:93
#define Sign_Extend(a, b)
Definition: mprec.h:94
#define word1(x)
Definition: mprec.h:115
_CONST double bigtens[]
Definition: mprec.c:971
unsigned long __ULong
Definition: mprec.c:100
#define d0
#define Balloc
Definition: mprec.h:341
#define ulp
Definition: mprec.h:353
_CONST double tinytens[]
Definition: mprec.c:973
#define i2b
Definition: mprec.h:347
#define diff
Definition: mprec.h:352
static void * mprec_calloc(void *ignore, size_t x1, size_t x2)
Definition: mprec.c:104
#define _REENT_MP_P5S(x)
Definition: mprec.c:98
#define Ebits
Definition: mprec.h:240
double d
Definition: mprec.h:106
#define pow5mult
Definition: mprec.h:349
#define assert(x)
#define hi0bits
Definition: mprec.h:346
#define _CONST
Definition: mprec.h:311
#define b2d
Definition: mprec.h:354
#define lshift
Definition: mprec.h:350
#define d2b
Definition: mprec.h:355
_CONST double tens[]
Definition: mprec.c:964
static int rv
Definition: area_alloc.c:52
#define NULL
Definition: freelistheap.h:34
#define PREC
Definition: mprec.h:236
#define mult
Definition: mprec.h:348
#define Bias
Definition: mprec.h:237
#define ratio
Definition: mprec.h:356
#define Frac_mask
Definition: mprec.h:241
#define Exp_mask
Definition: mprec.h:235
#define s2b
Definition: mprec.h:344
#define cmp
Definition: mprec.h:351
#define d1
_Bigint * _DEFUN(Balloc,(ptr, k), struct _reent *ptr _AND int k)
Definition: mprec.c:112
#define Exp_msk1
Definition: mprec.h:233
#define Exp_shift
Definition: mprec.h:231
#define Exp_msk11
Definition: mprec.h:234
#define _Bigint
Definition: mprec.c:94
#define Storeinc(a, b, c)
Definition: mprec.h:126
#define multadd
Definition: mprec.h:343
int i
Definition: dynamic_load.c:954
#define lo0bits
Definition: mprec.h:345
#define _REENT_MP_FREELIST(x)
Definition: mprec.c:97
#define word0(x)
Definition: mprec.h:114
#define _REENT_CHECK_MP(x)
Definition: mprec.c:96
#define Exp_1
Definition: mprec.h:238
#define Bfree
Definition: mprec.h:342
long __Long
Definition: mprec.c:101