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

via libcxx-commits libcxx-commits at lists.llvm.org
Tue May 5 06:57:59 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 01/10] [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 02/10] 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 03/10] 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 04/10] 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 05/10] 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 5dee0d69d8e9b0395388706d637b19dd7065c495 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 06/10] 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..119fe85f9c2d7 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 + (unsigned)(__a > __b)) / 2;
   } else {
     using _Up                = make_unsigned_t<_Tp>;
     constexpr _Up __bitshift = numeric_limits<_Up>::digits - 1;

>From 330b26c4e476e2b3a7f838284f756187a44ca44d Mon Sep 17 00:00:00 2001
From: serge-sans-paille <sguelton at mozilla.com>
Date: Sun, 19 Apr 2026 22:34:55 +0200
Subject: [PATCH 07/10] fixup! fixup! fixup! fixup! fixup! fixup! [libc++]
 Improve performance of std::midpoint for small types

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

diff --git a/libcxx/include/__numeric/midpoint.h b/libcxx/include/__numeric/midpoint.h
index 119fe85f9c2d7..e2bfb98950625 100644
--- a/libcxx/include/__numeric/midpoint.h
+++ b/libcxx/include/__numeric/midpoint.h
@@ -36,9 +36,17 @@ 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 defined(_LIBCPP_COMPILER_CLANG_BASED)
+  if constexpr (is_unsigned_v<_Tp>  {
+    using _Ip = unsigned _BitInt(sizeof(_Tp) * 8 + 1);
+    return (_Ip(__a) + _Ip(__b) + _Ip(__a > __b)) / 2;
+  }
+#  else
   if constexpr (is_unsigned_v<_Tp> && sizeof(_Tp) < sizeof(unsigned)) {
     return ((unsigned)__a + (unsigned)__b + (unsigned)(__a > __b)) / 2;
-  } else {
+  }
+#  endif
+  else {
     using _Up                = make_unsigned_t<_Tp>;
     constexpr _Up __bitshift = numeric_limits<_Up>::digits - 1;
 

>From b7f46c34f8d57766cb50803dc0c0ec34d3a6451f Mon Sep 17 00:00:00 2001
From: serge-sans-paille <sguelton at mozilla.com>
Date: Fri, 24 Apr 2026 15:50:01 +0200
Subject: [PATCH 08/10] fixup! fixup! fixup! fixup! fixup! fixup! fixup!
 [libc++] Improve performance of std::midpoint for small types

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

diff --git a/libcxx/include/__numeric/midpoint.h b/libcxx/include/__numeric/midpoint.h
index e2bfb98950625..6b2c3d5d98993 100644
--- a/libcxx/include/__numeric/midpoint.h
+++ b/libcxx/include/__numeric/midpoint.h
@@ -41,10 +41,6 @@ _LIBCPP_HIDE_FROM_ABI constexpr _Tp midpoint(_Tp __a, _Tp __b) noexcept _LIBCPP_
     using _Ip = unsigned _BitInt(sizeof(_Tp) * 8 + 1);
     return (_Ip(__a) + _Ip(__b) + _Ip(__a > __b)) / 2;
   }
-#  else
-  if constexpr (is_unsigned_v<_Tp> && sizeof(_Tp) < sizeof(unsigned)) {
-    return ((unsigned)__a + (unsigned)__b + (unsigned)(__a > __b)) / 2;
-  }
 #  endif
   else {
     using _Up                = make_unsigned_t<_Tp>;

>From dcf32121f52c89ea02b02a4e0a30a6e052f6fc52 Mon Sep 17 00:00:00 2001
From: serge-sans-paille <sguelton at mozilla.com>
Date: Tue, 5 May 2026 12:08:48 +0200
Subject: [PATCH 09/10] fixup! fixup! fixup! 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 6b2c3d5d98993..571a41f34c678 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 defined(_LIBCPP_COMPILER_CLANG_BASED)
-  if constexpr (is_unsigned_v<_Tp>  {
+  if constexpr (is_unsigned_v<_Tp>)  {
     using _Ip = unsigned _BitInt(sizeof(_Tp) * 8 + 1);
     return (_Ip(__a) + _Ip(__b) + _Ip(__a > __b)) / 2;
   }

>From 27dadb59caac40839c39596e134faa78d443ebbc Mon Sep 17 00:00:00 2001
From: serge-sans-paille <sguelton at mozilla.com>
Date: Tue, 5 May 2026 15:57:15 +0200
Subject: [PATCH 10/10] fixup! fixup! fixup! fixup! fixup! fixup! fixup! fixup!
 fixup! [libc++] Improve performance of std::midpoint for small types

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

diff --git a/libcxx/include/__numeric/midpoint.h b/libcxx/include/__numeric/midpoint.h
index 571a41f34c678..4b7a87436a873 100644
--- a/libcxx/include/__numeric/midpoint.h
+++ b/libcxx/include/__numeric/midpoint.h
@@ -40,9 +40,9 @@ _LIBCPP_HIDE_FROM_ABI constexpr _Tp midpoint(_Tp __a, _Tp __b) noexcept _LIBCPP_
   if constexpr (is_unsigned_v<_Tp>)  {
     using _Ip = unsigned _BitInt(sizeof(_Tp) * 8 + 1);
     return (_Ip(__a) + _Ip(__b) + _Ip(__a > __b)) / 2;
-  }
+  } else
 #  endif
-  else {
+  {
     using _Up                = make_unsigned_t<_Tp>;
     constexpr _Up __bitshift = numeric_limits<_Up>::digits - 1;
 



More information about the libcxx-commits mailing list