Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
Loading...
Searching...
No Matches
uint128_t Class Reference

class for 128-bit unsigned integer More...

#include <uint128_t.hpp>

Public Member Functions

template<typename T , typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
 uint128_t (T low)
 Parameterized constructor.
 
 uint128_t (const std::string &str)
 Parameterized constructor.
 
 uint128_t (const uint64_t high, const uint64_t low)
 Parameterized constructor.
 
 uint128_t (const uint128_t &num)=default
 Copy constructor.
 
 uint128_t (uint128_t &&num) noexcept
 Move constructor.
 
 ~uint128_t ()=default
 Destructor for uint128_t.
 
uint32_t _lez ()
 Leading zeroes in binary.
 
uint32_t _trz ()
 Trailing zeroes in binary.
 
 operator bool () const
 casting operator to boolean value
 
template<typename T , typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
 operator T () const
 casting operator to any integer valu
 
uint64_t lower () const
 returns lower 64-bit integer part
 
uint64_t upper () const
 returns upper 64-bit integer part
 
template<typename T , typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint128_toperator= (const T &p)
 operator = for other types
 
uint128_toperator= (const std::string &p)
 operator = for type string
 
uint128_toperator= (const uint128_t &p)=default
 operator = for uint128_t
 
uint128_toperator= (uint128_t &&p)=default
 Move assignment operator.
 
template<typename T , typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint128_t operator+ (const T p)
 operator + for uint128_t and other integer types.
 
uint128_t operator+ (const uint128_t &p)
 operator + for uint128_t and other integer types.
 
template<typename T , typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint128_toperator+= (const T p)
 operator += for uint128_t and other integer types.
 
uint128_toperator+= (const uint128_t &p)
 operator += for uint128_t
 
uint128_toperator++ ()
 pre-increment operator
 
uint128_t operator++ (int)
 post-increment operator
 
template<typename T , typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint128_t operator- (const T &p)
 operator - for uint128_t and other integer types.
 
uint128_t operator- (const uint128_t &p)
 operator - for uint128_t
 
uint128_t operator- ()
 operator - using twos complement
 
uint128_toperator-- ()
 operator – (pre-decrement)
 
uint128_t operator-- (int p)
 operator – (post-decrement)
 
template<typename T , typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint128_toperator-= (const T &p)
 operator -= for uint128_t and other integer types.
 
uint128_toperator-= (const uint128_t &p)
 operator -= for uint128_t
 
template<typename T , typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint128_t operator* (const T p)
 operator * for uint128_t and other integer types.
 
uint128_t operator* (const uint128_t &p)
 operator * for uint128_t and other integer types.
 
template<typename T , typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint128_toperator*= (const T p)
 operator *= for uint128_t and other integer types.
 
uint128_toperator*= (const uint128_t &p)
 operator *= for uint128_t and other integer types.
 
std::pair< uint128_t, uint128_tdivide (const uint128_t &p)
 divide function for uint128_t and other integer types.
 
uint128_t operator/ (const uint128_t &p)
 operator / for uint128_t and other integer types.
 
template<typename T , typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint128_t operator/ (const T p)
 operator / for uint128_t and other integer types.
 
uint128_toperator/= (const uint128_t &p)
 operator /= for uint128_t
 
template<typename T , typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint128_toperator/= (const T p)
 operator /= for uint128_t and other integer types.
 
uint128_t operator% (const uint128_t &p)
 operator % for uint128_t
 
template<typename T , typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint128_t operator% (const T &p)
 operator % for uint128_t and other integer types.
 
uint128_toperator%= (const uint128_t &p)
 operator %= for uint128_t
 
template<typename T , typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint128_toperator%= (const T &p)
 operator %= for uint128_t
 
bool operator< (const uint128_t &other)
 operator < for uint128_t
 
bool operator<= (const uint128_t &other)
 operator <= for uint128_t
 
bool operator> (const uint128_t &other)
 operator > for uint128_t
 
bool operator>= (const uint128_t &other)
 operator >= for uint128_t
 
bool operator== (const uint128_t &other)
 operator == for uint128_t
 
bool operator!= (const uint128_t &other)
 operator != for uint128_t
 
bool operator! ()
 operator ! for uint128_t
 
bool operator&& (const uint128_t &b)
 operator && for uint128_t
 
bool operator|| (const uint128_t &b)
 operator || for uint128_t
 
bool operator() ()
 operator () for uint128_t
 
template<typename T , typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
bool operator< (const T other)
 operator < for other types
 
template<typename T , typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
bool operator<= (const T other)
 operator <= for other types
 
