[libcxx-commits] [libcxx] [libc++] simplify the midpoint function (PR #81717)
via libcxx-commits
libcxx-commits at lists.llvm.org
Thu Feb 15 11:20:10 PST 2024
https://github.com/rilysh updated https://github.com/llvm/llvm-project/pull/81717
>From 6f38fc2c896a14db13ae440d48ec3dc114ae9faf Mon Sep 17 00:00:00 2001
From: rilysh <nightquick at proton.me>
Date: Wed, 14 Feb 2024 14:57:11 +0530
Subject: [PATCH 1/2] [libc++] simplify the midpoint function
Right now we've a nested ternary for the midpoint function, but this
can be simplified a bit more, using if..else. This also slightly
increases the readability of that function.
---
libcxx/include/__numeric/midpoint.h | 18 ++++++++++--------
1 file changed, 10 insertions(+), 8 deletions(-)
diff --git a/libcxx/include/__numeric/midpoint.h b/libcxx/include/__numeric/midpoint.h
index 5d715c21d8eaca..4232610ce27be4 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
+ else if (std::__fp_abs(__a) < __lo)
+ return __a + __b / 2; // not safe to halve a
+ else if (std::__fp_abs(__b) < __lo)
+ return __a / 2 + __b; // not safe to halve b
+ else
+ return __a / 2 + __b / 2; // otherwise correctly rounded
}
#endif // _LIBCPP_STD_VER >= 20
>From 366b1d82c6370f77aa02356ed037e23f0a53ecb9 Mon Sep 17 00:00:00 2001
From: rilysh <nightquick at proton.me>
Date: Fri, 16 Feb 2024 00:47:51 +0530
Subject: [PATCH 2/2] [libc++] midpoint.h: use LLVM style condition block
---
libcxx/include/__numeric/midpoint.h | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/libcxx/include/__numeric/midpoint.h b/libcxx/include/__numeric/midpoint.h
index 4232610ce27be4..5ef30d4ec50f5a 100644
--- a/libcxx/include/__numeric/midpoint.h
+++ b/libcxx/include/__numeric/midpoint.h
@@ -71,12 +71,12 @@ _LIBCPP_HIDE_FROM_ABI constexpr enable_if_t<is_floating_point_v<_Fp>, _Fp> midpo
// typical case: overflow is impossible
if (std::__fp_abs(__a) <= __hi && std::__fp_abs(__b) <= __hi)
return (__a + __b) / 2; // always correctly rounded
- else if (std::__fp_abs(__a) < __lo)
+ if (std::__fp_abs(__a) < __lo)
return __a + __b / 2; // not safe to halve a
- else if (std::__fp_abs(__b) < __lo)
+ if (std::__fp_abs(__b) < __lo)
return __a / 2 + __b; // not safe to halve b
- else
- return __a / 2 + __b / 2; // otherwise correctly rounded
+
+ return __a / 2 + __b / 2; // otherwise correctly rounded
}
#endif // _LIBCPP_STD_VER >= 20
More information about the libcxx-commits
mailing list