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

UInt implements a big integer value without a sign. More...

#include <ttmathuint.h>

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

Data Structures

union  uint_
 

Public Member Functions

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 SetMax ()
 
void SetMin ()
 
void Swap (UInt< value_size > &ss2)
 
void SetFromTable (const unsigned int *temp_table, uint temp_table_len)
 
uint AddOne ()
 
uint SubOne ()
 
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
 
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)
 
uint FromInt (sint value)
 
template<uint argument_size>
UInt< value_size > & operator= (const UInt< argument_size > &p)
 
UInt< value_size > & operator= (const UInt< value_size > &p)
 
UInt< value_size > & operator= (uint i)
 
 UInt (uint i)
 
UInt< value_size > & operator= (sint i)
 
 UInt (sint i)
 
uint FromUInt (unsigned int i)
 
uint FromInt (unsigned int i)
 
uint FromInt (signed int i)
 
UInt< value_size > & operator= (unsigned int i)
 
 UInt (unsigned int i)
 
UInt< value_size > & operator= (signed int i)
 
 UInt (signed int i)
 
 UInt (const char *s)
 
 UInt (const std::string &s)
 
 UInt (const wchar_t *s)
 
 UInt (const std::wstring &s)
 
 UInt ()
 
 UInt (const UInt< value_size > &u)
 
template<uint argument_size>
 UInt (const UInt< argument_size > &u)
 
 ~UInt ()
 
uint ToUInt () 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
 
template<class string_type >
void ToStringBase (string_type &result, uint b=10, bool negative=false) 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 std::string &s, uint b=10)
 
UInt< value_size > & operator= (const char *s)
 
UInt< value_size > & operator= (const std::string &s)
 
uint FromString (const wchar_t *s, uint b=10, const wchar_t **after_source=0, bool *value_read=0)
 
uint FromString (const std::wstring &s, uint b=10)
 
UInt< value_size > & operator= (const wchar_t *s)
 
UInt< value_size > & operator= (const std::wstring &s)
 
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++ ()
 
UInt< value_size > operator++ (int)
 
UInt< value_size > & operator-- ()
 
UInt< value_size > operator-- (int)
 
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 AddInt (uint value, uint index=0)
 
uint AddTwoInts (uint x2, uint x1, uint index)
 
uint Sub (const UInt< value_size > &ss2, uint c=0)
 
uint SubInt (uint value, uint index=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 UInt< value_size > &l)
 
std::wostream & operator<< (std::wostream &s, const UInt< value_size > &l)
 
std::istream & operator>> (std::istream &s, UInt< value_size > &l)
 
std::wistream & operator>> (std::wistream &s, UInt< value_size > &l)
 

Detailed Description

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

UInt implements a big integer value without a sign.

value_size - how many bytes specify our value

Definition at line 73 of file ttmathuint.h.

Constructor & Destructor Documentation

§ UInt() [1/11]

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

a constructor for converting the uint to this class

Definition at line 2786 of file ttmathuint.h.

2787  {
2788  FromUInt(i);
2789  }
uint FromUInt(const UInt< argument_size > &p)
Definition: ttmathuint.h:2657

§ UInt() [2/11]

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

a constructor for converting the sint to this class

look at the description of UInt::operator=(sint)

Definition at line 2808 of file ttmathuint.h.

2809  {
2810  FromInt(i);
2811  }
uint FromInt(const UInt< argument_size > &p)
Definition: ttmathuint.h:2698

§ UInt() [3/11]

