[libcxx-commits] [PATCH] D69459: Optimize std::midpoint for integers

Jorg Brown via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Mon Nov 4 19:33:34 PST 2019


This revision was automatically updated to reflect the committed changes.
Closed by commit rG586952f4cefd: Optimize std::midpoint for integers (authored by jorgbrown).
Herald added a subscriber: christof.
Herald added a project: libc++.

Changed prior to commit:
  https://reviews.llvm.org/D69459?vs=226530&id=227810#toc

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D69459/new/

https://reviews.llvm.org/D69459

Files:
  libcxx/include/numeric


Index: libcxx/include/numeric
===================================================================
--- libcxx/include/numeric
+++ libcxx/include/numeric
@@ -532,17 +532,14 @@
 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK
 {
     using _Up = std::make_unsigned_t<_Tp>;
+    constexpr _Up __bitshift = std::numeric_limits<_Up>::digits - 1;
 
-    int __sign = 1;
-    _Up __m = __a;
-    _Up __M = __b;
-    if (__a > __b)
-    {
-        __sign = -1;
-        __m = __b;
-        __M = __a;
-    }
-     return __a + __sign * _Tp(_Up(__M-__m) >> 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;
 }
 
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D69459.227810.patch
Type: text/x-patch
Size: 752 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20191105/4221df9d/attachment.bin>


More information about the libcxx-commits mailing list