[libcxx-commits] [PATCH] D134625: [libc++] Fix the rotate direction used in countl_zero()

Louis Dionne via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Tue Sep 12 07:50:08 PDT 2023


ldionne updated this revision to Diff 556572.
ldionne retitled this revision from "Summary:
bug fix! Rotation direction on `__countl_zero()`
probably unnoticed because only affects rare cases
(does not affect 128 bit ints because rotation is effectively swap)
(does not affect integers of sizes less or equal to 64bits)" to "[libc++] Fix the rotate direction used in countl_zero()".
ldionne edited the summary of this revision.
ldionne added a comment.

Rebase and fix CI. I can't add tests because this isn't technically a "live bug" -- but the bug would trigger if we started supporting 256 bit integers.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D134625

Files:
  libcxx/include/__bit/countl.h
  libcxx/include/__bit/rotate.h


Index: libcxx/include/__bit/rotate.h
===================================================================
--- libcxx/include/__bit/rotate.h
+++ libcxx/include/__bit/rotate.h
@@ -35,11 +35,16 @@
   return (__t >> (__cnt % __dig)) | (__t << (__dig - (__cnt % __dig)));
 }
 
+template <class _Tp>
+_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Tp __rotl(_Tp __t, int __cnt) _NOEXCEPT {
+  return std::__rotr(__t, -__cnt);
+}
+
 #if _LIBCPP_STD_VER >= 20
 
 template <__libcpp_unsigned_integer _Tp>
 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp rotl(_Tp __t, int __cnt) noexcept {
-  return std::__rotr(__t, -__cnt);
+  return std::__rotl(__t, __cnt);
 }
 
 template <__libcpp_unsigned_integer _Tp>
Index: libcxx/include/__bit/countl.h
===================================================================
--- libcxx/include/__bit/countl.h
+++ libcxx/include/__bit/countl.h
@@ -74,7 +74,7 @@
         int __iter = 0;
         const unsigned int __ulldigits = numeric_limits<unsigned long long>::digits;
         while (true) {
-            __t = std::__rotr(__t, __ulldigits);
+            __t = std::__rotl(__t, __ulldigits);
             if ((__iter = std::__countl_zero(static_cast<unsigned long long>(__t))) != __ulldigits)
                 break;
             __ret += __iter;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D134625.556572.patch
Type: text/x-patch
Size: 1287 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20230912/5c360dbf/attachment-0001.bin>


More information about the libcxx-commits mailing list