template<uint value_size>
ttmath::UInt< value_size >::UInt ( 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 2964 of file ttmathuint.h.

2965  {
2966  FromUInt(i);
2967  }
uint FromUInt(const UInt< argument_size > &p)
Definition: ttmathuint.h:2657

§ UInt() [4/11]

template<uint value_size>
ttmath::UInt< value_size >::UInt ( 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 2986 of file ttmathuint.h.

2987  {
2988  FromInt(i);
2989  }
uint FromInt(const UInt< argument_size > &p)
Definition: ttmathuint.h:2698

§ UInt() [5/11]

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

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

Definition at line 3001 of file ttmathuint.h.

3002  {
3003  FromString(s);
3004  }
uint FromString(const char *s, uint b=10, const char **after_source=0, bool *value_read=0)
Definition: ttmathuint.h:3474

§ UInt() [6/11]

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

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

Definition at line 3010 of file ttmathuint.h.

3011  {
3012  FromString( s.c_str() );
3013  }
uint FromString(const char *s, uint b=10, const char **after_source=0, bool *value_read=0)
Definition: ttmathuint.h:3474

§ UInt() [7/11]

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

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

Definition at line 3021 of file ttmathuint.h.

3022  {
3023  FromString(s);
3024  }
uint FromString(const char *s, uint b=10, const char **after_source=0, bool *value_read=0)
Definition: ttmathuint.h:3474

§ UInt() [8/11]

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

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

Definition at line 3030 of file ttmathuint.h.

3031  {
3032  FromString( s.c_str() );
3033  }
uint FromString(const char *s, uint b=10, const char **after_source=0, bool *value_read=0)
Definition: ttmathuint.h:3474

§ UInt() [9/11]

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

a default constructor

we don't clear the table

Definition at line 3045 of file ttmathuint.h.

3046  {
3047  // when macro TTMATH_DEBUG_LOG is defined
3048  // we set special values to the table
3049  // in order to be everywhere the same value of the UInt object
3050  // without this it would be difficult to analyse the log file
3051  #ifdef TTMATH_DEBUG_LOG
3052  #ifdef TTMATH_PLATFORM32
3053  for(uint i=0 ; i<value_size ; ++i)
3054  table[i] = 0xc1c1c1c1;
3055  #else
3056  for(uint i=0 ; i<value_size ; ++i)
3057  table[i] = 0xc1c1c1c1c1c1c1c1;
3058  #endif
3059  #endif
3060  }
uint table[value_size]
Definition: ttmathuint.h:81
unsigned long uint
Definition: ttmathtypes.h:238

§ UInt() [10/11]

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

a copy constructor

Definition at line 3066 of file ttmathuint.h.

3067  {
3068  for(uint i=0 ; i<value_size ; ++i)
3069  table[i] = u.table[i];
3070 
3071  TTMATH_LOG("UInt::UInt(UInt<>)")
3072  }
uint table[value_size]
Definition: ttmathuint.h:81
unsigned long uint
Definition: ttmathtypes.h:238

§ UInt() [11/11]

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

a template for producting constructors for copying from another types

Definition at line 3080 of file ttmathuint.h.

3081  {
3082  // look that 'size' we still set as 'value_size' and not as u.value_size
3083  FromUInt(u);
3084  }
uint FromUInt(const UInt< argument_size > &p)
Definition: ttmathuint.h:2657

§ ~UInt()

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

a destructor

Definition at line 3092 of file ttmathuint.h.

3093  {
3094  }

Member Function Documentation

§ Add()

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

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::UInt< value_size >::AddInt ( uint  value,
uint  index = 0 
)

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

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

if we've got (value_size=3):

table[0] = 10;
table[1] = 30;
table[2] = 5;

and we call:

AddInt(2,1)

then it'll be:

table[0] = 10;
table[1] = 30 + 2;
table[2] = 5;

of course if there was a carry from table[2] it would be returned

Definition at line 231 of file ttmathuint_x86_64.h.

Referenced by ttmath::Int< exp >::AddInt(), and ttmath::UInt< man >::AddOne().

232  {
233  uint b = value_size;
234  uint * p1 = table;
235  uint c;
236 
237  TTMATH_ASSERT( index < value_size )
238 
239  #ifndef __GNUC__
240  c = ttmath_addindexed_x64(p1,b,index,value);
241  #endif
242 
243 
244  #ifdef __GNUC__
245  uint dummy, dummy2;
246 
247  __asm__ __volatile__(
248 
249  "subq %%rdx, %%rcx \n"
250 
251  "1: \n"
252  "addq %%rax, (%%rbx,%%rdx,8) \n"
253  "jnc 2f \n"
254 
255  "movq $1, %%rax \n"
256  "incq %%rdx \n"
257  "decq %%rcx \n"
258  "jnz 1b \n"
259 
260  "2: \n"
261  "setc %%al \n"
262  "movzx %%al, %%rdx \n"
263 
264  : "=d" (c), "=a" (dummy), "=c" (dummy2)
265  : "0" (index), "1" (value), "2" (b), "b" (p1)
266  : "cc", "memory" );
267 
268  #endif
269 
270  TTMATH_LOGC("UInt::AddInt", c)
271 
272  return c;
273  }
uint table[value_size]
Definition: ttmathuint.h:81
unsigned long uint
Definition: ttmathtypes.h:238

§ AddOne()

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

basic mathematic functions

this method adds one to the existing value

Definition at line 386 of file ttmathuint.h.

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

387  {
388  return AddInt(1);
389  }
uint AddInt(uint value, uint index=0)

§ AddTwoInts()

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

this method adds only two unsigned words to the existing value and these words begin on the 'index' position (it's used in the multiplication algorithm 2)

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

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

for example if we've got value_size equal 4 and:

table[0] = 3
table[1] = 4
table[2] = 5
table[3] = 6

then let

x1 = 10
x2 = 20

and

index = 1

the result of this method will be:

table[0] = 3
table[1] = 4 + x1 = 14
table[2] = 5 + x2 = 25
table[3] = 6

and no carry at the end of table[3]

(of course if there was a carry in table[2](5+20) then this carry would be passed to the table[3] etc.)

Definition at line 316 of file ttmathuint_x86_64.h.

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

317  {
318  uint b = value_size;
319  uint * p1 = table;
320  uint c;
321 
322  TTMATH_ASSERT( index < value_size - 1 )
323 
324  #ifndef __GNUC__
325  c = ttmath_addindexed2_x64(p1,b,index,x1,x2);
326  #endif
327 
328 
329  #ifdef __GNUC__
330  uint dummy, dummy2;
331 
332  __asm__ __volatile__(
333 
334  "subq %%rdx, %%rcx \n"
335 
336  "addq %%rsi, (%%rbx,%%rdx,8) \n"
337  "incq %%rdx \n"
338  "decq %%rcx \n"
339 
340  "1: \n"
341  "adcq %%rax, (%%rbx,%%rdx,8) \n"
342  "jnc 2f \n"
343 
344  "mov $0, %%rax \n"
345  "incq %%rdx \n"
346  "decq %%rcx \n"
347  "jnz 1b \n"
348 
349  "2: \n"
350  "setc %%al \n"
351  "movzx %%al, %%rax \n"
352 
353  : "=a" (c), "=c" (dummy), "=d" (dummy2)
354  : "0" (x2), "1" (b), "2" (index), "b" (p1), "S" (x1)
355  : "cc", "memory" );
356 
357  #endif
358 
359  TTMATH_LOGC("UInt::AddTwoInts", c)
360 
361  return c;
362  }
uint table[value_size]
Definition: ttmathuint.h:81
unsigned long uint
Definition: ttmathtypes.h:238

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

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

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)

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

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

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)

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)

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

