TTMath  0.9.4
 C++ bignum library
Public Member Functions | Static Public Member Functions | Data Fields | Protected Member Functions | Friends
ttmath::Int< value_size > Class Template Reference

Int implements a big integer value with a sign. More...

#include <ttmathint.h>

Inheritance diagram for ttmath::Int< value_size >:
Inheritance graph
[legend]
Collaboration diagram for ttmath::Int< value_size >:
Collaboration graph
[legend]

Public Member Functions

void SetMax ()
 
void SetMin ()
 
void SetSignOne ()
 
uint ChangeSign ()
 
void SetSign ()
 
bool IsSign () const
 
uint Abs ()
 
uint Add (const Int< value_size > &ss2)
 
uint AddInt (uint value, uint index=0)
 
uint AddTwoInts (uint x2, uint x1, uint index)
 
uint Sub (const Int< value_size > &ss2)
 
uint SubInt (uint value, uint index=0)
 
uint AddOne ()
 
uint SubOne ()
 
uint MulInt (sint ss2)
 
uint Mul (Int< value_size > ss2)
 
uint Div (Int< value_size > ss2, Int< value_size > *remainder=0)
 
uint Div (const Int< value_size > &ss2, Int< value_size > &remainder)
 
uint DivInt (sint ss2, sint *remainder=0)
 
uint DivInt (sint ss2, sint &remainder)
 
uint Pow (Int< value_size > pow)
 
template<uint argument_size>
uint FromInt (const Int< argument_size > &p)
 
uint FromInt (sint value)
 
template<uint argument_size>
uint FromUInt (const UInt< argument_size > &p)
 
template<uint argument_size>
uint FromInt (const UInt< argument_size > &p)
 
uint FromUInt (uint value)
 
uint FromInt (uint value)
 
Int< value_size > & operator= (const Int< value_size > &p)
 
template<uint argument_size>
Int< value_size > & operator= (const Int< argument_size > &p)
 
Int< value_size > & operator= (sint i)
 
 Int (sint i)
 
 Int (const Int< value_size > &u)
 
template<uint argument_size>
 Int (const Int< argument_size > &u)
 
template<uint argument_size>
Int< value_size > & operator= (const UInt< argument_size > &p)
 
Int< value_size > & operator= (uint i)
 
 Int (uint i)
 
template<uint argument_size>
 Int (const UInt< argument_size > &u)
 
uint FromUInt (unsigned int i)
 
uint FromInt (unsigned int i)
 
uint FromInt (signed int i)
 
Int< value_size > & operator= (unsigned int i)
 
 Int (unsigned int i)
 
Int< value_size > & operator= (signed int i)
 
 Int (signed int i)
 
 Int (const char *s)
 
 Int (const std::string &s)
 
 Int (const wchar_t *s)
 
 Int (const std::wstring &s)
 
 Int ()
 
 ~Int ()
 
sint ToInt () const
 
uint ToUInt (uint &result) const
 
uint ToInt (uint &result) const
 
uint ToInt (sint &result) const
 
uint ToUInt (unsigned int &result) const
 
uint ToInt (unsigned int &result) const
 
uint ToInt (int &result) const
 
void ToString (std::string &result, uint b=10) const
 
std::string ToString (uint b=10) const
 
void ToString (std::wstring &result, uint b=10) const
 
std::wstring ToWString (uint b=10) const
 
uint FromString (const char *s, uint b=10, const char **after_source=0, bool *value_read=0)
 
uint FromString (const wchar_t *s, uint b=10, const wchar_t **after_source=0, bool *value_read=0)
 
uint FromString (const std::string &s, uint b=10)
 
Int< value_size > & operator= (const char *s)
 
uint FromString (const std::wstring &s, uint b=10)
 
Int< value_size > & operator= (const wchar_t *s)
 
Int< value_size > & operator= (const std::wstring &s)
 
Int< value_size > & operator= (const std::string &s)
 
bool operator== (const Int< value_size > &l) const
 
bool operator!= (const Int< value_size > &l) const
 
bool operator< (const Int< value_size > &l) const
 
bool operator> (const Int< value_size > &l) const
 
bool operator<= (const Int< value_size > &l) const
 
bool operator>= (const Int< value_size > &l) const
 
Int< value_size > operator- () const
 
Int< value_size > operator- (const Int< value_size > &p2) const
 
Int< value_size > & operator-= (const Int< value_size > &p2)
 
Int< value_size > operator+ (const Int< value_size > &p2) const
 
Int< value_size > & operator+= (const Int< value_size > &p2)
 
Int< value_size > operator* (const Int< value_size > &p2) const
 
Int< value_size > & operator*= (const Int< value_size > &p2)
 
Int< value_size > operator/ (const Int< value_size > &p2) const
 
Int< value_size > & operator/= (const Int< value_size > &p2)
 
Int< value_size > operator% (const Int< value_size > &p2) const
 
Int< value_size > & operator%= (const Int< value_size > &p2)
 
UInt< value_size > & operator++ ()
 
UInt< value_size > operator++ (int)
 
UInt< value_size > & operator-- ()
 
UInt< value_size > operator-- (int)
 
template<class ostream_type >
void PrintTable (ostream_type &output) const
 
template<class char_type , class ostream_type >
void PrintLog (const char_type *msg, ostream_type &output) const
 
template<class char_type , class ostream_type >
void PrintLog (const char_type *msg, uint carry, ostream_type &output) const
 
uint Size () const
 
void SetZero ()
 
void SetOne ()
 
void Swap (UInt< value_size > &ss2)
 
void SetFromTable (const unsigned int *temp_table, uint temp_table_len)
 
uint Rcl (uint bits, uint c=0)
 
uint Rcr (uint bits, uint c=0)
 
uint CompensationToLeft ()
 
bool FindLeadingBit (uint &table_id, uint &index) const
 
bool FindLowestBit (uint &table_id, uint &index) const
 
uint GetBit (uint bit_index) const
 
uint SetBit (uint bit_index)
 
void BitAnd (const UInt< value_size > &ss2)
 
void BitOr (const UInt< value_size > &ss2)
 
void BitXor (const UInt< value_size > &ss2)
 
void BitNot ()
 
void BitNot2 ()
 
uint MulInt (uint ss2)
 
template<uint result_size>
void MulInt (uint ss2, UInt< result_size > &result) const
 
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)
 
uint Mul1 (const UInt< value_size > &ss2)
 
void Mul1Big (const UInt< value_size > &ss2_, UInt< value_size *2 > &result)
 
uint Mul2 (const UInt< value_size > &ss2)
 
void Mul2Big (const UInt< value_size > &ss2, UInt< value_size *2 > &result)
 
uint Mul3 (const UInt< value_size > &ss2)
 
void Mul3Big (const UInt< value_size > &ss2, UInt< value_size *2 > &result)
 
uint MulFastest (const UInt< value_size > &ss2)
 
void MulFastestBig (const UInt< value_size > &ss2, UInt< value_size *2 > &result)
 
uint DivInt (uint divisor, uint *remainder=0)
 
uint DivInt (uint divisor, uint &remainder)
 
uint Div (const UInt< value_size > &divisor, UInt< value_size > *remainder=0, uint algorithm=3)
 
uint Div (const UInt< value_size > &divisor, UInt< value_size > &remainder, uint algorithm=3)
 
uint Div1 (const UInt< value_size > &divisor, UInt< value_size > *remainder=0)
 
uint Div1 (const UInt< value_size > &divisor, UInt< value_size > &remainder)
 
uint Div2 (const UInt< value_size > &divisor, UInt< value_size > *remainder=0)
 
uint Div2 (const UInt< value_size > &divisor, UInt< value_size > &remainder)
 
uint Div3 (const UInt< value_size > &ss2, UInt< value_size > *remainder=0)
 
uint Div3 (const UInt< value_size > &ss2, UInt< value_size > &remainder)
 
uint Pow (UInt< value_size > pow)
 
void Sqrt ()
 
void ClearFirstBits (uint n)
 
bool IsTheHighestBitSet () const
 
bool IsTheLowestBitSet () const
 
bool IsOnlyTheHighestBitSet () const
 
bool IsOnlyTheLowestBitSet () const
 
bool IsZero () const
 
bool AreFirstBitsZero (uint bits) const
 
uint ToUInt () const
 
template<class string_type >
void ToStringBase (string_type &result, uint b=10, bool negative=false) const
 
bool CmpSmaller (const UInt< value_size > &l, sint index=-1) const
 
bool CmpBigger (const UInt< value_size > &l, sint index=-1) const
 
bool CmpEqual (const UInt< value_size > &l, sint index=-1) const
 
bool CmpSmallerEqual (const UInt< value_size > &l, sint index=-1) const
 
bool CmpBiggerEqual (const UInt< value_size > &l, sint index=-1) const
 
bool operator< (const UInt< value_size > &l) const
 
bool operator> (const UInt< value_size > &l) const
 
bool operator== (const UInt< value_size > &l) const
 
bool operator!= (const UInt< value_size > &l) const
 
bool operator<= (const UInt< value_size > &l) const
 
bool operator>= (const UInt< value_size > &l) const
 
UInt< value_size > operator- (const UInt< value_size > &p2) const
 
UInt< value_size > & operator-= (const UInt< value_size > &p2)
 
UInt< value_size > operator+ (const UInt< value_size > &p2) const
 
UInt< value_size > & operator+= (const UInt< value_size > &p2)
 
UInt< value_size > operator* (const UInt< value_size > &p2) const
 
UInt< value_size > & operator*= (const UInt< value_size > &p2)
 
UInt< value_size > operator/ (const UInt< value_size > &p2) const
 
UInt< value_size > & operator/= (const UInt< value_size > &p2)
 
UInt< value_size > operator% (const UInt< value_size > &p2) const
 
UInt< value_size > & operator%= (const UInt< value_size > &p2)
 
UInt< value_size > operator~ () const
 
UInt< value_size > operator & (const UInt< value_size > &p2) const
 
UInt< value_size > & operator &= (const UInt< value_size > &p2)
 
UInt< value_size > operator| (const UInt< value_size > &p2) const
 
UInt< value_size > & operator|= (const UInt< value_size > &p2)
 
UInt< value_size > operator^ (const UInt< value_size > &p2) const
 
UInt< value_size > & operator^= (const UInt< value_size > &p2)
 
UInt< value_size > operator>> (int move) const
 
UInt< value_size > & operator>>= (int move)
 
UInt< value_size > operator<< (int move) const
 
UInt< value_size > & operator<<= (int move)
 
uint Add (const UInt< value_size > &ss2, uint c=0)
 
uint Sub (const UInt< value_size > &ss2, uint c=0)
 

Static Public Member Functions

template<class char_type , class ostream_type >
static void PrintVectorLog (const char_type *msg, ostream_type &output, const uint *vector, uint vector_len)
 
template<class char_type , class ostream_type >
static void PrintVectorLog (const char_type *msg, uint carry, ostream_type &output, const uint *vector, uint vector_len)
 
static uint AddTwoWords (uint a, uint b, uint carry, uint *result)
 
static uint SubTwoWords (uint a, uint b, uint carry, uint *result)
 
static void DivTwoWords2 (uint a, uint b, uint c, uint *r, uint *rest)
 
static uint DivTwoWordsNormalize (uint_ &a_, uint_ &b_, uint_ &c_)
 
static uint DivTwoWordsUnnormalize (uint u, uint d)
 
static unsigned int DivTwoWordsCalculate (uint_ u_, unsigned int u3, uint_ v_)
 
static void MultiplySubtract (uint_ &u_, unsigned int &u3, unsigned int &q, uint_ v_)
 
static const char * LibTypeStr ()
 
static LibTypeCode LibType ()
 
static uint AddVector (const uint *ss1, const uint *ss2, uint ss1_size, uint ss2_size, uint *result)
 
static uint SubVector (const uint *ss1, const uint *ss2, uint ss1_size, uint ss2_size, uint *result)
 
static sint FindLeadingBitInWord (uint x)
 
static sint FindLowestBitInWord (uint x)
 
static uint SetBitInWord (uint &value, uint bit)
 
static void MulTwoWords (uint a, uint b, uint *result_high, uint *result_low)
 
static void DivTwoWords (uint a, uint b, uint c, uint *r, uint *rest)
 

Data Fields

uint table [value_size]
 

Protected Member Functions

double ToStringLog2 (uint x) const
 

Friends

std::ostream & operator<< (std::ostream &s, const Int< value_size > &l)
 
std::wostream & operator<< (std::wostream &s, const Int< value_size > &l)
 
std::istream & operator>> (std::istream &s, Int< value_size > &l)
 
std::wistream & operator>> (std::wistream &s, Int< value_size > &l)
 

Detailed Description

template<uint value_size>
class ttmath::Int< value_size >

Int implements a big integer value with a sign.

value_size - how many bytes specify our value

Definition at line 63 of file ttmathint.h.

Constructor & Destructor Documentation

§ Int() [1/12]

template<uint value_size>
ttmath::Int< value_size >::Int ( sint  i)

a constructor for converting the uint to this class

Definition at line 807 of file ttmathint.h.

808  {
809  FromInt(i);
810  }
uint FromInt(const Int< argument_size > &p)
Definition: ttmathint.h:697

§ Int() [2/12]

template<uint value_size>
ttmath::Int< value_size >::Int ( const Int< value_size > &  u)

a copy constructor

Definition at line 816 of file ttmathint.h.

816  : UInt<value_size>()
817  {
818  FromInt(u);
819  }
uint FromInt(const Int< argument_size > &p)
Definition: ttmathint.h:697

§ Int() [3/12]

template<uint value_size>
template<uint argument_size>
ttmath::Int< value_size >::Int ( const Int< argument_size > &  u)

a constructor for copying from another types

Definition at line 826 of file ttmathint.h.

827  {
828  // look that 'size' we still set as 'value_size' and not as u.value_size
829  FromInt(u);
830  }
uint FromInt(const Int< argument_size > &p)
Definition: ttmathint.h:697

§ Int() [4/12]

template<uint value_size>
ttmath::Int< value_size >::Int ( uint  i)

a constructor for converting the uint to this class

Definition at line 862 of file ttmathint.h.

863  {
864  FromUInt(i);
865  }
uint FromUInt(const UInt< argument_size > &p)
Definition: ttmathint.h:724

§ Int() [5/12]

template<uint value_size>
template<uint argument_size>
ttmath::Int< value_size >::Int ( const UInt< argument_size > &  u)

a constructor for copying from another types

Definition at line 872 of file ttmathint.h.

873  {
874  // look that 'size' we still set as 'value_size' and not as u.value_size
875  FromUInt(u);
876  }
uint FromUInt(const UInt< argument_size > &p)
Definition: ttmathint.h:724

§ Int() [6/12]

template<uint value_size>
ttmath::Int< value_size >::Int ( unsigned int  i)

a constructor for converting 32 bit unsigned int to this class this constructor is created only on a 64bit platform***

Definition at line 1037 of file ttmathint.h.

1038  {
1039  FromUInt(i);
1040  }
uint FromUInt(const UInt< argument_size > &p)
Definition: ttmathint.h:724

§ Int() [7/12]

template<uint value_size>
ttmath::Int< value_size >::Int ( signed int  i)

a constructor for converting 32 bit signed int to this class this constructor is created only on a 64bit platform***

Definition at line 1059 of file ttmathint.h.

1060  {
1061  FromInt(i);
1062  }
uint FromInt(const Int< argument_size > &p)
Definition: ttmathint.h:697

§ Int() [8/12]

template<uint value_size>
ttmath::Int< value_size >::Int ( const char *  s)

a constructor for converting string to this class (with the base=10)

Definition at line 1071 of file ttmathint.h.

1072  {
1073  FromString(s);
1074  }
uint FromString(const char *s, uint b=10, const char **after_source=0, bool *value_read=0)
Definition: ttmathint.h:1453

§ Int() [9/12]

template<uint value_size>
ttmath::Int< value_size >::Int ( const std::string &  s)

a constructor for converting a string to this class (with the base=10)

Definition at line 1080 of file ttmathint.h.

1081  {
1082  FromString( s.c_str() );
1083  }
uint FromString(const char *s, uint b=10, const char **after_source=0, bool *value_read=0)
Definition: ttmathint.h:1453

§ Int() [10/12]

template<uint value_size>
ttmath::Int< value_size >::Int ( const wchar_t *  s)

a constructor for converting string to this class (with the base=10)

Definition at line 1091 of file ttmathint.h.

1092  {
1093  FromString(s);
1094  }
uint FromString(const char *s, uint b=10, const char **after_source=0, bool *value_read=0)
Definition: ttmathint.h:1453

