TheAlgorithms/C++ 1.0.0
All the 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::ostream & operator<< (std::ostream &op, const uint128_t &p)
 operator << for printing uint128_t integer
 

Detailed Description

class for 128-bit unsigned integer

Definition at line 60 of file uint128_t.hpp.

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

Definition at line 101 of file uint128_t.hpp.

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

Definition at line 107 of file uint128_t.hpp.

107 {
109 }
void __get_integer_from_string(const std::string &str)
First and second half of 128 bit number.
Definition uint128_t.hpp:70

◆ 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

Definition at line 116 of file uint128_t.hpp.

116: 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

Definition at line 128 of file uint128_t.hpp.

128: 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

Definition at line 70 of file uint128_t.hpp.

70 {
71 this->f = this->s = 0;
72 if (str.size() > 1 && str[1] == 'x') { // if hexadecimal
73 for (auto i = 2; i < str.size(); ++i) {
74 *this *= 16LL;
75 if (str[i] >= '0' && str[i] <= '9') {
76 *this += (str[i] - '0');
77 } else if (str[i] >= 'A' && str[i] <= 'F') {
78 *this += (str[i] - 'A' + 10);
79 } else if (str[i] >= 'a' && str[i] <= 'f') {
80 *this += (str[i] - 'a' + 10);
81 }
82 }
83 } else { // if decimal
84 for (auto &x : str) {
85 *this *= 10LL;
86 *this += (x - '0');
87 }
88 }
89 }

◆ _lez()

uint32_t uint128_t::_lez ( )
inline

Leading zeroes in binary.

Calculates leading zeros in 128-bit integer

Returns
Integer denoting leading zeroes

Definition at line 140 of file uint128_t.hpp.

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

◆ _trz()

uint32_t uint128_t::_trz ( )
inline

Trailing zeroes in binary.

Calculates leading zeros in 128-bit integer

Returns
Integer denoting Trailing zeroes

Definition at line 163 of file uint128_t.hpp.

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

◆ 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.

Definition at line 455 of file uint128_t.hpp.

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

◆ lower()

uint64_t uint128_t::lower ( ) const
inline

returns lower 64-bit integer part

Returns
returns lower 64-bit integer part

Definition at line 202 of file uint128_t.hpp.

