diff options
author | rilysh <nightquick@proton.me> | 2024-02-17 21:26:02 +0530 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-02-17 16:56:02 +0100 |
commit | 715567d03771c437e26b16fbc71c30c4474b8895 (patch) | |
tree | 49ba1409392d8b50d06c654b5c8f6aff38f25c90 | |
parent | 97eff26d0ca4d187a5efb8534af484dbb68bce30 (diff) | |
download | llvm-715567d03771c437e26b16fbc71c30c4474b8895.zip llvm-715567d03771c437e26b16fbc71c30c4474b8895.tar.gz llvm-715567d03771c437e26b16fbc71c30c4474b8895.tar.bz2 |
[libc++] simplify the midpoint function (#81717)
Right now we've a nested ternary for the midpoint function, but this can
be simplified a bit more, using if statements. This also slightly
increases the readability of that function.
-rw-r--r-- | libcxx/include/__numeric/midpoint.h | 18 |
1 files changed, 10 insertions, 8 deletions
diff --git a/libcxx/include/__numeric/midpoint.h b/libcxx/include/__numeric/midpoint.h index 5d715c2..5ef30d4 100644 --- a/libcxx/include/__numeric/midpoint.h +++ b/libcxx/include/__numeric/midpoint.h @@ -67,14 +67,16 @@ template <class _Fp> _LIBCPP_HIDE_FROM_ABI constexpr enable_if_t<is_floating_point_v<_Fp>, _Fp> midpoint(_Fp __a, _Fp __b) noexcept { constexpr _Fp __lo = numeric_limits<_Fp>::min() * 2; constexpr _Fp __hi = numeric_limits<_Fp>::max() / 2; - return std::__fp_abs(__a) <= __hi && std::__fp_abs(__b) <= __hi - ? // typical case: overflow is impossible - (__a + __b) / 2 - : // always correctly rounded - std::__fp_abs(__a) < __lo ? __a + __b / 2 : // not safe to halve a - std::__fp_abs(__b) < __lo ? __a / 2 + __b - : // not safe to halve b - __a / 2 + __b / 2; // otherwise correctly rounded + + // typical case: overflow is impossible + if (std::__fp_abs(__a) <= __hi && std::__fp_abs(__b) <= __hi) + return (__a + __b) / 2; // always correctly rounded + if (std::__fp_abs(__a) < __lo) + return __a + __b / 2; // not safe to halve a + if (std::__fp_abs(__b) < __lo) + return __a / 2 + __b; // not safe to halve b + + return __a / 2 + __b / 2; // otherwise correctly rounded } #endif // _LIBCPP_STD_VER >= 20 |