§ Int() [11/12]

template<uint value_size>
ttmath::Int< value_size >::Int ( const std::wstring &  s)

a constructor for converting a string to this class (with the base=10)

Definition at line 1100 of file ttmathint.h.

1101  {
1102  FromString( s.c_str() );
1103  }
uint FromString(const char *s, uint b=10, const char **after_source=0, bool *value_read=0)
Definition: ttmathint.h:1453

§ Int() [12/12]

template<uint value_size>
ttmath::Int< value_size >::Int ( )

a default constructor

we don't clear table etc.

Definition at line 1113 of file ttmathint.h.

Referenced by ttmath::Int< exp >::Int().

1114  {
1115  }

§ ~Int()

template<uint value_size>
ttmath::Int< value_size >::~Int ( )

the destructor

Definition at line 1121 of file ttmathint.h.

1122  {
1123  }

Member Function Documentation

§ Abs()

template<uint value_size>
uint ttmath::Int< value_size >::Abs ( )

it sets an absolute value

it can return carry (1) (look on ChangeSign() for details)

Definition at line 168 of file ttmathint.h.

Referenced by ttmath::Big< exp, man >::Add(), ttmath::Big< exp, man >::BitAnd(), ttmath::Big< exp, man >::BitOr(), ttmath::Big< exp, man >::BitXor(), ttmath::Int< exp >::Div(), ttmath::Int< exp >::DivInt(), ttmath::Int< exp >::Mul(), ttmath::Int< exp >::MulInt(), and ttmath::Int< exp >::ToInt().

169  {
170  if( !IsSign() )
171  return 0;
172 
173  return ChangeSign();
174  }
bool IsSign() const
Definition: ttmathint.h:156
uint ChangeSign()
Definition: ttmathint.h:105

§ Add() [1/2]

template<uint value_size>
uint ttmath::Int< value_size >::Add ( const Int< value_size > &  ss2)

this method adds two value with a sign and returns a carry

we're using methods from the base class because values are stored with U2 we must only make the carry correction

this = p1(=this) + p2

when p1>=0 i p2>=0 carry is set when the highest bit of value is set when p1<0 i p2<0 carry is set when the highest bit of value is clear when p1>=0 i p2<0 carry will never be set when p1<0 i p2>=0 carry will never be set

Definition at line 220 of file ttmathint.h.

Referenced by ttmath::Big< exp, man >::Exp(), ttmath::Big< exp, man >::MulUInt(), and ttmath::Int< exp >::operator-().

221  {
222  bool p1_is_sign = IsSign();
223  bool p2_is_sign = ss2.IsSign();
224 
225  UInt<value_size>::Add(ss2);
226 
227  return CorrectCarryAfterAdding(p1_is_sign, p2_is_sign);
228  }
uint Add(const UInt< value_size > &ss2, uint c=0)
bool IsSign() const
Definition: ttmathint.h:156

§ Add() [2/2]

template<uint value_size>
uint ttmath::UInt< value_size >::Add ( const UInt< value_size > &  ss2,
uint  c = 0 
)
inherited

basic mathematic functions

this method adding ss2 to the this and adding carry if it's defined (this = this + ss2 + c)

this method is created only on a 64bit platform***

c must be zero or one (might be a bigger value than 1) function returns carry (1) (if it was)

Definition at line 158 of file ttmathuint_x86_64.h.

Referenced by ttmath::Int< exp >::Add(), ttmath::UInt< man >::Div1(), ttmath::UInt< man >::MulBig(), ttmath::UInt< man >::operator-(), ttmath::UInt< man >::Sqrt(), and ttmath::UInt< man >::ToString().

159  {
160  uint b = value_size;
161  uint * p1 = table;
162  const uint * p2 = ss2.table;
163 
164  // we don't have to use TTMATH_REFERENCE_ASSERT here
165  // this algorithm doesn't require it
166 
167  #ifndef __GNUC__
168  c = ttmath_adc_x64(p1,p2,b,c);
169  #endif
170 
171  #ifdef __GNUC__
172  uint dummy, dummy2;
173 
174  /*
175  this part should be compiled with gcc
176  */
177  __asm__ __volatile__(
178 
179  "xorq %%rdx, %%rdx \n"
180  "negq %%rax \n" // CF=1 if rax!=0 , CF=0 if rax==0
181 
182  "1: \n"
183  "movq (%%rsi,%%rdx,8), %%rax \n"
184  "adcq %%rax, (%%rbx,%%rdx,8) \n"
185 
186  "incq %%rdx \n"
187  "decq %%rcx \n"
188  "jnz 1b \n"
189 
190  "adcq %%rcx, %%rcx \n"
191 
192  : "=c" (c), "=a" (dummy), "=d" (dummy2)
193  : "0" (b), "1" (c), "b" (p1), "S" (p2)
194  : "cc", "memory" );
195 
196  #endif
197 
198  TTMATH_LOGC("UInt::Add", c)
199 
200  return c;
201  }
uint table[value_size]
Definition: ttmathuint.h:81
unsigned long uint
Definition: ttmathtypes.h:238

§ AddInt()

template<uint value_size>
uint ttmath::Int< value_size >::AddInt ( uint  value,
uint  index = 0 
)

this method adds one unsigned word (at a specific position) and returns a carry (if it was)

look at a description in UInt<>::AddInt(...)

Definition at line 237 of file ttmathint.h.

Referenced by ttmath::Big< exp, man >::FromBig().

238  {
239  bool p1_is_sign = IsSign();
240 
241  UInt<value_size>::AddInt(value, index);
242 
243  return CorrectCarryAfterAdding(p1_is_sign, false);
244  }
uint AddInt(uint value, uint index=0)
bool IsSign() const
Definition: ttmathint.h:156

§ AddOne()

template<uint value_size>
uint ttmath::Int< value_size >::AddOne ( )

this method adds one to the value and returns carry

Definition at line 327 of file ttmathint.h.

Referenced by ttmath::Big< exp, man >::ChangeSign(), ttmath::Big< exp, man >::Exp(), and ttmath::Int< exp >::operator++().

328  {
329  bool p1_is_sign = IsSign();
330 
332 
333  return CorrectCarryAfterAdding(p1_is_sign, false);
334  }
bool IsSign() const
Definition: ttmathint.h:156
uint AddOne()
Definition: ttmathuint.h:386

§ AddTwoInts()

template<uint value_size>
uint ttmath::Int< value_size >::AddTwoInts ( uint  x2,
uint  x1,
uint  index 
)

this method adds two unsigned words to the existing value and these words begin on the 'index' position

index should be equal or smaller than value_size-2 (index <= value_size-2) x1 - lower word, x2 - higher word

look at a description in UInt<>::AddTwoInts(...)

Definition at line 256 of file ttmathint.h.

257  {
258  bool p1_is_sign = IsSign();
259 
260  UInt<value_size>::AddTwoInts(x2, x1, index);
261 
262  return CorrectCarryAfterAdding(p1_is_sign, false);
263  }
uint AddTwoInts(uint x2, uint x1, uint index)
bool IsSign() const
Definition: ttmathint.h:156

§ AddVector()

template<uint value_size>
uint ttmath::UInt< value_size >::AddVector ( const uint ss1,
const uint ss2,
uint  ss1_size,
uint  ss2_size,
uint result 
)
staticinherited

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)

Definition at line 387 of file ttmathuint_x86_64.h.

388  {
389  TTMATH_ASSERT( ss1_size >= ss2_size )
390 
391  uint c;
392 
393  #ifndef __GNUC__
394  c = ttmath_addvector_x64(ss1, ss2, ss1_size, ss2_size, result);
395  #endif
396 
397 
398  #ifdef __GNUC__
399  uint dummy1, dummy2, dummy3;
400  uint rest = ss1_size - ss2_size;
401 
402  // this part should be compiled with gcc
403 
404  __asm__ __volatile__(
405  "mov %%rdx, %%r8 \n"
406  "xor %%rdx, %%rdx \n" // rdx = 0, cf = 0
407  "1: \n"
408  "mov (%%rsi,%%rdx,8), %%rax \n"
409  "adc (%%rbx,%%rdx,8), %%rax \n"
410  "mov %%rax, (%%rdi,%%rdx,8) \n"
411 
412  "inc %%rdx \n"
413  "dec %%rcx \n"
414  "jnz 1b \n"
415 
416  "adc %%rcx, %%rcx \n" // rcx has the cf state
417 
418  "or %%r8, %%r8 \n"
419  "jz 3f \n"
420 
421  "xor %%rbx, %%rbx \n" // ebx = 0
422  "neg %%rcx \n" // setting cf from rcx
423  "mov %%r8, %%rcx \n" // rcx=rest and is != 0
424  "2: \n"
425  "mov (%%rsi, %%rdx, 8), %%rax \n"
426  "adc %%rbx, %%rax \n"
427  "mov %%rax, (%%rdi, %%rdx, 8) \n"
428 
429  "inc %%rdx \n"
430  "dec %%rcx \n"
431  "jnz 2b \n"
432 
433  "adc %%rcx, %%rcx \n"
434  "3: \n"
435 
436  : "=a" (dummy1), "=b" (dummy2), "=c" (c), "=d" (dummy3)
437  : "1" (ss2), "2" (ss2_size), "3" (rest), "S" (ss1), "D" (result)
438  : "%r8", "cc", "memory" );
439 
440  #endif
441 
442  TTMATH_VECTOR_LOGC("UInt::AddVector", c, result, ss1_size)
443 
444  return c;
445  }
unsigned long uint
Definition: ttmathtypes.h:238

§ AreFirstBitsZero()

template<uint value_size>
bool ttmath::UInt< value_size >::AreFirstBitsZero ( uint  bits) const
inherited

returning true if first 'bits' bits are equal zero

Definition at line 2618 of file ttmathuint.h.

2619  {
2620  TTMATH_ASSERT( bits <= value_size * TTMATH_BITS_PER_UINT )
2621 
2622  uint index = bits / TTMATH_BITS_PER_UINT;
2623  uint rest = bits % TTMATH_BITS_PER_UINT;
2624  uint i;
2625 
2626  for(i=0 ; i<index ; ++i)
2627  if(table[i] != 0 )
2628  return false;
2629 
2630  if( rest == 0 )
2631  return true;
2632 
2633  uint mask = TTMATH_UINT_MAX_VALUE >> (TTMATH_BITS_PER_UINT - rest);
2634 
2635  return (table[i] & mask) == 0;
2636  }
uint table[value_size]
Definition: ttmathuint.h:81
#define TTMATH_UINT_MAX_VALUE
Definition: ttmathtypes.h:264
#define TTMATH_BITS_PER_UINT
Definition: ttmathtypes.h:253
unsigned long uint
Definition: ttmathtypes.h:238

§ BitAnd()

template<uint value_size>
void ttmath::UInt< value_size >::BitAnd ( const UInt< value_size > &  ss2)
inherited

this method performs a bitwise operation AND

Definition at line 743 of file ttmathuint.h.

Referenced by ttmath::Big< exp, man >::BitAnd(), and ttmath::UInt< man >::operator~().

744  {
745  for(uint x=0 ; x<value_size ; ++x)
746  table[x] &= ss2.table[x];
747 
748  TTMATH_LOG("UInt::BitAnd")
749  }
uint table[value_size]
Definition: ttmathuint.h:81
unsigned long uint
Definition: ttmathtypes.h:238

§ BitNot()

template<uint value_size>
void ttmath::UInt< value_size >::BitNot ( )
inherited

this method performs a bitwise operation NOT

Definition at line 779 of file ttmathuint.h.

Referenced by ttmath::UInt< man >::operator~().

780  {
781  for(uint x=0 ; x<value_size ; ++x)
782  table[x] = ~table[x];
783 
784  TTMATH_LOG("UInt::BitNot")
785  }
uint table[value_size]
Definition: ttmathuint.h:81
unsigned long uint
Definition: ttmathtypes.h:238

§ BitNot2()

template<uint value_size>
void ttmath::UInt< value_size >::BitNot2 ( )
inherited

this method performs a bitwise operation NOT but only on the range of <0, leading_bit>

for example: BitNot2(8) = BitNot2( 1000(bin) ) = 111(bin) = 7

Definition at line 795 of file ttmathuint.h.

796  {
797  uint table_id, index;
798 
799  if( FindLeadingBit(table_id, index) )
800  {
801  for(uint x=0 ; x<table_id ; ++x)
802  table[x] = ~table[x];
803 
805  uint shift = TTMATH_BITS_PER_UINT - index - 1;
806 
807  if(shift)
808  mask >>= shift;
809 
810  table[table_id] ^= mask;
811  }
812  else
813  table[0] = 1;
814 
815 
816  TTMATH_LOG("UInt::BitNot2")
817  }
uint table[value_size]
Definition: ttmathuint.h:81
#define TTMATH_UINT_MAX_VALUE
Definition: ttmathtypes.h:264
#define TTMATH_BITS_PER_UINT
Definition: ttmathtypes.h:253
unsigned long uint
Definition: ttmathtypes.h:238
bool FindLeadingBit(uint &table_id, uint &index) const
Definition: ttmathuint.h:649

§ BitOr()

template<uint value_size>
void ttmath::UInt< value_size >::BitOr ( const UInt< value_size > &  ss2)
inherited

this method performs a bitwise operation OR

Definition at line 755 of file ttmathuint.h.

Referenced by ttmath::Big< exp, man >::BitOr(), and ttmath::UInt< man >::operator~().

756  {
757  for(uint x=0 ; x<value_size ; ++x)
758  table[x] |= ss2.table[x];
759 
760  TTMATH_LOG("UInt::BitOr")
761  }
uint table[value_size]
Definition: ttmathuint.h:81
unsigned long uint
Definition: ttmathtypes.h:238

§ BitXor()

template<uint value_size>
void ttmath::UInt< value_size >::BitXor ( const UInt< value_size > &  ss2)
inherited

this method performs a bitwise operation XOR

Definition at line 767 of file ttmathuint.h.

Referenced by ttmath::Big< exp, man >::BitXor(), and ttmath::UInt< man >::operator~().

768  {
769  for(uint x=0 ; x<value_size ; ++x)
770  table[x] ^= ss2.table[x];
771 
772  TTMATH_LOG("UInt::BitXor")
773  }
uint table[value_size]
Definition: ttmathuint.h:81
unsigned long uint
Definition: ttmathtypes.h:238

§ ChangeSign()

template<uint value_size>
uint ttmath::Int< value_size >::ChangeSign ( )

we change the sign of the value

if it isn't possible to change the sign this method returns 1 else return 0 and changing the sign

Definition at line 105 of file ttmathint.h.

Referenced by ttmath::Int< exp >::Abs(), ttmath::Int< exp >::DivInt(), ttmath::Big< exp, man >::FromInt(), ttmath::Int< exp >::operator-(), ttmath::Int< exp >::Pow(), ttmath::Big< exp, man >::Pow(), ttmath::Int< exp >::SetSign(), ttmath::Big< exp, man >::ToInt(), and ttmath::Int< exp >::ToWString().

106  {
107  /*
108  if the value is equal that one which has been returned from SetMin
109  (only the highest bit is set) that means we can't change sign
110  because the value is too big (bigger about one)
111 
112  e.g. when value_size = 1 and value is -2147483648 we can't change it to the
113  2147483648 because the max value which can be held is 2147483647
114 
115  we don't change the value and we're using this fact somewhere in some methods
116  (if we look on our value without the sign we get the correct value
117  eg. -2147483648 in Int<1> will be 2147483648 on the UInt<1> type)
118  */
120  return 1;
121 
122  UInt<value_size> temp(*this);
124  UInt<value_size>::Sub(temp);
125 
126  return 0;
127  }
uint Sub(const UInt< value_size > &ss2, uint c=0)
bool IsOnlyTheHighestBitSet() const
Definition: ttmathuint.h:2565
void SetZero()
Definition: ttmathuint.h:188

§ ClearFirstBits()

template<uint value_size>
void ttmath::UInt< value_size >::ClearFirstBits ( uint  n)
inherited

this method sets n first bits to value zero

For example: let n=2 then if there's a value 111 (bin) there'll be '100' (bin)

Definition at line 2508 of file ttmathuint.h.

Referenced by ttmath::Big< exp, man >::SkipFraction().

