- 32 bit ASM code and ASSERTS did not work as the ASM code put its result in EAX, but the ASSERT afterwards did destroy the EAX's contents, of course.

git-svn-id: svn://ttmath.org/publicrep/ttmath/branches/chk@155 e52654a7-88a9-db11-a3e9-0013d4bc506e
This commit is contained in:
Christian Kaiser 2009-05-28 14:42:19 +00:00
parent b31d34ebdd
commit be8913866a
5 changed files with 108 additions and 105 deletions

View File

@ -97,10 +97,13 @@ namespace ttmath
-2.7 = -3
*/
template<class ValueType>
ValueType Round(const ValueType & x)
ValueType Round(const ValueType & x, ErrorCode * err = 0)
{
ValueType result( x );
result.Round();
uint c = result.Round();
if( err )
*err = c ? err_overflow : err_ok;
return result;
}
@ -298,7 +301,7 @@ namespace ttmath
(you don't have to call this function)
*/
template<class ValueType>
void PrepareSin(ValueType & x, bool & change_sign)
uint PrepareSin(ValueType & x, bool & change_sign)
{
ValueType temp;
@ -314,12 +317,10 @@ namespace ttmath
// we're reducing the period 2*PI
// (for big values there'll always be zero)
temp.Set2Pi();
if( x > temp )
{
x.Div( temp );
x.RemainFraction();
x.Mul( temp );
}
if( x.Mod(temp) )
return 1;
// we're setting 'x' as being in the range of <0, 0.5PI>
@ -340,6 +341,8 @@ namespace ttmath
x.Sub( temp );
x = temp - x;
}
return 0;
}
@ -426,7 +429,7 @@ namespace ttmath
if( c )
// Sin is from <-1,1> and cannot make an overflow
// but the carry can be from the Taylor series
// (then we only breaks our calculations)
// (then we only break our calculations)
break;
if( addition )
@ -458,15 +461,15 @@ namespace ttmath
this function calculates the Sine
*/
template<class ValueType>
ValueType Sin(ValueType x)
ValueType Sin(ValueType x, ErrorCode * err = 0)
{
using namespace auxiliaryfunctions;
ValueType one;
ValueType one, result;
bool change_sign;
PrepareSin( x, change_sign );
ValueType result = Sin0pi05( x );
if( err )
*err = err_ok;
if( PrepareSin( x, change_sign ) )
{
@ -506,22 +509,24 @@ namespace ttmath
we're using the formula cos(x) = sin(x + PI/2)
*/
template<class ValueType>
ValueType Cos(ValueType x)
ValueType Cos(ValueType x, ErrorCode * err = 0)
{
ValueType pi05;
pi05.Set05Pi();
x.Add( pi05 );
uint c = x.Add( pi05 );
if( c )
{
if( err )
*err = err_overflow;
}
return ValueType(); // result is undefined (NaN is set by default)
}
return Sin(x, err);
}
/*!
this function calulates the Tangent
@ -536,7 +541,10 @@ namespace ttmath
template<class ValueType>
ValueType Tan(const ValueType & x, ErrorCode * err = 0)
{
ValueType result = Cos(x);
ValueType result = Cos(x, err);
if( err && *err != err_ok )
return result;
if( result.IsZero() )
{
@ -548,10 +556,7 @@ namespace ttmath
return result;
}
if( err )
*err = err_ok;
return Sin(x) / result;
return Sin(x, err) / result;
}
@ -578,7 +583,10 @@ namespace ttmath
template<class ValueType>
ValueType Cot(const ValueType & x, ErrorCode * err = 0)
{
ValueType result = Sin(x);
ValueType result = Sin(x, err);
if( err && *err != err_ok )
return result;
if( result.IsZero() )
{
@ -590,10 +598,7 @@ namespace ttmath
return result;
}
if( err )
*err = err_ok;
return Cos(x) / result;
return Cos(x, err) / result;
}
@ -2053,14 +2058,17 @@ namespace ttmath
e.g.
mod( 12.6 ; 3) = 0.6 because 12.6 = 3*4 + 0.6
mod(-12.6 ; 3) = -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
*/
template<class ValueType>
ValueType Mod(ValueType a, const ValueType & b)
{
a.Mod(b);
ValueType Mod(ValueType a, const ValueType & b, ErrorCode * err = 0)
{
uint c = a.Mod(b);
if( err )
*err = c ? err_overflow : err_ok;
return a;
}

View File

@ -2693,7 +2693,7 @@ public:
tchar_t decimal_point = TTMATH_COMMA_CHARACTER_1 ) const
{
static tchar_t error_overflow_msg[] = TTMATH_TEXT("overflow");
static char error_nan_msg[] = "NaN";
static tchar_t error_nan_msg[] = TTMATH_TEXT("NaN");
result.erase();
if( IsNan() )

View File

@ -976,7 +976,7 @@ public:
// table[i] = result.table[i];
// testing carry
for( ; i<value_size*2 ; ++i)
for(i = value_size ; i<value_size*2 ; ++i)
if( result.table[i] != 0 )
return 1;
@ -2877,7 +2877,7 @@ public:
ttmathuint_noasm.h
*/
#ifdef TTMATH_NOASM
#if defined(TTMATH_NOASM)
static uint AddTwoWords(uint a, uint b, uint carry, uint * result);
static uint SubTwoWords(uint a, uint b, uint carry, uint * result);

View File

@ -38,16 +38,17 @@
#ifndef headerfilettmathuint_noasm
#define headerfilettmathuint_noasm
#ifdef TTMATH_NOASM
/*!
\file ttmathuint_noasm.h
\file ttmathuint.h
\brief template class UInt<uint> with methods without any assembler code
this file is included at the end of ttmathuint.h
*/
#pragma message("TTMATH_NOASM")
namespace ttmath
{
@ -95,7 +96,7 @@ namespace ttmath
for(i=0 ; i<value_size ; ++i)
c = AddTwoWords(table[i], ss2.table[i], c, &table[i]);
TTMATH_LOG("UInt_noasm::Add")
TTMATH_LOG("UInt::Add")
return c;
}
@ -131,7 +132,7 @@ namespace ttmath
for(i=index+1 ; i<value_size && c ; ++i)
c = AddTwoWords(table[i], 0, c, &table[i]);
TTMATH_LOG("UInt_noasm::AddInt")
TTMATH_LOG("UInt::AddInt")
return c;
}
@ -184,7 +185,7 @@ namespace ttmath
for(i=index+2 ; i<value_size && c ; ++i)
c = AddTwoWords(table[i], 0, c, &table[i]);
TTMATH_LOG("UInt64::AddTwoInts")
TTMATH_LOG("UInt::AddTwoInts")
return c;
}
@ -232,7 +233,7 @@ namespace ttmath
for(i=0 ; i<value_size ; ++i)
c = SubTwoWords(table[i], ss2.table[i], c, &table[i]);
TTMATH_LOG("UInt_noasm::Sub")
TTMATH_LOG("UInt::Sub")
return c;
}
@ -270,7 +271,7 @@ namespace ttmath
for(i=index+1 ; i<value_size && c ; ++i)
c = SubTwoWords(table[i], 0, c, &table[i]);
TTMATH_LOG("UInt_noasm::SubInt")
TTMATH_LOG("UInt::SubInt")
return c;
}
@ -305,7 +306,7 @@ namespace ttmath
c = new_c;
}
TTMATH_LOG("UInt64::Rcl2_one")
TTMATH_LOG("UInt::Rcl2_one")
return c;
}
@ -344,7 +345,7 @@ namespace ttmath
c = new_c;
}
TTMATH_LOG("UInt64::Rcr2_one")
TTMATH_LOG("UInt::Rcr2_one")
return c;
}
@ -421,7 +422,7 @@ namespace ttmath
c = new_c;
}
TTMATH_LOG("UInt64::Rcr2")
TTMATH_LOG("UInt::Rcr2")
return (c & TTMATH_UINT_HIGHEST_BIT) ? 1 : 0;
}
@ -879,7 +880,3 @@ namespace ttmath
#endif //ifdef TTMATH_NOASM
#endif

