diff --git a/stl/inc/bit b/stl/inc/bit index 1ac47cd6a99..bacde1abff9 100644 --- a/stl/inc/bit +++ b/stl/inc/bit @@ -82,8 +82,8 @@ _NODISCARD constexpr _Ty bit_floor(const _Ty _Val) noexcept { } template , int> = 0> -_NODISCARD constexpr _Ty bit_width(const _Ty _Val) noexcept { - return static_cast<_Ty>(numeric_limits<_Ty>::digits - _STD countl_zero(_Val)); +_NODISCARD constexpr int bit_width(const _Ty _Val) noexcept { + return numeric_limits<_Ty>::digits - _STD countl_zero(_Val); } template , int> = 0> diff --git a/stl/inc/format b/stl/inc/format index f189bfa7dc8..bd0dfea89f5 100644 --- a/stl/inc/format +++ b/stl/inc/format @@ -2939,7 +2939,7 @@ _NODISCARD _OutputIt _Fmt_write( // Add 3 to the bit width so we always round up on the division. // Divide that by the amount of bits a hexit represents (log2(16) = log2(2^4) = 4). // Add 2 for the 0x prefix. - _Width = static_cast(2 + (_STD bit_width(reinterpret_cast(_Value)) + 3) / 4); + _Width = 2 + (_STD bit_width(reinterpret_cast(_Value)) + 3) / 4; } return _Write_aligned(_STD move(_Out), _Width, _Specs, _Fmt_align::_Right, diff --git a/tests/libcxx/expected_results.txt b/tests/libcxx/expected_results.txt index 0245f44cfae..e180d4efc43 100644 --- a/tests/libcxx/expected_results.txt +++ b/tests/libcxx/expected_results.txt @@ -652,6 +652,9 @@ std/utilities/charconv/charconv.msvc/test.pass.cpp FAIL std/ranges/range.adaptors/range.transform/end.pass.cpp FAIL std/ranges/range.adaptors/range.transform/iterator/base.pass.cpp FAIL +# libc++ doesn't yet implement LWG-3656 (https://reviews.llvm.org/D120444) +std/numerics/bit/bit.pow.two/bit_width.pass.cpp FAIL + # MaybePOCCAAllocator doesn't meet the allocator requirements std/containers/sequences/vector/vector.cons/assign_copy.pass.cpp FAIL diff --git a/tests/libcxx/skipped_tests.txt b/tests/libcxx/skipped_tests.txt index 5f0d8f4a26f..86379881df9 100644 --- a/tests/libcxx/skipped_tests.txt +++ b/tests/libcxx/skipped_tests.txt @@ -652,6 +652,9 @@ utilities\charconv\charconv.msvc\test.pass.cpp ranges\range.adaptors\range.transform\end.pass.cpp ranges\range.adaptors\range.transform\iterator\base.pass.cpp +# libc++ doesn't yet implement LWG-3656 (https://reviews.llvm.org/D120444) +numerics\bit\bit.pow.two\bit_width.pass.cpp + # MaybePOCCAAllocator doesn't meet the allocator requirements containers\sequences\vector\vector.cons\assign_copy.pass.cpp diff --git a/tests/std/tests/P0556R3_bit_integral_power_of_two_operations/test.cpp b/tests/std/tests/P0556R3_bit_integral_power_of_two_operations/test.cpp index 983145ab428..f727684f619 100644 --- a/tests/std/tests/P0556R3_bit_integral_power_of_two_operations/test.cpp +++ b/tests/std/tests/P0556R3_bit_integral_power_of_two_operations/test.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include using namespace std; @@ -76,12 +77,16 @@ constexpr bool test_bit_floor() { template constexpr bool test_bit_width() { constexpr int digits = numeric_limits::digits; - assert(bit_width(T{0}) == T{0}); + assert(bit_width(T{0}) == 0); assert(bit_width(numeric_limits::max()) == digits); - assert(bit_width(T{1}) == T{1}); + assert(bit_width(T{1}) == 1); for (int i = 1; i < digits; ++i) { - assert(bit_width(static_cast(T{1} << i)) == static_cast(i + 1)); + assert(bit_width(static_cast(T{1} << i)) == i + 1); } + + // LWG-3656: bit_width returns int + static_assert(is_same_v); + return true; }