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/branches/0.8.x@215 e52654a7-88a9-db11-a3e9-0013d4bc506e
This commit is contained in:
Tomasz Sowa 2009-10-16 21:38:25 +00:00
parent e046aba6d2
commit a4bb0b6f64
3 changed files with 28 additions and 22 deletions

View File

@ -1,4 +1,4 @@
Version 0.8.6 prerelease (2009.10.07): Version 0.8.6 prerelease (2009.10.16):
* fixed: UInt::SetBitInWord(uint & value, uint bit) set 1 if the bit was * fixed: UInt::SetBitInWord(uint & value, uint bit) set 1 if the bit was
equal 1 (should be set 2) equal 1 (should be set 2)
this affected only no-asm parts - when macro TTMATH_NOASM was defined this affected only no-asm parts - when macro TTMATH_NOASM was defined
@ -10,6 +10,12 @@ Version 0.8.6 prerelease (2009.10.07):
* fixed: Int::FromString(const tt_string & s, uint b = 10) * fixed: Int::FromString(const tt_string & s, uint b = 10)
didn't use 'b' (always was '10') didn't use 'b' (always was '10')
* fixed: buffer overflow in Big::ToInt(Int<int_size> & result) * fixed: buffer overflow in Big::ToInt(Int<int_size> & result)
* 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
Version 0.8.5 (2009.06.16): Version 0.8.5 (2009.06.16):

View File

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

View File

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