2509  {
2510  if( n >= value_size*TTMATH_BITS_PER_UINT )
2511  {
2512  SetZero();
2513  TTMATH_LOG("UInt::ClearFirstBits")
2514  return;
2515  }
2516 
2517  uint * p = table;
2518 
2519  // first we're clearing the whole words
2520  while( n >= TTMATH_BITS_PER_UINT )
2521  {
2522  *p++ = 0;
2523  n -= TTMATH_BITS_PER_UINT;
2524  }
2525 
2526  if( n == 0 )
2527  {
2528  TTMATH_LOG("UInt::ClearFirstBits")
2529  return;
2530  }
2531 
2532  // and then we're clearing one word which has left
2533  // mask -- all bits are set to one
2534  uint mask = TTMATH_UINT_MAX_VALUE;
2535 
2536  mask = mask << n;
2537 
2538  (*p) &= mask;
2539 
2540  TTMATH_LOG("UInt::ClearFirstBits")
2541  }
uint table[value_size]
Definition: ttmathuint.h:81
void SetZero()
Definition: ttmathuint.h:188
#define TTMATH_UINT_MAX_VALUE
Definition: ttmathtypes.h:264
#define TTMATH_BITS_PER_UINT
Definition: ttmathtypes.h:253
unsigned long uint
Definition: ttmathtypes.h:238
void ClearFirstBits(uint n)
Definition: ttmathuint.h:2508

§ CmpBigger()

template<uint value_size>
bool ttmath::UInt< value_size >::CmpBigger ( const UInt< value_size > &  l,
sint  index = -1 
) const
inherited

this method returns true if 'this' is bigger than 'l'

'index' is an index of the first word from will be the comparison performed (note: we start the comparison from back - from the last word, when index is -1 /default/ it is automatically set into the last word)

I introduced it for some kind of optimization made in the second division algorithm (Div2)

Definition at line 3606 of file ttmathuint.h.

Referenced by ttmath::UInt< man >::CmpBiggerEqual().

3607  {
3608  sint i;
3609 
3610  if( index==-1 || index>=sint(value_size) )
3611  i = value_size - 1;
3612  else
3613  i = index;
3614 
3615 
3616  for( ; i>=0 ; --i)
3617  {
3618  if( table[i] != l.table[i] )
3619  return table[i] > l.table[i];
3620  }
3621 
3622  // they're equal
3623  return false;
3624  }
signed long sint
Definition: ttmathtypes.h:243
uint table[value_size]
Definition: ttmathuint.h:81

§ CmpBiggerEqual()

template<uint value_size>
bool ttmath::UInt< value_size >::CmpBiggerEqual ( const UInt< value_size > &  l,
sint  index = -1 
) const
inherited

this method returns true if 'this' is bigger than or equal 'l'

'index' is an index of the first word from will be the comparison performed (note: we start the comparison from back - from the last word, when index is -1 /default/ it is automatically set into the last word)

Definition at line 3689 of file ttmathuint.h.

Referenced by ttmath::UInt< man >::CmpBiggerEqual(), and ttmath::UInt< man >::Div2().

3690  {
3691  sint i;
3692 
3693  if( index==-1 || index>=sint(value_size) )
3694  i = value_size - 1;
3695  else
3696  i = index;
3697 
3698 
3699  for( ; i>=0 ; --i)
3700  {
3701  if( table[i] != l.table[i] )
3702  return table[i] > l.table[i];
3703  }
3704 
3705  // they're equal
3706  return true;
3707  }
signed long sint
Definition: ttmathtypes.h:243
uint table[value_size]
Definition: ttmathuint.h:81

§ CmpEqual()

template<uint value_size>
bool ttmath::UInt< value_size >::CmpEqual ( const UInt< value_size > &  l,
sint  index = -1 
) const
inherited

this method returns true if 'this' is equal 'l'

'index' is an index of the first word from will be the comparison performed (note: we start the comparison from back - from the last word, when index is -1 /default/ it is automatically set into the last word)

Definition at line 3634 of file ttmathuint.h.

Referenced by ttmath::UInt< man >::CmpBiggerEqual().

3635  {
3636  sint i;
3637 
3638  if( index==-1 || index>=sint(value_size) )
3639  i = value_size - 1;
3640  else
3641  i = index;
3642 
3643 
3644  for( ; i>=0 ; --i)
3645  if( table[i] != l.table[i] )
3646  return false;
3647 
3648  return true;
3649  }
signed long sint
Definition: ttmathtypes.h:243
uint table[value_size]
Definition: ttmathuint.h:81

§ CmpSmaller()

template<uint value_size>
bool ttmath::UInt< value_size >::CmpSmaller ( const UInt< value_size > &  l,
sint  index = -1 
) const
inherited

methods for comparing

this method returns true if 'this' is smaller than 'l'

'index' is an index of the first word from will be the comparison performed (note: we start the comparison from back - from the last word, when index is -1 /default/ it is automatically set into the last word) I introduced it for some kind of optimization made in the second division algorithm (Div2)

Definition at line 3575 of file ttmathuint.h.

Referenced by ttmath::UInt< man >::CmpBiggerEqual().

3576  {
3577  sint i;
3578 
3579  if( index==-1 || index>=sint(value_size) )
3580  i = value_size - 1;
3581  else
3582  i = index;
3583 
3584 
3585  for( ; i>=0 ; --i)
3586  {
3587  if( table[i] != l.table[i] )
3588  return table[i] < l.table[i];
3589  }
3590 
3591  // they're equal
3592  return false;
3593  }
signed long sint
Definition: ttmathtypes.h:243
uint table[value_size]
Definition: ttmathuint.h:81

§ CmpSmallerEqual()

template<uint value_size>
bool ttmath::UInt< value_size >::CmpSmallerEqual ( const UInt< value_size > &  l,
sint  index = -1 
) const
inherited

this method returns true if 'this' is smaller than or equal 'l'

'index' is an index of the first word from will be the comparison performed (note: we start the comparison from back - from the last word, when index is -1 /default/ it is automatically set into the last word)

Definition at line 3660 of file ttmathuint.h.

Referenced by ttmath::UInt< man >::CmpBiggerEqual().

3661  {
3662  sint i;
3663 
3664  if( index==-1 || index>=sint(value_size) )
3665  i = value_size - 1;
3666  else
3667  i = index;
3668 
3669 
3670  for( ; i>=0 ; --i)
3671  {
3672  if( table[i] != l.table[i] )
3673  return table[i] < l.table[i];
3674  }
3675 
3676  // they're equal
3677  return true;
3678  }
signed long sint
Definition: ttmathtypes.h:243
uint table[value_size]
Definition: ttmathuint.h:81

§ CompensationToLeft()

template<uint value_size>
uint ttmath::UInt< value_size >::CompensationToLeft ( )
inherited

this method moves all bits into the left side (it returns value how many bits have been moved)

Definition at line 598 of file ttmathuint.h.

Referenced by ttmath::Big< exp, man >::FromInt(), ttmath::Big< exp, man >::FromUInt(), ttmath::Big< exp, man >::Mul(), ttmath::Big< exp, man >::MulUInt(), and ttmath::Big< exp, man >::Standardizing().

599  {
600  uint moving = 0;
601 
602  // a - index a last word which is different from zero
603  sint a;
604  for(a=value_size-1 ; a>=0 && table[a]==0 ; --a);
605 
606  if( a < 0 )
607  return moving; // all words in table have zero
608 
609  if( a != value_size-1 )
610  {
611  moving += ( value_size-1 - a ) * TTMATH_BITS_PER_UINT;
612 
613  // moving all words
614  sint i;
615  for(i=value_size-1 ; a>=0 ; --i, --a)
616  table[i] = table[a];
617 
618  // setting the rest word to zero
619  for(; i>=0 ; --i)
620  table[i] = 0;
621  }
622 
623  uint moving2 = FindLeadingBitInWord( table[value_size-1] );
624  // moving2 is different from -1 because the value table[value_size-1]
625  // is not zero
626 
627  moving2 = TTMATH_BITS_PER_UINT - moving2 - 1;
628  Rcl(moving2);
629 
630  TTMATH_LOG("UInt::CompensationToLeft")
631 
632  return moving + moving2;
633  }
signed long sint
Definition: ttmathtypes.h:243
uint Rcl(uint bits, uint c=0)
Definition: ttmathuint.h:460
uint table[value_size]
Definition: ttmathuint.h:81
#define TTMATH_BITS_PER_UINT
Definition: ttmathtypes.h:253
unsigned long uint
Definition: ttmathtypes.h:238

§ Div() [1/2]

template<uint value_size>
uint ttmath::Int< value_size >::Div ( Int< value_size >  ss2,
Int< value_size > *  remainder = 0 
)

division this = this / ss2 returned values:

  • 0 - ok
  • 1 - division by zero

for example: (result means 'this')

  • 20 / 3 –> result: 6 remainder: 2
  • -20 / 3 –> result: -6 remainder: -2
  • 20 / -3 –> result: -6 remainder: 2
  • -20 / -3 –> result: 6 remainder: -2

in other words: this(old) = ss2 * this(new)(result) + remainder

Definition at line 480 of file ttmathint.h.

Referenced by ttmath::Int< exp >::Div(), ttmath::Int< exp >::operator-(), and ttmath::Int< exp >::Pow().

481  {
482  bool ss1_is_sign, ss2_is_sign;
483 
484  ss1_is_sign = IsSign();
485  ss2_is_sign = ss2.IsSign();
486 
487  /*
488  we don't have to test the carry from Abs as well as in Mul
489  */
490  Abs();
491  ss2.Abs();
492 
493  uint c = UInt<value_size>::Div(ss2, remainder);
494 
495  if( ss1_is_sign != ss2_is_sign )
496  SetSign();
497 
498  if( ss1_is_sign && remainder )
499  remainder->SetSign();
500 
501  return c;
502  }
void SetSign()
Definition: ttmathint.h:141
uint Abs()
Definition: ttmathint.h:168
uint Div(const UInt< value_size > &divisor, UInt< value_size > *remainder=0, uint algorithm=3)
Definition: ttmathuint.h:1626
bool IsSign() const
Definition: ttmathint.h:156
unsigned long uint
Definition: ttmathtypes.h:238

§ Div() [2/2]

template<uint value_size>
uint ttmath::UInt< value_size >::Div ( const UInt< value_size > &  divisor,
UInt< value_size > *  remainder = 0,
uint  algorithm = 3 
)
inherited

division this = this / ss2

return values:

  • 0 - ok
  • 1 - division by zero
  • 'this' will be the quotient
  • 'remainder' - remainder

Definition at line 1626 of file ttmathuint.h.

Referenced by ttmath::Int< exp >::Div(), ttmath::UInt< man >::Div(), ttmath::Big< exp, man >::Mul(), and ttmath::UInt< man >::operator-().

1629  {
1630  switch( algorithm )
1631  {
1632  case 1:
1633  return Div1(divisor, remainder);
1634 
1635  case 2:
1636  return Div2(divisor, remainder);
1637 
1638  case 3:
1639  default:
1640  return Div3(divisor, remainder);
1641  }
1642  }
uint Div1(const UInt< value_size > &divisor, UInt< value_size > *remainder=0)
Definition: ttmathuint.h:1752
uint Div3(const UInt< value_size > &ss2, UInt< value_size > *remainder=0)
Definition: ttmathuint.h:2108
uint Div2(const UInt< value_size > &divisor, UInt< value_size > *remainder=0)
Definition: ttmathuint.h:1862

§ Div1() [1/2]

template<uint value_size>
uint ttmath::UInt< value_size >::Div1 ( const UInt< value_size > &  divisor,
UInt< value_size > *  remainder = 0 
)
inherited

the first division algorithm (radix 2)

Definition at line 1752 of file ttmathuint.h.

Referenced by ttmath::UInt< man >::Div(), and ttmath::UInt< man >::Div1().

1753  {
1754  uint m,n, test;
1755 
1756  test = Div_StandardTest(divisor, m, n, remainder);
1757  if( test < 2 )
1758  return test;
1759 
1760  if( !remainder )
1761  {
1762  UInt<value_size> rem;
1763 
1764  return Div1_Calculate(divisor, rem);
1765  }
1766 
1767  return Div1_Calculate(divisor, *remainder);
1768  }
unsigned long uint
Definition: ttmathtypes.h:238

§ Div1() [2/2]

template<uint value_size>
uint ttmath::UInt< value_size >::Div1 ( const UInt< value_size > &  divisor,
UInt< value_size > &  remainder 
)
inherited

the first division algorithm (radix 2)

Definition at line 1775 of file ttmathuint.h.

1776  {
1777  return Div1(divisor, &remainder);
1778  }
uint Div1(const UInt< value_size > &divisor, UInt< value_size > *remainder=0)
Definition: ttmathuint.h:1752

§ Div2() [1/2]

template<uint value_size>
uint ttmath::UInt< value_size >::Div2 ( const UInt< value_size > &  divisor,
UInt< value_size > *  remainder = 0 
)
inherited

the second division algorithm

return values:

  • 0 - ok
  • 1 - division by zero

Definition at line 1862 of file ttmathuint.h.

Referenced by ttmath::UInt< man >::Div(), and ttmath::UInt< man >::Div2().

1863  {
1864  if( this == &divisor )
1865  {
1866  UInt<value_size> divisor_copy(divisor);
1867  return Div2Ref(divisor_copy, remainder);
1868  }
1869  else
1870  {
1871  return Div2Ref(divisor, remainder);
1872  }
1873  }

§ Div2() [2/2]

template<uint value_size>
uint ttmath::UInt< value_size >::Div2 ( const UInt< value_size > &  divisor,
UInt< value_size > &  remainder 
)
inherited

the second division algorithm

return values:

  • 0 - ok
  • 1 - division by zero

Definition at line 1883 of file ttmathuint.h.

1884  {
1885  return Div2(divisor, &remainder);
1886  }
uint Div2(const UInt< value_size > &divisor, UInt< value_size > *remainder=0)
Definition: ttmathuint.h:1862

§ Div3() [1/2]

template<uint value_size>
uint ttmath::UInt< value_size >::Div3 ( const UInt< value_size > &  ss2,
UInt< value_size > *  remainder = 0 
)
inherited

the third division algorithm

Definition at line 2108 of file ttmathuint.h.

Referenced by ttmath::UInt< man >::Div(), and ttmath::UInt< man >::Div3().

2109  {
2110  if( this == &ss2 )
2111  {
2112  UInt<value_size> copy_ss2(ss2);
2113  return Div3Ref(copy_ss2, remainder);
2114  }
2115  else
2116  {
2117  return Div3Ref(ss2, remainder);
2118  }
2119  }

§ Div3() [2/2]

template<uint value_size>
uint ttmath::UInt< value_size >::Div3 ( const UInt< value_size > &  ss2,
UInt< value_size > &  remainder 
)
inherited

the third division algorithm

Definition at line 2125 of file ttmathuint.h.

2126  {
2127  return Div3(ss2, &remainder);
2128  }
uint Div3(const UInt< value_size > &ss2, UInt< value_size > *remainder=0)
Definition: ttmathuint.h:2108

§ DivInt() [1/2]

template<uint value_size>
uint ttmath::Int< value_size >::DivInt ( sint  ss2,
sint remainder = 0 
)

division this = this / ss2 (ss2 is int) returned values:

  • 0 - ok
  • 1 - division by zero

for example: (result means 'this')

  • 20 / 3 –> result: 6 remainder: 2
  • -20 / 3 –> result: -6 remainder: -2
  • 20 / -3 –> result: -6 remainder: 2
  • -20 / -3 –> result: 6 remainder: -2

in other words: this(old) = ss2 * this(new)(result) + remainder

Definition at line 524 of file ttmathint.h.

Referenced by ttmath::Int< exp >::DivInt().

525  {
526  bool ss1_is_sign, ss2_is_sign;
527 
528  ss1_is_sign = IsSign();
529 
530  /*
531  we don't have to test the carry from Abs as well as in Mul
532  */
533  Abs();
534 
535  if( ss2 < 0 )
536  {
537  ss2 = -ss2;
538  ss2_is_sign = true;
539  }
540  else
541  {
542  ss2_is_sign = false;
543  }
544 
545  uint rem;
546  uint c = UInt<value_size>::DivInt((uint)ss2, &rem);
547 
548  if( ss1_is_sign != ss2_is_sign )
549  SetSign();
550 
551  if( remainder )
552  {
553  if( ss1_is_sign )
554  *remainder = -sint(rem);
555  else
556  *remainder = sint(rem);
557  }
558 
559  return c;
560  }
signed long sint
Definition: ttmathtypes.h:243
void SetSign()
Definition: ttmathint.h:141
uint Abs()
Definition: ttmathint.h:168
bool IsSign() const
Definition: ttmathint.h:156
unsigned long uint
Definition: ttmathtypes.h:238
uint DivInt(uint divisor, uint *remainder=0)
Definition: ttmathuint.h:1568

