current chk version - too many changes on both sides for now ;-(

git-svn-id: svn://ttmath.org/publicrep/ttmath/branches/chk@150 e52654a7-88a9-db11-a3e9-0013d4bc506e
This commit is contained in:
Christian Kaiser 2009-05-19 10:50:41 +00:00
commit fdc292e91a
9 changed files with 12491 additions and 12487 deletions

View File

@ -1,4 +1,4 @@
Version 0.8.5 (2009.05.11): Version 0.8.5 prerelease (2009.05.15):
* fixed: Big::Mod(x) didn't correctly return a carry * fixed: Big::Mod(x) didn't correctly return a carry
and the result was sometimes very big (even greater than x) and the result was sometimes very big (even greater than x)
* fixed: global function Mod(x) didn't set an ErrorCode object * fixed: global function Mod(x) didn't set an ErrorCode object
@ -11,6 +11,25 @@ Version 0.8.5 (2009.05.11):
the same is to Cos() function the same is to Cos() function
* changed: PrepareSin(x) is using Big::Mod() now when reducing 2PI period * changed: PrepareSin(x) is using Big::Mod() now when reducing 2PI period
should be a little accurate especially on a very big 'x' should be a little accurate especially on a very big 'x'
* changed: uint Mul(const UInt<value_size> & ss2, uint algorithm = 100)
void MulBig(const UInt<value_size> & ss2, UInt<value_size*2> & result, uint algorithm = 100)
those methods by default use MulFastest() and MulFastestBig()
* changed: changed a little Mul2Big() to cooperate with Mul3Big()
* added: uint UInt::Mul3(const UInt<value_size> & ss2)
void UInt::Mul3Big(const UInt<value_size> & ss2, UInt<value_size*2> & result)
a new multiplication algorithm: Karatsuba multiplication,
on a vector UInt<100> with all items different from zero this algorithm is faster
about 3 times than Mul2Big(), and on a vector UInt<1000> with all items different from
zero this algorithm is faster more than 5 times than Mul2Big()
(measured on 32bit platform with GCC 4.3.3 with -O3 and -DTTMATH_RELEASE)
* added: uint MulFastest(const UInt<value_size> & ss2)
void MulFastestBig(const UInt<value_size> & ss2, UInt<value_size*2> & result)
those methods are trying to select the fastest multiplication algorithm
* added: uint AddVector(const uint * ss1, const uint * ss2, uint ss1_size, uint ss2_size, uint * result)
uint SubVector(const uint * ss1, const uint * ss2, uint ss1_size, uint ss2_size, uint * result)
three forms: asm x86, asm x86_64, no-asm
those methods are used by the Karatsuba multiplication algorithm
Version 0.8.4 (2009.05.08): Version 0.8.4 (2009.05.08):
* fixed: UInt::DivInt() didn't check whether the divisor is zero * fixed: UInt::DivInt() didn't check whether the divisor is zero

View File

@ -96,13 +96,10 @@ namespace ttmath
-2.7 = -3 -2.7 = -3
*/ */
template<class ValueType> template<class ValueType>
ValueType Round(const ValueType & x, ErrorCode * err = 0) ValueType Round(const ValueType & x)
{ {
ValueType result( x ); ValueType result( x );
uint c = result.Round(); result.Round();
if( err )
*err = c ? err_overflow : err_ok;
return result; return result;
} }
@ -300,7 +297,7 @@ namespace ttmath
(you don't have to call this function) (you don't have to call this function)
*/ */
template<class ValueType> template<class ValueType>
uint PrepareSin(ValueType & x, bool & change_sign) void PrepareSin(ValueType & x, bool & change_sign)
{ {
ValueType temp; ValueType temp;
@ -316,10 +313,12 @@ namespace ttmath
// we're reducing the period 2*PI // we're reducing the period 2*PI
// (for big values there'll always be zero) // (for big values there'll always be zero)
temp.Set2Pi(); temp.Set2Pi();
if( x > temp )
if( x.Mod(temp) ) {
return 1; x.Div( temp );
x.RemainFraction();
x.Mul( temp );
}
// we're setting 'x' as being in the range of <0, 0.5PI> // we're setting 'x' as being in the range of <0, 0.5PI>
@ -340,8 +339,6 @@ namespace ttmath
x.Sub( temp ); x.Sub( temp );
x = temp - x; x = temp - x;
} }
return 0;
} }
@ -428,7 +425,7 @@ namespace ttmath
if( c ) if( c )
// Sin is from <-1,1> and cannot make an overflow // Sin is from <-1,1> and cannot make an overflow
// but the carry can be from the Taylor series // but the carry can be from the Taylor series
// (then we only break our calculations) // (then we only breaks our calculations)
break; break;
if( addition ) if( addition )
@ -460,28 +457,15 @@ namespace ttmath
this function calculates the Sine this function calculates the Sine
*/ */
template<class ValueType> template<class ValueType>
ValueType Sin(ValueType x, ErrorCode * err = 0) ValueType Sin(ValueType x)
{ {
using namespace auxiliaryfunctions; using namespace auxiliaryfunctions;
ValueType one, result; ValueType one;
bool change_sign; bool change_sign;
if( err ) PrepareSin( x, change_sign );
*err = err_ok; ValueType result = Sin0pi05( x );
if( PrepareSin( x, change_sign ) )
{
// x is too big, we cannnot reduce the 2*PI period
// prior to version 0.8.5 the result was zero
if( err )
*err = err_overflow; // maybe another error code?
return result; // result we remain as undefined
}
result = Sin0pi05( x );
one.SetOne(); one.SetOne();
@ -506,22 +490,14 @@ namespace ttmath
we're using the formula cos(x) = sin(x + PI/2) we're using the formula cos(x) = sin(x + PI/2)
*/ */
template<class ValueType> template<class ValueType>
ValueType Cos(ValueType x, ErrorCode * err = 0) ValueType Cos(ValueType x)
{ {
ValueType pi05; ValueType pi05;
pi05.Set05Pi(); pi05.Set05Pi();
uint c = x.Add( pi05 ); x.Add( pi05 );
if( c ) return Sin(x);
{
if( err )
*err = err_overflow;
return ValueType(); // result is undefined
}
return Sin(x, err);
} }
@ -538,10 +514,7 @@ namespace ttmath
template<class ValueType> template<class ValueType>
ValueType Tan(const ValueType & x, ErrorCode * err = 0) ValueType Tan(const ValueType & x, ErrorCode * err = 0)
{ {
ValueType result = Cos(x, err); ValueType result = Cos(x);
if( err && *err != err_ok )
return result;
if( result.IsZero() ) if( result.IsZero() )
{ {
@ -551,7 +524,10 @@ namespace ttmath
return result; return result;
} }
return Sin(x, err) / result; if( err )
*err = err_ok;
return Sin(x) / result;
} }
@ -578,10 +554,7 @@ namespace ttmath
template<class ValueType> template<class ValueType>
ValueType Cot(const ValueType & x, ErrorCode * err = 0) ValueType Cot(const ValueType & x, ErrorCode * err = 0)
{ {
ValueType result = Sin(x, err); ValueType result = Sin(x);
if( err && *err != err_ok )
return result;
if( result.IsZero() ) if( result.IsZero() )
{ {
@ -591,7 +564,10 @@ namespace ttmath
return result; return result;
} }
return Cos(x, err) / result; if( err )
*err = err_ok;
return Cos(x) / result;
} }
@ -2035,18 +2011,15 @@ namespace ttmath
the remainder from a division the remainder from a division
e.g. e.g.
mod( 12.6 ; 3) = 0.6 because 12.6 = 3*4 + 0.6 mod( 12.6 ; 3) = 0.6 because 12.6 = 3*4 + 0.6
mod(-12.6 ; 3) = -0.6 bacause -12.6 = 3*(-4) + (-0.6) mod(-12.6 ; 3) = -0.6
mod( 12.6 ; -3) = 0.6 mod( 12.6 ; -3) = 0.6
mod(-12.6 ; -3) = -0.6 mod(-12.6 ; -3) = -0.6
*/ */
template<class ValueType> template<class ValueType>
ValueType Mod(ValueType a, const ValueType & b, ErrorCode * err = 0) ValueType Mod(ValueType a, const ValueType & b)
{ {
uint c = a.Mod(b); a.Mod(b);
if( err )
*err = c ? err_overflow : err_ok;
return a; return a;
} }

View File

@ -954,7 +954,7 @@ public:
UInt<man*2> man1; UInt<man*2> man1;
UInt<man*2> man2; UInt<man*2> man2;
uint i,c = 0; uint i,c;
if( ss2.IsZero() ) if( ss2.IsZero() )
{ {
@ -978,9 +978,7 @@ public:
i = man1.CompensationToLeft(); i = man1.CompensationToLeft();
if( i ) c = exponent.Sub(i);
c += exponent.Sub(i);
c += exponent.Sub(ss2.exponent); c += exponent.Sub(ss2.exponent);
for(i=0 ; i<man ; ++i) for(i=0 ; i<man ; ++i)
@ -1001,8 +999,8 @@ public:
the remainder from a division the remainder from a division
e.g. e.g.
12.6 mod 3 = 0.6 because 12.6 = 3*4 + 0.6 12.6 mod 3 = 0.6 because 12.6 = 3*4 + 0.6
-12.6 mod 3 = -0.6 bacause -12.6 = 3*(-4) + (-0.6) -12.6 mod 3 = -0.6
12.6 mod -3 = 0.6 12.6 mod -3 = 0.6
-12.6 mod -3 = -0.6 -12.6 mod -3 = -0.6
@ -1015,25 +1013,18 @@ public:
uint c = 0; uint c = 0;
if( !SmallerWithoutSignThan(ss2) ) Big<exp, man> temp(*this);
{
Big<exp, man> temp(*this);
c = temp.Div(ss2); c += temp.Div(ss2);
temp.SkipFraction(); temp.SkipFraction();
c += temp.Mul(ss2); c += temp.Mul(ss2);
c += Sub(temp); c += Sub(temp);
if( !SmallerWithoutSignThan( ss2 ) )
c += 1;
}
return (c==0)? 0 : 1; return (c==0)? 0 : 1;
} }
/*! /*!
power this = this ^ pow power this = this ^ pow
(pow without a sign) (pow without a sign)
@ -1885,7 +1876,7 @@ public:
// error but I leave it at the moment as is // error but I leave it at the moment as is
TTMATH_ASSERT( sizeof(double) == 8 ) TTMATH_ASSERT( sizeof(double) == 8 )
// I am not sure what will be on a platform which has // I am not sure what will be on a plaltform which has
// a different endianness... but we use this library only // a different endianness... but we use this library only
// on x86 and amd (intel) 64 bits (as there's a lot of assembler code) // on x86 and amd (intel) 64 bits (as there's a lot of assembler code)
union union
@ -3443,7 +3434,7 @@ private:
*/ */
int FromString_ReadScientificIfExists(const tchar_t * & source) int FromString_ReadScientificIfExists(const tchar_t * & source)
{ {
uint c = 0; int c = 0;
bool scientific_read = false; bool scientific_read = false;
const tchar_t * before_scientific = source; const tchar_t * before_scientific = source;

View File

@ -708,11 +708,7 @@ void Sin(int sindex, int amount_of_args, ValueType & result)
if( amount_of_args != 1 ) if( amount_of_args != 1 )
Error( err_improper_amount_of_arguments ); Error( err_improper_amount_of_arguments );
ErrorCode err; result = ttmath::Sin( ConvertAngleToRad(stack[sindex].value) );
result = ttmath::Sin( ConvertAngleToRad(stack[sindex].value), &err );
if(err != err_ok)
Error( err );
} }
void Cos(int sindex, int amount_of_args, ValueType & result) void Cos(int sindex, int amount_of_args, ValueType & result)
@ -720,11 +716,7 @@ void Cos(int sindex, int amount_of_args, ValueType & result)
if( amount_of_args != 1 ) if( amount_of_args != 1 )
Error( err_improper_amount_of_arguments ); Error( err_improper_amount_of_arguments );
ErrorCode err; result = ttmath::Cos( ConvertAngleToRad(stack[sindex].value) );
result = ttmath::Cos( ConvertAngleToRad(stack[sindex].value), &err );
if(err != err_ok)
Error( err );
} }
void Tan(int sindex, int amount_of_args, ValueType & result) void Tan(int sindex, int amount_of_args, ValueType & result)
@ -765,10 +757,7 @@ void Round(int sindex, int amount_of_args, ValueType & result)
if( amount_of_args != 1 ) if( amount_of_args != 1 )
Error( err_improper_amount_of_arguments ); Error( err_improper_amount_of_arguments );
result = stack[sindex].value; result = ttmath::Round(stack[sindex].value);
if( result.Round() )
Error( err_overflow );
} }

