Compare commits
42 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
51e938eaa7 | ||
|
e102086f80 | ||
53547cfab5 | |||
d3a64b79ca | |||
|
51b2c974a1 | ||
|
5597373093 | ||
|
de58378488 | ||
|
de64608eba | ||
c70a947c07 | |||
8972fdfdb3 | |||
019a902fed | |||
74553109a5 | |||
9e42a5a9fd | |||
1b6858616d | |||
d789ac5396 | |||
|
be8913866a | ||
|
b31d34ebdd | ||
|
be821b59dd | ||
bb2583649e | |||
|
de1e7ac957 | ||
|
fdc292e91a | ||
5e5a106605 | |||
eaa19dd46a | |||
|
9b576ddbe2 | ||
|
a8c3a506ea | ||
|
3ba94dca90 | ||
|
cae50cd425 | ||
939d0f7519 | |||
05b67e7103 | |||
3231780a85 | |||
1bae0d6cb8 | |||
277dd72fb6 | |||
|
00e39d3608 | ||
|
37379d2f1f | ||
|
d7b67e4d47 | ||
|
c91bd24e98 | ||
|
cbc12db22f | ||
3e9bd5b093 | |||
a7a7eb7808 | |||
e665f91682 | |||
85945b2bb0 | |||
1efe39686b |
112
CHANGELOG
112
CHANGELOG
@@ -1,3 +1,115 @@
|
||||
Version 0.9.0 prerelease (2009.07.16):
|
||||
* added: support for wide characters (wchar_t)
|
||||
wide characters are used when macro TTMATH_USE_WCHAR is defined
|
||||
this macro is defined automatically when there is macro UNICODE or _UNICODE defined
|
||||
some types have been changed
|
||||
char -> tt_char
|
||||
std::string -> tt_string
|
||||
std::ostringstream -> tt_ostringstream
|
||||
std::ostream -> tt_ostream
|
||||
std::istream -> tt_istream
|
||||
normally tt_char is equal char but when you are using wide characters then tt_char will be wchar_t (and so on)
|
||||
(all typedef's are in ttmathtypes.h)
|
||||
* added: Big::IsInteger()
|
||||
returns true if the value is integer (without fraction)
|
||||
(NaN flag is not checked)
|
||||
* added: global Gamma() function
|
||||
* added: gamma() function to the parser
|
||||
* added: CGamma<ValueType> class
|
||||
is used with Gamma() and Factorial() in multithreaded environment
|
||||
* changed: Factorial() is using the Gamma() function now
|
||||
* removed: Parser<>::SetFactorialMax() method
|
||||
the factorial() is such a fast now that we don't need the method longer
|
||||
* removed: ErrorCode::err_too_big_factorial
|
||||
|
||||
|
||||
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
|
||||
(now the method returns one)
|
||||
* fixed: the problem with GCC optimization on x86_64
|
||||
sometimes when using -O2 or -O3 GCC doesn't set correctly
|
||||
the stack pointer (actually the stack is used for other things)
|
||||
and you can't use instructions like push/pop in assembler code.
|
||||
All the asm code in x86_64 have been rewritten, now instructions
|
||||
push/pop are not used, other thing which have access to stack
|
||||
(like "m" (mask) constraints in Rcl2 and Rcr2) have also gone away,
|
||||
now the library works well with -O2 and -O3 and the asm code
|
||||
is a little faster
|
||||
* added: UInt::PrintLog(const char * msg, std::ostream & output)
|
||||
used (for debugging purposes) by macro TTMATH_LOG(msg)
|
||||
(it is used in nearly all methods in UInt class)
|
||||
* added: macro TTMATH_DEBUG_LOG: when defined then TTMATH_LOG()
|
||||
put some debug information (to std::cout)
|
||||
* added: ttmathuint_x86.h, ttmathuint_x86_64.h, ttmathuint_noasm.h,
|
||||
all the methods which are using assembler code have been
|
||||
rewritten to no-asm forms, now we have:
|
||||
1. asm for x86 file: ttmathuint_x86.h
|
||||
2. asm for x86_64 file: ttmathuint_x86_64.h
|
||||
3. no asm file: ttmathuint_noasm.h
|
||||
(it's used when macro TTMATH_NOASM is defined)
|
||||
The third form can be used on x86 and x86_64 as well and
|
||||
on other platforms with a little effort.
|
||||
|
||||
|
||||
Version 0.8.3 (2009.04.06):
|
||||
* fixed: RclMoveAllWords() and RcrMoveAllWords() sometimes didn't return
|
||||
the proper carry, (when 'bits' was greater than or equal to 'value_size')
|
||||
|
4
README
4
README
@@ -21,6 +21,6 @@ This means only C++ developers can use this library and one thing they have
|
||||
to do is to use 'include' directive of the preprocessor. How big the
|
||||
values can be is set directly in the source code by the programmer.
|
||||
|
||||
Author: Tomasz Sowa <t.sowa@slimaczek.pl>
|
||||
Project pages: http://ttmath.slimaczek.pl
|
||||
Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
Project pages: http://www.ttmath.org
|
||||
http://sourceforge.net/projects/ttmath
|
||||
|
@@ -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);
|
||||
|
4822
ttmath/ttmath.h
4822
ttmath/ttmath.h
File diff suppressed because it is too large
Load Diff
8372
ttmath/ttmathbig.h
8372
ttmath/ttmathbig.h
File diff suppressed because it is too large
Load Diff
110
ttmath/ttmathconfig.h
Normal file
110
ttmath/ttmathconfig.h
Normal file
@@ -0,0 +1,110 @@
|
||||
/*
|
||||
* This file is a part of TTMath Bignum Library
|
||||
* and is distributed under the PNG licence.
|
||||
* Author: Christian Kaiser <chk@online.de>
|
||||
*/
|
||||
|
||||
/*
|
||||
Copyright (c) 2009 Christian Kaiser
|
||||
|
||||
This software is provided 'as-is', without any express or implied
|
||||
warranty. In no event will the authors be held liable for any damages
|
||||
arising from the use of this software.
|
||||
|
||||
Permission is granted to anyone to use this software for any purpose,
|
||||
including commercial applications, and to alter it and redistribute it
|
||||
freely, subject to the following restrictions:
|
||||
|
||||
1. The origin of this software must not be misrepresented; you must not
|
||||
claim that you wrote the original software. If you use this software
|
||||
in a product, an acknowledgment in the product documentation would be
|
||||
appreciated but is not required.
|
||||
|
||||
2. Altered source versions must be plainly marked as such, and must not be
|
||||
misrepresented as being the original software.
|
||||
|
||||
3. This notice may not be removed or altered from any source
|
||||
distribution.
|
||||
*/
|
||||
|
||||
#ifndef headerfilettmathconfig
|
||||
#define headerfilettmathconfig
|
||||
#pragma once
|
||||
|
||||
#include <sstream>
|
||||
|
||||
namespace ttmath
|
||||
{
|
||||
#if defined(WIN32)
|
||||
#include <windows.h>
|
||||
|
||||
#if defined(_MT)
|
||||
class clsCrit
|
||||
{
|
||||
private:
|
||||
mutable CRITICAL_SECTION _Crit;
|
||||
|
||||
clsCrit(const clsCrit&) // inhibit copy (easy mistake to do; use clsCritObj instead!!!)
|
||||
{
|
||||
}
|
||||
clsCrit& operator=(const clsCrit& rhs); // inhibit assignment
|
||||
public:
|
||||
clsCrit(void)
|
||||
{
|
||||
InitializeCriticalSection(&_Crit);
|
||||
}
|
||||
virtual ~clsCrit(void)
|
||||
{
|
||||
DeleteCriticalSection(&_Crit);
|
||||
}
|
||||
|
||||
void Enter(void) const
|
||||
{
|
||||
EnterCriticalSection(&_Crit);
|
||||
}
|
||||
void Leave(void) const
|
||||
{
|
||||
LeaveCriticalSection(&_Crit);
|
||||
}
|
||||
};
|
||||
|
||||
class clsCritObj
|
||||
{
|
||||
private:
|
||||
const clsCrit& _Crit;
|
||||
|
||||
clsCritObj& operator=(const clsCritObj& rhs); // not applicable
|
||||
public:
|
||||
clsCritObj(const clsCrit& Sync)
|
||||
: _Crit(Sync)
|
||||
{
|
||||
_Crit.Enter();
|
||||
}
|
||||
~clsCritObj(void)
|
||||
{
|
||||
_Crit.Leave();
|
||||
}
|
||||
};
|
||||
#define TTMATH_IMPLEMENT_THREADSAFE_OBJ \
|
||||
private: \
|
||||
clsCrit CritSect; \
|
||||
public: \
|
||||
operator clsCrit&() \
|
||||
{ \
|
||||
return(CritSect); \
|
||||
}
|
||||
#define TTMATH_USE_THREADSAFE_OBJ(c) clsCritObj lock(c)
|
||||
#endif
|
||||
#else // defined(WIN32)
|
||||
// not Windows world: no threading synchronization for now
|
||||
#endif
|
||||
|
||||
#if !defined(TTMATH_IMPLEMENT_THREADSAFE_OBJ)
|
||||
// if we don't know about serialization, make it a no-op
|
||||
#define TTMATH_IMPLEMENT_THREADSAFE_OBJ /* */
|
||||
#define TTMATH_USE_THREADSAFE_OBJ(c) /* */
|
||||
#endif
|
||||
|
||||
} // namespace
|
||||
|
||||
#endif // headerfilettmathconfig
|
@@ -1,7 +1,7 @@
|
||||
/*
|
||||
* This file is a part of TTMath Bignum Library
|
||||
* and is distributed under the (new) BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@slimaczek.pl>
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
@@ -47,6 +47,10 @@
|
||||
|
||||
#include "ttmathuint.h"
|
||||
|
||||
#if defined(_MSC_VER)
|
||||
#pragma warning(disable:4127) // conditional expression is constant
|
||||
#endif
|
||||
|
||||
namespace ttmath
|
||||
{
|
||||
|
||||
@@ -641,13 +645,19 @@ public:
|
||||
|
||||
// there can be a carry here when the size of this value is equal one word
|
||||
// and the 'value' has the highest bit set
|
||||
#if defined(_MSC_VER)
|
||||
#pragma warning(disable:4127) // conditional expression is constant
|
||||
#endif
|
||||
if( value_size==1 && (value & TTMATH_UINT_HIGHEST_BIT)!=0 )
|
||||
return 1;
|
||||
#if defined(_MSC_VER)
|
||||
#pragma warning(default:4127) // conditional expression is constant
|
||||
#endif
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// converting from Int
|
||||
|
||||
|
||||
/*!
|
||||
the default assignment operator
|
||||
@@ -714,7 +724,6 @@ public:
|
||||
}
|
||||
|
||||
|
||||
// converting from UInt
|
||||
|
||||
/*!
|
||||
this operator converts an UInt<another_size> type to this class
|
||||
@@ -759,8 +768,8 @@ public:
|
||||
// look that 'size' we still set as 'value_size' and not as u.value_size
|
||||
FromUInt(u);
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
|
||||
|
||||
#ifdef TTMATH_PLATFORM64
|
||||
@@ -822,7 +831,7 @@ public:
|
||||
/*!
|
||||
a constructor for converting string to this class (with the base=10)
|
||||
*/
|
||||
Int(const char * s)
|
||||
Int(const tt_char * s)
|
||||
{
|
||||
FromString(s);
|
||||
}
|
||||
@@ -831,7 +840,7 @@ public:
|
||||
/*!
|
||||
a constructor for converting a string to this class (with the base=10)
|
||||
*/
|
||||
Int(const std::string & s)
|
||||
Int(const tt_string & s)
|
||||
{
|
||||
FromString( s.c_str() );
|
||||
}
|
||||
@@ -870,7 +879,7 @@ public:
|
||||
/*!
|
||||
this method converts the value to a string with a base equal 'b'
|
||||
*/
|
||||
void ToString(std::string & result, uint b = 10) const
|
||||
void ToString(tt_string & result, uint b = 10) const
|
||||
{
|
||||
if( IsSign() )
|
||||
{
|
||||
@@ -891,12 +900,14 @@ public:
|
||||
|
||||
/*!
|
||||
this method converts a string into its value
|
||||
string is given either as 'const char *' or 'const wchar_t *'
|
||||
|
||||
it returns carry=1 if the value will be too big or an incorrect base 'b' is given
|
||||
|
||||
string is ended with a non-digit value, for example:
|
||||
"-12" will be translated to -12
|
||||
as well as:
|
||||
"- 12foo" will be translated to 12 too
|
||||
"- 12foo" will be translated to -12 too
|
||||
|
||||
existing first white characters will be ommited
|
||||
(between '-' and a first digit can be white characters too)
|
||||
@@ -905,7 +916,7 @@ public:
|
||||
|
||||
value_read (if exists) tells whether something has actually been read (at least one digit)
|
||||
*/
|
||||
uint FromString(const char * s, uint b = 10, const char ** after_source = 0, bool * value_read = 0)
|
||||
uint FromString(const tt_char * s, uint b = 10, const tt_char ** after_source = 0, bool * value_read = 0)
|
||||
{
|
||||
bool is_sign = false;
|
||||
|
||||
@@ -962,16 +973,16 @@ public:
|
||||
this method converts a string into its value
|
||||
it returns carry=1 if the value will be too big or an incorrect base 'b' is given
|
||||
*/
|
||||
uint FromString(const std::string & s, uint b = 10)
|
||||
uint FromString(const tt_string & s, uint b = 10)
|
||||
{
|
||||
return FromString( s.c_str() );
|
||||
return FromString( s.c_str(), b );
|
||||
}
|
||||
|
||||
|
||||
/*!
|
||||
this operator converts a string into its value (with base = 10)
|
||||
*/
|
||||
Int<value_size> & operator=(const char * s)
|
||||
Int<value_size> & operator=(const tt_char * s)
|
||||
{
|
||||
FromString(s);
|
||||
|
||||
@@ -982,7 +993,7 @@ public:
|
||||
/*!
|
||||
this operator converts a string into its value (with base = 10)
|
||||
*/
|
||||
Int<value_size> & operator=(const std::string & s)
|
||||
Int<value_size> & operator=(const tt_string & s)
|
||||
{
|
||||
FromString( s.c_str() );
|
||||
|
||||
@@ -1269,9 +1280,14 @@ public:
|
||||
*
|
||||
*/
|
||||
|
||||
friend std::ostream & operator<<(std::ostream & s, const Int<value_size> & l)
|
||||
/*!
|
||||
output for standard streams
|
||||
|
||||
tt_ostream is either std::ostream or std::wostream
|
||||
*/
|
||||
friend tt_ostream & operator<<(tt_ostream & s, const Int<value_size> & l)
|
||||
{
|
||||
std::string ss;
|
||||
tt_string ss;
|
||||
|
||||
l.ToString(ss);
|
||||
s << ss;
|
||||
@@ -1280,13 +1296,17 @@ public:
|
||||
}
|
||||
|
||||
|
||||
/*!
|
||||
input from standard streams
|
||||
|
||||
friend std::istream & operator>>(std::istream & s, Int<value_size> & l)
|
||||
tt_istream is either std::istream or std::wistream
|
||||
*/
|
||||
friend tt_istream & operator>>(tt_istream & s, Int<value_size> & l)
|
||||
{
|
||||
std::string ss;
|
||||
tt_string ss;
|
||||
|
||||
// char for operator>>
|
||||
unsigned char z;
|
||||
// tt_char for operator>>
|
||||
tt_char z;
|
||||
|
||||
// operator>> omits white characters if they're set for ommiting
|
||||
s >> z;
|
||||
@@ -1301,7 +1321,7 @@ public:
|
||||
while( s.good() && UInt<value_size>::CharToDigit(z, 10)>=0 )
|
||||
{
|
||||
ss += z;
|
||||
z = s.get();
|
||||
z = static_cast<tt_char>(s.get());
|
||||
}
|
||||
|
||||
// we're leaving the last readed character
|
||||
@@ -1317,5 +1337,9 @@ public:
|
||||
|
||||
} // namespace
|
||||
|
||||
#if defined(_MSC_VER)
|
||||
#pragma warning(default:4127) // conditional expression is constant
|
||||
#endif
|
||||
|
||||
|
||||
#endif
|
||||
|
@@ -1,7 +1,7 @@
|
||||
/*
|
||||
* This file is a part of TTMath Mathematical Library
|
||||
* and is distributed under the (new) BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@slimaczek.pl>
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
@@ -47,6 +47,7 @@
|
||||
#include "ttmathtypes.h"
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <list>
|
||||
#include <map>
|
||||
|
||||
@@ -73,18 +74,19 @@ public:
|
||||
struct Item
|
||||
{
|
||||
// name of a variable of a function
|
||||
std::string value;
|
||||
// (either std::string or std::wstring)
|
||||
tt_string value;
|
||||
|
||||
// number of parameters required by the function
|
||||
// (if there's a variable this 'param' is ignored)
|
||||
int param;
|
||||
|
||||
Item() {}
|
||||
Item(const std::string & v, int p) : value(v), param(p) {}
|
||||
Item(const tt_string & v, int p) : value(v), param(p) {}
|
||||
};
|
||||
|
||||
// 'Table' is the type of our table
|
||||
typedef std::map<std::string, Item> Table;
|
||||
typedef std::map<tt_string, Item> Table;
|
||||
typedef Table::iterator Iterator;
|
||||
typedef Table::const_iterator CIterator;
|
||||
|
||||
@@ -112,7 +114,7 @@ public:
|
||||
/*!
|
||||
this method returns true if the name can be as a name of an object
|
||||
*/
|
||||
static bool IsNameCorrect(const std::string & name)
|
||||
static bool IsNameCorrect(const tt_string & name)
|
||||
{
|
||||
if( name.empty() )
|
||||
return false;
|
||||
@@ -120,7 +122,7 @@ public:
|
||||
if( !CorrectCharacter(name[0], false) )
|
||||
return false;
|
||||
|
||||
std::string::const_iterator i=name.begin();
|
||||
tt_string::const_iterator i=name.begin();
|
||||
|
||||
for(++i ; i!=name.end() ; ++i)
|
||||
if( !CorrectCharacter(*i, true) )
|
||||
@@ -133,7 +135,7 @@ public:
|
||||
/*!
|
||||
this method returns true if such an object is defined (name exists)
|
||||
*/
|
||||
bool IsDefined(const std::string & name)
|
||||
bool IsDefined(const tt_string & name)
|
||||
{
|
||||
Iterator i = table.find(name);
|
||||
|
||||
@@ -148,7 +150,7 @@ public:
|
||||
/*!
|
||||
this method adds one object (variable of function) into the table
|
||||
*/
|
||||
ErrorCode Add(const std::string & name, const std::string & value, int param = 0)
|
||||
ErrorCode Add(const tt_string & name, const tt_string & value, int param = 0)
|
||||
{
|
||||
if( !IsNameCorrect(name) )
|
||||
return err_incorrect_name;
|
||||
@@ -205,7 +207,7 @@ public:
|
||||
/*!
|
||||
this method changes the value and the number of parameters for a specific object
|
||||
*/
|
||||
ErrorCode EditValue(const std::string & name, const std::string & value, int param = 0)
|
||||
ErrorCode EditValue(const tt_string & name, const tt_string & value, int param = 0)
|
||||
{
|
||||
if( !IsNameCorrect(name) )
|
||||
return err_incorrect_name;
|
||||
@@ -225,7 +227,7 @@ public:
|
||||
/*!
|
||||
this method changes the name of a specific object
|
||||
*/
|
||||
ErrorCode EditName(const std::string & old_name, const std::string & new_name)
|
||||
ErrorCode EditName(const tt_string & old_name, const tt_string & new_name)
|
||||
{
|
||||
if( !IsNameCorrect(old_name) || !IsNameCorrect(new_name) )
|
||||
return err_incorrect_name;
|
||||
@@ -256,7 +258,7 @@ public:
|
||||
/*!
|
||||
this method deletes an object
|
||||
*/
|
||||
ErrorCode Delete(const std::string & name)
|
||||
ErrorCode Delete(const tt_string & name)
|
||||
{
|
||||
if( !IsNameCorrect(name) )
|
||||
return err_incorrect_name;
|
||||
@@ -275,7 +277,7 @@ public:
|
||||
/*!
|
||||
this method gets the value of a specific object
|
||||
*/
|
||||
ErrorCode GetValue(const std::string & name, std::string & value) const
|
||||
ErrorCode GetValue(const tt_string & name, tt_string & value) const
|
||||
{
|
||||
if( !IsNameCorrect(name) )
|
||||
return err_incorrect_name;
|
||||
@@ -298,7 +300,7 @@ public:
|
||||
this method gets the value of a specific object
|
||||
(this version is used for not copying the whole string)
|
||||
*/
|
||||
ErrorCode GetValue(const std::string & name, const char ** value) const
|
||||
ErrorCode GetValue(const tt_string & name, const tt_char ** value) const
|
||||
{
|
||||
if( !IsNameCorrect(name) )
|
||||
return err_incorrect_name;
|
||||
@@ -321,7 +323,7 @@ public:
|
||||
this method gets the value and the number of parameters
|
||||
of a specific object
|
||||
*/
|
||||
ErrorCode GetValueAndParam(const std::string & name, std::string & value, int * param) const
|
||||
ErrorCode GetValueAndParam(const tt_string & name, tt_string & value, int * param) const
|
||||
{
|
||||
if( !IsNameCorrect(name) )
|
||||
return err_incorrect_name;
|
||||
@@ -347,7 +349,7 @@ public:
|
||||
of a specific object
|
||||
(this version is used for not copying the whole string)
|
||||
*/
|
||||
ErrorCode GetValueAndParam(const std::string & name, const char ** value, int * param) const
|
||||
ErrorCode GetValueAndParam(const tt_string & name, const tt_char ** value, int * param) const
|
||||
{
|
||||
if( !IsNameCorrect(name) )
|
||||
return err_incorrect_name;
|
||||
@@ -430,7 +432,7 @@ public:
|
||||
*/
|
||||
History()
|
||||
{
|
||||
buffer_max_size = 10;
|
||||
buffer_max_size = 15;
|
||||
}
|
||||
|
||||
|
||||
@@ -487,10 +489,118 @@ public:
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
/*!
|
||||
this methods deletes an item
|
||||
|
||||
we assume that there is only one item with the 'key'
|
||||
(this methods removes the first one)
|
||||
*/
|
||||
bool Remove(const ValueType & key)
|
||||
{
|
||||
typename buffer_type::iterator i = buffer.begin();
|
||||
|
||||
for( ; i != buffer.end() ; ++i )
|
||||
{
|
||||
if( i->key == key )
|
||||
{
|
||||
buffer.erase(i);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
}; // end of class History
|
||||
|
||||
|
||||
|
||||
/*!
|
||||
this is an auxiliary class used when calculating Gamma() or Factorial()
|
||||
|
||||
in multithreaded environment you can provide an object of this class to
|
||||
the Gamma() or Factorial() function, e.g;
|
||||
typedef Big<1, 3> MyBig;
|
||||
MyBig x = 123456;
|
||||
CGamma<MyBig> cgamma;
|
||||
std::cout << Gamma(x, cgamma);
|
||||
each thread should have its own CGamma<> object
|
||||
|
||||
in a single-thread environment a CGamma<> object is a static variable
|
||||
in a second version of Gamma() and you don't have to explicitly use it, e.g.
|
||||
typedef Big<1, 3> MyBig;
|
||||
MyBig x = 123456;
|
||||
std::cout << Gamma(x);
|
||||
*/
|
||||
template<class ValueType>
|
||||
struct CGamma
|
||||
{
|
||||
/*!
|
||||
this table holds factorials
|
||||
1
|
||||
1
|
||||
2
|
||||
6
|
||||
24
|
||||
120
|
||||
720
|
||||
.......
|
||||
*/
|
||||
std::vector<ValueType> fact;
|
||||
|
||||
|
||||
/*!
|
||||
this table holds Bernoulli numbers
|
||||
1
|
||||
-0.5
|
||||
0.166666666666666666666666667
|
||||
0
|
||||
-0.0333333333333333333333333333
|
||||
0
|
||||
0.0238095238095238095238095238
|
||||
0
|
||||
-0.0333333333333333333333333333
|
||||
0
|
||||
0.075757575757575757575757576
|
||||
.....
|
||||
*/
|
||||
std::vector<ValueType> bern;
|
||||
|
||||
|
||||
/*!
|
||||
here we store some calculated values
|
||||
(this is for speeding up, if the next argument of Gamma() or Factorial()
|
||||
is in the 'history' then the result we are not calculating but simply
|
||||
return from the 'history' object)
|
||||
*/
|
||||
History<ValueType> history;
|
||||
|
||||
|
||||
/*!
|
||||
this method prepares some coefficients: factorials and Bernoulli numbers
|
||||
stored in 'fact' and 'bern' objects
|
||||
|
||||
how many values should be depends on the size of the mantissa - if
|
||||
the mantissa is larger then we must calculate more values
|
||||
for a mantissa which consists of 256 bits (8 words on a 32bit platform)
|
||||
we have to calculate about 30 values (the size of fact and bern will be 30),
|
||||
and for a 2048 bits mantissa we have to calculate 306 coefficients
|
||||
|
||||
you don't have to call this method, these coefficients will be automatically calculated
|
||||
when they are needed
|
||||
|
||||
you must note that calculating of the coefficients is a little time-consuming operation,
|
||||
(especially when the mantissa is large) and first called to Gamma() or Factorial()
|
||||
can take more time than next calls, and in the end this is the point when InitAll()
|
||||
comes in handy: you can call this method somewhere at the beginning of your program
|
||||
*/
|
||||
void InitAll();
|
||||
// definition is in ttmath.h
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
} // namespace
|
||||
|
File diff suppressed because it is too large
Load Diff
@@ -1,413 +1,522 @@
|
||||
/*
|
||||
* This file is a part of TTMath Bignum Library
|
||||
* and is distributed under the (new) BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@slimaczek.pl>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2006-2009, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* * Neither the name Tomasz Sowa nor the names of contributors to this
|
||||
* project may be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
|
||||
* THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
#ifndef headerfilettmathtypes
|
||||
#define headerfilettmathtypes
|
||||
|
||||
/*!
|
||||
\file ttmathtypes.h
|
||||
\brief constants used in the library
|
||||
|
||||
As our library is written in header files (templates) we cannot use
|
||||
constants like 'const int' etc. because we should have some source files
|
||||
*.cpp to define this variables. Only what we can have are constants
|
||||
defined by #define preprocessor macros.
|
||||
|
||||
All macros are preceded by TTMATH_ prefix
|
||||
*/
|
||||
|
||||
|
||||
#include <stdexcept>
|
||||
#include <sstream>
|
||||
|
||||
|
||||
/*!
|
||||
the version of the library
|
||||
|
||||
TTMATH_PRERELEASE_VER is either zero or one
|
||||
if zero that means this is the release version of the library
|
||||
*/
|
||||
#define TTMATH_MAJOR_VER 0
|
||||
#define TTMATH_MINOR_VER 8
|
||||
#define TTMATH_REVISION_VER 3
|
||||
#define TTMATH_PRERELEASE_VER 0
|
||||
|
||||
|
||||
/*!
|
||||
TTMATH_DEBUG
|
||||
this macro enables further testing during writing your code
|
||||
you don't have to define it in a release mode
|
||||
|
||||
if this macro is set then macros TTMATH_ASSERT and TTMATH_REFERENCE_ASSERT
|
||||
are set as well and these macros can throw an exception if a condition in it
|
||||
is not fulfilled (look at the definition of TTMATH_ASSERT and TTMATH_REFERENCE_ASSERT)
|
||||
|
||||
TTMATH_RELEASE
|
||||
if you are confident that your code is perfect you can define TTMATH_RELEASE
|
||||
macro for example by using -D option in gcc
|
||||
gcc -DTTMATH_RELEASE -o myprogram myprogram.cpp
|
||||
or by defining this macro in your code before using any header files of this library
|
||||
|
||||
if TTMATH_RELEASE is not set then TTMATH_DEBUG is set
|
||||
*/
|
||||
#ifndef TTMATH_RELEASE
|
||||
#define TTMATH_DEBUG
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
namespace ttmath
|
||||
{
|
||||
|
||||
#if !defined _M_X64 && !defined __x86_64__
|
||||
|
||||
/*!
|
||||
we're using a 32bit platform
|
||||
*/
|
||||
#define TTMATH_PLATFORM32
|
||||
|
||||
#else
|
||||
|
||||
/*!
|
||||
we're using a 64bit platform
|
||||
*/
|
||||
#define TTMATH_PLATFORM64
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
#ifdef TTMATH_PLATFORM32
|
||||
|
||||
/*!
|
||||
on 32bit platforms one word (uint, sint) will be equal 32bits
|
||||
*/
|
||||
typedef unsigned int uint;
|
||||
typedef signed int sint;
|
||||
|
||||
/*!
|
||||
how many bits there are in the uint type
|
||||
*/
|
||||
#define TTMATH_BITS_PER_UINT 32u
|
||||
|
||||
/*!
|
||||
the mask for the highest bit in the unsigned 32bit word (2^31)
|
||||
*/
|
||||
#define TTMATH_UINT_HIGHEST_BIT 2147483648u
|
||||
|
||||
/*!
|
||||
the max value of the unsigned 32bit word (2^32 - 1)
|
||||
(all bits equal one)
|
||||
*/
|
||||
#define TTMATH_UINT_MAX_VALUE 4294967295u
|
||||
|
||||
/*!
|
||||
the number of words (32bit words on 32bit platform)
|
||||
which are kept in built-in variables for a Big<> type
|
||||
(these variables are defined in ttmathbig.h)
|
||||
*/
|
||||
#define TTMATH_BUILTIN_VARIABLES_SIZE 256u
|
||||
|
||||
#else
|
||||
|
||||
/*!
|
||||
on 64bit platforms one word (uint, sint) will be equal 64bits
|
||||
*/
|
||||
typedef unsigned long uint;
|
||||
typedef signed long sint;
|
||||
|
||||
/*!
|
||||
how many bits there are in the uint type
|
||||
*/
|
||||
#define TTMATH_BITS_PER_UINT 64ul
|
||||
|
||||
/*!
|
||||
the mask for the highest bit in the unsigned 64bit word (2^63)
|
||||
*/
|
||||
#define TTMATH_UINT_HIGHEST_BIT 9223372036854775808ul
|
||||
|
||||
/*!
|
||||
the max value of the unsigned 64bit word (2^64 - 1)
|
||||
(all bits equal one)
|
||||
*/
|
||||
#define TTMATH_UINT_MAX_VALUE 18446744073709551615ul
|
||||
|
||||
/*!
|
||||
the number of words (64bit words on 64bit platforms)
|
||||
which are kept in built-in variables for a Big<> type
|
||||
(these variables are defined in ttmathbig.h)
|
||||
*/
|
||||
#define TTMATH_BUILTIN_VARIABLES_SIZE 128ul
|
||||
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*!
|
||||
characters which represent the comma operator
|
||||
|
||||
TTMATH_COMMA_CHARACTER_1 is used in reading (parsing) and in writing (default, can be overwritten in ToString() function)
|
||||
TTMATH_COMMA_CHARACTER_2 can be used in reading as an auxiliary comma character
|
||||
that means you can input values for example 1.2345 and 1,2345 as well
|
||||
|
||||
if you don't want it just put 0 there e.g.
|
||||
#define TTMATH_COMMA_CHARACTER_2 0
|
||||
then only TTMATH_COMMA_CHARACTER_1 will be used
|
||||
|
||||
don't put there any special character which is used by the parser
|
||||
(for example a semicolon ';' shouldn't be there)
|
||||
*/
|
||||
#define TTMATH_COMMA_CHARACTER_1 '.'
|
||||
#define TTMATH_COMMA_CHARACTER_2 ','
|
||||
|
||||
|
||||
|
||||
/*!
|
||||
this variable defines how many iterations are performed
|
||||
during some kind of calculating when we're making any long formulas
|
||||
(for example Taylor series)
|
||||
|
||||
it's used in ExpSurrounding0(...), LnSurrounding1(...), Sin0pi05(...), etc.
|
||||
|
||||
note! there'll not be so many iterations, iterations are stopped when
|
||||
there is no sense to continue calculating (for example when the result
|
||||
still remains unchanged after adding next series and we know that the next
|
||||
series are smaller than previous ones)
|
||||
*/
|
||||
#define TTMATH_ARITHMETIC_MAX_LOOP 10000
|
||||
|
||||
|
||||
|
||||
|
||||
namespace ttmath
|
||||
{
|
||||
|
||||
/*!
|
||||
error codes
|
||||
*/
|
||||
enum ErrorCode
|
||||
{
|
||||
err_ok = 0,
|
||||
err_nothing_has_read,
|
||||
err_unknown_character,
|
||||
err_unexpected_final_bracket,
|
||||
err_stack_not_clear,
|
||||
err_unknown_variable,
|
||||
err_division_by_zero,
|
||||
err_interrupt,
|
||||
err_overflow,
|
||||
err_unknown_function,
|
||||
err_unknown_operator,
|
||||
err_unexpected_semicolon_operator,
|
||||
err_improper_amount_of_arguments,
|
||||
err_improper_argument,
|
||||
err_unexpected_end,
|
||||
err_internal_error,
|
||||
err_incorrect_name,
|
||||
err_incorrect_value,
|
||||
err_variable_exists,
|
||||
err_variable_loop,
|
||||
err_functions_loop,
|
||||
err_must_be_only_one_value,
|
||||
err_object_exists,
|
||||
err_unknown_object,
|
||||
err_still_calculating,
|
||||
err_too_big_factorial,
|
||||
err_in_short_form_used_function
|
||||
};
|
||||
|
||||
|
||||
/*!
|
||||
this simple class can be used in multithreading model
|
||||
(you can write your own class derived from this one)
|
||||
|
||||
for example: in some functions like Factorial()
|
||||
/at the moment only Factorial/ you can give a pointer to
|
||||
the 'stop object', if the method WasStopSignal() of this
|
||||
object returns true that means we should break the calculating
|
||||
and return
|
||||
*/
|
||||
class StopCalculating
|
||||
{
|
||||
public:
|
||||
virtual bool WasStopSignal() const volatile { return false; }
|
||||
virtual ~StopCalculating(){}
|
||||
};
|
||||
|
||||
|
||||
/*!
|
||||
a small class which is useful when compiling with gcc
|
||||
|
||||
object of this type holds the name and the line of a file
|
||||
in which the macro TTMATH_ASSERT or TTMATH_REFERENCE_ASSERT was used
|
||||
*/
|
||||
class ExceptionInfo
|
||||
{
|
||||
const char * file;
|
||||
int line;
|
||||
|
||||
public:
|
||||
ExceptionInfo() : file(0), line(0) {}
|
||||
ExceptionInfo(const char * f, int l) : file(f), line(l) {}
|
||||
|
||||
std::string Where() const
|
||||
{
|
||||
if( !file )
|
||||
return "unknown";
|
||||
|
||||
std::ostringstream result;
|
||||
result << file << ":" << line;
|
||||
|
||||
return result.str();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/*!
|
||||
A small class used for reporting 'reference' errors
|
||||
|
||||
In the library is used macro TTMATH_REFERENCE_ASSERT which
|
||||
can throw an exception of this type
|
||||
|
||||
If you compile with gcc you can get a small benefit
|
||||
from using method Where() (it returns std::string with
|
||||
the name and the line of a file where the macro TTMATH_REFERENCE_ASSERT
|
||||
was used)
|
||||
|
||||
What is the 'reference' error?
|
||||
Some kind of methods use a reference as their argument to another object,
|
||||
and the another object not always can be the same which is calling, e.g.
|
||||
Big<1,2> foo(10);
|
||||
foo.Mul(foo); // this is incorrect
|
||||
above method Mul is making something more with 'this' object and
|
||||
'this' cannot be passed as the argument because the result will be undefined
|
||||
|
||||
macro TTMATH_REFERENCE_ASSERT helps us to solve the above problem
|
||||
|
||||
note! some methods can use 'this' object as the argument
|
||||
for example this code is correct:
|
||||
UInt<2> foo(10);
|
||||
foo.Add(foo);
|
||||
but there are only few methods which can do that
|
||||
*/
|
||||
class ReferenceError : public std::logic_error, ExceptionInfo
|
||||
{
|
||||
public:
|
||||
|
||||
ReferenceError() : std::logic_error ("reference error")
|
||||
{
|
||||
}
|
||||
|
||||
ReferenceError(const char * f, int l) :
|
||||
std::logic_error ("reference error"), ExceptionInfo(f,l)
|
||||
{
|
||||
}
|
||||
|
||||
std::string Where() const
|
||||
{
|
||||
return ExceptionInfo::Where();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/*!
|
||||
a small class used for reporting errors
|
||||
|
||||
in the library is used macro TTMATH_ASSERT which
|
||||
(if the condition in it is false) throw an exception
|
||||
of this type
|
||||
|
||||
if you compile with gcc you can get a small benefit
|
||||
from using method Where() (it returns std::string with
|
||||
the name and the line of a file where the macro TTMATH_ASSERT
|
||||
was used)
|
||||
*/
|
||||
class RuntimeError : public std::runtime_error, ExceptionInfo
|
||||
{
|
||||
public:
|
||||
|
||||
RuntimeError() : std::runtime_error ("internal error")
|
||||
{
|
||||
}
|
||||
|
||||
RuntimeError(const char * f, int l) :
|
||||
std::runtime_error ("internal error"), ExceptionInfo(f,l)
|
||||
{
|
||||
}
|
||||
|
||||
std::string Where() const
|
||||
{
|
||||
return ExceptionInfo::Where();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
/*!
|
||||
look at the description of macros TTMATH_RELEASE and TTMATH_DEBUG
|
||||
*/
|
||||
#ifdef TTMATH_DEBUG
|
||||
|
||||
#if defined(__FILE__) && defined(__LINE__)
|
||||
|
||||
#define TTMATH_REFERENCE_ASSERT(expression) \
|
||||
if( &(expression) == this ) throw ttmath::ReferenceError(__FILE__, __LINE__);
|
||||
|
||||
#define TTMATH_ASSERT(expression) \
|
||||
if( !(expression) ) throw ttmath::RuntimeError(__FILE__, __LINE__);
|
||||
|
||||
#else
|
||||
|
||||
#define TTMATH_REFERENCE_ASSERT(expression) \
|
||||
if( &(expression) == this ) throw ReferenceError();
|
||||
|
||||
#define TTMATH_ASSERT(expression) \
|
||||
if( !(expression) ) throw RuntimeError();
|
||||
#endif
|
||||
|
||||
#else
|
||||
#define TTMATH_REFERENCE_ASSERT(expression)
|
||||
#define TTMATH_ASSERT(expression)
|
||||
#endif
|
||||
|
||||
|
||||
} // namespace
|
||||
|
||||
|
||||
#endif
|
||||
/*
|
||||
* This file is a part of TTMath Bignum Library
|
||||
* and is distributed under the (new) BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2006-2009, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* * Neither the name Tomasz Sowa nor the names of contributors to this
|
||||
* project may be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
|
||||
* THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
#ifndef headerfilettmathtypes
|
||||
#define headerfilettmathtypes
|
||||
|
||||
/*!
|
||||
\file ttmathtypes.h
|
||||
\brief constants used in the library
|
||||
|
||||
As our library is written in header files (templates) we cannot use
|
||||
constants like 'const int' etc. because we should have some source files
|
||||
*.cpp to define this variables. Only what we can have are constants
|
||||
defined by #define preprocessor macros.
|
||||
|
||||
All macros are preceded by TTMATH_ prefix
|
||||
*/
|
||||
|
||||
|
||||
#include <stdexcept>
|
||||
#include <sstream>
|
||||
#include <vector>
|
||||
|
||||
/*!
|
||||
the version of the library
|
||||
|
||||
TTMATH_PRERELEASE_VER is either zero or one
|
||||
if zero that means this is the release version of the library
|
||||
*/
|
||||
#define TTMATH_MAJOR_VER 0
|
||||
#define TTMATH_MINOR_VER 9
|
||||
#define TTMATH_REVISION_VER 0
|
||||
#define TTMATH_PRERELEASE_VER 1
|
||||
|
||||
|
||||
/*!
|
||||
TTMATH_DEBUG
|
||||
this macro enables further testing during writing your code
|
||||
you don't have to define it in a release mode
|
||||
|
||||
if this macro is set then macros TTMATH_ASSERT and TTMATH_REFERENCE_ASSERT
|
||||
are set as well and these macros can throw an exception if a condition in it
|
||||
is not fulfilled (look at the definition of TTMATH_ASSERT and TTMATH_REFERENCE_ASSERT)
|
||||
|
||||
TTMATH_RELEASE
|
||||
if you are confident that your code is perfect you can define TTMATH_RELEASE
|
||||
macro for example by using -D option in gcc
|
||||
gcc -DTTMATH_RELEASE -o myprogram myprogram.cpp
|
||||
or by defining this macro in your code before using any header files of this library
|
||||
|
||||
if TTMATH_RELEASE is not set then TTMATH_DEBUG is set automatically
|
||||
*/
|
||||
#ifndef TTMATH_RELEASE
|
||||
#define TTMATH_DEBUG
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
namespace ttmath
|
||||
{
|
||||
|
||||
#if !defined _M_X64 && !defined __x86_64__
|
||||
|
||||
/*!
|
||||
we're using a 32bit platform
|
||||
*/
|
||||
#define TTMATH_PLATFORM32
|
||||
|
||||
#else
|
||||
|
||||
/*!
|
||||
we're using a 64bit platform
|
||||
*/
|
||||
#define TTMATH_PLATFORM64
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
#ifdef TTMATH_PLATFORM32
|
||||
|
||||
/*!
|
||||
on 32bit platforms one word (uint, sint) will be equal 32bits
|
||||
*/
|
||||
typedef unsigned int uint;
|
||||
typedef signed int sint;
|
||||
|
||||
|
||||
/*!
|
||||
this type is twice bigger than uint
|
||||
(64bit on a 32bit platforms)
|
||||
|
||||
although C++ Standard - ANSI ISO IEC 14882:2003 doesn't define such a type (long long)
|
||||
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
|
||||
*/
|
||||
typedef unsigned long long int ulint;
|
||||
|
||||
/*!
|
||||
the mask for the highest bit in the unsigned 32bit word (2^31)
|
||||
*/
|
||||
const uint TTMATH_UINT_HIGHEST_BIT = 0x80000000ul;
|
||||
|
||||
/*!
|
||||
the max value of the unsigned 32bit word (2^32 - 1)
|
||||
(all bits equal one)
|
||||
*/
|
||||
const uint TTMATH_UINT_MAX_VALUE = 0xfffffffful;
|
||||
|
||||
/*!
|
||||
the number of words (32bit words on 32bit platform)
|
||||
which are kept in built-in variables for a Big<> type
|
||||
(these variables are defined in ttmathbig.h)
|
||||
*/
|
||||
const uint TTMATH_BUILTIN_VARIABLES_SIZE = 256u;
|
||||
|
||||
#else
|
||||
|
||||
/*!
|
||||
on 64bit platforms one word (uint, sint) will be equal 64bits
|
||||
*/
|
||||
#if defined(_MSC_VER)
|
||||
typedef unsigned __int64 uint;
|
||||
typedef signed __int64 sint;
|
||||
#else
|
||||
typedef unsigned long long uint;
|
||||
typedef signed long long sint;
|
||||
#endif
|
||||
/*!
|
||||
on 64bit platform we do not define ulint
|
||||
sizeof(long long) is 8 (64bit) but we need 128bit
|
||||
|
||||
on 64 bit platform (when there is defined TTMATH_NOASM macro)
|
||||
methods UInt::MulTwoWords and UInt::DivTwoWords are using other algorithms than those on 32 bit
|
||||
*/
|
||||
//typedef unsigned long long int ulint;
|
||||
|
||||
/*!
|
||||
the mask for the highest bit in the unsigned 64bit word (2^63)
|
||||
*/
|
||||
const uint TTMATH_UINT_HIGHEST_BIT = 0x8000000000000000ul;
|
||||
|
||||
/*!
|
||||
the max value of the unsigned 64bit word (2^64 - 1)
|
||||
(all bits equal one)
|
||||
*/
|
||||
const uint TTMATH_UINT_MAX_VALUE = 0xfffffffffffffffful;
|
||||
|
||||
/*!
|
||||
the number of words (64bit words on 64bit platforms)
|
||||
which are kept in built-in variables for a Big<> type
|
||||
(these variables are defined in ttmathbig.h)
|
||||
*/
|
||||
const uint TTMATH_BUILTIN_VARIABLES_SIZE = 128ul;
|
||||
|
||||
#endif
|
||||
|
||||
const uint TTMATH_BITS_PER_UINT = (sizeof(uint)*8);
|
||||
|
||||
}
|
||||
|
||||
|
||||
#if defined(UNICODE) || defined(_UNICODE)
|
||||
#define TTMATH_USE_WCHAR
|
||||
#endif
|
||||
|
||||
|
||||
#ifdef TTMATH_USE_WCHAR
|
||||
|
||||
typedef wchar_t tt_char;
|
||||
typedef std::wstring tt_string;
|
||||
typedef std::wostringstream tt_ostringstream;
|
||||
typedef std::wostream tt_ostream;
|
||||
typedef std::wistream tt_istream;
|
||||
#define TTMATH_TEXT_HELPER(txt) L##txt
|
||||
|
||||
#else
|
||||
|
||||
typedef char tt_char;
|
||||
typedef std::string tt_string;
|
||||
typedef std::ostringstream tt_ostringstream;
|
||||
typedef std::ostream tt_ostream;
|
||||
typedef std::istream tt_istream;
|
||||
#define TTMATH_TEXT_HELPER(txt) txt
|
||||
|
||||
#endif
|
||||
|
||||
#define TTMATH_TEXT(txt) TTMATH_TEXT_HELPER(txt)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/*!
|
||||
characters which represent the comma operator
|
||||
|
||||
TTMATH_COMMA_CHARACTER_1 is used in reading (parsing) and in writing (default, can be overwritten in ToString() function)
|
||||
TTMATH_COMMA_CHARACTER_2 can be used in reading as an auxiliary comma character
|
||||
that means you can input values for example 1.2345 and 1,2345 as well
|
||||
|
||||
if you don't want it just put 0 there e.g.
|
||||
#define TTMATH_COMMA_CHARACTER_2 0
|
||||
then only TTMATH_COMMA_CHARACTER_1 will be used
|
||||
|
||||
don't put there any special character which is used by the parser
|
||||
(for example a semicolon ';' shouldn't be there)
|
||||
*/
|
||||
#define TTMATH_COMMA_CHARACTER_1 '.'
|
||||
#define TTMATH_COMMA_CHARACTER_2 ','
|
||||
|
||||
|
||||
|
||||
/*!
|
||||
this variable defines how many iterations are performed
|
||||
during some kind of calculating when we're making any long formulas
|
||||
(for example Taylor series)
|
||||
|
||||
it's used in ExpSurrounding0(...), LnSurrounding1(...), Sin0pi05(...), etc.
|
||||
|
||||
note! there'll not be so many iterations, iterations are stopped when
|
||||
there is no sense to continue calculating (for example when the result
|
||||
still remains unchanged after adding next series and we know that the next
|
||||
series are smaller than previous ones)
|
||||
*/
|
||||
#define TTMATH_ARITHMETIC_MAX_LOOP 10000
|
||||
|
||||
|
||||
|
||||
/*!
|
||||
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
|
||||
|
||||
|
||||
/*!
|
||||
this is a special value used when calculating the Gamma(x) function
|
||||
if x is greater than this value then the Gamma(x) will be calculated using
|
||||
some kind of series
|
||||
|
||||
don't use smaller values than about 100
|
||||
*/
|
||||
#define TTMATH_GAMMA_BOUNDARY 2000
|
||||
|
||||
|
||||
|
||||
|
||||
namespace ttmath
|
||||
{
|
||||
|
||||
/*!
|
||||
error codes
|
||||
*/
|
||||
enum ErrorCode
|
||||
{
|
||||
err_ok = 0,
|
||||
err_nothing_has_read,
|
||||
err_unknown_character,
|
||||
err_unexpected_final_bracket,
|
||||
err_stack_not_clear,
|
||||
err_unknown_variable,
|
||||
err_division_by_zero,
|
||||
err_interrupt,
|
||||
err_overflow,
|
||||
err_unknown_function,
|
||||
err_unknown_operator,
|
||||
err_unexpected_semicolon_operator,
|
||||
err_improper_amount_of_arguments,
|
||||
err_improper_argument,
|
||||
err_unexpected_end,
|
||||
err_internal_error,
|
||||
err_incorrect_name,
|
||||
err_incorrect_value,
|
||||
err_variable_exists,
|
||||
err_variable_loop,
|
||||
err_functions_loop,
|
||||
err_must_be_only_one_value,
|
||||
err_object_exists,
|
||||
err_unknown_object,
|
||||
err_still_calculating,
|
||||
err_in_short_form_used_function
|
||||
};
|
||||
|
||||
|
||||
/*!
|
||||
this simple class can be used in multithreading model
|
||||
(you can write your own class derived from this one)
|
||||
|
||||
for example: in some functions like Factorial()
|
||||
/at the moment only Factorial/ you can give a pointer to
|
||||
the 'stop object', if the method WasStopSignal() of this
|
||||
object returns true that means we should break the calculating
|
||||
and return
|
||||
*/
|
||||
class StopCalculating
|
||||
{
|
||||
public:
|
||||
virtual bool WasStopSignal() const volatile { return false; }
|
||||
virtual ~StopCalculating(){}
|
||||
};
|
||||
|
||||
|
||||
/*!
|
||||
a small class which is useful when compiling with gcc
|
||||
|
||||
object of this type holds the name and the line of a file
|
||||
in which the macro TTMATH_ASSERT or TTMATH_REFERENCE_ASSERT was used
|
||||
*/
|
||||
class ExceptionInfo
|
||||
{
|
||||
const tt_char * file;
|
||||
int line;
|
||||
|
||||
public:
|
||||
ExceptionInfo() : file(0), line(0) {}
|
||||
ExceptionInfo(const tt_char * f, int l) : file(f), line(l) {}
|
||||
|
||||
tt_string Where() const
|
||||
{
|
||||
if( !file )
|
||||
return TTMATH_TEXT("unknown");
|
||||
|
||||
tt_ostringstream result;
|
||||
result << file << TTMATH_TEXT(":") << line;
|
||||
|
||||
return result.str();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/*!
|
||||
A small class used for reporting 'reference' errors
|
||||
|
||||
In the library is used macro TTMATH_REFERENCE_ASSERT which
|
||||
can throw an exception of this type
|
||||
|
||||
If you compile with gcc you can get a small benefit
|
||||
from using method Where() (it returns std::string (or std::wstring) with
|
||||
the name and the line of a file where the macro TTMATH_REFERENCE_ASSERT
|
||||
was used)
|
||||
|
||||
What is the 'reference' error?
|
||||
Some kind of methods use a reference as their argument to another object,
|
||||
and the another object not always can be the same which is calling, e.g.
|
||||
Big<1,2> foo(10);
|
||||
foo.Mul(foo); // this is incorrect
|
||||
above method Mul is making something more with 'this' object and
|
||||
'this' cannot be passed as the argument because the result will be undefined
|
||||
|
||||
macro TTMATH_REFERENCE_ASSERT helps us to solve the above problem
|
||||
|
||||
note! some methods can use 'this' object as the argument
|
||||
for example this code is correct:
|
||||
UInt<2> foo(10);
|
||||
foo.Add(foo);
|
||||
but there are only few methods which can do that
|
||||
*/
|
||||
class ReferenceError : public std::logic_error, public ExceptionInfo
|
||||
{
|
||||
public:
|
||||
|
||||
ReferenceError() : std::logic_error ("reference error")
|
||||
{
|
||||
}
|
||||
|
||||
ReferenceError(const tt_char * f, int l) :
|
||||
std::logic_error ("reference error"), ExceptionInfo(f,l)
|
||||
{
|
||||
}
|
||||
|
||||
tt_string Where() const
|
||||
{
|
||||
return ExceptionInfo::Where();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/*!
|
||||
a small class used for reporting errors
|
||||
|
||||
in the library is used macro TTMATH_ASSERT which
|
||||
(if the condition in it is false) throw an exception
|
||||
of this type
|
||||
|
||||
if you compile with gcc you can get a small benefit
|
||||
from using method Where() (it returns std::string (or std::wstring) with
|
||||
the name and the line of a file where the macro TTMATH_ASSERT
|
||||
was used)
|
||||
*/
|
||||
class RuntimeError : public std::runtime_error, public ExceptionInfo
|
||||
{
|
||||
public:
|
||||
|
||||
RuntimeError() : std::runtime_error ("internal error")
|
||||
{
|
||||
}
|
||||
|
||||
RuntimeError(const tt_char * f, int l) :
|
||||
std::runtime_error ("internal error"), ExceptionInfo(f,l)
|
||||
{
|
||||
}
|
||||
|
||||
tt_string Where() const
|
||||
{
|
||||
return ExceptionInfo::Where();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
/*!
|
||||
look at the description of macros TTMATH_RELEASE and TTMATH_DEBUG
|
||||
*/
|
||||
#ifdef TTMATH_DEBUG
|
||||
|
||||
#if defined(__FILE__) && defined(__LINE__)
|
||||
|
||||
#ifdef TTMATH_USE_WCHAR
|
||||
#define TTMATH_FILE_HELPER2(arg) L##arg
|
||||
#define TTMATH_FILE_HELPER(x) TTMATH_FILE_HELPER2(x)
|
||||
#define TTMATH_FILE TTMATH_FILE_HELPER(__FILE__)
|
||||
#else
|
||||
#define TTMATH_FILE __FILE__
|
||||
#endif
|
||||
|
||||
#define TTMATH_REFERENCE_ASSERT(expression) \
|
||||
if( &(expression) == this ) throw ttmath::ReferenceError(TTMATH_FILE, __LINE__);
|
||||
|
||||
#define TTMATH_ASSERT(expression) \
|
||||
if( !(expression) ) throw ttmath::RuntimeError(TTMATH_FILE, __LINE__);
|
||||
|
||||
#define TTMATH_VERIFY(expression) \
|
||||
if( !(expression) ) throw ttmath::RuntimeError(TTMATH_TEXT(__FILE__), __LINE__);
|
||||
|
||||
#else
|
||||
|
||||
#define TTMATH_REFERENCE_ASSERT(expression) \
|
||||
if( &(expression) == this ) throw ReferenceError();
|
||||
|
||||
#define TTMATH_ASSERT(expression) \
|
||||
if( !(expression) ) throw RuntimeError();
|
||||
|
||||
#define TTMATH_VERIFY(expression) \
|
||||
if( !(expression) ) throw RuntimeError();
|
||||
#endif
|
||||
|
||||
#else
|
||||
#define TTMATH_REFERENCE_ASSERT(expression)
|
||||
#define TTMATH_ASSERT(expression)
|
||||
#define TTMATH_VERIFY(expression) (void)(expression);
|
||||
#endif
|
||||
|
||||
#if !defined(LOG_PRINTF)
|
||||
#define LOG_PRINTF printf
|
||||
#endif
|
||||
|
||||
#ifdef TTMATH_DEBUG_LOG
|
||||
|
||||
#ifdef TTMATH_USE_WCHAR
|
||||
#define TTMATH_LOG_HELPER(msg) \
|
||||
PrintLog(L##msg, std::wcout);
|
||||
#else
|
||||
#define TTMATH_LOG_HELPER(msg) \
|
||||
PrintLog(msg, std::cout);
|
||||
#endif
|
||||
|
||||
#define TTMATH_LOG(msg) TTMATH_LOG_HELPER(msg)
|
||||
|
||||
#else
|
||||
|
||||
#define TTMATH_LOG(msg)
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
} // namespace
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
|
7033
ttmath/ttmathuint.h
7033
ttmath/ttmathuint.h
File diff suppressed because it is too large
Load Diff
939
ttmath/ttmathuint_noasm.h
Normal file
939
ttmath/ttmathuint_noasm.h
Normal file
@@ -0,0 +1,939 @@
|
||||
/*
|
||||
* This file is a part of TTMath Bignum Library
|
||||
* and is distributed under the (new) BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2006-2009, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* * Neither the name Tomasz Sowa nor the names of contributors to this
|
||||
* project may be used to endorse or promote products derived
|
||||
* from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
|
||||
* THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifndef headerfilettmathuint_noasm
|
||||
#define headerfilettmathuint_noasm
|
||||
|
||||
|
||||
#ifdef TTMATH_NOASM
|
||||
|
||||
#pragma message("TTMATH_NOASM")
|
||||
|
||||
/*!
|
||||
\file ttmathuint_noasm.h
|
||||
\brief template class UInt<uint> with methods without any assembler code
|
||||
|
||||
this file is included at the end of ttmathuint.h
|
||||
*/
|
||||
|
||||
|
||||
namespace ttmath
|
||||
{
|
||||
template<uint value_size>
|
||||
uint UInt<value_size>::AddTwoWords(uint a, uint b, uint carry, uint * result)
|
||||
{
|
||||
uint temp;
|
||||
|
||||
if( carry == 0 )
|
||||
{
|
||||
temp = a + b;
|
||||
|
||||
if( temp < a )
|
||||
carry = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
carry = 1;
|
||||
temp = a + b + carry;
|
||||
|
||||
if( temp > a ) // !(temp<=a)
|
||||
carry = 0;
|
||||
}
|
||||
|
||||
*result = temp;
|
||||
|
||||
return carry;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*!
|
||||
this method adding ss2 to the this and adding carry if it's defined
|
||||
(this = this + ss2 + c)
|
||||
|
||||
c must be zero or one (might be a bigger value than 1)
|
||||
function returns carry (1) (if it was)
|
||||
*/
|
||||
|
||||
template<uint value_size>
|
||||
uint UInt<value_size>::Add(const UInt<value_size> & ss2, uint c)
|
||||
{
|
||||
uint i;
|
||||
|
||||
for(i=0 ; i<value_size ; ++i)
|
||||
c = AddTwoWords(table[i], ss2.table[i], c, &table[i]);
|
||||
|
||||
TTMATH_LOG("UInt::Add")
|
||||
|
||||
return c;
|
||||
}
|
||||
|
||||
|
||||
/*!
|
||||
this method adds one word (at a specific position)
|
||||
and returns a carry (if it was)
|
||||
|
||||
if we've got (value_size=3):
|
||||
table[0] = 10;
|
||||
table[1] = 30;
|
||||
table[2] = 5;
|
||||
and we call:
|
||||
AddInt(2,1)
|
||||
then it'll be:
|
||||
table[0] = 10;
|
||||
table[1] = 30 + 2;
|
||||
table[2] = 5;
|
||||
|
||||
of course if there was a carry from table[2] it would be returned
|
||||
*/
|
||||
template<uint value_size>
|
||||
uint UInt<value_size>::AddInt(uint value, uint index)
|
||||
{
|
||||
uint i, c;
|
||||
|
||||
TTMATH_ASSERT( index < value_size )
|
||||
|
||||
|
||||
c = AddTwoWords(table[index], value, 0, &table[index]);
|
||||
|
||||
for(i=index+1 ; i<value_size && c ; ++i)
|
||||
c = AddTwoWords(table[i], 0, c, &table[i]);
|
||||
|
||||
TTMATH_LOG("UInt::AddInt")
|
||||
|
||||
return c;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/*!
|
||||
this method adds only two unsigned words to the existing value
|
||||
and these words begin on the 'index' position
|
||||
(it's used in the multiplication algorithm 2)
|
||||
|
||||
index should be equal or smaller than value_size-2 (index <= value_size-2)
|
||||
x1 - lower word, x2 - higher word
|
||||
|
||||
for example if we've got value_size equal 4 and:
|
||||
table[0] = 3
|
||||
table[1] = 4
|
||||
table[2] = 5
|
||||
table[3] = 6
|
||||
then let
|
||||
x1 = 10
|
||||
x2 = 20
|
||||
and
|
||||
index = 1
|
||||
|
||||
the result of this method will be:
|
||||
table[0] = 3
|
||||
table[1] = 4 + x1 = 14
|
||||
table[2] = 5 + x2 = 25
|
||||
table[3] = 6
|
||||
|
||||
and no carry at the end of table[3]
|
||||
|
||||
(of course if there was a carry in table[2](5+20) then
|
||||
this carry would be passed to the table[3] etc.)
|
||||
*/
|
||||
template<uint value_size>
|
||||
uint UInt<value_size>::AddTwoInts(uint x2, uint x1, uint index)
|
||||
{
|
||||
uint i, c;
|
||||
|
||||
TTMATH_ASSERT( index < value_size - 1 )
|
||||
|
||||
|
||||
c = AddTwoWords(table[index], x1, 0, &table[index]);
|
||||
c = AddTwoWords(table[index+1], x2, c, &table[index+1]);
|
||||
|
||||
for(i=index+2 ; i<value_size && c ; ++i)
|
||||
c = AddTwoWords(table[i], 0, c, &table[i]);
|
||||
|
||||
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)
|
||||
{
|
||||
if( carry == 0 )
|
||||
{
|
||||
*result = a - b;
|
||||
|
||||
if( a < b )
|
||||
carry = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
carry = 1;
|
||||
*result = a - b - carry;
|
||||
|
||||
if( a > b ) // !(a <= b )
|
||||
carry = 0;
|
||||
}
|
||||
|
||||
return carry;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/*!
|
||||
this method's subtracting ss2 from the 'this' and subtracting
|
||||
carry if it has been defined
|
||||
(this = this - ss2 - c)
|
||||
|
||||
c must be zero or one (might be a bigger value than 1)
|
||||
function returns carry (1) (if it was)
|
||||
*/
|
||||
template<uint value_size>
|
||||
uint UInt<value_size>::Sub(const UInt<value_size> & ss2, uint c)
|
||||
{
|
||||
uint i;
|
||||
|
||||
for(i=0 ; i<value_size ; ++i)
|
||||
c = SubTwoWords(table[i], ss2.table[i], c, &table[i]);
|
||||
|
||||
TTMATH_LOG("UInt::Sub")
|
||||
|
||||
return c;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/*!
|
||||
this method subtracts one word (at a specific position)
|
||||
and returns a carry (if it was)
|
||||
|
||||
if we've got (value_size=3):
|
||||
table[0] = 10;
|
||||
table[1] = 30;
|
||||
table[2] = 5;
|
||||
and we call:
|
||||
SubInt(2,1)
|
||||
then it'll be:
|
||||
table[0] = 10;
|
||||
table[1] = 30 - 2;
|
||||
table[2] = 5;
|
||||
|
||||
of course if there was a carry from table[2] it would be returned
|
||||
*/
|
||||
template<uint value_size>
|
||||
uint UInt<value_size>::SubInt(uint value, uint index)
|
||||
{
|
||||
uint i, c;
|
||||
|
||||
TTMATH_ASSERT( index < value_size )
|
||||
|
||||
|
||||
c = SubTwoWords(table[index], value, 0, &table[index]);
|
||||
|
||||
for(i=index+1 ; i<value_size && c ; ++i)
|
||||
c = SubTwoWords(table[i], 0, c, &table[i]);
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*!
|
||||
this method moves all bits into the left hand side
|
||||
return value <- this <- c
|
||||
|
||||
the lowest *bit* will be held the 'c' and
|
||||
the state of one additional bit (on the left hand side)
|
||||
will be returned
|
||||
|
||||
for example:
|
||||
let this is 001010000
|
||||
after Rcl2_one(1) there'll be 010100001 and Rcl2_one returns 0
|
||||
*/
|
||||
template<uint value_size>
|
||||
uint UInt<value_size>::Rcl2_one(uint c)
|
||||
{
|
||||
uint i, new_c;
|
||||
|
||||
if( c != 0 )
|
||||
c = 1;
|
||||
|
||||
for(i=0 ; i<value_size ; ++i)
|
||||
{
|
||||
new_c = (table[i] & TTMATH_UINT_HIGHEST_BIT) ? 1 : 0;
|
||||
table[i] = (table[i] << 1) | c;
|
||||
c = new_c;
|
||||
}
|
||||
|
||||
TTMATH_LOG("UInt::Rcl2_one")
|
||||
|
||||
return c;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/*!
|
||||
this method moves all bits into the right hand side
|
||||
c -> this -> return value
|
||||
|
||||
the highest *bit* will be held the 'c' and
|
||||
the state of one additional bit (on the right hand side)
|
||||
will be returned
|
||||
|
||||
for example:
|
||||
let this is 000000010
|
||||
after Rcr2_one(1) there'll be 100000001 and Rcr2_one returns 0
|
||||
*/
|
||||
template<uint value_size>
|
||||
uint UInt<value_size>::Rcr2_one(uint c)
|
||||
{
|
||||
sint i; // signed i
|
||||
uint new_c;
|
||||
|
||||
if( c != 0 )
|
||||
c = TTMATH_UINT_HIGHEST_BIT;
|
||||
|
||||
for(i=sint(value_size)-1 ; i>=0 ; --i)
|
||||
{
|
||||
new_c = (table[i] & 1) ? TTMATH_UINT_HIGHEST_BIT : 0;
|
||||
table[i] = (table[i] >> 1) | c;
|
||||
c = new_c;
|
||||
}
|
||||
|
||||
TTMATH_LOG("UInt::Rcr2_one")
|
||||
|
||||
return c;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/*!
|
||||
this method moves all bits into the left hand side
|
||||
return value <- this <- c
|
||||
|
||||
the lowest *bits* will be held the 'c' and
|
||||
the state of one additional bit (on the left hand side)
|
||||
will be returned
|
||||
|
||||
for example:
|
||||
let this is 001010000
|
||||
after Rcl2(3, 1) there'll be 010000111 and Rcl2 returns 1
|
||||
*/
|
||||
template<uint value_size>
|
||||
uint UInt<value_size>::Rcl2(uint bits, uint c)
|
||||
{
|
||||
TTMATH_ASSERT( bits>0 && bits<TTMATH_BITS_PER_UINT )
|
||||
|
||||
uint move = TTMATH_BITS_PER_UINT - bits;
|
||||
uint i, new_c;
|
||||
|
||||
if( c != 0 )
|
||||
c = TTMATH_UINT_MAX_VALUE >> move;
|
||||
|
||||
for(i=0 ; i<value_size ; ++i)
|
||||
{
|
||||
new_c = table[i] >> move;
|
||||
table[i] = (table[i] << bits) | c;
|
||||
c = new_c;
|
||||
}
|
||||
|
||||
TTMATH_LOG("UInt::Rcl2")
|
||||
|
||||
return (c & 1);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/*!
|
||||
this method moves all bits into the right hand side
|
||||
C -> this -> return value
|
||||
|
||||
the highest *bits* will be held the 'c' and
|
||||
the state of one additional bit (on the right hand side)
|
||||
will be returned
|
||||
|
||||
for example:
|
||||
let this is 000000010
|
||||
after Rcr2(2, 1) there'll be 110000000 and Rcr2 returns 1
|
||||
*/
|
||||
template<uint value_size>
|
||||
uint UInt<value_size>::Rcr2(uint bits, uint c)
|
||||
{
|
||||
TTMATH_ASSERT( bits>0 && bits<TTMATH_BITS_PER_UINT )
|
||||
|
||||
uint move = TTMATH_BITS_PER_UINT - bits;
|
||||
sint i; // signed
|
||||
uint new_c;
|
||||
|
||||
if( c != 0 )
|
||||
c = TTMATH_UINT_MAX_VALUE << move;
|
||||
|
||||
for(i=value_size-1 ; i>=0 ; --i)
|
||||
{
|
||||
new_c = table[i] << move;
|
||||
table[i] = (table[i] >> bits) | c;
|
||||
c = new_c;
|
||||
}
|
||||
|
||||
TTMATH_LOG("UInt::Rcr2")
|
||||
|
||||
return (c & TTMATH_UINT_HIGHEST_BIT) ? 1 : 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
this method returns the number of the highest set bit in x
|
||||
if the 'x' is zero this method returns '-1'
|
||||
|
||||
*/
|
||||
template<uint value_size>
|
||||
sint UInt<value_size>::FindLeadingBitInWord(uint x)
|
||||
{
|
||||
if( x == 0 )
|
||||
return -1;
|
||||
|
||||
uint bit = TTMATH_BITS_PER_UINT - 1;
|
||||
|
||||
while( (x & TTMATH_UINT_HIGHEST_BIT) == 0 )
|
||||
{
|
||||
x = x << 1;
|
||||
--bit;
|
||||
}
|
||||
|
||||
return bit;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/*!
|
||||
this method sets a special bit in the 'value'
|
||||
and returns the last state of the bit (zero or one)
|
||||
|
||||
bit is from <0,63>
|
||||
|
||||
e.g.
|
||||
uint x = 100;
|
||||
uint bit = SetBitInWord(x, 3);
|
||||
now: x = 108 and bit = 0
|
||||
*/
|
||||
template<uint value_size>
|
||||
uint UInt<value_size>::SetBitInWord(uint & value, uint bit)
|
||||
{
|
||||
TTMATH_ASSERT( bit < TTMATH_BITS_PER_UINT )
|
||||
|
||||
uint mask = 1;
|
||||
|
||||
if( bit > 1 )
|
||||
mask = mask << bit;
|
||||
|
||||
uint last = value & mask;
|
||||
value = value | mask;
|
||||
|
||||
return (last != 0) ? 1 : 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/*!
|
||||
*
|
||||
* Multiplication
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
/*!
|
||||
multiplication: result_high:result_low = a * b
|
||||
result_high - higher word of the result
|
||||
result_low - lower word of the result
|
||||
|
||||
this methos never returns a carry
|
||||
this method is used in the second version of the multiplication algorithms
|
||||
*/
|
||||
template<uint value_size>
|
||||
void UInt<value_size>::MulTwoWords(uint a, uint b, uint * result_high, uint * result_low)
|
||||
{
|
||||
#ifdef TTMATH_PLATFORM32
|
||||
|
||||
/*
|
||||
on 32bit platforms we have defined 'unsigned long long int' type known as 'ulint' in ttmath namespace
|
||||
this type has 64 bits, then we're using only one multiplication: 32bit * 32bit = 64bit
|
||||
*/
|
||||
|
||||
union uint_
|
||||
{
|
||||
struct
|
||||
{
|
||||
uint low; // 32 bits
|
||||
uint high; // 32 bits
|
||||
} u_;
|
||||
|
||||
ulint u; // 64 bits
|
||||
} res;
|
||||
|
||||
res.u = ulint(a) * ulint(b); // multiply two 32bit words, the result has 64 bits
|
||||
|
||||
*result_high = res.u_.high;
|
||||
*result_low = res.u_.low;
|
||||
|
||||
#else
|
||||
|
||||
/*
|
||||
64 bits platforms
|
||||
|
||||
we don't have a native type which has 128 bits
|
||||
then we're splitting 'a' and 'b' to 4 parts (high and low halves)
|
||||
and using 4 multiplications (with additions and carry correctness)
|
||||
*/
|
||||
|
||||
uint_ a_;
|
||||
uint_ b_;
|
||||
uint_ res_high1, res_high2;
|
||||
uint_ res_low1, res_low2;
|
||||
|
||||
a_.u = a;
|
||||
b_.u = b;
|
||||
|
||||
/*
|
||||
the multiplication is as follows (schoolbook algorithm with O(n^2) ):
|
||||
|
||||
32 bits 32 bits
|
||||
|
||||
+--------------------------------+
|
||||
| a_.u_.high | a_.u_.low |
|
||||
+--------------------------------+
|
||||
| b_.u_.high | b_.u_.low |
|
||||
+--------------------------------+--------------------------------+
|
||||
| res_high1.u | res_low1.u |
|
||||
+--------------------------------+--------------------------------+
|
||||
| res_high2.u | res_low2.u |
|
||||
+--------------------------------+--------------------------------+
|
||||
|
||||
64 bits 64 bits
|
||||
*/
|
||||
|
||||
|
||||
uint_ temp;
|
||||
|
||||
res_low1.u = uint(b_.u_.low) * uint(a_.u_.low);
|
||||
|
||||
temp.u = uint(res_low1.u_.high) + uint(b_.u_.low) * uint(a_.u_.high);
|
||||
res_low1.u_.high = temp.u_.low;
|
||||
res_high1.u_.low = temp.u_.high;
|
||||
res_high1.u_.high = 0;
|
||||
|
||||
res_low2.u_.low = 0;
|
||||
temp.u = uint(b_.u_.high) * uint(a_.u_.low);
|
||||
res_low2.u_.high = temp.u_.low;
|
||||
|
||||
res_high2.u = uint(b_.u_.high) * uint(a_.u_.high) + uint(temp.u_.high);
|
||||
|
||||
uint c = AddTwoWords(res_low1.u, res_low2.u, 0, &res_low2.u);
|
||||
AddTwoWords(res_high1.u, res_high2.u, c, &res_high2.u); // there is no carry from here
|
||||
|
||||
*result_high = res_high2.u;
|
||||
*result_low = res_low2.u;
|
||||
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/*!
|
||||
*
|
||||
* Division
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
/*!
|
||||
this method calculates 64bits word a:b / 32bits c (a higher, b lower word)
|
||||
r = a:b / c and rest - remainder
|
||||
|
||||
*
|
||||
* WARNING:
|
||||
* the c has to be suitably large for the result being keeped in one word,
|
||||
* if c is equal zero there'll be a hardware interruption (0)
|
||||
* and probably the end of your program
|
||||
*
|
||||
*/
|
||||
template<uint value_size>
|
||||
void UInt<value_size>::DivTwoWords(uint a, uint b, uint c, uint * r, uint * rest)
|
||||
{
|
||||
// (a < c ) for the result to be one word
|
||||
TTMATH_ASSERT( c != 0 && a < c )
|
||||
|
||||
#ifdef TTMATH_PLATFORM32
|
||||
|
||||
union
|
||||
{
|
||||
struct
|
||||
{
|
||||
uint low; // 32 bits
|
||||
uint high; // 32 bits
|
||||
} u_;
|
||||
|
||||
ulint u; // 64 bits
|
||||
} ab;
|
||||
|
||||
ab.u_.high = a;
|
||||
ab.u_.low = b;
|
||||
|
||||
*r = uint(ab.u / c);
|
||||
*rest = uint(ab.u % c);
|
||||
|
||||
#else
|
||||
|
||||
uint_ c_;
|
||||
c_.u = c;
|
||||
|
||||
|
||||
if( a == 0 )
|
||||
{
|
||||
*r = b / c;
|
||||
*rest = b % c;
|
||||
}
|
||||
else
|
||||
if( c_.u_.high == 0 )
|
||||
{
|
||||
// higher half of 'c' is zero
|
||||
// then higher half of 'a' is zero too (look at the asserts at the beginning - 'a' is smaller than 'c')
|
||||
uint_ a_, b_, res_, temp1, temp2;
|
||||
|
||||
a_.u = a;
|
||||
b_.u = b;
|
||||
|
||||
temp1.u_.high = a_.u_.low;
|
||||
temp1.u_.low = b_.u_.high;
|
||||
|
||||
res_.u_.high = temp1.u / c;
|
||||
temp2.u_.high = temp1.u % c;
|
||||
temp2.u_.low = b_.u_.low;
|
||||
|
||||
res_.u_.low = temp2.u / c;
|
||||
*rest = temp2.u % c;
|
||||
|
||||
*r = res_.u;
|
||||
}
|
||||
else
|
||||
{
|
||||
return DivTwoWords2(a, b, c, r, rest);
|
||||
}
|
||||
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
#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>
|
||||
void UInt<value_size>::DivTwoWords2(uint a, uint b, uint c, uint * r, uint * rest)
|
||||
{
|
||||
// a is not zero
|
||||
// c_.u_.high is not zero
|
||||
|
||||
uint_ a_, b_, c_, u_, q_;
|
||||
unsigned int u3; // 32 bit
|
||||
|
||||
a_.u = a;
|
||||
b_.u = b;
|
||||
c_.u = c;
|
||||
|
||||
// normalizing
|
||||
uint d = DivTwoWordsNormalize(a_, b_, c_);
|
||||
|
||||
// loop from j=1 to j=0
|
||||
// the first step (for j=2) is skipped because our result is only in one word,
|
||||
// (first 'q' were 0 and nothing would be changed)
|
||||
u_.u_.high = a_.u_.high;
|
||||
u_.u_.low = a_.u_.low;
|
||||
u3 = b_.u_.high;
|
||||
q_.u_.high = DivTwoWordsCalculate(u_, u3, c_);
|
||||
MultiplySubtract(u_, u3, q_.u_.high, c_);
|
||||
|
||||
u_.u_.high = u_.u_.low;
|
||||
u_.u_.low = u3;
|
||||
u3 = b_.u_.low;
|
||||
q_.u_.low = DivTwoWordsCalculate(u_, u3, c_);
|
||||
MultiplySubtract(u_, u3, q_.u_.low, c_);
|
||||
|
||||
*r = q_.u;
|
||||
|
||||
// unnormalizing for the remainder
|
||||
u_.u_.high = u_.u_.low;
|
||||
u_.u_.low = u3;
|
||||
*rest = DivTwoWordsUnnormalize(u_.u, d);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
template<uint value_size>
|
||||
uint UInt<value_size>::DivTwoWordsNormalize(uint_ & a_, uint_ & b_, uint_ & c_)
|
||||
{
|
||||
uint d = 0;
|
||||
|
||||
for( ; (c_.u & TTMATH_UINT_HIGHEST_BIT) == 0 ; ++d )
|
||||
{
|
||||
c_.u = c_.u << 1;
|
||||
|
||||
uint bc = b_.u & TTMATH_UINT_HIGHEST_BIT; // carry from 'b'
|
||||
|
||||
b_.u = b_.u << 1;
|
||||
a_.u = a_.u << 1; // carry bits from 'a' are simply skipped
|
||||
|
||||
if( bc )
|
||||
a_.u = a_.u | 1;
|
||||
}
|
||||
|
||||
return d;
|
||||
}
|
||||
|
||||
|
||||
template<uint value_size>
|
||||
uint UInt<value_size>::DivTwoWordsUnnormalize(uint u, uint d)
|
||||
{
|
||||
if( d == 0 )
|
||||
return u;
|
||||
|
||||
u = u >> d;
|
||||
|
||||
return u;
|
||||
}
|
||||
|
||||
|
||||
template<uint value_size>
|
||||
unsigned int UInt<value_size>::DivTwoWordsCalculate(uint_ u_, unsigned int u3, uint_ v_)
|
||||
{
|
||||
bool next_test;
|
||||
uint_ qp_, rp_, temp_;
|
||||
|
||||
qp_.u = u_.u / uint(v_.u_.high);
|
||||
rp_.u = u_.u % uint(v_.u_.high);
|
||||
|
||||
TTMATH_ASSERT( qp_.u_.high==0 || qp_.u_.high==1 )
|
||||
|
||||
do
|
||||
{
|
||||
bool decrease = false;
|
||||
|
||||
if( qp_.u_.high == 1 )
|
||||
decrease = true;
|
||||
else
|
||||
{
|
||||
temp_.u_.high = rp_.u_.low;
|
||||
temp_.u_.low = u3;
|
||||
|
||||
if( qp_.u * uint(v_.u_.low) > temp_.u )
|
||||
decrease = true;
|
||||
}
|
||||
|
||||
next_test = false;
|
||||
|
||||
if( decrease )
|
||||
{
|
||||
--qp_.u;
|
||||
rp_.u += v_.u_.high;
|
||||
|
||||
if( rp_.u_.high == 0 )
|
||||
next_test = true;
|
||||
}
|
||||
}
|
||||
while( next_test );
|
||||
|
||||
return qp_.u_.low;
|
||||
}
|
||||
|
||||
|
||||
template<uint value_size>
|
||||
void UInt<value_size>::MultiplySubtract(uint_ & u_, unsigned int & u3, unsigned int & q, uint_ v_)
|
||||
{
|
||||
uint_ temp_;
|
||||
|
||||
uint res_high;
|
||||
uint res_low;
|
||||
|
||||
MulTwoWords(v_.u, q, &res_high, &res_low);
|
||||
|
||||
uint_ sub_res_high_;
|
||||
uint_ sub_res_low_;
|
||||
|
||||
temp_.u_.high = u_.u_.low;
|
||||
temp_.u_.low = u3;
|
||||
|
||||
uint c = SubTwoWords(temp_.u, res_low, 0, &sub_res_low_.u);
|
||||
|
||||
temp_.u_.high = 0;
|
||||
temp_.u_.low = u_.u_.high;
|
||||
c = SubTwoWords(temp_.u, res_high, c, &sub_res_high_.u);
|
||||
|
||||
if( c )
|
||||
{
|
||||
--q;
|
||||
|
||||
c = AddTwoWords(sub_res_low_.u, v_.u, 0, &sub_res_low_.u);
|
||||
AddTwoWords(sub_res_high_.u, 0, c, &sub_res_high_.u);
|
||||
}
|
||||
|
||||
u_.u_.high = sub_res_high_.u_.low;
|
||||
u_.u_.low = sub_res_low_.u_.high;
|
||||
u3 = sub_res_low_.u_.low;
|
||||
}
|
||||
|
||||
#endif // #ifdef TTMATH_PLATFORM64
|
||||
|
||||
|
||||
|
||||
} //namespace
|
||||
|
||||
|
||||
#endif //ifdef TTMATH_NOASM
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
|
1371
ttmath/ttmathuint_x86.h
Normal file
1371
ttmath/ttmathuint_x86.h
Normal file
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
386
ttmath/ttmathuint_x86_amd64_msvc.asm
Normal file
386
ttmath/ttmathuint_x86_amd64_msvc.asm
Normal file
@@ -0,0 +1,386 @@
|
||||
PUBLIC adc_x64
|
||||
PUBLIC addindexed_x64
|
||||
PUBLIC addindexed2_x64
|
||||
|
||||
PUBLIC sbb_x64
|
||||
PUBLIC subindexed_x64
|
||||
|
||||
PUBLIC rcl_x64
|
||||
PUBLIC rcr_x64
|
||||
|
||||
PUBLIC rcl2_x64
|
||||
PUBLIC rcr2_x64
|
||||
|
||||
PUBLIC div_x64
|
||||
|
||||
;
|
||||
; "rax, rcx, rdx, r8-r11 are volatile."
|
||||
; "rbx, rbp, rdi, rsi, r12-r15 are nonvolatile."
|
||||
;
|
||||
|
||||
.CODE
|
||||
|
||||
ALIGN 8
|
||||
|
||||
;----------------------------------------
|
||||
|
||||
adc_x64 PROC
|
||||
; rcx = p1
|
||||
; rdx = p2
|
||||
; r8 = nSize
|
||||
; r9 = nCarry
|
||||
|
||||
xor rax, rax
|
||||
xor r11, r11
|
||||
sub rax, r9 ; sets CARRY if r9 != 0
|
||||
|
||||
ALIGN 16
|
||||
loop1:
|
||||
mov rax,qword ptr [rdx + r11 * 8]
|
||||
adc qword ptr [rcx + r11 * 8], rax
|
||||
lea r11, [r11+1]
|
||||
dec r8
|
||||
jnz loop1
|
||||
|
||||
setc al
|
||||
movzx rax, al
|
||||
|
||||
ret
|
||||
|
||||
adc_x64 ENDP
|
||||
|
||||
;----------------------------------------
|
||||
|
||||
ALIGN 8
|
||||
|
||||
;----------------------------------------
|
||||
|
||||
addindexed_x64 PROC
|
||||
|
||||
; rcx = p1
|
||||
; rdx = nSize
|
||||
; r8 = nPos
|
||||
; r9 = nValue
|
||||
|
||||
xor rax, rax ; rax = result
|
||||
sub rdx, r8 ; rdx = remaining count of uints
|
||||
|
||||
add qword ptr [rcx + r8 * 8], r9
|
||||
jc next1
|
||||
|
||||
ret
|
||||
|
||||
next1:
|
||||
mov r9, 1
|
||||
|
||||
ALIGN 16
|
||||
loop1:
|
||||
dec rdx
|
||||
jz done_with_cy
|
||||
lea r8, [r8+1]
|
||||
add qword ptr [rcx + r8 * 8], r9
|
||||
jc loop1
|
||||
|
||||
ret
|
||||
|
||||
done_with_cy:
|
||||
lea rax, [rax+1] ; rax = 1
|
||||
|
||||
ret
|
||||
|
||||
addindexed_x64 ENDP
|
||||
|
||||
;----------------------------------------
|
||||
|
||||
ALIGN 8
|
||||
|
||||
;----------------------------------------
|
||||
|
||||
addindexed2_x64 PROC
|
||||
|
||||
; rcx = p1 (pointer)
|
||||
; rdx = b (value size)
|
||||
; r8 = nPos
|
||||
; r9 = nValue1
|
||||
; [esp+0x28] = nValue2
|
||||
|
||||
xor rax, rax ; return value
|
||||
mov r11, rcx ; table
|
||||
sub rdx, r8 ; rdx = remaining count of uints
|
||||
mov r10, [esp+028h] ; r10 = nValue2
|
||||
|
||||
add qword ptr [r11 + r8 * 8], r9
|
||||
lea r8, [r8+1]
|
||||
lea rdx, [rdx-1]
|
||||
adc qword ptr [r11 + r8 * 8], r10
|
||||
jc next
|
||||
ret
|
||||
|
||||
ALIGN 16
|
||||
loop1:
|
||||
lea r8, [r8+1]
|
||||
add qword ptr [r11 + r8 * 8], 1
|
||||
jc next
|
||||
ret
|
||||
|
||||
next:
|
||||
dec rdx ; does not modify CY too...
|
||||
jnz loop1
|
||||
lea rax, [rax+1]
|
||||
ret
|
||||
|
||||
addindexed2_x64 ENDP
|
||||
|
||||
;----------------------------------------
|
||||
|
||||
ALIGN 8
|
||||
|
||||
;----------------------------------------
|
||||
|
||||
sbb_x64 PROC
|
||||
|
||||
; rcx = p1
|
||||
; rdx = p2
|
||||
; r8 = nCount
|
||||
; r9 = nCarry
|
||||
|
||||
xor rax, rax
|
||||
xor r11, r11
|
||||
sub rax, r9 ; sets CARRY if r9 != 0
|
||||
|
||||
ALIGN 16
|
||||
loop1:
|
||||
mov rax,qword ptr [rdx + r11 * 8]
|
||||
sbb qword ptr [rcx + r11 * 8], rax
|
||||
lea r11, [r11+1]
|
||||
dec r8
|
||||
jnz loop1
|
||||
|
||||
setc al
|
||||
movzx rax, al
|
||||
|
||||
ret
|
||||
|
||||
sbb_x64 ENDP
|
||||
|
||||
;----------------------------------------
|
||||
|
||||
ALIGN 8
|
||||
|
||||
;----------------------------------------
|
||||
|
||||
subindexed_x64 PROC
|
||||
; rcx = p1
|
||||
; rdx = nSize
|
||||
; r8 = nPos
|
||||
; r9 = nValue
|
||||
|
||||
sub rdx, r8 ; rdx = remaining count of uints
|
||||
|
||||
ALIGN 16
|
||||
loop1:
|
||||
sub qword ptr [rcx + r8 * 8], r9
|
||||
jnc done
|
||||
|
||||
lea r8, [r8+1]
|
||||
mov r9, 1
|
||||
dec rdx
|
||||
jnz loop1
|
||||
jc return_1 ; most of the times, there will be NO carry (I hope)
|
||||
|
||||
done:
|
||||
xor rax, rax
|
||||
ret
|
||||
|
||||
return_1:
|
||||
mov rax, 1
|
||||
ret
|
||||
|
||||
subindexed_x64 ENDP
|
||||
|
||||
;----------------------------------------
|
||||
|
||||
ALIGN 8
|
||||
|
||||
;----------------------------------------
|
||||
|
||||
rcl_x64 PROC
|
||||
; rcx = p1
|
||||
; rdx = b
|
||||
; r8 = nLowestBit
|
||||
|
||||
mov r11, rcx ; table
|
||||
xor r10, r10
|
||||
neg r8 ; CY set if r8 <> 0
|
||||
|
||||
ALIGN 16
|
||||
loop1:
|
||||
rcl qword ptr [r11 + r10 * 8], 1
|
||||
lea r10, [r10+1]
|
||||
dec rdx
|
||||
jnz loop1
|
||||
|
||||
setc al
|
||||
movzx rax, al
|
||||
|
||||
ret
|
||||
|
||||
rcl_x64 ENDP
|
||||
|
||||
;----------------------------------------
|
||||
|
||||
ALIGN 8
|
||||
|
||||
;----------------------------------------
|
||||
|
||||
rcr_x64 PROC
|
||||
; rcx = p1
|
||||
; rdx = nSize
|
||||
; r8 = nLowestBit
|
||||
|
||||
xor r10, r10
|
||||
neg r8 ; CY set if r8 <> 0
|
||||
|
||||
ALIGN 16
|
||||
loop1:
|
||||
rcr qword ptr -8[rcx + rdx * 8], 1
|
||||
dec rdx
|
||||
jnz loop1
|
||||
|
||||
setc al
|
||||
movzx rax, al
|
||||
|
||||
ret
|
||||
|
||||
rcr_x64 ENDP
|
||||
|
||||
;----------------------------------------
|
||||
|
||||
ALIGN 8
|
||||
|
||||
;----------------------------------------
|
||||
|
||||
div_x64 PROC
|
||||
|
||||
; rcx = &Hi
|
||||
; rdx = &Lo
|
||||
; r8 = nDiv
|
||||
|
||||
mov r11, rcx
|
||||
mov r10, rdx
|
||||
|
||||
mov rdx, qword ptr [r11]
|
||||
mov rax, qword ptr [r10]
|
||||
div r8
|
||||
mov qword ptr [r10], rdx ; remainder
|
||||
mov qword ptr [r11], rax ; value
|
||||
|
||||
ret
|
||||
|
||||
div_x64 ENDP
|
||||
|
||||
;----------------------------------------
|
||||
|
||||
ALIGN 8
|
||||
|
||||
;----------------------------------------
|
||||
|
||||
rcl2_x64 PROC
|
||||
; rcx = p1
|
||||
; rdx = nSize
|
||||
; r8 = bits
|
||||
; r9 = c
|
||||
|
||||
push rbx
|
||||
|
||||
mov r10, rcx ; r10 = p1
|
||||
xor rax, rax
|
||||
|
||||
mov rcx, 64
|
||||
sub rcx, r8
|
||||
|
||||
mov r11, -1
|
||||
shr r11, cl ; r11 = mask
|
||||
|
||||
mov rcx, r8 ; rcx = count of bits
|
||||
|
||||
mov rbx, rax ; rbx = old value = 0
|
||||
or r9, r9
|
||||
cmovnz rbx, r11 ; if (c) then old value = mask
|
||||
|
||||
mov r9, rax ; r9 = index (0..nSize-1)
|
||||
|
||||
ALIGN 16
|
||||
loop1:
|
||||
rol qword ptr [r10+r9*8], cl
|
||||
mov rax, qword ptr [r10+r9*8]
|
||||
and rax, r11
|
||||
xor qword ptr [r10+r9*8], rax
|
||||
or qword ptr [r10+r9*8], rbx
|
||||
mov rbx, rax
|
||||
|
||||
lea r9, [r9+1]
|
||||
dec rdx
|
||||
|
||||
jnz loop1
|
||||
|
||||
and rax, 1
|
||||
pop rbx
|
||||
ret
|
||||
|
||||
rcl2_x64 ENDP
|
||||
|
||||
;----------------------------------------
|
||||
|
||||
ALIGN 8
|
||||
|
||||
;----------------------------------------
|
||||
|
||||
rcr2_x64 PROC
|
||||
; rcx = p1
|
||||
; rdx = nSize
|
||||
; r8 = bits
|
||||
; r9 = c
|
||||
|
||||
push rbx
|
||||
mov r10, rcx ; r10 = p1
|
||||
xor rax, rax
|
||||
|
||||
mov rcx, 64
|
||||
sub rcx, r8
|
||||
|
||||
mov r11, -1
|
||||
shl r11, cl ; r11 = mask
|
||||
|
||||
mov rcx, r8 ; rcx = count of bits
|
||||
|
||||
mov rbx, rax ; rbx = old value = 0
|
||||
or r9, r9
|
||||
cmovnz rbx, r11 ; if (c) then old value = mask
|
||||
|
||||
mov r9, rdx ; r9 = index (0..nSize-1)
|
||||
lea r9, [r9-1]
|
||||
|
||||
ALIGN 16
|
||||
loop1:
|
||||
ror qword ptr [r10+r9*8], cl
|
||||
mov rax, qword ptr [r10+r9*8]
|
||||
and rax, r11
|
||||
xor qword ptr [r10+r9*8], rax
|
||||
or qword ptr [r10+r9*8], rbx
|
||||
mov rbx, rax
|
||||
|
||||
lea r9, [r9-1]
|
||||
dec rdx
|
||||
|
||||
jnz loop1
|
||||
|
||||
rol rax, 1
|
||||
and rax, 1
|
||||
pop rbx
|
||||
|
||||
ret
|
||||
|
||||
rcr2_x64 ENDP
|
||||
|
||||
END
|
Reference in New Issue
Block a user