Skip to content
Merged
Show file tree
Hide file tree
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
17 changes: 17 additions & 0 deletions include/intx/intx.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <bit>
#include <cassert>
#include <climits>
#include <compare>
#include <concepts>
#include <cstddef>
#include <cstdint>
Expand Down Expand Up @@ -311,6 +312,14 @@ struct uint<128>
friend constexpr bool operator>(uint x, uint y) noexcept { return y < x; }
friend constexpr bool operator>=(uint x, uint y) noexcept { return !(x < y); }

friend constexpr std::strong_ordering operator<=>(uint x, uint y) noexcept
{
if (x == y)
return std::strong_ordering::equal;

return (x < y) ? std::strong_ordering::less : std::strong_ordering::greater;
}

friend constexpr uint operator~(uint x) noexcept { return {~x[0], ~x[1]}; }
friend constexpr uint operator|(uint x, uint y) noexcept { return {x[0] | y[0], x[1] | y[1]}; }
friend constexpr uint operator&(uint x, uint y) noexcept { return {x[0] & y[0], x[1] & y[1]}; }
Expand Down Expand Up @@ -1025,6 +1034,14 @@ struct uint
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); }

friend constexpr std::strong_ordering operator<=>(const uint& x, const uint& y) noexcept
{
if (x == y)
return std::strong_ordering::equal;

return (x < y) ? std::strong_ordering::less : std::strong_ordering::greater;
}

friend constexpr uint operator<<(const uint& x, uint64_t shift) noexcept
{
if (shift >= num_bits) [[unlikely]]
Expand Down
43 changes: 43 additions & 0 deletions test/unittests/test_intx_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,49 @@ TYPED_TEST(uint_api, comparison)
EXPECT_TRUE(d >= x);
}

template <typename T>
struct Wrapper
{
T v;

friend auto operator<=>(Wrapper, Wrapper) = default;
};

TYPED_TEST(uint_api, spaceship_operator_with_wrapper)
{
Wrapper<TypeParam> a{1};
Wrapper<TypeParam> b{2};
Wrapper<TypeParam> c{3};

EXPECT_EQ(a <=> b, std::strong_ordering::less);
EXPECT_EQ(a <=> a, std::strong_ordering::equal);
EXPECT_EQ(c <=> b, std::strong_ordering::greater);

EXPECT_TRUE(a < b);
EXPECT_TRUE(c > a);
EXPECT_TRUE(a >= a);
EXPECT_TRUE(b <= c);
EXPECT_TRUE(b == b);
EXPECT_TRUE(a != c);
}

TYPED_TEST(uint_api, spaceship_operator)
{
TypeParam a{1};
TypeParam b{2};
TypeParam c{3};

EXPECT_EQ(a <=> a, std::strong_ordering::equal);
EXPECT_EQ(a <=> b, std::strong_ordering::less);
EXPECT_EQ(a <=> c, std::strong_ordering::less);
EXPECT_EQ(b <=> a, std::strong_ordering::greater);
EXPECT_EQ(b <=> b, std::strong_ordering::equal);
EXPECT_EQ(b <=> c, std::strong_ordering::less);
EXPECT_EQ(c <=> a, std::strong_ordering::greater);
EXPECT_EQ(c <=> b, std::strong_ordering::greater);
EXPECT_EQ(c <=> c, std::strong_ordering::equal);
}

TYPED_TEST(uint_api, arithmetic_op_assignment)
{
TypeParam x;
Expand Down