[libcxx-commits] [PATCH] D120444: [libc++] [LWG3656] Update the return type of std::bit_width
Arthur O'Dwyer via Phabricator via libcxx-commits
libcxx-commits at lists.llvm.org
Wed Feb 23 17:20:48 PST 2022
Quuxplusone created this revision.
Quuxplusone added reviewers: jwakely, ldionne, Mordante, philnik, var-const, jloser, libc++, CaseyCarter.
Quuxplusone added a project: libc++.
Quuxplusone requested review of this revision.
Herald added a subscriber: libcxx-commits.
Herald added 1 blocking reviewer(s): libc++.
Fixes LWG3656, "Inconsistent bit operations returning a count".
https://cplusplus.github.io/LWG/issue3656
The fix has been approved for C++23 and left to vendors' discretion in C++20 (but it sounds like everyone's on the same page that of course it should be DR'ed back to C++20 too).
As usual, this PR is kinda premature because the LWG issue's resolution hasn't been formally adopted yet, so there's nothing in Cxx2bIssues.csv for it yet, but I figured I might as well prepare this PR to go in absolutely as soon as @ldionne's comfortable with it processwise. :)
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D120444
Files:
libcxx/include/bit
libcxx/test/std/numerics/bit/bit.pow.two/bit_width.pass.cpp
Index: libcxx/test/std/numerics/bit/bit.pow.two/bit_width.pass.cpp
===================================================================
--- libcxx/test/std/numerics/bit/bit.pow.two/bit_width.pass.cpp
+++ libcxx/test/std/numerics/bit/bit.pow.two/bit_width.pass.cpp
@@ -9,7 +9,7 @@
// UNSUPPORTED: c++03, c++11, c++14, c++17
// template <class T>
-// constexpr T bit_width(T x) noexcept;
+// constexpr int bit_width(T x) noexcept;
// Constraints: T is an unsigned integer type
// Returns: If x == 0, 0; otherwise one plus the base-2 logarithm of x, with any fractional part discarded.
@@ -29,28 +29,28 @@
template <class T>
constexpr bool test()
{
- ASSERT_SAME_TYPE(decltype(std::bit_width(T())), T);
+ ASSERT_SAME_TYPE(decltype(std::bit_width(T())), int);
ASSERT_NOEXCEPT(std::bit_width(T()));
T max = std::numeric_limits<T>::max();
- assert(std::bit_width(T(0)) == T(0));
- assert(std::bit_width(T(1)) == T(1));
- assert(std::bit_width(T(2)) == T(2));
- assert(std::bit_width(T(3)) == T(2));
- assert(std::bit_width(T(4)) == T(3));
- assert(std::bit_width(T(5)) == T(3));
- assert(std::bit_width(T(6)) == T(3));
- assert(std::bit_width(T(7)) == T(3));
- assert(std::bit_width(T(8)) == T(4));
- assert(std::bit_width(T(9)) == T(4));
- assert(std::bit_width(T(125)) == T(7));
- assert(std::bit_width(T(126)) == T(7));
- assert(std::bit_width(T(127)) == T(7));
- assert(std::bit_width(T(128)) == T(8));
- assert(std::bit_width(T(129)) == T(8));
- assert(std::bit_width(T(130)) == T(8));
- assert(std::bit_width(T(max - 1)) == T(std::numeric_limits<T>::digits));
- assert(std::bit_width(max) == T(std::numeric_limits<T>::digits));
+ assert(std::bit_width(T(0)) == 0);
+ assert(std::bit_width(T(1)) == 1);
+ assert(std::bit_width(T(2)) == 2);
+ assert(std::bit_width(T(3)) == 2);
+ assert(std::bit_width(T(4)) == 3);
+ assert(std::bit_width(T(5)) == 3);
+ assert(std::bit_width(T(6)) == 3);
+ assert(std::bit_width(T(7)) == 3);
+ assert(std::bit_width(T(8)) == 4);
+ assert(std::bit_width(T(9)) == 4);
+ assert(std::bit_width(T(125)) == 7);
+ assert(std::bit_width(T(126)) == 7);
+ assert(std::bit_width(T(127)) == 7);
+ assert(std::bit_width(T(128)) == 8);
+ assert(std::bit_width(T(129)) == 8);
+ assert(std::bit_width(T(130)) == 8);
+ assert(std::bit_width(T(max - 1)) == std::numeric_limits<T>::digits);
+ assert(std::bit_width(max) == std::numeric_limits<T>::digits);
#ifndef TEST_HAS_NO_INT128
if constexpr (std::is_same_v<T, __uint128_t>) {
Index: libcxx/include/bit
===================================================================
--- libcxx/include/bit
+++ libcxx/include/bit
@@ -30,7 +30,7 @@
template <class T>
constexpr T bit_floor(T x) noexcept; // C++20
template <class T>
- constexpr T bit_width(T x) noexcept; // C++20
+ constexpr int bit_width(T x) noexcept; // C++20
// [bit.rotate], rotating
template<class T>
@@ -322,7 +322,7 @@
template <class _Tp>
_LIBCPP_INLINE_VISIBILITY constexpr
-enable_if_t<__libcpp_is_unsigned_integer<_Tp>::value, _Tp>
+enable_if_t<__libcpp_is_unsigned_integer<_Tp>::value, int>
bit_width(_Tp __t) noexcept
{
return __t == 0 ? 0 : __bit_log2(__t) + 1;
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D120444.410979.patch
Type: text/x-patch
Size: 3304 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20220224/d14e122d/attachment.bin>
More information about the libcxx-commits
mailing list