§ ClearFirstBits()

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

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

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

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

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

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

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

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

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

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 
)

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 
)

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 
)

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 
)

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 
)

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 
)

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

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

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

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

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

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/5]

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

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

this operation has mainly sense if the value from p is equal or smaller than that one which is returned from UInt<value_size>::SetMax()

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

Definition at line 2698 of file ttmathuint.h.

Referenced by ttmath::UInt< man >::FromInt(), ttmath::UInt< man >::operator=(), and ttmath::UInt< man >::UInt().

2699  {
2700  return FromUInt(p);
2701  }
uint FromUInt(const UInt< argument_size > &p)
Definition: ttmathuint.h:2657

§ FromInt() [2/5]

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

this method converts the uint type to this class

Definition at line 2724 of file ttmathuint.h.

2725  {
2726  return FromUInt(value);
2727  }
uint FromUInt(const UInt< argument_size > &p)
Definition: ttmathuint.h:2657

§ FromInt() [3/5]

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

this method converts the sint type to this class

Definition at line 2733 of file ttmathuint.h.

2734  {
2735  uint c = FromUInt(uint(value));
2736 
2737  if( c || value < 0 )
2738  return 1;
2739 
2740  return 0;
2741  }
uint FromUInt(const UInt< argument_size > &p)
Definition: ttmathuint.h:2657
unsigned long uint
Definition: ttmathtypes.h:238

