[libcxx-commits] [libcxx] [libc++] Improve performance of std::midpoint for small types (PR #192804)

via libcxx-commits libcxx-commits at lists.llvm.org
Sun Apr 19 00:20:43 PDT 2026


https://github.com/serge-sans-paille updated https://github.com/llvm/llvm-project/pull/192804

>From a604431a4c78776b2db85fa0429e0573a76bd67e Mon Sep 17 00:00:00 2001
From: serge-sans-paille <sguelton at mozilla.com>
Date: Sat, 18 Apr 2026 20:08:10 +0200
Subject: [PATCH 1/6] [libc++] Improve performance of std::midpoint for small
 types

---
 libcxx/include/__numeric/midpoint.h | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)

diff --git a/libcxx/include/__numeric/midpoint.h b/libcxx/include/__numeric/midpoint.h
index d8e73ab8cad36..736e2b152cd38 100644
--- a/libcxx/include/__numeric/midpoint.h
+++ b/libcxx/include/__numeric/midpoint.h
@@ -36,14 +36,18 @@ template <class _Tp>
 [[nodiscard]]
 _LIBCPP_HIDE_FROM_ABI constexpr _Tp midpoint(_Tp __a, _Tp __b) noexcept _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK {
   using _Up                = make_unsigned_t<_Tp>;
-  constexpr _Up __bitshift = numeric_limits<_Up>::digits - 1;
+  if constexpr (sizeof(_Tp) < sizeof(unsigned)) {
+    return (static_cast<unsigned>(__a) + static_cast<unsigned>(__b)) / 2;
+  } else {
+    constexpr _Up __bitshift = numeric_limits<_Up>::digits - 1;
 
-  _Up __diff     = _Up(__b) - _Up(__a);
-  _Up __sign_bit = __b < __a;
+    _Up __diff     = _Up(__b) - _Up(__a);
+    _Up __sign_bit = __b < __a;
 
-  _Up __half_diff = (__diff / 2) + (__sign_bit << __bitshift) + (__sign_bit & __diff);
+    _Up __half_diff = (__diff / 2) + (__sign_bit << __bitshift) + (__sign_bit & __diff);
 
-  return __a + __half_diff;
+    return __a + __half_diff;
+  }
 }
 
 template <class _Tp>

>From 24123590a0d64d80bacb9740c5b262763681eeb3 Mon Sep 17 00:00:00 2001
From: serge-sans-paille <sguelton at mozilla.com>
Date: Sat, 18 Apr 2026 22:11:10 +0200
Subject: [PATCH 2/6] fixup! [libc++] Improve performance of std::midpoint for
 small types

---
 libcxx/include/__numeric/midpoint.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/libcxx/include/__numeric/midpoint.h b/libcxx/include/__numeric/midpoint.h
