Skip to content

Commit

Permalink
Move comparison operators into the class (#322)
Browse files Browse the repository at this point in the history
  • Loading branch information
chfast authored Sep 2, 2024
1 parent 24a93b6 commit 0ec0e70
Showing 1 changed file with 17 additions and 91 deletions.
108 changes: 17 additions & 91 deletions include/intx/intx.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -980,104 +980,30 @@ struct uint
folded |= (x[i] ^ y[i]);
return folded == 0;
}
};

using uint256 = uint<256>;

template <unsigned N>
inline constexpr bool operator<(const uint<N>& x, const uint<N>& y) noexcept
{
if constexpr (N == 256)
friend inline constexpr bool operator<(const uint& x, const uint& y) noexcept
{
auto xp = uint128{x[2], x[3]};
auto yp = uint128{y[2], y[3]};
if (xp == yp)
if constexpr (N == 256)
{
xp = uint128{x[0], x[1]};
yp = uint128{y[0], y[1]};
auto xp = uint128{x[2], x[3]};
auto yp = uint128{y[2], y[3]};
if (xp == yp)
{
xp = uint128{x[0], x[1]};
yp = uint128{y[0], y[1]};
}
return xp < yp;
}
return xp < yp;
else
return subc(x, y).carry;
}
else
return subc(x, y).carry;
}

template <unsigned N, typename T>
inline constexpr bool operator<(const uint<N>& x, const T& y) noexcept
requires std::is_convertible_v<T, uint<N>>
{
return x < uint<N>(y);
}

template <unsigned N, typename T>
inline constexpr bool operator<(const T& x, const uint<N>& y) noexcept
requires std::is_convertible_v<T, uint<N>>
{
return uint<N>(x) < y;
}


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

template <unsigned N, typename T>
inline constexpr bool operator>(const uint<N>& x, const T& y) noexcept
requires std::is_convertible_v<T, uint<N>>
{
return x > uint<N>(y);
}

template <unsigned N, typename T>
inline constexpr bool operator>(const T& x, const uint<N>& y) noexcept
requires std::is_convertible_v<T, uint<N>>
{
return uint<N>(x) > y;
}


template <unsigned N>
inline constexpr bool operator>=(const uint<N>& x, const uint<N>& y) noexcept
{
return !(x < y);
}

template <unsigned N, typename T>
inline constexpr bool operator>=(const uint<N>& x, const T& y) noexcept
requires std::is_convertible_v<T, uint<N>>
{
return x >= uint<N>(y);
}

template <unsigned N, typename T>
inline constexpr bool operator>=(const T& x, const uint<N>& y) noexcept
requires std::is_convertible_v<T, uint<N>>
{
return uint<N>(x) >= y;
}


template <unsigned N>
inline constexpr bool operator<=(const uint<N>& x, const uint<N>& y) noexcept
{
return !(y < x);
}
friend constexpr bool operator>(const uint& x, const uint& y) noexcept { return y < x; }
friend constexpr bool operator>=(const uint& x, const uint& y) noexcept { return !(x < y); }
friend constexpr bool operator<=(const uint& x, const uint& y) noexcept { return !(y < x); }
};

template <unsigned N, typename T>
inline constexpr bool operator<=(const uint<N>& x, const T& y) noexcept
requires std::is_convertible_v<T, uint<N>>
{
return x <= uint<N>(y);
}
using uint256 = uint<256>;

template <unsigned N, typename T>
inline constexpr bool operator<=(const T& x, const uint<N>& y) noexcept
requires std::is_convertible_v<T, uint<N>>
{
return uint<N>(x) <= y;
}

/// Signed less than comparison.
///
Expand Down

0 comments on commit 0ec0e70

Please sign in to comment.