From c1a98a07b083b1e82ea7a25bc57b155c496edbe2 Mon Sep 17 00:00:00 2001 From: Videoman Date: Fri, 15 Oct 2021 15:53:57 +0300 Subject: [PATCH] Change includes order --- include/slimcpplib/long_math.h | 61 ---------------------- include/slimcpplib/long_math_long.h | 79 +++++++++++++++++++++++++++++ include/slimcpplib/long_uint.h | 9 ---- 3 files changed, 79 insertions(+), 70 deletions(-) diff --git a/include/slimcpplib/long_math.h b/include/slimcpplib/long_math.h index 7f387ac..444c2cc 100644 --- a/include/slimcpplib/long_math.h +++ b/include/slimcpplib/long_math.h @@ -160,22 +160,16 @@ constexpr type_t shr2(type_t value_hi, type_t value_lo, uint_t shift) noexcept; template, int> = 0> constexpr type_t addc(type_t value1, type_t value2, bool& carry) noexcept; -template, int> = 0> -constexpr void add(type_t& value1, const type_t& value2) noexcept; // subtract with borrow template, int> = 0> constexpr type_t subb(type_t value1, type_t value2, bool& borrow) noexcept; -template, int> = 0> -constexpr void sub(type_t& value1, const type_t& value2) noexcept; // multiply with carry template, int> = 0> constexpr type_t mulc(type_t value1, type_t value2, type_t& carry) noexcept; -template, int> = 0> -constexpr void mul(type_t& value1, const type_t& value2) noexcept; // divide with remainder @@ -311,18 +305,6 @@ constexpr type_t addc(type_t value1, const type_t value2, bool& carry) noexcept -//////////////////////////////////////////////////////////////////////////////////////////////////// -template, int>> -constexpr void add(type_t& value1, const type_t& value2) noexcept -{ - bool carry = false; - - for (uint_t n = 0; n < std::size(value1); ++n) - value1[n] = addc(value1[n], value2[n], carry); -} - - - //////////////////////////////////////////////////////////////////////////////////////////////////// template, int>> constexpr type_t subb(type_t value1, type_t value2, bool& borrow) noexcept @@ -342,18 +324,6 @@ constexpr type_t subb(type_t value1, type_t value2, bool& borrow) noexcept -//////////////////////////////////////////////////////////////////////////////////////////////////// -template, int>> -constexpr void sub(type_t& value1, const type_t& value2) noexcept -{ - bool borrow = false; - - for (uint_t n = 0; n < std::size(value1); ++n) - value1[n] = subb(value1[n], value2[n], borrow); -} - - - //////////////////////////////////////////////////////////////////////////////////////////////////// template, int> = 0> constexpr type_t mulc_classic(type_t value1, type_t value2, type_t& carry) noexcept @@ -457,37 +427,6 @@ constexpr type_t mulc(type_t value1, type_t value2, type_t& carry) noexcept -//////////////////////////////////////////////////////////////////////////////////////////////////// -template, int>> -constexpr void mul(type_t& value1, const type_t& value2) noexcept -{ - using value_t = typename type_t::value_type; - value_t carry = 0; - - type_t result; - result[0] = mulc(value1[0], value2[0], carry); - - for (uint_t n = 1; n < std::size(value1); ++n) - result[n] = mulc(value1[n], value2[0], carry); - - for (uint_t n = 1; n < std::size(value1); ++n) { - - type_t tmp; - carry = 0; - - for (uint_t k = 0; k < n; ++k) - tmp[k] = 0; - for (uint_t k = 0; k < std::size(value1) - n; ++k) - tmp[k + n] = mulc(value1[k], value2[n], carry); - - add(result, tmp); - } - - value1 = result; -} - - - //////////////////////////////////////////////////////////////////////////////////////////////////// template, int>> constexpr type_t divr(type_t value1, type_t value2, std::optional& remainder) noexcept diff --git a/include/slimcpplib/long_math_long.h b/include/slimcpplib/long_math_long.h index 5ff3cbc..b391976 100644 --- a/include/slimcpplib/long_math_long.h +++ b/include/slimcpplib/long_math_long.h @@ -34,6 +34,15 @@ #include "long_math.h" +#if __has_include("long_math_gcc.h") +#include "long_math_gcc.h" +#endif // __has_include("long_math_gcc.h") + +#if __has_include("long_math_msvc.h") +#include "long_math_msvc.h" +#endif // __has_include("long_math_msvc.h") + + namespace slim { //////////////////////////////////////////////////////////////////////////////////////////////////// @@ -107,6 +116,21 @@ constexpr long_uint_t mulc(long_uint_t value1, long_ template constexpr long_uint_t divr(long_uint_t value1, long_uint_t value2, std::optional>& remainder) noexcept; +// add two vectors + +template, int> = 0> +constexpr void add(type_t& value1, const type_t& value2) noexcept; + +// subtract two vectors + +template, int> = 0> +constexpr void sub(type_t& value1, const type_t& value2) noexcept; + +// multiply two vectors + +template, int> = 0> +constexpr void mul(type_t& value1, const type_t& value2) noexcept; + //////////////////////////////////////////////////////////////////////////////////////////////////// @@ -321,6 +345,61 @@ constexpr long_uint_t divr(long_uint_t value1, long_ return quotient; } + + +//////////////////////////////////////////////////////////////////////////////////////////////////// +template, int>> +constexpr void add(type_t& value1, const type_t& value2) noexcept +{ + bool carry = false; + + for (uint_t n = 0; n < std::size(value1); ++n) + value1[n] = addc(value1[n], value2[n], carry); +} + + + +//////////////////////////////////////////////////////////////////////////////////////////////////// +template, int>> +constexpr void sub(type_t& value1, const type_t& value2) noexcept +{ + bool borrow = false; + + for (uint_t n = 0; n < std::size(value1); ++n) + value1[n] = subb(value1[n], value2[n], borrow); +} + + + +//////////////////////////////////////////////////////////////////////////////////////////////////// +template, int>> +constexpr void mul(type_t& value1, const type_t& value2) noexcept +{ + using value_t = typename type_t::value_type; + value_t carry = 0; + + type_t result; + result[0] = mulc(value1[0], value2[0], carry); + + for (uint_t n = 1; n < std::size(value1); ++n) + result[n] = mulc(value1[n], value2[0], carry); + + for (uint_t n = 1; n < std::size(value1); ++n) { + + type_t tmp; + carry = 0; + + for (uint_t k = 0; k < n; ++k) + tmp[k] = 0; + for (uint_t k = 0; k < std::size(value1) - n; ++k) + tmp[k + n] = mulc(value1[k], value2[n], carry); + + add(result, tmp); + } + + value1 = result; +} + } // namespace slim //////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/include/slimcpplib/long_uint.h b/include/slimcpplib/long_uint.h index 11545ab..ebcc7a1 100644 --- a/include/slimcpplib/long_uint.h +++ b/include/slimcpplib/long_uint.h @@ -32,17 +32,8 @@ #pragma once -#include "long_math.h" #include "long_math_long.h" -#if __has_include("long_math_gcc.h") -#include "long_math_gcc.h" -#endif // __has_include("long_math_gcc.h") - -#if __has_include("long_math_msvc.h") -#include "long_math_msvc.h" -#endif // __has_include("long_math_msvc.h") - namespace slim { ////////////////////////////////////////////////////////////////////////////////////////////////////