template<typename T , typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
bool operator> (const T other)
 operator > for other types
 
template<typename T , typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
bool operator>= (const T other)
 operator >= for other types
 
template<typename T , typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
bool operator== (const T other)
 operator == for other types
 
template<typename T , typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
bool operator!= (const T other)
 operator != for other types
 
template<typename T , typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
bool operator&& (const T b)
 operator && for other types
 
template<typename T , typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
bool operator|| (const T b)
 operator || for other types
 
uint128_t operator~ ()
 operator ~ for uint128_t
 
template<typename T , typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint128_t operator<< (const T p)
 operator << for uint128_t
 
template<typename T , typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint128_toperator<<= (const T p)
 operator <<= for uint128_t
 
template<typename T , typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint128_t operator>> (const T p)
 operator >> for uint128_t
 
template<typename T , typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint128_toperator>>= (const T p)
 operator >>= for uint128_t
 
uint128_t operator& (const uint128_t &p)
 operator & for uint128_t (bitwise operator)
 
template<typename T , typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint128_t operator& (const T p)
 operator & for other types (bitwise operator)
 
uint128_toperator&= (const uint128_t &p)
 operator &= for uint128_t (bitwise operator)
 
template<typename T , typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint128_toperator&= (const T p)
 operator &= for other types (bitwise operator)
 
template<typename T , typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint128_t operator| (const T p)
 operator | for other types (bitwise operator)
 
uint128_t operator| (const uint128_t &p)
 operator | for uint128_t (bitwise operator)
 
uint128_toperator|= (const uint128_t &p)
 operator |= for uint128_t (bitwise operator)
 
template<typename T , typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint128_toperator|= (const T p)
 operator |= for other types (bitwise operator)
 
template<typename T , typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint128_t operator^ (const T p)
 operator ^ for other types (bitwise operator)
 
uint128_t operator^ (const uint128_t &p)
 operator ^ for uint128_t (bitwise operator)
 
uint128_toperator^= (const uint128_t &p)
 operator ^= for uint128_t (bitwise operator)
 
template<typename T , typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint128_toperator^= (const T &p)
 operator ^= for other types (bitwise operator)
 

Private Member Functions

void __get_integer_from_string (const std::string &str)
 First and second half of 128 bit number.
 

Private Attributes

uint64_t f {}
 
uint64_t s {}
 

Friends

std::ostreamoperator<< (std::ostream &op, const uint128_t &p)
 operator << for printing uint128_t integer
 

Detailed Description

class for 128-bit unsigned integer

Constructor & Destructor Documentation

◆ uint128_t() [1/5]

template<typename T , typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint128_t::uint128_t ( T low)
inlineexplicit

Parameterized constructor.

Template Parameters
Tintegral type
Parameters
lowlower part 8-bit unisgned integer
100: s(low) {}

◆ uint128_t() [2/5]

uint128_t::uint128_t ( const std::string & str)
inlineexplicit

Parameterized constructor.

Parameters
strInteger string (hexadecimal starting with 0x.. or decimal)
106 {
108 }
void __get_integer_from_string(const std::string &str)
First and second half of 128 bit number.
Definition uint128_t.hpp:69
Here is the call graph for this function:

◆ uint128_t() [3/5]

uint128_t::uint128_t ( const uint64_t high,
const uint64_t low )
inline

Parameterized constructor.

Parameters
highhigher part 64-bit unsigned integer
lowlower part 64-bit unsigned integer
115: f(high), s(low) {}

◆ uint128_t() [4/5]

uint128_t::uint128_t ( const uint128_t & num)
default

Copy constructor.

Parameters
num128-bit unsigned integer

◆ uint128_t() [5/5]

uint128_t::uint128_t ( uint128_t && num)
inlinenoexcept

Move constructor.

Parameters
num128-bit unsigned integer
127: f(num.f), s(num.s) {}

Member Function Documentation

◆ __get_integer_from_string()

void uint128_t::__get_integer_from_string ( const std::string & str)
inlineprivate

First and second half of 128 bit number.

Get integer from given string.

Create an integer from a given string

