[libcxx-commits] [libcxx] 865ad6b - [libc++] Use __libcpp_clz for a tighter __log2i function

Hans Wennborg via libcxx-commits libcxx-commits at lists.llvm.org
Fri May 27 09:58:28 PDT 2022


Author: Hans Wennborg
Date: 2022-05-27T18:53:59+02:00
New Revision: 865ad6bd21656aef5fabd2f4603ad4f759df6e62

URL: https://github.com/llvm/llvm-project/commit/865ad6bd21656aef5fabd2f4603ad4f759df6e62
DIFF: https://github.com/llvm/llvm-project/commit/865ad6bd21656aef5fabd2f4603ad4f759df6e62.diff

LOG: [libc++] Use __libcpp_clz for a tighter __log2i function

While looking at D113413 I noticed that __log2i could perhaps be
improved to be both slightly smaller and faster.

projects/libcxx/benchmarks/sort.libcxx.out --benchmark_filter=BM_Sort_uint32_QuickSortAdversary*
suggests this is performance neutral, but it shaves a few bytes off the
benchmark binary, and even more off a Chromium build.

Differential revision: https://reviews.llvm.org/D125958

Added: 
    

Modified: 
    libcxx/include/__algorithm/sort.h

Removed: 
    


################################################################################
diff  --git a/libcxx/include/__algorithm/sort.h b/libcxx/include/__algorithm/sort.h
index d108dc31bd299..a6ff5e3ac54ad 100644
--- a/libcxx/include/__algorithm/sort.h
+++ b/libcxx/include/__algorithm/sort.h
@@ -14,8 +14,10 @@
 #include <__algorithm/min_element.h>
 #include <__algorithm/partial_sort.h>
 #include <__algorithm/unwrap_iter.h>
+#include <__bits>
 #include <__config>
 #include <__utility/swap.h>
+#include <climits>
 #include <memory>
 
 #if defined(_LIBCPP_DEBUG_RANDOMIZE_UNSPECIFIED_STABILITY)
@@ -498,6 +500,15 @@ void __introsort(_RandomAccessIterator __first, _RandomAccessIterator __last, _C
 
 template <typename _Number>
 inline _LIBCPP_HIDE_FROM_ABI _Number __log2i(_Number __n) {
+  if (__n == 0)
+    return 0;
+  if (sizeof(__n) <= sizeof(unsigned))
+    return sizeof(unsigned) * CHAR_BIT - 1 - __libcpp_clz(static_cast<unsigned>(__n));
+  if (sizeof(__n) <= sizeof(unsigned long))
+    return sizeof(unsigned long) * CHAR_BIT - 1 - __libcpp_clz(static_cast<unsigned long>(__n));
+  if (sizeof(__n) <= sizeof(unsigned long long))
+    return sizeof(unsigned long long) * CHAR_BIT - 1 - __libcpp_clz(static_cast<unsigned long long>(__n));
+
   _Number __log2 = 0;
   while (__n > 1) {
     __log2++;


        


More information about the libcxx-commits mailing list