20#ifndef CIPHERS_UINT128_T_HPP_
21#define CIPHERS_UINT128_T_HPP_
40 int16_t sum = 0, carry = 0;
41 for (int32_t i =
static_cast<int32_t
>(first.
size()) - 1,
42 j =
static_cast<int32_t
>(second.
size()) - 1;
43 i >= 0 || j >= 0; --i, --j) {
44 sum = ((i >= 0 ? first[i] -
'0' : 0) + (j >= 0 ? second[j] -
'0' : 0) +
71 this->f = this->s = 0;
72 if (str.
size() > 1 && str[1] ==
'x') {
73 for (
auto i = 2; i < str.
size(); ++i) {
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);
116 uint128_t(
const uint64_t high,
const uint64_t low) : f(high), s(low) {}
143 return __builtin_clzll(f);
145 return 64 + __builtin_clzll(s);
148 _BitScanForward64(&r, f);
151 _BitScanForward64(&l, s);
166 return __builtin_ctzll(f);
168 return 64 + __builtin_ctzll(s);
171 _BitScanReverse64(&r, s);
174 _BitScanReverse64(&l, f);
185 inline explicit operator bool()
const {
return (f || s); }
194 inline explicit operator T()
const {
195 return static_cast<T
>(s);
202 inline uint64_t
lower()
const {
return s; }
208 inline uint64_t
upper()
const {
return f; }
254 return uint128_t(f + (p + s < s), p + s);
263 return uint128_t(f + (p.s + s < s) + p.f, p.s + s);
275 bool app = p + s < s;
287 bool app = p.s + s < s;
331 return uint128_t(f - p.f - app, s - p.s);
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)
409 int cc = (tmp + tmp2 < tmp);
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);
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);
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;
458 }
else if (*
this == p) {
462 uint16_t left = tmp.
_lez() -
_lez();
467 uint16_t shf = tmp2._lez() - tmp.
_lez();
481 return {quotient << left, tmp2};
576 return f < other.f || (f == other.f && s < other.s);
585 return f < other.f || (f == other.f && s <= other.s);
594 return f > other.f || (f == other.f && s > other.s);
603 return (f > other.f) || (f == other.f && s >= other.s);
612 return f == other.f && s == other.s;
621 return f != other.f || s != other.s;
636 return (s || f) && (b.s || b.f);
645 return (s || f) || (b.s || b.f);
735 return (f || s) && b;
748 return (f || s) || b;
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))),
787 if (p >= 64 && p <= 128) {
788 this->f = (this->s << (p - 64));
791 f = ((this->f << p) + (this->s >> (64 - p)));
809 }
else if (p >= 64 && p <= 128) {
810 return uint128_t(0, (this->f >> (p - 64)));
811 }
else if (p < 64 && p > 0) {
813 (this->s >> p) + (this->f << (64 - p)));
830 s = (this->f >> (p - 64));
832 s = (this->s >> p) + (this->f << (64 - p));
845 return uint128_t(this->f & p.f, this->s & p.s);
903 return uint128_t(this->f | p.f, this->s | p.s);
948 return uint128_t(this->f ^ p.f, this->s ^ p.s);
989 for (
int i = 0; i < 64; ++i) {
990 if (p.s & (1LL << i)) {
995 for (
int i = 0; i < 64; ++i) {
996 if (p.f & (1LL << i)) {
1060inline bool operator&&(
const T p,
const uint128_t &q) {
1066inline bool operator||(
const T p,
const uint128_t &q) {
1073inline bool operator==(
const T p,
const uint128_t &q) {
1085inline bool operator<(
const T p,
const uint128_t &q) {
class for 128-bit unsigned integer
Definition uint128_t.hpp:60
uint128_t & operator%=(const T &p)
operator %= for uint128_t
Definition uint128_t.hpp:565
uint128_t operator-()
operator - using twos complement
Definition uint128_t.hpp:338
uint128_t & operator-=(const T &p)
operator -= for uint128_t and other integer types.
Definition uint128_t.hpp:366
bool operator&&(const T b)
operator && for other types
Definition uint128_t.hpp:734
uint128_t & operator>>=(const T p)
operator >>= for uint128_t
Definition uint128_t.hpp:826
uint128_t(const std::string &str)
Parameterized constructor.
Definition uint128_t.hpp:107
uint128_t operator+(const uint128_t &p)
operator + for uint128_t and other integer types.
Definition uint128_t.hpp:262
uint128_t operator<<(const T p)
operator << for uint128_t
Definition uint128_t.hpp:765
bool operator<=(const uint128_t &other)
operator <= for uint128_t
Definition uint128_t.hpp:584
uint128_t & operator--()
operator – (pre-decrement)
Definition uint128_t.hpp:344
uint64_t upper() const
returns upper 64-bit integer part
Definition uint128_t.hpp:208
uint128_t & operator&=(const T p)
operator &= for other types (bitwise operator)
Definition uint128_t.hpp:880
uint128_t & operator%=(const uint128_t &p)
operator %= for uint128_t
Definition uint128_t.hpp:552
bool operator>(const uint128_t &other)
operator > for uint128_t
Definition uint128_t.hpp:593
uint128_t operator--(int p)
operator – (post-decrement)
Definition uint128_t.hpp:353
uint128_t operator|(const uint128_t &p)
operator | for uint128_t (bitwise operator)
Definition uint128_t.hpp:902
uint128_t & operator/=(const uint128_t &p)
operator /= for uint128_t
Definition uint128_t.hpp:510
uint128_t & operator*=(const T p)
operator *= for uint128_t and other integer types.
Definition uint128_t.hpp:424
uint128_t operator/(const uint128_t &p)
operator / for uint128_t and other integer types.
Definition uint128_t.hpp:489
bool operator||(const uint128_t &b)
operator || for uint128_t
Definition uint128_t.hpp:644
bool operator>=(const T other)
operator >= for other types
Definition uint128_t.hpp:698
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:893
~uint128_t()=default
Destructor for uint128_t.
uint128_t operator~()
operator ~ for uint128_t
Definition uint128_t.hpp:755
uint128_t operator*(const uint128_t &p)
operator * for uint128_t and other integer types.
Definition uint128_t.hpp:402
uint128_t & operator^=(const T &p)
operator ^= for other types (bitwise operator)
Definition uint128_t.hpp:970
bool operator<=(const T other)
operator <= for other types
Definition uint128_t.hpp:674
uint128_t operator*(const T p)
operator * for uint128_t and other integer types.
Definition uint128_t.hpp:393
uint128_t operator+(const T p)
operator + for uint128_t and other integer types.
Definition uint128_t.hpp:253
uint128_t & operator+=(const T p)
operator += for uint128_t and other integer types.
Definition uint128_t.hpp:274
bool operator<(const T other)
operator < for other types
Definition uint128_t.hpp:662
friend std::ostream & operator<<(std::ostream &op, const uint128_t &p)
operator << for printing uint128_t integer
Definition uint128_t.hpp:984
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:925
uint128_t operator-(const T &p)
operator - for uint128_t and other integer types.
Definition uint128_t.hpp:319
uint128_t operator>>(const T p)
operator >> for uint128_t
Definition uint128_t.hpp:806
bool operator!=(const T other)
operator != for other types
Definition uint128_t.hpp:722
bool operator==(const T other)
operator == for other types
Definition uint128_t.hpp:710
bool operator==(const uint128_t &other)
operator == for uint128_t
Definition uint128_t.hpp:611
uint32_t _trz()
Trailing zeroes in binary.
Definition uint128_t.hpp:163
uint128_t(uint128_t &&num) noexcept
Move constructor.
Definition uint128_t.hpp:128
bool operator||(const T b)
operator || for other types
Definition uint128_t.hpp:747
uint128_t operator-(const uint128_t &p)
operator - for uint128_t
Definition uint128_t.hpp:329
bool operator>(const T other)
operator > for other types
Definition uint128_t.hpp:686
void __get_integer_from_string(const std::string &str)
First and second half of 128 bit number.
Definition uint128_t.hpp:70
std::pair< uint128_t, uint128_t > divide(const uint128_t &p)
divide function for uint128_t and other integer types.
Definition uint128_t.hpp:455
uint128_t operator^(const uint128_t &p)
operator ^ for uint128_t (bitwise operator)
Definition uint128_t.hpp:947
uint128_t(const uint64_t high, const uint64_t low)
Parameterized constructor.
Definition uint128_t.hpp:116
uint128_t & operator*=(const uint128_t &p)
operator *= for uint128_t and other integer types.
Definition uint128_t.hpp:434
uint128_t & operator+=(const uint128_t &p)
operator += for uint128_t
Definition uint128_t.hpp:286
uint128_t operator&(const T p)
operator & for other types (bitwise operator)
Definition uint128_t.hpp:856
uint128_t & operator<<=(const T p)
operator <<= for uint128_t
Definition uint128_t.hpp:785
uint64_t lower() const
returns lower 64-bit integer part
Definition uint128_t.hpp:202
uint128_t & operator/=(const T p)
operator /= for uint128_t and other integer types.
Definition uint128_t.hpp:523
uint128_t operator^(const T p)
operator ^ for other types (bitwise operator)
Definition uint128_t.hpp:938
bool operator&&(const uint128_t &b)
operator && for uint128_t
Definition uint128_t.hpp:635
bool operator!=(const uint128_t &other)
operator != for uint128_t
Definition uint128_t.hpp:620
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:911
uint128_t & operator=(const std::string &p)
operator = for type string
Definition uint128_t.hpp:228
uint128_t & operator-=(const uint128_t &p)
operator -= for uint128_t
Definition uint128_t.hpp:378
uint128_t operator%(const uint128_t &p)
operator % for uint128_t
Definition uint128_t.hpp:533
uint128_t & operator&=(const uint128_t &p)
operator &= for uint128_t (bitwise operator)
Definition uint128_t.hpp:866
uint128_t & operator++()
pre-increment operator
Definition uint128_t.hpp:297
uint128_t & operator=(const T &p)
operator = for other types
Definition uint128_t.hpp:218
bool operator<(const uint128_t &other)
operator < for uint128_t
Definition uint128_t.hpp:575
uint128_t operator&(const uint128_t &p)
operator & for uint128_t (bitwise operator)
Definition uint128_t.hpp:844
bool operator!()
operator ! for uint128_t
Definition uint128_t.hpp:628
uint128_t(T low)
Parameterized constructor.
Definition uint128_t.hpp:101
uint128_t operator%(const T &p)
operator % for uint128_t and other integer types.
Definition uint128_t.hpp:543
uint128_t & operator^=(const uint128_t &p)
operator ^= for uint128_t (bitwise operator)
Definition uint128_t.hpp:956
bool operator>=(const uint128_t &other)
operator >= for uint128_t
Definition uint128_t.hpp:602
uint128_t operator/(const T p)
operator / for uint128_t and other integer types.
Definition uint128_t.hpp:499
uint32_t _lez()
Leading zeroes in binary.
Definition uint128_t.hpp:140
bool operator()()
operator () for uint128_t
Definition uint128_t.hpp:652
uint128_t operator++(int)
post-increment operator
Definition uint128_t.hpp:306
std::string add(const std::string &first, const std::string &second)
Adding two string.
Definition uint128_t.hpp:38