Parameters
strinteger string, can be hexadecimal (starting on 0x... or number)
Returns
void
69 {
70 this->f = this->s = 0;
71 if (str.size() > 1 && str[1] == 'x') { // if hexadecimal
72 for (auto i = 2; i < str.size(); ++i) {
73 *this *= 16LL;
74 if (str[i] >= '0' && str[i] <= '9') {
75 *this += (str[i] - '0');
76 } else if (str[i] >= 'A' && str[i] <= 'F') {
77 *this += (str[i] - 'A' + 10);
78 } else if (str[i] >= 'a' && str[i] <= 'f') {
79 *this += (str[i] - 'a' + 10);
80 }
81 }
82 } else { // if decimal
83 for (auto &x : str) {
84 *this *= 10LL;
85 *this += (x - '0');
86 }
87 }
88 }
T size(T... args)
Here is the call graph for this function:

◆ _lez()

uint32_t uint128_t::_lez ( )
inline

Leading zeroes in binary.

Calculates leading zeros in 128-bit integer

Returns
Integer denoting leading zeroes
139 {
140#ifndef _MSC_VER
141 if (f) {
142 return __builtin_clzll(f);
143 }
144 return 64 + __builtin_clzll(s);
145#else
146 unsigned long r = 0;
147 _BitScanForward64(&r, f);
148 if (r == 64) {
149 unsigned long l = 0;
150 _BitScanForward64(&l, s);
151 return 64 + l;
152 }
153 return r;
154#endif
155 }
double l(double x)
Another test function.
Definition composite_simpson_rule.cpp:119

◆ _trz()

uint32_t uint128_t::_trz ( )
inline

Trailing zeroes in binary.

Calculates leading zeros in 128-bit integer

Returns
Integer denoting Trailing zeroes
162 {
163#ifndef _MSC_VER
164 if (f) {
165 return __builtin_ctzll(f);
166 }
167 return 64 + __builtin_ctzll(s);
168#else
169 unsigned long r = 0;
170 _BitScanReverse64(&r, s);
171 if (r == 64) {
172 unsigned long l = 0;
173 _BitScanReverse64(&l, f);
174 return 64 + l;
175 }
176 return r;
177#endif
178 }

◆ divide()

std::pair< uint128_t, uint128_t > uint128_t::divide ( const uint128_t & p)
inline

divide function for uint128_t and other integer types.

divide this value and

Parameters
p128-bit unsigned integer
Returns
pair denoting quotient and remainder.
454 {
455 if (*this < p) { // if this is less than divisor
456 return {uint128_t(0), *this};
457 } else if (*this == p) { // if this is equal to divisor
458 return {uint128_t(1), uint128_t(0)};
459 }
460 uint128_t tmp = p, tmp2 = *this;
461 uint16_t left = tmp._lez() - _lez();
462 tmp <<= left;
463 uint128_t quotient(0);
464 uint128_t zero(0);
465 while (tmp2 >= p) {
466 uint16_t shf = tmp2._lez() - tmp._lez();
467 if (shf) {
468 tmp >>= shf;
469 quotient <<= shf;
470 left -= shf;
471 }
472 if (tmp2 < tmp) {
473 tmp >>= 1;
474 quotient <<= 1;
475 --left;
476 }
477 tmp2 -= tmp;
478 ++quotient;
479 }
480 return {quotient << left, tmp2};
481 }
class for 128-bit unsigned integer
Definition uint128_t.hpp:59
uint32_t _lez()
Leading zeroes in binary.
Definition uint128_t.hpp:139
Here is the call graph for this function:

◆ lower()

uint64_t uint128_t::lower ( ) const
inline

returns lower 64-bit integer part

Returns
returns lower 64-bit integer part
201{ return s; }

◆ operator bool()

uint128_t::operator bool ( ) const
inlineexplicit

casting operator to boolean value

Returns
true if value of this is non-zero, else false
184{ return (f || s); }

◆ operator T()

template<typename T , typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint128_t::operator T ( ) const
inlineexplicit

casting operator to any integer valu

Template Parameters
Tany integer type
Returns
integer value casted to mentioned type
193 {
194 return static_cast<T>(s);
195 }

◆ operator!()

bool uint128_t::operator! ( )
inline

operator ! for uint128_t

Returns
true if this has zero value, else false
627{ return !f && !s; }

◆ operator!=() [1/2]

template<typename T , typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
bool uint128_t::operator!= ( const T other)
inline

operator != for other types

Template Parameters
Tintegral type
Parameters
othernumber to be compared with this
Returns
true if this is not equal to other, else false
721 {
722 return *this != uint128_t(other);
723 }

◆ operator!=() [2/2]

bool uint128_t::operator!= ( const uint128_t & other)
inline

operator != for uint128_t

Parameters
othernumber to be compared with this
Returns
true if this is not equal than other, else false
619 {
620 return f != other.f || s != other.s;
621 }

◆ operator%() [1/2]

template<typename T , typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint128_t uint128_t::operator% ( const T & p)
inline