§ DivInt() [2/2]

template<uint value_size>
uint ttmath::UInt< value_size >::DivInt ( uint  divisor,
uint remainder = 0 
)
inherited

Division

division by one unsigned word

returns 1 when divisor is zero

Definition at line 1568 of file ttmathuint.h.

Referenced by ttmath::UInt< man >::Div3(), ttmath::Int< exp >::DivInt(), and ttmath::UInt< man >::ToStringBase().

1569  {
1570  if( divisor == 0 )
1571  {
1572  if( remainder )
1573  *remainder = 0; // this is for convenience, without it the compiler can report that 'remainder' is uninitialized
1574 
1575  TTMATH_LOG("UInt::DivInt")
1576 
1577  return 1;
1578  }
1579 
1580  if( divisor == 1 )
1581  {
1582  if( remainder )
1583  *remainder = 0;
1584 
1585  TTMATH_LOG("UInt::DivInt")
1586 
1587  return 0;
1588  }
1589 
1590  UInt<value_size> dividend(*this);
1591  SetZero();
1592 
1593  sint i; // i must be with a sign
1594  uint r = 0;
1595 
1596  // we're looking for the last word in ss1
1597  for(i=value_size-1 ; i>0 && dividend.table[i]==0 ; --i);
1598 
1599  for( ; i>=0 ; --i)
1600  DivTwoWords(r, dividend.table[i], divisor, &table[i], &r);
1601 
1602  if( remainder )
1603  *remainder = r;
1604 
1605  TTMATH_LOG("UInt::DivInt")
1606 
1607  return 0;
1608  }
signed long sint
Definition: ttmathtypes.h:243
uint table[value_size]
Definition: ttmathuint.h:81
void SetZero()
Definition: ttmathuint.h:188
unsigned long uint
Definition: ttmathtypes.h:238
static void DivTwoWords(uint a, uint b, uint c, uint *r, uint *rest)
uint DivInt(uint divisor, uint *remainder=0)
Definition: ttmathuint.h:1568

§ DivTwoWords()

template<uint value_size>
void ttmath::UInt< value_size >::DivTwoWords ( uint  a,
uint  b,
uint  c,
uint r,
uint rest 
)
staticinherited

Division

this method calculates 64bits word a:b / 32bits c (a higher, b lower word) r = a:b / c and rest - remainder

this method is created only on a 64bit platform***

WARNING: if r (one word) is too small for the result or c is equal zero there'll be a hardware interruption (0) and probably the end of your program

Definition at line 1132 of file ttmathuint_x86_64.h.

1133  {
1134  uint r_;
1135  uint rest_;
1136  /*
1137  these variables have similar meaning like those in
1138  the multiplication algorithm MulTwoWords
1139  */
1140 
1141  TTMATH_ASSERT( c != 0 )
1142 
1143 
1144  #ifndef __GNUC__
1145 
1146  ttmath_div_x64(&a,&b,c);
1147  r_ = a;
1148  rest_ = b;
1149 
1150  #endif
1151 
1152 
1153  #ifdef __GNUC__
1154 
1155  __asm__ (
1156 
1157  "divq %%rcx \n"
1158 
1159  : "=a" (r_), "=d" (rest_)
1160  : "d" (a), "a" (b), "c" (c)
1161  : "cc" );
1162 
1163  #endif
1164 
1165 
1166  *r = r_;
1167  *rest = rest_;
1168  }
unsigned long uint
Definition: ttmathtypes.h:238

§ FindLeadingBit()

template<uint value_size>
bool ttmath::UInt< value_size >::FindLeadingBit ( uint table_id,
uint index 
) const
inherited

this method looks for the highest set bit

result:

  • if 'this' is not zero: return value - true, 'table_id' - the index of a word <0..value_size-1>, 'index' - the index of this set bit in the word <0..TTMATH_BITS_PER_UINT)
  • if 'this' is zero: return value - false, both 'table_id' and 'index' are zero

Definition at line 649 of file ttmathuint.h.

Referenced by ttmath::UInt< man >::BitNot2(), and ttmath::UInt< man >::ToStringBase().

650  {
651  for(table_id=value_size-1 ; table_id!=0 && table[table_id]==0 ; --table_id);
652 
653  if( table_id==0 && table[table_id]==0 )
654  {
655  // is zero
656  index = 0;
657 
658  return false;
659  }
660 
661  // table[table_id] is different from 0
662  index = FindLeadingBitInWord( table[table_id] );
663 
664  return true;
665  }
uint table[value_size]
Definition: ttmathuint.h:81

§ FindLowestBit()

template<uint value_size>
bool ttmath::UInt< value_size >::FindLowestBit ( uint table_id,
uint index 
) const
inherited

this method looks for the smallest set bit

result:

  • if 'this' is not zero: return value - true, 'table_id' - the index of a word <0..value_size-1>, 'index' - the index of this set bit in the word <0..TTMATH_BITS_PER_UINT)
  • if 'this' is zero: return value - false, both 'table_id' and 'index' are zero

Definition at line 681 of file ttmathuint.h.

682  {
683  for(table_id=0 ; table_id<value_size && table[table_id]==0 ; ++table_id);
684 
685  if( table_id >= value_size )
686  {
687  // is zero
688  index = 0;
689  table_id = 0;
690 
691  return false;
692  }
693 
694  // table[table_id] is different from 0
695  index = FindLowestBitInWord( table[table_id] );
696 
697  return true;
698  }
uint table[value_size]
Definition: ttmathuint.h:81

§ FromInt() [1/6]

template<uint value_size>
template<uint argument_size>
uint ttmath::Int< value_size >::FromInt ( const Int< argument_size > &  p)

this method converts an Int<another_size> type into this class

this operation has mainly sense if the value from p can be held in this type

it returns a carry if the value 'p' is too big

Definition at line 697 of file ttmathint.h.

Referenced by ttmath::Big< exp, man >::FromBig(), ttmath::Int< exp >::FromInt(), ttmath::Int< exp >::Int(), and ttmath::Int< exp >::operator=().

698  {
699  return FromUIntOrInt(p, false);
700  }

§ FromInt() [2/6]

template<uint value_size>
uint ttmath::Int< value_size >::FromInt ( sint  value)

this method converts the sint type into this class

Definition at line 706 of file ttmathint.h.

707  {
708  uint fill = ( value<0 ) ? TTMATH_UINT_MAX_VALUE : 0;
709 
710  for(uint i=1 ; i<value_size ; ++i)
711  UInt<value_size>::table[i] = fill;
712 
713  UInt<value_size>::table[0] = uint(value);
714 
715  // there'll never be a carry here
716  return 0;
717  }
uint table[value_size]
Definition: ttmathuint.h:81
#define TTMATH_UINT_MAX_VALUE
Definition: ttmathtypes.h:264
unsigned long uint
Definition: ttmathtypes.h:238

§ FromInt() [3/6]

template<uint value_size>
template<uint argument_size>
uint ttmath::Int< value_size >::FromInt ( const UInt< argument_size > &  p)

this method converts UInt<another_size> into this class

Definition at line 734 of file ttmathint.h.

735  {
736  return FromUIntOrInt(p, true);
737  }

§ FromInt() [4/6]

template<uint value_size>
uint ttmath::Int< value_size >::FromInt ( uint  value)

this method converts the uint type into this class

Definition at line 762 of file ttmathint.h.

763  {
764  return FromUInt(value);
765  }
uint FromUInt(const UInt< argument_size > &p)
Definition: ttmathint.h:724

§ FromInt() [5/6]

template<uint value_size>
uint ttmath::Int< value_size >::FromInt ( unsigned int  i)

this method converts 32 bit unsigned int type to this class this operator is created only on a 64bit platform***

Definition at line 1005 of file ttmathint.h.

1006  {
1007  return FromUInt(i);
1008  }
uint FromUInt(const UInt< argument_size > &p)
Definition: ttmathint.h:724

§ FromInt() [6/6]

template<uint value_size>
uint ttmath::Int< value_size >::FromInt ( signed int  i)

this method converts 32 bit signed int type to this class this operator is created only on a 64bit platform***

Definition at line 1015 of file ttmathint.h.

1016  {
1017  return FromInt(sint(i));
1018  }
signed long sint
Definition: ttmathtypes.h:243
uint FromInt(const Int< argument_size > &p)
Definition: ttmathint.h:697

§ FromString() [1/4]

template<uint value_size>
uint ttmath::Int< value_size >::FromString ( const char *  s,
uint  b = 10,
const char **  after_source = 0,
bool *  value_read = 0 
)

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

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

existing first white characters will be ommited (between '-' and a first digit can be white characters too)

after_source (if exists) is pointing at the end of the parsed string

value_read (if exists) tells whether something has actually been read (at least one digit)

Definition at line 1453 of file ttmathint.h.

Referenced by ttmath::Int< exp >::FromString(), ttmath::Int< exp >::Int(), and ttmath::Int< exp >::operator=().

1454  {
1455  return FromStringBase(s, b, after_source, value_read);
1456  }

§ FromString() [2/4]

template<uint value_size>
uint ttmath::Int< value_size >::FromString ( const wchar_t *  s,
uint  b = 10,
const wchar_t **  after_source = 0,
bool *  value_read = 0 
)

this method converts a string into its value

Definition at line 1462 of file ttmathint.h.

1463  {
1464  return FromStringBase(s, b, after_source, value_read);
1465  }

§ FromString() [3/4]

template<uint value_size>
uint ttmath::Int< value_size >::FromString ( const std::string &  s,
uint  b = 10 
)

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

Definition at line 1472 of file ttmathint.h.

1473  {
1474  return FromString( s.c_str(), b );
1475  }
uint FromString(const char *s, uint b=10, const char **after_source=0, bool *value_read=0)
Definition: ttmathint.h:1453

§ FromString() [4/4]

template<uint value_size>
uint ttmath::Int< value_size >::FromString ( const std::wstring &  s,
uint  b = 10 
)

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

Definition at line 1496 of file ttmathint.h.

1497  {
1498  return FromString( s.c_str(), b );
1499  }
uint FromString(const char *s, uint b=10, const char **after_source=0, bool *value_read=0)
Definition: ttmathint.h:1453

§ FromUInt() [1/3]

template<uint value_size>
template<uint argument_size>
uint ttmath::Int< value_size >::FromUInt ( const UInt< argument_size > &  p)

this method converts UInt<another_size> into this class

Definition at line 724 of file ttmathint.h.

Referenced by ttmath::Int< exp >::FromInt(), ttmath::Int< exp >::FromUInt(), ttmath::Int< exp >::Int(), and ttmath::Int< exp >::operator=().

725  {
726  return FromUIntOrInt(p, true);
727  }

§ FromUInt() [2/3]

template<uint value_size>
uint ttmath::Int< value_size >::FromUInt ( uint  value)

this method converts the uint type into this class

Definition at line 743 of file ttmathint.h.

744  {
745  for(uint i=1 ; i<value_size ; ++i)
747 
748  UInt<value_size>::table[0] = value;
749 
750  // there can be a carry here when the size of this value is equal one word
751  // and the 'value' has the highest bit set
752  if( value_size==1 && (value & TTMATH_UINT_HIGHEST_BIT)!=0 )
753  return 1;
754 
755  return 0;
756  }
uint table[value_size]
Definition: ttmathuint.h:81
#define TTMATH_UINT_HIGHEST_BIT
Definition: ttmathtypes.h:258
unsigned long uint
Definition: ttmathtypes.h:238

§ FromUInt() [3/3]

template<uint value_size>
uint ttmath::Int< value_size >::FromUInt ( unsigned int  i)

this method converts 32 bit unsigned int type to this class this operator is created only on a 64bit platform***

Definition at line 995 of file ttmathint.h.

996  {
997  return FromUInt(uint(i));
998  }
uint FromUInt(const UInt< argument_size > &p)
Definition: ttmathint.h:724
unsigned long uint
Definition: ttmathtypes.h:238

§ GetBit()

template<uint value_size>
uint ttmath::UInt< value_size >::GetBit ( uint  bit_index) const
inherited

getting the 'bit_index' bit

bit_index bigger or equal zero

Definition at line 706 of file ttmathuint.h.

Referenced by ttmath::Big< exp, man >::Mod2().

707  {
708  TTMATH_ASSERT( bit_index < value_size * TTMATH_BITS_PER_UINT )
709 
710  uint index = bit_index / TTMATH_BITS_PER_UINT;
711  uint bit = bit_index % TTMATH_BITS_PER_UINT;
712 
713  uint temp = table[index];
714  uint res = SetBitInWord(temp, bit);
715 
716  return res;
717  }
uint table[value_size]
Definition: ttmathuint.h:81
#define TTMATH_BITS_PER_UINT
Definition: ttmathtypes.h:253
static uint SetBitInWord(uint &value, uint bit)
unsigned long uint
Definition: ttmathtypes.h:238

§ IsOnlyTheHighestBitSet()

template<uint value_size>
bool ttmath::UInt< value_size >::IsOnlyTheHighestBitSet ( ) const
inherited

returning true if only the highest bit is set

Definition at line 2565 of file ttmathuint.h.

2566  {
2567 #ifdef __clang__
2568 #pragma clang diagnostic push
2569 #pragma clang diagnostic ignored "-Wtautological-compare"
2570 #endif
2571 
2572  for(uint i=0 ; i<value_size-1 ; ++i)
2573  if( table[i] != 0 )
2574  return false;
2575 
2576 #ifdef __clang__
2577 #pragma clang diagnostic pop
2578 #endif
2579  if( table[value_size-1] != TTMATH_UINT_HIGHEST_BIT )
2580  return false;
2581 
2582  return true;
2583  }
uint table[value_size]
Definition: ttmathuint.h:81
#define TTMATH_UINT_HIGHEST_BIT
Definition: ttmathtypes.h:258
unsigned long uint
Definition: ttmathtypes.h:238

§ IsOnlyTheLowestBitSet()

template<uint value_size>
bool ttmath::UInt< value_size >::IsOnlyTheLowestBitSet ( ) const
inherited

returning true if only the lowest bit is set

Definition at line 2589 of file ttmathuint.h.

2590  {
2591  if( table[0] != 1 )
2592  return false;
2593 
2594  for(uint i=1 ; i<value_size ; ++i)
2595  if( table[i] != 0 )
2596  return false;
2597 
2598  return true;
2599  }
uint table[value_size]
Definition: ttmathuint.h:81
unsigned long uint
Definition: ttmathtypes.h:238

§ IsSign()

template<uint value_size>
bool ttmath::Int< value_size >::IsSign ( ) const

§ IsTheHighestBitSet()

template<uint value_size>
bool ttmath::UInt< value_size >::IsTheHighestBitSet ( ) const
inherited

this method returns true if the highest bit of the value is set

Definition at line 2547 of file ttmathuint.h.

Referenced by ttmath::Int< exp >::IsSign(), and ttmath::Big< exp, man >::Standardizing().

2548  {
2549  return (table[value_size-1] & TTMATH_UINT_HIGHEST_BIT) != 0;
2550  }
uint table[value_size]
Definition: ttmathuint.h:81
#define TTMATH_UINT_HIGHEST_BIT
Definition: ttmathtypes.h:258

§ IsTheLowestBitSet()

template<uint value_size>
bool ttmath::UInt< value_size >::IsTheLowestBitSet ( ) const
inherited

this method returns true if the lowest bit of the value is set

Definition at line 2556 of file ttmathuint.h.

Referenced by ttmath::Big< exp, man >::ChangeSign().

2557  {
2558  return (*table & 1) != 0;
2559  }
uint table[value_size]
Definition: ttmathuint.h:81

§ IsZero()

template<uint value_size>
bool ttmath::UInt< value_size >::IsZero ( ) const
inherited

this method returns true if the value is equal zero

Definition at line 2605 of file ttmathuint.h.

Referenced by ttmath::Big< exp, man >::FromString(), ttmath::Big< exp, man >::Pow(), ttmath::UInt< man >::Pow(), ttmath::UInt< man >::Sqrt(), ttmath::Big< exp, man >::Standardizing(), and ttmath::UInt< man >::ToStringBase().

2606  {
2607  for(uint i=0 ; i<value_size ; ++i)
2608  if(table[i] != 0)
2609  return false;
2610 
2611  return true;
2612  }
uint table[value_size]
Definition: ttmathuint.h:81
unsigned long uint
Definition: ttmathtypes.h:238

§ LibType()

template<uint value_size>
LibTypeCode ttmath::UInt< value_size >::LibType ( )
staticinherited

returning the currect type of the library

Definition at line 126 of file ttmathuint_x86_64.h.

Referenced by ttmath::Big< exp, man >::LibType().

