Skip to content

Commit

Permalink
Simplify conversions for operator/ and operator%
Browse files Browse the repository at this point in the history
  • Loading branch information
chfast committed May 16, 2022
1 parent bc88cb8 commit 1c3c141
Showing 1 changed file with 15 additions and 55 deletions.
70 changes: 15 additions & 55 deletions include/intx/intx.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1106,6 +1106,20 @@ struct uint
}

inline constexpr uint& operator*=(const uint& y) noexcept { return *this = *this * y; }

friend inline constexpr uint operator/(const uint& x, const uint& y) noexcept
{
return udivrem(x, y).quot;
}

friend inline constexpr uint operator%(const uint& x, const uint& y) noexcept
{
return udivrem(x, y).rem;
}

inline constexpr uint& operator/=(const uint& y) noexcept { return *this = *this / y; }

inline constexpr uint& operator%=(const uint& y) noexcept { return *this = *this % y; }
};

using uint192 = uint<192>;
Expand Down Expand Up @@ -1761,7 +1775,7 @@ inline void udivrem_knuth(
} // namespace internal

template <unsigned M, unsigned N>
div_result<uint<M>, uint<N>> udivrem(const uint<M>& u, const uint<N>& v) noexcept
constexpr div_result<uint<M>, uint<N>> udivrem(const uint<M>& u, const uint<N>& v) noexcept
{
auto na = internal::normalize(u, v);

Expand Down Expand Up @@ -1815,32 +1829,6 @@ inline constexpr div_result<uint<N>> sdivrem(const uint<N>& u, const uint<N>& v)
return {q_is_neg ? -res.quot : res.quot, u_is_neg ? -res.rem : res.rem};
}

template <unsigned N>
inline constexpr uint<N> operator/(const uint<N>& x, const uint<N>& y) noexcept
{
return udivrem(x, y).quot;
}

template <unsigned N>
inline constexpr uint<N> operator%(const uint<N>& x, const uint<N>& y) noexcept
{
return udivrem(x, y).rem;
}

template <unsigned N, typename T,
typename = typename std::enable_if<std::is_convertible<T, uint<N>>::value>::type>
inline constexpr uint<N>& operator/=(uint<N>& x, const T& y) noexcept
{
return x = x / y;
}

template <unsigned N, typename T,
typename = typename std::enable_if<std::is_convertible<T, uint<N>>::value>::type>
inline constexpr uint<N>& operator%=(uint<N>& x, const T& y) noexcept
{
return x = x % y;
}

template <unsigned N>
inline constexpr uint<N> bswap(const uint<N>& x) noexcept
{
Expand All @@ -1854,34 +1842,6 @@ inline constexpr uint<N> bswap(const uint<N>& x) noexcept

// Support for type conversions for binary operators.

template <unsigned N, typename T,
typename = typename std::enable_if<std::is_convertible<T, uint<N>>::value>::type>
inline constexpr uint<N> operator/(const uint<N>& x, const T& y) noexcept
{
return x / uint<N>(y);
}

template <unsigned N, typename T,
typename = typename std::enable_if<std::is_convertible<T, uint<N>>::value>::type>
inline constexpr uint<N> operator/(const T& x, const uint<N>& y) noexcept
{
return uint<N>(x) / y;
}

template <unsigned N, typename T,
typename = typename std::enable_if<std::is_convertible<T, uint<N>>::value>::type>
inline constexpr uint<N> operator%(const uint<N>& x, const T& y) noexcept
{
return x % uint<N>(y);
}

template <unsigned N, typename T,
typename = typename std::enable_if<std::is_convertible<T, uint<N>>::value>::type>
inline constexpr uint<N> operator%(const T& x, const uint<N>& y) noexcept
{
return uint<N>(x) % y;
}

template <unsigned N, typename T,
typename = typename std::enable_if<std::is_convertible<T, uint<N>>::value>::type>
inline constexpr uint<N> operator|(const uint<N>& x, const T& y) noexcept
Expand Down

0 comments on commit 1c3c141

Please sign in to comment.