operator % for uint128_t and other integer types.

Template Parameters
Tdenoting integral type
Parameters
pa type of integer variable
Returns
unsigned 128-bit remainder.
542 {
543 return *this % uint128_t(p);
544 }

◆ operator%() [2/2]

uint128_t uint128_t::operator% ( const uint128_t & p)
inline

operator % for uint128_t

Parameters
p128-bit unsigned integer
Returns
unsigned 128-bit remainder.
532{ return divide(p).second; }
std::pair< uint128_t, uint128_t > divide(const uint128_t &p)
divide function for uint128_t and other integer types.
Definition uint128_t.hpp:454
Here is the call graph for this function:

◆ operator%=() [1/2]

template<typename T , typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint128_t & uint128_t::operator%= ( const T & p)
inline

operator %= for uint128_t

Template Parameters
Tdenoting integral type
Parameters
pa type of integer variable
Returns
this set as unsigned 128-bit remainder.
564 {
565 *this %= uint128_t(p);
566 return *this;
567 }

◆ operator%=() [2/2]

uint128_t & uint128_t::operator%= ( const uint128_t & p)
inline

operator %= for uint128_t

Parameters
p128-bit unsigned integer
Returns
this set as unsigned 128-bit remainder.
551 {
552 *this = divide(p).second;
553 return *this;
554 }
Here is the call graph for this function:

◆ operator&() [1/2]

template<typename T , typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint128_t uint128_t::operator& ( const T p)
inline

operator & for other types (bitwise operator)

Template Parameters
Tintegral type
Parameters
pnumber to be operated
Returns
value of this & p (& is bit-wise operator)
855 {
856 uint128_t tmp = *this;
857 return tmp & uint128_t(p);
858 }

◆ operator&() [2/2]

uint128_t uint128_t::operator& ( const uint128_t & p)
inline

operator & for uint128_t (bitwise operator)

Parameters
pnumber to be operated
Returns
value of this & p (& is bit-wise operator)
843 {
844 return uint128_t(this->f & p.f, this->s & p.s);
845 }

◆ operator&&() [1/2]

template<typename T , typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
bool uint128_t::operator&& ( const T b)
inline

operator && for other types

Template Parameters
Tintegral type
Parameters
othernumber to be compared with this
Returns
true if this is both values are non-zero, else false
733 {
734 return (f || s) && b;
735 }

◆ operator&&() [2/2]

bool uint128_t::operator&& ( const uint128_t & b)
inline

operator && for uint128_t

Parameters
bnumber to be compared with this
Returns
true if both of the values are not zero, else false
634 {
635 return (s || f) && (b.s || b.f);
636 }

◆ operator&=() [1/2]

template<typename T , typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint128_t & uint128_t::operator&= ( const T p)
inline

operator &= for other types (bitwise operator)

Template Parameters
Tintegral type
Parameters
pnumber to be operated
Returns
this = this & p (& is bit-wise operator)
879 {
880 *this &= uint128_t(p);
881 return *this;
882 }

◆ operator&=() [2/2]

uint128_t & uint128_t::operator&= ( const uint128_t & p)
inline

operator &= for uint128_t (bitwise operator)

Parameters
pnumber to be operated
Returns
this = this & p (& is bit-wise operator)
865 {
866 this->f &= p.f;
867 this->s &= p.s;
868 return *this;
869 }

◆ operator()()

bool uint128_t::operator() ( )
inline

operator () for uint128_t

Returns
true if this value is non-zero, else false
651{ return s || f; }

◆ operator*() [1/2]

template<typename T , typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint128_t uint128_t::operator* ( const T p)
inline

operator * for uint128_t and other integer types.

Template Parameters
Tdenoting integral type
Parameters
pa type of integer variable
Returns
multiplication of this and p, returning uint128_t integer
392 {
393 return *this * uint128_t(p);
394 }

◆ operator*() [2/2]

uint128_t uint128_t::operator* ( const uint128_t & p)
inline

operator * for uint128_t and other integer types.

Parameters
p128-bit unsigned integer
Returns
multiplication of this and p, returning uint128_t integer
401 {
402 uint64_t f_first = s >> 32, f_second = s & 0xFFFFFFFF,
403 s_first = p.s >> 32, s_second = p.s & 0xFFFFFFFF;
404 uint64_t fi = f_first * s_first, se = f_first * s_second,
405 th = s_first * f_second, fo = s_second * f_second;
406 uint64_t tmp = ((se & 0xFFFFFFFF) << 32), tmp2 = (th & 0xFFFFFFFF)
407 << 32;
408 int cc = (tmp + tmp2 < tmp);
409 tmp += tmp2;
410 cc += (tmp + fo < tmp);
411 uint64_t carry = fi + (se >> 32) + (th >> 32);
412 return uint128_t(this->f * p.s + this->s * p.f + carry + cc, tmp + fo);
413 }