127  {
128  #ifndef __GNUC__
129  LibTypeCode info = asm_vc_64;
130  #endif
131 
132  #ifdef __GNUC__
133  LibTypeCode info = asm_gcc_64;
134  #endif
135 
136  return info;
137  }

§ LibTypeStr()

template<uint value_size>
const char * ttmath::UInt< value_size >::LibTypeStr ( )
staticinherited

returning the string represents the currect type of the library we have following types: asm_vc_32 - with asm code designed for Microsoft Visual C++ (32 bits) asm_gcc_32 - with asm code designed for GCC (32 bits) asm_vc_64 - with asm for VC (64 bit) asm_gcc_64 - with asm for GCC (64 bit) no_asm_32 - pure C++ version (32 bit) - without any asm code no_asm_64 - pure C++ version (64 bit) - without any asm code

Definition at line 108 of file ttmathuint_x86_64.h.

Referenced by ttmath::Big< exp, man >::LibTypeStr().

109  {
110  #ifndef __GNUC__
111  static const char info[] = "asm_vc_64";
112  #endif
113 
114  #ifdef __GNUC__
115  static const char info[] = "asm_gcc_64";
116  #endif
117 
118  return info;
119  }

§ Mul() [1/2]

template<uint value_size>
uint ttmath::Int< value_size >::Mul ( Int< value_size >  ss2)

multiplication this = this * ss2

it returns carry if the result is too big (we're using the method from the base class but we have to make one correction in account of signs)

Definition at line 440 of file ttmathint.h.

Referenced by ttmath::Int< exp >::operator-().

441  {
442  bool ss1_is_sign, ss2_is_sign;
443  uint c;
444 
445  ss1_is_sign = IsSign();
446  ss2_is_sign = ss2.IsSign();
447 
448  /*
449  we don't have to check the carry from Abs (values will be correct
450  because next we're using the method Mul from the base class UInt
451  which is without a sign)
452  */
453  Abs();
454  ss2.Abs();
455 
456  c = UInt<value_size>::Mul(ss2);
457  c += CheckMinCarry(ss1_is_sign, ss2_is_sign);
458 
459  if( ss1_is_sign != ss2_is_sign )
460  SetSign();
461 
462  return c;
463  }
void SetSign()
Definition: ttmathint.h:141
uint Abs()
Definition: ttmathint.h:168
bool IsSign() const
Definition: ttmathint.h:156
unsigned long uint
Definition: ttmathtypes.h:238
uint Mul(const UInt< value_size > &ss2, uint algorithm=100)
Definition: ttmathuint.h:923

§ Mul() [2/2]

template<uint value_size>
uint ttmath::UInt< value_size >::Mul ( const UInt< value_size > &  ss2,
uint  algorithm = 100 
)
inherited

the multiplication 'this' = 'this' * ss2

algorithm: 100 - means automatically choose the fastest algorithm

Definition at line 923 of file ttmathuint.h.

Referenced by ttmath::Int< exp >::Mul(), ttmath::UInt< man >::operator-(), ttmath::UInt< man >::Pow(), and ttmath::UInt< man >::ToString().

924  {
925  switch( algorithm )
926  {
927  case 1:
928  return Mul1(ss2);
929 
930  case 2:
931  return Mul2(ss2);
932 
933  case 3:
934  return Mul3(ss2);
935 
936  case 100:
937  default:
938  return MulFastest(ss2);
939  }
940  }
uint Mul2(const UInt< value_size > &ss2)
Definition: ttmathuint.h:1082
uint MulFastest(const UInt< value_size > &ss2)
Definition: ttmathuint.h:1479
uint Mul3(const UInt< value_size > &ss2)
Definition: ttmathuint.h:1212
uint Mul1(const UInt< value_size > &ss2)
Definition: ttmathuint.h:1023

§ Mul1()

template<uint value_size>
uint ttmath::UInt< value_size >::Mul1 ( const UInt< value_size > &  ss2)
inherited

multiplication: this = this * ss2 can return carry

Definition at line 1023 of file ttmathuint.h.

Referenced by ttmath::UInt< man >::Mul(), and ttmath::UInt< man >::Mul1Big().

1024  {
1025  if( this == &ss2 )
1026  {
1027  UInt<value_size> copy_ss2(ss2);
1028  return Mul1Ref(copy_ss2);
1029  }
1030  else
1031  {
1032  return Mul1Ref(ss2);
1033  }
1034  }

§ Mul1Big()

template<uint value_size>
void ttmath::UInt< value_size >::Mul1Big ( const UInt< value_size > &  ss2_,
UInt< value_size *2 > &  result 
)
inherited

multiplication: result = this * ss2

result is twice bigger than 'this' and 'ss2' this method never returns carry

Definition at line 1043 of file ttmathuint.h.

Referenced by ttmath::UInt< man >::MulBig().

1044  {
1045  UInt<value_size*2> ss2;
1046  uint i;
1047 
1048  // copying *this into result and ss2_ into ss2
1049  for(i=0 ; i<value_size ; ++i)
1050  {
1051  result.table[i] = table[i];
1052  ss2.table[i] = ss2_.table[i];
1053  }
1054 
1055  // cleaning the highest bytes in result and ss2
1056  for( ; i < value_size*2 ; ++i)
1057  {
1058  result.table[i] = 0;
1059  ss2.table[i] = 0;
1060  }
1061 
1062  // multiply
1063  // (there will not be a carry)
1064  result.Mul1( ss2 );
1065 
1066  TTMATH_LOG("UInt::Mul1Big")
1067  }
uint table[value_size]
Definition: ttmathuint.h:81
unsigned long uint
Definition: ttmathtypes.h:238

§ Mul2()

template<uint value_size>
uint ttmath::UInt< value_size >::Mul2 ( const UInt< value_size > &  ss2)
inherited

the second version of the multiplication algorithm

this algorithm is similar to the 'schoolbook method' which is done by hand

multiplication: this = this * ss2

it returns carry if it has been

Definition at line 1082 of file ttmathuint.h.

Referenced by ttmath::UInt< man >::Mul().

1083  {
1084  UInt<value_size*2> result;
1085  uint i, c = 0;
1086 
1087  Mul2Big(ss2, result);
1088 
1089  // copying result
1090  for(i=0 ; i<value_size ; ++i)
1091  table[i] = result.table[i];
1092 
1093  // testing carry
1094  for( ; i<value_size*2 ; ++i)
1095  if( result.table[i] != 0 )
1096  {
1097  c = 1;
1098  break;
1099  }
1100 
1101  TTMATH_LOGC("UInt::Mul2", c)
1102 
1103  return c;
1104  }
void Mul2Big(const UInt< value_size > &ss2, UInt< value_size *2 > &result)
Definition: ttmathuint.h:1113
uint table[value_size]
Definition: ttmathuint.h:81
unsigned long uint
Definition: ttmathtypes.h:238

§ Mul2Big()

template<uint value_size>
void ttmath::UInt< value_size >::Mul2Big ( const UInt< value_size > &  ss2,
UInt< value_size *2 > &  result 
)
inherited

multiplication: result = this * ss2

result is twice bigger than this and ss2 this method never returns carry

Definition at line 1113 of file ttmathuint.h.

Referenced by ttmath::UInt< man >::Mul2(), ttmath::UInt< man >::MulBig(), and ttmath::UInt< man >::MulFastestBig().

1114  {
1115  Mul2Big2<value_size>(table, ss2.table, result);
1116 
1117  TTMATH_LOG("UInt::Mul2Big")
1118  }
uint table[value_size]
Definition: ttmathuint.h:81

§ Mul3()

template<uint value_size>
uint ttmath::UInt< value_size >::Mul3 ( const UInt< value_size > &  ss2)
inherited

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)) )

Definition at line 1212 of file ttmathuint.h.

Referenced by ttmath::UInt< man >::Mul().

1213  {
1214  UInt<value_size*2> result;
1215  uint i, c = 0;
1216 
1217  Mul3Big(ss2, result);
1218 
1219  // copying result
1220  for(i=0 ; i<value_size ; ++i)
1221  table[i] = result.table[i];
1222 
1223  // testing carry
1224  for( ; i<value_size*2 ; ++i)
1225  if( result.table[i] != 0 )
1226  {
1227  c = 1;
1228  break;
1229  }
1230 
1231  TTMATH_LOGC("UInt::Mul3", c)
1232 
1233  return c;
1234  }
void Mul3Big(const UInt< value_size > &ss2, UInt< value_size *2 > &result)
Definition: ttmathuint.h:1245
uint table[value_size]
Definition: ttmathuint.h:81
unsigned long uint
Definition: ttmathtypes.h:238

§ Mul3Big()

template<uint value_size>
void ttmath::UInt< value_size >::Mul3Big ( const UInt< value_size > &  ss2,
UInt< value_size *2 > &  result 
)
inherited

multiplication: result = this * ss2

result is twice bigger than this and ss2, this method never returns carry, (Karatsuba multiplication)

Definition at line 1245 of file ttmathuint.h.

Referenced by ttmath::UInt< man >::Mul3(), ttmath::UInt< man >::MulBig(), and ttmath::UInt< man >::MulFastestBig().

1246  {
1247  Mul3Big2<value_size>(table, ss2.table, result.table);
1248 
1249  TTMATH_LOG("UInt::Mul3Big")
1250  }
uint table[value_size]
Definition: ttmathuint.h:81

§ MulBig()

template<uint value_size>
void ttmath::UInt< value_size >::MulBig ( const UInt< value_size > &  ss2,
UInt< value_size *2 > &  result,
uint  algorithm = 100 
)
inherited

the multiplication 'result' = 'this' * ss2

since the 'result' is twice bigger than 'this' and 'ss2' this method never returns a carry

algorithm: 100 - means automatically choose the fastest algorithm

Definition at line 951 of file ttmathuint.h.

954  {
955  switch( algorithm )
956  {
957  case 1:
958  Mul1Big(ss2, result);
959  break;
960 
961  case 2:
962  Mul2Big(ss2, result);
963  break;
964 
965  case 3:
966  Mul3Big(ss2, result);
967  break;
968 
969  case 100:
970  default:
971  MulFastestBig(ss2, result);
972  }
973  }
void Mul2Big(const UInt< value_size > &ss2, UInt< value_size *2 > &result)
Definition: ttmathuint.h:1113
void Mul3Big(const UInt< value_size > &ss2, UInt< value_size *2 > &result)
Definition: ttmathuint.h:1245
void MulFastestBig(const UInt< value_size > &ss2, UInt< value_size *2 > &result)
Definition: ttmathuint.h:1510
void Mul1Big(const UInt< value_size > &ss2_, UInt< value_size *2 > &result)
Definition: ttmathuint.h:1043

§ MulFastest()

template<uint value_size>
uint ttmath::UInt< value_size >::MulFastest ( const UInt< value_size > &  ss2)
inherited

multiplication this = this * ss2

Definition at line 1479 of file ttmathuint.h.

Referenced by ttmath::UInt< man >::Mul().

1480  {
1481  UInt<value_size*2> result;
1482  uint i, c = 0;
1483 
1484  MulFastestBig(ss2, result);
1485 
1486  // copying result
1487  for(i=0 ; i<value_size ; ++i)
1488  table[i] = result.table[i];
1489 
1490  // testing carry
1491  for( ; i<value_size*2 ; ++i)
1492  if( result.table[i] != 0 )
1493  {
1494  c = 1;
1495  break;
1496  }
1497 
1498  TTMATH_LOGC("UInt::MulFastest", c)
1499 
1500  return c;
1501  }
uint table[value_size]
Definition: ttmathuint.h:81
void MulFastestBig(const UInt< value_size > &ss2, UInt< value_size *2 > &result)
Definition: ttmathuint.h:1510
unsigned long uint
Definition: ttmathtypes.h:238

§ MulFastestBig()

template<uint value_size>
void ttmath::UInt< value_size >::MulFastestBig ( const UInt< value_size > &  ss2,
UInt< value_size *2 > &  result 
)
inherited

multiplication result = this * ss2

this method is trying to select the fastest algorithm (in the future this method can be improved)

Definition at line 1510 of file ttmathuint.h.

Referenced by ttmath::UInt< man >::MulBig(), and ttmath::UInt< man >::MulFastest().

1511  {
1513  {
1514  Mul2Big(ss2, result);
1515  return;
1516  }
1517 
1518  uint x1size = value_size, x2size = value_size;
1519  uint x1start = 0, x2start = 0;
1520 
1521  for(x1size=value_size ; x1size>0 && table[x1size-1]==0 ; --x1size);
1522  for(x2size=value_size ; x2size>0 && ss2.table[x2size-1]==0 ; --x2size);
1523 
1524  if( x1size==0 || x2size==0 )
1525  {
1526  // either 'this' or 'ss2' is equal zero - the result is zero too
1527  result.SetZero();
1528  return;
1529  }
1530 
1531  for(x1start=0 ; x1start<x1size && table[x1start]==0 ; ++x1start);
1532  for(x2start=0 ; x2start<x2size && ss2.table[x2start]==0 ; ++x2start);
1533 
1534  uint distancex1 = x1size - x1start;
1535  uint distancex2 = x2size - x2start;
1536 
1537  if( distancex1 < 3 || distancex2 < 3 )
1538  {
1539  // either 'this' or 'ss2' have only 2 (or 1) items different from zero (side by side)
1540  // (this condition in the future can be improved)
1541  Mul2Big3<value_size>(table, ss2.table, result, x1start, x1size, x2start, x2size);
1542  return;
1543  }
1544 
1545 
1546  // Karatsuba multiplication
1547  Mul3Big(ss2, result);
1548 
1549  TTMATH_LOG("UInt::MulFastestBig")
1550  }
void Mul2Big(const UInt< value_size > &ss2, UInt< value_size *2 > &result)
Definition: ttmathuint.h:1113
void Mul3Big(const UInt< value_size > &ss2, UInt< value_size *2 > &result)
Definition: ttmathuint.h:1245
uint table[value_size]
Definition: ttmathuint.h:81
#define TTMATH_USE_KARATSUBA_MULTIPLICATION_FROM_SIZE
Definition: ttmathtypes.h:326
unsigned long uint
Definition: ttmathtypes.h:238

§ MulInt() [1/3]

template<uint value_size>
uint ttmath::Int< value_size >::MulInt ( sint  ss2)

multiplication: this = this * ss2

it can return a carry

Definition at line 398 of file ttmathint.h.

399  {
400  bool ss1_is_sign, ss2_is_sign;
401  uint c;
402 
403  ss1_is_sign = IsSign();
404 
405  /*
406  we don't have to check the carry from Abs (values will be correct
407  because next we're using the method MulInt from the base class UInt
408  which is without a sign)
409  */
410  Abs();
411 
412  if( ss2 < 0 )
413  {
414  ss2 = -ss2;
415  ss2_is_sign = true;
416  }
417  else
418  {
419  ss2_is_sign = false;
420  }
421 
422  c = UInt<value_size>::MulInt((uint)ss2);
423  c += CheckMinCarry(ss1_is_sign, ss2_is_sign);
424 
425  if( ss1_is_sign != ss2_is_sign )
426  SetSign();
427 
428  return c;
429  }
void SetSign()
Definition: ttmathint.h:141
uint Abs()
Definition: ttmathint.h:168
uint MulInt(uint ss2)
Definition: ttmathuint.h:835
bool IsSign() const
Definition: ttmathint.h:156
unsigned long uint
Definition: ttmathtypes.h:238

§ MulInt() [2/3]

template<uint value_size>
uint ttmath::UInt< value_size >::MulInt ( uint  ss2)
inherited

Multiplication

multiplication: this = this * ss2

it can return a carry

Definition at line 835 of file ttmathuint.h.

Referenced by ttmath::Int< exp >::MulInt(), and ttmath::Big< exp, man >::MulUInt().

836  {
837  uint r1, r2, x1;
838  uint c = 0;
839 
840  UInt<value_size> u(*this);
841  SetZero();
842 
843  if( ss2 == 0 )
844  {
845  TTMATH_LOGC("UInt::MulInt(uint)", 0)
846  return 0;
847  }
848 
849  for(x1=0 ; x1<value_size-1 ; ++x1)
850  {
851  MulTwoWords(u.table[x1], ss2, &r2, &r1);
852  c += AddTwoInts(r2,r1,x1);
853  }
854 
855  // x1 = value_size-1 (last word)
856  MulTwoWords(u.table[x1], ss2, &r2, &r1);
857  c += (r2!=0) ? 1 : 0;
858  c += AddInt(r1, x1);
859 
860  TTMATH_LOGC("UInt::MulInt(uint)", c)
861 
862  return (c==0)? 0 : 1;
863  }
uint AddInt(uint value, uint index=0)
static void MulTwoWords(uint a, uint b, uint *result_high, uint *result_low)
void SetZero()
Definition: ttmathuint.h:188
uint AddTwoInts(uint x2, uint x1, uint index)
unsigned long uint
Definition: ttmathtypes.h:238

