fixed: powering algorithm in:

UInt::Pow(UInt<value_size> pow)
       Big::Pow(UInt<pow_size> pow)
       Big::PowUInt(Big<exp, man> pow)
       when 'pow' was sufficient large the algorithm returned carry
       but the result could have been calculated correctly



git-svn-id: svn://ttmath.org/publicrep/ttmath/trunk@213 e52654a7-88a9-db11-a3e9-0013d4bc506e
This commit is contained in:
2009-10-16 16:56:49 +00:00
parent 462ff7cc65
commit af4fbf3098
4 changed files with 426 additions and 419 deletions

View File

@@ -1258,15 +1258,18 @@ public:
result.SetOne();
uint c = 0;
while( !c && !pow.IsZero() )
while( !c )
{
if( pow.table[0] & 1 )
c += result.Mul(start);
pow.Rcr(1);
if( pow.IsZero() )
break;
start_temp = start;
c += start.Mul(start_temp);
pow.Rcr(1);
}
*this = result;
@@ -1374,15 +1377,18 @@ public:
one.SetOne();
result = one;
while( !c && pow >= one )
while( !c )
{
if( pow.Mod2() )
c += result.Mul(start);
c += pow.exponent.Sub( e_one );
if( pow < one )
break;
start_temp = start;
c += start.Mul(start_temp);
c += pow.exponent.Sub( e_one );
}
*this = result;

View File

@@ -514,7 +514,7 @@ public:
return Pow2(pow);
if( UInt<value_size>::IsZero() )
// if 'p' is negative then
// if 'pow' is negative then
// 'this' must be different from zero
return 2;

View File

@@ -2228,32 +2228,27 @@ public:
UInt<value_size> start(*this), start_temp;
UInt<value_size> result;
result.SetOne();
uint c = 0;
while( !pow.IsZero() )
while( !c )
{
if( pow.table[0] & 1 )
if( result.Mul(start) )
{
TTMATH_LOGC("UInt::Pow(UInt<>)", 1)
return 1;
}
c += result.Mul(start);
pow.Rcr2_one(0);
if( pow.IsZero() )
break;
start_temp = start;
// in the second Mul algorithm we can use start.Mul(start) directly (there is no TTMATH_ASSERT_REFERENCE there)
if( start.Mul(start_temp) )
{
TTMATH_LOGC("UInt::Pow(UInt<>)", 1)
return 1;
}
pow.Rcr2_one(0);
c += start.Mul(start_temp);
}
*this = result;
TTMATH_LOGC("UInt::Pow(UInt<>)", 0)
TTMATH_LOGC("UInt::Pow(UInt<>)", c)
return 0;
return (c==0)? 0 : 1;
}