Skip to content

Commit

Permalink
Change includes order
Browse files Browse the repository at this point in the history
  • Loading branch information
devatrun committed Oct 15, 2021
1 parent b5d28e9 commit c1a98a0
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 70 deletions.
61 changes: 0 additions & 61 deletions include/slimcpplib/long_math.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,22 +160,16 @@ constexpr type_t shr2(type_t value_hi, type_t value_lo, uint_t shift) noexcept;

template<typename type_t, std::enable_if_t<is_unsigned_v<type_t>, int> = 0>
constexpr type_t addc(type_t value1, type_t value2, bool& carry) noexcept;
template<typename type_t, std::enable_if_t<is_unsigned_array_v<type_t>, int> = 0>
constexpr void add(type_t& value1, const type_t& value2) noexcept;

// subtract with borrow

template<typename type_t, std::enable_if_t<is_unsigned_v<type_t>, int> = 0>
constexpr type_t subb(type_t value1, type_t value2, bool& borrow) noexcept;
template<typename type_t, std::enable_if_t<is_unsigned_array_v<type_t>, int> = 0>
constexpr void sub(type_t& value1, const type_t& value2) noexcept;

// multiply with carry

template<typename type_t, std::enable_if_t<is_unsigned_v<type_t>, int> = 0>
constexpr type_t mulc(type_t value1, type_t value2, type_t& carry) noexcept;
template<typename type_t, std::enable_if_t<is_unsigned_array_v<type_t>, int> = 0>
constexpr void mul(type_t& value1, const type_t& value2) noexcept;

// divide with remainder

Expand Down Expand Up @@ -311,18 +305,6 @@ constexpr type_t addc(type_t value1, const type_t value2, bool& carry) noexcept



////////////////////////////////////////////////////////////////////////////////////////////////////
template<typename type_t, std::enable_if_t<is_unsigned_array_v<type_t>, 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<typename type_t, std::enable_if_t<is_unsigned_v<type_t>, int>>
constexpr type_t subb(type_t value1, type_t value2, bool& borrow) noexcept
Expand All @@ -342,18 +324,6 @@ constexpr type_t subb(type_t value1, type_t value2, bool& borrow) noexcept



////////////////////////////////////////////////////////////////////////////////////////////////////
template<typename type_t, std::enable_if_t<is_unsigned_array_v<type_t>, 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<typename type_t, std::enable_if_t<is_unsigned_v<type_t>, int> = 0>
constexpr type_t mulc_classic(type_t value1, type_t value2, type_t& carry) noexcept
Expand Down Expand Up @@ -457,37 +427,6 @@ constexpr type_t mulc(type_t value1, type_t value2, type_t& carry) noexcept



////////////////////////////////////////////////////////////////////////////////////////////////////
template<typename type_t, std::enable_if_t<is_unsigned_array_v<type_t>, 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<typename type_t, std::enable_if_t<is_unsigned_v<type_t>, int>>
constexpr type_t divr(type_t value1, type_t value2, std::optional<type_t>& remainder) noexcept
Expand Down
79 changes: 79 additions & 0 deletions include/slimcpplib/long_math_long.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
////////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -107,6 +116,21 @@ constexpr long_uint_t<type_t, size> mulc(long_uint_t<type_t, size> value1, long_
template<typename type_t, uint_t size>
constexpr long_uint_t<type_t, size> divr(long_uint_t<type_t, size> value1, long_uint_t<type_t, size> value2, std::optional<long_uint_t<type_t, size>>& remainder) noexcept;

// add two vectors

template<typename type_t, std::enable_if_t<is_unsigned_array_v<type_t>, int> = 0>
constexpr void add(type_t& value1, const type_t& value2) noexcept;

// subtract two vectors

template<typename type_t, std::enable_if_t<is_unsigned_array_v<type_t>, int> = 0>
constexpr void sub(type_t& value1, const type_t& value2) noexcept;

// multiply two vectors

template<typename type_t, std::enable_if_t<is_unsigned_array_v<type_t>, int> = 0>
constexpr void mul(type_t& value1, const type_t& value2) noexcept;



////////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -321,6 +345,61 @@ constexpr long_uint_t<type_t, size> divr(long_uint_t<type_t, size> value1, long_
return quotient;
}



////////////////////////////////////////////////////////////////////////////////////////////////////
template<typename type_t, std::enable_if_t<is_unsigned_array_v<type_t>, 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<typename type_t, std::enable_if_t<is_unsigned_array_v<type_t>, 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<typename type_t, std::enable_if_t<is_unsigned_array_v<type_t>, 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

////////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down
9 changes: 0 additions & 9 deletions include/slimcpplib/long_uint.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
////////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down

0 comments on commit c1a98a0

Please sign in to comment.