added: UInt::BitAnd(), UInt::BitOr(), UInt::BitXor(), UInt::BitNot(),

Big::BitAnd(), Big::BitOr(), Big::BitXor()
added: to the parser: bitand(), bitor(), bitxor()
       /band(), bor(), bxor()/


git-svn-id: svn://ttmath.org/publicrep/ttmath/trunk@36 e52654a7-88a9-db11-a3e9-0013d4bc506e
This commit is contained in:
Tomasz Sowa 2007-04-13 18:14:11 +00:00
parent 062881900a
commit 2116418f08
4 changed files with 250 additions and 4 deletions

4
TODO
View File

@ -1,7 +1,5 @@
TODO TTMath Library
===================
* Add bitwise operators (or functions) and, or, xor
* Add functions for generating random values
* Add something like NaN to the Big<> type

View File

@ -616,6 +616,145 @@ public:
}
/*!
bitwise AND
this and ss2 must be >= 0
return values:
0 - ok
1 - carry
2 - this or ss2 was negative
*/
uint BitAnd(Big<exp, man> ss2)
{
if( IsSign() || ss2.IsSign() )
return 2;
Int<exp> exp_offset( exponent );
Int<exp> mantissa_size_in_bits( man * TTMATH_BITS_PER_UINT );
uint c = 0;
exp_offset.Sub( ss2.exponent );
exp_offset.Abs();
// abs(this) will be >= abs(ss2)
if( SmallerWithoutSignThan(ss2) )
{
Big<exp, man> temp(ss2);
ss2 = *this;
*this = temp;
}
if( exp_offset >= mantissa_size_in_bits )
{
// the second value is too short
SetZero();
return 0;
}
// exp_offset < mantissa_size_in_bits, moving 'exp_offset' times
ss2.mantissa.Rcr( exp_offset.ToInt(), 0 );
mantissa.BitAnd(ss2.mantissa);
c += Standardizing();
return (c==0)? 0 : 1;
}
/*!
bitwise OR
this and ss2 must be >= 0
return values:
0 - ok
1 - carry
2 - this or ss2 was negative
*/
uint BitOr(Big<exp, man> ss2)
{
if( IsSign() || ss2.IsSign() )
return 2;
Int<exp> exp_offset( exponent );
Int<exp> mantissa_size_in_bits( man * TTMATH_BITS_PER_UINT );
uint c = 0;
exp_offset.Sub( ss2.exponent );
exp_offset.Abs();
// abs(this) will be >= abs(ss2)
if( SmallerWithoutSignThan(ss2) )
{
Big<exp, man> temp(ss2);
ss2 = *this;
*this = temp;
}
if( exp_offset >= mantissa_size_in_bits )
// the second value is too short
return 0;
// exp_offset < mantissa_size_in_bits, moving 'exp_offset' times
ss2.mantissa.Rcr( exp_offset.ToInt(), 0 );
mantissa.BitOr(ss2.mantissa);
c += Standardizing();
return (c==0)? 0 : 1;
}
/*!
bitwise XOR
this and ss2 must be >= 0
return values:
0 - ok
1 - carry
2 - this or ss2 was negative
*/
uint BitXor(Big<exp, man> ss2)
{
if( IsSign() || ss2.IsSign() )
return 2;
Int<exp> exp_offset( exponent );
Int<exp> mantissa_size_in_bits( man * TTMATH_BITS_PER_UINT );
uint c = 0;
exp_offset.Sub( ss2.exponent );
exp_offset.Abs();
// abs(this) will be >= abs(ss2)
if( SmallerWithoutSignThan(ss2) )
{
Big<exp, man> temp(ss2);
ss2 = *this;
*this = temp;
}
if( exp_offset >= mantissa_size_in_bits )
// the second value is too short
return 0;
// exp_offset < mantissa_size_in_bits, moving 'exp_offset' times
ss2.mantissa.Rcr( exp_offset.ToInt(), 0 );
mantissa.BitXor(ss2.mantissa);
c += Standardizing();
return (c==0)? 0 : 1;
}
/*!
Multiplication this = this * ss2 (ss2 is uint)

View File

@ -1097,6 +1097,67 @@ void ACoth(int sindex, int amount_of_args, ValueType & result)
}
void BitAnd(int sindex, int amount_of_args, ValueType & result)
{
if( amount_of_args != 2 )
Error( err_improper_amount_of_arguments );
uint err;
result = stack[sindex].value;
err = result.BitAnd(stack[sindex+2].value);
switch(err)
{
case 1:
Error( err_overflow );
break;
case 2:
Error( err_improper_argument );
break;
}
}
void BitOr(int sindex, int amount_of_args, ValueType & result)
{
if( amount_of_args != 2 )
Error( err_improper_amount_of_arguments );
uint err;
result = stack[sindex].value;
err = result.BitOr(stack[sindex+2].value);
switch(err)
{
case 1:
Error( err_overflow );
break;
case 2:
Error( err_improper_argument );
break;
}
}
void BitXor(int sindex, int amount_of_args, ValueType & result)
{
if( amount_of_args != 2 )
Error( err_improper_amount_of_arguments );
uint err;
result = stack[sindex].value;
err = result.BitXor(stack[sindex+2].value);
switch(err)
{
case 1:
Error( err_overflow );
break;
case 2:
Error( err_improper_argument );
break;
}
}
/*!
this method returns the value from a user-defined function
@ -1252,6 +1313,12 @@ void CreateFunctionsTable()
InsertFunctionToTable(std::string("atgh"), &Parser<ValueType>::ATanh);
InsertFunctionToTable(std::string("acoth"), &Parser<ValueType>::ACoth);
InsertFunctionToTable(std::string("actgh"), &Parser<ValueType>::ACoth);
InsertFunctionToTable(std::string("bitand"), &Parser<ValueType>::BitAnd);
InsertFunctionToTable(std::string("bitor"), &Parser<ValueType>::BitOr);
InsertFunctionToTable(std::string("bitxor"), &Parser<ValueType>::BitXor);
InsertFunctionToTable(std::string("band"), &Parser<ValueType>::BitAnd);
InsertFunctionToTable(std::string("bor"), &Parser<ValueType>::BitOr);
InsertFunctionToTable(std::string("bxor"), &Parser<ValueType>::BitXor);
}

View File

@ -1489,6 +1489,48 @@ public:
}
/*!
this method performs a bitwise operation AND
*/
void BitAnd(const UInt<value_size> & ss2)
{
for(uint x=0 ; x<value_size ; ++x)
table[x] &= ss2.table[x];
}
/*!
this method performs a bitwise operation OR
*/
void BitOr(const UInt<value_size> & ss2)
{
for(uint x=0 ; x<value_size ; ++x)
table[x] |= ss2.table[x];
}
/*!
this method performs a bitwise operation XOR
*/
void BitXor(const UInt<value_size> & ss2)
{
for(uint x=0 ; x<value_size ; ++x)
table[x] ^= ss2.table[x];
}
/*!
this method performs a bitwise operation NOT
*/
void BitNot()
{
for(uint x=0 ; x<value_size ; ++x)
table[x] = ~table[x];
}
/*!
*
* Multiplication
@ -1499,7 +1541,7 @@ public:
public:
#ifdef TTMATH_PLATFORM32