[libcxx-commits] [PATCH] D125958: [libc++] Use __libcpp_clz for a tighter __log2i function

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


This revision was automatically updated to reflect the committed changes.
Closed by commit rG865ad6bd2165: [libc++] Use __libcpp_clz for a tighter __log2i function (authored by hans).

Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D125958

Files:
  libcxx/include/__algorithm/sort.h


Index: libcxx/include/__algorithm/sort.h
===================================================================
--- libcxx/include/__algorithm/sort.h
+++ 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 @@
 
 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++;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D125958.432594.patch
Type: text/x-patch
Size: 1124 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20220527/7e429834/attachment.bin>


More information about the libcxx-commits mailing list