Skip to content

Commit

Permalink
Merge pull request #275 from ckormanyos/clang_modulus_with_integral
Browse files Browse the repository at this point in the history
  • Loading branch information
ckormanyos authored May 17, 2022
2 parents 29a500f + fa550f9 commit ada039d
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 11 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/wide_integer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ jobs:
working-directory: ${{runner.workspace}}/build
run: ctest --verbose --output-on-failure
gnumake-clang-tidy-12-native:
runs-on: ubuntu-latest
runs-on: ubuntu-20.04
defaults:
run:
shell: bash
Expand Down Expand Up @@ -642,7 +642,7 @@ jobs:
ls -la ./wide_integer.exe
./wide_integer.exe
gcc-clang-boost_backend:
runs-on: ubuntu-latest
runs-on: ubuntu-20.04
defaults:
run:
shell: bash
Expand Down
12 changes: 6 additions & 6 deletions math/wide_integer/uintwide_t.h
Original file line number Diff line number Diff line change
Expand Up @@ -569,9 +569,9 @@
template<typename IntegralType, const size_t Width2, typename LimbType, typename AllocatorType, const bool IsSigned> constexpr auto operator/(const uintwide_t<Width2, LimbType, AllocatorType, IsSigned>& u, const IntegralType& v) -> typename std::enable_if<std::is_integral<IntegralType>::value, uintwide_t<Width2, LimbType, AllocatorType, IsSigned>>::type;

template<typename IntegralType, const size_t Width2, typename LimbType, typename AllocatorType, const bool IsSigned>
constexpr auto operator%(const uintwide_t<Width2, LimbType, AllocatorType, IsSigned>& u, const IntegralType& v) -> typename std::enable_if<( std::is_integral<IntegralType>::value
&& (!std::is_unsigned<IntegralType>::value)),
uintwide_t<Width2, LimbType, AllocatorType, IsSigned>>::type;
constexpr auto operator%(const uintwide_t<Width2, LimbType, AllocatorType, IsSigned>& u, const IntegralType& v) -> typename std::enable_if<( std::is_integral<IntegralType>::value
&& std::is_signed<IntegralType>::value),
uintwide_t<Width2, LimbType, AllocatorType, IsSigned>>::type;

template<typename IntegralType, const size_t Width2, typename LimbType, typename AllocatorType, const bool IsSigned>
WIDE_INTEGER_CONSTEXPR auto operator%(const uintwide_t<Width2, LimbType, AllocatorType, IsSigned>& u, const IntegralType& v) -> typename std::enable_if<( std::is_integral <IntegralType>::value
Expand Down Expand Up @@ -4305,9 +4305,9 @@
template<typename IntegralType, const size_t Width2, typename LimbType, typename AllocatorType, const bool IsSigned> constexpr auto operator/(const uintwide_t<Width2, LimbType, AllocatorType, IsSigned>& u, const IntegralType& v) -> typename std::enable_if<std::is_integral<IntegralType>::value, uintwide_t<Width2, LimbType, AllocatorType, IsSigned>>::type { return uintwide_t<Width2, LimbType, AllocatorType, IsSigned>(u).operator/=(uintwide_t<Width2, LimbType, AllocatorType, IsSigned>(v)); }

template<typename IntegralType, const size_t Width2, typename LimbType, typename AllocatorType, const bool IsSigned>
constexpr auto operator%(const uintwide_t<Width2, LimbType, AllocatorType, IsSigned>& u, const IntegralType& v) -> typename std::enable_if<( ( std::is_integral<IntegralType>::value)
&& (!std::is_unsigned<IntegralType>::value)),
uintwide_t<Width2, LimbType, AllocatorType, IsSigned>>::type
constexpr auto operator%(const uintwide_t<Width2, LimbType, AllocatorType, IsSigned>& u, const IntegralType& v) -> typename std::enable_if<( std::is_integral<IntegralType>::value
&& std::is_signed<IntegralType>::value),
uintwide_t<Width2, LimbType, AllocatorType, IsSigned>>::type
{ return uintwide_t<Width2, LimbType, AllocatorType, IsSigned>(u).operator%=(uintwide_t<Width2, LimbType, AllocatorType, IsSigned>(v)); }

template<typename IntegralType, const size_t Width2, typename LimbType, typename AllocatorType, const bool IsSigned>
Expand Down
38 changes: 35 additions & 3 deletions test/test_uintwide_t_spot_values.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1081,15 +1081,47 @@ auto local_test_spot_values::test() -> bool // NOLINT(readability-function-cogni

WIDE_INTEGER_CONSTEXPR uint256_t c("0xE491A360C57EB4306C61F9A04F7F7D99BE3676AAD2D71C5592D5AE70F84AF076");

result_is_ok = (((a * b) == c) && result_is_ok);
WIDE_INTEGER_CONSTEXPR auto result_mul_is_ok = ((a * b) == c);

#if(WIDE_INTEGER_CONSTEXPR_IS_COMPILE_TIME_CONST == 1)
static_assert(result_mul_is_ok, "Error: Static check of spot value multiplication is not OK");
#endif

result_is_ok = (result_mul_is_ok && result_is_ok);

WIDE_INTEGER_CONSTEXPR uint256_t q(10U); // NOLINT(cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers)

result_is_ok = (((a / b) == q) && result_is_ok);
WIDE_INTEGER_CONSTEXPR auto result_div_is_ok = ((a / b) == q);

#if(WIDE_INTEGER_CONSTEXPR_IS_COMPILE_TIME_CONST == 1)
static_assert(result_div_is_ok, "Error: Static check of spot value division is not OK");
#endif

result_is_ok = (result_div_is_ok && result_is_ok);

WIDE_INTEGER_CONSTEXPR uint256_t m("0x14998D5CA3DB6385F7DEDF4621DE48A9104AC13797C6567713D7ABC216D7AB4C");

result_is_ok = (((a % b) == m) && result_is_ok);
WIDE_INTEGER_CONSTEXPR auto result_mod_is_ok = ((a % b) == m);

#if(WIDE_INTEGER_CONSTEXPR_IS_COMPILE_TIME_CONST == 1)
static_assert(result_mod_is_ok, "Error: Static check of spot value modulus is not OK");
#endif

result_is_ok = (result_mod_is_ok && result_is_ok);

{
// See also: https://github.com/ckormanyos/wide-integer/issues/274

WIDE_INTEGER_CONSTEXPR auto result_mod1_is_ok = ((a % 1) == 0);
WIDE_INTEGER_CONSTEXPR auto result_mod7u_is_ok = ((a % 7U) == 3U); // NOLINT(cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers)

#if(WIDE_INTEGER_CONSTEXPR_IS_COMPILE_TIME_CONST == 1)
static_assert(result_mod1_is_ok, "Error: Static check of spot value modulus with 1 is not OK");
static_assert(result_mod7u_is_ok, "Error: Static check of spot value modulus with 7U is not OK");
#endif

result_is_ok = ((result_mod1_is_ok && result_mod7u_is_ok) && result_is_ok);
}
}

return result_is_ok;
Expand Down

0 comments on commit ada039d

Please sign in to comment.