Skip to content

Commit b8fe847

Browse files
committed
Fix branching in isinf
1 parent d20e8ff commit b8fe847

1 file changed

Lines changed: 3 additions & 5 deletions

File tree

include/boost/safe_numbers/detail/float_basis.hpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -364,19 +364,17 @@ BOOST_SAFE_NUMBERS_HOST_DEVICE [[nodiscard]] constexpr auto modbyzero_mod_msg()
364364

365365
namespace impl {
366366

367-
// Clear the sign bit rather than branch on val < 0 which was frequently mis-predicted
368367
template <compatible_float_type T>
369368
BOOST_SAFE_NUMBERS_HOST_DEVICE [[nodiscard]] constexpr auto constexpr_abs(const T val) noexcept -> T
370369
{
371-
using bit_type = std::conditional_t<std::is_same_v<T, float>, std::uint32_t, std::uint64_t>;
372-
constexpr bit_type mask {static_cast<bit_type>(~(bit_type{1} << (sizeof(T) * 8U - 1U)))};
373-
return std::bit_cast<T>(static_cast<bit_type>(std::bit_cast<bit_type>(val) & mask));
370+
return val < 0 ? -val : val;
374371
}
375372

373+
// Two-sided compare rather than constexpr_abs(val) > max()
376374
template <compatible_float_type T>
377375
BOOST_SAFE_NUMBERS_HOST_DEVICE [[nodiscard]] constexpr auto constexpr_isinf(const T val) noexcept -> bool
378376
{
379-
return constexpr_abs(val) > std::numeric_limits<T>::max();
377+
return (val > std::numeric_limits<T>::max()) | (val < std::numeric_limits<T>::lowest());
380378
}
381379

382380
// val != val is the canonical NAN test

0 commit comments

Comments
 (0)