◆ operator*=() [1/2]

template<typename T , typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint128_t & uint128_t::operator*= ( const T p)
inline

operator *= for uint128_t and other integer types.

Template Parameters
Tdenoting integral type
Parameters
pa type of integer variable
Returns
multiplication of this and p, returning this
423 {
424 *this *= uint128_t(p);
425 return *this;
426 }

◆ operator*=() [2/2]

uint128_t & uint128_t::operator*= ( const uint128_t & p)
inline

operator *= for uint128_t and other integer types.

Parameters
p128-bit unsigned integer
Returns
multiplication of this and p, returning this
433 {
434 uint64_t f_first = s >> 32, f_second = s & 0xFFFFFFFF,
435 s_first = p.s >> 32, s_second = p.s & 0xFFFFFFFF;
436 uint64_t fi = f_first * s_first, se = f_first * s_second,
437 th = s_first * f_second, fo = s_second * f_second;
438 uint64_t tmp = (se << 32), tmp2 = (th << 32);
439 int cc = (tmp + tmp2 < tmp);
440 tmp += tmp2;
441 cc += (tmp + fo < tmp);
442 uint64_t carry = fi + (se >> 32) + (th >> 32);
443 f = this->f * p.s + this->s * p.f + carry + cc;
444 s = tmp + fo;
445 return *this;
446 }

◆ operator+() [1/2]

template<typename T , typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint128_t uint128_t::operator+ ( const T p)
inline

operator + for uint128_t and other integer types.

Template Parameters
Tdenoting integral type
Parameters
pa type of integer variable
Returns
addition of this and p, returning uint128_t integer
252 {
253 return uint128_t(f + (p + s < s), p + s);
254 }

◆ operator+() [2/2]

uint128_t uint128_t::operator+ ( const uint128_t & p)
inline

operator + for uint128_t and other integer types.

Parameters
p128-bit unsigned integer
Returns
addition of this and p, returning uint128_t integer
261 {
262 return uint128_t(f + (p.s + s < s) + p.f, p.s + s);
263 }

◆ operator++() [1/2]

uint128_t & uint128_t::operator++ ( )
inline

pre-increment operator

Returns
incremented value of this.
296 {
297 *this += 1;
298 return *this;
299 }

◆ operator++() [2/2]

uint128_t uint128_t::operator++ ( int )
inline

post-increment operator

Returns
incremented value of this.
305 {
306 ++*this;
307 return *this;
308 }

◆ operator+=() [1/2]

template<typename T , typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint128_t & uint128_t::operator+= ( const T p)
inline

operator += for uint128_t and other integer types.

Template Parameters
Tdenoting integral type
Parameters
pa type of integer variable
Returns
addition of this and p, returning this
273 {
274 bool app = p + s < s;
275 this->f += app;
276 this->s += p;
277 return *this;
278 }

◆ operator+=() [2/2]

uint128_t & uint128_t::operator+= ( const uint128_t & p)
inline

operator += for uint128_t

Parameters
p128-bit unsigned integer
Returns
addition of this and p, returning this
285 {
286 bool app = p.s + s < s;
287 f = f + app + p.f;
288 s = p.s + s;
289 return *this;
290 }

◆ operator-() [1/3]

uint128_t uint128_t::operator- ( )
inline

operator - using twos complement

Returns
2's complement of this.
337{ return ~*this + uint128_t(1); }

◆ operator-() [2/3]

template<typename T , typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint128_t uint128_t::operator- ( const T & p)
inline

operator - for uint128_t and other integer types.

Template Parameters
Tdenoting integral type
Parameters
pa type of integer variable
Returns
subtraction of this and p, returning uint128_t integer
318 {
319 bool app = p > s;
320 return uint128_t(f - app, s - p);
321 }

◆ operator-() [3/3]

uint128_t uint128_t::operator- ( const uint128_t & p)
inline

operator - for uint128_t

Parameters
pa type of integer variable
Returns
subtraction of this and p, returning uint128_t integer
328 {
329 bool app = p.s > s;
330 return uint128_t(f - p.f - app, s - p.s);
331 }

◆ operator--() [1/2]

uint128_t & uint128_t::operator-- ( )
inline

operator – (pre-decrement)

Returns
decremented value of this
343 {
344 *this -= 1;
345 return *this;
346 }

