From 0ec0e7055f112a7a9f9fe5fbe85d114770c032ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Bylica?= Date: Mon, 2 Sep 2024 23:20:33 +0200 Subject: [PATCH] Move comparison operators into the class (#322) --- include/intx/intx.hpp | 108 +++++++----------------------------------- 1 file changed, 17 insertions(+), 91 deletions(-) diff --git a/include/intx/intx.hpp b/include/intx/intx.hpp index 6611a69..37ee2c7 100644 --- a/include/intx/intx.hpp +++ b/include/intx/intx.hpp @@ -980,104 +980,30 @@ struct uint folded |= (x[i] ^ y[i]); return folded == 0; } -}; - -using uint256 = uint<256>; -template -inline constexpr bool operator<(const uint& x, const uint& 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 -inline constexpr bool operator<(const uint& x, const T& y) noexcept - requires std::is_convertible_v> -{ - return x < uint(y); -} - -template -inline constexpr bool operator<(const T& x, const uint& y) noexcept - requires std::is_convertible_v> -{ - return uint(x) < y; -} - - -template -inline constexpr bool operator>(const uint& x, const uint& y) noexcept -{ - return y < x; -} - -template -inline constexpr bool operator>(const uint& x, const T& y) noexcept - requires std::is_convertible_v> -{ - return x > uint(y); -} - -template -inline constexpr bool operator>(const T& x, const uint& y) noexcept - requires std::is_convertible_v> -{ - return uint(x) > y; -} - - -template -inline constexpr bool operator>=(const uint& x, const uint& y) noexcept -{ - return !(x < y); -} - -template -inline constexpr bool operator>=(const uint& x, const T& y) noexcept - requires std::is_convertible_v> -{ - return x >= uint(y); -} - -template -inline constexpr bool operator>=(const T& x, const uint& y) noexcept - requires std::is_convertible_v> -{ - return uint(x) >= y; -} - - -template -inline 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 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 -inline constexpr bool operator<=(const uint& x, const T& y) noexcept - requires std::is_convertible_v> -{ - return x <= uint(y); -} +using uint256 = uint<256>; -template -inline constexpr bool operator<=(const T& x, const uint& y) noexcept - requires std::is_convertible_v> -{ - return uint(x) <= y; -} /// Signed less than comparison. ///