[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
Tue May 24 09:58:35 PDT 2022


hans updated this revision to Diff 431705.
hans marked an inline comment as done.
hans added a comment.

Rebase to kick the CI.


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,6 +14,7 @@
 #include <__algorithm/min_element.h>
 #include <__algorithm/partial_sort.h>
 #include <__algorithm/unwrap_iter.h>
+#include <__bits>
 #include <__config>
 #include <__utility/swap.h>
 #include <memory>
@@ -498,6 +499,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.431705.patch
Type: text/x-patch
Size: 1053 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20220524/90af1161/attachment.bin>


More information about the libcxx-commits mailing list