202{ 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

Definition at line 185 of file uint128_t.hpp.

185{ 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

Definition at line 194 of file uint128_t.hpp.

194 {
195 return static_cast<T>(s);
196 }

◆ operator!()

bool uint128_t::operator! ( )
inline

operator ! for uint128_t

Returns
true if this has zero value, else false

Definition at line 628 of file uint128_t.hpp.

628{ 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

Definition at line 722 of file uint128_t.hpp.

722 {
723 return *this != uint128_t(other);
724 }

◆ 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

Definition at line 620 of file uint128_t.hpp.

620 {
621 return f != other.f || s != other.s;
622 }

◆ 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.

Definition at line 543 of file uint128_t.hpp.

543 {
544 return *this % uint128_t(p);
545 }

◆ 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.

Definition at line 533 of file uint128_t.hpp.

533{ return divide(p).second; }
std::pair< uint128_t, uint128_t > divide(const uint128_t &p)
divide function for uint128_t and other integer types.

◆ 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.

Definition at line 565 of file uint128_t.hpp.

565 {
566 *this %= uint128_t(p);
567 return *this;
568 }

◆ 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.

Definition at line 552 of file uint128_t.hpp.

552 {
553 *this = divide(p).second;
554 return *this;
555 }

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

Definition at line 856 of file uint128_t.hpp.

856 {
857 uint128_t tmp = *this;
858 return tmp & uint128_t(p);
859 }

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

Definition at line 844 of file uint128_t.hpp.

844 {
845 return uint128_t(this->f & p.f, this->s & p.s);
846 }

◆ 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

Definition at line 734 of file uint128_t.hpp.

734 {
735 return (f || s) && b;
736 }

◆ 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

Definition at line 635 of file uint128_t.hpp.

635 {
636 return (s || f) && (b.s || b.f);
637 }

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

Definition at line 880 of file uint128_t.hpp.

880 {
881 *this &= uint128_t(p);
882 return *this;
883 }

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

Definition at line 866 of file uint128_t.hpp.

866 {
867 this->f &= p.f;
868 this->s &= p.s;
869 return *this;
870 }

◆ operator()()

bool uint128_t::operator() ( )
inline

operator () for uint128_t

Returns
true if this value is non-zero, else false

Definition at line 652 of file uint128_t.hpp.

652{ 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

Definition at line 393 of file uint128_t.hpp.

393 {
394 return *this * uint128_t(p);
395 }

◆ 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

Definition at line 402 of file uint128_t.hpp.

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

◆ 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

Definition at line 424 of file uint128_t.hpp.

424 {
425 *this *= uint128_t(p);
426 return *this;
427 }

◆ 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

Definition at line 434 of file uint128_t.hpp.

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

◆ 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

Definition at line 253 of file uint128_t.hpp.

253 {
254 return uint128_t(f + (p + s < s), p + s);
255 }

◆ 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

Definition at line 262 of file uint128_t.hpp.

262 {
263 return uint128_t(f + (p.s + s < s) + p.f, p.s + s);
264 }

◆ operator++() [1/2]

uint128_t & uint128_t::operator++ ( )
inline

pre-increment operator

Returns
incremented value of this.

Definition at line 297 of file uint128_t.hpp.

297 {
298 *this += 1;
299 return *this;
300 }

◆ operator++() [2/2]

uint128_t uint128_t::operator++ ( int )
inline

post-increment operator

Returns
incremented value of this.

Definition at line 306 of file uint128_t.hpp.

306 {
307 ++*this;
308 return *this;
309 }

◆ 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

Definition at line 274 of file uint128_t.hpp.

274 {
275 bool app = p + s < s;
276 this->f += app;
277 this->s += p;
278 return *this;
279 }

◆ 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

Definition at line 286 of file uint128_t.hpp.

286 {
287 bool app = p.s + s < s;
288 f = f + app + p.f;
289 s = p.s + s;
290 return *this;
291 }

◆ operator-() [1/3]

uint128_t uint128_t::operator- ( )
inline

operator - using twos complement

Returns
2's complement of this.

Definition at line 338 of file uint128_t.hpp.

338{ 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

Definition at line 319 of file uint128_t.hpp.

319 {
320 bool app = p > s;
321 return uint128_t(f - app, s - p);
322 }

◆ 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

Definition at line 329 of file uint128_t.hpp.

329 {
330 bool app = p.s > s;
331 return uint128_t(f - p.f - app, s - p.s);
332 }

◆ operator--() [1/2]

uint128_t & uint128_t::operator-- ( )
inline

operator – (pre-decrement)

Returns
decremented value of this

Definition at line 344 of file uint128_t.hpp.

344 {
345 *this -= 1;
346 return *this;
347 }

◆ operator--() [2/2]

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

operator – (post-decrement)

Returns
decremented value of this

Definition at line 353 of file uint128_t.hpp.

353 {
354 --*this;
355 return *this;
356 }

◆ 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

Definition at line 366 of file uint128_t.hpp.

366 {
367 bool app = p > s;
368 f -= app;
369 s -= p;
370 return *this;
371 }

◆ 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

Definition at line 378 of file uint128_t.hpp.

378 {
379 bool app = p.s > s;
380 f = f - p.f - app;
381 s = s - p.s;
382 return *this;
383 }

◆ 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.

Definition at line 499 of file uint128_t.hpp.

499 {
500 uint128_t tmp = *this;
501 tmp /= uint128_t(0, p);
502 return tmp;
503 }

◆ 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.

Definition at line 489 of file uint128_t.hpp.

489{ return divide(p).first; }

◆ 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.

Definition at line 523 of file uint128_t.hpp.

523 {
524 *this /= uint128_t(0, p);
525 return *this;
526 }

◆ 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.

Definition at line 510 of file uint128_t.hpp.

510 {
511 *this = divide(p).first;
512 return *this;
513 }

◆ 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

Definition at line 662 of file uint128_t.hpp.

662 {
663 return *this < uint128_t(other);
664 }

◆ 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

Definition at line 575 of file uint128_t.hpp.

575 {
576 return f < other.f || (f == other.f && s < other.s);
577 }

◆ 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

Definition at line 765 of file uint128_t.hpp.

765 {
766 if (!p) {
767 return uint128_t(f, s);
768 } else if (p >= 64 && p <= 128) {
769 return uint128_t((this->s << (p - 64)), 0);
770 } else if (p < 64 && p > 0) {
771 return uint128_t((this->f << p) + ((this->s >> (64 - p))),
772 this->s << p);
773 }
774 return uint128_t(0);
775 }

◆ 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

Definition at line 785 of file uint128_t.hpp.

785 {
786 if (p) {
787 if (p >= 64 && p <= 128) {
788 this->f = (this->s << (p - 64));
789 this->s = 0;
790 } else {
791 f = ((this->f << p) + (this->s >> (64 - p)));
792 s = (this->s << p);
793 }
794 }
795 return *this;
796 }

◆ 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

Definition at line 674 of file uint128_t.hpp.

674 {
675 return *this <= uint128_t(other);
676 }

◆ 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

Definition at line 584 of file uint128_t.hpp.

584 {
585 return f < other.f || (f == other.f && s <= other.s);
586 }

◆ 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

Definition at line 228 of file uint128_t.hpp.

228 {
230 return *this;
231 }

◆ 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

Definition at line 218 of file uint128_t.hpp.

218 {
219 this->s = p;
220 return *this;
221 }

◆ 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

Definition at line 710 of file uint128_t.hpp.

710 {
711 return *this == uint128_t(other);
712 }

◆ 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

Definition at line 611 of file uint128_t.hpp.

611 {
612 return f == other.f && s == other.s;
613 }

◆ 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

Definition at line 686 of file uint128_t.hpp.

686 {
687 return *this > uint128_t(other);
688 }

◆ 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

Definition at line 593 of file uint128_t.hpp.

593 {
594 return f > other.f || (f == other.f && s > other.s);
595 }

◆ 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

Definition at line 698 of file uint128_t.hpp.

698 {
699 return *this >= uint128_t(other);
700 }

◆ 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

Definition at line 602 of file uint128_t.hpp.

602 {
603 return (f > other.f) || (f == other.f && s >= other.s);
604 }

◆ 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

Definition at line 806 of file uint128_t.hpp.

806 {
807 if (!p) {
808 return uint128_t(this->f, this->s);
809 } else if (p >= 64 && p <= 128) {
810 return uint128_t(0, (this->f >> (p - 64)));
811 } else if (p < 64 && p > 0) {
812 return uint128_t((this->f >> p),
813 (this->s >> p) + (this->f << (64 - p)));
814 }
815 return uint128_t(0);
816 }

◆ 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

Definition at line 826 of file uint128_t.hpp.

826 {
827 if (p) {
828 if (p >= 64) {
829 f = 0;
830 s = (this->f >> (p - 64));
831 } else {
832 s = (this->s >> p) + (this->f << (64 - p));
833 f = (this->f >> p);
834 }
835 }
836 return *this;
837 }

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

Definition at line 938 of file uint128_t.hpp.

938 {
939 return uint128_t(this->f, this->s ^ p);
940 }

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

Definition at line 947 of file uint128_t.hpp.

947 {
948 return uint128_t(this->f ^ p.f, this->s ^ p.s);
949 }

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

Definition at line 970 of file uint128_t.hpp.

970 {
971 s ^= p;
972 return *this;
973 }

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

Definition at line 956 of file uint128_t.hpp.

956 {
957 f ^= p.f;
958 s ^= p.s;
959 return *this;
960 }

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

Definition at line 893 of file uint128_t.hpp.

893 {
894 return uint128_t(p | s);
895 }

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

Definition at line 902 of file uint128_t.hpp.

902 {
903 return uint128_t(this->f | p.f, this->s | p.s);
904 }

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

Definition at line 925 of file uint128_t.hpp.

925 {
926 s |= p.s;
927 return *this;
928 }

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

Definition at line 911 of file uint128_t.hpp.

911 {
912 f |= p.f;
913 s |= p.s;
914 return *this;
915 }

◆ 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

Definition at line 747 of file uint128_t.hpp.

747 {
748 return (f || s) || b;
749 }

◆ 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

Definition at line 644 of file uint128_t.hpp.

644 {
645 return (s || f) || (b.s || b.f);
646 }

◆ operator~()

uint128_t uint128_t::operator~ ( )
inline

operator ~ for uint128_t

Returns
1's complement of this number

Definition at line 755 of file uint128_t.hpp.

755{ 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

Definition at line 208 of file uint128_t.hpp.

208{ 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.

Definition at line 984 of file uint128_t.hpp.

984 {
985 if (!p.f) {
986 op << p.s;
987 } else {
988 std::string out = "0", p_2 = "1";
989 for (int i = 0; i < 64; ++i) {
990 if (p.s & (1LL << i)) {
991 out = add(out, p_2);
992 }
993 p_2 = add(p_2, p_2);
994 }
995 for (int i = 0; i < 64; ++i) {
996 if (p.f & (1LL << i)) {
997 out = add(out, p_2);
998 }
999 p_2 = add(p_2, p_2);
1000 }
1001 op << out;
1002 }
1003 return op;
1004 }
std::string add(const std::string &first, const std::string &second)
Adding two string.
Definition uint128_t.hpp:38

Member Data Documentation

◆ f

uint64_t uint128_t::f {}
private

Definition at line 61 of file uint128_t.hpp.

61{}, s{};

◆ s

uint64_t uint128_t::s {}
private

Definition at line 61 of file uint128_t.hpp.

61{}, s{};

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