40 #ifndef headerfilettmathuint 41 #define headerfilettmathuint 72 template<u
int value_size>
96 template<
class ostream_type>
100 const int columns = 8;
103 for(
int i=value_size-1 ; i>=0 ; --i)
105 output <<
"0x" << std::setfill(
'0');
107 #ifdef TTMATH_PLATFORM32 108 output << std::setw(8);
110 output << std::setw(16);
113 output << std::hex << table[i];
127 output << std::dec << std::endl;
134 template<
class char_type,
class ostream_type>
137 output << msg << std::endl;
139 for(
uint i=0 ; i<vector_len ; ++i)
140 output <<
" table[" << i <<
"]: " << vector[i] << std::endl;
147 template<
class char_type,
class ostream_type>
151 output <<
" carry: " << carry << std::endl;
158 template<
class char_type,
class ostream_type>
159 void PrintLog(
const char_type * msg, ostream_type & output)
const 168 template<
class char_type,
class ostream_type>
169 void PrintLog(
const char_type * msg,
uint carry, ostream_type & output)
const 172 output <<
" carry: " << carry << std::endl;
192 for(
uint i=0 ; i<value_size ; ++i)
195 TTMATH_LOG(
"UInt::SetZero")
207 TTMATH_LOG(
"UInt::SetOne")
217 for(
uint i=0 ; i<value_size ; ++i)
220 TTMATH_LOG(
"UInt::SetMax")
232 TTMATH_LOG(
"UInt::SetMin")
241 for(
uint i=0 ; i<value_size ; ++i)
243 uint temp = table[i];
244 table[i] = ss2.
table[i];
250 #ifdef TTMATH_PLATFORM32 268 uint temp_table_index = 0;
271 for(i=value_size-1 ; i>=0 && temp_table_index<temp_table_len; --i, ++temp_table_index)
272 table[i] = temp_table[ temp_table_index ];
276 if( temp_table_index < temp_table_len )
300 TTMATH_LOG(
"UInt::SetFromTable")
306 #ifdef TTMATH_PLATFORM64 327 uint temp_table_index = 0;
330 for(i=value_size-1 ; i>=0 && temp_table_index<temp_table_len; --i, ++temp_table_index)
332 table[i] =
uint(temp_table[ temp_table_index ]) << 32;
336 if( temp_table_index<temp_table_len )
337 table[i] |= temp_table[ temp_table_index ];
342 if( temp_table_index < temp_table_len )
365 TTMATH_LOG(
"UInt::SetFromTable")
416 if( all_words >= value_size )
418 if( all_words == value_size && rest_bits == 0 )
419 last_c = table[0] & 1;
423 for(
uint i = 0 ; i<value_size ; ++i)
434 last_c = table[value_size - all_words] & 1;
437 for(first = value_size-1, second=first-all_words ; second>=0 ; --first, --second)
438 table[first] = table[second];
441 for( ; first>=0 ; --first )
445 TTMATH_LOG(
"UInt::RclMoveAllWords")
463 uint rest_bits = bits;
469 RclMoveAllWords(rest_bits, last_c, bits, c);
473 TTMATH_LOG(
"UInt::Rcl")
480 last_c = Rcl2_one(c);
482 else if( rest_bits == 2 )
486 last_c = Rcl2_one(c);
490 last_c = Rcl2(rest_bits, c);
493 TTMATH_LOGC(
"UInt::Rcl", last_c)
512 if( all_words >= value_size )
514 if( all_words == value_size && rest_bits == 0 )
519 for(
uint i = 0 ; i<value_size ; ++i)
524 else if( all_words > 0 )
532 for(first=0, second=all_words ; second<value_size ; ++first, ++second)
533 table[first] = table[second];
536 for( ; first<value_size ; ++first )
540 TTMATH_LOG(
"UInt::RcrMoveAllWords")
558 uint rest_bits = bits;
564 RcrMoveAllWords(rest_bits, last_c, bits, c);
568 TTMATH_LOG(
"UInt::Rcr")
575 last_c = Rcr2_one(c);
577 else if( rest_bits == 2 )
581 last_c = Rcr2_one(c);
585 last_c = Rcr2(rest_bits, c);
588 TTMATH_LOGC(
"UInt::Rcr", last_c)
604 for(a=value_size-1 ; a>=0 && table[a]==0 ; --a);
609 if( a != value_size-1 )
615 for(i=value_size-1 ; a>=0 ; --i, --a)
623 uint moving2 = FindLeadingBitInWord( table[value_size-1] );
630 TTMATH_LOG(
"UInt::CompensationToLeft")
632 return moving + moving2;
651 for(table_id=value_size-1 ; table_id!=0 && table[table_id]==0 ; --table_id);
653 if( table_id==0 && table[table_id]==0 )
662 index = FindLeadingBitInWord( table[table_id] );
683 for(table_id=0 ; table_id<value_size && table[table_id]==0 ; ++table_id);
685 if( table_id >= value_size )
695 index = FindLowestBitInWord( table[table_id] );
713 uint temp = table[index];
734 TTMATH_LOG(
"UInt::SetBit")
745 for(
uint x=0 ; x<value_size ; ++x)
746 table[x] &= ss2.
table[x];
748 TTMATH_LOG(
"UInt::BitAnd")
757 for(
uint x=0 ; x<value_size ; ++x)
758 table[x] |= ss2.
table[x];
760 TTMATH_LOG(
"UInt::BitOr")
769 for(
uint x=0 ; x<value_size ; ++x)
770 table[x] ^= ss2.
table[x];
772 TTMATH_LOG(
"UInt::BitXor")
781 for(
uint x=0 ; x<value_size ; ++x)
782 table[x] = ~table[x];
784 TTMATH_LOG(
"UInt::BitNot")
797 uint table_id, index;
801 for(
uint x=0 ; x<table_id ; ++x)
802 table[x] = ~table[x];
810 table[table_id] ^= mask;
816 TTMATH_LOG(
"UInt::BitNot2")
845 TTMATH_LOGC(
"UInt::MulInt(uint)", 0)
849 for(x1=0 ; x1<value_size-1 ; ++x1)
857 c += (r2!=0) ? 1 : 0;
860 TTMATH_LOGC(
"UInt::MulInt(uint)", c)
862 return (c==0)? 0 : 1;
872 template<u
int result_size>
875 TTMATH_ASSERT( result_size > value_size )
878 uint x1size=value_size;
885 TTMATH_VECTOR_LOG(
"UInt::MulInt(uint, UInt<>)", result.
table, result_size)
894 for(x1size=value_size ; x1size>0 && table[x1size-1]==0 ; --x1size);
898 TTMATH_VECTOR_LOG(
"UInt::MulInt(uint, UInt<>)", result.
table, result_size)
902 for(x1start=0 ; x1start<x1size && table[x1start]==0 ; ++x1start);
905 for(
uint x1=x1start ; x1<x1size ; ++x1)
911 TTMATH_VECTOR_LOG(
"UInt::MulInt(uint, UInt<>)", result.
table, result_size)
953 uint algorithm = 100)
999 TTMATH_LOGC(
"UInt::Mul1", 1)
1006 TTMATH_LOGC(
"UInt::Mul1", 1)
1011 TTMATH_LOGC(
"UInt::Mul1", 0)
1028 return Mul1Ref(copy_ss2);
1032 return Mul1Ref(ss2);
1049 for(i=0 ; i<value_size ; ++i)
1051 result.
table[i] = table[i];
1056 for( ; i < value_size*2 ; ++i)
1058 result.
table[i] = 0;
1066 TTMATH_LOG(
"UInt::Mul1Big")
1090 for(i=0 ; i<value_size ; ++i)
1091 table[i] = result.
table[i];
1094 for( ; i<value_size*2 ; ++i)
1095 if( result.
table[i] != 0 )
1101 TTMATH_LOGC(
"UInt::Mul2", c)
1115 Mul2Big2<value_size>(
table, ss2.
table, result);
1117 TTMATH_LOG(
"UInt::Mul2Big")
1130 template<u
int ss_size>
1133 uint x1size = ss_size, x2size = ss_size;
1134 uint x1start = 0, x2start = 0;
1141 for(x1size=ss_size ; x1size>0 && ss1[x1size-1]==0 ; --x1size);
1142 for(x2size=ss_size ; x2size>0 && ss2[x2size-1]==0 ; --x2size);
1144 for(x1start=0 ; x1start<x1size && ss1[x1start]==0 ; ++x1start);
1145 for(x2start=0 ; x2start<x2size && ss2[x2start]==0 ; ++x2start);
1148 Mul2Big3<ss_size>(ss1, ss2, result, x1start, x1size, x2start, x2size);
1156 template<u
int ss_size>
1163 if( x1size==0 || x2size==0 )
1166 for(
uint x1=x1start ; x1<x1size ; ++x1)
1168 for(
uint x2=x2start ; x2<x2size ; ++x2)
1220 for(i=0 ; i<value_size ; ++i)
1221 table[i] = result.
table[i];
1224 for( ; i<value_size*2 ; ++i)
1225 if( result.
table[i] != 0 )
1231 TTMATH_LOGC(
"UInt::Mul3", c)
1249 TTMATH_LOG(
"UInt::Mul3Big")
1261 template<u
int ss_size>
1262 void Mul3Big2(
const uint * ss1,
const uint * ss2,
uint * result)
1264 const uint * x1, * x0, * y1, * y0;
1270 Mul2Big2<ss_size>(ss1, ss2, res);
1273 #pragma clang diagnostic push 1274 #pragma clang diagnostic ignored "-Wtautological-compare" 1277 for(
uint i=0 ; i<ss_size*2 ; ++i)
1278 result[i] = res.
table[i];
1281 #pragma clang diagnostic pop
1289 return MulTwoWords(*ss1, *ss2, &result[1], &result[0]);
1293 if( (ss_size & 1) == 1 )
1298 x1 = ss1 + ss_size / 2 + 1;
1299 y1 = ss2 + ss_size / 2 + 1;
1302 Mul3Big3<ss_size/2 + 1, ss_size/2, ss_size*2>(x1, x0, y1, y0, result);
1309 x1 = ss1 + ss_size / 2;
1310 y1 = ss2 + ss_size / 2;
1313 Mul3Big3<ss_size/2, ss_size/2, ss_size*2>(x1, x0, y1, y0, result);
1320 #pragma warning (disable : 4717) 1325 #if defined(__GNUC__) && !defined(__clang__) 1326 #pragma GCC diagnostic push 1327 #pragma GCC diagnostic ignored "-Wunused-but-set-variable" 1346 template<u
int first_size, u
int second_size, u
int result_size>
1347 void Mul3Big3(
const uint * x1,
const uint * x0,
const uint * y1,
const uint * y0,
uint * result)
1355 Mul3Big2<first_size>(x0, y0, result);
1356 Mul3Big2<second_size>(x1, y1, result+first_size*2);
1382 #pragma clang diagnostic push 1383 #pragma clang diagnostic ignored "-Wtautological-compare" 1387 for(i=first_size*2 ; i<first_size*3 ; ++i)
1391 #pragma clang diagnostic pop
1397 TTMATH_ASSERT( c==0 )
1403 TTMATH_ASSERT( c==0 )
1410 #pragma clang diagnostic push 1411 #pragma clang diagnostic ignored "-Wtautological-compare" 1414 for( i=first_size*2 ; i<first_size*3 ; ++i )
1415 if( ++z1.
table[i] != 0 )
1419 #pragma clang diagnostic pop 1434 if( first_size > second_size )
1436 uint z1_size = result_size - first_size;
1437 TTMATH_ASSERT( z1_size <= first_size*3 )
1440 #pragma clang diagnostic push 1441 #pragma clang diagnostic ignored "-Wtautological-compare" 1444 for(i=z1_size ; i<first_size*3 ; ++i)
1446 TTMATH_ASSERT( z1.
table[i] == 0 )
1450 #pragma clang diagnostic pop 1453 c =
AddVector(result+first_size, z1.
table, result_size-first_size, z1_size, result+first_size);
1458 c =
AddVector(result+first_size, z1.
table, result_size-first_size, first_size*3, result+first_size);
1464 #if defined(__GNUC__) && !defined(__clang__) 1465 #pragma GCC diagnostic pop 1469 #pragma warning (default : 4717) 1487 for(i=0 ; i<value_size ; ++i)
1488 table[i] = result.
table[i];
1491 for( ; i<value_size*2 ; ++i)
1492 if( result.
table[i] != 0 )
1498 TTMATH_LOGC(
"UInt::MulFastest", c)
1518 uint x1size = value_size, x2size = value_size;
1519 uint x1start = 0, x2start = 0;
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);
1524 if( x1size==0 || x2size==0 )
1531 for(x1start=0 ; x1start<x1size && table[x1start]==0 ; ++x1start);
1532 for(x2start=0 ; x2start<x2size && ss2.
table[x2start]==0 ; ++x2start);
1534 uint distancex1 = x1size - x1start;
1535 uint distancex2 = x2size - x2start;
1537 if( distancex1 < 3 || distancex2 < 3 )
1541 Mul2Big3<value_size>(
table, ss2.
table, result, x1start, x1size, x2start, x2size);
1549 TTMATH_LOG(
"UInt::MulFastestBig")
1575 TTMATH_LOG(
"UInt::DivInt")
1585 TTMATH_LOG(
"UInt::DivInt")
1597 for(i=value_size-1 ; i>0 && dividend.
table[i]==0 ; --i);
1605 TTMATH_LOG(
"UInt::DivInt")
1612 return DivInt(divisor, &remainder);
1633 return Div1(divisor, remainder);
1636 return Div2(divisor, remainder);
1640 return Div3(divisor, remainder);
1646 return Div(divisor, &remainder, algorithm);
1663 switch( Div_CalculatingSize(v, m, n) )
1670 TTMATH_LOG(
"UInt::Div_StandardTest")
1678 TTMATH_LOG(
"UInt::Div_StandardTest")
1686 TTMATH_LOG(
"UInt::Div_StandardTest")
1690 TTMATH_LOG(
"UInt::Div_StandardTest")
1694 TTMATH_LOG(
"UInt::Div_StandardTest")
1715 m = n = value_size-1;
1717 for( ; n!=0 && v.
table[n]==0 ; --n);
1719 if( n==0 && v.
table[n]==0 )
1722 for( ; m!=0 && table[m]==0 ; --m);
1724 if( m==0 && table[m]==0 )
1733 for(i = n ; i!=0 && table[i]==v.
table[i] ; --i);
1735 if( table[i] < v.
table[i] )
1738 if (table[i] == v.
table[i] )
1756 test = Div_StandardTest(divisor, m, n, remainder);
1764 return Div1_Calculate(divisor, rem);
1767 return Div1_Calculate(divisor, *remainder);
1777 return Div1(divisor, &remainder);
1785 if(
this == &divisor )
1788 return Div1_CalculateRef(divisor_copy, rest);
1792 return Div1_CalculateRef(divisor, rest);
1811 c = rest.
Add(rest,c);
1812 c = rest.
Sub(divisor,c);
1826 TTMATH_LOG(
"UInt::Div1_Calculate")
1832 c = rest.
Add(rest,c);
1833 c = rest.
Add(divisor);
1845 c = rest.
Add(divisor);
1847 TTMATH_LOG(
"UInt::Div1_Calculate")
1864 if(
this == &divisor )
1867 return Div2Ref(divisor_copy, remainder);
1871 return Div2Ref(divisor, remainder);
1885 return Div2(divisor, &remainder);
1901 uint status = Div2_Calculate(divisor, remainder, bits_diff);
1907 Div2(divisor, remainder);
1919 TTMATH_LOG(
"UInt::Div2")
1935 uint table_id, index;
1936 uint divisor_table_id, divisor_index;
1938 uint status = Div2_FindLeadingBitsAndCheck( divisor, remainder,
1940 divisor_table_id, divisor_index);
1944 TTMATH_LOG(
"UInt::Div2_Calculate")
1950 bits_diff = index - divisor_index;
1953 divisor_copy.
Rcl(bits_diff, 0);
1957 divisor_copy.
Rcr(1);
1961 Sub(divisor_copy, 0);
1963 TTMATH_LOG(
"UInt::Div2_Calculate")
1978 uint & divisor_table_id,
uint & divisor_index)
1983 TTMATH_LOG(
"UInt::Div2_FindLeadingBitsAndCheck")
1996 TTMATH_LOG(
"UInt::Div2_FindLeadingBitsAndCheck")
2004 if( divisor_table_id == 0 )
2014 remainder->
table[0] = r;
2017 TTMATH_LOG(
"UInt::Div2_FindLeadingBitsAndCheck")
2023 if( Div2_DivisorGreaterOrEqual( divisor, remainder,
2027 TTMATH_LOG(
"UInt::Div2_FindLeadingBitsAndCheck")
2032 TTMATH_LOG(
"UInt::Div2_FindLeadingBitsAndCheck")
2045 uint divisor_index )
2047 if( divisor_index > index )
2056 TTMATH_LOG(
"UInt::Div2_DivisorGreaterOrEqual")
2061 if( divisor_index == index )
2066 for(i = table_id ; i!=0 && table[i]==divisor.
table[i] ; --i);
2068 if( table[i] < divisor.
table[i] )
2077 TTMATH_LOG(
"UInt::Div2_DivisorGreaterOrEqual")
2082 if( table[i] == divisor.
table[i] )
2091 TTMATH_LOG(
"UInt::Div2_DivisorGreaterOrEqual")
2097 TTMATH_LOG(
"UInt::Div2_DivisorGreaterOrEqual")
2113 return Div3Ref(copy_ss2, remainder);
2117 return Div3Ref(ss2, remainder);
2127 return Div3(ss2, &remainder);
2145 test = Div_StandardTest(v, m, n, remainder);
2157 remainder->
table[0] = r;
2160 TTMATH_LOG(
"UInt::Div3")
2171 Div3_Division(v, remainder, m, n);
2173 TTMATH_LOG(
"UInt::Div3")
2185 TTMATH_ASSERT( n>=2 )
2189 uint d, u_value_size, u0, u1, u2, v1, v0, j=m;
2191 u_value_size = Div3_Normalize(v, n, d);
2193 if( j+n == value_size )
2198 Div3_MakeBiggerV(v, vv);
2200 for(
uint i = j+1 ; i<value_size ; ++i)
2210 uint qp = Div3_Calculate(u2,u1,u0, v1,v0);
2212 Div3_MakeNewU(uu, j, n, u2);
2213 Div3_MultiplySubtract(uu, vv, qp);
2214 Div3_CopyNewU(uu, j, n);
2226 Div3_Unnormalize(remainder, n, d);
2230 TTMATH_LOG(
"UInt::Div3_Division")
2238 for(i=0 ; i<n ; ++i, ++j)
2239 uu.
table[i] = table[j];
2243 uu.
table[i] = u_max;
2245 for( ++i ; i<value_size+1 ; ++i)
2248 TTMATH_LOG(
"UInt::Div3_MakeNewU")
2256 for(i=0 ; i<n ; ++i)
2257 table[i+j] = uu.
table[i];
2259 if( i+j < value_size )
2260 table[i+j] = uu.
table[i];
2262 TTMATH_LOG(
"UInt::Div3_CopyNewU")
2272 for(
uint i=0 ; i<value_size ; ++i)
2275 vv.
table[value_size] = 0;
2277 TTMATH_LOG(
"UInt::Div3_MakeBiggerV")
2296 uint res = table[value_size-1];
2303 res = res >> (bit + 1);
2310 TTMATH_LOG(
"UInt::Div3_Normalize")
2318 for(
uint i=n ; i<value_size ; ++i)
2325 TTMATH_LOG(
"UInt::Div3_Unnormalize")
2335 TTMATH_ASSERT( v1 != 0 )
2337 u_temp.
table[1] = u2;
2338 u_temp.
table[0] = u1;
2341 TTMATH_ASSERT( u_temp.
table[1]==0 || u_temp.
table[1]==1 )
2345 bool decrease =
false;
2347 if( u_temp.
table[1] == 1 )
2354 temp2.
table[1] = rp;
2355 temp2.
table[0] = u0;
2375 TTMATH_LOG(
"UInt::Div3_Calculate")
2377 return u_temp.
table[0];
2390 if( uu.
Sub(vv_temp) )
2405 TTMATH_LOG(
"UInt::Div3_MultiplySubtract")
2438 if( pow.
table[0] & 1 )
2439 c += result.
Mul(start);
2445 c += start.
Mul(start);
2450 TTMATH_LOGC(
"UInt::Pow(UInt<>)", c)
2452 return (c==0)? 0 : 1;
2474 while( bit > value )
2496 TTMATH_LOG(
"UInt::Sqrt")
2513 TTMATH_LOG(
"UInt::ClearFirstBits")
2520 while( n >= TTMATH_BITS_PER_UINT )
2528 TTMATH_LOG(
"UInt::ClearFirstBits")
2540 TTMATH_LOG(
"UInt::ClearFirstBits")
2558 return (*table & 1) != 0;
2568 #pragma clang diagnostic push 2569 #pragma clang diagnostic ignored "-Wtautological-compare" 2572 for(
uint i=0 ; i<value_size-1 ; ++i)
2577 #pragma clang diagnostic pop 2594 for(
uint i=1 ; i<value_size ; ++i)
2607 for(
uint i=0 ; i<value_size ; ++i)
2626 for(i=0 ; i<index ; ++i)
2635 return (table[i] & mask) == 0;
2656 template<u
int argument_size>
2659 uint min_size = (value_size < argument_size)? value_size : argument_size;
2662 for(i=0 ; i<min_size ; ++i)
2663 table[i] = p.
table[i];
2666 if( value_size > argument_size )
2670 for( ; i<value_size ; ++i)
2675 for( ; i<argument_size ; ++i)
2676 if( p.
table[i] != 0 )
2678 TTMATH_LOGC(
"UInt::FromUInt(UInt<>)", 1)
2683 TTMATH_LOGC(
"UInt::FromUInt(UInt<>)", 0)
2697 template<u
int argument_size>
2709 for(
uint i=1 ; i<value_size ; ++i)
2714 TTMATH_LOG(
"UInt::FromUInt(uint)")
2737 if( c || value < 0 )
2749 template<u
int argument_size>
2763 for(
uint i=0 ; i<value_size ; ++i)
2764 table[i] = p.
table[i];
2766 TTMATH_LOG(
"UInt::operator=(UInt<>)")
2814 #ifdef TTMATH_PLATFORM32 2825 if( value_size == 1 )
2829 TTMATH_LOGC(
"UInt::FromUInt(ulint)", c)
2835 for(
uint i=2 ; i<value_size ; ++i)
2838 TTMATH_LOG(
"UInt::FromUInt(ulint)")
2916 #ifdef TTMATH_PLATFORM64 3016 #ifndef TTMATH_DONT_USE_WCHAR 3051 #ifdef TTMATH_DEBUG_LOG 3052 #ifdef TTMATH_PLATFORM32 3053 for(
uint i=0 ; i<value_size ; ++i)
3054 table[i] = 0xc1c1c1c1;
3056 for(
uint i=0 ; i<value_size ; ++i)
3057 table[i] = 0xc1c1c1c1c1c1c1c1;
3068 for(
uint i=0 ; i<value_size ; ++i)
3069 table[i] = u.
table[i];
3071 TTMATH_LOG(
"UInt::UInt(UInt<>)")
3079 template<u
int argument_size>
3117 for(
uint i=1 ; i<value_size ; ++i)
3141 result =
sint(table[0]);
3146 for(
uint i=1 ; i<value_size ; ++i)
3154 #ifdef TTMATH_PLATFORM32 3163 if( value_size == 1 )
3169 uint low = table[0];
3170 uint high = table[1];
3175 for(
uint i=2 ; i<value_size ; ++i)
3205 result = slint(temp);
3207 if( c || result < 0 )
3217 #ifdef TTMATH_PLATFORM64 3226 result = (
unsigned int)table[0];
3228 if( (table[0] >> 32) != 0 )
3231 for(
uint i=1 ; i<value_size ; ++i)
3262 if( c || result < 0 )
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
3304 return log_tab[x-2];
3315 template<
class string_type>
3319 uint rest, table_id, index, digits;
3337 digits_d =
static_cast<double>(table_id);
3339 digits_d += index + 1;
3341 digits =
static_cast<uint>(digits_d) + 3;
3343 if( result.capacity() < digits )
3344 result.reserve(digits);
3350 result.insert(result.end(), character);
3354 size_t i1 = negative ? 1 : 0;
3355 size_t i2 = result.size() - 1;
3357 for( ; i1 < i2 ; ++i1, --i2 )
3359 char tempc =
static_cast<char>(result[i1]);
3360 result[i1] = result[i2];
3385 #ifndef TTMATH_DONT_USE_WCHAR 3387 void ToString(std::wstring & result,
uint b = 10)
const 3392 std::wstring ToWString(
uint b = 10)
const 3394 std::wstring result;
3409 template<
class char_type>
3410 uint FromStringBase(
const char_type * s,
uint b = 10,
const char_type ** after_source = 0,
bool * value_read = 0)
3419 Misc::SkipWhiteCharacters(s);
3425 *value_read =
false;
3448 TTMATH_LOGC(
"UInt::FromString", c)
3450 return (c==0)? 0 : 1;
3476 return FromStringBase(s, b, after_source, value_read);
3514 #ifndef TTMATH_DONT_USE_WCHAR 3519 uint FromString(
const wchar_t * s,
uint b = 10,
const wchar_t ** after_source = 0,
bool * value_read = 0)
3521 return FromStringBase(s, b, after_source, value_read);
3579 if( index==-1 || index>=
sint(value_size) )
3587 if( table[i] != l.
table[i] )
3588 return table[i] < l.
table[i];
3610 if( index==-1 || index>=
sint(value_size) )
3618 if( table[i] != l.
table[i] )
3619 return table[i] > l.
table[i];
3638 if( index==-1 || index>=
sint(value_size) )
3645 if( table[i] != l.
table[i] )
3664 if( index==-1 || index>=
sint(value_size) )
3672 if( table[i] != l.
table[i] )
3673 return table[i] < l.
table[i];
3693 if( index==-1 || index>=
sint(value_size) )
3701 if( table[i] != l.
table[i] )
3702 return table[i] > l.
table[i];
3714 bool operator<(const UInt<value_size> & l)
const 3734 return !operator==(l);
3738 bool operator<=(const UInt<value_size> & l)
const 3829 temp.
Div( p2, remainder );
3839 Div( p2, remainder );
4010 template<
class ostream_type,
class string_type>
4011 static ostream_type & OutputToStream(ostream_type & s,
const UInt<value_size> & l)
4028 friend std::ostream & operator<<(std::ostream & s, const UInt<value_size> & l)
4030 return OutputToStream<std::ostream, std::string>(s, l);
4034 #ifndef TTMATH_DONT_USE_WCHAR 4039 friend std::wostream & operator<<(std::wostream & s, const UInt<value_size> & l)
4041 return OutputToStream<std::wostream, std::wstring>(s, l);
4053 template<
class istream_type,
class string_type,
class char_type>
4054 static istream_type & InputFromStream(istream_type & s,
UInt<value_size> & l)
4068 z =
static_cast<char_type
>(s.get());
4088 return InputFromStream<std::istream, std::string, char>(s, l);
4092 #ifndef TTMATH_DONT_USE_WCHAR 4099 return InputFromStream<std::wistream, std::wstring, wchar_t>(s, l);
4116 #ifdef TTMATH_PLATFORM64 4133 static unsigned int DivTwoWordsCalculate(
uint_ u_,
unsigned int u3,
uint_ v_);
4134 static void MultiplySubtract(
uint_ & u_,
unsigned int & u3,
unsigned int & q,
uint_ v_);
4136 #endif // TTMATH_PLATFORM64 4137 #endif // TTMATH_NOASM 4156 static sint FindLeadingBitInWord(
uint x);
4157 static sint FindLowestBitInWord(
uint x);
4177 void SetZero() { TTMATH_ASSERT(
false) };
void SetFromTable(const unsigned int *temp_table, uint temp_table_len)
UInt< value_size > & operator=(const char *s)
uint Div1(const UInt< value_size > &divisor, UInt< value_size > *remainder=0)
UInt(const UInt< argument_size > &u)
uint Rcl(uint bits, uint c=0)
bool CmpSmaller(const UInt< value_size > &l, sint index=-1) const
void PrintLog(const char_type *msg, uint carry, ostream_type &output) const
void Mul2Big(const UInt< value_size > &ss2, UInt< value_size *2 > &result)
uint SubInt(uint value, uint index=0)
uint FromInt(const UInt< argument_size > &p)
UInt< value_size > & operator=(const std::wstring &s)
static uint CharToDigit(uint c)
uint FromUInt(const UInt< argument_size > &p)
void Mul3Big(const UInt< value_size > &ss2, UInt< value_size *2 > &result)
uint AddInt(uint value, uint index=0)
uint FromUInt(unsigned int i)
uint FromString(const char *s, uint b=10, const char **after_source=0, bool *value_read=0)
UInt< value_size > & operator=(const std::string &s)
uint Div3(const UInt< value_size > &ss2, UInt< value_size > *remainder=0)
UInt< value_size > operator-(const UInt< value_size > &p2) const
static const char * LibTypeStr()
bool CmpBiggerEqual(const UInt< value_size > &l, sint index=-1) const
UInt(const std::wstring &s)
UInt< value_size > & operator=(const wchar_t *s)
UInt< value_size > operator++(int)
UInt< value_size > & operator=(uint i)
static uint DigitToChar(uint digit)
uint ToInt(sint &result) const
uint Sub(const UInt< value_size > &ss2, uint c=0)
UInt(const UInt< value_size > &u)
uint Mul2(const UInt< value_size > &ss2)
bool IsTheLowestBitSet() const
uint Add(const UInt< value_size > &ss2, uint c=0)
UInt< value_size > & operator=(sint i)
void PrintLog(const char_type *msg, ostream_type &output) const
static LibTypeCode LibType()
double ToStringLog2(uint x) const
UInt< value_size > & operator=(const UInt< argument_size > &p)
bool IsTheHighestBitSet() const
friend std::istream & operator>>(std::istream &s, UInt< value_size > &l)
uint ToUInt(unsigned int &result) const
void MulBig(const UInt< value_size > &ss2, UInt< value_size *2 > &result, uint algorithm=100)
uint FromString(const std::string &s, uint b=10)
static void MulTwoWords(uint a, uint b, uint *result_high, uint *result_low)
bool CmpBigger(const UInt< value_size > &l, sint index=-1) const
void PrintTable(ostream_type &output) const
#define TTMATH_UINT_HIGHEST_BIT
bool IsOnlyTheHighestBitSet() const
template class UInt<uint> with methods without any assembler code (used for no-asm version of ttmath)...
static void PrintVectorLog(const char_type *msg, ostream_type &output, const uint *vector, uint vector_len)
uint ToUInt(uint &result) const
UInt< value_size > & operator=(const UInt< value_size > &p)
a namespace for the TTMath library
uint Div2(const UInt< value_size > &divisor, UInt< value_size > *remainder=0)
constants used in the library
bool IsOnlyTheLowestBitSet() const
static void PrintVectorLog(const char_type *msg, uint carry, ostream_type &output, const uint *vector, uint vector_len)
uint Div(const UInt< value_size > &divisor, UInt< value_size > *remainder=0, uint algorithm=3)
#define TTMATH_UINT_MAX_VALUE
bool CmpEqual(const UInt< value_size > &l, sint index=-1) const
bool AreFirstBitsZero(uint bits) const
uint Pow(UInt< value_size > pow)
void BitAnd(const UInt< value_size > &ss2)
uint FromInt(unsigned int i)
uint AddTwoInts(uint x2, uint x1, uint index)
uint Rcr(uint bits, uint c=0)
uint Div2(const UInt< value_size > &divisor, UInt< value_size > &remainder)
template class UInt<uint> with assembler code for 64bit x86_64 processors
void BitOr(const UInt< value_size > &ss2)
bool CmpSmallerEqual(const UInt< value_size > &l, sint index=-1) const
UInt< value_size > & operator=(signed int i)
UInt< value_size > operator~() const
#define TTMATH_BITS_PER_UINT
UInt< value_size > & operator=(unsigned int i)
void BitXor(const UInt< value_size > &ss2)
uint CompensationToLeft()
UInt implements a big integer value without a sign.
#define TTMATH_USE_KARATSUBA_MULTIPLICATION_FROM_SIZE
uint Div3(const UInt< value_size > &ss2, UInt< value_size > &remainder)
void ToStringBase(string_type &result, uint b=10, bool negative=false) const
uint ToInt(uint &result) const
uint MulFastest(const UInt< value_size > &ss2)
void MulFastestBig(const UInt< value_size > &ss2, UInt< value_size *2 > &result)
static uint SetBitInWord(uint &value, uint bit)
void Mul1Big(const UInt< value_size > &ss2_, UInt< value_size *2 > &result)
static uint AddVector(const uint *ss1, const uint *ss2, uint ss1_size, uint ss2_size, uint *result)
friend std::wistream & operator>>(std::wistream &s, UInt< value_size > &l)
void MulInt(uint ss2, UInt< result_size > &result) const
UInt(const std::string &s)
uint Mul3(const UInt< value_size > &ss2)
uint ToInt(unsigned int &result) const
void ToString(std::string &result, uint b=10) const
void ClearFirstBits(uint n)
uint Div1(const UInt< value_size > &divisor, UInt< value_size > &remainder)
uint Mul(const UInt< value_size > &ss2, uint algorithm=100)
UInt< value_size > & operator++()
uint FromUInt(uint value)
uint FromInt(signed int i)
uint SetBit(uint bit_index)
static void DivTwoWords(uint a, uint b, uint c, uint *r, uint *rest)
uint GetBit(uint bit_index) const
uint FromString(const wchar_t *s, uint b=10, const wchar_t **after_source=0, bool *value_read=0)
static uint SubVector(const uint *ss1, const uint *ss2, uint ss1_size, uint ss2_size, uint *result)
template class UInt<uint> with assembler code for 32bit x86 processors
uint DivInt(uint divisor, uint *remainder=0)
#define TTMATH_REFERENCE_ASSERT(expression)
uint ToInt(int &result) const
bool FindLowestBit(uint &table_id, uint &index) const
uint Mul1(const UInt< value_size > &ss2)
bool FindLeadingBit(uint &table_id, uint &index) const
void Swap(UInt< value_size > &ss2)
uint FromString(const std::wstring &s, uint b=10)