changed: small optimization in Big::ExpSurrounding0() and Big::LnSurrounding1()

the remainder from a division '%' was changed with a bitwise And operation '&'
         ((i % 5) == 0) was changed to: ((i & 3) == 0) - it means ((i % 4) == 0)
         now the test if performed after 4 iterations (early were after 5 iterations)
         we can do that when the divisor is a power of 2
changed: optimization in Factorial()
         we're testing WasStopSignal() only after a few iterations
         it's faster now about 4 times on GCC 4.3.3 (when stop object is provided to the factorial)


git-svn-id: svn://ttmath.org/publicrep/ttmath/trunk@161 e52654a7-88a9-db11-a3e9-0013d4bc506e
This commit is contained in:
Tomasz Sowa 2009-06-14 19:55:15 +00:00
parent 74553109a5
commit 019a902fed
2 changed files with 22 additions and 19 deletions

View File

@ -1902,13 +1902,16 @@ namespace ttmath
while( !carry && multipler<maxvalue )
{
// !! the test here we don't have to make in all iterations (performance)
if( stop && stop->WasStopSignal() )
if( stop && (multipler & 127)==0 ) // it means 'stop && (multipler % 128)==0'
{
if( err )
*err = err_interrupt;
// after each 128 iterations we make a test
if( stop->WasStopSignal() )
{
if( err )
*err = err_interrupt;
return 2;
return 2;
}
}
++multipler;
@ -1932,20 +1935,25 @@ namespace ttmath
one.SetOne();
uint carry = 0;
uint iter = 1; // only for testing the stop object
while( !carry && multipler < x )
{
// !! the test here we don't have to make in all iterations (performance)
if( stop && stop->WasStopSignal() )
if( stop && (iter & 31)==0 ) // it means 'stop && (iter % 32)==0'
{
if( err )
*err = err_interrupt;
// after each 32 iterations we make a test
if( stop->WasStopSignal() )
{
if( err )
*err = err_interrupt;
return 2;
return 2;
}
}
carry += multipler.Add(one);
carry += result.Mul(multipler);
++iter;
}
if( err )

View File

@ -1399,19 +1399,17 @@ public:
denominator.SetOne();
denominator_i.SetOne();
// every 'step_test' times we make a test
const uint step_test = 5;
uint i;
old_value = *this;
// we begin from 1 in order to not testing at the beginning
// we begin from 1 in order to not test at the beginning
#ifdef TTMATH_CONSTANTSGENERATOR
for(i=1 ; true ; ++i)
#else
for(i=1 ; i<=TTMATH_ARITHMETIC_MAX_LOOP ; ++i)
#endif
{
bool testing = ((i % step_test) == 0);
bool testing = ((i & 3) == 0); // it means '(i % 4) == 0'
next_part = numerator;
@ -1567,20 +1565,17 @@ public:
SetZero();
old_value = *this;
// every 'step_test' times we make a test
const uint step_test = 5;
uint i;
#ifdef TTMATH_CONSTANTSGENERATOR
for(i=1 ; true ; ++i)
#else
// we begin from 1 in order to not testing at the beginning
// we begin from 1 in order to not test at the beginning
for(i=1 ; i<=TTMATH_ARITHMETIC_MAX_LOOP ; ++i)
#endif
{
bool testing = ((i % step_test) == 0);
bool testing = ((i & 3) == 0); // it means '(i % 4) == 0'
next_part = x1;