89 #include "small_mprec.h"
94 #if defined (_SMALL_PRINTF) || defined(SMALL_SCANF)
113 _DEFUN (Balloc, (ptr, k),
struct _reent *ptr _AND
int k)
118 _REENT_CHECK_MP(ptr);
119 if (_REENT_MP_FREELIST(ptr) ==
NULL)
122 _REENT_MP_FREELIST(ptr) = (
struct _Bigint **) _calloc_r (ptr,
123 sizeof (
struct _Bigint *),
125 if (_REENT_MP_FREELIST(ptr) ==
NULL)
131 if ((rv = _REENT_MP_FREELIST(ptr)[k]) != 0)
133 _REENT_MP_FREELIST(ptr)[k] = rv->_next;
139 rv = (_Bigint *) _calloc_r (ptr,
142 (x-1) *
sizeof(rv->_x));
147 rv->_sign = rv->_wds = 0;
152 _DEFUN (Bfree, (ptr, v),
struct _reent *ptr _AND _Bigint * v)
154 _REENT_CHECK_MP(ptr);
157 v->_next = _REENT_MP_FREELIST(ptr)[v->_k];
158 _REENT_MP_FREELIST(ptr)[v->_k] = v;
164 _DEFUN (multadd, (ptr, b, m, a),
165 struct _reent *ptr _AND
184 y = (xi & 0xffff) * m + a;
185 z = (xi >> 16) * m + (y >> 16);
187 *x++ = (z << 16) + (y & 0xffff);
197 if (wds >= b->_maxwds)
199 b1 = Balloc (ptr, b->_k + 1);
211 _DEFUN (s2b, (ptr, s, nd0, nd, y9),
212 struct _reent * ptr _AND
223 for (k = 0, y = 1; x > y; y <<= 1, k++);
229 b = Balloc (ptr, k + 1);
230 b->_x[0] = y9 & 0xffff;
231 b->_wds = (b->_x[1] = y9 >> 16) ? 2 : 1;
239 b = multadd (ptr, b, 10, *s++ -
'0');
246 b = multadd (ptr, b, 10, *s++ -
'0');
252 (x),
register __ULong x)
256 if (!(x & 0xffff0000))
261 if (!(x & 0xff000000))
266 if (!(x & 0xf0000000))
271 if (!(x & 0xc0000000))
276 if (!(x & 0x80000000))
279 if (!(x & 0x40000000))
286 _DEFUN (lo0bits, (y), __ULong *y)
289 register __ULong x = *y;
336 _DEFUN (i2b, (ptr, i),
struct _reent * ptr _AND
int i)
347 _DEFUN (mult, (ptr, a, b),
struct _reent * ptr _AND _Bigint * a _AND _Bigint * b)
352 __ULong *x, *xa, *xae, *xb, *xbe, *xc, *xc0;
357 if (a->_wds < b->_wds)
370 for (x = c->_x, xa = x + wc; x < xa; x++)
378 for (; xb < xbe; xb++, xc0++)
380 if ((y = *xb & 0xffff) != 0)
387 z = (*x & 0xffff) * y + (*xc & 0xffff) + carry;
389 z2 = (*x++ >> 16) * y + (*xc >> 16) + carry;
391 Storeinc (xc, z2, z);
396 if ((y = *xb >> 16) != 0)
404 z = (*x & 0xffff) * y + (*xc >> 16) + carry;
406 Storeinc (xc, z, z2);
407 z2 = (*x++ >> 16) * y + (*xc & 0xffff) + carry;
415 for (; xb < xbe; xc0++)
424 z = *x++ * y + *xc + carry;
433 for (xc0 = c->_x, xc = xc0 + wc; wc > 0 && !*--xc; --wc);
440 (ptr, b, k),
struct _reent * ptr _AND _Bigint * b _AND
int k)
442 _Bigint *b1, *p5, *p51;
444 static _CONST
int p05[3] = {5, 25, 125};
446 if ((i = k & 3) != 0)
447 b = multadd (ptr, b, p05[i - 1], 0);
451 _REENT_CHECK_MP(ptr);
452 if (!(p5 = _REENT_MP_P5S(ptr)))
455 p5 = _REENT_MP_P5S(ptr) = i2b (ptr, 625);
462 b1 = mult (ptr, b, p5);
468 if (!(p51 = p5->_next))
470 p51 = p5->_next = mult (ptr, p5, p5);
479 _DEFUN (lshift, (ptr, b, k),
struct _reent * ptr _AND _Bigint * b _AND
int k)
483 __ULong *x, *x1, *xe, z;
491 n1 = n + b->_wds + 1;
492 for (i = b->_maxwds; n1 > i; i <<= 1)
494 b1 = Balloc (ptr, k1);
496 for (i = 0; i < n; i++)
521 *x1++ = *x << k & 0xffff | z;
539 _DEFUN (cmp, (a, b), _Bigint * a _AND _Bigint * b)
541 __ULong *xa, *xa0, *xb, *xb0;
547 if (i > 1 && !a->_x[i - 1])
548 Bug (
"cmp called with a->_x[a->_wds-1] == 0");
549 if (j > 1 && !b->_x[j - 1])
550 Bug (
"cmp called with b->_x[b->_wds-1] == 0");
561 return *xa < *xb ? -1 : 1;
569 _DEFUN (diff, (ptr, a, b),
struct _reent * ptr _AND
570 _Bigint * a _AND _Bigint * b)
575 __ULong *xa, *xae, *xb, *xbe, *xc;
597 c = Balloc (ptr, a->_k);
610 y = (*xa & 0xffff) - (*xb & 0xffff) + borrow;
612 Sign_Extend (borrow, y);
613 z = (*xa++ >> 16) - (*xb++ >> 16) + borrow;
615 Sign_Extend (borrow, z);
621 y = (*xa & 0xffff) + borrow;
623 Sign_Extend (borrow, y);
624 z = (*xa++ >> 16) + borrow;
626 Sign_Extend (borrow, z);
632 y = *xa++ - *xb++ + borrow;
634 Sign_Extend (borrow, y);
642 Sign_Extend (borrow, y);
653 _DEFUN (ulp, (_x),
double _x)
655 union double_union x, a;
660 L = (word0 (x) & Exp_mask) - (P - 1) * Exp_msk1;
661 #ifndef Sudden_Underflow
669 #ifndef _DOUBLE_IS_32BITS
673 #ifndef Sudden_Underflow
680 word0 (a) = 0x80000 >> L;
681 #ifndef _DOUBLE_IS_32BITS
689 #ifndef _DOUBLE_IS_32BITS
690 word1 (a) = L >= 31 ? 1 : 1 << (31 - L);
700 _Bigint * a _AND
int *e)
702 __ULong *xa, *xa0, w, y, z;
704 union double_union d;
717 Bug (
"zero y in b2d");
724 d0 = Exp_1 | y >> (Ebits - k);
725 w = xa > xa0 ? *--xa : 0;
726 #ifndef _DOUBLE_IS_32BITS
727 d1 = y << ((32 - Ebits) + k) | w >> (Ebits - k);
731 z = xa > xa0 ? *--xa : 0;
734 d0 = Exp_1 | y << k | z >> (32 - k);
735 y = xa > xa0 ? *--xa : 0;
736 #ifndef _DOUBLE_IS_32BITS
737 d1 = z << k | y >> (32 - k);
743 #ifndef _DOUBLE_IS_32BITS
750 z = xa > xa0 ? *--xa : 0;
751 d0 = Exp_1 | y << k - Ebits | z >> Ebits + 16 - k;
752 w = xa > xa0 ? *--xa : 0;
753 y = xa > xa0 ? *--xa : 0;
754 d1 = z << k + 16 - Ebits | w << k - Ebits | y >> 16 + Ebits - k;
757 z = xa > xa0 ? *--xa : 0;
758 w = xa > xa0 ? *--xa : 0;
760 d0 = Exp_1 | y << k + 16 | z << k | w >> 16 - k;
761 y = xa > xa0 ? *--xa : 0;
762 d1 = w << k + 16 | y << k;
766 word0 (d) = d0 >> 16 | d0 << 16;
767 word1 (d) = d1 >> 16 | d1 << 16;
778 struct _reent * ptr _AND
784 union double_union d;
793 d0 = word0 (d) >> 16 | word0 (d) << 16;
794 d1 = word1 (d) >> 16 | word1 (d) << 16;
810 #ifdef Sudden_Underflow
811 de = (int) (d0 >> Exp_shift);
816 if ((de = (
int) (d0 >> Exp_shift)) != 0)
820 #ifndef _DOUBLE_IS_32BITS
827 x[0] = y | z << (32 - k);
832 i = b->_wds = (x[1] = z) ? 2 : 1;
839 Bug (
"Zero passed to d2b");
844 #ifndef _DOUBLE_IS_32BITS
856 x[0] = y | z << 32 - k & 0xffff;
857 x[1] = z >> k - 16 & 0xffff;
864 x[1] = y >> 16 | z << 16 - k & 0xffff;
865 x[2] = z >> k & 0xffff;
882 Bug (
"Zero passed to d2b");
902 #ifndef Sudden_Underflow
907 *e = (de - Bias - (P - 1) << 2) + k;
908 *bits = 4 * P + 8 - k - hi0bits (word0 (d) & Frac_mask);
910 *e = de - Bias - (P - 1) + k;
913 #ifndef Sudden_Underflow
917 *e = de - Bias - (P - 1) + 1 + k;
919 *bits = 32 * i - hi0bits (x[i - 1]);
921 *bits = (i + 2) * 16 - hi0bits (x[i]);
931 _DEFUN (ratio, (a, b), _Bigint * a _AND _Bigint * b)
934 union double_union da, db;
940 k = ka - kb + 32 * (a->_wds - b->_wds);
942 k = ka - kb + 16 * (a->_wds - b->_wds);
947 word0 (da) += (k >> 2) * Exp_msk1;
954 word0 (db) += (k >> 2) * Exp_msk1;
960 word0 (da) += k * Exp_msk1;
964 word0 (db) += k * Exp_msk1;
974 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9,
975 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19,
976 1e20, 1e21, 1e22, 1e23, 1e24
980 #if !defined(_DOUBLE_IS_32BITS) && !defined(__v800)
981 _CONST
double bigtens[] =
982 {1e16, 1e32, 1e64, 1e128, 1e256};
984 _CONST
double tinytens[] =
985 {1e-16, 1e-32, 1e-64, 1e-128, 1e-256};
987 _CONST
double bigtens[] =
990 _CONST
double tinytens[] =
996 _DEFUN (_mprec_log10, (dig),
1032 _DEFUN (small_multadd, (ptr, b, m, a, tab),
1033 struct _reent *ptr _AND
1053 y = (xi & 0xffff) * m + a;
1054 z = (xi >> 16) * m + (y >> 16);
1055 a = (int) (z >> 16);
1056 *x++ = (z << 16) + (y & 0xffff);
1059 a = (int) (y >> 16);
1066 if (wds >= b->_maxwds)
1070 b1->_maxwds = (1 <<(b->_k+1));
1071 b1->_sign=b1->_wds=0;
1083 _DEFUN (small_s2b, (ptr, s, nd0, nd, y9,tab),
1084 struct _reent * ptr _AND
1097 for (k = 0, y = 1; x > y; y <<= 1, k++);
1109 b->_mawxds = 1 <<(k+1);
1111 b->_x[0] = y9 & 0xffff;
1112 b->_wds = (b->_x[1] = y9 >> 16) ? 2 : 1;
1126 b = small_multadd (ptr, b, 10, *s++ -
'0',tab);
1131 _DEFUN (small_hi0bits,
1132 (x),
register __ULong x)
1136 if (!(x & 0xffff0000))
1141 if (!(x & 0xff000000))
1146 if (!(x & 0xf0000000))
1151 if (!(x & 0xc0000000))
1156 if (!(x & 0x80000000))
1159 if (!(x & 0x40000000))
1166 _DEFUN (small_lo0bits, (y), __ULong *y)
1169 register __ULong x = *y;
1217 _DEFUN (small_i2b, (ptr, i,tab),
struct _reent * ptr _AND
int i _AND _Bigint tab[])
1223 b->_maxwds = 1 << 1;
1233 _DEFUN (small_mult, (ptr, a, b,tab),
struct _reent * ptr _AND _Bigint * a _AND _Bigint * b _AND _Bigint tab[])
1237 __ULong carry, y, z;
1238 __ULong *x, *xa, *xae, *xb, *xbe, *xc, *xc0;
1243 if (a->_wds < b->_wds)
1253 if (wc > a->_maxwds)
1257 c->_maxwds = 1 << k;
1258 c->_sign= c->_wds =0;
1260 for (x = c->_x, xa = x + wc; x < xa; x++)
1268 for (; xb < xbe; xb++, xc0++)
1270 if ((y = *xb & 0xffff) != 0)
1277 z = (*x & 0xffff) * y + (*xc & 0xffff) + carry;
1279 z2 = (*x++ >> 16) * y + (*xc >> 16) + carry;
1281 Storeinc (xc, z2, z);
1286 if ((y = *xb >> 16) != 0)
1294 z = (*x & 0xffff) * y + (*xc >> 16) + carry;
1296 Storeinc (xc, z, z2);
1297 z2 = (*x++ >> 16) * y + (*xc & 0xffff) + carry;
1305 for (; xb < xbe; xc0++)
1314 z = *x++ * y + *xc + carry;
1323 for (xc0 = c->_x, xc = xc0 + wc; wc > 0 && !*--xc; --wc);
1331 _DEFUN (small_pow5mult,
1332 (ptr, b, k, tab),
struct _reent * ptr _AND _Bigint * b _AND
int k _AND _Bigint tab[])
1334 _Bigint *b1, *p5, *p51;
1335 _Bigint tab_p5[50], tab_p5mult[50];
1336 _Bigint tab_b2[40], tab_b1[40];
1339 static _CONST
int p05[3] = {5, 25, 125};
1340 if ((i = k & 3) != 0){
1345 b = small_multadd (ptr, b, p05[i - 1], 0,&tab[0]);
1349 b = small_multadd (ptr, b, p05[i - 1], 0,&tab_b1[0]);
1357 p5 =small_i2b (ptr, 625,tab_p5);
1368 if (!(k_sauv >>= 1) ){
1370 b1 = small_mult (ptr, b, p5,&tab[0]);
1375 if (b == &tab_b1[0]){
1376 b1 = small_mult (ptr, b, p5,&tab_b2[0]);
1379 b1 = small_mult (ptr, b, p5,&tab_b1[0]);
1386 if (!(p51 = p5->_next))
1388 if ( p5 == &tab_p5[0] ){
1389 p51 = p5->_next = small_mult (ptr, p5, p5,&tab_p5mult[0]);
1392 p51 = p5->_next = small_mult (ptr, p5, p5,&tab_p5[0]);
1403 _DEFUN (small_lshift, (ptr, b, k,tab),
struct _reent * ptr _AND _Bigint * b _AND
int k _AND _Bigint tab[] )
1407 __ULong *x, *x1, *xe, z;
1415 n1 = n + b->_wds + 1;
1416 for (i = b->_maxwds; n1 > i; i <<= 1)
1421 b1->_maxwds = 1 << k1;
1422 b1->_sign= b1->_wds =0;
1424 for (i = 0; i < n; i++)
1435 *x1++ = *x << k | z;
1449 *x1++ = *x << k & 0xffff | z;
1467 _DEFUN (small_cmp, (a, b), _Bigint * a _AND _Bigint * b)
1469 __ULong *xa, *xa0, *xb, *xb0;
1475 if (i > 1 && !a->_x[i - 1])
1476 Bug (
"cmp called with a->_x[a->_wds-1] == 0");
1477 if (j > 1 && !b->_x[j - 1])
1478 Bug (
"cmp called with b->_x[b->_wds-1] == 0");
1489 return *xa < *xb ? -1 : 1;
1499 _DEFUN (small_diff, (ptr, a, b,tab),
struct _reent * ptr _AND
1500 _Bigint * a _AND _Bigint * b _AND _Bigint tab[])
1505 __ULong *xa, *xae, *xb, *xbe, *xc;
1510 i = small_cmp (a, b);
1515 c->_maxwds = (1 << 0) ;
1532 c->_maxwds = (1 << (a->_k)) ;
1546 y = (*xa & 0xffff) - (*xb & 0xffff) + borrow;
1548 Sign_Extend (borrow, y);
1549 z = (*xa++ >> 16) - (*xb++ >> 16) + borrow;
1551 Sign_Extend (borrow, z);
1552 Storeinc (xc, z, y);
1557 y = (*xa & 0xffff) + borrow;
1559 Sign_Extend (borrow, y);
1560 z = (*xa++ >> 16) + borrow;
1562 Sign_Extend (borrow, z);
1563 Storeinc (xc, z, y);
1568 y = *xa++ - *xb++ + borrow;
1570 Sign_Extend (borrow, y);
1578 Sign_Extend (borrow, y);
1589 _DEFUN (small_ulp, (_x),
double _x)
1591 union double_union x, a;
1596 L = (word0 (x) & Exp_mask) - (P - 1) * Exp_msk1;
1597 #ifndef Sudden_Underflow
1605 #ifndef _DOUBLE_IS_32BITS
1609 #ifndef Sudden_Underflow
1613 L = -L >> Exp_shift;
1616 word0 (a) = 0x80000 >> L;
1617 #ifndef _DOUBLE_IS_32BITS
1625 #ifndef _DOUBLE_IS_32BITS
1626 word1 (a) = L >= 31 ? 1 : 1 << (31 - L);
1635 _DEFUN (small_b2d, (a, e),
1636 _Bigint * a _AND
int *e)
1638 __ULong *xa, *xa0, w, y, z;
1640 union double_union d;
1653 Bug (
"zero y in b2d");
1655 k = small_hi0bits (y);
1660 d0 = Exp_1 | y >> (Ebits - k);
1661 w = xa > xa0 ? *--xa : 0;
1662 #ifndef _DOUBLE_IS_32BITS
1663 d1 = y << ((32 - Ebits) + k) | w >> (Ebits - k);
1667 z = xa > xa0 ? *--xa : 0;
1670 d0 = Exp_1 | y << k | z >> (32 - k);
1671 y = xa > xa0 ? *--xa : 0;
1672 #ifndef _DOUBLE_IS_32BITS
1673 d1 = z << k | y >> (32 - k);
1679 #ifndef _DOUBLE_IS_32BITS
1686 z = xa > xa0 ? *--xa : 0;
1687 d0 = Exp_1 | y << k - Ebits | z >> Ebits + 16 - k;
1688 w = xa > xa0 ? *--xa : 0;
1689 y = xa > xa0 ? *--xa : 0;
1690 d1 = z << k + 16 - Ebits | w << k - Ebits | y >> 16 + Ebits - k;
1693 z = xa > xa0 ? *--xa : 0;
1694 w = xa > xa0 ? *--xa : 0;
1696 d0 = Exp_1 | y << k + 16 | z << k | w >> 16 - k;
1697 y = xa > xa0 ? *--xa : 0;
1698 d1 = w << k + 16 | y << k;
1702 word0 (d) = d0 >> 16 | d0 << 16;
1703 word1 (d) = d1 >> 16 | d1 << 16;
1714 (ptr, _d, e, bits,tab),
1715 struct _reent * ptr _AND
1723 union double_union d;
1733 d0 = word0 (d) >> 16 | word0 (d) << 16;
1734 d1 = word1 (d) >> 16 | word1 (d) << 16;
1744 b->_maxwds = (1 << 1) ;
1745 b->_sign = b->_wds = 0 ;
1750 b->_maxwds = (1 << 2) ;
1751 b->_sign = b->_wds = 0 ;
1759 #ifdef Sudden_Underflow
1760 de = (int) (d0 >> Exp_shift);
1765 if ((de = (
int) (d0 >> Exp_shift)) != 0)
1769 #ifndef _DOUBLE_IS_32BITS
1773 k = small_lo0bits (&y);
1776 x[0] = y | z << (32 - k);
1781 i = b->_wds = (x[1] = z) ? 2 : 1;
1788 Bug (
"Zero passed to d2b");
1790 k = small_lo0bits (&z);
1793 #ifndef _DOUBLE_IS_32BITS
1801 k = small_lo0bits (&y);
1805 x[0] = y | z << 32 - k & 0xffff;
1806 x[1] = z >> k - 16 & 0xffff;
1813 x[1] = y >> 16 | z << 16 - k & 0xffff;
1814 x[2] = z >> k & 0xffff;
1831 Bug (
"Zero passed to d2b");
1851 #ifndef Sudden_Underflow
1856 *e = (de - Bias - (P - 1) << 2) + k;
1857 *bits = 4 * P + 8 - k - small_hi0bits (word0 (d) & Frac_mask);
1859 *e = de - Bias - (P - 1) + k;
1862 #ifndef Sudden_Underflow
1866 *e = de - Bias - (P - 1) + 1 + k;
1868 *bits = 32 * i - small_hi0bits (x[i - 1]);
1870 *bits = (i + 2) * 16 - small_hi0bits (x[i]);
1880 _DEFUN (small_ratio, (a, b), _Bigint * a _AND _Bigint * b)
1883 union double_union da, db;
1886 da.d = small_b2d (a, &ka);
1887 db.d = small_b2d (b, &kb);
1889 k = ka - kb + 32 * (a->_wds - b->_wds);
1891 k = ka - kb + 16 * (a->_wds - b->_wds);
1896 word0 (da) += (k >> 2) * Exp_msk1;
1903 word0 (db) += (k >> 2) * Exp_msk1;
1909 word0 (da) += k * Exp_msk1;
1913 word0 (db) += k * Exp_msk1;
1923 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9,
1924 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19,
1925 1e20, 1e21, 1e22, 1e23, 1e24
1929 #if !defined(_DOUBLE_IS_32BITS) && !defined(__v800)
1930 _CONST
double small_bigtens[] =
1931 {1e16, 1e32, 1e64, 1e128, 1e256};
1933 _CONST
double small_tinytens[] =
1934 {1e-16, 1e-32, 1e-64, 1e-128, 1e-256};
1936 _CONST
double small_bigtens[] =
1939 _CONST
double small_tinytens[] =
1947 _DEFUN (small__mprec_log10, (dig),
1952 return small_tens[dig];
1961 #endif // SMALL_PRINTF