◆ operator--() [2/2]

uint128_t uint128_t::operator-- ( int p)
inline

operator – (post-decrement)

Returns
decremented value of this
352 {
353 --*this;
354 return *this;
355 }

◆ operator-=() [1/2]

template<typename T , typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint128_t & uint128_t::operator-= ( const T & p)
inline

operator -= for uint128_t and other integer types.

Template Parameters
Tdenoting integral type
Parameters
pa type of integer variable
Returns
subtraction of this and p, returning this
365 {
366 bool app = p > s;
367 f -= app;
368 s -= p;
369 return *this;
370 }

◆ operator-=() [2/2]

uint128_t & uint128_t::operator-= ( const uint128_t & p)
inline

operator -= for uint128_t

Parameters
p128-bit unsigned integer
Returns
subtraction of this and p, returning this
377 {
378 bool app = p.s > s;
379 f = f - p.f - app;
380 s = s - p.s;
381 return *this;
382 }

◆ operator/() [1/2]

template<typename T , typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint128_t uint128_t::operator/ ( const T p)
inline

operator / for uint128_t and other integer types.

Template Parameters
Tdenoting integral type
Parameters
pa type of integer variable
Returns
unsigned 128-bit quotient.
498 {
499 uint128_t tmp = *this;
500 tmp /= uint128_t(0, p);
501 return tmp;
502 }

◆ operator/() [2/2]

uint128_t uint128_t::operator/ ( const uint128_t & p)
inline

operator / for uint128_t and other integer types.

Parameters
p128-bit unsigned integer
Returns
unsigned 128-bit quotient.
488{ return divide(p).first; }
Here is the call graph for this function:

◆ operator/=() [1/2]

template<typename T , typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint128_t & uint128_t::operator/= ( const T p)
inline

operator /= for uint128_t and other integer types.

Template Parameters
Tdenoting integral type
Parameters
pa type of integer variable
Returns
this set as unsigned 128-bit quotient.
522 {
523 *this /= uint128_t(0, p);
524 return *this;
525 }

◆ operator/=() [2/2]

uint128_t & uint128_t::operator/= ( const uint128_t & p)
inline

operator /= for uint128_t

Parameters
p128-bit unsigned integer
Returns
this set as unsigned 128-bit quotient.
509 {
510 *this = divide(p).first;
511 return *this;
512 }
Here is the call graph for this function:

◆ operator<() [1/2]

template<typename T , typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
bool uint128_t::operator< ( const T other)
inline

operator < for other types

Template Parameters
Tintegral type
Parameters
othernumber to be compared with this
Returns
true if this is less than other, else false
661 {
662 return *this < uint128_t(other);
663 }

◆ operator<() [2/2]

bool uint128_t::operator< ( const uint128_t & other)
inline

operator < for uint128_t

Parameters
othernumber to be compared with this
Returns
true if this is less than other, else false
574 {
575 return f < other.f || (f == other.f && s < other.s);
576 }

◆ operator<<()

template<typename T , typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint128_t uint128_t::operator<< ( const T p)
inline

operator << for uint128_t

Template Parameters
Tintegral type
Parameters
pnumber denoting number of shifts
Returns
value of this shifted by p to left
764 {
765 if (!p) {
766 return uint128_t(f, s);
767 } else if (p >= 64 && p <= 128) {
768 return uint128_t((this->s << (p - 64)), 0);
769 } else if (p < 64 && p > 0) {
770 return uint128_t((this->f << p) + ((this->s >> (64 - p))),
771 this->s << p);
772 }
773 return uint128_t(0);
774 }

◆ operator<<=()

template<typename T , typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint128_t & uint128_t::operator<<= ( const T p)
inline

operator <<= for uint128_t

Template Parameters
Tintegral type
Parameters
pnumber denoting number of shifts
Returns
this shifted by p to left
784 {
785 if (p) {
786 if (p >= 64 && p <= 128) {
787 this->f = (this->s << (p - 64));
788 this->s = 0;
789 } else {
790 f = ((this->f << p) + (this->s >> (64 - p)));
791 s = (this->s << p);
792 }
793 }
794 return *this;
795 }

◆ operator<=() [1/2]

template<typename T , typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
bool uint128_t::operator<= ( const T other)
inline

operator <= for other types

Template Parameters
Tintegral type
Parameters
othernumber to be compared with this
Returns
true if this is less than or equal to other, else false
673 {
674 return *this <= uint128_t(other);
675 }

◆ operator<=() [2/2]

bool uint128_t::operator<= ( const uint128_t & other)
inline

