Format
This commit is contained in:
parent
f3df308366
commit
e6a6de048d
@ -4,5 +4,6 @@ PointerAlignment: Left
|
|||||||
IndentWidth: 4 # spaces per indent level
|
IndentWidth: 4 # spaces per indent level
|
||||||
TabWidth: 4 # width of a tab character
|
TabWidth: 4 # width of a tab character
|
||||||
UseTab: Never # options: Never, ForIndentation, Alwayss
|
UseTab: Never # options: Never, ForIndentation, Alwayss
|
||||||
|
AccessModifierOffset: -4
|
||||||
BreakTemplateDeclarations: Yes
|
BreakTemplateDeclarations: Yes
|
||||||
AllowShortFunctionsOnASingleLine: Inline
|
AllowShortFunctionsOnASingleLine: Inline
|
||||||
|
|||||||
@ -2,8 +2,8 @@
|
|||||||
|
|
||||||
#include <limits>
|
#include <limits>
|
||||||
|
|
||||||
#include "utils.hpp"
|
|
||||||
#include "storage.hpp"
|
#include "storage.hpp"
|
||||||
|
#include "utils.hpp"
|
||||||
|
|
||||||
namespace mp
|
namespace mp
|
||||||
{
|
{
|
||||||
@ -37,7 +37,7 @@ inline void parse_string(const char* str, T& number)
|
|||||||
template <ElementSuitable TElem, size_t MaxBytes>
|
template <ElementSuitable TElem, size_t MaxBytes>
|
||||||
class BasicInt
|
class BasicInt
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
using ElementType = TElem;
|
using ElementType = TElem;
|
||||||
constexpr static size_t MAX_BYTES = MaxBytes;
|
constexpr static size_t MAX_BYTES = MaxBytes;
|
||||||
constexpr static size_t ELEMENT_BYTES = sizeof(TElem);
|
constexpr static size_t ELEMENT_BYTES = sizeof(TElem);
|
||||||
@ -71,10 +71,7 @@ class BasicInt
|
|||||||
*this = value;
|
*this = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
BasicInt(const char* str)
|
BasicInt(const char* str) { *this = str; }
|
||||||
{
|
|
||||||
*this = str;
|
|
||||||
}
|
|
||||||
|
|
||||||
template <AnyMpInt T>
|
template <AnyMpInt T>
|
||||||
requires ElementTypesMatch<T, BasicInt>
|
requires ElementTypesMatch<T, BasicInt>
|
||||||
@ -98,7 +95,7 @@ class BasicInt
|
|||||||
{
|
{
|
||||||
using UnsignedType = std::make_unsigned_t<T>;
|
using UnsignedType = std::make_unsigned_t<T>;
|
||||||
UnsignedType uvalue;
|
UnsignedType uvalue;
|
||||||
|
|
||||||
if (std::is_signed_v<T> && value < 0)
|
if (std::is_signed_v<T> && value < 0)
|
||||||
{
|
{
|
||||||
uvalue = static_cast<UnsignedType>(-value);
|
uvalue = static_cast<UnsignedType>(-value);
|
||||||
@ -109,7 +106,7 @@ class BasicInt
|
|||||||
uvalue = static_cast<UnsignedType>(value);
|
uvalue = static_cast<UnsignedType>(value);
|
||||||
set_negative(false);
|
set_negative(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
zero();
|
zero();
|
||||||
|
|
||||||
if constexpr (sizeof(UnsignedType) <= sizeof(TElem))
|
if constexpr (sizeof(UnsignedType) <= sizeof(TElem))
|
||||||
@ -137,7 +134,7 @@ class BasicInt
|
|||||||
parse_string(str, *this);
|
parse_string(str, *this);
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
BasicInt operator-() const
|
BasicInt operator-() const
|
||||||
{
|
{
|
||||||
BasicInt res = *this;
|
BasicInt res = *this;
|
||||||
@ -148,10 +145,7 @@ class BasicInt
|
|||||||
TElem& operator[](size_t index) { return m_data[index]; }
|
TElem& operator[](size_t index) { return m_data[index]; }
|
||||||
const TElem& operator[](size_t index) const { return m_data[index]; }
|
const TElem& operator[](size_t index) const { return m_data[index]; }
|
||||||
|
|
||||||
void resize(size_t new_size)
|
void resize(size_t new_size) { m_data.resize(new_size); }
|
||||||
{
|
|
||||||
m_data.resize(new_size);
|
|
||||||
}
|
|
||||||
|
|
||||||
void zero() { m_data.clear(); }
|
void zero() { m_data.clear(); }
|
||||||
|
|
||||||
@ -211,19 +205,19 @@ class BasicInt
|
|||||||
m_data.resize(new_size);
|
m_data.resize(new_size);
|
||||||
}
|
}
|
||||||
|
|
||||||
//std::span<TElem> data() { return m_data; }
|
// std::span<TElem> data() { return m_data; }
|
||||||
|
|
||||||
size_t size_elems() const { return m_data.size(); }
|
size_t size_elems() const { return m_data.size(); }
|
||||||
|
|
||||||
bool negative() const { return m_negative; }
|
bool negative() const { return m_negative; }
|
||||||
void set_negative(bool neg) { m_negative = neg; }
|
void set_negative(bool neg) { m_negative = neg; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Container<TElem, MAX_ELEMS> m_data;
|
Container<TElem, MAX_ELEMS> m_data;
|
||||||
bool m_negative = false;
|
bool m_negative = false;
|
||||||
};
|
};
|
||||||
|
|
||||||
template<size_t MaxBytes>
|
template <size_t MaxBytes>
|
||||||
using Int = BasicInt<LongestElementSuitableType, MaxBytes>;
|
using Int = BasicInt<LongestElementSuitableType, MaxBytes>;
|
||||||
|
|
||||||
constexpr size_t UNLIMITED = std::numeric_limits<size_t>::max();
|
constexpr size_t UNLIMITED = std::numeric_limits<size_t>::max();
|
||||||
|
|||||||
@ -1,11 +1,11 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <string>
|
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
#include "utils.hpp"
|
|
||||||
#include "int.hpp"
|
#include "int.hpp"
|
||||||
|
#include "utils.hpp"
|
||||||
|
|
||||||
namespace mp
|
namespace mp
|
||||||
{
|
{
|
||||||
@ -38,7 +38,6 @@ inline std::string to_hex_string(const T& number)
|
|||||||
}
|
}
|
||||||
|
|
||||||
return "0x0";
|
return "0x0";
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template <AnyMpInt T>
|
template <AnyMpInt T>
|
||||||
@ -47,7 +46,7 @@ inline typename T::ElementType div_mod(T& value, const typename T::ElementType d
|
|||||||
using ElementType = typename T::ElementType;
|
using ElementType = typename T::ElementType;
|
||||||
|
|
||||||
ElementType remainder = 0;
|
ElementType remainder = 0;
|
||||||
for (size_t i = value.size_elems(); i-- > 0; )
|
for (size_t i = value.size_elems(); i-- > 0;)
|
||||||
{
|
{
|
||||||
using LongerType = DoubleWidthType<ElementType>;
|
using LongerType = DoubleWidthType<ElementType>;
|
||||||
|
|
||||||
@ -64,7 +63,8 @@ inline std::string to_string(const T& number)
|
|||||||
{
|
{
|
||||||
std::string str;
|
std::string str;
|
||||||
T temp = number;
|
T temp = number;
|
||||||
do {
|
do
|
||||||
|
{
|
||||||
auto rem = div_mod(temp, 10U);
|
auto rem = div_mod(temp, 10U);
|
||||||
str.push_back(static_cast<char>('0' + rem));
|
str.push_back(static_cast<char>('0' + rem));
|
||||||
} while (temp != 0U);
|
} while (temp != 0U);
|
||||||
@ -109,7 +109,6 @@ inline T factorial(const T& n)
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// template <AnyInt T>
|
// template <AnyInt T>
|
||||||
// inline T parse_string(const char* str)
|
// inline T parse_string(const char* str)
|
||||||
// {
|
// {
|
||||||
@ -118,4 +117,4 @@ inline T factorial(const T& n)
|
|||||||
// return number;
|
// return number;
|
||||||
// }
|
// }
|
||||||
|
|
||||||
}
|
} // namespace mp
|
||||||
@ -23,7 +23,7 @@ struct OpResult<TLhs, TRhs>
|
|||||||
template <AnyMpInt TLhs, AnyRegularInt TRhs>
|
template <AnyMpInt TLhs, AnyRegularInt TRhs>
|
||||||
struct OpResult<TLhs, TRhs>
|
struct OpResult<TLhs, TRhs>
|
||||||
{
|
{
|
||||||
//using type = BasicInt<typename TLhs::ElementType, ResultMaxSize<TLhs::MAX_BYTES, sizeof(TRhs)>>;
|
// using type = BasicInt<typename TLhs::ElementType, ResultMaxSize<TLhs::MAX_BYTES, sizeof(TRhs)>>;
|
||||||
using type = TLhs; // keep the same size as TLhs when operating with regular ints
|
using type = TLhs; // keep the same size as TLhs when operating with regular ints
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -33,12 +33,12 @@ using OpResultType = typename OpResult<TLhs, TRhs>::type;
|
|||||||
template <AnyMpInt T>
|
template <AnyMpInt T>
|
||||||
class OverflowError : public std::runtime_error
|
class OverflowError : public std::runtime_error
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
OverflowError(T&& value) : std::runtime_error("Overflow"), m_value(std::move(value)) {}
|
OverflowError(T&& value) : std::runtime_error("Overflow"), m_value(std::move(value)) {}
|
||||||
|
|
||||||
const T& value() const { return m_value; }
|
const T& value() const { return m_value; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
T m_value;
|
T m_value;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -136,7 +136,7 @@ inline bool less_ignore_sign(const TLhs& lhs, const TRhs& rhs)
|
|||||||
{
|
{
|
||||||
size_t max_size = std::max(lhs.size_elems(), rhs.size_elems());
|
size_t max_size = std::max(lhs.size_elems(), rhs.size_elems());
|
||||||
|
|
||||||
for (size_t i = max_size; i-- > 0; )
|
for (size_t i = max_size; i-- > 0;)
|
||||||
{
|
{
|
||||||
auto l = lhs.get(i);
|
auto l = lhs.get(i);
|
||||||
auto r = rhs.get(i);
|
auto r = rhs.get(i);
|
||||||
@ -279,8 +279,6 @@ struct Subtraction
|
|||||||
add_ignore_sign(lhs, rhs, res);
|
add_ignore_sign(lhs, rhs, res);
|
||||||
res.set_negative(false);
|
res.set_negative(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -400,9 +398,9 @@ struct Division
|
|||||||
for (int j = static_cast<int>(m); j >= 0; --j)
|
for (int j = static_cast<int>(m); j >= 0; --j)
|
||||||
{
|
{
|
||||||
// u[j + n] might be zero or >0
|
// u[j + n] might be zero or >0
|
||||||
const LongerType uj_n = static_cast<LongerType>(u.get(j + n));
|
const LongerType uj_n = static_cast<LongerType>(u.get(j + n));
|
||||||
const LongerType uj_n1 = static_cast<LongerType>(u.get(j + n - 1));
|
const LongerType uj_n1 = static_cast<LongerType>(u.get(j + n - 1));
|
||||||
const LongerType vn_1 = static_cast<LongerType>(v.get(n - 1));
|
const LongerType vn_1 = static_cast<LongerType>(v.get(n - 1));
|
||||||
|
|
||||||
// Estimate q_hat = (u[j+n]*BASE + u[j+n-1]) / v[n-1]
|
// Estimate q_hat = (u[j+n]*BASE + u[j+n-1]) / v[n-1]
|
||||||
LongerType numerator = (uj_n * BASE) + uj_n1;
|
LongerType numerator = (uj_n * BASE) + uj_n1;
|
||||||
@ -462,7 +460,8 @@ struct Division
|
|||||||
LongerType carry_add = 0;
|
LongerType carry_add = 0;
|
||||||
for (size_t i = 0; i < n; ++i)
|
for (size_t i = 0; i < n; ++i)
|
||||||
{
|
{
|
||||||
LongerType sum = static_cast<LongerType>(u.get(j + i)) + static_cast<LongerType>(v.get(i)) + carry_add;
|
LongerType sum =
|
||||||
|
static_cast<LongerType>(u.get(j + i)) + static_cast<LongerType>(v.get(i)) + carry_add;
|
||||||
u.set(j + i, static_cast<ElementType>(sum & MASK));
|
u.set(j + i, static_cast<ElementType>(sum & MASK));
|
||||||
carry_add = sum >> W;
|
carry_add = sum >> W;
|
||||||
}
|
}
|
||||||
@ -571,5 +570,4 @@ TLhs& operator/=(TLhs& lhs, const TRhs& rhs)
|
|||||||
return ca_binary_op<Division>(lhs, rhs);
|
return ca_binary_op<Division>(lhs, rhs);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
} // namespace mp
|
} // namespace mp
|
||||||
@ -1,5 +1,5 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "int.hpp"
|
#include "int.hpp"
|
||||||
#include "math.hpp"
|
|
||||||
#include "lib.hpp"
|
#include "lib.hpp"
|
||||||
|
#include "math.hpp"
|
||||||
|
|||||||
@ -11,7 +11,7 @@ namespace mp
|
|||||||
template <typename TElem, size_t MaxSize>
|
template <typename TElem, size_t MaxSize>
|
||||||
class ArrayContainer
|
class ArrayContainer
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
ArrayContainer() = default;
|
ArrayContainer() = default;
|
||||||
|
|
||||||
TElem& operator[](size_t idx) { return m_data[idx]; }
|
TElem& operator[](size_t idx) { return m_data[idx]; }
|
||||||
@ -31,7 +31,7 @@ class ArrayContainer
|
|||||||
m_size = new_size;
|
m_size = new_size;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
size_t m_size = 0;
|
size_t m_size = 0;
|
||||||
std::array<TElem, MaxSize> m_data;
|
std::array<TElem, MaxSize> m_data;
|
||||||
};
|
};
|
||||||
@ -39,7 +39,7 @@ class ArrayContainer
|
|||||||
template <typename TElem, size_t MaxSize>
|
template <typename TElem, size_t MaxSize>
|
||||||
class VectorContainer
|
class VectorContainer
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
VectorContainer() = default;
|
VectorContainer() = default;
|
||||||
|
|
||||||
TElem& operator[](size_t idx) { return m_data[idx]; }
|
TElem& operator[](size_t idx) { return m_data[idx]; }
|
||||||
@ -56,7 +56,7 @@ class VectorContainer
|
|||||||
m_data.resize(new_size, TElem{0});
|
m_data.resize(new_size, TElem{0});
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::vector<TElem> m_data;
|
std::vector<TElem> m_data;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -64,7 +64,7 @@ class VectorContainer
|
|||||||
constexpr size_t MAX_ARRAY_BYTES = 128;
|
constexpr size_t MAX_ARRAY_BYTES = 128;
|
||||||
|
|
||||||
template <typename TElem, size_t MaxSize>
|
template <typename TElem, size_t MaxSize>
|
||||||
using Container =
|
using Container = std::conditional_t<(MaxSize > MAX_ARRAY_BYTES / sizeof(TElem)), VectorContainer<TElem, MaxSize>,
|
||||||
std::conditional_t<(MaxSize > MAX_ARRAY_BYTES / sizeof(TElem)), VectorContainer<TElem, MaxSize>, ArrayContainer<TElem, MaxSize>>;
|
ArrayContainer<TElem, MaxSize>>;
|
||||||
|
|
||||||
} // namespace mp
|
} // namespace mp
|
||||||
@ -137,4 +137,4 @@ constexpr TElem calculate_last_elem_mask()
|
|||||||
return mask;
|
return mask;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
} // namespace mp
|
||||||
Loading…
x
Reference in New Issue
Block a user