19#ifndef CIPHERS_UINT128_T_HPP_
20#define CIPHERS_UINT128_T_HPP_
39 int16_t sum = 0, carry = 0;
40 for (int32_t i =
static_cast<int32_t
>(first.
size()) - 1,
41 j =
static_cast<int32_t
>(second.
size()) - 1;
42 i >= 0 || j >= 0; --i, --j) {
43 sum = ((i >= 0 ? first[i] -
'0' : 0) + (j >= 0 ? second[j] -
'0' : 0) +
70 this->f = this->s = 0;
71 if (str.
size() > 1 && str[1] ==
'x') {
72 for (
auto i = 2; i < str.
size(); ++i) {
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);
115 uint128_t(
const uint64_t high,
const uint64_t low) : f(high), s(low) {}
142 return __builtin_clzll(f);
144 return 64 + __builtin_clzll(s);
147 _BitScanForward64(&r, f);
150 _BitScanForward64(&l, s);
165 return __builtin_ctzll(f);
167 return 64 + __builtin_ctzll(s);
170 _BitScanReverse64(&r, s);
173 _BitScanReverse64(&l, f);
184 inline explicit operator bool()
const {
return (f || s); }
193 inline explicit operator T()
const {
194 return static_cast<T
>(s);
201 inline uint64_t
lower()
const {
return s; }
207 inline uint64_t
upper()
const {
return f; }
253 return uint128_t(f + (p + s < s), p + s);
262 return uint128_t(f + (p.s + s < s) + p.f, p.s + s);
274 bool app = p + s < s;
286 bool app = p.s + s < s;
330 return uint128_t(f - p.f - app, s - p.s);
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)
408 int cc = (tmp + tmp2 < tmp);
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);
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);
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;
457 }
else if (*
this == p) {
461 uint16_t left = tmp.
_lez() -
_lez();
466 uint16_t shf = tmp2._lez() - tmp.
_lez();
480 return {quotient << left, tmp2};
575 return f < other.f || (f == other.f && s < other.s);
584 return f < other.f || (f == other.f && s <= other.s);
593 return f > other.f || (f == other.f && s > other.s);
602 return (f > other.f) || (f == other.f && s >= other.s);
611 return f == other.f && s == other.s;
620 return f != other.f || s != other.s;
635 return (s || f) && (b.s || b.f);
644 return (s || f) || (b.s || b.f);
734 return (f || s) && b;
747 return (f || s) || b;
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))),
786 if (p >= 64 && p <= 128) {
787 this->f = (this->s << (p - 64));
790 f = ((this->f << p) + (this->s >> (64 - p)));
808 }
else if (p >= 64 && p <= 128) {
809 return uint128_t(0, (this->f >> (p - 64)));
810 }
else if (p < 64 && p > 0) {
812 (this->s >> p) + (this->f << (64 - p)));
829 s = (this->f >> (p - 64));
831 s = (this->s >> p) + (this->f << (64 - p));
844 return uint128_t(this->f & p.f, this->s & p.s);
902 return uint128_t(this->f | p.f, this->s | p.s);
947 return uint128_t(this->f ^ p.f, this->s ^ p.s);
988 for (
int i = 0; i < 64; ++i) {
989 if (p.s & (1LL << i)) {
994 for (
int i = 0; i < 64; ++i) {
995 if (p.f & (1LL << i)) {
1059inline bool operator&&(
const T p,
const uint128_t &q) {
1065inline bool operator||(
const T p,
const uint128_t &q) {
1072inline bool operator==(
const T p,
const uint128_t &q) {
1084inline bool operator<(
const T p,
const uint128_t &q) {
class for 128-bit unsigned integer
Definition uint128_t.hpp:59
uint128_t & operator%=(const T &p)
operator %= for uint128_t
Definition uint128_t.hpp:564
uint128_t operator-()
operator - using twos complement
Definition uint128_t.hpp:337
uint128_t & operator-=(const T &p)
operator -= for uint128_t and other integer types.
Definition uint128_t.hpp:365
bool operator&&(const T b)
operator && for other types
Definition uint128_t.hpp:733
uint128_t & operator>>=(const T p)
operator >>= for uint128_t
Definition uint128_t.hpp:825
uint128_t(const std::string &str)
Parameterized constructor.
Definition uint128_t.hpp:106
uint128_t operator+(const uint128_t &p)
operator + for uint128_t and other integer types.
Definition uint128_t.hpp:261
uint128_t operator<<(const T p)
operator << for uint128_t
Definition uint128_t.hpp:764
bool operator<=(const uint128_t &other)
operator <= for uint128_t
Definition uint128_t.hpp:583
uint128_t & operator--()
operator – (pre-decrement)
Definition uint128_t.hpp:343
uint64_t upper() const
returns upper 64-bit integer part
Definition uint128_t.hpp:207
uint128_t & operator&=(const T p)
operator &= for other types (bitwise operator)
Definition uint128_t.hpp:879
uint128_t & operator%=(const uint128_t &p)
operator %= for uint128_t
Definition uint128_t.hpp:551
bool operator>(const uint128_t &other)
operator > for uint128_t
Definition uint128_t.hpp:592
uint128_t operator--(int p)
operator – (post-decrement)
Definition uint128_t.hpp:352
uint128_t operator|(const uint128_t &p)
operator | for uint128_t (bitwise operator)
Definition uint128_t.hpp:901
uint128_t & operator/=(const uint128_t &p)
operator /= for uint128_t
Definition uint128_t.hpp:509
uint128_t & operator*=(const T p)
operator *= for uint128_t and other integer types.
Definition uint128_t.hpp:423
uint128_t operator/(const uint128_t &p)
operator / for uint128_t and other integer types.
Definition uint128_t.hpp:488
bool operator||(const uint128_t &b)
operator || for uint128_t
Definition uint128_t.hpp:643
bool operator>=(const T other)
operator >= for other types
Definition uint128_t.hpp:697
uint128_t & operator=(uint128_t &&p)=default
Move assignment operator.
uint128_t operator|(const T p)
operator | for other types (bitwise operator)
Definition uint128_t.hpp:892
~uint128_t()=default
Destructor for uint128_t.
uint128_t operator~()
operator ~ for uint128_t
Definition uint128_t.hpp:754
uint128_t operator*(const uint128_t &p)
operator * for uint128_t and other integer types.
Definition uint128_t.hpp:401
uint128_t & operator^=(const T &p)
operator ^= for other types (bitwise operator)
Definition uint128_t.hpp:969
bool operator<=(const T other)
operator <= for other types
Definition uint128_t.hpp:673
uint128_t operator*(const T p)
operator * for uint128_t and other integer types.
Definition uint128_t.hpp:392
uint128_t operator+(const T p)
operator + for uint128_t and other integer types.
Definition uint128_t.hpp:252
uint128_t & operator+=(const T p)
operator += for uint128_t and other integer types.
Definition uint128_t.hpp:273
bool operator<(const T other)
operator < for other types
Definition uint128_t.hpp:661
friend std::ostream & operator<<(std::ostream &op, const uint128_t &p)
operator << for printing uint128_t integer
Definition uint128_t.hpp:983
uint128_t(const uint128_t &num)=default
Copy constructor.
uint128_t & operator|=(const T p)
operator |= for other types (bitwise operator)
Definition uint128_t.hpp:924
uint128_t operator-(const T &p)
operator - for uint128_t and other integer types.
Definition uint128_t.hpp:318
uint128_t operator>>(const T p)
operator >> for uint128_t
Definition uint128_t.hpp:805
bool operator!=(const T other)
operator != for other types
Definition uint128_t.hpp:721
bool operator==(const T other)
operator == for other types
Definition uint128_t.hpp:709
bool operator==(const uint128_t &other)
operator == for uint128_t
Definition uint128_t.hpp:610
uint32_t _trz()
Trailing zeroes in binary.
Definition uint128_t.hpp:162
uint128_t(uint128_t &&num) noexcept
Move constructor.
Definition uint128_t.hpp:127
bool operator||(const T b)
operator || for other types
Definition uint128_t.hpp:746
uint128_t operator-(const uint128_t &p)
operator - for uint128_t
Definition uint128_t.hpp:328
bool operator>(const T other)
operator > for other types
Definition uint128_t.hpp:685
void __get_integer_from_string(const std::string &str)
First and second half of 128 bit number.
Definition uint128_t.hpp:69
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
uint128_t operator^(const uint128_t &p)
operator ^ for uint128_t (bitwise operator)
Definition uint128_t.hpp:946
uint128_t(const uint64_t high, const uint64_t low)
Parameterized constructor.
Definition uint128_t.hpp:115
uint128_t & operator*=(const uint128_t &p)
operator *= for uint128_t and other integer types.
Definition uint128_t.hpp:433
uint128_t & operator+=(const uint128_t &p)
operator += for uint128_t
Definition uint128_t.hpp:285
uint128_t operator&(const T p)
operator & for other types (bitwise operator)
Definition uint128_t.hpp:855
uint128_t & operator<<=(const T p)
operator <<= for uint128_t
Definition uint128_t.hpp:784
uint64_t lower() const
returns lower 64-bit integer part
Definition uint128_t.hpp:201
uint128_t & operator/=(const T p)
operator /= for uint128_t and other integer types.
Definition uint128_t.hpp:522
uint128_t operator^(const T p)
operator ^ for other types (bitwise operator)
Definition uint128_t.hpp:937
bool operator&&(const uint128_t &b)
operator && for uint128_t
Definition uint128_t.hpp:634
bool operator!=(const uint128_t &other)
operator != for uint128_t
Definition uint128_t.hpp:619
uint128_t & operator=(const uint128_t &p)=default
operator = for uint128_t
uint128_t & operator|=(const uint128_t &p)
operator |= for uint128_t (bitwise operator)
Definition uint128_t.hpp:910
uint128_t & operator=(const std::string &p)
operator = for type string
Definition uint128_t.hpp:227
uint128_t & operator-=(const uint128_t &p)
operator -= for uint128_t
Definition uint128_t.hpp:377
uint128_t operator%(const uint128_t &p)
operator % for uint128_t
Definition uint128_t.hpp:532
uint128_t & operator&=(const uint128_t &p)
operator &= for uint128_t (bitwise operator)
Definition uint128_t.hpp:865
uint128_t & operator++()
pre-increment operator
Definition uint128_t.hpp:296
uint128_t & operator=(const T &p)
operator = for other types
Definition uint128_t.hpp:217
bool operator<(const uint128_t &other)
operator < for uint128_t
Definition uint128_t.hpp:574
uint128_t operator&(const uint128_t &p)
operator & for uint128_t (bitwise operator)
Definition uint128_t.hpp:843
bool operator!()
operator ! for uint128_t
Definition uint128_t.hpp:627
uint128_t(T low)
Parameterized constructor.
Definition uint128_t.hpp:100
uint128_t operator%(const T &p)
operator % for uint128_t and other integer types.
Definition uint128_t.hpp:542
uint128_t & operator^=(const uint128_t &p)
operator ^= for uint128_t (bitwise operator)
Definition uint128_t.hpp:955
bool operator>=(const uint128_t &other)
operator >= for uint128_t
Definition uint128_t.hpp:601
uint128_t operator/(const T p)
operator / for uint128_t and other integer types.
Definition uint128_t.hpp:498
uint32_t _lez()
Leading zeroes in binary.
Definition uint128_t.hpp:139
bool operator()()
operator () for uint128_t
Definition uint128_t.hpp:651
uint128_t operator++(int)
post-increment operator
Definition uint128_t.hpp:305
std::string add(const std::string &first, const std::string &second)
Adding two string.
Definition uint128_t.hpp:37