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
13 changes: 13 additions & 0 deletions include/boost/int128/detail/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,17 @@ using builtin_u128 = unsigned __int128;
# define BOOST_INT128_FORCE_INLINE inline
#endif

#ifdef __x86_64__

# include <x86intrin.h>
# ifdef __ADX__
# define BOOST_INT128_ADD_CARRY _addcarryx_u64
# define BOOST_INT128_SUB_CARRY _subcarryx_u64
# else
# define BOOST_INT128_ADD_CARRY _addcarry_u64
# define BOOST_INT128_SUB_CARRY _subcarry_u64
#endif

#endif // x64 macros

#endif // BOOST_INT128_DETAIL_CONFIG_HPP
171 changes: 167 additions & 4 deletions include/boost/int128/detail/int128_imp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <boost/int128/detail/traits.hpp>
#include <boost/int128/detail/constants.hpp>
#include <cstdint>
#include <cstring>

namespace boost {
namespace int128 {
Expand Down Expand Up @@ -220,7 +221,7 @@ constexpr bool operator==(const int128_t lhs, const int128_t rhs) noexcept
{
// x64 and ARM64 like the values in opposite directions

#if defined(__aarch64__) || defined(_M_ARM64)
#if defined(__aarch64__) || defined(_M_ARM64) || defined(__x86_64__) || defined(_M_X64) || defined(_M_IX86)

return lhs.low == rhs.low && lhs.high == rhs.high;

Expand Down Expand Up @@ -293,10 +294,27 @@ constexpr bool operator!=(const int128_t lhs, const int128_t rhs) noexcept
{
// x64 and ARM64 like the values in opposite directions

#if defined(__aarch64__) || defined(_M_ARM64)
#if defined(__aarch64__) || defined(_M_ARM64) || defined(_M_X64) || defined(_M_IX86)

return lhs.low != rhs.low || lhs.high != rhs.high;

#elif defined(__x86_64__) && !defined(BOOST_INT128_NO_CONSTEVAL_DETECTION) && defined(__GNUC__) && !defined(__clang__)

if (BOOST_INT128_IS_CONSTANT_EVALUATED(lhs))
{
return lhs.high != rhs.high || lhs.low != rhs.low;
}
else
{
detail::builtin_i128 builtin_lhs {};
detail::builtin_i128 builtin_rhs {};

std::memcpy(&builtin_lhs, &lhs, sizeof(builtin_lhs));
std::memcpy(&builtin_rhs, &rhs, sizeof(builtin_rhs));

return builtin_lhs != builtin_rhs;
}

#else

return lhs.high != rhs.high || lhs.low != rhs.low;
Expand Down Expand Up @@ -374,7 +392,33 @@ constexpr bool operator!=(const detail::builtin_u128 lhs, const int128_t rhs) no

constexpr bool operator<(const int128_t lhs, const int128_t rhs) noexcept
{
// On ARM macs only with the clang compiler is casting to __int128 uniformly better (and seemingly cost free)
#if defined(__aarch64__) && defined(__APPLE__) && defined(__clang__)

return static_cast<detail::builtin_i128>(lhs) < static_cast<detail::builtin_i128>(rhs);

#elif defined(__x86_64__) && !defined(BOOST_INT128_NO_CONSTEVAL_DETECTION) && defined(__GNUC__) && !defined(__clang__)

if (BOOST_INT128_IS_CONSTANT_EVALUATED(lhs))
{
return lhs.high == rhs.high ? lhs.low < rhs.low : lhs.high < rhs.high;
}
else
{
detail::builtin_i128 builtin_lhs {};
detail::builtin_i128 builtin_rhs {};

std::memcpy(&builtin_lhs, &lhs, sizeof(builtin_lhs));
std::memcpy(&builtin_rhs, &rhs, sizeof(builtin_rhs));

return builtin_lhs < builtin_rhs;
}

#else

return lhs.high == rhs.high ? lhs.low < rhs.low : lhs.high < rhs.high;

#endif
}

template <BOOST_INT128_DEFAULTED_SIGNED_INTEGER_CONCEPT>
Expand Down Expand Up @@ -431,7 +475,33 @@ constexpr bool operator<(const detail::builtin_u128 lhs, const int128_t rhs) noe

constexpr bool operator>(const int128_t lhs, const int128_t rhs) noexcept
{
// On ARM macs only with the clang compiler is casting to __int128 uniformly better (and seemingly cost free)
#if defined(__aarch64__) && defined(__APPLE__) && defined(__clang__)

return static_cast<detail::builtin_i128>(lhs) > static_cast<detail::builtin_i128>(rhs);

#elif defined(__x86_64__) && !defined(BOOST_INT128_NO_CONSTEVAL_DETECTION) && defined(__GNUC__) && !defined(__clang__)

if (BOOST_INT128_IS_CONSTANT_EVALUATED(lhs))
{
return lhs.high == rhs.high ? lhs.low > rhs.low : lhs.high > rhs.high;
}
else
{
detail::builtin_i128 builtin_lhs {};
detail::builtin_i128 builtin_rhs {};

std::memcpy(&builtin_lhs, &lhs, sizeof(builtin_lhs));
std::memcpy(&builtin_rhs, &rhs, sizeof(builtin_rhs));

return builtin_lhs > builtin_rhs;
}

#else

return lhs.high == rhs.high ? lhs.low > rhs.low : lhs.high > rhs.high;

#endif
}

template <BOOST_INT128_DEFAULTED_SIGNED_INTEGER_CONCEPT>
Expand Down Expand Up @@ -488,7 +558,33 @@ constexpr bool operator>(const detail::builtin_u128 lhs, const int128_t rhs) noe

constexpr bool operator<=(const int128_t lhs, const int128_t rhs) noexcept
{
// On ARM macs only with the clang compiler is casting to __int128 uniformly better (and seemingly cost free)
#if defined(__aarch64__) && defined(__APPLE__) && defined(__clang__)

return static_cast<detail::builtin_i128>(lhs) <= static_cast<detail::builtin_i128>(rhs);

#elif defined(__x86_64__) && !defined(BOOST_INT128_NO_CONSTEVAL_DETECTION) && defined(__GNUC__) && !defined(__clang__)

if (BOOST_INT128_IS_CONSTANT_EVALUATED(lhs))
{
return lhs.high == rhs.high ? lhs.low <= rhs.low : lhs.high <= rhs.high;
}
else
{
detail::builtin_i128 builtin_lhs {};
detail::builtin_i128 builtin_rhs {};

std::memcpy(&builtin_lhs, &lhs, sizeof(builtin_lhs));
std::memcpy(&builtin_rhs, &rhs, sizeof(builtin_rhs));

return builtin_lhs <= builtin_rhs;
}

#else

return lhs.high == rhs.high ? lhs.low <= rhs.low : lhs.high <= rhs.high;

#endif
}

template <BOOST_INT128_DEFAULTED_SIGNED_INTEGER_CONCEPT>
Expand Down Expand Up @@ -545,7 +641,33 @@ constexpr bool operator<=(const detail::builtin_u128 lhs, const int128_t rhs) no

constexpr bool operator>=(const int128_t lhs, const int128_t rhs) noexcept
{
// On ARM macs only with the clang compiler is casting to __int128 uniformly better (and seemingly cost free)
#if defined(__aarch64__) && defined(__APPLE__) && defined(__clang__)

return static_cast<detail::builtin_i128>(lhs) >= static_cast<detail::builtin_i128>(rhs);

#elif defined(__x86_64__) && !defined(BOOST_INT128_NO_CONSTEVAL_DETECTION) && defined(__GNUC__) && !defined(__clang__)

if (BOOST_INT128_IS_CONSTANT_EVALUATED(lhs))
{
return lhs.high == rhs.high ? lhs.low >= rhs.low : lhs.high >= rhs.high;
}
else
{
detail::builtin_i128 builtin_lhs {};
detail::builtin_i128 builtin_rhs {};

std::memcpy(&builtin_lhs, &lhs, sizeof(builtin_lhs));
std::memcpy(&builtin_rhs, &rhs, sizeof(builtin_rhs));

return builtin_lhs >= builtin_rhs;
}

#else

return lhs.high == rhs.high ? lhs.low >= rhs.low : lhs.high >= rhs.high;

#endif
}

template <BOOST_INT128_DEFAULTED_SIGNED_INTEGER_CONCEPT>
Expand Down Expand Up @@ -1054,16 +1176,57 @@ constexpr int128_t& int128_t::operator++(int) noexcept

namespace detail {

BOOST_INT128_FORCE_INLINE constexpr int128_t default_add(const int128_t lhs, const int128_t rhs) noexcept
BOOST_INT128_FORCE_INLINE constexpr int128_t library_add(const int128_t lhs, const int128_t rhs) noexcept
{
const auto new_low {lhs.low + rhs.low};
const auto new_high {static_cast<std::uint64_t>(lhs.high) +
static_cast<std::uint64_t>(rhs.high) +
static_cast<std::uint64_t>(new_low < lhs.low)};

return int128_t{static_cast<std::int64_t>(new_high), new_low};
}

BOOST_INT128_FORCE_INLINE constexpr int128_t default_add(const int128_t lhs, const int128_t rhs) noexcept
{
#if defined(__x86_64__) && !defined(BOOST_INT128_NO_CONSTEVAL_DETECTION) && !defined(_WIN32)

if (BOOST_INT128_IS_CONSTANT_EVALUATED(lhs))
{
return library_add(lhs, rhs);
}
else
{
#if defined(__GNUC__) && __GNUC__ >= 8
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wclass-memaccess"
#endif

builtin_i128 builtin_lhs {};
builtin_i128 builtin_rhs {};

std::memcpy(&builtin_lhs, &lhs, sizeof(builtin_lhs));
std::memcpy(&builtin_rhs, &rhs, sizeof(builtin_rhs));

const auto builtin_res {builtin_lhs + builtin_rhs};

int128_t result {};

std::memcpy(&result, &builtin_res, sizeof(result));

return result;

#if defined(__GNUC__) && __GNUC__ >= 8
# pragma GCC diagnostic pop
#endif
}

#else

return library_add(lhs, rhs);

#endif
}

template <BOOST_INT128_DEFAULTED_INTEGER_CONCEPT>
BOOST_INT128_FORCE_INLINE constexpr int128_t default_add(const int128_t lhs, const Integer rhs) noexcept
{
Expand Down
Loading