[libcxx-commits] [PATCH] D134625: 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)

Armando Martín via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Sun Sep 25 23:15:44 PDT 2022


Delta-dev-99 created this revision.
Herald added a project: All.
Delta-dev-99 requested review of this revision.
Herald added a project: libc++.
Herald added a subscriber: libcxx-commits.
Herald added a reviewer: libc++.

__countl_zero should count zeros (bits) starting from the left.
The bug fixed by this only affects big integers that cannot be handled in one go.
The idea is to successively take blocks from the left and apply the opperation recursively.
To do that we rotate to the left and the left-most block gets wrapped-around to the right-most position,
where we can apply the operation to it alone (by casting it to a smaller integer).

The current rotation used is to the right (64-bits), which works for 128-bits because it just swaps the halves.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D134625

Files:
  libcxx/include/bit


Index: libcxx/include/bit
===================================================================
--- libcxx/include/bit
+++ libcxx/include/bit
@@ -114,7 +114,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.462812.patch
Type: text/x-patch
Size: 550 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20220926/12dc58d1/attachment.bin>


More information about the libcxx-commits mailing list