Cleanup
This commit is contained in:
parent
753dfe8eef
commit
42a055d224
137
mp_int.hpp
137
mp_int.hpp
@ -1,137 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <array>
|
||||
#include <cstddef>
|
||||
#include <vector>
|
||||
|
||||
#include "mp_math.hpp"
|
||||
#include "mp_utils.hpp"
|
||||
|
||||
namespace mp
|
||||
{
|
||||
|
||||
static constexpr size_t UNLIMITED = std::numeric_limits<size_t>().max();
|
||||
|
||||
// template<class TElem, size_t MaxSize, bool SelfContained>
|
||||
// struct ElemContainerImpl
|
||||
// {
|
||||
// using type = std::vector<TElem>;
|
||||
// };
|
||||
|
||||
// template<class TElem, size_t MaxSize>
|
||||
// struct ElemContainerImpl<TElem, MaxSize, true>
|
||||
// {
|
||||
// using type = std::array<TElem, MaxSize>;
|
||||
// };
|
||||
|
||||
// template<class TElem, size_t MaxSize, bool SelfContained>
|
||||
// using ElemContainer = typename ElemContainerImpl<TElem, MaxSize, SelfContained>::type;
|
||||
|
||||
template <class TElem, size_t MaxSize> class BasicInt;
|
||||
|
||||
template <class TA, class TB,
|
||||
typename = typename std::enable_if<std::is_same_v<typename TA::ElementType, typename TB::ElementType>>>
|
||||
using IntOpResultType = BasicInt<typename TA::ElementType, std::max(TA::MAX_SIZE, TB::MAX_SIZE)>;
|
||||
|
||||
template <class TElem, size_t MaxSize> class BasicInt
|
||||
{
|
||||
public:
|
||||
using ElementType = TElem;
|
||||
static constexpr size_t MAX_SIZE = MaxSize;
|
||||
|
||||
using ThisType = BasicInt<TElem, MaxSize>;
|
||||
|
||||
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<ElementType>(MAX_SIZE % ELEMENT_BYTES);
|
||||
static constexpr ElementType ELEMENT_MAX = std::numeric_limits<ElementType>().max();
|
||||
|
||||
// static constexpr size_t LAST_ELEM_MASK = GetNBytesMask(3);
|
||||
|
||||
BasicInt(std::initializer_list<ElementType> init) : m_elems{init}
|
||||
{
|
||||
}
|
||||
|
||||
// template <class... TArgs> 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 <class TOp, class TR, class TRhs> void BinaryOp(TR& r, const TRhs& rhs) const
|
||||
{
|
||||
TOp::Op(r, *this, rhs);
|
||||
}
|
||||
|
||||
template <class TOp, class TRhs> IntOpResultType<ThisType, TRhs> ExtBinaryOp(const TRhs& rhs) const
|
||||
{
|
||||
IntOpResultType<ThisType, TRhs> result{};
|
||||
BinaryOp<TOp>(result, rhs);
|
||||
return result;
|
||||
}
|
||||
|
||||
template <class TOp, class TRhs> ThisType& CompoundBinaryOp(const TRhs& rhs)
|
||||
{
|
||||
BinaryOp<TOp>(*this, rhs);
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <class TRhs> auto operator+(const TRhs& rhs) const
|
||||
{
|
||||
return ExtBinaryOp<Addition>(rhs);
|
||||
}
|
||||
|
||||
template <class TRhs> auto operator+=(const TRhs& rhs)
|
||||
{
|
||||
return CompoundBinaryOp<Addition>(rhs);
|
||||
}
|
||||
|
||||
template <class TRhs> auto operator-(const TRhs& rhs) const
|
||||
{
|
||||
return ExtBinaryOp<Subtraction>(rhs);
|
||||
}
|
||||
|
||||
template <class TRhs> auto& operator-=(const TRhs& rhs)
|
||||
{
|
||||
return CompoundBinaryOp<Subtraction>(rhs);
|
||||
}
|
||||
|
||||
private:
|
||||
std::vector<ElementType> m_elems;
|
||||
};
|
||||
|
||||
// using UnlimitedInt = Int<UNLIMITED>;
|
||||
using DefaultElemType = size_t;
|
||||
|
||||
|
||||
template <size_t MaxSize> using Int = BasicInt<DefaultElemType, MaxSize>;
|
||||
using UnlimitedInt = Int<UNLIMITED>;
|
||||
|
||||
} // namespace mp
|
||||
28
mp_lib.hpp
28
mp_lib.hpp
@ -1,28 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include "mp_utils.hpp"
|
||||
|
||||
namespace mp
|
||||
{
|
||||
|
||||
template <class T>
|
||||
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
|
||||
73
mp_math.hpp
73
mp_math.hpp
@ -1,73 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <stdexcept>
|
||||
|
||||
namespace mp
|
||||
{
|
||||
|
||||
struct Addition
|
||||
{
|
||||
template <class TR, class TLhs, class TRhs> static void Op(TR& r, const TLhs& lhs, const TRhs& rhs)
|
||||
{
|
||||
static_assert(std::is_same_v<typename TR::ElementType, typename TLhs::ElementType>);
|
||||
static_assert(std::is_same_v<typename TR::ElementType, typename TRhs::ElementType>);
|
||||
|
||||
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 <class TR, class TLhs, class TRhs> static void Op(TR& r, const TLhs& lhs, const TRhs& rhs)
|
||||
{
|
||||
static_assert(std::is_same_v<typename TR::ElementType, typename TLhs::ElementType>);
|
||||
static_assert(std::is_same_v<typename TR::ElementType, typename TRhs::ElementType>);
|
||||
|
||||
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
|
||||
33
mp_utils.hpp
33
mp_utils.hpp
@ -1,33 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
|
||||
namespace mp::utils
|
||||
{
|
||||
|
||||
template <class T>
|
||||
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
|
||||
Loading…
x
Reference in New Issue
Block a user