operator <= for uint128_t

Parameters
othernumber to be compared with this
Returns
true if this is less than or equal to other, else false
583 {
584 return f < other.f || (f == other.f && s <= other.s);
585 }

◆ operator=() [1/3]

uint128_t & uint128_t::operator= ( const std::string & p)
inline

operator = for type string

Parameters
pa string to assign it's value to equivalent integer
Returns
this pointer with it's value equal to p
227 {
229 return *this;
230 }
Here is the call graph for this function:

◆ operator=() [2/3]

template<typename T , typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint128_t & uint128_t::operator= ( const T & p)
inline

operator = for other types

Template Parameters
Tdenoting any integer type
Parameters
pan integer to assign it's value
Returns
this pointer with it's value equal to p
217 {
218 this->s = p;
219 return *this;
220 }

◆ operator=() [3/3]

uint128_t & uint128_t::operator= ( const uint128_t & p)
inlinedefault

operator = for uint128_t

Parameters
pan 128-bit integer to assign it's value
Returns
this pointer with it's value equal to p

◆ operator==() [1/2]

template<typename T , typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
bool uint128_t::operator== ( const T other)
inline

operator == for other types

Template Parameters
Tintegral type
Parameters
othernumber to be compared with this
Returns
true if this is equal to other, else false
709 {
710 return *this == uint128_t(other);
711 }

◆ operator==() [2/2]

bool uint128_t::operator== ( const uint128_t & other)
inline

operator == for uint128_t

Parameters
othernumber to be compared with this
Returns
true if this is equal than other, else false
610 {
611 return f == other.f && s == other.s;
612 }

◆ operator>() [1/2]

template<typename T , typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
bool uint128_t::operator> ( const T other)
inline

operator > for other types

Template Parameters
Tintegral type
Parameters
othernumber to be compared with this
Returns
true if this is greater than other, else false
685 {
686 return *this > uint128_t(other);
687 }

◆ operator>() [2/2]

bool uint128_t::operator> ( const uint128_t & other)
inline

operator > for uint128_t

Parameters
othernumber to be compared with this
Returns
true if this is greater than other, else false
592 {
593 return f > other.f || (f == other.f && s > other.s);
594 }

◆ operator>=() [1/2]

template<typename T , typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
bool uint128_t::operator>= ( const T other)
inline

operator >= for other types

Template Parameters
Tintegral type
Parameters
othernumber to be compared with this
Returns
true if this is greater than or equal other, else false
697 {
698 return *this >= uint128_t(other);
699 }

◆ operator>=() [2/2]

bool uint128_t::operator>= ( const uint128_t & other)
inline

operator >= for uint128_t

Parameters
othernumber to be compared with this
Returns
true if this is greater than or equal than other, else false
601 {
602 return (f > other.f) || (f == other.f && s >= other.s);
603 }

◆ operator>>()

template<typename T , typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint128_t uint128_t::operator>> ( const T p)
inline

operator >> for uint128_t

Template Parameters
Tintegral type
Parameters
pnumber denoting number of shifts
Returns
value of this shifted by p to right
805 {
806 if (!p) {
807 return uint128_t(this->f, this->s);
808 } else if (p >= 64 && p <= 128) {
809 return uint128_t(0, (this->f >> (p - 64)));
810 } else if (p < 64 && p > 0) {
811 return uint128_t((this->f >> p),
812 (this->s >> p) + (this->f << (64 - p)));
813 }
814 return uint128_t(0);
815 }

◆ operator>>=()

template<typename T , typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint128_t & uint128_t::operator>>= ( const T p)
inline

operator >>= for uint128_t

Template Parameters
Tintegral type
Parameters
pnumber denoting number of shifts
Returns
this shifted by p to right
825 {
826 if (p) {
827 if (p >= 64) {
828 f = 0;
829 s = (this->f >> (p - 64));
830 } else {
831 s = (this->s >> p) + (this->f << (64 - p));
832 f = (this->f >> p);
833 }
834 }
835 return *this;
836 }

◆ operator^() [1/2]

template<typename T , typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint128_t uint128_t::operator^ ( const T p)
inline

operator ^ for other types (bitwise operator)

Template Parameters
Tintegral type
Parameters
pnumber to be operated
Returns
value of this ^ p (^ is bit-wise XOR operator)
937 {
938 return uint128_t(this->f, this->s ^ p);
939 }

◆ operator^() [2/2]

uint128_t uint128_t::operator^ ( const uint128_t & p)
inline

operator ^ for uint128_t (bitwise operator)

