From 42a055d224ae3e7593be1acac67fd263219be4ae Mon Sep 17 00:00:00 2001 From: tovjemam Date: Wed, 26 Nov 2025 16:38:42 +0100 Subject: [PATCH] Cleanup --- mp.hpp | 4 -- mp_int.hpp | 137 --------------------------------------------------- mp_lib.hpp | 28 ----------- mp_math.hpp | 73 --------------------------- mp_utils.hpp | 33 ------------- 5 files changed, 275 deletions(-) delete mode 100644 mp.hpp delete mode 100644 mp_int.hpp delete mode 100644 mp_lib.hpp delete mode 100644 mp_math.hpp delete mode 100644 mp_utils.hpp diff --git a/mp.hpp b/mp.hpp deleted file mode 100644 index c0c1545..0000000 --- a/mp.hpp +++ /dev/null @@ -1,4 +0,0 @@ -#pragma once - -#include "mp_int.hpp" -#include "mp_lib.hpp" diff --git a/mp_int.hpp b/mp_int.hpp deleted file mode 100644 index 0e7b23c..0000000 --- a/mp_int.hpp +++ /dev/null @@ -1,137 +0,0 @@ -#pragma once - -#include -#include -#include - -#include "mp_math.hpp" -#include "mp_utils.hpp" - -namespace mp -{ - -static constexpr size_t UNLIMITED = std::numeric_limits().max(); - -// template -// struct ElemContainerImpl -// { -// using type = std::vector; -// }; - -// template -// struct ElemContainerImpl -// { -// using type = std::array; -// }; - -// template -// using ElemContainer = typename ElemContainerImpl::type; - -template class BasicInt; - -template >> -using IntOpResultType = BasicInt; - -template class BasicInt -{ - public: - using ElementType = TElem; - static constexpr size_t MAX_SIZE = MaxSize; - - using ThisType = BasicInt; - - static constexpr size_t ELEMENT_BYTES = sizeof(ElementType); - static constexpr size_t MAX_ELEMS = utils::AlignUp(MaxSize, ELEMENT_BYTES) / ELEMENT_BYTES; - static constexpr ElementType LAST_ELEM_MASK = utils::GetNBytesMask(MAX_SIZE % ELEMENT_BYTES); - static constexpr ElementType ELEMENT_MAX = std::numeric_limits().max(); - - // static constexpr size_t LAST_ELEM_MASK = GetNBytesMask(3); - - BasicInt(std::initializer_list init) : m_elems{init} - { - } - - // template Int(TArgs&&... args) - // { - // (m_value.push_back(args), ...); - // } - - size_t GetNumElements() const - { - return m_elems.size(); - } - - TElem GetElement(size_t index) const - { - if (index >= m_elems.size()) - return 0; - - return m_elems[index]; - } - - void SetElement(size_t index, TElem value) - { - if (index >= m_elems.size()) - { - if (value == 0) - { - return; - } - - m_elems.resize(index + 1, 0); - } - - m_elems[index] = value; - } - - template void BinaryOp(TR& r, const TRhs& rhs) const - { - TOp::Op(r, *this, rhs); - } - - template IntOpResultType ExtBinaryOp(const TRhs& rhs) const - { - IntOpResultType result{}; - BinaryOp(result, rhs); - return result; - } - - template ThisType& CompoundBinaryOp(const TRhs& rhs) - { - BinaryOp(*this, rhs); - return *this; - } - - template auto operator+(const TRhs& rhs) const - { - return ExtBinaryOp(rhs); - } - - template auto operator+=(const TRhs& rhs) - { - return CompoundBinaryOp(rhs); - } - - template auto operator-(const TRhs& rhs) const - { - return ExtBinaryOp(rhs); - } - - template auto& operator-=(const TRhs& rhs) - { - return CompoundBinaryOp(rhs); - } - - private: - std::vector m_elems; -}; - -// using UnlimitedInt = Int; -using DefaultElemType = size_t; - - -template using Int = BasicInt; -using UnlimitedInt = Int; - -} // namespace mp diff --git a/mp_lib.hpp b/mp_lib.hpp deleted file mode 100644 index 0140085..0000000 --- a/mp_lib.hpp +++ /dev/null @@ -1,28 +0,0 @@ -#pragma once - -#include -#include "mp_utils.hpp" - -namespace mp -{ - -template -inline std::string ToHexString(const T& number) -{ - constexpr size_t ELEMENT_DIGITS = T::ELEMENT_BYTES * 2; - - std::string str(number.GetNumElements() * ELEMENT_DIGITS, '-'); - - for (size_t elem = 0; elem < number.GetNumElements(); ++elem) - { - auto v = number.GetElement(elem); - for (size_t digit = 0; digit < ELEMENT_DIGITS; ++digit) - { - str[str.size() - 1 - (elem * ELEMENT_DIGITS) - digit] = utils::HexDigit((v >> (digit * 4)) & 0xF); - } - } - - return str; -} - -} // namespace mp \ No newline at end of file diff --git a/mp_math.hpp b/mp_math.hpp deleted file mode 100644 index 938d9ce..0000000 --- a/mp_math.hpp +++ /dev/null @@ -1,73 +0,0 @@ -#pragma once - -#include -#include -#include -#include - -namespace mp -{ - -struct Addition -{ - template static void Op(TR& r, const TLhs& lhs, const TRhs& rhs) - { - static_assert(std::is_same_v); - static_assert(std::is_same_v); - - using ElementType = TR::ElementType; - - ElementType carry = 0; - size_t end = std::max(lhs.GetNumElements(), rhs.GetNumElements()); - for (size_t i = 0; i < end; ++i) - { - ElementType a = lhs.GetElement(i); - ElementType b = rhs.GetElement(i); - - ElementType c = carry + a; - carry = (c < a) ? 1 : 0; - c += b; - carry |= (c < b) ? 1 : 0; - - r.SetElement(i, c); - - // carry = (((a + carry) > (ELEMENT_MAX - b)) || (a > (ELEMENT_MAX - carry))) ? 1 : 0; - } - - r.SetElement(end, carry); - } -}; - -struct Subtraction -{ - template static void Op(TR& r, const TLhs& lhs, const TRhs& rhs) - { - static_assert(std::is_same_v); - static_assert(std::is_same_v); - - using ElementType = TR::ElementType; - - ElementType borrow = 0; - size_t end = std::max(lhs.GetNumElements(), rhs.GetNumElements()); - for (size_t i = 0; i < end; ++i) - { - ElementType a = lhs.GetElement(i); - ElementType b = rhs.GetElement(i); - - ElementType c = a - b - borrow; - - borrow = (a < b || (a - b) < borrow) ? 1 : 0; - - r.SetElement(i, c); - } - - if (borrow > 0) - { - throw std::runtime_error("Subtraction result negative"); - } - - // r.SetElement(end, borrow); - } -}; - -} // namespace mp \ No newline at end of file diff --git a/mp_utils.hpp b/mp_utils.hpp deleted file mode 100644 index 2b78455..0000000 --- a/mp_utils.hpp +++ /dev/null @@ -1,33 +0,0 @@ -#pragma once - -#include -#include - -namespace mp::utils -{ - -template -inline constexpr T GetNBytesMask(size_t bytes) -{ - T res = 0; - - for (size_t i = 0; i < bytes; ++i) - { - res <<= 8; - res |= 0xFF; - } - - return res; -} - -inline char HexDigit(uint8_t bits) -{ - return bits > 9 ? 'a' + (bits - 10) : '0' + bits; -} - -inline constexpr size_t AlignUp(size_t value, size_t alignment) -{ - return (value + alignment - 1) / alignment * alignment; -} - -} // namespace mp::utils