View File

@ -64,7 +64,7 @@
*/ */
#define TTMATH_MAJOR_VER 0 #define TTMATH_MAJOR_VER 0
#define TTMATH_MINOR_VER 8 #define TTMATH_MINOR_VER 8
#define TTMATH_REVISION_VER 5 #define TTMATH_REVISION_VER 4
#define TTMATH_PRERELEASE_VER 1 #define TTMATH_PRERELEASE_VER 1
@ -120,6 +120,7 @@ namespace ttmath
typedef unsigned int uint; typedef unsigned int uint;
typedef signed int sint; typedef signed int sint;
/*! /*!
this type is twice bigger than uint this type is twice bigger than uint
(64bit on a 32bit platforms) (64bit on a 32bit platforms)
@ -128,11 +129,8 @@ namespace ttmath
but it is defined in C99 and in upcoming C++0x /3.9.1 (2)/ and many compilers support it but it is defined in C99 and in upcoming C++0x /3.9.1 (2)/ and many compilers support it
this type is used in UInt::MulTwoWords and UInt::DivTwoWords when macro TTMATH_NOASM is defined this type is used in UInt::MulTwoWords and UInt::DivTwoWords when macro TTMATH_NOASM is defined
but only on a 32bit platform
*/ */
#ifdef TTMATH_NOASM typedef unsigned long long int ulint;
typedef unsigned long long int ulint;
#endif
/*! /*!
the mask for the highest bit in the unsigned 32bit word (2^31) the mask for the highest bit in the unsigned 32bit word (2^31)

View File

@ -143,7 +143,10 @@ public:
*/ */
void SetZero() void SetZero()
{ {
memset(table,0,sizeof(table)); // in the future here can be 'memset'
for(uint i=0 ; i<value_size ; ++i)
table[i] = 0;
TTMATH_LOG("UInt::SetZero") TTMATH_LOG("UInt::SetZero")
} }
@ -2069,7 +2072,8 @@ public:
*/ */
void FromUInt(uint value) void FromUInt(uint value)
{ {
memset(table,0,sizeof(table)); for(uint i=1 ; i<value_size ; ++i)
table[i] = 0;
table[0] = value; table[0] = value;
@ -2901,6 +2905,7 @@ public:
private: private:
public: // !!! chwilowo public
uint Rcl2_one(uint c); uint Rcl2_one(uint c);
uint Rcr2_one(uint c); uint Rcr2_one(uint c);
uint Rcl2(uint bits, uint c); uint Rcl2(uint bits, uint c);

View File

@ -116,7 +116,7 @@ namespace ttmath
table[1] = 30 + 2; table[1] = 30 + 2;
table[2] = 5; table[2] = 5;
of course if there was a carry from table[2] it would be returned of course if there was a carry from table[3] it would be returned
*/ */
template<uint value_size> template<uint value_size>
uint UInt<value_size>::AddInt(uint value, uint index) uint UInt<value_size>::AddInt(uint value, uint index)
@ -175,7 +175,7 @@ namespace ttmath
{ {
uint i, c; uint i, c;
TTMATH_ASSERT( index < value_size - 1 ) TTMATH_ASSERT( index < value_size )
c = AddTwoWords(table[index], x1, 0, &table[index]); c = AddTwoWords(table[index], x1, 0, &table[index]);
@ -255,7 +255,7 @@ namespace ttmath
table[1] = 30 - 2; table[1] = 30 - 2;
table[2] = 5; table[2] = 5;
of course if there was a carry from table[2] it would be returned of course if there was a carry from table[3] it would be returned
*/ */
template<uint value_size> template<uint value_size>
uint UInt<value_size>::SubInt(uint value, uint index) uint UInt<value_size>::SubInt(uint value, uint index)
@ -473,8 +473,8 @@ namespace ttmath
uint mask = 1; uint mask = 1;
if( bit > 1 ) while( bit-- > 0 )
mask = mask << bit; mask = mask << 1;
uint last = value & mask; uint last = value & mask;
value = value | mask; value = value | mask;
@ -601,6 +601,7 @@ namespace ttmath
*/ */
// !! maybe returns something? a carry? or when c is zero?
/*! /*!
this method calculates 64bits word a:b / 32bits c (a higher, b lower word) this method calculates 64bits word a:b / 32bits c (a higher, b lower word)
r = a:b / c and rest - remainder r = a:b / c and rest - remainder
@ -647,6 +648,10 @@ namespace ttmath
{ {
*r = b / c; *r = b / c;
*rest = b % c; *rest = b % c;
#ifdef TTMATH_WARTOWNIK
++tester_wartownik1; // !!!!! skasowac
#endif
} }
else else
if( c_.u_.high == 0 ) if( c_.u_.high == 0 )
@ -669,6 +674,10 @@ namespace ttmath
*rest = temp2.u % c; *rest = temp2.u % c;
*r = res_.u; *r = res_.u;
#ifdef TTMATH_WARTOWNIK
++tester_wartownik2; // !!!!! skasowac
#endif
} }
else else
{ {
@ -681,13 +690,6 @@ namespace ttmath
#ifdef TTMATH_PLATFORM64 #ifdef TTMATH_PLATFORM64
/*!
this method is available only on 64bit platforms
the same algorithm like the third division algorithm in ttmathuint.h
but now with the radix=2^32
*/
template<uint value_size> template<uint value_size>
void UInt<value_size>::DivTwoWords2(uint a, uint b, uint c, uint * r, uint * rest) void UInt<value_size>::DivTwoWords2(uint a, uint b, uint c, uint * r, uint * rest)
{ {
@ -702,6 +704,7 @@ namespace ttmath
c_.u = c; c_.u = c;
// normalizing // normalizing
// a0 will actually not be used
uint d = DivTwoWordsNormalize(a_, b_, c_); uint d = DivTwoWordsNormalize(a_, b_, c_);
// loop from j=1 to j=0 // loop from j=1 to j=0
@ -745,7 +748,12 @@ namespace ttmath
a_.u = a_.u << 1; // carry bits from 'a' are simply skipped a_.u = a_.u << 1; // carry bits from 'a' are simply skipped
if( bc ) if( bc )
{
a_.u = a_.u | 1; a_.u = a_.u | 1;
#ifdef TTMATH_WARTOWNIK
++tester_wartownik3; // !!!!! skasowac
#endif
}
} }
return d; return d;
@ -794,11 +802,23 @@ namespace ttmath
if( decrease ) if( decrease )
{ {
#ifdef TTMATH_WARTOWNIK
++tester_wartownik4; // !!!!! skasowac
#endif
--qp_.u; --qp_.u;
rp_.u += v_.u_.high; rp_.u += v_.u_.high;
if( rp_.u_.high == 0 ) if( rp_.u_.high == 0 )
{
next_test = true; next_test = true;
#ifdef TTMATH_WARTOWNIK
++tester_wartownik5; // !!!!! skasowac
#endif
}
} }
} }
while( next_test ); while( next_test );
@ -829,12 +849,20 @@ namespace ttmath
temp_.u_.low = u_.u_.high; temp_.u_.low = u_.u_.high;
c = SubTwoWords(temp_.u, res_high, c, &sub_res_high_.u); c = SubTwoWords(temp_.u, res_high, c, &sub_res_high_.u);
#ifdef TTMATH_WARTOWNIK
++tester_wartownik6; // !!!!! skasowac
#endif
if( c ) if( c )
{ {
--q; --q;
c = AddTwoWords(sub_res_low_.u, v_.u, 0, &sub_res_low_.u); c = AddTwoWords(sub_res_low_.u, v_.u, 0, &sub_res_low_.u);
AddTwoWords(sub_res_high_.u, 0, c, &sub_res_high_.u); AddTwoWords(sub_res_high_.u, 0, c, &sub_res_high_.u);
#ifdef TTMATH_WARTOWNIK
++tester_wartownik7; // !!!!! skasowac
#endif
} }
u_.u_.high = sub_res_high_.u_.low; u_.u_.high = sub_res_high_.u_.low;

View File

@ -158,7 +158,7 @@ namespace ttmath
table[1] = 30 + 2; table[1] = 30 + 2;
table[2] = 5; table[2] = 5;
of course if there was a carry from table[2] it would be returned of course if there was a carry from table[3] it would be returned
*/ */
template<uint value_size> template<uint value_size>
uint UInt<value_size>::AddInt(uint value, uint index) uint UInt<value_size>::AddInt(uint value, uint index)
@ -374,7 +374,7 @@ namespace ttmath
table[1] = 30 - 2; table[1] = 30 - 2;
table[2] = 5; table[2] = 5;
of course if there was a carry from table[2] it would be returned of course if there was a carry from table[3] it would be returned
*/ */
template<uint value_size> template<uint value_size>
uint UInt<value_size>::SubInt(uint value, uint index) uint UInt<value_size>::SubInt(uint value, uint index)
@ -695,7 +695,7 @@ namespace ttmath
/* /*
this method returns the number of the highest set bit in one 64-bit word this method returns the number of the highest set bit in one 32-bit word
if the 'x' is zero this method returns '-1' if the 'x' is zero this method returns '-1'
***this method is created only on a 64bit platform*** ***this method is created only on a 64bit platform***
@ -800,17 +800,18 @@ namespace ttmath
/*! /*!
multiplication: result_high:result_low = a * b multiplication: result2:result1 = a * b
result_high - higher word of the result result2 - higher word
result_low - lower word of the result result1 - lower word of the result
this methos never returns a carry this methos never returns a carry
this method is used in the second version of the multiplication algorithms
***this method is created only on a 64bit platform*** ***this method is created only on a 64bit platform***
it is an auxiliary method for version two of the multiplication algorithm
*/ */
template<uint value_size> template<uint value_size>
void UInt<value_size>::MulTwoWords(uint a, uint b, uint * result_high, uint * result_low) void UInt<value_size>::MulTwoWords(uint a, uint b, uint * result2, uint * result1)
{ {
/* /*
we must use these temporary variables in order to inform the compilator we must use these temporary variables in order to inform the compilator
@ -843,8 +844,8 @@ namespace ttmath
#endif #endif
*result_low = result1_; *result1 = result1_;
*result_high = result2_; *result2 = result2_;
} }

View File

@ -114,9 +114,9 @@ loop1:
mov r9, 0 ; set to 0 -> cy still set! mov r9, 0 ; set to 0 -> cy still set!
dec rdx dec rdx
jnz loop1 jnz loop1
jc return_1 ; most of the times, there will be NO carry (I hope)
done: done:
jc return_1 ; most of the times, there will be NO carry (I hope)
xor rax, rax xor rax, rax
ret ret
@ -184,9 +184,9 @@ loop1:
mov r9, 1 mov r9, 1
dec rdx dec rdx
jnz loop1 jnz loop1
jc return_1 ; most of the times, there will be NO carry (I hope)
done: done:
jc return_1 ; most of the times, there will be NO carry (I hope)
xor rax, rax xor rax, rax
ret ret