§ FromInt() [4/5]

template<uint value_size>
uint ttmath::UInt< 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 2932 of file ttmathuint.h.

2933  {
2934  return FromUInt(uint(i));
2935  }
uint FromUInt(const UInt< argument_size > &p)
Definition: ttmathuint.h:2657
unsigned long uint
Definition: ttmathtypes.h:238

§ FromInt() [5/5]

template<uint value_size>
uint ttmath::UInt< 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 2942 of file ttmathuint.h.

2943  {
2944  return FromInt(sint(i));
2945  }
signed long sint
Definition: ttmathtypes.h:243
uint FromInt(const UInt< argument_size > &p)
Definition: ttmathuint.h:2698

§ FromString() [1/4]

template<uint value_size>
uint ttmath::UInt< 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

if the value from s is too large the rest digits will be skipped

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 3474 of file ttmathuint.h.

Referenced by ttmath::UInt< man >::FromString(), ttmath::UInt< man >::operator=(), and ttmath::UInt< man >::UInt().

3475  {
3476  return FromStringBase(s, b, after_source, value_read);
3477  }

§ FromString() [2/4]

template<uint value_size>
uint ttmath::UInt< 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 3485 of file ttmathuint.h.

3486  {
3487  return FromString( s.c_str(), b );
3488  }
uint FromString(const char *s, uint b=10, const char **after_source=0, bool *value_read=0)
Definition: ttmathuint.h:3474

§ FromString() [3/4]

template<uint value_size>
uint ttmath::UInt< 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 3519 of file ttmathuint.h.

3520  {
3521  return FromStringBase(s, b, after_source, value_read);
3522  }

§ FromString() [4/4]

template<uint value_size>
uint ttmath::UInt< 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 3530 of file ttmathuint.h.

3531  {
3532  return FromString( s.c_str(), b );
3533  }
uint FromString(const char *s, uint b=10, const char **after_source=0, bool *value_read=0)
Definition: ttmathuint.h:3474

§ FromUInt() [1/3]

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

conversion methods

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

this operation has mainly sense if the value from p is equal or smaller than that one which is returned from UInt<value_size>::SetMax()

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

Definition at line 2657 of file ttmathuint.h.

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

2658  {
2659  uint min_size = (value_size < argument_size)? value_size : argument_size;
2660  uint i;
2661 
2662  for(i=0 ; i<min_size ; ++i)
2663  table[i] = p.table[i];
2664 
2665 
2666  if( value_size > argument_size )
2667  {
2668  // 'this' is longer than 'p'
2669 
2670  for( ; i<value_size ; ++i)
2671  table[i] = 0;
2672  }
2673  else
2674  {
2675  for( ; i<argument_size ; ++i)
2676  if( p.table[i] != 0 )
2677  {
2678  TTMATH_LOGC("UInt::FromUInt(UInt<>)", 1)
2679  return 1;
2680  }
2681  }
2682 
2683  TTMATH_LOGC("UInt::FromUInt(UInt<>)", 0)
2684 
2685  return 0;
2686  }
uint FromUInt(const UInt< argument_size > &p)
Definition: ttmathuint.h:2657
uint table[value_size]
Definition: ttmathuint.h:81
unsigned long uint
Definition: ttmathtypes.h:238

