Skip to content

Commit

Permalink
refactor __hypot_factors, e.g. replace std::array usage by std::pair
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul authored and Paul committed Jul 7, 2024
1 parent e0459e5 commit cf378ed
Showing 1 changed file with 24 additions and 35 deletions.
59 changes: 24 additions & 35 deletions libcxx/include/__math/hypot.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#include <__type_traits/is_arithmetic.h>
#include <__type_traits/is_same.h>
#include <__type_traits/promote.h>
#include <array>
#include <__utility/pair.h>
#include <limits>

#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Expand Down Expand Up @@ -48,42 +48,29 @@ inline _LIBCPP_HIDE_FROM_ABI typename __promote<_A1, _A2>::type hypot(_A1 __x, _

#if _LIBCPP_STD_VER >= 17
// Factors needed to determine if over-/underflow might happen for `std::hypot(x,y,z)`.
// returns [overflow_threshold, overflow_scale]
template <class _Real>
struct __hypot_factors {
_Real __threshold;
_Real __scale_xyz;
_Real __scale_M;
};

// Computes `__hypot_factors` needed to determine if over-/underflow might happen for `std::hypot(x,y,z)`.
// Returns: [underflow_factors, overflow_factors]
template <class _Real>
_LIBCPP_HIDE_FROM_ABI array<__math::__hypot_factors<_Real>, 2> __create_factors() {
_LIBCPP_HIDE_FROM_ABI std::pair<_Real, _Real> __hypot_factors() {
static_assert(std::numeric_limits<_Real>::is_iec559);

__math::__hypot_factors<_Real> __underflow, __overflow;
if constexpr (std::is_same_v<_Real, float>) {
static_assert(-125 == std::numeric_limits<_Real>::min_exponent);
static_assert(+128 == std::numeric_limits<_Real>::max_exponent);
__underflow = __math::__hypot_factors<_Real>{0x1.0p-62f, 0x1.0p70f, 0x1.0p-70f};
__overflow = __math::__hypot_factors<_Real>{0x1.0p62f, 0x1.0p-70f, 0x1.0p+70f};
return {0x1.0p+62f, 0x1.0p-70f};
} else if constexpr (std::is_same_v<_Real, double>) {
static_assert(-1021 == std::numeric_limits<_Real>::min_exponent);
static_assert(+1024 == std::numeric_limits<_Real>::max_exponent);
__underflow = __math::__hypot_factors<_Real>{0x1.0p-510, 0x1.0p600, 0x1.0p-600};
__overflow = __math::__hypot_factors<_Real>{0x1.0p510, 0x1.0p-600, 0x1.0p+600};
return {0x1.0p+510, 0x1.0p-600};
} else { // long double
static_assert(std::is_same_v<_Real, long double>);
if constexpr (sizeof(_Real) == sizeof(double))
return static_cast<std::array<__math::__hypot_factors<_Real>, 2>>(__math::__create_factors<double>());
return static_cast<std::pair<_Real, _Real>>(__math::__hypot_factors<double>());
else {
static_assert(-16'381 == std::numeric_limits<_Real>::min_exponent);
static_assert(+16'384 == std::numeric_limits<_Real>::max_exponent);
__underflow = __math::__hypot_factors<_Real>{0x1.0p-8'190l, 0x1.0p9'000l, 0x1.0p-9'000l};
__overflow = __math::__hypot_factors<_Real>{0x1.0p8'190l, 0x1.0p-9'000l, 0x1.0p+9'000l};
return {0x1.0p+8'190l, 0x1.0p-9'000l};
}
}
return {__underflow, __overflow};
}

// Computes the three-dimensional hypotenuse: `std::hypot(x,y,z)`.
Expand All @@ -92,21 +79,22 @@ _LIBCPP_HIDE_FROM_ABI array<__math::__hypot_factors<_Real>, 2> __create_factors(
// See https://github.com/llvm/llvm-project/issues/92782 for a detailed discussion and summary.
template <class _Real>
_LIBCPP_HIDE_FROM_ABI _Real __hypot(_Real __x, _Real __y, _Real __z) {
const auto [__underflow, __overflow] = __math::__create_factors<_Real>();
_Real __M = std::max({__math::fabs(__x), __math::fabs(__y), __math::fabs(__z)});
if (__M > __overflow.__threshold) { // x*x + y*y + z*z might overflow
__x *= __overflow.__scale_xyz;
__y *= __overflow.__scale_xyz;
__z *= __overflow.__scale_xyz;
__M = __overflow.__scale_M;
} else if (__M < __underflow.__threshold) { // x*x + y*y + z*z might underflow
__x *= __underflow.__scale_xyz;
__y *= __underflow.__scale_xyz;
__z *= __underflow.__scale_xyz;
__M = __underflow.__scale_M;
const _Real __max_abs = std::max({__math::fabs(__x), __math::fabs(__y), __math::fabs(__z)});
const auto [__overflow_threshold, __overflow_scale] = __math::__hypot_factors<_Real>();
_Real __scale;
if (__max_abs > __overflow_threshold) { // x*x + y*y + z*z might overflow
__scale = __overflow_scale;
__x *= __scale;
__y *= __scale;
__z *= __scale;
} else if (__max_abs < 1 / __overflow_threshold) { // x*x + y*y + z*z might underflow
__scale = 1 / __overflow_scale;
__x *= __scale;
__y *= __scale;
__z *= __scale;
} else
__M = 1;
return __M * __math::sqrt(__x * __x + __y * __y + __z * __z);
__scale = 1;
return __math::sqrt(__x * __x + __y * __y + __z * __z) / __scale;
}

inline _LIBCPP_HIDE_FROM_ABI float hypot(float __x, float __y, float __z) { return __math::__hypot(__x, __y, __z); }
Expand All @@ -125,7 +113,8 @@ _LIBCPP_HIDE_FROM_ABI typename __promote<_A1, _A2, _A3>::type hypot(_A1 __x, _A2
using __result_type = typename __promote<_A1, _A2, _A3>::type;
static_assert(!(
std::is_same_v<_A1, __result_type> && std::is_same_v<_A2, __result_type> && std::is_same_v<_A3, __result_type>));
return __math::__hypot(static_cast<__result_type>(__x), static_cast<__result_type>(__y), static_cast<__result_type>(__z));
return __math::__hypot(
static_cast<__result_type>(__x), static_cast<__result_type>(__y), static_cast<__result_type>(__z));
}
#endif

Expand Down

0 comments on commit cf378ed

Please sign in to comment.