20#ifndef CIPHERS_UINT128_T_HPP_
21#define CIPHERS_UINT128_T_HPP_
25struct std::is_integral<
uint128_t> : std::true_type {};
27struct std::is_arithmetic<
uint128_t> : std::true_type {};
29struct std::is_unsigned<
uint128_t> : std::true_type {};
38std::string
add(
const std::string &first,
const std::string &second) {
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) +
48 third.push_back(sum +
'0');
53 std::reverse(third.begin(), third.end());
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);
99 template <
typename T,
typename =
typename std::enable_if<
100 std::is_integral<T>::value, T>::type>
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); }
192 template <
typename T,
typename =
typename std::enable_if<
193 std::is_integral<T>::value, T>::type>
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; }
216 template <
typename T,
typename =
typename std::enable_if<
217 std::is_integral<T>::value, T>::type>
251 template <
typename T,
typename =
typename std::enable_if<
252 std::is_integral<T>::value, T>::type>
254 return uint128_t(f + (p + s < s), p + s);
263 return uint128_t(f + (p.s + s < s) + p.f, p.s + s);
272 template <
typename T,
typename =
typename std::enable_if<
273 std::is_integral<T>::value, T>::type>
275 bool app = p + s < s;
287 bool app = p.s + s < s;
317 template <
typename T,
typename =
typename std::enable_if<
318 std::is_integral<T>::value, T>::type>
331 return uint128_t(f - p.f - app, s - p.s);
364 template <
typename T,
typename =
typename std::enable_if<
365 std::is_integral<T>::value, T>::type>
391 template <
typename T,
typename =
typename std::enable_if<
392 std::is_integral<T>::value, T>::type>
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);
422 template <
typename T,
typename =
typename std::enable_if<
423 std::is_integral<T>::value, T>::type>
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};
497 template <
typename T,
typename =
typename std::enable_if<
498 std::is_integral<T>::value, T>::type>
521 template <
typename T,
typename =
typename std::enable_if<
522 std::is_integral<T>::value, T>::type>
541 template <
typename T,
typename =
typename std::enable_if<
542 std::is_integral<T>::value, T>::type>
563 template <
typename T,
typename =
typename std::enable_if<
564 std::is_integral<T>::value, T>::type>
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);
660 template <
typename T,
typename =
typename std::enable_if<
661 std::is_integral<T>::value, T>::type>
672 template <
typename T,
typename =
typename std::enable_if<
673 std::is_integral<T>::value, T>::type>
684 template <
typename T,
typename =
typename std::enable_if<
685 std::is_integral<T>::value, T>::type>
696 template <
typename T,
typename =
typename std::enable_if<
697 std::is_integral<T>::value, T>::type>
708 template <
typename T,
typename =
typename std::enable_if<
709 std::is_integral<T>::value, T>::type>
720 template <
typename T,
typename =
typename std::enable_if<
721 std::is_integral<T>::value, T>::type>
732 template <
typename T,
typename =
typename std::enable_if<
733 std::is_integral<T>::value, T>::type>
735 return (f || s) && b;
745 template <
typename T,
typename =
typename std::enable_if<
746 std::is_integral<T>::value, T>::type>
748 return (f || s) || b;
763 template <
typename T,
typename =
typename std::enable_if<
764 std::is_integral<T>::value, T>::type>
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))),
783 template <
typename T,
typename =
typename std::enable_if<
784 std::is_integral<T>::value, T>::type>
787 if (p >= 64 && p <= 128) {
788 this->f = (this->s << (p - 64));
791 f = ((this->f << p) + (this->s >> (64 - p)));
804 template <
typename T,
typename =
typename std::enable_if<
805 std::is_integral<T>::value, T>::type>
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)));
824 template <
typename T,
typename =
typename std::enable_if<
825 std::is_integral<T>::value, T>::type>
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);
854 template <
typename T,
typename =
typename std::enable_if<
855 std::is_integral<T>::value, T>::type>
878 template <
typename T,
typename =
typename std::enable_if<
879 std::is_integral<T>::value, T>::type>
891 template <
typename T,
typename =
typename std::enable_if<
892 std::is_integral<T>::value, T>::type>
903 return uint128_t(this->f | p.f, this->s | p.s);
923 template <
typename T,
typename =
typename std::enable_if<
924 std::is_integral<T>::value, T>::type>
936 template <
typename T,
typename =
typename std::enable_if<
937 std::is_integral<T>::value, T>::type>
948 return uint128_t(this->f ^ p.f, this->s ^ p.s);
968 template <
typename T,
typename =
typename std::enable_if<
969 std::is_integral<T>::value, T>::type>
988 std::string out =
"0", p_2 =
"1";
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)) {
1008template <
typename T,
typename =
typename std::enable_if<
1009 std::is_integral<T>::value, T>::type>
1014template <
typename T,
typename =
typename std::enable_if<
1015 std::is_integral<T>::value, T>::type>
1020template <
typename T,
typename =
typename std::enable_if<
1021 std::is_integral<T>::value, T>::type>
1026template <
typename T,
typename =
typename std::enable_if<
1027 std::is_integral<T>::value, T>::type>
1032template <
typename T,
typename =
typename std::enable_if<
1033 std::is_integral<T>::value, T>::type>
1039template <
typename T,
typename =
typename std::enable_if<
1040 std::is_integral<T>::value, T>::type>
1045template <
typename T,
typename =
typename std::enable_if<
1046 std::is_integral<T>::value, T>::type>
1051template <
typename T,
typename =
typename std::enable_if<
1052 std::is_integral<T>::value, T>::type>
1058template <
typename T,
typename =
typename std::enable_if<
1059 std::is_integral<T>::value, T>::type>
1060inline bool operator&&(
const T p,
const uint128_t &q) {
1064template <
typename T,
typename =
typename std::enable_if<
1065 std::is_integral<T>::value, T>::type>
1066inline bool operator||(
const T p,
const uint128_t &q) {
1071template <
typename T,
typename =
typename std::enable_if<
1072 std::is_integral<T>::value, T>::type>
1073inline bool operator==(
const T p,
const uint128_t &q) {
1077template <
typename T,
typename =
typename std::enable_if<
1078 std::is_integral<T>::value, T>::type>
1079inline bool operator!=(
const T p,
const uint128_t &q) {
1083template <
typename T,
typename =
typename std::enable_if<
1084 std::is_integral<T>::value, T>::type>
1085inline bool operator<(
const T p,
const uint128_t &q) {
1089template <
typename T,
typename =
typename std::enable_if<
1090 std::is_integral<T>::value, T>::type>
1091inline bool operator<=(
const T p,
const uint128_t &q) {
1095template <
typename T,
typename =
typename std::enable_if<
1096 std::is_integral<T>::value, T>::type>
1097inline bool operator>(
const T p,
const uint128_t &q) {
1101template <
typename T,
typename =
typename std::enable_if<
1102 std::is_integral<T>::value, T>::type>
1103inline bool operator>=(
const T p,
const uint128_t &q) {
class for 128-bit unsigned integer
uint128_t & operator%=(const T &p)
operator %= for uint128_t
uint128_t operator-()
operator - using twos complement
uint128_t & operator-=(const T &p)
operator -= for uint128_t and other integer types.
bool operator&&(const T b)
operator && for other types
uint128_t & operator>>=(const T p)
operator >>= for uint128_t
uint128_t(const std::string &str)
Parameterized constructor.
uint128_t operator+(const uint128_t &p)
operator + for uint128_t and other integer types.
uint128_t operator<<(const T p)
operator << for uint128_t
bool operator<=(const uint128_t &other)
operator <= for uint128_t
uint128_t & operator--()
operator – (pre-decrement)
uint64_t upper() const
returns upper 64-bit integer part
uint128_t & operator&=(const T p)
operator &= for other types (bitwise operator)
uint128_t & operator%=(const uint128_t &p)
operator %= for uint128_t
bool operator>(const uint128_t &other)
operator > for uint128_t
uint128_t operator--(int p)
operator – (post-decrement)
uint128_t operator|(const uint128_t &p)
operator | for uint128_t (bitwise operator)
uint128_t & operator/=(const uint128_t &p)
operator /= for uint128_t
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.
bool operator||(const uint128_t &b)
operator || for uint128_t
bool operator>=(const T other)
operator >= for other types
uint128_t & operator=(uint128_t &&p)=default
Move assignment operator.
uint128_t operator|(const T p)
operator | for other types (bitwise operator)
~uint128_t()=default
Destructor for uint128_t.
uint128_t operator~()
operator ~ for uint128_t
uint128_t operator*(const uint128_t &p)
operator * for uint128_t and other integer types.
uint128_t & operator^=(const T &p)
operator ^= for other types (bitwise operator)
bool operator<=(const T other)
operator <= for other types
uint128_t operator*(const T p)
operator * for uint128_t and other integer types.
uint128_t operator+(const T p)
operator + for uint128_t and other integer types.
uint128_t & operator+=(const T p)
operator += for uint128_t and other integer types.
bool operator<(const T other)
operator < for other types
friend std::ostream & operator<<(std::ostream &op, const uint128_t &p)
operator << for printing uint128_t integer
uint128_t(const uint128_t &num)=default
Copy constructor.
uint128_t & operator|=(const T p)
operator |= for other types (bitwise operator)
uint128_t operator-(const T &p)
operator - for uint128_t and other integer types.
uint128_t operator>>(const T p)
operator >> for uint128_t
bool operator!=(const T other)
operator != for other types
bool operator==(const T other)
operator == for other types
bool operator==(const uint128_t &other)
operator == for uint128_t
uint32_t _trz()
Trailing zeroes in binary.
uint128_t(uint128_t &&num) noexcept
Move constructor.
bool operator||(const T b)
operator || for other types
uint128_t operator-(const uint128_t &p)
operator - for uint128_t
bool operator>(const T other)
operator > for other types
void __get_integer_from_string(const std::string &str)
First and second half of 128 bit number.
std::pair< uint128_t, uint128_t > divide(const uint128_t &p)
divide function for uint128_t and other integer types.
uint128_t operator^(const uint128_t &p)
operator ^ for uint128_t (bitwise operator)
uint128_t(const uint64_t high, const uint64_t low)
Parameterized constructor.
uint128_t & operator*=(const uint128_t &p)
operator *= for uint128_t and other integer types.
uint128_t & operator+=(const uint128_t &p)
operator += for uint128_t
uint128_t operator&(const T p)
operator & for other types (bitwise operator)
uint128_t & operator<<=(const T p)
operator <<= for uint128_t
uint64_t lower() const
returns lower 64-bit integer part
uint128_t & operator/=(const T p)
operator /= for uint128_t and other integer types.
uint128_t operator^(const T p)
operator ^ for other types (bitwise operator)
bool operator&&(const uint128_t &b)
operator && for uint128_t
bool operator!=(const uint128_t &other)
operator != for uint128_t
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)
uint128_t & operator=(const std::string &p)
operator = for type string
uint128_t & operator-=(const uint128_t &p)
operator -= for uint128_t
uint128_t operator%(const uint128_t &p)
operator % for uint128_t
uint128_t & operator&=(const uint128_t &p)
operator &= for uint128_t (bitwise operator)
uint128_t & operator++()
pre-increment operator
uint128_t & operator=(const T &p)
operator = for other types
bool operator<(const uint128_t &other)
operator < for uint128_t
uint128_t operator&(const uint128_t &p)
operator & for uint128_t (bitwise operator)
bool operator!()
operator ! for uint128_t
uint128_t(T low)
Parameterized constructor.
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 (bitwise operator)
bool operator>=(const uint128_t &other)
operator >= for uint128_t
uint128_t operator/(const T p)
operator / for uint128_t and other integer types.
uint32_t _lez()
Leading zeroes in binary.
bool operator()()
operator () for uint128_t
uint128_t operator++(int)
post-increment operator
std::string add(const std::string &first, const std::string &second)
Adding two string.