diff --git a/.clang-format b/.clang-format index 0f3b668..ea1fc60 100644 --- a/.clang-format +++ b/.clang-format @@ -4,5 +4,6 @@ PointerAlignment: Left IndentWidth: 4 # spaces per indent level TabWidth: 4 # width of a tab character UseTab: Never # options: Never, ForIndentation, Alwayss +AccessModifierOffset: -4 BreakTemplateDeclarations: Yes AllowShortFunctionsOnASingleLine: Inline diff --git a/src/mp/int.hpp b/src/mp/int.hpp index ecdecd2..662782a 100644 --- a/src/mp/int.hpp +++ b/src/mp/int.hpp @@ -2,8 +2,8 @@ #include -#include "utils.hpp" #include "storage.hpp" +#include "utils.hpp" namespace mp { @@ -37,7 +37,7 @@ inline void parse_string(const char* str, T& number) template class BasicInt { - public: +public: using ElementType = TElem; constexpr static size_t MAX_BYTES = MaxBytes; constexpr static size_t ELEMENT_BYTES = sizeof(TElem); @@ -71,10 +71,7 @@ class BasicInt *this = value; } - BasicInt(const char* str) - { - *this = str; - } + BasicInt(const char* str) { *this = str; } template requires ElementTypesMatch @@ -98,7 +95,7 @@ class BasicInt { using UnsignedType = std::make_unsigned_t; UnsignedType uvalue; - + if (std::is_signed_v && value < 0) { uvalue = static_cast(-value); @@ -109,7 +106,7 @@ class BasicInt uvalue = static_cast(value); set_negative(false); } - + zero(); if constexpr (sizeof(UnsignedType) <= sizeof(TElem)) @@ -137,7 +134,7 @@ class BasicInt parse_string(str, *this); return *this; } - + BasicInt operator-() const { BasicInt res = *this; @@ -148,10 +145,7 @@ class BasicInt TElem& operator[](size_t index) { return m_data[index]; } const TElem& operator[](size_t index) const { return m_data[index]; } - void resize(size_t new_size) - { - m_data.resize(new_size); - } + void resize(size_t new_size) { m_data.resize(new_size); } void zero() { m_data.clear(); } @@ -211,19 +205,19 @@ class BasicInt m_data.resize(new_size); } - //std::span data() { return m_data; } + // std::span data() { return m_data; } size_t size_elems() const { return m_data.size(); } bool negative() const { return m_negative; } void set_negative(bool neg) { m_negative = neg; } - private: +private: Container m_data; bool m_negative = false; }; -template +template using Int = BasicInt; constexpr size_t UNLIMITED = std::numeric_limits::max(); diff --git a/src/mp/lib.hpp b/src/mp/lib.hpp index f34b419..cbb0c89 100644 --- a/src/mp/lib.hpp +++ b/src/mp/lib.hpp @@ -1,11 +1,11 @@ #pragma once #include -#include #include +#include -#include "utils.hpp" #include "int.hpp" +#include "utils.hpp" namespace mp { @@ -38,7 +38,6 @@ inline std::string to_hex_string(const T& number) } return "0x0"; - } template @@ -47,7 +46,7 @@ inline typename T::ElementType div_mod(T& value, const typename T::ElementType d using ElementType = typename T::ElementType; ElementType remainder = 0; - for (size_t i = value.size_elems(); i-- > 0; ) + for (size_t i = value.size_elems(); i-- > 0;) { using LongerType = DoubleWidthType; @@ -64,7 +63,8 @@ inline std::string to_string(const T& number) { std::string str; T temp = number; - do { + do + { auto rem = div_mod(temp, 10U); str.push_back(static_cast('0' + rem)); } while (temp != 0U); @@ -109,7 +109,6 @@ inline T factorial(const T& n) return result; } - // template // inline T parse_string(const char* str) // { @@ -118,4 +117,4 @@ inline T factorial(const T& n) // return number; // } -} \ No newline at end of file +} // namespace mp \ No newline at end of file diff --git a/src/mp/math.hpp b/src/mp/math.hpp index 8bdce22..f481048 100644 --- a/src/mp/math.hpp +++ b/src/mp/math.hpp @@ -23,7 +23,7 @@ struct OpResult template struct OpResult { - //using type = BasicInt>; + // using type = BasicInt>; using type = TLhs; // keep the same size as TLhs when operating with regular ints }; @@ -33,12 +33,12 @@ using OpResultType = typename OpResult::type; template class OverflowError : public std::runtime_error { - public: +public: OverflowError(T&& value) : std::runtime_error("Overflow"), m_value(std::move(value)) {} const T& value() const { return m_value; } - private: +private: 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()); - for (size_t i = max_size; i-- > 0; ) + for (size_t i = max_size; i-- > 0;) { auto l = lhs.get(i); auto r = rhs.get(i); @@ -279,8 +279,6 @@ struct Subtraction add_ignore_sign(lhs, rhs, res); res.set_negative(false); } - - } }; @@ -400,9 +398,9 @@ struct Division for (int j = static_cast(m); j >= 0; --j) { // u[j + n] might be zero or >0 - const LongerType uj_n = static_cast(u.get(j + n)); - const LongerType uj_n1 = static_cast(u.get(j + n - 1)); - const LongerType vn_1 = static_cast(v.get(n - 1)); + const LongerType uj_n = static_cast(u.get(j + n)); + const LongerType uj_n1 = static_cast(u.get(j + n - 1)); + const LongerType vn_1 = static_cast(v.get(n - 1)); // Estimate q_hat = (u[j+n]*BASE + u[j+n-1]) / v[n-1] LongerType numerator = (uj_n * BASE) + uj_n1; @@ -462,7 +460,8 @@ struct Division LongerType carry_add = 0; for (size_t i = 0; i < n; ++i) { - LongerType sum = static_cast(u.get(j + i)) + static_cast(v.get(i)) + carry_add; + LongerType sum = + static_cast(u.get(j + i)) + static_cast(v.get(i)) + carry_add; u.set(j + i, static_cast(sum & MASK)); carry_add = sum >> W; } @@ -571,5 +570,4 @@ TLhs& operator/=(TLhs& lhs, const TRhs& rhs) return ca_binary_op(lhs, rhs); } - } // namespace mp \ No newline at end of file diff --git a/src/mp/mp.hpp b/src/mp/mp.hpp index 5092ebe..d15ab96 100644 --- a/src/mp/mp.hpp +++ b/src/mp/mp.hpp @@ -1,5 +1,5 @@ #pragma once #include "int.hpp" -#include "math.hpp" #include "lib.hpp" +#include "math.hpp" diff --git a/src/mp/storage.hpp b/src/mp/storage.hpp index 760c117..43ae420 100644 --- a/src/mp/storage.hpp +++ b/src/mp/storage.hpp @@ -11,7 +11,7 @@ namespace mp template class ArrayContainer { - public: +public: ArrayContainer() = default; TElem& operator[](size_t idx) { return m_data[idx]; } @@ -31,7 +31,7 @@ class ArrayContainer m_size = new_size; } - private: +private: size_t m_size = 0; std::array m_data; }; @@ -39,7 +39,7 @@ class ArrayContainer template class VectorContainer { - public: +public: VectorContainer() = default; TElem& operator[](size_t idx) { return m_data[idx]; } @@ -56,7 +56,7 @@ class VectorContainer m_data.resize(new_size, TElem{0}); } - private: +private: std::vector m_data; }; @@ -64,7 +64,7 @@ class VectorContainer constexpr size_t MAX_ARRAY_BYTES = 128; template -using Container = - std::conditional_t<(MaxSize > MAX_ARRAY_BYTES / sizeof(TElem)), VectorContainer, ArrayContainer>; +using Container = std::conditional_t<(MaxSize > MAX_ARRAY_BYTES / sizeof(TElem)), VectorContainer, + ArrayContainer>; } // namespace mp \ No newline at end of file diff --git a/src/mp/utils.hpp b/src/mp/utils.hpp index ce5ab33..d152bfc 100644 --- a/src/mp/utils.hpp +++ b/src/mp/utils.hpp @@ -137,4 +137,4 @@ constexpr TElem calculate_last_elem_mask() return mask; } -} \ No newline at end of file +} // namespace mp \ No newline at end of file