diff options
author | Victor Khimenko <khim@google.com> | 2023-12-13 19:14:50 +0000 |
---|---|---|
committer | Victor Khimenko <khim@google.com> | 2023-12-13 23:31:23 +0000 |
commit | 4ad3aa097985122b7cd928ebd81cd40d1c0472ec (patch) | |
tree | 7637c760ac82ea0c04e9c6934aa8adea53a9990a | |
parent | cdbf77f85400050bbb295c256f3ccc74f11158ae (diff) | |
download | binary_translation-4ad3aa097985122b7cd928ebd81cd40d1c0472ec.tar.gz |
Implement BitUtilLog2 efficiently
Test: m berberis_all
Change-Id: If9bf80a942bb3220ecdb320338a63e930ee6ec6a
-rw-r--r-- | base/include/berberis/base/bit_util.h | 5 |
1 files changed, 3 insertions, 2 deletions
diff --git a/base/include/berberis/base/bit_util.h b/base/include/berberis/base/bit_util.h index e3610dde..4fb08470 100644 --- a/base/include/berberis/base/bit_util.h +++ b/base/include/berberis/base/bit_util.h @@ -70,8 +70,9 @@ constexpr bool IsAligned(T* p, size_t align) { template <typename T> constexpr T BitUtilLog2(T x) { static_assert(std::is_integral_v<T>, "Log2: T must be integral"); - DCHECK(IsPowerOf2(x)); - return x == 1 ? 0 : BitUtilLog2(x >> 1) + 1; + CHECK(IsPowerOf2(x)); + // TODO(b/260725458): Use std::countr_zero after C++20 becomes available + return __builtin_ctz(x); } // Verify that argument value fits into a target. |