§ FromUInt() [2/3]

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

this method converts the uint type to this class

Definition at line 2707 of file ttmathuint.h.

2708  {
2709  for(uint i=1 ; i<value_size ; ++i)
2710  table[i] = 0;
2711 
2712  table[0] = value;
2713 
2714  TTMATH_LOG("UInt::FromUInt(uint)")
2715 
2716  // there'll never be a carry here
2717  return 0;
2718  }
uint table[value_size]
Definition: ttmathuint.h:81
unsigned long uint
Definition: ttmathtypes.h:238

§ FromUInt() [3/3]

template<uint value_size>
uint ttmath::UInt< 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 2923 of file ttmathuint.h.

2924  {
2925  return FromUInt(uint(i));
2926  }
uint FromUInt(const UInt< argument_size > &p)
Definition: ttmathuint.h:2657
unsigned long uint
Definition: ttmathtypes.h:238

§ GetBit()

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

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

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

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

§ IsTheHighestBitSet()

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

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

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

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

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

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

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

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)

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 
)

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)

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 
)

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)

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 
)

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 
)

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)

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 
)

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/2]

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

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() [2/2]

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

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

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::UInt< value_size >::operator++ ( )

Prefix operator e.g ++variable

Definition at line 3849 of file ttmathuint.h.

3850  {
3851  AddOne();
3852 
3853  return *this;
3854  }
uint AddOne()
Definition: ttmathuint.h:386

§ operator++() [2/2]

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

Postfix operator e.g variable++

Definition at line 3860 of file ttmathuint.h.

3861  {
3862  UInt<value_size> temp( *this );
3863 
3864  AddOne();
3865 
3866  return temp;
3867  }
uint AddOne()
Definition: ttmathuint.h:386

§ operator-()

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

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/10]

template<uint value_size>
template<uint argument_size>
UInt<value_size>& ttmath::UInt< 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 2750 of file ttmathuint.h.

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

2751  {
2752  FromUInt(p);
2753 
2754  return *this;
2755  }
uint FromUInt(const UInt< argument_size > &p)
Definition: ttmathuint.h:2657

§ operator=() [2/10]

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

the assignment operator

Definition at line 2761 of file ttmathuint.h.

2762  {
2763  for(uint i=0 ; i<value_size ; ++i)
2764  table[i] = p.table[i];
2765 
2766  TTMATH_LOG("UInt::operator=(UInt<>)")
2767 
2768  return *this;
2769  }
uint table[value_size]
Definition: ttmathuint.h:81
unsigned long uint
Definition: ttmathtypes.h:238

§ operator=() [3/10]

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

this method converts the uint type to this class

Definition at line 2775 of file ttmathuint.h.

2776  {
2777  FromUInt(i);
2778 
2779  return *this;
2780  }
uint FromUInt(const UInt< argument_size > &p)
Definition: ttmathuint.h:2657

§ operator=() [4/10]

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

this method converts the sint type to this class

Definition at line 2795 of file ttmathuint.h.

2796  {
2797  FromInt(i);
2798 
2799  return *this;
2800  }
uint FromInt(const UInt< argument_size > &p)
Definition: ttmathuint.h:2698

§ operator=() [5/10]

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

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

Definition at line 2952 of file ttmathuint.h.

2953  {
2954  FromUInt(i);
2955 
2956  return *this;
2957  }
uint FromUInt(const UInt< argument_size > &p)
Definition: ttmathuint.h:2657

§ operator=() [6/10]

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

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

Definition at line 2974 of file ttmathuint.h.

2975  {
2976  FromInt(i);
2977 
2978  return *this;
2979  }
uint FromInt(const UInt< argument_size > &p)
Definition: ttmathuint.h:2698

§ operator=() [7/10]

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

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

Definition at line 3494 of file ttmathuint.h.

3495  {
3496  FromString(s);
3497 
3498  return *this;
3499  }
uint FromString(const char *s, uint b=10, const char **after_source=0, bool *value_read=0)
Definition: ttmathuint.h:3474