§ MulInt() [3/3]

template<uint value_size>
template<uint result_size>
void ttmath::UInt< value_size >::MulInt ( uint  ss2,
UInt< result_size > &  result 
) const
inherited

multiplication: result = this * ss2

we're using this method only when result_size is greater than value_size if so there will not be a carry

Definition at line 873 of file ttmathuint.h.

874  {
875  TTMATH_ASSERT( result_size > value_size )
876 
877  uint r2,r1;
878  uint x1size=value_size;
879  uint x1start=0;
880 
881  result.SetZero();
882 
883  if( ss2 == 0 )
884  {
885  TTMATH_VECTOR_LOG("UInt::MulInt(uint, UInt<>)", result.table, result_size)
886  return;
887  }
888 
889  if( value_size > 2 )
890  {
891  // if the value_size is smaller than or equal to 2
892  // there is no sense to set x1size and x1start to another values
893 
894  for(x1size=value_size ; x1size>0 && table[x1size-1]==0 ; --x1size);
895 
896  if( x1size == 0 )
897  {
898  TTMATH_VECTOR_LOG("UInt::MulInt(uint, UInt<>)", result.table, result_size)
899  return;
900  }
901 
902  for(x1start=0 ; x1start<x1size && table[x1start]==0 ; ++x1start);
903  }
904 
905  for(uint x1=x1start ; x1<x1size ; ++x1)
906  {
907  MulTwoWords(table[x1], ss2, &r2, &r1 );
908  result.AddTwoInts(r2,r1,x1);
909  }
910 
911  TTMATH_VECTOR_LOG("UInt::MulInt(uint, UInt<>)", result.table, result_size)
912 
913  return;
914  }
uint table[value_size]
Definition: ttmathuint.h:81
static void MulTwoWords(uint a, uint b, uint *result_high, uint *result_low)
void SetZero()
Definition: ttmathuint.h:188
unsigned long uint
Definition: ttmathtypes.h:238

§ MulTwoWords()

template<uint value_size>
void ttmath::UInt< value_size >::MulTwoWords ( uint  a,
uint  b,
uint result_high,
uint result_low 
)
staticinherited

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

this method is created only on a 64bit platform***

Definition at line 1072 of file ttmathuint_x86_64.h.

1073  {
1074  /*
1075  we must use these temporary variables in order to inform the compilator
1076  that value pointed with result1 and result2 has changed
1077 
1078  this has no effect in visual studio but it's usefull when
1079  using gcc and options like -O
1080  */
1081  uint result1_;
1082  uint result2_;
1083 
1084 
1085  #ifndef __GNUC__
1086  result1_ = _umul128(a,b,&result2_);
1087  #endif
1088 
1089 
1090  #ifdef __GNUC__
1091 
1092  __asm__ (
1093 
1094  "mulq %%rdx \n"
1095 
1096  : "=a" (result1_), "=d" (result2_)
1097  : "0" (a), "1" (b)
1098  : "cc" );
1099 
1100  #endif
1101 
1102 
1103  *result_low = result1_;
1104  *result_high = result2_;
1105  }
unsigned long uint
Definition: ttmathtypes.h:238

§ operator++() [1/2]

template<uint value_size>
UInt<value_size>& ttmath::Int< value_size >::operator++ ( )

Prefix operator e.g. ++variable

Definition at line 1767 of file ttmathint.h.

1768  {
1769  AddOne();
1770 
1771  return *this;
1772  }
uint AddOne()
Definition: ttmathint.h:327

§ operator++() [2/2]

template<uint value_size>
UInt<value_size> ttmath::Int< value_size >::operator++ ( int  )

Postfix operator e.g. variable++

Definition at line 1778 of file ttmathint.h.

1779  {
1780  UInt<value_size> temp( *this );
1781 
1782  AddOne();
1783 
1784  return temp;
1785  }
uint AddOne()
Definition: ttmathint.h:327

§ operator-() [1/2]

template<uint value_size>
Int<value_size> ttmath::Int< value_size >::operator- ( ) const

standard mathematical operators

an operator for changing the sign

it's not changing 'this' but the changed value will be returned

Definition at line 1660 of file ttmathint.h.

Referenced by ttmath::Int< exp >::operator-().

1661  {
1662  Int<value_size> temp(*this);
1663 
1664  temp.ChangeSign();
1665 
1666  return temp;
1667  }

§ operator-() [2/2]

template<uint value_size>
UInt<value_size> ttmath::UInt< value_size >::operator- ( const UInt< value_size > &  p2) const
inherited

standard mathematical operators

Definition at line 3755 of file ttmathuint.h.

3756  {
3757  UInt<value_size> temp(*this);
3758 
3759  temp.Sub(p2);
3760 
3761  return temp;
3762  }

§ operator=() [1/11]

template<uint value_size>
Int<value_size>& ttmath::Int< value_size >::operator= ( const Int< value_size > &  p)

the default assignment operator

Definition at line 771 of file ttmathint.h.

Referenced by ttmath::Int< exp >::Int(), and ttmath::Int< exp >::operator-().

772  {
773  FromInt(p);
774 
775  return *this;
776  }
uint FromInt(const Int< argument_size > &p)
Definition: ttmathint.h:697

§ operator=() [2/11]

template<uint value_size>
template<uint argument_size>
Int<value_size>& ttmath::Int< value_size >::operator= ( const Int< argument_size > &  p)

this operator converts an Int<another_size> type to this class

it doesn't return a carry

Definition at line 785 of file ttmathint.h.

786  {
787  FromInt(p);
788 
789  return *this;
790  }
uint FromInt(const Int< argument_size > &p)
Definition: ttmathint.h:697

§ operator=() [3/11]

template<uint value_size>
Int<value_size>& ttmath::Int< value_size >::operator= ( sint  i)

this method converts the sint type to this class

Definition at line 796 of file ttmathint.h.

797  {
798  FromInt(i);
799 
800  return *this;
801  }
uint FromInt(const Int< argument_size > &p)
Definition: ttmathint.h:697

§ operator=() [4/11]

template<uint value_size>
template<uint argument_size>
Int<value_size>& ttmath::Int< value_size >::operator= ( const UInt< argument_size > &  p)

this operator converts an UInt<another_size> type to this class

it doesn't return a carry

Definition at line 840 of file ttmathint.h.

841  {
842  FromUInt(p);
843 
844  return *this;
845  }
uint FromUInt(const UInt< argument_size > &p)
Definition: ttmathint.h:724

§ operator=() [5/11]

template<uint value_size>
Int<value_size>& ttmath::Int< value_size >::operator= ( uint  i)

this method converts the Uint type to this class

Definition at line 851 of file ttmathint.h.

852  {
853  FromUInt(i);
854 
855  return *this;
856  }
uint FromUInt(const UInt< argument_size > &p)
Definition: ttmathint.h:724

§ operator=() [6/11]

template<uint value_size>
Int<value_size>& ttmath::Int< value_size >::operator= ( unsigned int  i)

this method converts 32 bit unsigned int type to this class this operator is created only on a 64bit platform***

Definition at line 1025 of file ttmathint.h.

1026  {
1027  FromUInt(i);
1028 
1029  return *this;
1030  }
uint FromUInt(const UInt< argument_size > &p)
Definition: ttmathint.h:724

§ operator=() [7/11]

template<uint value_size>
Int<value_size>& ttmath::Int< value_size >::operator= ( signed int  i)

this operator converts 32 bit signed int type to this class this operator is created only on a 64bit platform***

Definition at line 1047 of file ttmathint.h.

1048  {
1049  FromInt(i);
1050 
1051  return *this;
1052  }
uint FromInt(const Int< argument_size > &p)
Definition: ttmathint.h:697

§ operator=() [8/11]

template<uint value_size>
Int<value_size>& ttmath::Int< value_size >::operator= ( const char *  s)

this operator converts a string into its value (with base = 10)

Definition at line 1481 of file ttmathint.h.

1482  {
1483  FromString(s);
1484 
1485  return *this;
1486  }
uint FromString(const char *s, uint b=10, const char **after_source=0, bool *value_read=0)
Definition: ttmathint.h:1453

§ operator=() [9/11]

template<uint value_size>
Int<value_size>& ttmath::Int< value_size >::operator= ( const wchar_t *  s)

this operator converts a string into its value (with base = 10)

Definition at line 1505 of file ttmathint.h.

1506  {
1507  FromString(s);
1508 
1509  return *this;
1510  }
uint FromString(const char *s, uint b=10, const char **after_source=0, bool *value_read=0)
Definition: ttmathint.h:1453

§ operator=() [10/11]

template<uint value_size>
Int<value_size>& ttmath::Int< value_size >::operator= ( const std::wstring &  s)

this operator converts a string into its value (with base = 10)

Definition at line 1516 of file ttmathint.h.

1517  {
1518  FromString( s.c_str() );
1519 
1520  return *this;
1521  }
uint FromString(const char *s, uint b=10, const char **after_source=0, bool *value_read=0)
Definition: ttmathint.h:1453

§ operator=() [11/11]

template<uint value_size>
Int<value_size>& ttmath::Int< value_size >::operator= ( const std::string &  s)

this operator converts a string into its value (with base = 10)

Definition at line 1529 of file ttmathint.h.

1530  {
1531  FromString( s.c_str() );
1532 
1533  return *this;
1534  }
uint FromString(const char *s, uint b=10, const char **after_source=0, bool *value_read=0)
Definition: ttmathint.h:1453

§ operator==()

template<uint value_size>
bool ttmath::Int< value_size >::operator== ( const Int< value_size > &  l) const

methods for comparing

Definition at line 1545 of file ttmathint.h.

1546  {
1547  return UInt<value_size>::operator==(l);
1548  }

§ operator~()

template<uint value_size>
UInt<value_size> ttmath::UInt< value_size >::operator~ ( ) const
inherited

bitwise operators

Definition at line 3895 of file ttmathuint.h.

3896  {
3897  UInt<value_size> temp( *this );
3898 
3899  temp.BitNot();
3900 
3901  return temp;
3902  }

§ Pow() [1/2]

template<uint value_size>
uint ttmath::Int< value_size >::Pow ( Int< value_size >  pow)

power this = this ^ pow

return values:

  • 0 - ok
  • 1 - carry
  • 2 - incorrect arguments 0^0 or 0^(-something)

Definition at line 608 of file ttmathint.h.

609  {
610  if( !pow.IsSign() )
611  return Pow2(pow);
612 
614  // if 'pow' is negative then
615  // 'this' must be different from zero
616  return 2;
617 
618  if( pow.ChangeSign() )
619  return 1;
620 
621  Int<value_size> t(*this);
622  uint c_temp = t.Pow2(pow);
623  if( c_temp > 0 )
624  return c_temp;
625 
627  if( Div(t) )
628  return 1;
629 
630  return 0;
631  }
unsigned long uint
Definition: ttmathtypes.h:238
uint Div(Int< value_size > ss2, Int< value_size > *remainder=0)
Definition: ttmathint.h:480
void SetOne()
Definition: ttmathuint.h:202
bool IsZero() const
Definition: ttmathuint.h:2605

§ Pow() [2/2]

template<uint value_size>
uint ttmath::UInt< value_size >::Pow ( UInt< value_size >  pow)
inherited

power this = this ^ pow binary algorithm (r-to-l)

return values:

  • 0 - ok
  • 1 - carry
  • 2 - incorrect argument (0^0)

Definition at line 2425 of file ttmathuint.h.

Referenced by ttmath::Int< exp >::DivInt().

2426  {
2427  if(pow.IsZero() && IsZero())
2428  // we don't define zero^zero
2429  return 2;
2430 
2431  UInt<value_size> start(*this);
2432  UInt<value_size> result;
2433  result.SetOne();
2434  uint c = 0;
2435 
2436  while( !c )
2437  {
2438  if( pow.table[0] & 1 )
2439  c += result.Mul(start);
2440 
2441  pow.Rcr2_one(0);
2442  if( pow.IsZero() )
2443  break;
2444 
2445  c += start.Mul(start);
2446  }
2447 
2448  *this = result;
2449 
2450  TTMATH_LOGC("UInt::Pow(UInt<>)", c)
2451 
2452  return (c==0)? 0 : 1;
2453  }
unsigned long uint
Definition: ttmathtypes.h:238
bool IsZero() const
Definition: ttmathuint.h:2605

§ PrintLog() [1/2]

template<uint value_size>
template<class char_type , class ostream_type >
void ttmath::UInt< value_size >::PrintLog ( const char_type *  msg,
ostream_type &  output 
) const
inherited

this method is used when macro TTMATH_DEBUG_LOG is defined

Definition at line 159 of file ttmathuint.h.

160  {
161  PrintVectorLog(msg, output, table, value_size);
162  }
uint table[value_size]
Definition: ttmathuint.h:81
static void PrintVectorLog(const char_type *msg, ostream_type &output, const uint *vector, uint vector_len)
Definition: ttmathuint.h:135

§ PrintLog() [2/2]

template<uint value_size>
template<class char_type , class ostream_type >
void ttmath::UInt< value_size >::PrintLog ( const char_type *  msg,
uint  carry,
ostream_type &  output 
) const
inherited

this method is used when macro TTMATH_DEBUG_LOG is defined

Definition at line 169 of file ttmathuint.h.

170  {
171  PrintVectorLog(msg, output, table, value_size);
172  output << " carry: " << carry << std::endl;
173  }
uint table[value_size]
Definition: ttmathuint.h:81
static void PrintVectorLog(const char_type *msg, ostream_type &output, const uint *vector, uint vector_len)
Definition: ttmathuint.h:135

§ PrintTable()

template<uint value_size>
template<class ostream_type >
void ttmath::UInt< value_size >::PrintTable ( ostream_type &  output) const
inherited

some methods used for debugging purposes

this method is only for debugging purposes or when we want to make a table of a variable (constant) in ttmathbig.h

it prints the table in a nice form of several columns

Definition at line 97 of file ttmathuint.h.

98  {
99  // how many columns there'll be
100  const int columns = 8;
101 
102  int c = 1;
103  for(int i=value_size-1 ; i>=0 ; --i)
104  {
105  output << "0x" << std::setfill('0');
106 
107  #ifdef TTMATH_PLATFORM32
108  output << std::setw(8);
109  #else
110  output << std::setw(16);
111  #endif
112 
113  output << std::hex << table[i];
114 
115  if( i>0 )
116  {
117  output << ", ";
118 
119  if( ++c > columns )
120  {
121  output << std::endl;
122  c = 1;
123  }
124  }
125  }
126 
127  output << std::dec << std::endl;
128  }
uint table[value_size]
Definition: ttmathuint.h:81

§ PrintVectorLog() [1/2]

template<uint value_size>
template<class char_type , class ostream_type >
static void ttmath::UInt< value_size >::PrintVectorLog ( const char_type *  msg,
ostream_type &  output,
const uint vector,
uint  vector_len 
)
staticinherited

this method is used when macro TTMATH_DEBUG_LOG is defined

Definition at line 135 of file ttmathuint.h.

Referenced by ttmath::UInt< man >::PrintLog(), and ttmath::UInt< man >::PrintVectorLog().

136  {
137  output << msg << std::endl;
138 
139  for(uint i=0 ; i<vector_len ; ++i)
140  output << " table[" << i << "]: " << vector[i] << std::endl;
141  }
unsigned long uint
Definition: ttmathtypes.h:238

§ PrintVectorLog() [2/2]

template<uint value_size>
template<class char_type , class ostream_type >
static void ttmath::UInt< value_size >::PrintVectorLog ( const char_type *  msg,
uint  carry,
ostream_type &  output,
const uint vector,
uint  vector_len 
)
staticinherited

this method is used when macro TTMATH_DEBUG_LOG is defined

Definition at line 148 of file ttmathuint.h.

149  {
150  PrintVectorLog(msg, output, vector, vector_len);
151  output << " carry: " << carry << std::endl;
152  }
static void PrintVectorLog(const char_type *msg, ostream_type &output, const uint *vector, uint vector_len)
Definition: ttmathuint.h:135

§ Rcl()

template<uint value_size>
uint ttmath::UInt< value_size >::Rcl ( uint  bits,
uint  c = 0 
)
inherited

moving all bits into the left side 'bits' times return value <- this <- C

