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

Use XOR-fold to implement operator== #245

Merged
merged 4 commits into from
Dec 16, 2021
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
3 changes: 2 additions & 1 deletion .clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ Checks: >
portability-*,
readability-*,
-readability-braces-around-statements,
-readability-identifier-length,
-readability-implicit-bool-conversion,
-readability-magic-numbers,
-readability-uppercase-literal-suffix,

WarningsAsErrors: '*'
FormatStyle: file
FormatStyle: file
6 changes: 3 additions & 3 deletions cmake/Hunter/config.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ hunter_config(

hunter_config(
benchmark
VERSION 1.5.3
URL https://github.com/google/benchmark/archive/v1.5.3.tar.gz
SHA1 32655d8796e708439ac5d4de8aa31f00d9dbda3b
VERSION 1.6.0
URL https://github.com/google/benchmark/archive/v1.6.0.tar.gz
SHA1 c4d1a9135e779c5507015ccc8c428cb4aca69cef
)
19 changes: 7 additions & 12 deletions include/intx/intx.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -281,17 +281,12 @@ inline constexpr uint128 fast_add(uint128 x, uint128 y) noexcept

inline constexpr bool operator==(uint128 x, uint128 y) noexcept
{
// Clang7: generates perfect xor based code,
// much better than __int128 where it uses vector instructions.
// GCC8: generates a bit worse cmp based code
// although it generates the xor based one for __int128.
return (x[0] == y[0]) & (x[1] == y[1]);
return ((x[0] ^ y[0]) | (x[1] ^ y[1])) == 0;
}

inline constexpr bool operator!=(uint128 x, uint128 y) noexcept
{
// Analogous to ==, but == not used directly, because that confuses GCC 8-9.
return (x[0] != y[0]) | (x[1] != y[1]);
return !(x == y);
}

inline constexpr bool operator<(uint128 x, uint128 y) noexcept
Expand All @@ -302,7 +297,7 @@ inline constexpr bool operator<(uint128 x, uint128 y) noexcept
#if INTX_HAS_BUILTIN_INT128
return builtin_uint128{x} < builtin_uint128{y};
#else
return (x[1] < y[1]) | ((x[1] == y[1]) & (x[0] < y[0]));
return (unsigned{x[1] < y[1]} | (unsigned{x[1] == y[1]} & unsigned{x[0] < y[0]})) != 0;
#endif
}

Expand Down Expand Up @@ -1005,10 +1000,10 @@ using uint512 = uint<512>;
template <unsigned N>
inline constexpr bool operator==(const uint<N>& x, const uint<N>& y) noexcept
{
bool result = true;
uint64_t folded = 0;
for (size_t i = 0; i < uint<N>::num_words; ++i)
result &= (x[i] == y[i]);
return result;
folded |= (x[i] ^ y[i]);
return folded == 0;
}

template <unsigned N, typename T,
Expand Down Expand Up @@ -1053,7 +1048,7 @@ inline constexpr bool operator<(const uint256& x, const uint256& y) noexcept
const auto xlo = uint128{x[0], x[1]};
const auto yhi = uint128{y[2], y[3]};
const auto ylo = uint128{y[0], y[1]};
return (xhi < yhi) | ((xhi == yhi) & (xlo < ylo));
return (unsigned(xhi < yhi) | (unsigned(xhi == yhi) & unsigned(xlo < ylo))) != 0;
}
#endif

Expand Down