From 019a902fed05d173edcf3f0ae116f774334a25dc Mon Sep 17 00:00:00 2001 From: Tomasz Sowa Date: Sun, 14 Jun 2009 19:55:15 +0000 Subject: [PATCH] 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 --- ttmath/ttmath.h | 28 ++++++++++++++++++---------- ttmath/ttmathbig.h | 13 ++++--------- 2 files changed, 22 insertions(+), 19 deletions(-) diff --git a/ttmath/ttmath.h b/ttmath/ttmath.h index b807564..3883108 100644 --- a/ttmath/ttmath.h +++ b/ttmath/ttmath.h @@ -1902,13 +1902,16 @@ namespace ttmath while( !carry && multiplerWasStopSignal() ) + 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 ) diff --git a/ttmath/ttmathbig.h b/ttmath/ttmathbig.h index 422d563..2b5057a 100644 --- a/ttmath/ttmathbig.h +++ b/ttmath/ttmathbig.h @@ -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;