index 736e2b152cd38..aa6d7096878f0 100644
--- a/libcxx/include/__numeric/midpoint.h
+++ b/libcxx/include/__numeric/midpoint.h
@@ -37,7 +37,8 @@ template <class _Tp>
 _LIBCPP_HIDE_FROM_ABI constexpr _Tp midpoint(_Tp __a, _Tp __b) noexcept _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK {
   using _Up                = make_unsigned_t<_Tp>;
   if constexpr (sizeof(_Tp) < sizeof(unsigned)) {
-    return (static_cast<unsigned>(__a) + static_cast<unsigned>(__b)) / 2;
+    using _Ip = conditional_t<is_signed_v<_Tp>, signed, unsigned>;
+    return (static_cast<_Ip>(__a) + static_cast<_Ip>(__b)) / 2;
   } else {
     constexpr _Up __bitshift = numeric_limits<_Up>::digits - 1;
 

>From 22be4aa8bc72308c2aec9f64876146a30ec9ccb1 Mon Sep 17 00:00:00 2001
From: serge-sans-paille <sguelton at mozilla.com>
Date: Sat, 18 Apr 2026 22:34:58 +0200
Subject: [PATCH 3/6] fixup! fixup! [libc++] Improve performance of
 std::midpoint for small types

---
 libcxx/include/__numeric/midpoint.h | 16 ++++------------
 1 file changed, 4 insertions(+), 12 deletions(-)

diff --git a/libcxx/include/__numeric/midpoint.h b/libcxx/include/__numeric/midpoint.h
index aa6d7096878f0..4b47523e565a8 100644
--- a/libcxx/include/__numeric/midpoint.h
+++ b/libcxx/include/__numeric/midpoint.h
@@ -35,19 +35,11 @@ template <class _Tp>
   requires(is_integral_v<_Tp> && !is_same_v<remove_cv_t<_Tp>, bool>)
 [[nodiscard]]
 _LIBCPP_HIDE_FROM_ABI constexpr _Tp midpoint(_Tp __a, _Tp __b) noexcept _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK {
-  using _Up                = make_unsigned_t<_Tp>;
-  if constexpr (sizeof(_Tp) < sizeof(unsigned)) {
-    using _Ip = conditional_t<is_signed_v<_Tp>, signed, unsigned>;
-    return (static_cast<_Ip>(__a) + static_cast<_Ip>(__b)) / 2;
+  if constexpr (is_unsigned_v<_Tp>) {
+    return ((__a ^ __b) >> 1) + (__a & __b);
   } else {
-    constexpr _Up __bitshift = numeric_limits<_Up>::digits - 1;
-
-    _Up __diff     = _Up(__b) - _Up(__a);
-    _Up __sign_bit = __b < __a;
-
-    _Up __half_diff = (__diff / 2) + (__sign_bit << __bitshift) + (__sign_bit & __diff);
-
-    return __a + __half_diff;
+    _Tp __t = ((__a ^ __b) >> 1) + (__a & __b);
+    return __t + (_Tp(__t < 0) & (__a ^ __b) & _Tp(1));
   }
 }
 

>From 6c39724828954b13cc8520da4e5d13e2d4750efa Mon Sep 17 00:00:00 2001
From: serge-sans-paille <sguelton at mozilla.com>
Date: Sat, 18 Apr 2026 23:13:14 +0200
Subject: [PATCH 4/6] fixup! fixup! fixup! [libc++] Improve performance of
 std::midpoint for small types

---
 libcxx/include/__numeric/midpoint.h | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)

diff --git a/libcxx/include/__numeric/midpoint.h b/libcxx/include/__numeric/midpoint.h
index 4b47523e565a8..43dc8d45e2e1c 100644
--- a/libcxx/include/__numeric/midpoint.h
+++ b/libcxx/include/__numeric/midpoint.h
@@ -35,11 +35,18 @@ template <class _Tp>
   requires(is_integral_v<_Tp> && !is_same_v<remove_cv_t<_Tp>, bool>)
 [[nodiscard]]
 _LIBCPP_HIDE_FROM_ABI constexpr _Tp midpoint(_Tp __a, _Tp __b) noexcept _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK {
-  if constexpr (is_unsigned_v<_Tp>) {
-    return ((__a ^ __b) >> 1) + (__a & __b);
+  if constexpr (is_unsigned_v<_Tp> && sizeof(_Tp) < sizeof(unsigned)) {
+    return ((unsigned)__a + (unsigned)__b) / 2;
   } else {
-    _Tp __t = ((__a ^ __b) >> 1) + (__a & __b);
-    return __t + (_Tp(__t < 0) & (__a ^ __b) & _Tp(1));
+    using _Up                = make_unsigned_t<_Tp>;
+    constexpr _Up __bitshift = numeric_limits<_Up>::digits - 1;
+
+    _Up __diff     = _Up(__b) - _Up(__a);
+    _Up __sign_bit = __b < __a;
+
+    _Up __half_diff = (__diff / 2) + (__sign_bit << __bitshift) + (__sign_bit & __diff);
+
+    return __a + __half_diff;
   }
 }
 

>From 540ecc6919128aa374dc4dcb9200871c92a75051 Mon Sep 17 00:00:00 2001
From: serge-sans-paille <sguelton at mozilla.com>
Date: Sun, 19 Apr 2026 09:00:03 +0200
Subject: [PATCH 5/6] fixup! fixup! fixup! fixup! [libc++] Improve performance
 of std::midpoint for small types

---
 libcxx/include/__numeric/midpoint.h | 1 +
 1 file changed, 1 insertion(+)

diff --git a/libcxx/include/__numeric/midpoint.h b/libcxx/include/__numeric/midpoint.h
index 43dc8d45e2e1c..3858c41fdf8df 100644
--- a/libcxx/include/__numeric/midpoint.h
+++ b/libcxx/include/__numeric/midpoint.h
@@ -16,6 +16,7 @@
 #include <__type_traits/is_integral.h>
 #include <__type_traits/is_object.h>
 #include <__type_traits/is_same.h>
+#include <__type_traits/is_unsigned.h>
 #include <__type_traits/is_void.h>
 #include <__type_traits/make_unsigned.h>
 #include <__type_traits/remove_cv.h>

>From 1bea2bb3ae8b2b80a3245c1146ba489450f7a340 Mon Sep 17 00:00:00 2001
From: serge-sans-paille <sguelton at mozilla.com>
Date: Sun, 19 Apr 2026 09:20:19 +0200
Subject: [PATCH 6/6] fixup! fixup! fixup! fixup! fixup! [libc++] Improve
 performance of std::midpoint for small types

---
 libcxx/include/__numeric/midpoint.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libcxx/include/__numeric/midpoint.h b/libcxx/include/__numeric/midpoint.h
index 3858c41fdf8df..ca35aa7ecc426 100644
--- a/libcxx/include/__numeric/midpoint.h
+++ b/libcxx/include/__numeric/midpoint.h
@@ -37,7 +37,7 @@ template <class _Tp>
 [[nodiscard]]
 _LIBCPP_HIDE_FROM_ABI constexpr _Tp midpoint(_Tp __a, _Tp __b) noexcept _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK {
   if constexpr (is_unsigned_v<_Tp> && sizeof(_Tp) < sizeof(unsigned)) {
-    return ((unsigned)__a + (unsigned)__b) / 2;
+    return ((unsigned)__a + (unsigned)__b + 1u) / 2;
   } else {
     using _Up                = make_unsigned_t<_Tp>;
     constexpr _Up __bitshift = numeric_limits<_Up>::digits - 1;



More information about the libcxx-commits mailing list