View File

@ -36,14 +36,13 @@
*/
#ifndef headerfilettmathuint_x86
#define headerfilettmathuint_x86
#ifndef TTMATH_NOASM
#ifdef TTMATH_PLATFORM32
#pragma message("TTMATH_ASM")
/*!
\file ttmathuint_x86.h
@ -65,8 +64,7 @@ namespace ttmath
* basic mathematic functions
*
*/
/*!
adding ss2 to the this and adding carry if it's defined
(this = this + ss2 + c)
@ -85,7 +83,6 @@ namespace ttmath
// this algorithm doesn't require it
#ifndef __GNUC__
// this part might be compiled with for example visual c
__asm
@ -109,8 +106,8 @@ namespace ttmath
setc al
movzx eax, al
mov [c], eax
}
#endif
@ -143,11 +140,11 @@ namespace ttmath
: "=d" (c)
: "D" (c), "c" (b), "b" (p1), "S" (p2)
: "%eax", "cc", "memory" );
return c;
#endif
TTMATH_LOG("UInt32::Add")
TTMATH_LOG("UInt::Add")
return c;
}
@ -174,13 +171,13 @@ namespace ttmath
template<uint value_size>
uint UInt<value_size>::AddInt(uint value, uint index)
{
register uint b = value_size;
register uint * p1 = table;
uint b = value_size;
uint * p1 = table;
uint c;
TTMATH_ASSERT( index < value_size )
#ifndef __GNUC__
__asm
{
mov ecx, [b]
@ -204,14 +201,12 @@ namespace ttmath
end:
setc al
movzx eax, al
mov [c], eax
}
#endif
#ifdef __GNUC__
register uint c;
__asm__ __volatile__(
"push %%eax \n"
@ -238,11 +233,11 @@ namespace ttmath
: "=d" (c)
: "a" (value), "c" (b), "0" (index), "b" (p1)
: "cc", "memory" );
return c;
#endif
TTMATH_LOG("UInt32::AddInt")
TTMATH_LOG("UInt::AddInt")
return c;
}
@ -281,8 +276,9 @@ namespace ttmath
template<uint value_size>
uint UInt<value_size>::AddTwoInts(uint x2, uint x1, uint index)
{
register uint b = value_size;
register uint * p1 = table;
uint b = value_size;
uint * p1 = table;
uint c;
TTMATH_ASSERT( index < value_size - 1 )
@ -313,13 +309,11 @@ namespace ttmath
end:
setc al
movzx eax, al
mov [c], eax
}
#endif
#ifdef __GNUC__
register uint c;
__asm__ __volatile__(
"push %%ecx \n"
@ -350,11 +344,11 @@ namespace ttmath
: "=a" (c)
: "c" (b), "d" (index), "b" (p1), "S" (x1), "0" (x2)
: "cc", "memory" );
return c;
#endif
TTMATH_LOG("UInt32::AddTwoInts")
TTMATH_LOG("UInt::AddTwoInts")
return c;
}
@ -380,7 +374,6 @@ namespace ttmath
// this algorithm doesn't require it
#ifndef __GNUC__
__asm
{
mov ecx,[b]
@ -404,6 +397,7 @@ namespace ttmath
setc al
movzx eax, al
mov [c], eax
}
#endif
@ -435,10 +429,11 @@ namespace ttmath
: "D" (c), "c" (b), "b" (p1), "S" (p2)
: "%eax", "cc", "memory" );
return c;
#endif
TTMATH_LOG("UInt32::Sub")
TTMATH_LOG("UInt::Sub")
return c;
}
@ -466,8 +461,9 @@ namespace ttmath
template<uint value_size>
uint UInt<value_size>::SubInt(uint value, uint index)
{
register uint b = value_size;
register uint * p1 = table;
uint b = value_size;
uint * p1 = table;
uint c;
TTMATH_ASSERT( index < value_size )
@ -495,13 +491,12 @@ namespace ttmath
end:
setc al
movzx eax, al
mov [c], eax
}
#endif
#ifdef __GNUC__
register uint c;
__asm__ __volatile__(
"push %%eax \n"
@ -528,12 +523,11 @@ namespace ttmath
: "=d" (c)
: "a" (value), "c" (b), "0" (index), "b" (p1)
: "cc", "memory" );
return c;
#endif
TTMATH_LOG("UInt32::SubInt")
TTMATH_LOG("UInt::SubInt")
return c;
}
@ -577,6 +571,7 @@ namespace ttmath
setc al
movzx eax, al
mov [c], eax
}
#endif
@ -606,12 +601,11 @@ namespace ttmath
: "=a" (c)
: "0" (c), "c" (b), "b" (p1)
: "cc", "memory" );
return c;
#endif
TTMATH_LOG("UInt32::Rcl2_one")
TTMATH_LOG("UInt::Rcl2_one")
return c;
}
@ -652,6 +646,7 @@ namespace ttmath
setc al
movzx eax, al
mov [c], eax
}
#endif
@ -677,11 +672,11 @@ namespace ttmath
: "=a" (c)
: "0" (c), "c" (b), "b" (p1)
: "cc", "memory" );
return c;
#endif
TTMATH_LOG("UInt32::Rcr2_one")
TTMATH_LOG("UInt::Rcr2_one")
return c;
}
@ -743,6 +738,7 @@ namespace ttmath
jnz p
and eax, 1
mov dword ptr [c], eax
}
#endif
@ -790,11 +786,11 @@ namespace ttmath
: "=a" (c)
: "0" (c), "D" (b), "b" (p1), "c" (bits), [amask] "m" (mask)
: "cc", "memory" );
return c;
#endif
TTMATH_LOG("UInt32::Rcl2")
TTMATH_LOG("UInt::Rcl2")
return c;
}
@ -860,6 +856,8 @@ namespace ttmath
rol eax, 1 // bit 31 will be bit 0
and eax, 1
mov dword ptr [c], eax
}
#endif
@ -910,11 +908,11 @@ namespace ttmath
: "=a" (c)
: "0" (c), "D" (b), "b" (p1), "c" (bits), [amask] "m" (mask)
: "cc", "memory" );
return c;
#endif
TTMATH_LOG("UInt32::Rcr2")
TTMATH_LOG("UInt::Rcr2")
return c;
}
@ -926,6 +924,7 @@ namespace ttmath
template<uint value_size>
sint UInt<value_size>::FindLeadingBitInWord(uint x)
{
sint result;
#ifndef __GNUC__
__asm
@ -933,13 +932,13 @@ namespace ttmath
mov edx,-1
bsr eax,[x]
cmovz eax,edx
mov [result], eax
}
#endif
#ifdef __GNUC__
register sint result;
__asm__ __volatile__(
"bsrl %1, %0 \n"
@ -951,9 +950,9 @@ namespace ttmath
: "R" (x)
: "cc" );
return result;
#endif
return result;
}
@ -976,6 +975,7 @@ namespace ttmath
TTMATH_ASSERT( bit < TTMATH_BITS_PER_UINT )
uint v = value;
uint old_bit;
#ifndef __GNUC__
__asm
@ -987,13 +987,12 @@ namespace ttmath
setc bl
movzx ebx, bl
mov eax, ebx
mov [old_bit], ebx
}
#endif
#ifdef __GNUC__
uint old_bit;
__asm__ __volatile__(
@ -1005,11 +1004,10 @@ namespace ttmath
: "=a" (v), "=b" (old_bit)
: "0" (v), "1" (bit)
: "cc" );
return old_bit;
#endif
value = v;
return old_bit;
}
@ -1101,7 +1099,7 @@ namespace ttmath
these variables have similar meaning like those in
the multiplication algorithm MulTwoWords
*/
TTMATH_ASSERT( c != 0 )
#ifndef __GNUC__