Last active
February 4, 2020 18:19
-
-
Save Andersama/f7f895a6a2442b10941e690dc3cf258c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #pragma once | |
| #include <memory> | |
| #include <functional> | |
| #include <vector> | |
| #include <string> | |
| #include <compare> | |
| #include <array> | |
| #include <type_traits> | |
| #include <intrin.h> | |
| #include <immintrin.h> | |
| #include <xmmintrin.h> | |
| #include <execution> | |
| #include <charconv> | |
| #include <iostream> | |
| //#include "SSEOps.h" | |
| #if __has_include(<span>) | |
| #include <span> | |
| #else | |
| /* https://github.com/tcbrindle/span */ | |
| #include "span.hpp" | |
| namespace std { | |
| using tcb::span; | |
| } | |
| #endif | |
| template<typename T, size_t N = 16> | |
| class aligned_allocator : std::pmr::polymorphic_allocator<T> { | |
| public: | |
| typedef T value_type; | |
| typedef T& reference; | |
| typedef const T& const_reference; | |
| typedef T* pointer; | |
| typedef const T* const_pointer; | |
| typedef size_t size_type; | |
| typedef ptrdiff_t difference_type; | |
| template <class U> | |
| struct rebind | |
| { | |
| typedef aligned_allocator<U, N> other; | |
| }; | |
| inline aligned_allocator() throw() {} | |
| inline aligned_allocator(const aligned_allocator&) throw() {} | |
| template <class U> | |
| inline aligned_allocator(const aligned_allocator<U, N>&) throw() {} | |
| inline ~aligned_allocator() throw() {} | |
| inline pointer address(reference r) { return &r; } | |
| inline const_pointer address(const_reference r) const { return &r; } | |
| //pointer allocate(size_type n, typename std::allocator<void>::const_pointer hint = 0); | |
| //inline void deallocate(pointer p, size_type); | |
| inline [[nodiscard]] T* allocate(std::size_t count) { | |
| return static_cast<T*>(_aligned_malloc(sizeof(T) * count, N)); | |
| } | |
| inline void deallocate(T* p, size_t) { | |
| _aligned_free(p); | |
| } | |
| inline void construct(pointer p, const_reference value) { new (p) value_type(value); } | |
| inline void destroy(pointer p) { p->~value_type(); } | |
| inline size_type max_size() const throw() { return size_type(-1) / sizeof(T); } | |
| inline bool operator==(const aligned_allocator&) { return true; } | |
| inline bool operator!=(const aligned_allocator& rhs) { return !operator==(rhs); } | |
| }; | |
| template<typename, typename = void> | |
| struct is_addable : std::false_type {}; | |
| template <size_t> | |
| struct make_of_size { | |
| template<class> | |
| using t = std::false_type; | |
| }; | |
| template <> | |
| struct make_of_size<1> { | |
| template<class> | |
| using t = uint8_t; | |
| }; | |
| template <> | |
| struct make_of_size<2> { | |
| template<class> | |
| using t = uint16_t; | |
| }; | |
| template <> | |
| struct make_of_size<4> { | |
| template<class> | |
| using t = uint32_t; | |
| }; | |
| template <> | |
| struct make_of_size<8> { | |
| template<class> | |
| using t = uint64_t; | |
| }; | |
| template <size_t N> | |
| using make_size = typename make_of_size<N>::template t<uint8_t>; | |
| template <class Ty> | |
| using make_w_size = typename make_of_size<sizeof(Ty)>::template t<Ty>; | |
| template <class _Ty> | |
| using make_bigger = typename make_of_size<sizeof(_Ty) * 2>::template t<_Ty>; | |
| template <class _Ty> | |
| using make_smaller = typename make_of_size<sizeof(_Ty) / 2>::template t<_Ty>; | |
| static_assert(std::is_same<make_size<1>, uint8_t>::value, "size 1 is not uint8_t"); | |
| static_assert(std::is_same<make_size<2>, uint16_t>::value, "size 2 is not uint16_t"); | |
| static_assert(std::is_same<make_size<4>, uint32_t>::value, "size 4 is not uint32_t"); | |
| static_assert(std::is_same<make_size<8>, uint64_t>::value, "size 8 is not uint64_t"); | |
| static_assert(std::is_same<make_w_size<uint8_t>, uint8_t>::value, "size 1 is not uint8_t"); | |
| static_assert(std::is_same<make_w_size<uint16_t>, uint16_t>::value, "size 2 is not uint16_t"); | |
| static_assert(std::is_same<make_w_size<uint32_t>, uint32_t>::value, "size 4 is not uint32_t"); | |
| static_assert(std::is_same<make_w_size<uint64_t>, uint64_t>::value, "size 8 is not uint64_t"); | |
| static_assert(std::is_same<make_bigger<uint8_t>, uint16_t>::value, "failed to scale up from 8"); | |
| static_assert(std::is_same<make_bigger<uint16_t>, uint32_t>::value, "failed to scale up from 16"); | |
| static_assert(std::is_same<make_bigger<uint32_t>, uint64_t>::value, "failed to scale up from 32"); | |
| static_assert(std::is_same<make_bigger<uint64_t>, std::false_type>::value, "failed to scale up from 64"); | |
| static_assert(std::is_same<make_bigger<int8_t>, uint16_t>::value, "failed to scale up from 8"); | |
| static_assert(std::is_same<make_bigger<int16_t>, uint32_t>::value, "failed to scale up from 16"); | |
| static_assert(std::is_same<make_bigger<int32_t>, uint64_t>::value, "failed to scale up from 32"); | |
| static_assert(std::is_same<make_bigger<int64_t>, std::false_type>::value, "failed to scale up from 64"); | |
| static_assert(!std::is_same<make_bigger<int8_t>, std::false_type>::value, "8 is scalable"); | |
| static_assert(!std::is_same<make_bigger<int16_t>, std::false_type>::value, "16 is scalable"); | |
| static_assert(!std::is_same<make_bigger<int32_t>, std::false_type>::value, "32 is scalable"); | |
| static_assert(!std::is_same<std::false_type, make_bigger<int8_t>>::value, "8 is scalable"); | |
| static_assert(!std::is_same<std::false_type, make_bigger<int16_t>>::value, "16 is scalable"); | |
| static_assert(!std::is_same<std::false_type, make_bigger<int32_t>>::value, "32 is scalable"); | |
| static_assert(std::is_same<make_smaller<uint8_t>, std::false_type>::value, "failed to scale down from 8"); | |
| static_assert(std::is_same<make_smaller<uint16_t>, uint8_t>::value, "failed to scale down from 16"); | |
| static_assert(std::is_same<make_smaller<uint32_t>, uint16_t>::value, "failed to scale down from 32"); | |
| static_assert(std::is_same<make_smaller<uint64_t>, uint32_t>::value, "failed to scale down from 64"); | |
| template<typename T> | |
| constexpr auto lo_bits(T v) { | |
| const T mask = ~(T{ 0 }) >> (sizeof(T) * 4); | |
| return v & mask; | |
| } | |
| template<typename T> | |
| constexpr auto hi_bits(T v) { | |
| return lo_bits(v >> (sizeof(T) * 4)); | |
| } | |
| //template<typename T> | |
| //constexpr auto add(const T lhs, const T rhs); | |
| template<typename T> | |
| constexpr std::enable_if_t<std::is_same<T, bool>::value, std::pair<T, T>> | |
| add(const T lhs, const T rhs) { | |
| return std::make_pair(lhs && rhs, lhs != rhs); | |
| } | |
| //unsigned addition (quick) | |
| template<typename T> | |
| constexpr std::enable_if_t< | |
| !std::is_same<T, bool>::value && | |
| std::is_arithmetic<T>::value && | |
| std::is_same<T, std::make_unsigned_t<T>>::value && | |
| !std::is_same<std::false_type, make_bigger<T>>::value, | |
| std::pair<T, T>> | |
| add(const T lhs, const T rhs) { | |
| using BT = std::make_unsigned_t<make_bigger<T>>; | |
| using UT = std::make_unsigned_t<T>; | |
| const BT ret = (BT)lhs + (BT)rhs; | |
| return std::make_pair((UT)hi_bits(ret), (UT)lo_bits(ret)); | |
| } | |
| //signed addition (quick) | |
| template<typename T> | |
| constexpr std::enable_if_t< | |
| !std::is_same<T, bool>::value && | |
| std::is_arithmetic<T>::value && | |
| std::is_same<T, std::make_signed_t<T>>::value && | |
| !std::is_same<std::false_type, make_bigger<T>>::value, std::pair<T, T>> | |
| add(const T lhs, const T rhs) { | |
| using BT = std::make_signed_t<make_bigger<T>>; | |
| using UT = std::make_unsigned_t<T>; | |
| const BT ret = (BT)lhs + (BT)rhs; | |
| return std::make_pair((UT)hi_bits(ret), (UT)lo_bits(ret)); | |
| } | |
| //unsigned addition (slow) | |
| template<typename T> | |
| constexpr std::enable_if_t< | |
| !std::is_same<T, bool>::value && | |
| std::is_arithmetic<T>::value && | |
| std::is_same<T, std::make_unsigned_t<T>>::value && | |
| std::is_same<std::false_type, make_bigger<T>>::value, std::pair<T, T>> | |
| add(const T lhs, const T rhs) { | |
| const T r = lhs + rhs; | |
| return std::make_pair(r < lhs ? (T)1 : (T)0, r); | |
| } | |
| //signed addition (slow) | |
| template<typename T> | |
| constexpr std::enable_if_t< | |
| !std::is_same<T, bool>::value && | |
| std::is_arithmetic<T>::value && | |
| std::is_same<T, std::make_signed_t<T>>::value && | |
| std::is_same<std::false_type, make_bigger<T>>::value, std::pair<T, T>> | |
| add(const T lhs, const T rhs) { | |
| using UT = std::make_unsigned_t<T>; | |
| const UT r = (UT)lhs + (UT)rhs; | |
| return std::make_pair(r < (UT)lhs ? (T)-1 : (T)0, (T)r); | |
| } | |
| using std::vector; | |
| //unsigned multiplication (fast) | |
| template<typename T> | |
| constexpr std::enable_if_t< | |
| !std::is_same<T, bool>::value && | |
| std::is_arithmetic<T>::value && | |
| std::is_same<T, std::make_unsigned_t<T>>::value && | |
| !std::is_same<std::false_type, make_bigger<T>>::value, | |
| std::pair<T, T>> | |
| mult(const T lhs, const T rhs) { | |
| using BT = std::make_signed_t<make_bigger<T>>; | |
| using UT = std::make_unsigned_t<T>; | |
| const BT ret = (BT)lhs * (BT)rhs; | |
| return std::make_pair((UT)hi_bits(ret), (UT)lo_bits(ret)); | |
| } | |
| //unsigned multplication (slow) | |
| template<typename T> | |
| constexpr std::enable_if_t< | |
| !std::is_same<T, bool>::value && | |
| std::is_arithmetic<T>::value && | |
| std::is_same<T, std::make_unsigned_t<T>>::value && | |
| std::is_same<std::false_type, make_bigger<T>>::value, | |
| std::pair<T, T>> | |
| mult(const T lhs, const T rhs) { | |
| using UT = std::make_unsigned_t<T>; | |
| const UT a0 = lo_bits(lhs); | |
| const UT a1 = hi_bits(lhs); | |
| const UT b0 = lo_bits(rhs); | |
| const UT b1 = hi_bits(rhs); | |
| const UT a0b0 = a0 * b0; | |
| const UT a0b1 = a0 * b1; | |
| const UT a1b0 = a1 * b0; | |
| const UT a1b1 = a1 * b1; | |
| const UT la0b1 = lo_bits(a0b1); | |
| const UT la1b0 = lo_bits(a1b0); | |
| const UT ha0b1 = hi_bits(a0b1); | |
| const UT ha1b0 = hi_bits(a1b0); | |
| const size_t shift = sizeof(UT) * 4; | |
| const UT carry = (hi_bits(a0b0) + la0b1 + la1b0); | |
| const UT carry_r = carry >> shift; | |
| const UT lo = a0b0 + ((la0b1 + la1b0) << shift); | |
| const UT hi = a1b1 + ha0b1 + ha1b0 + carry_r; | |
| return std::make_pair( | |
| hi, | |
| lo | |
| ); | |
| } | |
| template<typename T> | |
| constexpr std::enable_if_t< | |
| !std::is_same<std::false_type, make_bigger<T>>::value && | |
| !std::is_same<T, bool>::value && | |
| std::is_arithmetic<T>::value && | |
| !std::is_signed<T>::value | |
| , T> | |
| add_w_carry(const T a, const T b, T& c) { | |
| using BT = std::make_unsigned_t<make_bigger<T>>; | |
| const BT result = (BT)a + (BT)b + (BT)c; | |
| c = (T)hi_bits(result); | |
| return (T)lo_bits(result); | |
| } | |
| template<typename T> | |
| constexpr std::enable_if_t< | |
| std::is_same<std::false_type, make_bigger<T>>::value && | |
| !std::is_same<T, bool>::value && | |
| std::is_arithmetic<T>::value && | |
| !std::is_signed<T>::value, | |
| T> | |
| add_w_carry(const T a, const T b, T& c) { | |
| const T c0 = c; | |
| auto rpair = add(a, c0); | |
| auto rpair2 = add(rpair.second, b); | |
| c = rpair.first + rpair2.first; | |
| return rpair2.second; | |
| } | |
| /* | |
| template<typename T> | |
| constexpr std::enable_if_t< | |
| !std::is_same<std::false_type, make_bigger<T>>::value && | |
| !std::is_same<T, bool>::value && | |
| std::is_arithmetic<T>::value && | |
| !std::is_signed<T>::value, | |
| std::pair<T,T>> | |
| div_w_rem(const T a, const T b) { | |
| using BT = std::make_unsigned_t<make_bigger<T>>; | |
| return std::make_pair((BT)a/(BT)b, (BT)a%(BT)b); | |
| } | |
| template<typename T> | |
| constexpr std::enable_if_t< | |
| std::is_same<std::false_type, make_bigger<T>>::value && | |
| !std::is_same<T, bool>::value && | |
| std::is_arithmetic<T>::value && | |
| !std::is_signed<T>::value, | |
| std::pair<T,T>> | |
| div_w_rem(const T a, const T b) { | |
| const T c0 = c; | |
| auto rpair = add(a, c0); | |
| auto rpair2 = add(rpair.second, b); | |
| c = rpair.first + rpair2.first; | |
| return rpair2.second; | |
| } | |
| */ | |
| //using vector_t = vector<UT, aligned_allocator<UT, 32>>; | |
| template <typename T, size_t N> | |
| using aligned_vector = vector<T, aligned_allocator<T, N>>; | |
| template<typename T> | |
| std::enable_if_t< | |
| !std::is_same<T, bool>::value && | |
| std::is_arithmetic<T>::value && | |
| !std::is_signed<T>::value, | |
| std::vector<T>>add_vec(const vector<T>& lhs, const vector<T>& rhs) { | |
| const size_t mx = lhs.size() > rhs.size() ? lhs.size() + 1 : rhs.size() + 1; | |
| const size_t mn = lhs.size() < rhs.size() ? lhs.size() : rhs.size(); | |
| const vector<T>& tmp = lhs.size() > rhs.size() ? lhs : rhs; | |
| vector<T> ret(mx, 0); | |
| const bool nl = lhs.size() ? lhs.back() & ~(~(T{ 0 }) >> 1) : false; | |
| const bool nr = rhs.size() ? rhs.back() & ~(~(T{ 0 }) >> 1) : false; | |
| const T pad_lhs = nl ? ~(T{ 0 }) : T{ 0 }; | |
| const T pad_rhs = nr ? ~(T{ 0 }) : T{ 0 }; | |
| const T pad_tmp = lhs.size() > rhs.size() ? pad_rhs : pad_lhs; | |
| T carry{ 0 }; | |
| size_t i = 0; | |
| for (; i < mn; i++) { | |
| ret[i] = add_w_carry(lhs[i], rhs[i], carry); | |
| } | |
| for (; i < tmp.size(); i++) { | |
| ret[i] = add_w_carry(tmp[i], pad_tmp, carry); | |
| } | |
| for (; i < mx; i++) { | |
| ret[i] = add_w_carry(pad_lhs, pad_rhs, carry); | |
| } | |
| return ret; | |
| } | |
| template<typename T, size_t N> | |
| std::enable_if_t< | |
| !std::is_same<T, bool>::value && | |
| std::is_arithmetic<T>::value && | |
| !std::is_signed<T>::value, | |
| aligned_vector<T,N>>add_vec(const aligned_vector<T, N>& lhs, const aligned_vector<T, N>& rhs) { | |
| const size_t mx = lhs.size() > rhs.size() ? lhs.size() + 1 : rhs.size() + 1; | |
| const size_t mn = lhs.size() < rhs.size() ? lhs.size() : rhs.size(); | |
| const size_t mxl = mx - 1; | |
| const aligned_vector<T, N>& tmp = lhs.size() > rhs.size() ? lhs : rhs; | |
| aligned_vector<T, N> aligned_r(mx, 0); | |
| aligned_vector<T, N> aligned_c(mx, 0); | |
| T* ret = aligned_r.data(); | |
| T* carry = aligned_c.data(); | |
| const bool nl = lhs.size() ? lhs.back() & ~(~(T{ 0 }) >> 1) : false; | |
| const bool nr = rhs.size() ? rhs.back() & ~(~(T{ 0 }) >> 1) : false; | |
| const T pad_lhs = nl ? ~(T{ 0 }) : T{ 0 }; | |
| const T pad_rhs = nr ? ~(T{ 0 }) : T{ 0 }; | |
| const T pad_tmp = lhs.size() > rhs.size() ? pad_rhs : pad_lhs; | |
| //T carry{ 0 }; | |
| size_t i = 0; | |
| for (; i < mn; i++) { | |
| ret[i] = lhs.data()[i] + rhs.data()[i]; | |
| carry[i + 1] = ret[i] < lhs[i]; | |
| } | |
| for (; i < tmp.size(); i++) { | |
| ret[i] = tmp[i] + pad_tmp; | |
| carry[i + 1] = ret[i] < tmp[i]; | |
| } | |
| ret[tmp.size()] = pad_lhs + pad_rhs; | |
| //proper ripple carry | |
| for (size_t i = 1; i < mx; i++) { | |
| ret[i] = ret[i] + carry[i]; | |
| carry[i + 1] += ret[i] < carry[i]; | |
| } | |
| return aligned_r; | |
| /* | |
| for (; i < mn; i++) { | |
| ret[i] = add_w_carry(lhs[i], rhs[i], carry); | |
| } | |
| for (; i < tmp.size(); i++) { | |
| ret[i] = add_w_carry(tmp[i], pad_tmp, carry); | |
| } | |
| for (; i < mx; i++) { | |
| ret[i] = add_w_carry(pad_lhs, pad_rhs, carry); | |
| } | |
| */ | |
| } | |
| template<typename T, size_t N> | |
| /* | |
| std::enable_if_t< | |
| !std::is_same<T, bool>::value && | |
| std::is_arithmetic<T>::value && | |
| !std::is_signed<T>::value, | |
| aligned_vector<T, N>> | |
| */ | |
| void add_vec(const aligned_vector<T, N>& lhs, | |
| const aligned_vector<T, N>& rhs, | |
| aligned_vector<T, N> &aligned_r, | |
| aligned_vector<T, N> &aligned_c) { | |
| const size_t mx = lhs.size() > rhs.size() ? lhs.size() + 1 : rhs.size() + 1; | |
| const size_t mn = lhs.size() < rhs.size() ? lhs.size() : rhs.size(); | |
| const aligned_vector<T, N>& tmp = lhs.size() > rhs.size() ? lhs : rhs; | |
| //aligned_r.clear(); | |
| //aligned_c.clear(); | |
| //aligned_r.reserve(mx); | |
| //aligned_c.reserve(mx); | |
| aligned_r.resize(mx); | |
| aligned_c.resize(mx); | |
| /* | |
| for (size_t i = 0; i < mx; i++) { | |
| aligned_r.emplace_back(0); | |
| aligned_c.emplace_back(0); | |
| } | |
| */ | |
| //aligned_r.assign(mx, 0); | |
| //aligned_c.assign(mx, 0); | |
| T* ret = aligned_r.data(); | |
| T* carry = aligned_c.data(); | |
| const bool nl = lhs.size() ? lhs.back() & ~(~(T{ 0 }) >> 1) : false; | |
| const bool nr = rhs.size() ? rhs.back() & ~(~(T{ 0 }) >> 1) : false; | |
| const T pad_lhs = nl ? ~(T{ 0 }) : T{ 0 }; | |
| const T pad_rhs = nr ? ~(T{ 0 }) : T{ 0 }; | |
| const T pad_tmp = lhs.size() > rhs.size() ? pad_rhs : pad_lhs; | |
| //T carry{ 0 }; | |
| size_t i = 0; | |
| for (; i < mn; i++) { | |
| ret[i] = lhs.data()[i] + rhs.data()[i]; | |
| carry[i + 1] = ret[i] < lhs[i]; | |
| } | |
| for (; i < tmp.size(); i++) { | |
| ret[i] = tmp[i] + pad_tmp; | |
| carry[i + 1] = ret[i] < tmp[i]; | |
| } | |
| ret[tmp.size()] = pad_lhs + pad_rhs; | |
| //proper ripple carry | |
| for (size_t i = 1; i < mx; i++) { | |
| ret[i] = ret[i] + carry[i]; | |
| carry[i + 1] += ret[i] < carry[i]; | |
| } | |
| } | |
| template<typename T, size_t N> | |
| /* | |
| std::enable_if_t< | |
| !std::is_same<T, bool>::value && | |
| std::is_arithmetic<T>::value && | |
| !std::is_signed<T>::value, | |
| aligned_vector<T, N>> | |
| */ | |
| void add_vec(const aligned_vector<T, N>& lhs, | |
| const T& rhs, | |
| aligned_vector<T, N>& aligned_r, | |
| aligned_vector<T, N>& aligned_c) { | |
| const size_t mx = lhs.size() > 1 ? lhs.size() + 1 : (1 + 1); | |
| const size_t mn = lhs.size() < 1 ? lhs.size() : 1; | |
| aligned_r.resize(mx); | |
| aligned_c.resize(mx); | |
| T* ret = aligned_r.data(); | |
| T* carry = aligned_c.data(); | |
| const bool nl = lhs.size() ? lhs.back() & ~(~(T{ 0 }) >> 1) : false; | |
| const bool nr = rhs & ~(~(T{ 0 }) >> 1); | |
| const T pad_lhs = nl ? ~(T{ 0 }) : T{ 0 }; | |
| const T pad_rhs = nr ? ~(T{ 0 }) : T{ 0 }; | |
| const T pad_tmp = lhs.size() > 1 ? pad_rhs : pad_lhs; | |
| //T carry{ 0 }; | |
| size_t i = 0; | |
| for (; i < mn; i++) { | |
| ret[i] = lhs[i] + rhs; | |
| carry[i + 1] = ret[i] < lhs[i]; | |
| } | |
| for (; i < lhs.size(); i++) { | |
| ret[i] = lhs[i] + pad_tmp; | |
| carry[i + 1] = ret[i] < lhs[i]; | |
| } | |
| ret[lhs.size()] = pad_lhs + pad_rhs; | |
| //proper ripple carry | |
| for (size_t i = 1; i < mx; i++) { | |
| ret[i] = ret[i] + carry[i]; | |
| carry[i + 1] += ret[i] < carry[i]; | |
| } | |
| } | |
| template<typename T, size_t N> | |
| void add_vec(const aligned_vector<T, N>& lhs, | |
| const aligned_vector<T, N>& rhs, | |
| aligned_vector<T, N>& aligned_r) { | |
| const size_t mx = lhs.size() > rhs.size() ? lhs.size() + 1 : rhs.size() + 1; | |
| const size_t mn = lhs.size() < rhs.size() ? lhs.size() : rhs.size(); | |
| const aligned_vector<T, N>& tmp = lhs.size() > rhs.size() ? lhs : rhs; | |
| aligned_r.resize(2*mx); | |
| aligned_r.resize(mx); | |
| //aligned_c.resize(mx); | |
| //aligned_vector<T, N>& aligned_c | |
| T* ret = aligned_r.data(); | |
| T* carry = aligned_r.data() + mx; | |
| const bool nl = lhs.size() ? lhs.back() & ~(~(T{ 0 }) >> 1) : false; | |
| const bool nr = rhs.size() ? rhs.back() & ~(~(T{ 0 }) >> 1) : false; | |
| const T pad_lhs = nl ? ~(T{ 0 }) : T{ 0 }; | |
| const T pad_rhs = nr ? ~(T{ 0 }) : T{ 0 }; | |
| const T pad_tmp = lhs.size() > rhs.size() ? pad_rhs : pad_lhs; | |
| //T carry{ 0 }; | |
| size_t i = 0; | |
| for (; i < mn; i++) { | |
| ret[i] = lhs.data()[i] + rhs.data()[i]; | |
| carry[i + 1] = ret[i] < lhs[i]; | |
| } | |
| for (; i < tmp.size(); i++) { | |
| ret[i] = tmp[i] + pad_tmp; | |
| carry[i + 1] = ret[i] < tmp[i]; | |
| } | |
| ret[tmp.size()] = pad_lhs + pad_rhs; | |
| //proper ripple carry | |
| for (size_t i = 1; i < mx; i++) { | |
| ret[i] = ret[i] + carry[i]; | |
| carry[i + 1] += ret[i] < carry[i]; | |
| } | |
| } | |
| template<typename T, size_t N> | |
| void add_vec(const aligned_vector<T, N>& lhs, | |
| const T& rhs, | |
| aligned_vector<T, N>& aligned_r) { | |
| const size_t mx = lhs.size() > 1 ? lhs.size() + 1 : (1 + 1); | |
| const size_t mn = lhs.size() < 1 ? lhs.size() : 1; | |
| aligned_r.resize(2*mx); | |
| aligned_r.resize(mx); | |
| T* ret = aligned_r.data(); | |
| T* carry = (aligned_r.data() + mx); | |
| const bool nl = lhs.size() ? lhs.back() & ~(~(T{ 0 }) >> 1) : false; | |
| const bool nr = rhs & ~(~(T{ 0 }) >> 1); | |
| const T pad_lhs = nl ? ~(T{ 0 }) : T{ 0 }; | |
| const T pad_rhs = nr ? ~(T{ 0 }) : T{ 0 }; | |
| const T pad_tmp = lhs.size() > 1 ? pad_rhs : pad_lhs; | |
| //T carry{ 0 }; | |
| size_t i = 0; | |
| for (; i < mn; i++) { | |
| ret[i] = lhs[i] + rhs; | |
| carry[i + 1] = ret[i] < lhs[i]; | |
| } | |
| for (; i < lhs.size(); i++) { | |
| ret[i] = lhs[i] + pad_tmp; | |
| carry[i + 1] = ret[i] < lhs[i]; | |
| } | |
| ret[lhs.size()] = pad_lhs + pad_rhs; | |
| //proper ripple carry | |
| for (size_t i = 1; i < mx; i++) { | |
| ret[i] = ret[i] + carry[i]; | |
| carry[i + 1] += ret[i] < carry[i]; | |
| } | |
| } | |
| template<typename T, size_t N> | |
| /* | |
| std::enable_if_t< | |
| !std::is_same<T, bool>::value && | |
| std::is_arithmetic<T>::value && | |
| !std::is_signed<T>::value, | |
| aligned_vector<T, N>> | |
| */ | |
| void mult_vec(const aligned_vector<T, N>& lhs, | |
| const aligned_vector<T, N>& rhs, | |
| aligned_vector<T, N>& product) { | |
| const size_t count = lhs.size() + rhs.size() + 1; | |
| //product.resize(count, 0); | |
| product.resize(count); | |
| std::fill(product.begin(), product.end(), 0); | |
| //for each digit of result | |
| for (size_t i = 0; i < lhs.size(); i++) { | |
| T c{ 0 }; | |
| T r{ 0 }; | |
| T c2{ 0 }; | |
| //mult w/ carry | |
| for (size_t j = 0; j < rhs.size(); j++) { | |
| const size_t idx = i + j; | |
| std::tie(c2, product[idx]) = add(product[idx], c); | |
| std::tie(c, r) = mult(lhs[i], rhs[j]); | |
| std::tie(r, product[idx]) = add(product[idx], r); | |
| c += c2 + r; | |
| } | |
| //carry through rest of result | |
| for (size_t j = rhs.size() + i; j < product.size(); j++) { | |
| std::tie(c, product[j]) = add(c, product[j]); | |
| } | |
| } | |
| } | |
| template<typename T, size_t N> | |
| /* | |
| std::enable_if_t< | |
| !std::is_same<T, bool>::value && | |
| std::is_arithmetic<T>::value && | |
| !std::is_signed<T>::value, | |
| aligned_vector<T, N>> | |
| */ | |
| void mult_vec(const aligned_vector<T, N>& lhs, | |
| const T& rhs, | |
| aligned_vector<T, N>& product) { | |
| const size_t count = lhs.size() + (1 + 1); | |
| product.resize(count); | |
| std::fill(product.begin(), product.end(), 0); | |
| //for each digit of result | |
| for (size_t i = 0; i < lhs.size(); i++) { | |
| T c{ 0 }; | |
| T r{ 0 }; | |
| T c2{ 0 }; | |
| //mult w/ carry | |
| const size_t idx = i; | |
| std::tie(c2, product[idx]) = add(product[idx], c); | |
| std::tie(c, r) = mult(lhs[i], rhs); | |
| std::tie(r, product[idx]) = add(product[idx], r); | |
| c += c2 + r; | |
| //carry through rest of result | |
| for (size_t j = i + 1; j < product.size(); j++) | |
| std::tie(c, product[j]) = add(c, product[j]); | |
| } | |
| } | |
| template<typename T> | |
| class wide_int { | |
| static_assert(!std::is_same_v<T, bool>&& std::is_trivial<T>::value, "must be trivial and not a bool"); | |
| using UT = T; | |
| using vector_t = vector<T, aligned_allocator<T, 32>>; | |
| using span_t = std::span<T>; | |
| public: | |
| vector_t digits = { 0,0 }; | |
| wide_int(const wide_int& rhs) { | |
| digits = rhs.digits; | |
| } | |
| wide_int(const vector_t& rhs) { | |
| digits = rhs; | |
| } | |
| wide_int(const UT& rhs) { | |
| digits.clear(); | |
| digits.push_back(rhs); | |
| } | |
| wide_int(const UT& rhs, const uint64_t &size) { | |
| digits.clear(); | |
| digits.reserve(std::max(size,1)); | |
| digits.push_back(rhs); | |
| } | |
| operator std::string() const { | |
| std::string ret = ""; | |
| wide_int abs_v = is_neg() ? -(*this) : *this; | |
| UT mx = max_powbase(10); | |
| std::string tmp = std::to_string(mx); | |
| vector_t q; | |
| vector_t q0; | |
| vector_t r0; | |
| vector_t mul; | |
| vector_t _buffer; | |
| //10 (size 2) | |
| // 9 | |
| std::string zer = tmp; | |
| std::fill(zer.begin(), zer.end(), '0'); | |
| std::string buf; | |
| buf.reserve(64); | |
| //std::string tmp = std::to_string(mx); | |
| const wide_int mxb = mx; | |
| wide_int r = mx; | |
| wide_int d = 0; | |
| ret.reserve((2 * abs_v.digits.size() * tmp.size()) + 1); | |
| //std::tie(d, r) = plain_division(abs_v, mx); | |
| //std::tie(d, r) = division_helper(abs_v, mxb); | |
| //std::tie(d, r) = newton_division(abs_v, mx); | |
| _division(abs_v, mx, q, q0, r0, mul, _buffer); | |
| d.digits = q; | |
| r.digits = r0; | |
| while (d > 0) { | |
| auto v = std::to_chars(buf.data(), buf.data() + buf.capacity(), r.digits[0]); | |
| std::string_view num{ buf.data(), (size_t)std::distance(buf.data(), v.ptr) }; | |
| std::string_view padding{ zer.data(), (tmp.size()-1) - num.size() }; | |
| //std::string num = std::to_string(r.digits[0]); | |
| //std::string padding((tmp.size() - 1) - num.size(), '0'); | |
| ret.insert(0, num); | |
| ret.insert(0, padding); | |
| //ret = padding + num + ret; | |
| //std::tie(d, r) = plain_division(d, mx); | |
| //std::tie(d, r) = division_helper(d, mxb); | |
| //std::tie(d, r) = newton_division(d, mx); | |
| _division(d, mx, q, q0, r0, mul, _buffer); | |
| d.digits = q; | |
| r.digits = r0; | |
| } | |
| auto v = std::to_chars(buf.data(), buf.data() + buf.capacity(), r.digits[0]); | |
| std::string_view num{ buf.data(), (size_t)std::distance(buf.data(), v.ptr) }; | |
| ret.insert(0, num); | |
| //ret = num + ret; | |
| //ret = std::to_string(r.digits[0]) + ret; | |
| if (is_neg()) { | |
| ret = "-" + ret; | |
| } | |
| return ret; | |
| } | |
| constexpr std::string to_string() const { | |
| return (std::string)*this; | |
| } | |
| static constexpr UT max_mask() noexcept { | |
| UT t = -1; | |
| return t; | |
| } | |
| static constexpr UT max_powbase(UT base = 10) noexcept { | |
| UT ret = base; | |
| if (base == 0) | |
| return ret; | |
| UT lim = (max_mask() >> 1); | |
| UT p10 = base; | |
| for (; p10 < lim;) { | |
| ret = p10; | |
| p10 *= base; | |
| } | |
| return ret; | |
| } | |
| static constexpr bool is_T_neg(const UT v) noexcept { | |
| UT mask = ~(max_mask() >> 1); | |
| return v & mask; | |
| } | |
| bool constexpr is_neg() const noexcept { | |
| UT mask = ~(max_mask() >> 1); | |
| if (digits.size()) | |
| return (digits.back() & mask) != 0; | |
| else | |
| return false; | |
| } | |
| static bool constexpr is_neg(const vector_t& vec) noexcept { | |
| UT mask = ~(max_mask() >> 1); | |
| if (vec.size()) | |
| return (vec.back() & mask) != 0; | |
| else | |
| return false; | |
| } | |
| static void slim(vector_t& vec) noexcept { | |
| while (vec.size() > 1 && ( | |
| (vec.back() == 0 && !is_T_neg(vec[vec.size() - 2])) || | |
| (vec.back() == max_mask() && is_T_neg(vec[vec.size() - 2])) | |
| )) | |
| vec.pop_back(); | |
| } | |
| void slim() noexcept { | |
| slim(digits); | |
| } | |
| static void padded(vector_t& vec, size_t m) noexcept { | |
| if (vec.size() < m) { | |
| if (is_neg(vec)) { | |
| vec.resize(m, max_mask()); | |
| } | |
| else { | |
| vec.resize(m, UT{ 0 }); | |
| } | |
| } | |
| } | |
| template <typename func> | |
| static vector_t bitwiseop(const vector_t& lhs, const vector_t& rhs, func cb) { | |
| const size_t mx = lhs.size() > rhs.size() ? lhs.size() : rhs.size(); | |
| const size_t mn = lhs.size() < rhs.size() ? lhs.size() : rhs.size(); | |
| vector_t ret(mx, 0); | |
| const UT pad_lhs = is_neg(lhs) ? max_mask() : 0; | |
| const UT pad_rhs = is_neg(rhs) ? max_mask() : 0; | |
| const vector_t& tmp = lhs.size() > rhs.size() ? lhs : rhs; | |
| const UT pad_tmp = lhs.size() > rhs.size() ? pad_rhs : pad_lhs; | |
| size_t i = 0; | |
| for (; i < mn; i++) { | |
| ret[i] = cb(lhs[i], rhs[i]); | |
| } | |
| for (; i < mx; i++) { | |
| ret[i] = cb(tmp[i], pad_tmp); | |
| } | |
| return ret; | |
| } | |
| wide_int& operator++() noexcept { | |
| *this += 1; | |
| return *this; | |
| } | |
| wide_int operator++(int) noexcept { | |
| wide_int ret(*this); | |
| operator++(); | |
| return ret; | |
| } | |
| wide_int& operator--() noexcept { | |
| *this -= 1; | |
| return *this; | |
| } | |
| wide_int operator--(int) noexcept { | |
| wide_int ret(*this); | |
| operator--(); | |
| return ret; | |
| } | |
| friend constexpr wide_int operator+(const wide_int &lhs, const wide_int& rhs) noexcept { | |
| return add_vec<UT, 32>(lhs.digits, rhs.digits); | |
| } | |
| friend constexpr wide_int operator&(const wide_int &lhs, const wide_int &rhs) noexcept { | |
| return bitwiseop(lhs.digits, rhs.digits, std::bit_and<UT>()); | |
| } | |
| friend constexpr wide_int operator|(const wide_int &lhs, const wide_int& rhs) noexcept { | |
| return bitwiseop(lhs.digits, rhs.digits, std::bit_or<UT>()); | |
| } | |
| friend constexpr wide_int operator^(const wide_int &lhs, const wide_int& rhs) noexcept { | |
| return bitwiseop(lhs.digits, rhs.digits, std::bit_xor<UT>()); | |
| } | |
| friend constexpr wide_int operator*(const wide_int &lhs, const wide_int& rhs) noexcept { | |
| bool nl = lhs.is_neg(); | |
| bool nr = rhs.is_neg(); | |
| bool nres = nl != nr; | |
| wide_int l = nl ? -lhs : lhs; | |
| wide_int r = nr ? -rhs : rhs; | |
| if (r > l) | |
| std::swap(l, r); | |
| const vector_t& l_digits = l.digits; | |
| const vector_t& r_digits = r.digits; | |
| vector_t product(l_digits.size() + r_digits.size() + 1, 0); | |
| //for each digit of result | |
| for (size_t i = 0; i < l_digits.size(); i++) { | |
| UT c{ 0 }; | |
| UT r{ 0 }; | |
| UT c2{ 0 }; | |
| //mult w/ carry | |
| for (size_t j = 0; j < r_digits.size(); j++) { | |
| const size_t idx = i + j; | |
| std::tie(c2, product[idx]) = add(product[idx], c); | |
| std::tie(c, r) = mult(l_digits[i], r_digits[j]); | |
| std::tie(r, product[idx]) = add(product[idx], r); | |
| c += c2 + r; | |
| } | |
| //carry through rest of result | |
| for (size_t j = r_digits.size() + i; j < product.size(); j++) { | |
| std::tie(c, product[j]) = add(c, product[j]); | |
| } | |
| } | |
| return nres ? -wide_int(product) : wide_int(product); | |
| } | |
| static constexpr size_t highest_bit(const vector_t& vec) noexcept { | |
| size_t max = vec.size() * (sizeof(UT) * 8); | |
| size_t c = 0; | |
| for (size_t i = 0; i < vec.size(); i++) { | |
| size_t t = (vec.size() - 1) - i; | |
| for (size_t b = (sizeof(UT) * 8) - 1; b < (sizeof(UT) * 8); b--) { | |
| UT mask = UT{ 1 } << b; | |
| if (vec[t] & mask) | |
| return max - c; | |
| c++; | |
| } | |
| } | |
| return max - c; | |
| } | |
| static constexpr size_t lowest_bit(const vector_t& vec) noexcept { | |
| size_t c = 0; | |
| for (size_t i = 0; i < vec.size(); i++) { | |
| for (size_t b = 0; b < (sizeof(UT) * 8); b++) { | |
| UT mask = UT{ 1 } << b; | |
| if (vec[i] & mask) | |
| return c; | |
| c++; | |
| } | |
| } | |
| return c; | |
| } | |
| static constexpr std::pair<wide_int, size_t> normalize_helper(const wide_int &lhs, const wide_int &rhs) { | |
| wide_int ret = lhs; | |
| //gets highest common power of two | |
| const size_t lhs_lb = lowest_bit(lhs.digits); | |
| const size_t rhs_lb = lowest_bit(rhs.digits); | |
| const size_t min_lb = std::min(lhs_lb, rhs_lb); | |
| return std::make_pair(ret >>= min_lb, min_lb); | |
| } | |
| static constexpr std::pair<wide_int, wide_int> division_helper(const wide_int &lhs, const wide_int &rhs) { | |
| if (rhs == 0) | |
| return std::make_pair(lhs, rhs); | |
| size_t lhs_hb = highest_bit(lhs.digits); | |
| size_t rhs_hb = highest_bit(rhs.digits); | |
| size_t offset = lhs_hb >= rhs_hb ? lhs_hb - rhs_hb : 0; | |
| wide_int d = rhs << offset; | |
| wide_int inc = 1; | |
| inc <<= offset; | |
| wide_int q = 0; | |
| wide_int r = lhs; | |
| for (size_t i = 0; i <= offset; i++) { | |
| if (d <= r) { | |
| r -= d; | |
| q += inc; | |
| r.slim(); | |
| q.slim(); | |
| } | |
| d >>= 1; | |
| inc >>= 1; | |
| } | |
| return std::make_pair(q, r); | |
| } | |
| static constexpr std::pair<wide_int, wide_int> plain_division(const wide_int &lhs, const T& rhs) { | |
| wide_int quotient = lhs; | |
| wide_int remainder = lhs; | |
| std::fill(quotient.digits.begin(), quotient.digits.end(), 0); | |
| using ST = make_smaller<T>; | |
| ST* rdata = (ST*)(remainder.digits.data()); | |
| ST* qdata = (ST*)(quotient.digits.data()); | |
| T rem = 0; | |
| /* | |
| 05|46 | |
| ..| | |
| .|. | |
| |.. | |
| size 2 | |
| -> size 4 | |
| start on (3-1) | |
| */ | |
| //3210 | |
| /* | |
| 0204_6000r0 | |
| 05)1023_0000 | |
| 10 | |
| 002 | |
| 000 | |
| 23 | |
| 20 | |
| 30 | |
| 30 | |
| 00... | |
| */ | |
| for (size_t i = (lhs.digits.size() * 2)-1; i--;) { | |
| const T v = *((T*)(&rdata[i])); | |
| const T div = v / rhs; | |
| rem = v % rhs; | |
| *((T*)(&rdata[i])) = rem; | |
| qdata[i] = div; | |
| //rdata[i] = rem; | |
| //qdata[i] = div; | |
| } | |
| std::fill(remainder.digits.begin(), remainder.digits.end(), 0); | |
| remainder.digits[0] = rem; | |
| return std::make_pair(quotient, remainder); | |
| } | |
| static constexpr std::pair<wide_int, wide_int> newton_division(const wide_int &lhs, const T &rhs) { | |
| const size_t k = (sizeof(T) * 8); //64 | |
| const size_t v = size_t{ 1 } << (k - 1); //1 << 63 | |
| const size_t m = v / rhs; | |
| /* | |
| auto nd = [&k, &v, &m](const wide_int &lhs, const wide_int& rhs) { | |
| wide_int q = (lhs * m) >> (k - 1); | |
| wide_int r = lhs - (q * rhs); | |
| return std::make_pair(q, r); | |
| }; | |
| */ | |
| wide_int q = (lhs * m) >> (k-1); | |
| wide_int r = lhs - (q * rhs); | |
| wide_int q0 = 0; | |
| //wide_int r0 = 0; | |
| int c = 0; | |
| //r > rhs | |
| while ((c = lge_helper(r, rhs)) > 0) { | |
| q0 = (r * m); | |
| q0 >>= (k - 1); | |
| r -= (q0 * rhs); | |
| r.slim(); | |
| q += q0; | |
| q.slim(); | |
| //r0 = r - (q0 * rhs); | |
| //wide_int q = (lhs * m) >> (k - 1); | |
| //wide_int r = lhs - (q * rhs); | |
| } | |
| if (c == 0) { | |
| q += 1; | |
| r = 0; | |
| } | |
| return std::make_pair(q, r); | |
| } | |
| static constexpr void _division( | |
| const wide_int& lhs, const T& rhs, vector_t &q, vector_t &q0, vector_t &r, vector_t &mul, vector_t &_buffer) { | |
| const size_t k = (sizeof(T) * 8); //64 | |
| const size_t km1 = k - 1; | |
| const size_t v = size_t{ 1 } << km1; //1 << 63 | |
| const size_t m = v / rhs; | |
| //wide_int q0 = 0; | |
| //wide_int q = lhs; | |
| //q._unsigned_mult(m, remainder_buffer); | |
| //q >>= (k - 1); | |
| q = lhs.digits; | |
| mult_vec(q, m, _buffer); | |
| q = _buffer; | |
| right_shift(q, km1); | |
| slim(q); | |
| //(lhs * m) >> (k - 1); | |
| mul = q; | |
| mult_vec(mul, rhs, _buffer); | |
| mul = _buffer; | |
| std::transform(mul.begin(), mul.end(), mul.begin(), std::bit_not<T>()); | |
| add_vec(mul, T{ 1 }, _buffer); | |
| mul = _buffer; | |
| slim(mul); | |
| r = lhs.digits; | |
| add_vec(r, mul, _buffer); | |
| r = _buffer; | |
| //uint32_t loops = 0; | |
| int c = 0; | |
| //r > rhs | |
| while ((c = lge_helper(r, rhs)) > 0) { | |
| // q = (r * m) | |
| q0 = r; | |
| mult_vec(q0, m, _buffer); | |
| q0 = _buffer; | |
| // >> (k-1) | |
| right_shift(q0, km1); | |
| mul = q0; | |
| mult_vec(mul, rhs, _buffer); | |
| mul = _buffer; | |
| std::transform(mul.begin(), mul.end(), mul.begin(), std::bit_not<T>()); | |
| add_vec(mul, T{ 1 }, _buffer); | |
| mul = _buffer; | |
| slim(mul); | |
| // r = lhs - (q * rhs); | |
| //r = r - (q0 * rhs); | |
| add_vec(r, mul, _buffer); | |
| r = _buffer; | |
| slim(r); | |
| //q += q0 | |
| add_vec(q, q0, _buffer); | |
| q = _buffer; | |
| slim(q); | |
| //loops++; | |
| } | |
| if (c == 0) { | |
| //q += 1; | |
| //q._add(T{ 1 }, _buffer); | |
| add_vec(q, T{ 1 }, _buffer); | |
| q = _buffer; | |
| r.clear(); | |
| r.push_back(0); | |
| //loops++; | |
| } | |
| slim(q); | |
| slim(r); | |
| } | |
| friend constexpr wide_int operator%(const wide_int &lhs, const wide_int& rhs) noexcept { | |
| bool nl = lhs.is_neg(); | |
| bool nr = rhs.is_neg(); | |
| const wide_int l = nl ? -lhs : lhs; | |
| const wide_int r = nr ? -rhs : rhs; | |
| auto ret = division_helper(l, r); | |
| return ret.second; | |
| } | |
| friend constexpr wide_int operator/(const wide_int& lhs, const wide_int& rhs) noexcept { | |
| bool nl = lhs.is_neg(); | |
| bool nr = rhs.is_neg(); | |
| bool nres = nl != nr; | |
| const wide_int l = nl ? -lhs : lhs; | |
| const wide_int r = nr ? -rhs : rhs; | |
| auto ret = division_helper(l, r); | |
| return nres ? -ret.first : ret.first; | |
| } | |
| friend constexpr wide_int operator>>(const wide_int& lhs, const size_t& shift) noexcept { | |
| vector_t ret = lhs.digits; | |
| const size_t bits = sizeof(UT) * 8; | |
| const size_t digit_div = shift / bits; | |
| const size_t digit_rem = shift % bits; | |
| const size_t digit_rem_inv = bits - digit_rem; | |
| const UT lmask = max_mask() << digit_rem; | |
| const UT rmask = ~lmask; | |
| if (digit_rem) { | |
| for (size_t i = 1; i < ret.size(); i++) { | |
| ret[i - 1] = (ret[i - 1] >> digit_rem) + ((ret[i] & rmask) << digit_rem_inv); | |
| } | |
| ret.back() = ret.back() >> digit_rem; | |
| } | |
| if (digit_div) { | |
| const size_t d_div = std::min(digit_div, ret.size()); | |
| for (size_t i = d_div; i < ret.size(); i++) { | |
| ret[i - d_div] = ret[i]; | |
| } | |
| for (size_t i = ret.size() - d_div; i < ret.size(); i++) { | |
| ret[i] = rmask; | |
| } | |
| } | |
| slim(ret); | |
| return ret; | |
| } | |
| friend constexpr wide_int operator<<(const wide_int &lhs, const size_t& shift) noexcept { | |
| vector_t ret = lhs.digits; | |
| const size_t bits = sizeof(UT) * 8; | |
| const size_t digit_div = shift / bits; | |
| const size_t digit_rem = shift % bits; | |
| const size_t digit_rem_inv = bits - digit_rem; | |
| const UT lmask = max_mask() << digit_rem_inv; | |
| const UT rmask = ~lmask; | |
| padded(ret, ret.size() + digit_div + (digit_rem ? 1 : 0)); | |
| if (digit_rem) { | |
| for (size_t i = 1; i < ret.size(); i++) { | |
| size_t o = ret.size() - (1 + i); | |
| ret[o + 1] = (ret[o + 1] << digit_rem) + | |
| ((ret[o] & lmask) >> digit_rem_inv); | |
| } | |
| ret.front() = ret.front() << digit_rem; | |
| } | |
| if (digit_div) { | |
| for (size_t i = digit_div; i < ret.size(); i++) { | |
| size_t o = ret.size() - (1 + i); | |
| size_t t = o + digit_div; | |
| ret[t] = ret[o]; | |
| } | |
| for (size_t i = 0; i < digit_div; i++) | |
| ret[i] = 0; | |
| } | |
| return wide_int(ret); | |
| } | |
| static constexpr vector_t& left_shift(vector_t& lhs, const size_t& shift) noexcept { | |
| //vector_t& ret = lhs; | |
| const size_t bits = sizeof(UT) * 8; | |
| const size_t digit_div = shift / bits; | |
| const size_t digit_rem = shift % bits; | |
| const size_t digit_rem_inv = bits - digit_rem; | |
| const UT lmask = max_mask() << digit_rem_inv; | |
| const UT rmask = ~lmask; | |
| padded(lhs, lhs.size() + digit_div + (digit_rem ? 1 : 0)); | |
| if (digit_rem) { | |
| for (size_t i = 1; i < lhs.size(); i++) { | |
| size_t o = lhs.size() - (1 + i); | |
| lhs[o + 1] = (lhs[o + 1] << digit_rem) + | |
| ((lhs[o] & lmask) >> digit_rem_inv); | |
| } | |
| lhs.front() = lhs.front() << digit_rem; | |
| } | |
| if (digit_div) { | |
| for (size_t i = digit_div; i < lhs.size(); i++) { | |
| size_t o = lhs.size() - (1 + i); | |
| size_t t = o + digit_div; | |
| lhs[t] = lhs[o]; | |
| } | |
| for (size_t i = 0; i < digit_div; i++) | |
| lhs[i] = 0; | |
| } | |
| return lhs; | |
| } | |
| static constexpr vector_t& right_shift(vector_t& lhs, const size_t& shift) noexcept { | |
| //vector_t& ret = lhs; | |
| const size_t bits = sizeof(UT) * 8; | |
| const size_t digit_div = shift / bits; | |
| const size_t digit_rem = shift % bits; | |
| const size_t digit_rem_inv = bits - digit_rem; | |
| const UT lmask = max_mask() << digit_rem; | |
| const UT rmask = ~lmask; | |
| if (digit_rem) { | |
| for (size_t i = 1; i < lhs.size(); i++) { | |
| lhs[i - 1] = (lhs[i - 1] >> digit_rem) + ((lhs[i] & rmask) << digit_rem_inv); | |
| } | |
| lhs.back() = lhs.back() >> digit_rem; | |
| } | |
| if (digit_div) { | |
| const size_t d_div = std::min(digit_div, lhs.size()); | |
| for (size_t i = d_div; i < lhs.size(); i++) { | |
| lhs[i - d_div] = lhs[i]; | |
| } | |
| for (size_t i = lhs.size() - d_div; i < lhs.size(); i++) { | |
| lhs[i] = rmask; | |
| } | |
| } | |
| slim(lhs); | |
| return lhs; | |
| } | |
| friend constexpr wide_int operator+(const wide_int& rhs) noexcept { | |
| return rhs; | |
| } | |
| friend constexpr wide_int operator-(const wide_int& rhs) noexcept { | |
| return (~rhs + 1); | |
| } | |
| //two's complement | |
| friend constexpr wide_int operator-(const wide_int &lhs, const wide_int &rhs) noexcept { | |
| return lhs + (~rhs + 1); | |
| } | |
| //assignements | |
| wide_int& operator+=(const wide_int& other) noexcept { | |
| *this = (*this + other); | |
| return *this; | |
| } | |
| #if 1 | |
| wide_int& _add(const wide_int& other, | |
| vector_t& result_buffer) { | |
| add_vec(digits, other.digits, result_buffer); | |
| digits = result_buffer; | |
| return *this; | |
| } | |
| wide_int& _add(const vector_t& other, | |
| vector_t& result_buffer) { | |
| add_vec(digits, other, result_buffer); | |
| digits = result_buffer; | |
| return *this; | |
| } | |
| wide_int& _add(const T& other, | |
| vector_t& result_buffer) { | |
| add_vec(digits, other, result_buffer); | |
| digits = result_buffer; | |
| return *this; | |
| } | |
| #endif | |
| //two buffers | |
| wide_int& _add(const wide_int& other, | |
| vector_t& result_buffer, vector_t& carry_buffer) { | |
| add_vec(digits, other.digits, result_buffer, carry_buffer); | |
| digits = result_buffer; | |
| return *this; | |
| } | |
| wide_int& _add(const vector_t& other, | |
| vector_t& result_buffer, vector_t& carry_buffer) { | |
| add_vec(digits, other, result_buffer, carry_buffer); | |
| digits = result_buffer; | |
| return *this; | |
| } | |
| wide_int& _add(const T& other, | |
| vector_t& result_buffer, vector_t& carry_buffer) { | |
| add_vec(digits, other.digits, result_buffer, carry_buffer); | |
| digits = result_buffer; | |
| return *this; | |
| } | |
| wide_int& _mult(const wide_int& other, vector_t& result_buffer) { | |
| const bool nl = this->is_neg(); | |
| const bool nr = other.is_neg(); | |
| const bool nres = nl != nr; | |
| *this = nl ? -*this : *this; | |
| wide_int r = nr ? -other : other; | |
| if (r > *this) | |
| std::swap(*this, r); | |
| mult_vec(digits, r.digits, result_buffer); | |
| digits = result_buffer; | |
| if (nres) | |
| *this = -*this; | |
| return *this; | |
| } | |
| #if 0 | |
| wide_int& _mult(const vector_t& other, vector_t& result_buffer) { | |
| const bool nl = this->is_neg(); | |
| const bool nr = other.is_neg(); | |
| const bool nres = nl != nr; | |
| *this = nl ? -*this : *this; | |
| wide_int r = nr ? -other : other; | |
| if (r > * this) | |
| std::swap(*this, r); | |
| mult_vec(digits, r.digits, result_buffer); | |
| digits = result_buffer; | |
| if (nres) | |
| *this = -*this; | |
| return *this; | |
| } | |
| #endif | |
| wide_int& _mult(const T& other, vector_t& result_buffer) { | |
| const bool nl = this->is_neg(); | |
| const bool nr = other & (~(~T{0} >> 1)); | |
| const bool nres = nl != nr; | |
| *this = nl ? -*this : *this; | |
| const T r = nr ? -other : other; | |
| mult_vec(digits, r, result_buffer); | |
| digits = result_buffer; | |
| if (nres) | |
| *this = -*this; | |
| return *this; | |
| } | |
| //mult | |
| wide_int& _unsigned_mult(const wide_int& other, vector_t& result_buffer) { | |
| auto c = lge_helper(other.digits, digits); | |
| if (c > 0) | |
| mult_vec(other.digits, digits, result_buffer); | |
| else | |
| mult_vec(digits, other.digits, result_buffer); | |
| digits = result_buffer; | |
| return *this; | |
| } | |
| wide_int& _unsigned_mult(const vector_t& other, vector_t& result_buffer) { | |
| auto c = lge_helper(other, digits); | |
| if (c > 0) | |
| mult_vec(other, digits, result_buffer); | |
| else | |
| mult_vec(digits, other, result_buffer); | |
| digits = result_buffer; | |
| return *this; | |
| } | |
| wide_int& _unsigned_mult(const T& other, vector_t& result_buffer) { | |
| mult_vec(digits, other, result_buffer); | |
| digits = result_buffer; | |
| return *this; | |
| } | |
| wide_int& _unsigned_div(const T& rhs, vector_t& remainder_buffer) { | |
| remainder_buffer.assign(digits.data(), digits.data() + digits.size()); | |
| std::fill(digits.begin(), digits.end(), T{ 0 }); | |
| using ST = make_smaller<T>; | |
| ST* rdata = (ST*)(remainder_buffer.data()); | |
| ST* qdata = (ST*)(digits.data()); | |
| T rem = 0; | |
| for (size_t i = (digits.size() * 2) - 1; i--;) { | |
| const T v = *((T*)(&rdata[i])); | |
| const T div = v / rhs; | |
| rem = v % rhs; | |
| *((T*)(&rdata[i])) = rem; | |
| qdata[i] = div; | |
| } | |
| slim(); | |
| return *this; | |
| } | |
| wide_int& _unsigned_mod(const T& rhs, vector_t& remainder_buffer) { | |
| remainder_buffer.assign(digits.data(), digits.data() + digits.size()); | |
| std::fill(digits.begin(), digits.end(), T{ 0 }); | |
| using ST = make_smaller<T>; | |
| ST* rdata = (ST*)(remainder_buffer.data()); | |
| ST* qdata = (ST*)(digits.data()); | |
| T rem = 0; | |
| for (size_t i = (digits.size() * 2) - 1; i--;) { | |
| const T v = *((T*)(&rdata[i])); | |
| const T div = v / rhs; | |
| rem = v % rhs; | |
| *((T*)(&rdata[i])) = rem; | |
| qdata[i] = div; | |
| } | |
| digits.clear(); | |
| digits.push_back(rem); | |
| return *this; | |
| } | |
| wide_int& _unsigned_div(const wide_int& rhs, vector_t& remainder_buffer, | |
| vector_t& inc_buffer, vector_t& digit_buffer, vector_t& neg_buffer, | |
| vector_t& add_buffer, vector_t& carry_buffer) { | |
| if (rhs == 0) | |
| return *this; | |
| const T one{ 1 }; | |
| size_t lhs_hb = highest_bit(digits); | |
| size_t rhs_hb = highest_bit(rhs.digits); | |
| size_t offset = (lhs_hb >= rhs_hb) ? lhs_hb - rhs_hb : 0; | |
| remainder_buffer.assign(digits.begin(), digits.end()); | |
| digits.assign(digits.size(), 0); | |
| digit_buffer.assign(rhs.digits.begin(), rhs.digits.end()); | |
| left_shift(digit_buffer, offset); | |
| inc_buffer.assign(offset+1, 0); | |
| inc_buffer[0] = 0x1; | |
| left_shift(inc_buffer, offset); | |
| for (size_t i = 0; i <= offset; i++) { | |
| auto c = lge_helper(digit_buffer, remainder_buffer); | |
| if (c <= 0) { | |
| neg_buffer.assign(digit_buffer.begin(), digit_buffer.end()); | |
| bitwiseop(neg_buffer.begin(), neg_buffer.end(), std::bit_not<T>()); | |
| add_vec(remainder_buffer, neg_buffer, add_buffer, carry_buffer); | |
| remainder_buffer = add_buffer; | |
| add_vec(remainder_buffer, T{ 1 }, add_buffer, carry_buffer); | |
| remainder_buffer = add_buffer; | |
| _add(inc_buffer, add_buffer, carry_buffer); | |
| } | |
| right_shift(neg_buffer, 1); | |
| right_shift(digit_buffer, 1); | |
| right_shift(inc_buffer, 1); | |
| } | |
| /* | |
| wide_int d = rhs << offset; | |
| wide_int inc = 1; | |
| inc <<= offset; | |
| wide_int q = 0; | |
| wide_int r = lhs; | |
| for (size_t i = 0; i <= offset; i++) { | |
| if (d <= r) { | |
| r -= d; | |
| q += inc; | |
| r.slim(); | |
| q.slim(); | |
| } | |
| d >>= 1; | |
| inc >>= 1; | |
| } | |
| */ | |
| return *this; | |
| } | |
| wide_int& _unsigned_mod(const wide_int& rhs, vector_t& remainder_buffer, | |
| vector_t& inc_buffer, vector_t& digit_buffer, vector_t& neg_buffer, | |
| vector_t& add_buffer, vector_t& carry_buffer) { | |
| if (rhs == 0) | |
| return *this; | |
| size_t lhs_hb = highest_bit(digits); | |
| size_t rhs_hb = highest_bit(rhs.digits); | |
| size_t offset = (lhs_hb >= rhs_hb) ? lhs_hb - rhs_hb : 0; | |
| remainder_buffer.assign(digits.begin(), digits.end()); | |
| digits.assign(digits.size(), 0); | |
| digit_buffer.assign(rhs.digits.begin(), rhs.digits.end()); | |
| left_shift(digit_buffer, offset); | |
| //left_shift(remainder_buffer, offset); | |
| inc_buffer.assign(offset + 1, 0); | |
| inc_buffer[0] = 0x1; | |
| left_shift(inc_buffer, offset); | |
| for (size_t i = 0; i <= offset; i++) { | |
| auto c = lge_helper(digit_buffer, remainder_buffer); | |
| if (c <= 0) { | |
| neg_buffer.assign(digit_buffer.begin(), digit_buffer.end()); | |
| bitwiseop(neg_buffer.begin(), neg_buffer.end(), std::bit_not<T>()); | |
| add_vec(remainder_buffer, neg_buffer, add_buffer, carry_buffer); | |
| remainder_buffer = add_buffer; | |
| add_vec(remainder_buffer, T{ 1 }, add_buffer, carry_buffer); | |
| remainder_buffer = add_buffer; | |
| //add_vec(remainder_buffer, neg_buffer, add_buffer, carry_buffer); | |
| _add(inc_buffer, add_buffer, carry_buffer); | |
| //remainder_buffer -= digit_buffer; | |
| //add_vec(remainder_buffer, digit_buffer, result_buffer, carry_buffer); | |
| } | |
| right_shift(digit_buffer, 1); | |
| right_shift(inc_buffer, 1); | |
| } | |
| digits = remainder_buffer; | |
| slim(); | |
| return *this; | |
| } | |
| wide_int& operator*=(const wide_int& other) noexcept { | |
| *this = (*this * other); | |
| return *this; | |
| } | |
| wide_int& operator-=(const wide_int& other) noexcept { | |
| *this = (*this - other); | |
| return *this; | |
| } | |
| wide_int& operator%=(const wide_int& other) noexcept { | |
| *this = (*this % other); | |
| return *this; | |
| } | |
| wide_int& operator/=(const wide_int& other) noexcept { | |
| *this = (*this / other); | |
| return *this; | |
| } | |
| wide_int& operator&=(const wide_int& other) noexcept { | |
| *this = (*this & other); | |
| return *this; | |
| } | |
| wide_int& operator|=(const wide_int& other) noexcept { | |
| *this = (*this | other); | |
| return *this; | |
| } | |
| wide_int& operator^=(const wide_int& other) noexcept { | |
| *this = (*this ^ other); | |
| return *this; | |
| } | |
| wide_int& operator>>=(const wide_int& other) noexcept { | |
| vector_t &ret = digits; | |
| const T& shift = other.digits[0]; | |
| const size_t bits = sizeof(UT) * 8; | |
| const size_t digit_div = shift / bits; | |
| const size_t digit_rem = shift % bits; | |
| const size_t digit_rem_inv = bits - digit_rem; | |
| const UT lmask = max_mask() << digit_rem; | |
| const UT rmask = ~lmask; | |
| if (digit_rem) { | |
| for (size_t i = 1; i < ret.size(); i++) { | |
| ret[i - 1] = (ret[i - 1] >> digit_rem) + ((ret[i] & rmask) << digit_rem_inv); | |
| } | |
| ret.back() = ret.back() >> digit_rem; | |
| } | |
| if (digit_div) { | |
| const size_t d_div = std::min(digit_div, ret.size()); | |
| for (size_t i = d_div; i < ret.size(); i++) { | |
| ret[i - d_div] = ret[i]; | |
| } | |
| for (size_t i = ret.size() - d_div; i < ret.size(); i++) { | |
| ret[i] = rmask; | |
| } | |
| } | |
| slim(); | |
| return *this; | |
| /* | |
| *this = (*this >> other.digits[0]); | |
| return *this; | |
| */ | |
| } | |
| wide_int& operator<<=(const wide_int& other) noexcept { | |
| vector_t& ret = digits; | |
| const T& shift = other.digits[0]; | |
| const size_t bits = sizeof(UT) * 8; | |
| const size_t digit_div = shift / bits; | |
| const size_t digit_rem = shift % bits; | |
| const size_t digit_rem_inv = bits - digit_rem; | |
| const UT lmask = max_mask() << digit_rem_inv; | |
| const UT rmask = ~lmask; | |
| padded(ret, ret.size() + digit_div + (digit_rem ? 1 : 0)); | |
| if (digit_rem) { | |
| for (size_t i = 1; i < ret.size(); i++) { | |
| size_t o = ret.size() - (1 + i); | |
| ret[o + 1] = (ret[o + 1] << digit_rem) + | |
| ((ret[o] & lmask) >> digit_rem_inv); | |
| } | |
| ret.front() = ret.front() << digit_rem; | |
| } | |
| if (digit_div) { | |
| for (size_t i = digit_div; i < ret.size(); i++) { | |
| size_t o = ret.size() - (1 + i); | |
| size_t t = o + digit_div; | |
| ret[t] = ret[o]; | |
| } | |
| for (size_t i = 0; i < digit_div; i++) | |
| ret[i] = 0; | |
| } | |
| return *this; | |
| /* | |
| *this = (*this << other.digits[0]); | |
| return *this; | |
| */ | |
| } | |
| wide_int& operator>>=(const T& shift) noexcept { | |
| vector_t& ret = digits; | |
| const size_t bits = sizeof(UT) * 8; | |
| const size_t digit_div = shift / bits; | |
| const size_t digit_rem = shift % bits; | |
| const size_t digit_rem_inv = bits - digit_rem; | |
| const UT lmask = max_mask() << digit_rem; | |
| const UT rmask = ~lmask; | |
| if (digit_rem) { | |
| for (size_t i = 1; i < ret.size(); i++) { | |
| ret[i - 1] = (ret[i - 1] >> digit_rem) + ((ret[i] & rmask) << digit_rem_inv); | |
| } | |
| ret.back() = ret.back() >> digit_rem; | |
| } | |
| if (digit_div) { | |
| const size_t d_div = std::min(digit_div, ret.size()); | |
| for (size_t i = d_div; i < ret.size(); i++) { | |
| ret[i - d_div] = ret[i]; | |
| } | |
| for (size_t i = ret.size() - d_div; i < ret.size(); i++) { | |
| ret[i] = rmask; | |
| } | |
| } | |
| slim(); | |
| return *this; | |
| /* | |
| *this = (*this >> other.digits[0]); | |
| return *this; | |
| */ | |
| } | |
| wide_int& operator<<=(const T& shift) noexcept { | |
| vector_t& ret = digits; | |
| const size_t bits = sizeof(UT) * 8; | |
| const size_t digit_div = shift / bits; | |
| const size_t digit_rem = shift % bits; | |
| const size_t digit_rem_inv = bits - digit_rem; | |
| const UT lmask = max_mask() << digit_rem_inv; | |
| const UT rmask = ~lmask; | |
| padded(ret, ret.size() + digit_div + (digit_rem ? 1 : 0)); | |
| if (digit_rem) { | |
| for (size_t i = 1; i < ret.size(); i++) { | |
| size_t o = ret.size() - (1 + i); | |
| ret[o + 1] = (ret[o + 1] << digit_rem) + | |
| ((ret[o] & lmask) >> digit_rem_inv); | |
| } | |
| ret.front() = ret.front() << digit_rem; | |
| } | |
| if (digit_div) { | |
| for (size_t i = digit_div; i < ret.size(); i++) { | |
| size_t o = ret.size() - (1 + i); | |
| size_t t = o + digit_div; | |
| ret[t] = ret[o]; | |
| } | |
| for (size_t i = 0; i < digit_div; i++) | |
| ret[i] = 0; | |
| } | |
| return *this; | |
| /* | |
| *this = (*this << other.digits[0]); | |
| return *this; | |
| */ | |
| } | |
| //copy | |
| wide_int& operator=(const vector_t& rhs) noexcept { | |
| digits = rhs; | |
| return *this; | |
| } | |
| //move | |
| wide_int& operator=(vector_t&& rhs) noexcept { | |
| if (this != &rhs) | |
| std::swap(this->digits, rhs); | |
| return *this; | |
| } | |
| //copy | |
| wide_int& operator=(const wide_int& rhs) noexcept { | |
| digits = rhs.digits; | |
| return *this; | |
| } | |
| //move | |
| wide_int& operator=(wide_int&& rhs) noexcept { | |
| if (this != &rhs) | |
| std::swap(this->digits, rhs.digits); | |
| return *this; | |
| } | |
| //copy | |
| wide_int& operator=(const UT& rhs) noexcept { | |
| digits.clear(); | |
| digits.push_back(rhs); | |
| return *this; | |
| } | |
| //move | |
| wide_int& operator=(UT&& rhs) noexcept { | |
| digits.clear(); | |
| digits.push_back(rhs); | |
| return *this; | |
| } | |
| friend constexpr wide_int operator~(const wide_int &rhs) noexcept { | |
| wide_int ret = rhs; | |
| std::transform(ret.digits.begin(), ret.digits.end(), ret.digits.begin(), std::bit_not<T>()); | |
| return ret; | |
| } | |
| constexpr static int lge_helper(const wide_int& lhs, const T& rhs) { | |
| bool nlhs = lhs.is_neg(); | |
| bool nrhs = is_T_neg(rhs); | |
| if (nlhs && !nrhs) | |
| return -1; | |
| else if (!nlhs && nrhs) | |
| return 1; | |
| size_t m = lhs.digits.size() > 1 ? lhs.digits.size() : 1; | |
| if (nlhs) { | |
| //both - | |
| const UT blank = max_mask(); | |
| for (size_t i = m - 1; i < m; i--) { | |
| const UT& lhsv = i < lhs.digits.size() ? lhs.digits[i] : blank; | |
| //const UT& rhsv = i < rhs.digits.size() ? rhs.digits[i] : blank; | |
| const UT& rhsv = i < 1 ? rhs : blank; | |
| //unsigned lt | |
| if (lhsv < rhsv) | |
| return 1; | |
| else if (lhsv > rhsv) | |
| return -1; | |
| } | |
| return 0; | |
| } | |
| else { | |
| //both + | |
| const UT blank = 0; | |
| for (size_t i = m - 1; i < m; i--) { | |
| const UT& lhsv = i < lhs.digits.size() ? lhs.digits[i] : blank; | |
| //const UT& rhsv = i < rhs.digits.size() ? rhs.digits[i] : blank; | |
| const UT& rhsv = i < 1 ? rhs : blank; | |
| if (lhsv < rhsv) | |
| return -1; | |
| else if (lhsv > rhsv) | |
| return 1; | |
| } | |
| return 0; | |
| } | |
| } | |
| constexpr static int lge_helper(const span_t& lhs, const span_t& rhs) noexcept { | |
| bool nlhs = is_neg(lhs); | |
| bool nrhs = is_neg(rhs); | |
| if (nlhs && !nrhs) | |
| return -1; | |
| else if (!nlhs && nrhs) | |
| return 1; | |
| size_t m = lhs.size() > rhs.size() ? lhs.size() : rhs.size(); | |
| if (nlhs) { | |
| //both - | |
| const UT blank = max_mask(); | |
| for (size_t i = m - 1; i < m; i--) { | |
| const UT& lhsv = i < lhs.size() ? lhs[i] : blank; | |
| const UT& rhsv = i < rhs.size() ? rhs[i] : blank; | |
| //unsigned lt | |
| if (lhsv < rhsv) | |
| return 1; | |
| else if (lhsv > rhsv) | |
| return -1; | |
| } | |
| return 0; | |
| } | |
| else { | |
| //both + | |
| const UT blank = 0; | |
| for (size_t i = m - 1; i < m; i--) { | |
| const UT& lhsv = i < lhs.size() ? lhs[i] : blank; | |
| const UT& rhsv = i < rhs.size() ? rhs[i] : blank; | |
| if (lhsv < rhsv) | |
| return -1; | |
| else if (lhsv > rhsv) | |
| return 1; | |
| } | |
| return 0; | |
| } | |
| } | |
| constexpr static int lge_helper(const vector_t& lhs, const vector_t &rhs) noexcept { | |
| bool nlhs = is_neg(lhs); | |
| bool nrhs = is_neg(rhs); | |
| if (nlhs && !nrhs) | |
| return -1; | |
| else if (!nlhs && nrhs) | |
| return 1; | |
| size_t m = lhs.size() > rhs.size() ? lhs.size() : rhs.size(); | |
| if (nlhs) { | |
| //both - | |
| const UT blank = max_mask(); | |
| for (size_t i = m - 1; i < m; i--) { | |
| const UT& lhsv = i < lhs.size() ? lhs[i] : blank; | |
| const UT& rhsv = i < rhs.size() ? rhs[i] : blank; | |
| //unsigned lt | |
| if (lhsv < rhsv) | |
| return 1; | |
| else if (lhsv > rhsv) | |
| return -1; | |
| } | |
| return 0; | |
| } | |
| else { | |
| //both + | |
| const UT blank = 0; | |
| for (size_t i = m - 1; i < m; i--) { | |
| const UT& lhsv = i < lhs.size() ? lhs[i] : blank; | |
| const UT& rhsv = i < rhs.size() ? rhs[i] : blank; | |
| if (lhsv < rhsv) | |
| return -1; | |
| else if (lhsv > rhsv) | |
| return 1; | |
| } | |
| return 0; | |
| } | |
| } | |
| constexpr static int lge_helper(const wide_int& lhs, const wide_int& rhs) noexcept { | |
| bool nlhs = lhs.is_neg(); | |
| bool nrhs = rhs.is_neg(); | |
| if (nlhs && !nrhs) | |
| return -1; | |
| else if (!nlhs && nrhs) | |
| return 1; | |
| size_t m = lhs.digits.size() > rhs.digits.size() ? lhs.digits.size() : rhs.digits.size(); | |
| if (nlhs) { | |
| //both - | |
| const UT blank = max_mask(); | |
| for (size_t i = m - 1; i < m; i--) { | |
| const UT& lhsv = i < lhs.digits.size() ? lhs.digits[i] : blank; | |
| const UT& rhsv = i < rhs.digits.size() ? rhs.digits[i] : blank; | |
| //unsigned lt | |
| if (lhsv < rhsv) | |
| return 1; | |
| else if (lhsv > rhsv) | |
| return -1; | |
| } | |
| return 0; | |
| } | |
| else { | |
| //both + | |
| const UT blank = 0; | |
| for (size_t i = m - 1; i < m; i--) { | |
| const UT& lhsv = i < lhs.digits.size() ? lhs.digits[i] : blank; | |
| const UT& rhsv = i < rhs.digits.size() ? rhs.digits[i] : blank; | |
| if (lhsv < rhsv) | |
| return -1; | |
| else if (lhsv > rhsv) | |
| return 1; | |
| } | |
| return 0; | |
| } | |
| } | |
| friend constexpr bool operator<(const wide_int& lhs, const wide_int& rhs) noexcept { | |
| return lge_helper(lhs, rhs) < 0; | |
| } | |
| friend constexpr bool operator>(const wide_int& lhs, const wide_int& rhs) noexcept { | |
| return lge_helper(lhs, rhs) > 0; | |
| } | |
| friend constexpr bool operator<=(const wide_int& lhs, const wide_int& rhs) noexcept { | |
| return lge_helper(lhs, rhs) <= 0; | |
| } | |
| friend constexpr bool operator>=(const wide_int& lhs, const wide_int& rhs) noexcept { | |
| return lge_helper(lhs, rhs) >= 0; | |
| } | |
| friend constexpr bool operator==(const wide_int& lhs, const wide_int& rhs) noexcept { | |
| return lge_helper(lhs, rhs) == 0; | |
| } | |
| friend constexpr bool operator!=(const wide_int& lhs, const wide_int& rhs) noexcept { | |
| return lge_helper(lhs, rhs) != 0; | |
| } | |
| constexpr explicit operator bool() noexcept { | |
| return *this != 0; | |
| } | |
| static constexpr wide_int pow(wide_int base, wide_int power) noexcept { | |
| wide_int ret = 1; | |
| while (power > 0) { | |
| if (power & 0x1) { | |
| ret *= base; | |
| ret.slim(); | |
| } | |
| base *= base; | |
| base.slim(); | |
| power >>= 1; | |
| } | |
| return ret; | |
| } | |
| }; | |
| template<typename T> | |
| using vector_t = std::vector<T, aligned_allocator<T, 32>>; | |
| using BigInt32 = wide_int<uint32_t>; | |
| using BigInt64 = wide_int<uint64_t>; | |
| using VecInt32 = vector_t<uint32_t>; | |
| using VecInt64 = vector_t<uint64_t>; | |
| #if INTPTR_MAX == INT64_MAX | |
| // 64-bit | |
| //using BInt = BigInt<uint64_t>; | |
| #elif INTPTR_MAX == INT32_MAX | |
| // 32-bit | |
| //using BInt = BigInt<uint32_t>; | |
| #else | |
| #error unknown pointer size or missing size macros! | |
| #endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment