Compare commits
17 Commits
Author | SHA1 | Date | |
---|---|---|---|
5584adb23d | |||
a4bb0b6f64 | |||
e046aba6d2 | |||
5ef27bdbd0 | |||
b80f73f16b | |||
c70a947c07 | |||
8972fdfdb3 | |||
019a902fed | |||
74553109a5 | |||
9e42a5a9fd | |||
1b6858616d | |||
d789ac5396 | |||
bb2583649e | |||
5e5a106605 | |||
eaa19dd46a | |||
939d0f7519 | |||
05b67e7103 |
78
CHANGELOG
78
CHANGELOG
@@ -1,3 +1,81 @@
|
||||
Version 0.8.6 (2009.10.25):
|
||||
* fixed: UInt::SetBitInWord(uint & value, uint bit) set 1 if the bit was
|
||||
equal 1 (should be set 2)
|
||||
this affected only no-asm parts - when macro TTMATH_NOASM was defined
|
||||
* fixed: UInt<value_size>::MulInt(uint ss2)
|
||||
there was a buffer overflow when value_size was equal 1
|
||||
* fixed: UInt::AddVector() and UInt::SubVector() didn't want to compile
|
||||
when macro TTMATH_NOASM was defined
|
||||
* fixed: Big::operator>> didn't correctly recognize values in scientific mode (with 'e' character)
|
||||
* fixed: Int::FromString(const tt_string & s, uint b = 10)
|
||||
didn't use 'b' (always was '10')
|
||||
* fixed: buffer overflow in Big::ToInt(Int<int_size> & result)
|
||||
* fixed: powering algorithm in:
|
||||
UInt::Pow(UInt<value_size> pow)
|
||||
Big::Pow(UInt<pow_size> pow)
|
||||
Big::PowUInt(Big<exp, man> pow)
|
||||
when 'pow' was sufficient large the algorithm returned carry
|
||||
but the result could have been calculated correctly
|
||||
|
||||
|
||||
Version 0.8.5 (2009.06.16):
|
||||
* fixed: Big::Mod(x) didn't correctly return a carry
|
||||
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 Round() didn't test a carry
|
||||
now it sets ErrorCode object
|
||||
* changed: function Sin(x) to Sin(x, ErrorCode * err=0)
|
||||
when x was very big the function returns zero
|
||||
now it sets ErrorCode object to err_overflow
|
||||
and the result has a NaN flag set
|
||||
the same is to Cos() function
|
||||
* changed: PrepareSin(x) is using Big::Mod() now when reducing 2PI period
|
||||
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
|
||||
* added: to Big<> class: support for NaN flag (Not a Number)
|
||||
bool Big::IsNan() - returns true if the NaN flag is set
|
||||
void Big::SetNan() - sets the NaN flag
|
||||
The NaN flag is set by default after creating an object:
|
||||
Big<1, 2> a; // NaN is set (it means the object has not a valid number)
|
||||
std::cout << a; // cout gives "NaN"
|
||||
a = 123; // now NaN is not set
|
||||
std::cout << a; // cout gives "123"
|
||||
The NaN is set if there was a carry during calculations
|
||||
a.Mul(very_big_value); // a will have a NaN set
|
||||
The NaN is set if an argument is NaN too
|
||||
b.SetNan();
|
||||
a.Add(b); // a will have NaN because b has NaN too
|
||||
If you try to do something on a NaN object, the result is a NaN too
|
||||
a.SetNan();
|
||||
a.Add(2); // a is still a NaN
|
||||
The NaN is set if you use incorrect arguments
|
||||
a.Ln(-10); // a will have the NaN flag
|
||||
The only way to clear the NaN flag is to assign a correct value or other correct object,
|
||||
supposing 'a' has NaN flag, to remove the flag you can either:
|
||||
a = 10;
|
||||
a.FromInt(30);
|
||||
a.SetOne();
|
||||
a.FromBig(other_object_without_nan);
|
||||
etc.
|
||||
|
||||
|
||||
Version 0.8.4 (2009.05.08):
|
||||
* fixed: UInt::DivInt() didn't check whether the divisor is zero
|
||||
there was a hardware interruption when the divisor was zero
|
||||
|
@@ -1,6 +1,6 @@
|
||||
o = main.o
|
||||
CC = g++
|
||||
CFLAGS = -s -O2 -DCONSTANTSGENERATOR
|
||||
CFLAGS = -s -O2 -DTTMATH_CONSTANTSGENERATOR
|
||||
name = gen
|
||||
|
||||
|
||||
|
@@ -91,7 +91,7 @@ void CalcE()
|
||||
ttmath::Big<1,400> e;
|
||||
ttmath::uint steps;
|
||||
|
||||
// macro CONSTANTSGENERATOR has to be defined
|
||||
// macro TTMATH_CONSTANTSGENERATOR has to be defined
|
||||
e.ExpSurrounding0(1, &steps);
|
||||
std::cout << "---------------- e ----------------" << std::endl;
|
||||
e.mantissa.PrintTable(std::cout);
|
||||
@@ -105,7 +105,7 @@ void CalcLn(int x)
|
||||
ttmath::Big<1,400> ln;
|
||||
ttmath::uint steps;
|
||||
|
||||
// macro CONSTANTSGENERATOR has to be defined
|
||||
// macro TTMATH_CONSTANTSGENERATOR has to be defined
|
||||
ln.LnSurrounding1(x, &steps);
|
||||
std::cout << "---------------- ln(" << x << ") ----------------" << std::endl;
|
||||
ln.mantissa.PrintTable(std::cout);
|
||||
|
375
ttmath/ttmath.h
375
ttmath/ttmath.h
@@ -45,6 +45,12 @@
|
||||
\brief Mathematics functions.
|
||||
*/
|
||||
|
||||
#ifdef _MSC_VER
|
||||
//warning C4127: conditional expression is constant
|
||||
#pragma warning( disable: 4127 )
|
||||
#endif
|
||||
|
||||
|
||||
#include "ttmathbig.h"
|
||||
#include "ttmathobjects.h"
|
||||
|
||||
@@ -94,10 +100,21 @@ namespace ttmath
|
||||
-2.7 = -3
|
||||
*/
|
||||
template<class ValueType>
|
||||
ValueType Round(const ValueType & x)
|
||||
ValueType Round(const ValueType & x, ErrorCode * err = 0)
|
||||
{
|
||||
if( x.IsNan() )
|
||||
{
|
||||
if( err )
|
||||
*err = err_improper_argument;
|
||||
|
||||
return x; // NaN
|
||||
}
|
||||
|
||||
ValueType result( x );
|
||||
result.Round();
|
||||
uint c = result.Round();
|
||||
|
||||
if( err )
|
||||
*err = c ? err_overflow : err_ok;
|
||||
|
||||
return result;
|
||||
}
|
||||
@@ -118,6 +135,14 @@ namespace ttmath
|
||||
template<class ValueType>
|
||||
ValueType Ceil(const ValueType & x, ErrorCode * err = 0)
|
||||
{
|
||||
if( x.IsNan() )
|
||||
{
|
||||
if( err )
|
||||
*err = err_improper_argument;
|
||||
|
||||
return x; // NaN
|
||||
}
|
||||
|
||||
ValueType result(x);
|
||||
uint c = 0;
|
||||
|
||||
@@ -157,6 +182,14 @@ namespace ttmath
|
||||
template<class ValueType>
|
||||
ValueType Floor(const ValueType & x, ErrorCode * err = 0)
|
||||
{
|
||||
if( x.IsNan() )
|
||||
{
|
||||
if( err )
|
||||
*err = err_improper_argument;
|
||||
|
||||
return x; // NaN
|
||||
}
|
||||
|
||||
ValueType result(x);
|
||||
uint c = 0;
|
||||
|
||||
@@ -197,8 +230,15 @@ namespace ttmath
|
||||
template<class ValueType>
|
||||
ValueType Ln(const ValueType & x, ErrorCode * err = 0)
|
||||
{
|
||||
ValueType result;
|
||||
if( x.IsNan() )
|
||||
{
|
||||
if( err )
|
||||
*err = err_improper_argument;
|
||||
|
||||
return x; // NaN
|
||||
}
|
||||
|
||||
ValueType result;
|
||||
uint state = result.Ln(x);
|
||||
|
||||
if( err )
|
||||
@@ -231,8 +271,15 @@ namespace ttmath
|
||||
template<class ValueType>
|
||||
ValueType Log(const ValueType & x, const ValueType & base, ErrorCode * err = 0)
|
||||
{
|
||||
ValueType result;
|
||||
if( x.IsNan() || base.IsNan() )
|
||||
{
|
||||
if( err )
|
||||
*err = err_improper_argument;
|
||||
|
||||
return ValueType(); // default NaN
|
||||
}
|
||||
|
||||
ValueType result;
|
||||
uint state = result.Log(x, base);
|
||||
|
||||
if( err )
|
||||
@@ -265,8 +312,15 @@ namespace ttmath
|
||||
template<class ValueType>
|
||||
ValueType Exp(const ValueType & x, ErrorCode * err = 0)
|
||||
{
|
||||
ValueType result;
|
||||
if( x.IsNan() )
|
||||
{
|
||||
if( err )
|
||||
*err = err_improper_argument;
|
||||
|
||||
return x; // NaN
|
||||
}
|
||||
|
||||
ValueType result;
|
||||
uint c = result.Exp(x);
|
||||
|
||||
if( err )
|
||||
@@ -295,7 +349,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;
|
||||
|
||||
@@ -311,12 +365,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>
|
||||
|
||||
@@ -337,6 +389,8 @@ namespace ttmath
|
||||
x.Sub( temp );
|
||||
x = temp - x;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -455,15 +509,38 @@ 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( x.IsNan() )
|
||||
{
|
||||
if( err )
|
||||
*err = err_improper_argument;
|
||||
|
||||
return result; // NaN is set by default
|
||||
}
|
||||
|
||||
if( err )
|
||||
*err = err_ok;
|
||||
|
||||
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
|
||||
|
||||
// result has NaN flag set by default
|
||||
|
||||
if( err )
|
||||
*err = err_overflow; // maybe another error code? err_improper_argument?
|
||||
|
||||
return result; // NaN is set by default
|
||||
}
|
||||
|
||||
result = Sin0pi05( x );
|
||||
|
||||
one.SetOne();
|
||||
|
||||
@@ -488,14 +565,30 @@ 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)
|
||||
{
|
||||
if( x.IsNan() )
|
||||
{
|
||||
if( err )
|
||||
*err = err_improper_argument;
|
||||
|
||||
return x; // NaN
|
||||
}
|
||||
|
||||
ValueType pi05;
|
||||
pi05.Set05Pi();
|
||||
|
||||
x.Add( pi05 );
|
||||
uint c = x.Add( pi05 );
|
||||
|
||||
return Sin(x);
|
||||
if( c )
|
||||
{
|
||||
if( err )
|
||||
*err = err_overflow;
|
||||
|
||||
return ValueType(); // result is undefined (NaN is set by default)
|
||||
}
|
||||
|
||||
return Sin(x, err);
|
||||
}
|
||||
|
||||
|
||||
@@ -512,20 +605,22 @@ 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() )
|
||||
{
|
||||
if( err )
|
||||
*err = err_improper_argument;
|
||||
|
||||
result.SetNan();
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
if( err )
|
||||
*err = err_ok;
|
||||
|
||||
return Sin(x) / result;
|
||||
return Sin(x, err) / result;
|
||||
}
|
||||
|
||||
|
||||
@@ -552,20 +647,22 @@ 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() )
|
||||
{
|
||||
if( err )
|
||||
*err = err_improper_argument;
|
||||
|
||||
result.SetNan();
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
if( err )
|
||||
*err = err_ok;
|
||||
|
||||
return Cos(x) / result;
|
||||
return Cos(x, err) / result;
|
||||
}
|
||||
|
||||
|
||||
@@ -746,16 +843,24 @@ namespace ttmath
|
||||
{
|
||||
using namespace auxiliaryfunctions;
|
||||
|
||||
ValueType one;
|
||||
ValueType result, one;
|
||||
one.SetOne();
|
||||
bool change_sign = false;
|
||||
|
||||
if( x.IsNan() )
|
||||
{
|
||||
if( err )
|
||||
*err = err_improper_argument;
|
||||
|
||||
return result; // NaN is set by default
|
||||
}
|
||||
|
||||
if( x.GreaterWithoutSignThan(one) )
|
||||
{
|
||||
if( err )
|
||||
*err = err_improper_argument;
|
||||
|
||||
return one;
|
||||
return result; // NaN is set by default
|
||||
}
|
||||
|
||||
if( x.IsSign() )
|
||||
@@ -766,8 +871,6 @@ namespace ttmath
|
||||
|
||||
one.exponent.SubOne(); // =0.5
|
||||
|
||||
ValueType result;
|
||||
|
||||
// asin(-x) = -asin(x)
|
||||
if( x.GreaterWithoutSignThan(one) )
|
||||
result = ASin_1(x);
|
||||
@@ -974,6 +1077,9 @@ namespace ttmath
|
||||
one.SetOne();
|
||||
bool change_sign = false;
|
||||
|
||||
if( x.IsNan() )
|
||||
return result; // NaN is set by default
|
||||
|
||||
// if x is negative we're using the formula:
|
||||
// atan(-x) = -atan(x)
|
||||
if( x.IsSign() )
|
||||
@@ -1054,6 +1160,14 @@ namespace ttmath
|
||||
template<class ValueType>
|
||||
ValueType Sinh(const ValueType & x, ErrorCode * err = 0)
|
||||
{
|
||||
if( x.IsNan() )
|
||||
{
|
||||
if( err )
|
||||
*err = err_improper_argument;
|
||||
|
||||
return x; // NaN
|
||||
}
|
||||
|
||||
ValueType ex, emx;
|
||||
uint c = 0;
|
||||
|
||||
@@ -1078,6 +1192,14 @@ namespace ttmath
|
||||
template<class ValueType>
|
||||
ValueType Cosh(const ValueType & x, ErrorCode * err = 0)
|
||||
{
|
||||
if( x.IsNan() )
|
||||
{
|
||||
if( err )
|
||||
*err = err_improper_argument;
|
||||
|
||||
return x; // NaN
|
||||
}
|
||||
|
||||
ValueType ex, emx;
|
||||
uint c = 0;
|
||||
|
||||
@@ -1102,6 +1224,14 @@ namespace ttmath
|
||||
template<class ValueType>
|
||||
ValueType Tanh(const ValueType & x, ErrorCode * err = 0)
|
||||
{
|
||||
if( x.IsNan() )
|
||||
{
|
||||
if( err )
|
||||
*err = err_improper_argument;
|
||||
|
||||
return x; // NaN
|
||||
}
|
||||
|
||||
ValueType ex, emx, nominator, denominator;
|
||||
uint c = 0;
|
||||
|
||||
@@ -1142,12 +1272,20 @@ namespace ttmath
|
||||
template<class ValueType>
|
||||
ValueType Coth(const ValueType & x, ErrorCode * err = 0)
|
||||
{
|
||||
if( x.IsNan() )
|
||||
{
|
||||
if( err )
|
||||
*err = err_improper_argument;
|
||||
|
||||
return x; // NaN
|
||||
}
|
||||
|
||||
if( x.IsZero() )
|
||||
{
|
||||
if( err )
|
||||
*err = err_improper_argument;
|
||||
|
||||
return x;
|
||||
return ValueType(); // NaN is set by default
|
||||
}
|
||||
|
||||
ValueType ex, emx, nominator, denominator;
|
||||
@@ -1199,6 +1337,14 @@ namespace ttmath
|
||||
template<class ValueType>
|
||||
ValueType ASinh(const ValueType & x, ErrorCode * err = 0)
|
||||
{
|
||||
if( x.IsNan() )
|
||||
{
|
||||
if( err )
|
||||
*err = err_improper_argument;
|
||||
|
||||
return x; // NaN
|
||||
}
|
||||
|
||||
ValueType xx(x), one, result;
|
||||
uint c = 0;
|
||||
one.SetOne();
|
||||
@@ -1227,6 +1373,14 @@ namespace ttmath
|
||||
template<class ValueType>
|
||||
ValueType ACosh(const ValueType & x, ErrorCode * err = 0)
|
||||
{
|
||||
if( x.IsNan() )
|
||||
{
|
||||
if( err )
|
||||
*err = err_improper_argument;
|
||||
|
||||
return x; // NaN
|
||||
}
|
||||
|
||||
ValueType xx(x), one, result;
|
||||
uint c = 0;
|
||||
one.SetOne();
|
||||
@@ -1236,7 +1390,7 @@ namespace ttmath
|
||||
if( err )
|
||||
*err = err_improper_argument;
|
||||
|
||||
return result;
|
||||
return result; // NaN is set by default
|
||||
}
|
||||
|
||||
c += xx.Mul(x);
|
||||
@@ -1268,6 +1422,14 @@ namespace ttmath
|
||||
template<class ValueType>
|
||||
ValueType ATanh(const ValueType & x, ErrorCode * err = 0)
|
||||
{
|
||||
if( x.IsNan() )
|
||||
{
|
||||
if( err )
|
||||
*err = err_improper_argument;
|
||||
|
||||
return x; // NaN
|
||||
}
|
||||
|
||||
ValueType nominator(x), denominator, one, result;
|
||||
uint c = 0;
|
||||
one.SetOne();
|
||||
@@ -1277,7 +1439,7 @@ namespace ttmath
|
||||
if( err )
|
||||
*err = err_improper_argument;
|
||||
|
||||
return result;
|
||||
return result; // NaN is set by default
|
||||
}
|
||||
|
||||
c += nominator.Add(one);
|
||||
@@ -1313,6 +1475,14 @@ namespace ttmath
|
||||
template<class ValueType>
|
||||
ValueType ACoth(const ValueType & x, ErrorCode * err = 0)
|
||||
{
|
||||
if( x.IsNan() )
|
||||
{
|
||||
if( err )
|
||||
*err = err_improper_argument;
|
||||
|
||||
return x; // NaN
|
||||
}
|
||||
|
||||
ValueType nominator(x), denominator(x), one, result;
|
||||
uint c = 0;
|
||||
one.SetOne();
|
||||
@@ -1322,7 +1492,7 @@ namespace ttmath
|
||||
if( err )
|
||||
*err = err_improper_argument;
|
||||
|
||||
return result;
|
||||
return result; // NaN is set by default
|
||||
}
|
||||
|
||||
c += nominator.Add(one);
|
||||
@@ -1371,6 +1541,14 @@ namespace ttmath
|
||||
ValueType result, temp;
|
||||
uint c = 0;
|
||||
|
||||
if( x.IsNan() )
|
||||
{
|
||||
if( err )
|
||||
*err = err_improper_argument;
|
||||
|
||||
return result; // NaN is set by default
|
||||
}
|
||||
|
||||
result = x;
|
||||
|
||||
// it is better to make division first and then multiplication
|
||||
@@ -1399,6 +1577,14 @@ namespace ttmath
|
||||
ValueType result, delimiter;
|
||||
uint c = 0;
|
||||
|
||||
if( x.IsNan() )
|
||||
{
|
||||
if( err )
|
||||
*err = err_improper_argument;
|
||||
|
||||
return result; // NaN is set by default
|
||||
}
|
||||
|
||||
result = 180;
|
||||
c += result.Mul(x);
|
||||
|
||||
@@ -1436,12 +1622,12 @@ namespace ttmath
|
||||
ValueType delimiter, multipler;
|
||||
uint c = 0;
|
||||
|
||||
if( m.IsSign() || s.IsSign() )
|
||||
if( d.IsNan() || m.IsNan() || s.IsNan() || m.IsSign() || s.IsSign() )
|
||||
{
|
||||
if( err )
|
||||
*err = err_improper_argument;
|
||||
|
||||
return delimiter;
|
||||
return delimiter ; // NaN is set by default
|
||||
}
|
||||
|
||||
multipler = 60;
|
||||
@@ -1490,6 +1676,14 @@ namespace ttmath
|
||||
ValueType result, temp;
|
||||
uint c = 0;
|
||||
|
||||
if( x.IsNan() )
|
||||
{
|
||||
if( err )
|
||||
*err = err_improper_argument;
|
||||
|
||||
return result; // NaN is set by default
|
||||
}
|
||||
|
||||
result = x;
|
||||
|
||||
// it is better to make division first and then multiplication
|
||||
@@ -1518,6 +1712,14 @@ namespace ttmath
|
||||
ValueType result, delimiter;
|
||||
uint c = 0;
|
||||
|
||||
if( x.IsNan() )
|
||||
{
|
||||
if( err )
|
||||
*err = err_improper_argument;
|
||||
|
||||
return result; // NaN is set by default
|
||||
}
|
||||
|
||||
result = 200;
|
||||
c += result.Mul(x);
|
||||
|
||||
@@ -1542,6 +1744,14 @@ namespace ttmath
|
||||
ValueType result, temp;
|
||||
uint c = 0;
|
||||
|
||||
if( x.IsNan() )
|
||||
{
|
||||
if( err )
|
||||
*err = err_improper_argument;
|
||||
|
||||
return result; // NaN is set by default
|
||||
}
|
||||
|
||||
result = x;
|
||||
|
||||
temp = 200;
|
||||
@@ -1584,6 +1794,14 @@ namespace ttmath
|
||||
ValueType result, temp;
|
||||
uint c = 0;
|
||||
|
||||
if( x.IsNan() )
|
||||
{
|
||||
if( err )
|
||||
*err = err_improper_argument;
|
||||
|
||||
return result; // NaN is set by default
|
||||
}
|
||||
|
||||
result = x;
|
||||
|
||||
temp = 180;
|
||||
@@ -1617,12 +1835,12 @@ namespace ttmath
|
||||
template<class ValueType>
|
||||
ValueType Sqrt(ValueType x, ErrorCode * err = 0)
|
||||
{
|
||||
if( x.IsSign() )
|
||||
if( x.IsNan() || x.IsSign() )
|
||||
{
|
||||
if( err )
|
||||
*err = err_improper_argument;
|
||||
|
||||
return x;
|
||||
return ValueType(); // NaN is set by default
|
||||
}
|
||||
|
||||
if( x.IsZero() )
|
||||
@@ -1660,6 +1878,8 @@ namespace ttmath
|
||||
if( err )
|
||||
*err = err_improper_argument;
|
||||
|
||||
x.SetNan();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -1678,6 +1898,8 @@ namespace ttmath
|
||||
if( err )
|
||||
*err = err_improper_argument;
|
||||
|
||||
x.SetNan();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -1695,7 +1917,7 @@ namespace ttmath
|
||||
|
||||
|
||||
template<class ValueType>
|
||||
bool RootCheckIndexOne(ValueType & x, const ValueType & index, ErrorCode * err)
|
||||
bool RootCheckIndexOne(const ValueType & index, ErrorCode * err)
|
||||
{
|
||||
ValueType one;
|
||||
one.SetOne();
|
||||
@@ -1727,6 +1949,8 @@ namespace ttmath
|
||||
if( err )
|
||||
*err = err_improper_argument;
|
||||
|
||||
x.SetNan();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -1735,11 +1959,12 @@ namespace ttmath
|
||||
|
||||
|
||||
template<class ValueType>
|
||||
bool RootCheckXZero(ValueType & x, const ValueType & index, ErrorCode * err)
|
||||
bool RootCheckXZero(ValueType & x, ErrorCode * err)
|
||||
{
|
||||
if( x.IsZero() )
|
||||
{
|
||||
// root(0;index) is zero (if index!=0)
|
||||
// RootCheckIndexZero() must be called beforehand
|
||||
x.SetZero();
|
||||
|
||||
if( err )
|
||||
@@ -1775,6 +2000,8 @@ namespace ttmath
|
||||
if( err )
|
||||
*err = err_improper_argument;
|
||||
|
||||
x.SetNan();
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -1804,11 +2031,19 @@ namespace ttmath
|
||||
{
|
||||
using namespace auxiliaryfunctions;
|
||||
|
||||
if( x.IsNan() || index.IsNan() )
|
||||
{
|
||||
if( err )
|
||||
*err = err_improper_argument;
|
||||
|
||||
return ValueType(); // NaN is set by default
|
||||
}
|
||||
|
||||
if( RootCheckIndexSign(x, index, err) ) return x;
|
||||
if( RootCheckIndexZero(x, index, err) ) return x;
|
||||
if( RootCheckIndexOne (x, index, err) ) return x;
|
||||
if( RootCheckIndexOne ( index, err) ) return x;
|
||||
if( RootCheckIndexFrac(x, index, err) ) return x;
|
||||
if( RootCheckXZero(x, index, err) ) return x;
|
||||
if( RootCheckXZero (x, err) ) return x;
|
||||
|
||||
// index integer and index!=0
|
||||
// x!=0
|
||||
@@ -1859,13 +2094,17 @@ namespace ttmath
|
||||
|
||||
while( !carry && multipler<maxvalue )
|
||||
{
|
||||
if( stop && stop->WasStopSignal() )
|
||||
if( stop && (multipler & 127)==0 ) // it means 'stop && (multipler % 128)==0'
|
||||
{
|
||||
// after each 128 iterations we make a test
|
||||
if( stop->WasStopSignal() )
|
||||
{
|
||||
if( err )
|
||||
*err = err_interrupt;
|
||||
|
||||
return 2;
|
||||
}
|
||||
}
|
||||
|
||||
++multipler;
|
||||
carry += result.MulUInt(multipler);
|
||||
@@ -1888,19 +2127,25 @@ namespace ttmath
|
||||
|
||||
one.SetOne();
|
||||
uint carry = 0;
|
||||
uint iter = 1; // only for testing the stop object
|
||||
|
||||
while( !carry && multipler < x )
|
||||
{
|
||||
if( stop && stop->WasStopSignal() )
|
||||
if( stop && (iter & 31)==0 ) // it means 'stop && (iter % 32)==0'
|
||||
{
|
||||
// after each 32 iterations we make a test
|
||||
if( stop->WasStopSignal() )
|
||||
{
|
||||
if( err )
|
||||
*err = err_interrupt;
|
||||
|
||||
return 2;
|
||||
}
|
||||
}
|
||||
|
||||
carry += multipler.Add(one);
|
||||
carry += result.Mul(multipler);
|
||||
++iter;
|
||||
}
|
||||
|
||||
if( err )
|
||||
@@ -1927,16 +2172,16 @@ namespace ttmath
|
||||
static History<ValueType> history;
|
||||
ValueType result;
|
||||
|
||||
result.SetOne();
|
||||
|
||||
if( x.IsSign() )
|
||||
if( x.IsNan() || x.IsSign() )
|
||||
{
|
||||
if( err )
|
||||
*err = err_improper_argument;
|
||||
|
||||
return result;
|
||||
return result; // NaN set by default
|
||||
}
|
||||
|
||||
result.SetOne();
|
||||
|
||||
if( !x.exponent.IsSign() && !x.exponent.IsZero() )
|
||||
{
|
||||
// when x.exponent>0 there's no sense to calculate the formula
|
||||
@@ -1945,6 +2190,8 @@ namespace ttmath
|
||||
if( err )
|
||||
*err = err_overflow;
|
||||
|
||||
result.SetNan();
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -1963,8 +2210,11 @@ namespace ttmath
|
||||
status = FactorialMore(x, err, stop, result);
|
||||
|
||||
if( status == 2 )
|
||||
{
|
||||
// the calculation has been interrupted
|
||||
result.SetNan();
|
||||
return result;
|
||||
}
|
||||
|
||||
err_tmp = status==1 ? err_overflow : err_ok;
|
||||
history.Add(x, result, err_tmp);
|
||||
@@ -2008,14 +2258,25 @@ 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)
|
||||
ValueType Mod(ValueType a, const ValueType & b, ErrorCode * err = 0)
|
||||
{
|
||||
a.Mod(b);
|
||||
if( a.IsNan() || b.IsNan() )
|
||||
{
|
||||
if( err )
|
||||
*err = err_improper_argument;
|
||||
|
||||
return ValueType(); // NaN is set by default
|
||||
}
|
||||
|
||||
uint c = a.Mod(b);
|
||||
|
||||
if( err )
|
||||
*err = c ? err_overflow : err_ok;
|
||||
|
||||
return a;
|
||||
}
|
||||
@@ -2031,4 +2292,10 @@ namespace ttmath
|
||||
*/
|
||||
#include "ttmathparser.h"
|
||||
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma warning( default: 4127 )
|
||||
//warning C4127: conditional expression is constant
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
File diff suppressed because it is too large
Load Diff
@@ -963,7 +963,7 @@ public:
|
||||
*/
|
||||
uint FromString(const std::string & s, uint b = 10)
|
||||
{
|
||||
return FromString( s.c_str() );
|
||||
return FromString( s.c_str(), b );
|
||||
}
|
||||
|
||||
|
||||
@@ -1316,5 +1316,4 @@ public:
|
||||
|
||||
} // namespace
|
||||
|
||||
|
||||
#endif
|
||||
|
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* This file is a part of TTMath Mathematical Library
|
||||
* This file is a part of TTMath Bignum Library
|
||||
* and is distributed under the (new) BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@slimaczek.pl>
|
||||
*/
|
||||
@@ -708,7 +708,11 @@ void Sin(int sindex, int amount_of_args, ValueType & result)
|
||||
if( amount_of_args != 1 )
|
||||
Error( err_improper_amount_of_arguments );
|
||||
|
||||
result = ttmath::Sin( ConvertAngleToRad(stack[sindex].value) );
|
||||
ErrorCode err;
|
||||
result = ttmath::Sin( ConvertAngleToRad(stack[sindex].value), &err );
|
||||
|
||||
if(err != err_ok)
|
||||
Error( err );
|
||||
}
|
||||
|
||||
void Cos(int sindex, int amount_of_args, ValueType & result)
|
||||
@@ -716,7 +720,11 @@ void Cos(int sindex, int amount_of_args, ValueType & result)
|
||||
if( amount_of_args != 1 )
|
||||
Error( err_improper_amount_of_arguments );
|
||||
|
||||
result = ttmath::Cos( ConvertAngleToRad(stack[sindex].value) );
|
||||
ErrorCode err;
|
||||
result = ttmath::Cos( ConvertAngleToRad(stack[sindex].value), &err );
|
||||
|
||||
if(err != err_ok)
|
||||
Error( err );
|
||||
}
|
||||
|
||||
void Tan(int sindex, int amount_of_args, ValueType & result)
|
||||
@@ -757,7 +765,10 @@ void Round(int sindex, int amount_of_args, ValueType & result)
|
||||
if( amount_of_args != 1 )
|
||||
Error( err_improper_amount_of_arguments );
|
||||
|
||||
result = ttmath::Round(stack[sindex].value);
|
||||
result = stack[sindex].value;
|
||||
|
||||
if( result.Round() )
|
||||
Error( err_overflow );
|
||||
}
|
||||
|
||||
|
||||
@@ -973,7 +984,7 @@ void Not(int sindex, int amount_of_args, ValueType & result)
|
||||
|
||||
void DegToRad(int sindex, int amount_of_args, ValueType & result)
|
||||
{
|
||||
ErrorCode err;
|
||||
ErrorCode err = err_ok;
|
||||
|
||||
if( amount_of_args == 1 )
|
||||
{
|
||||
@@ -1052,7 +1063,7 @@ void RadToGrad(int sindex, int amount_of_args, ValueType & result)
|
||||
|
||||
void DegToGrad(int sindex, int amount_of_args, ValueType & result)
|
||||
{
|
||||
ErrorCode err;
|
||||
ErrorCode err = err_ok;
|
||||
|
||||
if( amount_of_args == 1 )
|
||||
{
|
||||
@@ -1555,7 +1566,7 @@ int character;
|
||||
|
||||
do
|
||||
{
|
||||
result += character;
|
||||
result += static_cast<char>( character );
|
||||
character = * ++pstring;
|
||||
}
|
||||
while( (character>='a' && character<='z') ||
|
||||
|
@@ -64,7 +64,7 @@
|
||||
*/
|
||||
#define TTMATH_MAJOR_VER 0
|
||||
#define TTMATH_MINOR_VER 8
|
||||
#define TTMATH_REVISION_VER 4
|
||||
#define TTMATH_REVISION_VER 6
|
||||
#define TTMATH_PRERELEASE_VER 0
|
||||
|
||||
|
||||
@@ -237,6 +237,17 @@ namespace ttmath
|
||||
|
||||
|
||||
|
||||
/*!
|
||||
this is a limit when calculating Karatsuba multiplication
|
||||
if the size of a vector is smaller than TTMATH_USE_KARATSUBA_MULTIPLICATION_FROM_SIZE
|
||||
the Karatsuba algorithm will use standard schoolbook multiplication
|
||||
*/
|
||||
#ifdef __GNUC__
|
||||
#define TTMATH_USE_KARATSUBA_MULTIPLICATION_FROM_SIZE 3
|
||||
#else
|
||||
#define TTMATH_USE_KARATSUBA_MULTIPLICATION_FROM_SIZE 5
|
||||
#endif
|
||||
|
||||
|
||||
namespace ttmath
|
||||
{
|
||||
@@ -349,7 +360,7 @@ namespace ttmath
|
||||
foo.Add(foo);
|
||||
but there are only few methods which can do that
|
||||
*/
|
||||
class ReferenceError : public std::logic_error, ExceptionInfo
|
||||
class ReferenceError : public std::logic_error, public ExceptionInfo
|
||||
{
|
||||
public:
|
||||
|
||||
@@ -381,7 +392,7 @@ namespace ttmath
|
||||
the name and the line of a file where the macro TTMATH_ASSERT
|
||||
was used)
|
||||
*/
|
||||
class RuntimeError : public std::runtime_error, ExceptionInfo
|
||||
class RuntimeError : public std::runtime_error, public ExceptionInfo
|
||||
{
|
||||
public:
|
||||
|
||||
|
@@ -40,6 +40,7 @@
|
||||
#ifndef headerfilettmathuint
|
||||
#define headerfilettmathuint
|
||||
|
||||
|
||||
/*!
|
||||
\file ttmathuint.h
|
||||
\brief template class UInt<uint>
|
||||
@@ -48,6 +49,7 @@
|
||||
#include <iostream>
|
||||
#include <iomanip>
|
||||
|
||||
|
||||
#include "ttmathtypes.h"
|
||||
|
||||
|
||||
@@ -766,8 +768,7 @@ public:
|
||||
{
|
||||
MulTwoWords(u.table[x1], ss2, &r2, &r1 );
|
||||
|
||||
|
||||
if( x1 <= value_size - 2 )
|
||||
if( value_size>1 && x1<=value_size-2 )
|
||||
{
|
||||
if( AddTwoInts(r2,r1,x1) )
|
||||
return 1;
|
||||
@@ -840,8 +841,10 @@ public:
|
||||
|
||||
/*!
|
||||
the multiplication 'this' = 'this' * ss2
|
||||
|
||||
algorithm: 100 - means automatically choose the fastest algorithm
|
||||
*/
|
||||
uint Mul(const UInt<value_size> & ss2, uint algorithm = 2)
|
||||
uint Mul(const UInt<value_size> & ss2, uint algorithm = 100)
|
||||
{
|
||||
switch( algorithm )
|
||||
{
|
||||
@@ -849,8 +852,14 @@ public:
|
||||
return Mul1(ss2);
|
||||
|
||||
case 2:
|
||||
default:
|
||||
return Mul2(ss2);
|
||||
|
||||
case 3:
|
||||
return Mul3(ss2);
|
||||
|
||||
case 100:
|
||||
default:
|
||||
return MulFastest(ss2);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -860,10 +869,12 @@ public:
|
||||
|
||||
since the 'result' is twice bigger than 'this' and 'ss2'
|
||||
this method never returns a carry
|
||||
|
||||
algorithm: 100 - means automatically choose the fastest algorithm
|
||||
*/
|
||||
void MulBig(const UInt<value_size> & ss2,
|
||||
UInt<value_size*2> & result,
|
||||
uint algorithm = 2)
|
||||
uint algorithm = 100)
|
||||
{
|
||||
switch( algorithm )
|
||||
{
|
||||
@@ -871,8 +882,14 @@ public:
|
||||
return Mul1Big(ss2, result);
|
||||
|
||||
case 2:
|
||||
default:
|
||||
return Mul2Big(ss2, result);
|
||||
|
||||
case 3:
|
||||
return Mul3Big(ss2, result);
|
||||
|
||||
case 100:
|
||||
default:
|
||||
return MulFastestBig(ss2, result);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -964,7 +981,7 @@ public:
|
||||
uint Mul2(const UInt<value_size> & ss2)
|
||||
{
|
||||
UInt<value_size*2> result;
|
||||
uint i;
|
||||
uint i, c = 0;
|
||||
|
||||
Mul2Big(ss2, result);
|
||||
|
||||
@@ -975,11 +992,14 @@ public:
|
||||
// testing carry
|
||||
for( ; i<value_size*2 ; ++i)
|
||||
if( result.table[i] != 0 )
|
||||
return 1;
|
||||
{
|
||||
c = 1;
|
||||
break;
|
||||
}
|
||||
|
||||
TTMATH_LOG("UInt::Mul2")
|
||||
|
||||
return 0;
|
||||
return c;
|
||||
}
|
||||
|
||||
|
||||
@@ -991,44 +1011,385 @@ public:
|
||||
*/
|
||||
void Mul2Big(const UInt<value_size> & ss2, UInt<value_size*2> & result)
|
||||
{
|
||||
uint r2,r1;
|
||||
uint x1size=value_size, x2size=value_size;
|
||||
Mul2Big2<value_size>(table, ss2.table, result);
|
||||
|
||||
TTMATH_LOG("UInt::Mul2Big")
|
||||
}
|
||||
|
||||
|
||||
private:
|
||||
|
||||
/*!
|
||||
an auxiliary method for calculating the multiplication
|
||||
|
||||
arguments we're taking as pointers (this is to improve the Mul3Big2()- avoiding
|
||||
unnecessary copying objects), the result should be taken as a pointer too,
|
||||
but at the moment there is no method AddTwoInts() which can operate on pointers
|
||||
*/
|
||||
template<uint ss_size>
|
||||
void Mul2Big2(const uint * ss1, const uint * ss2, UInt<ss_size*2> & result)
|
||||
{
|
||||
uint x1size = ss_size, x2size = ss_size;
|
||||
uint x1start = 0, x2start = 0;
|
||||
|
||||
if( ss_size > 2 )
|
||||
{
|
||||
// if the ss_size is smaller than or equal to 2
|
||||
// there is no sense to set x1size (and others) to another values
|
||||
|
||||
for(x1size=ss_size ; x1size>0 && ss1[x1size-1]==0 ; --x1size);
|
||||
for(x2size=ss_size ; x2size>0 && ss2[x2size-1]==0 ; --x2size);
|
||||
|
||||
for(x1start=0 ; x1start<x1size && ss1[x1start]==0 ; ++x1start);
|
||||
for(x2start=0 ; x2start<x2size && ss2[x2start]==0 ; ++x2start);
|
||||
}
|
||||
|
||||
Mul2Big3<ss_size>(ss1, ss2, result, x1start, x1size, x2start, x2size);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*!
|
||||
an auxiliary method for calculating the multiplication
|
||||
*/
|
||||
template<uint ss_size>
|
||||
void Mul2Big3(const uint * ss1, const uint * ss2, UInt<ss_size*2> & result, uint x1start, uint x1size, uint x2start, uint x2size)
|
||||
{
|
||||
uint r2, r1;
|
||||
|
||||
result.SetZero();
|
||||
|
||||
if( value_size > 2 )
|
||||
if( x1size==0 || x2size==0 )
|
||||
return;
|
||||
|
||||
for(uint x1=x1start ; x1<x1size ; ++x1)
|
||||
{
|
||||
// if the value_size is smaller than or equal to 2
|
||||
// there is no sense to set x1size (and others) to another values
|
||||
for(uint x2=x2start ; x2<x2size ; ++x2)
|
||||
{
|
||||
MulTwoWords(ss1[x1], ss2[x2], &r2, &r1);
|
||||
result.AddTwoInts(r2, r1, x2+x1);
|
||||
// here will never be a carry
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public:
|
||||
|
||||
|
||||
/*!
|
||||
multiplication: this = this * ss2
|
||||
|
||||
This is Karatsuba Multiplication algorithm, we're using it when value_size is greater than
|
||||
or equal to TTMATH_USE_KARATSUBA_MULTIPLICATION_FROM_SIZE macro (defined in ttmathuint.h).
|
||||
If value_size is smaller then we're using Mul2Big() instead.
|
||||
|
||||
Karatsuba multiplication:
|
||||
Assume we have:
|
||||
this = x = x1*B^m + x0
|
||||
ss2 = y = y1*B^m + y0
|
||||
where x0 and y0 are less than B^m
|
||||
the product from multiplication we can show as:
|
||||
x*y = (x1*B^m + x0)(y1*B^m + y0) = z2*B^(2m) + z1*B^m + z0
|
||||
where
|
||||
z2 = x1*y1
|
||||
z1 = x1*y0 + x0*y1
|
||||
z0 = x0*y0
|
||||
this is standard schoolbook algorithm with O(n^2), Karatsuba observed that z1 can be given in other form:
|
||||
z1 = (x1 + x0)*(y1 + y0) - z2 - z0 / z1 = (x1*y1 + x1*y0 + x0*y1 + x0*y0) - x1*y1 - x0*y0 = x1*y0 + x0*y1 /
|
||||
and to calculate the multiplication we need only three multiplications (with some additions and subtractions)
|
||||
|
||||
Our objects 'this' and 'ss2' we divide into two parts and by using recurrence we calculate the multiplication.
|
||||
Karatsuba multiplication has O( n^(ln(3)/ln(2)) )
|
||||
*/
|
||||
uint Mul3(const UInt<value_size> & ss2)
|
||||
{
|
||||
UInt<value_size*2> result;
|
||||
uint i, c = 0;
|
||||
|
||||
Mul3Big(ss2, result);
|
||||
|
||||
// copying result
|
||||
for(i=0 ; i<value_size ; ++i)
|
||||
table[i] = result.table[i];
|
||||
|
||||
// testing carry
|
||||
for( ; i<value_size*2 ; ++i)
|
||||
if( result.table[i] != 0 )
|
||||
{
|
||||
c = 1;
|
||||
break;
|
||||
}
|
||||
|
||||
TTMATH_LOG("UInt::Mul3")
|
||||
|
||||
return c;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*!
|
||||
multiplication: result = this * ss2
|
||||
|
||||
result is twice bigger than this and ss2,
|
||||
this method never returns carry,
|
||||
(Karatsuba multiplication)
|
||||
*/
|
||||
void Mul3Big(const UInt<value_size> & ss2, UInt<value_size*2> & result)
|
||||
{
|
||||
Mul3Big2<value_size>(table, ss2.table, result.table);
|
||||
|
||||
TTMATH_LOG("UInt::Mul3Big")
|
||||
}
|
||||
|
||||
|
||||
|
||||
private:
|
||||
|
||||
/*!
|
||||
an auxiliary method for calculating the Karatsuba multiplication
|
||||
|
||||
result_size is equal ss_size*2
|
||||
*/
|
||||
template<uint ss_size>
|
||||
void Mul3Big2(const uint * ss1, const uint * ss2, uint * result)
|
||||
{
|
||||
const uint * x1, * x0, * y1, * y0;
|
||||
|
||||
|
||||
if( ss_size>1 && ss_size<TTMATH_USE_KARATSUBA_MULTIPLICATION_FROM_SIZE )
|
||||
{
|
||||
UInt<ss_size*2> res;
|
||||
Mul2Big2<ss_size>(ss1, ss2, res);
|
||||
|
||||
for(uint i=0 ; i<ss_size*2 ; ++i)
|
||||
result[i] = res.table[i];
|
||||
|
||||
return;
|
||||
}
|
||||
else
|
||||
if( ss_size == 1 )
|
||||
{
|
||||
return MulTwoWords(*ss1, *ss2, &result[1], &result[0]);
|
||||
}
|
||||
|
||||
|
||||
if( (ss_size & 1) == 1 )
|
||||
{
|
||||
// ss_size is odd
|
||||
x0 = ss1;
|
||||
y0 = ss2;
|
||||
x1 = ss1 + ss_size / 2 + 1;
|
||||
y1 = ss2 + ss_size / 2 + 1;
|
||||
|
||||
// the second vectors (x1 and y1) are smaller about one from the first ones (x0 and y0)
|
||||
Mul3Big3<ss_size/2 + 1, ss_size/2, ss_size*2>(x1, x0, y1, y0, result);
|
||||
}
|
||||
else
|
||||
{
|
||||
// ss_size is even
|
||||
x0 = ss1;
|
||||
y0 = ss2;
|
||||
x1 = ss1 + ss_size / 2;
|
||||
y1 = ss2 + ss_size / 2;
|
||||
|
||||
// all four vectors (x0 x1 y0 y1) are equal in size
|
||||
Mul3Big3<ss_size/2, ss_size/2, ss_size*2>(x1, x0, y1, y0, result);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma warning (disable : 4717)
|
||||
//warning C4717: recursive on all control paths, function will cause runtime stack overflow
|
||||
//we have the stop point in Mul3Big2() method
|
||||
#endif
|
||||
|
||||
|
||||
/*!
|
||||
an auxiliary method for calculating the Karatsuba multiplication
|
||||
|
||||
x = x1*B^m + x0
|
||||
y = y1*B^m + y0
|
||||
|
||||
first_size - is the size of vectors: x0 and y0
|
||||
second_size - is the size of vectors: x1 and y1 (can be either equal first_size or smaller about one from first_size)
|
||||
|
||||
x*y = (x1*B^m + x0)(y1*B^m + y0) = z2*B^(2m) + z1*B^m + z0
|
||||
where
|
||||
z0 = x0*y0
|
||||
z2 = x1*y1
|
||||
z1 = (x1 + x0)*(y1 + y0) - z2 - z0
|
||||
*/
|
||||
template<uint first_size, uint second_size, uint result_size>
|
||||
void Mul3Big3(const uint * x1, const uint * x0, const uint * y1, const uint * y0, uint * result)
|
||||
{
|
||||
uint i, c, xc, yc;
|
||||
|
||||
UInt<first_size> temp, temp2;
|
||||
UInt<first_size*3> z1;
|
||||
|
||||
// z0 and z2 we store directly in the result (we don't use any temporary variables)
|
||||
Mul3Big2<first_size>(x0, y0, result); // z0
|
||||
Mul3Big2<second_size>(x1, y1, result+first_size*2); // z2
|
||||
|
||||
// now we calculate z1
|
||||
// temp = (x0 + x1)
|
||||
// temp2 = (y0 + y1)
|
||||
// we're using temp and temp2 with UInt<first_size>, although there can be a carry but
|
||||
// we simple remember it in xc and yc (xc and yc can be either 0 or 1),
|
||||
// and (x0 + x1)*(y0 + y1) we calculate in this way (schoolbook algorithm):
|
||||
//
|
||||
// xc | temp
|
||||
// yc | temp2
|
||||
// --------------------
|
||||
// (temp * temp2)
|
||||
// xc*temp2 |
|
||||
// yc*temp |
|
||||
// xc*yc |
|
||||
// ---------- z1 --------
|
||||
//
|
||||
// and the result is never larger in size than 3*first_size
|
||||
|
||||
xc = AddVector(x0, x1, first_size, second_size, temp.table);
|
||||
yc = AddVector(y0, y1, first_size, second_size, temp2.table);
|
||||
|
||||
Mul3Big2<first_size>(temp.table, temp2.table, z1.table);
|
||||
|
||||
// clearing the rest of z1
|
||||
for(i=first_size*2 ; i<first_size*3 ; ++i)
|
||||
z1.table[i] = 0;
|
||||
|
||||
|
||||
if( xc )
|
||||
{
|
||||
c = AddVector(z1.table+first_size, temp2.table, first_size*3-first_size, first_size, z1.table+first_size);
|
||||
TTMATH_ASSERT( c==0 )
|
||||
}
|
||||
|
||||
if( yc )
|
||||
{
|
||||
c = AddVector(z1.table+first_size, temp.table, first_size*3-first_size, first_size, z1.table+first_size);
|
||||
TTMATH_ASSERT( c==0 )
|
||||
}
|
||||
|
||||
|
||||
if( xc && yc )
|
||||
{
|
||||
for( i=first_size*2 ; i<first_size*3 ; ++i )
|
||||
if( ++z1.table[i] != 0 )
|
||||
break; // break if there was no carry
|
||||
}
|
||||
|
||||
// z1 = z1 - z2
|
||||
c = SubVector(z1.table, result+first_size*2, first_size*3, second_size*2, z1.table);
|
||||
TTMATH_ASSERT(c==0)
|
||||
|
||||
// z1 = z1 - z0
|
||||
c = SubVector(z1.table, result, first_size*3, first_size*2, z1.table);
|
||||
TTMATH_ASSERT(c==0)
|
||||
|
||||
// here we've calculated the z1
|
||||
// now we're adding it to the result
|
||||
|
||||
if( first_size > second_size )
|
||||
{
|
||||
uint z1_size = result_size - first_size;
|
||||
TTMATH_ASSERT( z1_size <= first_size*3 )
|
||||
|
||||
for(i=z1_size ; i<first_size*3 ; ++i)
|
||||
TTMATH_ASSERT( z1.table[i] == 0 )
|
||||
;
|
||||
|
||||
c = AddVector(result+first_size, z1.table, result_size-first_size, z1_size, result+first_size);
|
||||
TTMATH_ASSERT(c==0)
|
||||
}
|
||||
else
|
||||
{
|
||||
c = AddVector(result+first_size, z1.table, result_size-first_size, first_size*3, result+first_size);
|
||||
TTMATH_ASSERT(c==0)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma warning (default : 4717)
|
||||
#endif
|
||||
|
||||
|
||||
public:
|
||||
|
||||
|
||||
/*!
|
||||
multiplication this = this * ss2
|
||||
*/
|
||||
uint MulFastest(const UInt<value_size> & ss2)
|
||||
{
|
||||
UInt<value_size*2> result;
|
||||
uint i, c = 0;
|
||||
|
||||
MulFastestBig(ss2, result);
|
||||
|
||||
// copying result
|
||||
for(i=0 ; i<value_size ; ++i)
|
||||
table[i] = result.table[i];
|
||||
|
||||
// testing carry
|
||||
for( ; i<value_size*2 ; ++i)
|
||||
if( result.table[i] != 0 )
|
||||
{
|
||||
c = 1;
|
||||
break;
|
||||
}
|
||||
|
||||
TTMATH_LOG("UInt::MulFastest")
|
||||
|
||||
return c;
|
||||
}
|
||||
|
||||
|
||||
/*!
|
||||
multiplication result = this * ss2
|
||||
|
||||
this method is trying to select the fastest algorithm
|
||||
(in the future this method can be improved)
|
||||
*/
|
||||
void MulFastestBig(const UInt<value_size> & ss2, UInt<value_size*2> & result)
|
||||
{
|
||||
if( value_size < TTMATH_USE_KARATSUBA_MULTIPLICATION_FROM_SIZE )
|
||||
return Mul2Big(ss2, result);
|
||||
|
||||
uint x1size = value_size, x2size = value_size;
|
||||
uint x1start = 0, x2start = 0;
|
||||
|
||||
for(x1size=value_size ; x1size>0 && table[x1size-1]==0 ; --x1size);
|
||||
for(x2size=value_size ; x2size>0 && ss2.table[x2size-1]==0 ; --x2size);
|
||||
|
||||
if( x1size==0 || x2size==0 )
|
||||
{
|
||||
TTMATH_LOG("UInt::Mul2Big")
|
||||
// either 'this' or 'ss2' is equal zero - the result is zero too
|
||||
result.SetZero();
|
||||
return;
|
||||
}
|
||||
|
||||
for(x1start=0 ; x1start<x1size && table[x1start]==0 ; ++x1start);
|
||||
for(x2start=0 ; x2start<x2size && ss2.table[x2start]==0 ; ++x2start);
|
||||
}
|
||||
|
||||
for(uint x1=x1start ; x1<x1size ; ++x1)
|
||||
{
|
||||
for(uint x2=x2start ; x2<x2size ; ++x2)
|
||||
{
|
||||
MulTwoWords(table[x1], ss2.table[x2], &r2, &r1);
|
||||
result.AddTwoInts(r2,r1,x2+x1);
|
||||
// here will never be a carry
|
||||
}
|
||||
}
|
||||
uint distancex1 = x1size - x1start;
|
||||
uint distancex2 = x2size - x2start;
|
||||
|
||||
TTMATH_LOG("UInt::Mul2Big")
|
||||
}
|
||||
if( distancex1 < 3 || distancex2 < 3 )
|
||||
// either 'this' or 'ss2' have only 2 (or 1) item different from zero (side by side)
|
||||
// (this condition in the future can be improved)
|
||||
return Mul2Big3<value_size>(table, ss2.table, result, x1start, x1size, x2start, x2size);
|
||||
|
||||
|
||||
// Karatsuba multiplication
|
||||
Mul3Big(ss2, result);
|
||||
|
||||
TTMATH_LOG("UInt::MulFastestBig")
|
||||
}
|
||||
|
||||
|
||||
/*!
|
||||
@@ -1453,7 +1814,7 @@ private:
|
||||
|
||||
if( Div2_DivisorGreaterOrEqual( divisor, remainder,
|
||||
table_id, index,
|
||||
divisor_table_id, divisor_index) )
|
||||
divisor_index) )
|
||||
{
|
||||
TTMATH_LOG("UInt::Div2_FindLeadingBitsAndCheck")
|
||||
return 0;
|
||||
@@ -1473,7 +1834,7 @@ private:
|
||||
bool Div2_DivisorGreaterOrEqual( const UInt<value_size> & divisor,
|
||||
UInt<value_size> * remainder,
|
||||
uint table_id, uint index,
|
||||
uint divisor_table_id, uint divisor_index )
|
||||
uint divisor_index )
|
||||
{
|
||||
if( divisor_index > index )
|
||||
{
|
||||
@@ -1835,32 +2196,27 @@ public:
|
||||
UInt<value_size> start(*this), start_temp;
|
||||
UInt<value_size> result;
|
||||
result.SetOne();
|
||||
uint c = 0;
|
||||
|
||||
while( !pow.IsZero() )
|
||||
while( !c )
|
||||
{
|
||||
if( pow.table[0] & 1 )
|
||||
if( result.Mul(start) )
|
||||
{
|
||||
TTMATH_LOG("UInt::Pow(UInt<>)")
|
||||
return 1;
|
||||
}
|
||||
c += result.Mul(start);
|
||||
|
||||
pow.Rcr2_one(0);
|
||||
if( pow.IsZero() )
|
||||
break;
|
||||
|
||||
start_temp = start;
|
||||
// in the second Mul algorithm we can use start.Mul(start) directly (there is no TTMATH_ASSERT_REFERENCE there)
|
||||
if( start.Mul(start_temp) )
|
||||
{
|
||||
TTMATH_LOG("UInt::Pow(UInt<>)")
|
||||
return 1;
|
||||
}
|
||||
|
||||
pow.Rcr2_one(0);
|
||||
c += start.Mul(start_temp);
|
||||
}
|
||||
|
||||
*this = result;
|
||||
|
||||
TTMATH_LOG("UInt::Pow(UInt<>)")
|
||||
|
||||
return 0;
|
||||
return (c==0)? 0 : 1;
|
||||
}
|
||||
|
||||
|
||||
@@ -2346,7 +2702,7 @@ public:
|
||||
do
|
||||
{
|
||||
temp.DivInt(b, &rem);
|
||||
character = DigitToChar( rem );
|
||||
character = static_cast<char>( DigitToChar(rem) );
|
||||
result.insert(result.begin(), character);
|
||||
}
|
||||
while( !temp.IsZero() );
|
||||
@@ -2909,12 +3265,13 @@ private:
|
||||
uint Rcr2(uint bits, uint c);
|
||||
|
||||
public:
|
||||
|
||||
uint Add(const UInt<value_size> & ss2, uint c=0);
|
||||
uint AddInt(uint value, uint index = 0);
|
||||
uint AddTwoInts(uint x2, uint x1, uint index);
|
||||
static uint AddVector(const uint * ss1, const uint * ss2, uint ss1_size, uint ss2_size, uint * result);
|
||||
uint Sub(const UInt<value_size> & ss2, uint c=0);
|
||||
uint SubInt(uint value, uint index = 0);
|
||||
static uint SubVector(const uint * ss1, const uint * ss2, uint ss1_size, uint ss2_size, uint * result);
|
||||
static sint FindLeadingBitInWord(uint x);
|
||||
static uint SetBitInWord(uint & value, uint bit);
|
||||
static void MulTwoWords(uint a, uint b, uint * result_high, uint * result_low);
|
||||
@@ -2922,6 +3279,23 @@ public:
|
||||
};
|
||||
|
||||
|
||||
|
||||
/*!
|
||||
this specialization is needed in order to not confused the compiler "error: ISO C++ forbids zero-size array"
|
||||
when compiling Mul3Big2() method
|
||||
*/
|
||||
template<>
|
||||
class UInt<0>
|
||||
{
|
||||
public:
|
||||
uint table[1];
|
||||
|
||||
void Mul2Big(const UInt<0> &, UInt<0> &) { TTMATH_ASSERT(false) };
|
||||
void SetZero() { TTMATH_ASSERT(false) };
|
||||
uint AddTwoInts(uint, uint, uint) { TTMATH_ASSERT(false) return 0; };
|
||||
};
|
||||
|
||||
|
||||
} //namespace
|
||||
|
||||
|
||||
|
@@ -95,7 +95,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 +131,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,13 +184,54 @@ 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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*!
|
||||
this static method addes one vector to the other
|
||||
'ss1' is larger in size or equal to 'ss2'
|
||||
|
||||
ss1 points to the first (larger) vector
|
||||
ss2 points to the second vector
|
||||
ss1_size - size of the ss1 (and size of the result too)
|
||||
ss2_size - size of the ss2
|
||||
result - is the result vector (which has size the same as ss1: ss1_size)
|
||||
|
||||
Example: ss1_size is 5, ss2_size is 3
|
||||
ss1: ss2: result (output):
|
||||
5 1 5+1
|
||||
4 3 4+3
|
||||
2 7 2+7
|
||||
6 6
|
||||
9 9
|
||||
of course the carry is propagated and will be returned from the last item
|
||||
(this method is used by the Karatsuba multiplication algorithm)
|
||||
*/
|
||||
template<uint value_size>
|
||||
uint UInt<value_size>::AddVector(const uint * ss1, const uint * ss2, uint ss1_size, uint ss2_size, uint * result)
|
||||
{
|
||||
uint i, c = 0;
|
||||
|
||||
TTMATH_ASSERT( ss1_size >= ss2_size )
|
||||
|
||||
for(i=0 ; i<ss2_size ; ++i)
|
||||
c = AddTwoWords(ss1[i], ss2[i], c, &result[i]);
|
||||
|
||||
for( ; i<ss1_size ; ++i)
|
||||
c = AddTwoWords(ss1[i], 0, c, &result[i]);
|
||||
|
||||
//TTMATH_LOG("UInt::AddVector")
|
||||
|
||||
return c;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
template<uint value_size>
|
||||
uint UInt<value_size>::SubTwoWords(uint a, uint b, uint carry, uint * result)
|
||||
{
|
||||
@@ -232,7 +273,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,12 +311,51 @@ 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;
|
||||
}
|
||||
|
||||
|
||||
/*!
|
||||
this static method subtractes one vector from the other
|
||||
'ss1' is larger in size or equal to 'ss2'
|
||||
|
||||
ss1 points to the first (larger) vector
|
||||
ss2 points to the second vector
|
||||
ss1_size - size of the ss1 (and size of the result too)
|
||||
ss2_size - size of the ss2
|
||||
result - is the result vector (which has size the same as ss1: ss1_size)
|
||||
|
||||
Example: ss1_size is 5, ss2_size is 3
|
||||
ss1: ss2: result (output):
|
||||
5 1 5-1
|
||||
4 3 4-3
|
||||
2 7 2-7
|
||||
6 6-1 (the borrow from previous item)
|
||||
9 9
|
||||
return (carry): 0
|
||||
of course the carry (borrow) is propagated and will be returned from the last item
|
||||
(this method is used by the Karatsuba multiplication algorithm)
|
||||
*/
|
||||
template<uint value_size>
|
||||
uint UInt<value_size>::SubVector(const uint * ss1, const uint * ss2, uint ss1_size, uint ss2_size, uint * result)
|
||||
{
|
||||
uint i, c = 0;
|
||||
|
||||
TTMATH_ASSERT( ss1_size >= ss2_size )
|
||||
|
||||
for(i=0 ; i<ss2_size ; ++i)
|
||||
c = SubTwoWords(ss1[i], ss2[i], c, &result[i]);
|
||||
|
||||
for( ; i<ss1_size ; ++i)
|
||||
c = SubTwoWords(ss1[i], 0, c, &result[i]);
|
||||
|
||||
//TTMATH_LOG("UInt::SubVector")
|
||||
|
||||
return c;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*!
|
||||
@@ -305,7 +385,7 @@ namespace ttmath
|
||||
c = new_c;
|
||||
}
|
||||
|
||||
TTMATH_LOG("UInt64::Rcl2_one")
|
||||
TTMATH_LOG("UInt::Rcl2_one")
|
||||
|
||||
return c;
|
||||
}
|
||||
@@ -344,7 +424,7 @@ namespace ttmath
|
||||
c = new_c;
|
||||
}
|
||||
|
||||
TTMATH_LOG("UInt64::Rcr2_one")
|
||||
TTMATH_LOG("UInt::Rcr2_one")
|
||||
|
||||
return c;
|
||||
}
|
||||
@@ -421,7 +501,7 @@ namespace ttmath
|
||||
c = new_c;
|
||||
}
|
||||
|
||||
TTMATH_LOG("UInt64::Rcr2")
|
||||
TTMATH_LOG("UInt::Rcr2")
|
||||
|
||||
return (c & TTMATH_UINT_HIGHEST_BIT) ? 1 : 0;
|
||||
}
|
||||
@@ -473,7 +553,7 @@ namespace ttmath
|
||||
|
||||
uint mask = 1;
|
||||
|
||||
if( bit > 1 )
|
||||
if( bit > 0 )
|
||||
mask = mask << bit;
|
||||
|
||||
uint last = value & mask;
|
||||
|
File diff suppressed because it is too large
Load Diff
@@ -78,7 +78,6 @@ namespace ttmath
|
||||
uint b = value_size;
|
||||
uint * p1 = table;
|
||||
const uint * p2 = ss2.table;
|
||||
uint dummy, dummy2;
|
||||
|
||||
// we don't have to use TTMATH_REFERENCE_ASSERT here
|
||||
// this algorithm doesn't require it
|
||||
@@ -88,13 +87,15 @@ namespace ttmath
|
||||
#endif
|
||||
|
||||
#ifdef __GNUC__
|
||||
uint dummy, dummy2;
|
||||
|
||||
/*
|
||||
this part should be compiled with gcc
|
||||
*/
|
||||
__asm__ __volatile__(
|
||||
|
||||
"xorq %%rdx, %%rdx \n"
|
||||
"neg %%rax \n" // CF=1 if rax!=0 , CF=0 if rax==0
|
||||
"negq %%rax \n" // CF=1 if rax!=0 , CF=0 if rax==0
|
||||
|
||||
"1: \n"
|
||||
"movq (%%rsi,%%rdx,8), %%rax \n"
|
||||
@@ -112,7 +113,7 @@ namespace ttmath
|
||||
|
||||
#endif
|
||||
|
||||
TTMATH_LOG("UInt64::Add")
|
||||
TTMATH_LOG("UInt::Add")
|
||||
|
||||
return c;
|
||||
}
|
||||
@@ -145,7 +146,6 @@ namespace ttmath
|
||||
uint b = value_size;
|
||||
uint * p1 = table;
|
||||
uint c;
|
||||
uint dummy, dummy2;
|
||||
|
||||
TTMATH_ASSERT( index < value_size )
|
||||
|
||||
@@ -154,6 +154,7 @@ namespace ttmath
|
||||
#endif
|
||||
|
||||
#ifdef __GNUC__
|
||||
uint dummy, dummy2;
|
||||
|
||||
__asm__ __volatile__(
|
||||
|
||||
@@ -173,12 +174,12 @@ namespace ttmath
|
||||
"movzx %%al, %%rdx \n"
|
||||
|
||||
: "=d" (c), "=a" (dummy), "=c" (dummy2)
|
||||
: "a" (value), "c" (b), "0" (index), "b" (p1)
|
||||
: "0" (index), "1" (value), "2" (b), "b" (p1)
|
||||
: "cc", "memory" );
|
||||
|
||||
#endif
|
||||
|
||||
TTMATH_LOG("UInt64::AddInt")
|
||||
TTMATH_LOG("UInt::AddInt")
|
||||
|
||||
return c;
|
||||
}
|
||||
@@ -223,7 +224,6 @@ namespace ttmath
|
||||
uint b = value_size;
|
||||
uint * p1 = table;
|
||||
uint c;
|
||||
uint dummy, dummy2;
|
||||
|
||||
TTMATH_ASSERT( index < value_size - 1 )
|
||||
|
||||
@@ -232,6 +232,8 @@ namespace ttmath
|
||||
#endif
|
||||
|
||||
#ifdef __GNUC__
|
||||
uint dummy, dummy2;
|
||||
|
||||
__asm__ __volatile__(
|
||||
|
||||
"subq %%rdx, %%rcx \n"
|
||||
@@ -254,12 +256,94 @@ namespace ttmath
|
||||
"movzx %%al, %%rax \n"
|
||||
|
||||
: "=a" (c), "=c" (dummy), "=d" (dummy2)
|
||||
: "1" (b), "2" (index), "b" (p1), "S" (x1), "0" (x2)
|
||||
: "0" (x2), "1" (b), "2" (index), "b" (p1), "S" (x1)
|
||||
: "cc", "memory" );
|
||||
|
||||
#endif
|
||||
|
||||
TTMATH_LOG("UInt64::AddTwoInts")
|
||||
TTMATH_LOG("UInt::AddTwoInts")
|
||||
|
||||
return c;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*!
|
||||
this static method addes one vector to the other
|
||||
'ss1' is larger in size or equal to 'ss2'
|
||||
|
||||
ss1 points to the first (larger) vector
|
||||
ss2 points to the second vector
|
||||
ss1_size - size of the ss1 (and size of the result too)
|
||||
ss2_size - size of the ss2
|
||||
result - is the result vector (which has size the same as ss1: ss1_size)
|
||||
|
||||
Example: ss1_size is 5, ss2_size is 3
|
||||
ss1: ss2: result (output):
|
||||
5 1 5+1
|
||||
4 3 4+3
|
||||
2 7 2+7
|
||||
6 6
|
||||
9 9
|
||||
of course the carry is propagated and will be returned from the last item
|
||||
(this method is used by the Karatsuba multiplication algorithm)
|
||||
*/
|
||||
template<uint value_size>
|
||||
uint UInt<value_size>::AddVector(const uint * ss1, const uint * ss2, uint ss1_size, uint ss2_size, uint * result)
|
||||
{
|
||||
TTMATH_ASSERT( ss1_size >= ss2_size )
|
||||
|
||||
uint rest = ss1_size - ss2_size;
|
||||
uint c;
|
||||
|
||||
#ifndef __GNUC__
|
||||
#error "another compiler than GCC is currently not supported in 64bit mode"
|
||||
#endif
|
||||
|
||||
#ifdef __GNUC__
|
||||
uint dummy1, dummy2, dummy3;
|
||||
|
||||
// this part should be compiled with gcc
|
||||
|
||||
__asm__ __volatile__(
|
||||
"mov %%rdx, %%r8 \n"
|
||||
"xor %%rdx, %%rdx \n" // rdx = 0, cf = 0
|
||||
"1: \n"
|
||||
"mov (%%rsi,%%rdx,8), %%rax \n"
|
||||
"adc (%%rbx,%%rdx,8), %%rax \n"
|
||||
"mov %%rax, (%%rdi,%%rdx,8) \n"
|
||||
|
||||
"inc %%rdx \n"
|
||||
"dec %%rcx \n"
|
||||
"jnz 1b \n"
|
||||
|
||||
"adc %%rcx, %%rcx \n" // rcx has the cf state
|
||||
|
||||
"or %%r8, %%r8 \n"
|
||||
"jz 3f \n"
|
||||
|
||||
"xor %%rbx, %%rbx \n" // ebx = 0
|
||||
"neg %%rcx \n" // setting cf from rcx
|
||||
"mov %%r8, %%rcx \n" // rcx=rest and is != 0
|
||||
"2: \n"
|
||||
"mov (%%rsi, %%rdx, 8), %%rax \n"
|
||||
"adc %%rbx, %%rax \n"
|
||||
"mov %%rax, (%%rdi, %%rdx, 8) \n"
|
||||
|
||||
"inc %%rdx \n"
|
||||
"dec %%rcx \n"
|
||||
"jnz 2b \n"
|
||||
|
||||
"adc %%rcx, %%rcx \n"
|
||||
"3: \n"
|
||||
|
||||
: "=a" (dummy1), "=b" (dummy2), "=c" (c), "=d" (dummy3)
|
||||
: "1" (ss2), "2" (ss2_size), "3" (rest), "S" (ss1), "D" (result)
|
||||
: "%r8", "cc", "memory" );
|
||||
|
||||
#endif
|
||||
|
||||
TTMATH_LOG("UInt::AddVector")
|
||||
|
||||
return c;
|
||||
}
|
||||
@@ -284,7 +368,7 @@ namespace ttmath
|
||||
uint b = value_size;
|
||||
uint * p1 = table;
|
||||
const uint * p2 = ss2.table;
|
||||
uint dummy, dummy2;
|
||||
|
||||
|
||||
// we don't have to use TTMATH_REFERENCE_ASSERT here
|
||||
// this algorithm doesn't require it
|
||||
@@ -294,10 +378,12 @@ namespace ttmath
|
||||
#endif
|
||||
|
||||
#ifdef __GNUC__
|
||||
uint dummy, dummy2;
|
||||
|
||||
__asm__ __volatile__(
|
||||
|
||||
"xorq %%rdx, %%rdx \n"
|
||||
"neg %%rax \n" // CF=1 if rax!=0 , CF=0 if rax==0
|
||||
"negq %%rax \n" // CF=1 if rax!=0 , CF=0 if rax==0
|
||||
|
||||
"1: \n"
|
||||
"movq (%%rsi,%%rdx,8), %%rax \n"
|
||||
@@ -313,10 +399,9 @@ namespace ttmath
|
||||
: "0" (b), "1" (c), "b" (p1), "S" (p2)
|
||||
: "cc", "memory" );
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
TTMATH_LOG("UInt64::Sub")
|
||||
TTMATH_LOG("UInt::Sub")
|
||||
|
||||
return c;
|
||||
}
|
||||
@@ -374,17 +459,105 @@ namespace ttmath
|
||||
"movzx %%al, %%rdx \n"
|
||||
|
||||
: "=d" (c), "=a" (dummy), "=c" (dummy2)
|
||||
: "1" (value), "2" (b), "0" (index), "b" (p1)
|
||||
: "0" (index), "1" (value), "2" (b), "b" (p1)
|
||||
: "cc", "memory" );
|
||||
|
||||
#endif
|
||||
|
||||
TTMATH_LOG("UInt64::SubInt")
|
||||
TTMATH_LOG("UInt::SubInt")
|
||||
|
||||
return c;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/*!
|
||||
this static method subtractes one vector from the other
|
||||
'ss1' is larger in size or equal to 'ss2'
|
||||
|
||||
ss1 points to the first (larger) vector
|
||||
ss2 points to the second vector
|
||||
ss1_size - size of the ss1 (and size of the result too)
|
||||
ss2_size - size of the ss2
|
||||
result - is the result vector (which has size the same as ss1: ss1_size)
|
||||
|
||||
Example: ss1_size is 5, ss2_size is 3
|
||||
ss1: ss2: result (output):
|
||||
5 1 5-1
|
||||
4 3 4-3
|
||||
2 7 2-7
|
||||
6 6-1 (the borrow from previous item)
|
||||
9 9
|
||||
return (carry): 0
|
||||
of course the carry (borrow) is propagated and will be returned from the last item
|
||||
(this method is used by the Karatsuba multiplication algorithm)
|
||||
*/
|
||||
template<uint value_size>
|
||||
uint UInt<value_size>::SubVector(const uint * ss1, const uint * ss2, uint ss1_size, uint ss2_size, uint * result)
|
||||
{
|
||||
TTMATH_ASSERT( ss1_size >= ss2_size )
|
||||
|
||||
uint rest = ss1_size - ss2_size;
|
||||
uint c;
|
||||
|
||||
#ifndef __GNUC__
|
||||
#error "another compiler than GCC is currently not supported in 64bit mode"
|
||||
#endif
|
||||
|
||||
#ifdef __GNUC__
|
||||
|
||||
/*
|
||||
the asm code is nearly the same as in AddVector
|
||||
only two instructions 'adc' are changed to 'sbb'
|
||||
*/
|
||||
uint dummy1, dummy2, dummy3;
|
||||
|
||||
__asm__ __volatile__(
|
||||
"mov %%rdx, %%r8 \n"
|
||||
"xor %%rdx, %%rdx \n" // rdx = 0, cf = 0
|
||||
"1: \n"
|
||||
"mov (%%rsi,%%rdx,8), %%rax \n"
|
||||
"sbb (%%rbx,%%rdx,8), %%rax \n"
|
||||
"mov %%rax, (%%rdi,%%rdx,8) \n"
|
||||
|
||||
"inc %%rdx \n"
|
||||
"dec %%rcx \n"
|
||||
"jnz 1b \n"
|
||||
|
||||
"adc %%rcx, %%rcx \n" // rcx has the cf state
|
||||
|
||||
"or %%r8, %%r8 \n"
|
||||
"jz 3f \n"
|
||||
|
||||
"xor %%rbx, %%rbx \n" // ebx = 0
|
||||
"neg %%rcx \n" // setting cf from rcx
|
||||
"mov %%r8, %%rcx \n" // rcx=rest and is != 0
|
||||
"2: \n"
|
||||
"mov (%%rsi, %%rdx, 8), %%rax \n"
|
||||
"sbb %%rbx, %%rax \n"
|
||||
"mov %%rax, (%%rdi, %%rdx, 8) \n"
|
||||
|
||||
"inc %%rdx \n"
|
||||
"dec %%rcx \n"
|
||||
"jnz 2b \n"
|
||||
|
||||
"adc %%rcx, %%rcx \n"
|
||||
"3: \n"
|
||||
|
||||
: "=a" (dummy1), "=b" (dummy2), "=c" (c), "=d" (dummy3)
|
||||
: "1" (ss2), "2" (ss2_size), "3" (rest), "S" (ss1), "D" (result)
|
||||
: "%r8", "cc", "memory" );
|
||||
|
||||
#endif
|
||||
|
||||
TTMATH_LOG("UInt::SubVector")
|
||||
|
||||
return c;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*!
|
||||
this method moves all bits into the left hand side
|
||||
return value <- this <- c
|
||||
@@ -404,17 +577,19 @@ namespace ttmath
|
||||
{
|
||||
sint b = value_size;
|
||||
uint * p1 = table;
|
||||
uint dummy, dummy2;
|
||||
|
||||
|
||||
#ifndef __GNUC__
|
||||
#error "another compiler than GCC is currently not supported in 64bit mode"
|
||||
#endif
|
||||
|
||||
#ifdef __GNUC__
|
||||
uint dummy, dummy2;
|
||||
|
||||
__asm__ __volatile__(
|
||||
|
||||
"xorq %%rdx, %%rdx \n" // rdx=0
|
||||
"neg %%rax \n" // CF=1 if rax!=0 , CF=0 if rax==0
|
||||
"negq %%rax \n" // CF=1 if rax!=0 , CF=0 if rax==0
|
||||
|
||||
"1: \n"
|
||||
"rclq $1, (%%rbx, %%rdx, 8) \n"
|
||||
@@ -426,12 +601,12 @@ namespace ttmath
|
||||
"adcq %%rcx, %%rcx \n"
|
||||
|
||||
: "=c" (c), "=a" (dummy), "=d" (dummy2)
|
||||
: "1" (c), "0" (b), "b" (p1)
|
||||
: "0" (b), "1" (c), "b" (p1)
|
||||
: "cc", "memory" );
|
||||
|
||||
#endif
|
||||
|
||||
TTMATH_LOG("UInt64::Rcl2_one")
|
||||
TTMATH_LOG("UInt::Rcl2_one")
|
||||
|
||||
return c;
|
||||
}
|
||||
@@ -456,16 +631,18 @@ namespace ttmath
|
||||
{
|
||||
sint b = value_size;
|
||||
uint * p1 = table;
|
||||
uint dummy;
|
||||
|
||||
|
||||
#ifndef __GNUC__
|
||||
#error "another compiler than GCC is currently not supported in 64bit mode"
|
||||
#endif
|
||||
|
||||
#ifdef __GNUC__
|
||||
uint dummy;
|
||||
|
||||
__asm__ __volatile__(
|
||||
|
||||
"neg %%rax \n" // CF=1 if rax!=0 , CF=0 if rax==0
|
||||
"negq %%rax \n" // CF=1 if rax!=0 , CF=0 if rax==0
|
||||
|
||||
"1: \n"
|
||||
"rcrq $1, -8(%%rbx, %%rcx, 8) \n"
|
||||
@@ -476,12 +653,12 @@ namespace ttmath
|
||||
"adcq %%rcx, %%rcx \n"
|
||||
|
||||
: "=c" (c), "=a" (dummy)
|
||||
: "1" (c), "0" (b), "b" (p1)
|
||||
: "0" (b), "1" (c), "b" (p1)
|
||||
: "cc", "memory" );
|
||||
|
||||
#endif
|
||||
|
||||
TTMATH_LOG("UInt64::Rcr2_one")
|
||||
TTMATH_LOG("UInt::Rcr2_one")
|
||||
|
||||
return c;
|
||||
}
|
||||
@@ -509,13 +686,15 @@ namespace ttmath
|
||||
|
||||
uint b = value_size;
|
||||
uint * p1 = table;
|
||||
uint dummy, dummy2, dummy3;
|
||||
|
||||
|
||||
#ifndef __GNUC__
|
||||
#error "another compiler than GCC is currently not supported in 64bit mode"
|
||||
#endif
|
||||
|
||||
#ifdef __GNUC__
|
||||
uint dummy, dummy2, dummy3;
|
||||
|
||||
__asm__ __volatile__(
|
||||
|
||||
"movq %%rcx, %%rsi \n"
|
||||
@@ -528,7 +707,6 @@ namespace ttmath
|
||||
|
||||
"xorq %%rdx, %%rdx \n"
|
||||
"movq %%rdx, %%rsi \n"
|
||||
|
||||
"orq %%rax, %%rax \n"
|
||||
"cmovnz %%r8, %%rsi \n"
|
||||
|
||||
@@ -553,7 +731,7 @@ namespace ttmath
|
||||
|
||||
#endif
|
||||
|
||||
TTMATH_LOG("UInt64::Rcl2")
|
||||
TTMATH_LOG("UInt::Rcl2")
|
||||
|
||||
return c;
|
||||
}
|
||||
@@ -602,7 +780,6 @@ namespace ttmath
|
||||
"movq %%rdx, %%rsi \n"
|
||||
"addq %%rdi, %%rdx \n"
|
||||
"decq %%rdx \n"
|
||||
|
||||
"orq %%rax, %%rax \n"
|
||||
"cmovnz %%R8, %%rsi \n"
|
||||
|
||||
@@ -628,7 +805,7 @@ namespace ttmath
|
||||
|
||||
#endif
|
||||
|
||||
TTMATH_LOG("UInt64::Rcr2")
|
||||
TTMATH_LOG("UInt::Rcr2")
|
||||
|
||||
return c;
|
||||
}
|
||||
@@ -643,22 +820,24 @@ namespace ttmath
|
||||
template<uint value_size>
|
||||
sint UInt<value_size>::FindLeadingBitInWord(uint x)
|
||||
{
|
||||
register sint result;
|
||||
sint result;
|
||||
|
||||
|
||||
#ifndef __GNUC__
|
||||
#error "another compiler than GCC is currently not supported in 64bit mode"
|
||||
#endif
|
||||
|
||||
#ifdef __GNUC__
|
||||
__asm__ __volatile__(
|
||||
uint dummy;
|
||||
|
||||
"bsrq %1, %0 \n"
|
||||
"jnz 1f \n"
|
||||
"movq $-1, %0 \n"
|
||||
"1: \n"
|
||||
__asm__ (
|
||||
|
||||
: "=R" (result)
|
||||
: "R" (x)
|
||||
"movq $-1, %1 \n"
|
||||
"bsrq %2, %0 \n"
|
||||
"cmovz %1, %0 \n"
|
||||
|
||||
: "=r" (result), "=&r" (dummy)
|
||||
: "r" (x)
|
||||
: "cc" );
|
||||
|
||||
#endif
|
||||
@@ -695,10 +874,10 @@ namespace ttmath
|
||||
#endif
|
||||
|
||||
#ifdef __GNUC__
|
||||
__asm__ __volatile__(
|
||||
|
||||
__asm__ (
|
||||
|
||||
"btsq %%rbx, %%rax \n"
|
||||
|
||||
"setc %%bl \n"
|
||||
"movzx %%bl, %%rbx \n"
|
||||
|
||||
@@ -742,8 +921,8 @@ namespace ttmath
|
||||
this has no effect in visual studio but it's usefull when
|
||||
using gcc and options like -O
|
||||
*/
|
||||
register uint result1_;
|
||||
register uint result2_;
|
||||
uint result1_;
|
||||
uint result2_;
|
||||
|
||||
#ifndef __GNUC__
|
||||
#error "another compiler than GCC is currently not supported in 64bit mode"
|
||||
@@ -751,7 +930,7 @@ namespace ttmath
|
||||
|
||||
#ifdef __GNUC__
|
||||
|
||||
__asm__ __volatile__(
|
||||
__asm__ (
|
||||
|
||||
"mulq %%rdx \n"
|
||||
|
||||
@@ -793,8 +972,8 @@ namespace ttmath
|
||||
template<uint value_size>
|
||||
void UInt<value_size>::DivTwoWords(uint a,uint b, uint c, uint * r, uint * rest)
|
||||
{
|
||||
register uint r_;
|
||||
register uint rest_;
|
||||
uint r_;
|
||||
uint rest_;
|
||||
/*
|
||||
these variables have similar meaning like those in
|
||||
the multiplication algorithm MulTwoWords
|
||||
@@ -808,7 +987,7 @@ namespace ttmath
|
||||
|
||||
#ifdef __GNUC__
|
||||
|
||||
__asm__ __volatile__(
|
||||
__asm__ (
|
||||
|
||||
"divq %%rcx \n"
|
||||
|
||||
|
Reference in New Issue
Block a user