32 #define small_lo0bits lo0bits
33 #define small_hi0bits hi0bits
39 #define small_ratio ratio
41 #define small_tens tens
42 #define small_bigtens bigtens
43 #define small_tinytens tinytens
59 #include "small_mprec.h"
64 _Bigint * b _AND _Bigint * S)
69 __ULong *bx, *bxe, *sx, *sxe;
78 Bug (
"oversize b in quorem");
86 q = *bxe / (*sxe + 1);
89 Bug (
"oversized quotient in quorem");
99 ys = (si & 0xffff) * q + carry;
100 zs = (si >> 16) * q + (ys >> 16);
102 y = (*bx & 0xffff) - (ys & 0xffff) + borrow;
104 Sign_Extend (borrow, y);
105 z = (*bx >> 16) - (zs & 0xffff) + borrow;
107 Sign_Extend (borrow, z);
110 ys = *sx++ * q + carry;
112 y = *bx - (ys & 0xffff) + borrow;
114 Sign_Extend (borrow, y);
122 while (--bxe > bx && !*bxe)
128 if (small_cmp (b, S) >= 0)
139 ys = (si & 0xffff) + carry;
140 zs = (si >> 16) + (ys >> 16);
142 y = (*bx & 0xffff) - (ys & 0xffff) + borrow;
144 Sign_Extend (borrow, y);
145 z = (*bx >> 16) - (zs & 0xffff) + borrow;
147 Sign_Extend (borrow, z);
152 y = *bx - (ys & 0xffff) + borrow;
154 Sign_Extend (borrow, y);
163 while (--bxe > bx && !*bxe)
212 #ifndef _SMALL_PRINTF
213 #define _SMALL_PRINTF
221 (ptr, _d, mode, ndigits, decpt, sign, rve),
222 struct _reent *ptr _AND
264 int bbits, b2, b5, be, dig, i, ieps, ilim, ilim0, ilim1, j, j1, k, k0,
265 k_check, leftright, m2, m5, s2, s5, spec_case, try_quick;
266 union double_union d, d2, eps;
268 #ifndef Sudden_Underflow
272 _Bigint *b, *b1, *delta, *mlo =
NULL, *mhi, *S;
274 #ifndef _SMALL_PRINTF
277 #else //Declarations for SMALL_PRINTF
285 #define BUF_LSHIFT_SIZE 40// Size of each buffer for variables of _Bigint type
287 #define S0_SIZE 32 // Size of the buffer result that will be provided by _dtoa_r
299 _Bigint tab_b[BUF_LSHIFT_SIZE],tab_b1[BUF_SIZE],tab_delta[BUF_SIZE],tab_mlo[BUF_SIZE],tab_mhi[BUF_LSHIFT_SIZE],tab_S[BUF_LSHIFT_SIZE];
300 _Bigint tab_blshift[BUF_LSHIFT_SIZE],tab_Slshift[BUF_LSHIFT_SIZE],tab_mhilshift[BUF_LSHIFT_SIZE],tab_mlolshift[BUF_LSHIFT_SIZE];
301 char tab_s0[S0_SIZE];
303 #endif //Declarations for SMALL_PRINTF
306 #ifndef _SMALL_PRINTF
307 _REENT_CHECK_MP(ptr);
308 if (_REENT_MP_RESULT(ptr))
310 _REENT_MP_RESULT(ptr)->_k = _REENT_MP_RESULT_K(ptr);
311 _REENT_MP_RESULT(ptr)->_maxwds = 1 << _REENT_MP_RESULT_K(ptr);
312 Bfree (ptr, _REENT_MP_RESULT(ptr));
313 _REENT_MP_RESULT(ptr) = 0;
316 if (word0 (d) & Sign_bit)
320 word0 (d) &= ~Sign_bit;
325 #if defined(IEEE_Arith) + defined(VAX)
327 if ((word0 (d) & Exp_mask) == Exp_mask)
329 if (word0 (d) == 0x8000)
336 !word1 (d) && !(word0 (d) & 0xfffff) ?
"Infinity" :
363 b = small_d2b (ptr, d.d, &be, &bbits,&tab_b[0]);
365 b = small_d2b (ptr, d.d, &be, &bbits);
368 #ifdef Sudden_Underflow
369 i = (int) (word0 (d) >> Exp_shift1 & (Exp_mask >> Exp_shift1));
371 if ((i = (
int) (word0 (d) >> Exp_shift1 & (Exp_mask >> Exp_shift1))) != 0)
375 word0 (d2) &= Frac_mask1;
376 word0 (d2) |= Exp_11;
378 if (j = 11 - hi0bits (word0 (d2) & Frac_mask))
409 #ifndef Sudden_Underflow
416 i = bbits + be + (Bias + (P - 1) - 1);
417 x = (i > 32) ? (word0 (d) << (64 - i)) | (word1 (d) >> (i - 32))
418 : (word1 (d) << (32 - i));
420 word0 (d2) -= 31 * Exp_msk1;
421 i -= (Bias + (P - 1) - 1) + 1;
425 ds = (d2.d - 1.5) * 0.289529654602168 + 0.1760912590558 + i * 0.301029995663981;
427 if (ds < 0. && ds != k)
430 if (k >= 0 && k <= Ten_pmax)
432 if (d.d < small_tens[k])
459 if (mode < 0 || mode > 9)
482 ilim = ilim1 = i = ndigits;
494 j =
sizeof (__ULong);
495 #ifndef _SMALL_PRINTF
496 for (_REENT_MP_RESULT_K(ptr) = 0;
sizeof (_Bigint) -
sizeof (__ULong) + j <= i;
498 _REENT_MP_RESULT_K(ptr)++;
499 _REENT_MP_RESULT(ptr) = Balloc (ptr, _REENT_MP_RESULT_K(ptr));
500 s = s0 = (
char *) _REENT_MP_RESULT(ptr);
505 if (ilim >= 0 && ilim <= Quick_max && try_quick)
516 ds = small_tens[k & 0xf];
522 d.d /= small_bigtens[n_bigtens - 1];
525 for (; j; j >>= 1, i++)
529 ds *= small_bigtens[i];
533 else if ((j1 = -k) != 0)
535 d.d *= small_tens[j1 & 0xf];
536 for (j = j1 >> 4; j; j >>= 1, i++)
540 d.d *= small_bigtens[i];
543 if (k_check && d.d < 1. && ilim > 0)
552 eps.d = ieps * d.d + 7.;
553 word0 (eps) -= (P - 1) * Exp_msk1;
570 eps.d = 0.5 / small_tens[ilim - 1] - eps.d;
576 *s++ =
'0' + (int) L;
580 if (1. - d.d < eps.d)
592 eps.d *= small_tens[ilim - 1];
593 for (i = 1;; i++, d.d *= 10.)
597 *s++ =
'0' + (int) L;
600 if (d.d > 0.5 + eps.d)
602 else if (d.d < 0.5 - eps.d)
623 if (be >= 0 && k <= Int_max)
627 if (ndigits < 0 && ilim <= 0)
630 if (ilim < 0 || d.d <= 5 * ds)
638 #ifdef Check_FLT_ROUNDS
647 *s++ =
'0' + (int) L;
652 if ((d.d > ds) || ((d.d == ds) && (L & 1)))
684 #ifndef Sudden_Underflow
685 denorm ? be + (Bias + (P - 1) - 1 + 1) :
688 1 + 4 * P - 3 - bbits + ((bbits + be - 1) & 3);
714 mhi = small_i2b (ptr, 1,&tab_mhi[0]);
719 if (m2 > 0 && s2 > 0)
721 i = m2 < s2 ? m2 : s2;
733 if (mhi == &tab_mhi[0]){
734 mhi = small_pow5mult (ptr, mhi, m5,&tab_mhilshift[0]);
737 mhi = small_pow5mult (ptr, mhi, m5,&tab_mhi[0]);
740 b1 = small_mult (ptr, mhi, b,&tab_b1[0]);
742 mhi = pow5mult (ptr, mhi, m5);
743 b1 = mult (ptr, mhi, b);
747 if ((j = b5 - m5) != 0)
750 b = small_pow5mult (ptr, b, b5,&tab_blshift[0]);
753 b = small_pow5mult (ptr, b, b5,&tab_b[0]);
756 b = pow5mult (ptr, b, j);
765 b = small_pow5mult (ptr, b, b5,&tab_blshift[0]);
768 b = small_pow5mult (ptr, b, b5,&tab_b[0]);
772 b = pow5mult (ptr, b, b5);
777 S = small_i2b (ptr, 1,&tab_S[0]);
779 S = small_i2b (ptr, 1);
784 S = small_pow5mult (ptr, S, s5,&tab_Slshift[0]);
787 S = small_pow5mult (ptr, S, s5,&tab_S[0]);
791 S = pow5mult (ptr, S, s5);
799 if (!word1 (d) && !(word0 (d) & Bndry_mask)
800 #ifndef Sudden_Underflow
801 && word0 (d) & Exp_mask
821 if ((i = ((s5 ? 32 -small_hi0bits (S->_x[S->_wds - 1]) : 1) + s2) & 0x1f) != 0)
824 if ((i = ((s5 ? 32 - small_hi0bits (S->_x[S->_wds - 1]) : 1) + s2) & 0xf) != 0)
844 b = small_lshift (ptr, b, b2,&tab_blshift[0]);
847 b = small_lshift (ptr, b, b2,&tab_b[0]);
850 b = lshift (ptr, b, b2);
855 S = small_lshift (ptr, S, s2,&tab_Slshift[0]);
858 S = small_lshift (ptr, S, s2,&tab_S[0]);
861 S = lshift (ptr, S, s2);
865 if (small_cmp (b, S) < 0)
869 if (b == &tab_b[0] ){
871 b = small_multadd (ptr, b, 10, 0,&tab_blshift[0]);
874 b = small_multadd (ptr, b, 10, 0,&tab_b[0]);
877 b = multadd (ptr, b, 10, 0);
883 if (mhi == &tab_mhi[0] ){
885 mhi = small_multadd (ptr, mhi, 10, 0,&tab_mhilshift[0]);
888 mhi = small_multadd (ptr, mhi, 10, 0,&tab_mhi[0]);
891 mhi = multadd (ptr, mhi, 10, 0);
896 if (ilim <= 0 && mode > 2)
900 if ( S == &tab_S[0] ){
907 if (ilim < 0 || small_cmp (b, S = small_multadd (ptr, S, 5, 0,&tab[0])) <= 0)
910 if (ilim < 0 || small_cmp (b, S = multadd (ptr, S, 5, 0)) <= 0)
927 if (mhi == &tab_mhi[0]){
928 mhi = small_lshift (ptr, mhi, m2,&tab_mhilshift[0]);
931 mhi = small_lshift (ptr, mhi, m2,&tab_mhi[0]);
935 mhi = lshift (ptr, mhi, m2);
944 #ifndef _SMALL_PRINTF
946 mhi = Balloc (ptr, mhi->_k);
952 mhi->_maxwds = (1<<sauv_k);
953 mhi->_sign = mhi->_wds =0 ;
958 if( mhi == &tab_mhi[0]){
959 mhi = small_lshift (ptr, mhi, Log2P,&tab_mhilshift[0]);
962 mhi = small_lshift (ptr, mhi, Log2P,&tab_mhi[0]);
965 mhi = lshift (ptr, mhi, Log2P);
971 dig = quorem (b, S) +
'0';
975 j = small_cmp (b, mlo);
977 delta = small_diff (ptr, S, mhi,&tab_delta[0]);
979 delta = diff (ptr, S, mhi);
981 j1 = delta->_sign ? 1 : small_cmp (b, delta);
982 #ifndef _SMALL_PRINTF
986 if (j1 == 0 && !mode && !(word1 (d) & 1))
998 if ((j < 0) || ((j == 0) && !mode
1007 #ifdef _SMALL_PRINTF
1008 if (b == &tab_b[0]){
1009 b = small_lshift (ptr, b, 1,&tab_blshift[0]);
1012 b = small_lshift (ptr, b, 1,&tab_b[0]);
1015 b = lshift (ptr, b, 1);
1018 j1 = small_cmp (b, S);
1019 if (((j1 > 0) || ((j1 == 0) && (dig & 1)))
1048 #ifdef _SMALL_PRINTF
1050 b = small_multadd (ptr, b, 10, 0,&tab_blshift[0]);
1053 b = small_multadd (ptr, b, 10, 0,&tab_b[0]);
1057 b = multadd (ptr, b, 10, 0);
1062 #ifdef _SMALL_PRINTF
1063 if ( mhi = &tab_mhi[0] ) {
1064 mlo = mhi = small_multadd (ptr, mhi, 10, 0,&tab_mhilshift[0]);
1067 mlo = mhi = small_multadd (ptr, mhi, 10, 0,&tab_mhi[0]);
1071 mlo = mhi = multadd (ptr, mhi, 10, 0);
1076 #ifdef _SMALL_PRINTF
1077 if ( mlo = &tab_mhi[0] ) {
1078 mlo = small_multadd (ptr, mlo, 10, 0,&tab_mlolshift[0]);
1081 mlo = small_multadd (ptr, mlo, 10, 0,&tab_mlo[0]);
1083 if ( mhi = &tab_mhi[0] ) {
1084 mhi = small_multadd (ptr, mhi, 10, 0,&tab_mhilshift[0]);
1087 mhi = small_multadd (ptr, mhi, 10, 0,&tab_mhi[0]);
1090 mlo = multadd (ptr, mlo, 10, 0);
1091 mhi = multadd (ptr, mhi, 10, 0);
1100 *s++ = dig =quorem (b, S) +
'0';
1105 #ifdef _SMALL_PRINTF
1106 if ( b == &tab_b[0] ) {
1107 b = small_multadd (ptr, b, 10, 0,&tab_blshift[0]);
1110 b = small_multadd (ptr, b, 10, 0,&tab_b[0]);
1113 b = multadd (ptr, b, 10, 0);
1118 #ifdef _SMALL_PRINTF
1119 if (b == &tab_b[0]) {
1120 b = small_lshift (ptr, b, 1,&tab_blshift[0]);
1123 b = small_lshift (ptr, b, 1,&tab_b[0]);
1126 b = lshift (ptr, b, 1);
1129 j = small_cmp (b, S);
1130 if ((j > 0) || ((j == 0) && (dig & 1)))
1146 while (*--s ==
'0');
1153 #ifndef _SMALL_PRINTF
1158 if (mlo && mlo != mhi)
1164 #ifndef _SMALL_PRINTF