§ operator=() [8/10]

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

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

Definition at line 3505 of file ttmathuint.h.

3506  {
3507  FromString( s.c_str() );
3508 
3509  return *this;
3510  }
uint FromString(const char *s, uint b=10, const char **after_source=0, bool *value_read=0)
Definition: ttmathuint.h:3474

§ operator=() [9/10]

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

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

Definition at line 3539 of file ttmathuint.h.

3540  {
3541  FromString(s);
3542 
3543  return *this;
3544  }
uint FromString(const char *s, uint b=10, const char **after_source=0, bool *value_read=0)
Definition: ttmathuint.h:3474

§ operator=() [10/10]

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

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

Definition at line 3550 of file ttmathuint.h.

3551  {
3552  FromString( s.c_str() );
3553 
3554  return *this;
3555  }
uint FromString(const char *s, uint b=10, const char **after_source=0, bool *value_read=0)
Definition: ttmathuint.h:3474

§ operator~()

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

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

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

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

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

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

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

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

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 
)

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 
)

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)

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

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 
)

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::UInt< value_size >::SetMax ( )

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

Definition at line 215 of file ttmathuint.h.

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

216  {
217  for(uint i=0 ; i<value_size ; ++i)
219 
220  TTMATH_LOG("UInt::SetMax")
221  }
uint table[value_size]
Definition: ttmathuint.h:81
#define TTMATH_UINT_MAX_VALUE
Definition: ttmathtypes.h:264
unsigned long uint
Definition: ttmathtypes.h:238

§ SetMin()

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

this method sets the min value which this class can hold (for an unsigned integer value the zero is the smallest value)

Definition at line 228 of file ttmathuint.h.

229  {
230  SetZero();
231 
232  TTMATH_LOG("UInt::SetMin")
233  }
void SetZero()
Definition: ttmathuint.h:188

§ SetOne()

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

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

§ SetZero()

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

§ Size()

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

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

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

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

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::UInt< value_size >::SubInt ( uint  value,
uint  index = 0 
)

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

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

if we've got (value_size=3):

table[0] = 10;
table[1] = 30;
table[2] = 5;   

and we call:

SubInt(2,1)

then it'll be:

table[0] = 10;
table[1] = 30 - 2;
table[2] = 5;

of course if there was a carry from table[2] it would be returned

Definition at line 530 of file ttmathuint_x86_64.h.

Referenced by ttmath::Int< exp >::SubInt(), and ttmath::UInt< man >::SubOne().

531  {
532  uint b = value_size;
533  uint * p1 = table;
534  uint c;
535 
536  TTMATH_ASSERT( index < value_size )
537 
538  #ifndef __GNUC__
539  c = ttmath_subindexed_x64(p1,b,index,value);
540  #endif
541 
542 
543  #ifdef __GNUC__
544  uint dummy, dummy2;
545 
546  __asm__ __volatile__(
547 
548  "subq %%rdx, %%rcx \n"
549 
550  "1: \n"
551  "subq %%rax, (%%rbx,%%rdx,8) \n"
552  "jnc 2f \n"
553 
554  "movq $1, %%rax \n"
555  "incq %%rdx \n"
556  "decq %%rcx \n"
557  "jnz 1b \n"
558 
559  "2: \n"
560  "setc %%al \n"
561  "movzx %%al, %%rdx \n"
562 
563  : "=d" (c), "=a" (dummy), "=c" (dummy2)
564  : "0" (index), "1" (value), "2" (b), "b" (p1)
565  : "cc", "memory" );
566 
567  #endif
568 
569  TTMATH_LOGC("UInt::SubInt", c)
570 
571  return c;
572  }
uint table[value_size]
Definition: ttmathuint.h:81
unsigned long uint
Definition: ttmathtypes.h:238

§ SubOne()

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

this method subtracts one from the existing value

Definition at line 395 of file ttmathuint.h.

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

