Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move comparison operators into the class #322

Merged
merged 1 commit into from
Sep 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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