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 eb0eae1
Showing 1 changed file with 18 additions and 54 deletions.
72 changes: 18 additions & 54 deletions include/intx/intx.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -786,6 +786,10 @@ inline div_result<uint64_t, uint128> udivrem_3by2(
return {q[1], r};
}


template <unsigned M, unsigned N>
div_result<uint<M>, uint<N>> udivrem(const uint<M>& u, const uint<N>& v) noexcept;

inline div_result<uint128> udivrem(uint128 x, uint128 y) noexcept
{
if (y[1] == 0)
Expand Down Expand Up @@ -1106,6 +1110,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 @@ -1815,32 +1833,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 +1846,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 eb0eae1

Please sign in to comment.