bits is from a range of <0, man * TTMATH_BITS_PER_UINT> or it can be even bigger then all bits will be set to 'c'

the value c will be set into the lowest bits and the method returns state of the last moved bit

Definition at line 460 of file ttmathuint.h.

Referenced by ttmath::UInt< man >::CompensationToLeft(), ttmath::UInt< man >::Div1(), ttmath::UInt< man >::operator~(), ttmath::Big< exp, man >::RemainFraction(), and ttmath::Big< exp, man >::ToInt().

461  {
462  uint last_c = 0;
463  uint rest_bits = bits;
464 
465  if( bits == 0 )
466  return 0;
467 
468  if( bits >= TTMATH_BITS_PER_UINT )
469  RclMoveAllWords(rest_bits, last_c, bits, c);
470 
471  if( rest_bits == 0 )
472  {
473  TTMATH_LOG("UInt::Rcl")
474  return last_c;
475  }
476 
477  // rest_bits is from 1 to TTMATH_BITS_PER_UINT-1 now
478  if( rest_bits == 1 )
479  {
480  last_c = Rcl2_one(c);
481  }
482  else if( rest_bits == 2 )
483  {
484  // performance tests showed that for rest_bits==2 it's better to use Rcl2_one twice instead of Rcl2(2,c)
485  Rcl2_one(c);
486  last_c = Rcl2_one(c);
487  }
488  else
489  {
490  last_c = Rcl2(rest_bits, c);
491  }
492 
493  TTMATH_LOGC("UInt::Rcl", last_c)
494 
495  return last_c;
496  }
#define TTMATH_BITS_PER_UINT
Definition: ttmathtypes.h:253
unsigned long uint
Definition: ttmathtypes.h:238

§ Rcr()

template<uint value_size>
uint ttmath::UInt< value_size >::Rcr ( uint  bits,
uint  c = 0 
)
inherited

moving all bits into the right side 'bits' times c -> this -> return value

bits is from a range of <0, man * TTMATH_BITS_PER_UINT> or it can be even bigger then all bits will be set to 'c'

the value c will be set into the highest bits and the method returns state of the last moved bit

Definition at line 555 of file ttmathuint.h.

Referenced by ttmath::Big< exp, man >::BitAnd(), ttmath::Big< exp, man >::BitOr(), ttmath::Big< exp, man >::BitXor(), ttmath::Big< exp, man >::ChangeSign(), ttmath::Big< exp, man >::MulUInt(), ttmath::UInt< man >::operator~(), ttmath::Big< exp, man >::Pow(), ttmath::UInt< man >::Sqrt(), ttmath::Big< exp, man >::ToDouble(), and ttmath::Big< exp, man >::ToInt().

556  {
557  uint last_c = 0;
558  uint rest_bits = bits;
559 
560  if( bits == 0 )
561  return 0;
562 
563  if( bits >= TTMATH_BITS_PER_UINT )
564  RcrMoveAllWords(rest_bits, last_c, bits, c);
565 
566  if( rest_bits == 0 )
567  {
568  TTMATH_LOG("UInt::Rcr")
569  return last_c;
570  }
571 
572  // rest_bits is from 1 to TTMATH_BITS_PER_UINT-1 now
573  if( rest_bits == 1 )
574  {
575  last_c = Rcr2_one(c);
576  }
577  else if( rest_bits == 2 )
578  {
579  // performance tests showed that for rest_bits==2 it's better to use Rcr2_one twice instead of Rcr2(2,c)
580  Rcr2_one(c);
581  last_c = Rcr2_one(c);
582  }
583  else
584  {
585  last_c = Rcr2(rest_bits, c);
586  }
587 
588  TTMATH_LOGC("UInt::Rcr", last_c)
589 
590  return last_c;
591  }
#define TTMATH_BITS_PER_UINT
Definition: ttmathtypes.h:253
unsigned long uint
Definition: ttmathtypes.h:238

§ SetBit()

template<uint value_size>
uint ttmath::UInt< value_size >::SetBit ( uint  bit_index)
inherited

setting the 'bit_index' bit and returning the last state of the bit

bit_index bigger or equal zero

Definition at line 726 of file ttmathuint.h.

Referenced by ttmath::UInt< man >::Div2().

727  {
728  TTMATH_ASSERT( bit_index < value_size * TTMATH_BITS_PER_UINT )
729 
730  uint index = bit_index / TTMATH_BITS_PER_UINT;
731  uint bit = bit_index % TTMATH_BITS_PER_UINT;
732  uint res = SetBitInWord(table[index], bit);
733 
734  TTMATH_LOG("UInt::SetBit")
735 
736  return res;
737  }
uint table[value_size]
Definition: ttmathuint.h:81
#define TTMATH_BITS_PER_UINT
Definition: ttmathtypes.h:253
static uint SetBitInWord(uint &value, uint bit)
unsigned long uint
Definition: ttmathtypes.h:238

§ SetBitInWord()

template<uint value_size>
uint ttmath::UInt< value_size >::SetBitInWord ( uint value,
uint  bit 
)
staticinherited

this method sets a special bit in the 'value' and returns the last state of the bit (zero or one)

this method is created only on a 64bit platform***

bit is from <0,63>

e.g. uint x = 100; uint bit = SetBitInWord(x, 3); now: x = 108 and bit = 0

Definition at line 1020 of file ttmathuint_x86_64.h.

1021  {
1022  TTMATH_ASSERT( bit < TTMATH_BITS_PER_UINT )
1023 
1024  uint old_bit;
1025  uint v = value;
1026 
1027 
1028  #ifndef __GNUC__
1029  old_bit = _bittestandset64((__int64*)&value,bit) != 0;
1030  #endif
1031 
1032 
1033  #ifdef __GNUC__
1034 
1035  __asm__ (
1036 
1037  "btsq %%rbx, %%rax \n"
1038  "setc %%bl \n"
1039  "movzx %%bl, %%rbx \n"
1040 
1041  : "=a" (v), "=b" (old_bit)
1042  : "0" (v), "1" (bit)
1043  : "cc" );
1044 
1045  #endif
1046 
1047  value = v;
1048 
1049  return old_bit;
1050  }
#define TTMATH_BITS_PER_UINT
Definition: ttmathtypes.h:253
unsigned long uint
Definition: ttmathtypes.h:238

§ SetFromTable()

template<uint value_size>
void ttmath::UInt< value_size >::SetFromTable ( const unsigned int *  temp_table,
uint  temp_table_len 
)
inherited

this method copies the value stored in an another table (warning: first values in temp_table are the highest words – it's different from our table)

this method is created only on a 64bit platform***

we copy as many words as it is possible

if temp_table_len is bigger than value_size we'll try to round the lowest word from table depending on the last not used bit in temp_table (this rounding isn't a perfect rounding – look at the description below)

and if temp_table_len is smaller than value_size we'll clear the rest words in the table

warning: we're using 'temp_table' as a pointer at 32bit words

Definition at line 325 of file ttmathuint.h.

Referenced by ttmath::Big< exp, man >::SetE(), ttmath::Big< exp, man >::SetLn10(), ttmath::Big< exp, man >::SetLn2(), ttmath::UInt< man >::Swap(), and ttmath::Big< exp, man >::Swap().

326  {
327  uint temp_table_index = 0;
328  sint i; // 'i' with a sign
329 
330  for(i=value_size-1 ; i>=0 && temp_table_index<temp_table_len; --i, ++temp_table_index)
331  {
332  table[i] = uint(temp_table[ temp_table_index ]) << 32;
333 
334  ++temp_table_index;
335 
336  if( temp_table_index<temp_table_len )
337  table[i] |= temp_table[ temp_table_index ];
338  }
339 
340 
341  // rounding mantissa
342  if( temp_table_index < temp_table_len )
343  {
344  if( (temp_table[temp_table_index] & TTMATH_UINT_HIGHEST_BIT) != 0 )
345  {
346  /*
347  very simply rounding
348  if the bit from not used last word from temp_table is set to one
349  we're rouding the lowest word in the table
350 
351  in fact there should be a normal addition but
352  we don't use Add() or AddTwoInts() because these methods
353  can set a carry and then there'll be a small problem
354  for optimization
355  */
356  if( table[0] != TTMATH_UINT_MAX_VALUE )
357  ++table[0];
358  }
359  }
360 
361  // cleaning the rest of the mantissa
362  for( ; i >= 0 ; --i)
363  table[i] = 0;
364 
365  TTMATH_LOG("UInt::SetFromTable")
366  }
signed long sint
Definition: ttmathtypes.h:243
uint table[value_size]
Definition: ttmathuint.h:81
#define TTMATH_UINT_HIGHEST_BIT
Definition: ttmathtypes.h:258
#define TTMATH_UINT_MAX_VALUE
Definition: ttmathtypes.h:264
unsigned long uint
Definition: ttmathtypes.h:238

§ SetMax()

template<uint value_size>
void ttmath::Int< value_size >::SetMax ( )

this method sets the max value which this class can hold (all bits will be one besides the last one)

Definition at line 71 of file ttmathint.h.

Referenced by ttmath::Big< exp, man >::SetMax(), ttmath::Big< exp, man >::SetMin(), and ttmath::Int< exp >::ToWString().

72  {
75  }
uint table[value_size]
Definition: ttmathuint.h:81
void SetMax()
Definition: ttmathuint.h:215
#define TTMATH_UINT_HIGHEST_BIT
Definition: ttmathtypes.h:258

§ SetMin()

template<uint value_size>
void ttmath::Int< value_size >::SetMin ( )

this method sets the min value which this class can hold (all bits will be zero besides the last one which is one)

Definition at line 82 of file ttmathint.h.

Referenced by ttmath::Int< exp >::ToWString().

83  {
86  }
uint table[value_size]
Definition: ttmathuint.h:81
#define TTMATH_UINT_HIGHEST_BIT
Definition: ttmathtypes.h:258
void SetZero()
Definition: ttmathuint.h:188

§ SetOne()

template<uint value_size>
void ttmath::UInt< value_size >::SetOne ( )
inherited

this method sets one

Definition at line 202 of file ttmathuint.h.

Referenced by ttmath::UInt< man >::Div(), ttmath::Int< exp >::Pow(), and ttmath::UInt< man >::Pow().

203  {
204  SetZero();
205  table[0] = 1;
206 
207  TTMATH_LOG("UInt::SetOne")
208  }
uint table[value_size]
Definition: ttmathuint.h:81
void SetZero()
Definition: ttmathuint.h:188

§ SetSign()

template<uint value_size>
void ttmath::Int< value_size >::SetSign ( )

this method sets the sign

samples

  • 1 -> -1
  • -2 -> -2

from a positive value we make a negative value, if the value is negative we do nothing

Definition at line 141 of file ttmathint.h.

Referenced by ttmath::Int< exp >::Div(), ttmath::Int< exp >::DivInt(), ttmath::Int< exp >::Mul(), and ttmath::Int< exp >::MulInt().

142  {
143  if( IsSign() )
144  return;
145 
146  ChangeSign();
147  }
bool IsSign() const
Definition: ttmathint.h:156
uint ChangeSign()
Definition: ttmathint.h:105

§ SetSignOne()

template<uint value_size>
void ttmath::Int< value_size >::SetSignOne ( )

this method sets -1 as the value (-1 is equal the max value in an unsigned type)

Definition at line 93 of file ttmathint.h.

94  {
96  }
void SetMax()
Definition: ttmathuint.h:215

§ SetZero()

template<uint value_size>
void ttmath::UInt< value_size >::SetZero ( )
inherited

§ Size()

template<uint value_size>
uint ttmath::UInt< value_size >::Size ( ) const
inherited

this method returns the size of the table

Definition at line 179 of file ttmathuint.h.

180  {
181  return value_size;
182  }

§ Sqrt()

template<uint value_size>
void ttmath::UInt< value_size >::Sqrt ( )
inherited

square root e.g. Sqrt(9) = 3 ('digit-by-digit' algorithm)

Definition at line 2461 of file ttmathuint.h.

2462  {
2463  UInt<value_size> bit, temp;
2464 
2465  if( IsZero() )
2466  return;
2467 
2468  UInt<value_size> value(*this);
2469 
2470  SetZero();
2471  bit.SetZero();
2472  bit.table[value_size-1] = (TTMATH_UINT_HIGHEST_BIT >> 1);
2473 
2474  while( bit > value )
2475  bit.Rcr(2);
2476 
2477  while( !bit.IsZero() )
2478  {
2479  temp = *this;
2480  temp.Add(bit);
2481 
2482  if( value >= temp )
2483  {
2484  value.Sub(temp);
2485  Rcr(1);
2486  Add(bit);
2487  }
2488  else
2489  {
2490  Rcr(1);
2491  }
2492 
2493  bit.Rcr(2);
2494  }
2495 
2496  TTMATH_LOG("UInt::Sqrt")
2497  }
uint Add(const UInt< value_size > &ss2, uint c=0)
#define TTMATH_UINT_HIGHEST_BIT
Definition: ttmathtypes.h:258
void SetZero()
Definition: ttmathuint.h:188
uint Rcr(uint bits, uint c=0)
Definition: ttmathuint.h:555
bool IsZero() const
Definition: ttmathuint.h:2605

§ Sub() [1/2]

template<uint value_size>
uint ttmath::Int< value_size >::Sub ( const Int< value_size > &  ss2)

this method subtracts two values with a sign

we don't use the previous Add because the method ChangeSign can sometimes return carry

this = p1(=this) - p2

  • when p1>=0 i p2>=0 carry will never be set
  • when p1<0 i p2<0 carry will never be set
  • when p1>=0 i p2<0 carry is set when the highest bit of value is set
  • when p1<0 i p2>=0 carry is set when the highest bit of value is clear

Definition at line 299 of file ttmathint.h.

Referenced by ttmath::Big< exp, man >::Add(), ttmath::Big< exp, man >::BitAnd(), ttmath::Big< exp, man >::BitOr(), ttmath::Big< exp, man >::BitXor(), ttmath::Big< exp, man >::Mul(), ttmath::Int< exp >::operator-(), ttmath::Big< exp, man >::RemainFraction(), and ttmath::Big< exp, man >::Standardizing().

300  {
301  bool p1_is_sign = IsSign();
302  bool p2_is_sign = ss2.IsSign();
303 
304  UInt<value_size>::Sub(ss2);
305 
306  return CorrectCarryAfterSubtracting(p1_is_sign, p2_is_sign);
307  }
uint Sub(const UInt< value_size > &ss2, uint c=0)
bool IsSign() const
Definition: ttmathint.h:156

§ Sub() [2/2]

template<uint value_size>
uint ttmath::UInt< value_size >::Sub ( const UInt< value_size > &  ss2,
uint  c = 0 
)
inherited

this method's subtracting ss2 from the 'this' and subtracting carry if it has been defined (this = this - ss2 - c)

this method is created only on a 64bit platform***

c must be zero or one (might be a bigger value than 1) function returns carry (1) (if it was)

Definition at line 460 of file ttmathuint_x86_64.h.

Referenced by ttmath::Int< exp >::ChangeSign(), ttmath::UInt< man >::Div1(), ttmath::UInt< man >::operator-(), ttmath::UInt< man >::Sqrt(), and ttmath::Int< exp >::Sub().

461  {
462  uint b = value_size;
463  uint * p1 = table;
464  const uint * p2 = ss2.table;
465 
466  // we don't have to use TTMATH_REFERENCE_ASSERT here
467  // this algorithm doesn't require it
468 
469  #ifndef __GNUC__
470  c = ttmath_sbb_x64(p1,p2,b,c);
471  #endif
472 
473 
474  #ifdef __GNUC__
475  uint dummy, dummy2;
476 
477  __asm__ __volatile__(
478 
479  "xorq %%rdx, %%rdx \n"
480  "negq %%rax \n" // CF=1 if rax!=0 , CF=0 if rax==0
481 
482  "1: \n"
483  "movq (%%rsi,%%rdx,8), %%rax \n"
484  "sbbq %%rax, (%%rbx,%%rdx,8) \n"
485 
486  "incq %%rdx \n"
487  "decq %%rcx \n"
488  "jnz 1b \n"
489 
490  "adcq %%rcx, %%rcx \n"
491 
492  : "=c" (c), "=a" (dummy), "=d" (dummy2)
493  : "0" (b), "1" (c), "b" (p1), "S" (p2)
494  : "cc", "memory" );
495 
496  #endif
497 
498  TTMATH_LOGC("UInt::Sub", c)
499 
500  return c;
501  }
uint table[value_size]
Definition: ttmathuint.h:81
unsigned long uint
Definition: ttmathtypes.h:238