396  {
397  return SubInt(1);
398  }
uint SubInt(uint value, uint index=0)

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

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)

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/4]

template<uint value_size>
uint ttmath::UInt< 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 3129 of file ttmathuint.h.

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

3130  {
3131  return ToUInt(result);
3132  }
uint ToUInt() const
Definition: ttmathuint.h:3103

§ ToInt() [2/4]

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

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

Definition at line 3139 of file ttmathuint.h.

3140  {
3141  result = sint(table[0]);
3142 
3143  if( (result & TTMATH_UINT_HIGHEST_BIT) != 0 )
3144  return 1;
3145 
3146  for(uint i=1 ; i<value_size ; ++i)
3147  if( table[i] != 0 )
3148  return 1;
3149 
3150  return 0;
3151  }
signed long sint
Definition: ttmathtypes.h:243
uint table[value_size]
Definition: ttmathuint.h:81
#define TTMATH_UINT_HIGHEST_BIT
Definition: ttmathtypes.h:258
unsigned long uint
Definition: ttmathtypes.h:238

§ ToInt() [3/4]

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

this method converts the value to a 32 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 3244 of file ttmathuint.h.

3245  {
3246  return ToUInt(result);
3247  }
uint ToUInt() const
Definition: ttmathuint.h:3103

§ ToInt() [4/4]

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

this method converts the value to a 32 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 3255 of file ttmathuint.h.

3256  {
3257  unsigned int temp;
3258 
3259  uint c = ToUInt(temp);
3260  result = int(temp);
3261 
3262  if( c || result < 0 )
3263  return 1;
3264 
3265  return 0;
3266  }
unsigned long uint
Definition: ttmathtypes.h:238
uint ToUInt() const
Definition: ttmathuint.h:3103

§ ToString()

template<uint value_size>
void ttmath::UInt< 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 3370 of file ttmathuint.h.

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

3371  {
3372  return ToStringBase(result, b);
3373  }
void ToStringBase(string_type &result, uint b=10, bool negative=false) const
Definition: ttmathuint.h:3316

§ 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

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
protected

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::UInt< value_size >::ToUInt ( ) const

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

§ ToUInt() [2/3]

template<uint value_size>
uint ttmath::UInt< 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 3113 of file ttmathuint.h.

3114  {
3115  result = table[0];
3116 
3117  for(uint i=1 ; i<value_size ; ++i)
3118  if( table[i] != 0 )
3119  return 1;
3120 
3121  return 0;
3122  }
uint table[value_size]
Definition: ttmathuint.h:81
unsigned long uint
Definition: ttmathtypes.h:238

§ ToUInt() [3/3]

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

this method converts the value to a 32 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 3224 of file ttmathuint.h.

3225  {
3226  result = (unsigned int)table[0];
3227 
3228  if( (table[0] >> 32) != 0 )
3229  return 1;
3230 
3231  for(uint i=1 ; i<value_size ; ++i)
3232  if( table[i] != 0 )
3233  return 1;
3234 
3235  return 0;
3236  }
uint table[value_size]
Definition: ttmathuint.h:81
unsigned long uint
Definition: ttmathtypes.h:238

Friends And Related Function Documentation

§ operator<< [1/2]

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

output to standard streams

Definition at line 4028 of file ttmathuint.h.

4029  {
4030  return OutputToStream<std::ostream, std::string>(s, l);
4031  }

§ operator<< [2/2]

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

output to standard streams

Definition at line 4039 of file ttmathuint.h.

4040  {
4041  return OutputToStream<std::wostream, std::wstring>(s, l);
4042  }

§ operator>> [1/2]

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

input from standard streams

Definition at line 4086 of file ttmathuint.h.

4087  {
4088  return InputFromStream<std::istream, std::string, char>(s, l);
4089  }

§ operator>> [2/2]

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

input from standard streams

Definition at line 4097 of file ttmathuint.h.

4098  {
4099  return InputFromStream<std::wistream, std::wstring, wchar_t>(s, l);
4100  }

Field Documentation

§ table

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

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