Parameters
pnumber to be operated
Returns
value of this ^ p (^ is bit-wise XOR operator)
946 {
947 return uint128_t(this->f ^ p.f, this->s ^ p.s);
948 }

◆ operator^=() [1/2]

template<typename T , typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint128_t & uint128_t::operator^= ( const T & p)
inline

operator ^= for other types (bitwise operator)

Template Parameters
Tintegral type
Parameters
pnumber to be operated
Returns
this = this ^ p (^ is bit-wise XOR operator)
969 {
970 s ^= p;
971 return *this;
972 }

◆ operator^=() [2/2]

uint128_t & uint128_t::operator^= ( const uint128_t & p)
inline

operator ^= for uint128_t (bitwise operator)

Parameters
pnumber to be operated
Returns
this = this ^ p (^ is bit-wise XOR operator)
955 {
956 f ^= p.f;
957 s ^= p.s;
958 return *this;
959 }

◆ operator|() [1/2]

template<typename T , typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint128_t uint128_t::operator| ( const T p)
inline

operator | for other types (bitwise operator)

Template Parameters
Tintegral type
Parameters
pnumber to be operated
Returns
value of this | p (| is bit-wise operator)
892 {
893 return uint128_t(p | s);
894 }

◆ operator|() [2/2]

uint128_t uint128_t::operator| ( const uint128_t & p)
inline

operator | for uint128_t (bitwise operator)

Parameters
pnumber to be operated
Returns
value of this | p (| is bit-wise OR operator)
901 {
902 return uint128_t(this->f | p.f, this->s | p.s);
903 }

◆ operator|=() [1/2]

template<typename T , typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
uint128_t & uint128_t::operator|= ( const T p)
inline

operator |= for other types (bitwise operator)

Template Parameters
Tintegral type
Parameters
pnumber to be operated
Returns
this = this | p (| is bit-wise OR operator)
924 {
925 s |= p.s;
926 return *this;
927 }

◆ operator|=() [2/2]

uint128_t & uint128_t::operator|= ( const uint128_t & p)
inline

operator |= for uint128_t (bitwise operator)

Parameters
pnumber to be operated
Returns
this = this | p (| is bit-wise OR operator)
910 {
911 f |= p.f;
912 s |= p.s;
913 return *this;
914 }

◆ operator||() [1/2]

template<typename T , typename = typename std::enable_if< std::is_integral<T>::value, T>::type>
bool uint128_t::operator|| ( const T b)
inline

operator || for other types

Template Parameters
Tintegral type
Parameters
othernumber to be compared with this
Returns
true if this is either one of the values are non-zero, else false
746 {
747 return (f || s) || b;
748 }

◆ operator||() [2/2]

bool uint128_t::operator|| ( const uint128_t & b)
inline

operator || for uint128_t

Parameters
bnumber to be compared with this
Returns
true if one of the values are not zero, else false
643 {
644 return (s || f) || (b.s || b.f);
645 }

◆ operator~()

uint128_t uint128_t::operator~ ( )
inline

operator ~ for uint128_t

Returns
1's complement of this number
754{ return uint128_t(~this->f, ~this->s); }

◆ upper()

uint64_t uint128_t::upper ( ) const
inline

returns upper 64-bit integer part

Returns
returns upper 64-bit integer part
207{ return f; }

Friends And Related Symbol Documentation

◆ operator<<

std::ostream & operator<< ( std::ostream & op,
const uint128_t & p )
friend

operator << for printing uint128_t integer

Prints the uint128_t integer in decimal form

Note
Note that this operator is costly since it uses strings to print the value
Parameters
opostream object
p128-bit integer
Returns
op, ostream object.
983 {
984 if (!p.f) {
985 op << p.s;
986 } else {
987 std::string out = "0", p_2 = "1";
988 for (int i = 0; i < 64; ++i) {
989 if (p.s & (1LL << i)) {
990 out = add(out, p_2);
991 }
992 p_2 = add(p_2, p_2);
993 }
994 for (int i = 0; i < 64; ++i) {
995 if (p.f & (1LL << i)) {
996 out = add(out, p_2);
997 }
998 p_2 = add(p_2, p_2);
999 }
1000 op << out;
1001 }
1002 return op;
1003 }
std::string add(const std::string &first, const std::string &second)
Adding two string.
Definition uint128_t.hpp:37

Member Data Documentation

◆ f

uint64_t uint128_t::f {}
private
60{}, s{}; /// First and second half of 128 bit number

◆ s

uint64_t uint128_t::s {}
private
60{}, s{}; /// First and second half of 128 bit number

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