§ SubInt()

template<uint value_size>
uint ttmath::Int< value_size >::SubInt ( uint  value,
uint  index = 0 
)

this method subtracts one unsigned word (at a specific position) and returns a carry (if it was)

Definition at line 314 of file ttmathint.h.

Referenced by ttmath::Big< exp, man >::FromBig().

315  {
316  bool p1_is_sign = IsSign();
317 
318  UInt<value_size>::SubInt(value, index);
319 
320  return CorrectCarryAfterSubtracting(p1_is_sign, false);
321  }
uint SubInt(uint value, uint index=0)
bool IsSign() const
Definition: ttmathint.h:156

§ SubOne()

template<uint value_size>
uint ttmath::Int< value_size >::SubOne ( )

this method subtracts one from the value and returns carry

Definition at line 340 of file ttmathint.h.

Referenced by ttmath::Int< exp >::operator++(), ttmath::Big< exp, man >::PowUInt(), ttmath::Big< exp, man >::Set05(), and ttmath::Big< exp, man >::Sqrt().

341  {
342  bool p1_is_sign = IsSign();
343 
345 
346  return CorrectCarryAfterSubtracting(p1_is_sign, false);
347  }
bool IsSign() const
Definition: ttmathint.h:156
uint SubOne()
Definition: ttmathuint.h:395

§ SubVector()

template<uint value_size>
uint ttmath::UInt< value_size >::SubVector ( const uint ss1,
const uint ss2,
uint  ss1_size,
uint  ss2_size,
uint result 
)
staticinherited

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)

Definition at line 597 of file ttmathuint_x86_64.h.

598  {
599  TTMATH_ASSERT( ss1_size >= ss2_size )
600 
601  uint c;
602 
603  #ifndef __GNUC__
604  c = ttmath_subvector_x64(ss1, ss2, ss1_size, ss2_size, result);
605  #endif
606 
607 
608  #ifdef __GNUC__
609 
610  // the asm code is nearly the same as in AddVector
611  // only two instructions 'adc' are changed to 'sbb'
612 
613  uint dummy1, dummy2, dummy3;
614  uint rest = ss1_size - ss2_size;
615 
616  __asm__ __volatile__(
617  "mov %%rdx, %%r8 \n"
618  "xor %%rdx, %%rdx \n" // rdx = 0, cf = 0
619  "1: \n"
620  "mov (%%rsi,%%rdx,8), %%rax \n"
621  "sbb (%%rbx,%%rdx,8), %%rax \n"
622  "mov %%rax, (%%rdi,%%rdx,8) \n"
623 
624  "inc %%rdx \n"
625  "dec %%rcx \n"
626  "jnz 1b \n"
627 
628  "adc %%rcx, %%rcx \n" // rcx has the cf state
629 
630  "or %%r8, %%r8 \n"
631  "jz 3f \n"
632 
633  "xor %%rbx, %%rbx \n" // ebx = 0
634  "neg %%rcx \n" // setting cf from rcx
635  "mov %%r8, %%rcx \n" // rcx=rest and is != 0
636  "2: \n"
637  "mov (%%rsi, %%rdx, 8), %%rax \n"
638  "sbb %%rbx, %%rax \n"
639  "mov %%rax, (%%rdi, %%rdx, 8) \n"
640 
641  "inc %%rdx \n"
642  "dec %%rcx \n"
643  "jnz 2b \n"
644 
645  "adc %%rcx, %%rcx \n"
646  "3: \n"
647 
648  : "=a" (dummy1), "=b" (dummy2), "=c" (c), "=d" (dummy3)
649  : "1" (ss2), "2" (ss2_size), "3" (rest), "S" (ss1), "D" (result)
650  : "%r8", "cc", "memory" );
651 
652  #endif
653 
654  TTMATH_VECTOR_LOGC("UInt::SubVector", c, result, ss1_size)
655 
656  return c;
657  }
unsigned long uint
Definition: ttmathtypes.h:238

§ Swap()

template<uint value_size>
void ttmath::UInt< value_size >::Swap ( UInt< value_size > &  ss2)
inherited

this method swappes this for an argument

Definition at line 239 of file ttmathuint.h.

Referenced by ttmath::Big< exp, man >::Swap().

240  {
241  for(uint i=0 ; i<value_size ; ++i)
242  {
243  uint temp = table[i];
244  table[i] = ss2.table[i];
245  ss2.table[i] = temp;
246  }
247  }
uint table[value_size]
Definition: ttmathuint.h:81
unsigned long uint
Definition: ttmathtypes.h:238

§ ToInt() [1/5]

template<uint value_size>
sint ttmath::Int< value_size >::ToInt ( ) const

this method returns the lowest value from table with a sign

we must be sure when we using this method whether the value will hold in an sint type or not (the rest value from table must be zero or -1)

Definition at line 1132 of file ttmathint.h.

Referenced by ttmath::Big< exp, man >::Big(), ttmath::Big< exp, man >::BitAnd(), ttmath::Big< exp, man >::BitOr(), ttmath::Big< exp, man >::BitXor(), ttmath::Big< exp, man >::FromBig(), ttmath::Big< exp, man >::IsInteger(), ttmath::Big< exp, man >::Mod2(), ttmath::Big< exp, man >::RemainFraction(), ttmath::Big< exp, man >::SkipFraction(), ttmath::Big< exp, man >::ToDouble(), ttmath::Int< exp >::ToInt(), and ttmath::Big< exp, man >::ToInt().

1133  {
1134  return sint( UInt<value_size>::table[0] );
1135  }
signed long sint
Definition: ttmathtypes.h:243
uint table[value_size]
Definition: ttmathuint.h:81

§ ToInt() [2/5]

template<uint value_size>
uint ttmath::Int< value_size >::ToInt ( uint result) const

this method converts the value to uint type can return a carry if the value is too long to store it in uint type

Definition at line 1157 of file ttmathint.h.

1158  {
1159  return ToUInt(result);
1160  }
uint ToUInt() const
Definition: ttmathuint.h:3103

§ ToInt() [3/5]

template<uint value_size>
uint ttmath::Int< value_size >::ToInt ( sint result) const

this method converts the value to sint type can return a carry if the value is too long to store it in sint type

Definition at line 1167 of file ttmathint.h.

1168  {
1169  result = sint( UInt<value_size>::table[0] );
1170  uint mask = IsSign() ? TTMATH_UINT_MAX_VALUE : 0;
1171 
1172  if( (result & TTMATH_UINT_HIGHEST_BIT) != (mask & TTMATH_UINT_HIGHEST_BIT) )
1173  return 1;
1174 
1175  for(uint i=1 ; i<value_size ; ++i)
1176  if( UInt<value_size>::table[i] != mask )
1177  return 1;
1178 
1179  return 0;
1180  }
signed long sint
Definition: ttmathtypes.h:243
uint table[value_size]
Definition: ttmathuint.h:81
#define TTMATH_UINT_HIGHEST_BIT
Definition: ttmathtypes.h:258
#define TTMATH_UINT_MAX_VALUE
Definition: ttmathtypes.h:264
bool IsSign() const
Definition: ttmathint.h:156
unsigned long uint
Definition: ttmathtypes.h:238

§ ToInt() [4/5]

template<uint value_size>
uint ttmath::Int< value_size >::ToInt ( unsigned int &  result) const

this method converts the value to a 32 bit unsigned integer can return a carry if the value is too long to store it in this type this method is created only on a 64 bit platform ***

Definition at line 1274 of file ttmathint.h.

1275  {
1276  return ToUInt(result);
1277  }
uint ToUInt() const
Definition: ttmathuint.h:3103

§ ToInt() [5/5]

template<uint value_size>
uint ttmath::Int< value_size >::ToInt ( int &  result) const

this method converts the value to a 32 bit signed integer can return a carry if the value is too long to store it in this type this method is created only on a 64 bit platform ***

Definition at line 1285 of file ttmathint.h.

1286  {
1287  uint first = UInt<value_size>::table[0];
1288 
1289  result = int(first);
1290  uint mask = IsSign() ? TTMATH_UINT_MAX_VALUE : 0;
1291 
1292  if( (first >> 31) != (mask >> 31) )
1293  return 1;
1294 
1295  for(uint i=1 ; i<value_size ; ++i)
1296  if( UInt<value_size>::table[i] != mask )
1297  return 1;
1298 
1299  return 0;
1300  }
uint table[value_size]
Definition: ttmathuint.h:81
#define TTMATH_UINT_MAX_VALUE
Definition: ttmathtypes.h:264
bool IsSign() const
Definition: ttmathint.h:156
unsigned long uint
Definition: ttmathtypes.h:238

§ ToString() [1/3]

template<uint value_size>
void ttmath::Int< value_size >::ToString ( std::string &  result,
uint  b = 10 
) const

this method converts the value to a string with a base equal 'b'

Definition at line 1332 of file ttmathint.h.

Referenced by ttmath::Int< exp >::operator++().

1333  {
1334  return ToStringBase(result, b);
1335  }

§ ToString() [2/3]

template<uint value_size>
std::string ttmath::Int< value_size >::ToString ( uint  b = 10) const

this method converts the value to a string with a base equal 'b'

Definition at line 1341 of file ttmathint.h.

1342  {
1343  std::string result;
1344  ToStringBase(result, b);
1345 
1346  return result;
1347  }

§ ToString() [3/3]

template<uint value_size>
void ttmath::Int< value_size >::ToString ( std::wstring &  result,
uint  b = 10 
) const

this method converts the value to a string with a base equal 'b'

Definition at line 1355 of file ttmathint.h.

1356  {
1357  return ToStringBase(result, b);
1358  }

§ ToStringBase()

template<uint value_size>
template<class string_type >
void ttmath::UInt< value_size >::ToStringBase ( string_type &  result,
uint  b = 10,
bool  negative = false 
) const
inherited

an auxiliary method for converting to a string it's used from Int::ToString() too (negative is set true then)

Definition at line 3316 of file ttmathuint.h.

Referenced by ttmath::Int< exp >::ToInt(), and ttmath::UInt< man >::ToString().

3317  {
3318  UInt<value_size> temp(*this);
3319  uint rest, table_id, index, digits;
3320  double digits_d;
3321  char character;
3322 
3323  result.clear();
3324 
3325  if( b<2 || b>16 )
3326  return;
3327 
3328  if( !FindLeadingBit(table_id, index) )
3329  {
3330  result = '0';
3331  return;
3332  }
3333 
3334  if( negative )
3335  result = '-';
3336 
3337  digits_d = static_cast<double>(table_id); // for not making an overflow in uint type
3338  digits_d *= TTMATH_BITS_PER_UINT;
3339  digits_d += index + 1;
3340  digits_d *= ToStringLog2(b);
3341  digits = static_cast<uint>(digits_d) + 3; // plus some epsilon
3342 
3343  if( result.capacity() < digits )
3344  result.reserve(digits);
3345 
3346  do
3347  {
3348  temp.DivInt(b, &rest);
3349  character = static_cast<char>(Misc::DigitToChar(rest));
3350  result.insert(result.end(), character);
3351  }
3352  while( !temp.IsZero() );
3353 
3354  size_t i1 = negative ? 1 : 0; // the first is a hyphen (when negative is true)
3355  size_t i2 = result.size() - 1;
3356 
3357  for( ; i1 < i2 ; ++i1, --i2 )
3358  {
3359  char tempc = static_cast<char>(result[i1]);
3360  result[i1] = result[i2];
3361  result[i2] = tempc;
3362  }
3363  }
static uint DigitToChar(uint digit)
Definition: ttmathmisc.h:236
double ToStringLog2(uint x) const
Definition: ttmathuint.h:3281
#define TTMATH_BITS_PER_UINT
Definition: ttmathtypes.h:253
unsigned long uint
Definition: ttmathtypes.h:238
bool FindLeadingBit(uint &table_id, uint &index) const
Definition: ttmathuint.h:649

§ ToStringLog2()

template<uint value_size>
double ttmath::UInt< value_size >::ToStringLog2 ( uint  x) const
protectedinherited

an auxiliary method for converting into the string it returns the log (with the base 2) from x where x is in <2;16>

Definition at line 3281 of file ttmathuint.h.

Referenced by ttmath::UInt< man >::ToStringBase().

3282  {
3283  static double log_tab[] = {
3284  1.000000000000000000,
3285  0.630929753571457437,
3286  0.500000000000000000,
3287  0.430676558073393050,
3288  0.386852807234541586,
3289  0.356207187108022176,
3290  0.333333333333333333,
3291  0.315464876785728718,
3292  0.301029995663981195,
3293  0.289064826317887859,
3294  0.278942945651129843,
3295  0.270238154427319741,
3296  0.262649535037193547,
3297  0.255958024809815489,
3298  0.250000000000000000
3299  };
3300 
3301  if( x<2 || x>16 )
3302  return 0;
3303 
3304  return log_tab[x-2];
3305  }

§ ToUInt() [1/3]

template<uint value_size>
uint ttmath::Int< value_size >::ToUInt ( uint result) const

this method converts the value to uint type can return a carry if the value is too long to store it in uint type

Definition at line 1142 of file ttmathint.h.

1143  {
1144  uint c = UInt<value_size>::ToUInt(result);
1145 
1146  if( value_size == 1 )
1147  return (result & TTMATH_UINT_HIGHEST_BIT) == 0 ? 0 : 1;
1148 
1149  return c;
1150  }
#define TTMATH_UINT_HIGHEST_BIT
Definition: ttmathtypes.h:258
unsigned long uint
Definition: ttmathtypes.h:238
uint ToUInt() const
Definition: ttmathuint.h:3103

§ ToUInt() [2/3]

template<uint value_size>
uint ttmath::Int< value_size >::ToUInt ( unsigned int &  result) const

this method converts the value to a 32 bit unsigned integer can return a carry if the value is too long to store it in this type this method is created only on a 64 bit platform ***

Definition at line 1258 of file ttmathint.h.

1259  {
1260  uint c = UInt<value_size>::ToUInt(result);
1261 
1262  if( c || IsSign() )
1263  return 1;
1264 
1265  return 0;
1266  }
bool IsSign() const
Definition: ttmathint.h:156
unsigned long uint
Definition: ttmathtypes.h:238
uint ToUInt() const
Definition: ttmathuint.h:3103

§ ToUInt() [3/3]

template<uint value_size>
uint ttmath::UInt< value_size >::ToUInt ( ) const
inherited

this method returns the lowest value from table

we must be sure when we using this method whether the value will hold in an uint type or not (the rest value from the table must be zero)

Definition at line 3103 of file ttmathuint.h.

Referenced by ttmath::Big< exp, man >::Big(), ttmath::Int< exp >::ToInt(), ttmath::UInt< man >::ToInt(), and ttmath::Int< exp >::ToUInt().

3104  {
3105  return table[0];
3106  }
uint table[value_size]
Definition: ttmathuint.h:81

§ ToWString()

template<uint value_size>
std::wstring ttmath::Int< value_size >::ToWString ( uint  b = 10) const

this method converts the value to a string with a base equal 'b'

Definition at line 1364 of file ttmathint.h.

1365  {
1366  std::wstring result;
1367  ToStringBase(result, b);
1368 
1369  return result;
1370  }

Friends And Related Function Documentation

§ operator<< [1/2]

template<uint value_size>
std::ostream& operator<< ( std::ostream &  s,
const Int< value_size > &  l 
)
friend

output to standard streams

Definition at line 1837 of file ttmathint.h.

1838  {
1839  return OutputToStream<std::ostream, std::string>(s, l);
1840  }

§ operator<< [2/2]

template<uint value_size>
std::wostream& operator<< ( std::wostream &  s,
const Int< value_size > &  l 
)
friend

output to standard streams

Definition at line 1848 of file ttmathint.h.

1849  {
1850  return OutputToStream<std::wostream, std::wstring>(s, l);
1851  }

§ operator>> [1/2]

template<uint value_size>
std::istream& operator>> ( std::istream &  s,
Int< value_size > &  l 
)
friend

input from standard streams

Definition at line 1901 of file ttmathint.h.

1902  {
1903  return InputFromStream<std::istream, std::string, char>(s, l);
1904  }

§ operator>> [2/2]

template<uint value_size>
std::wistream& operator>> ( std::wistream &  s,
Int< value_size > &  l 
)
friend

input from standard streams

Definition at line 1912 of file ttmathint.h.

1913  {
1914  return InputFromStream<std::wistream, std::wstring, wchar_t>(s, l);
1915  }

Field Documentation

§ table

template<uint value_size>
uint ttmath::UInt< value_size >::table[value_size]
inherited

The documentation for this class was generated from the following file: