PR #2536: Fix bit_width return type with older libstdc++ Imported from GitHub PR https://github.com/abseil/abseil-cpp/pull/2536 With libstdc++ before version 13, C++20's `std::bit_width` returns the unsigned input type instead of `int`. Importing it makes `absl::bit_width` change return type between C++17 and C++20, and the existing `UnsignedIntegerTypesTest.ReturnTypes` assertions fail. Use Abseil's existing `bit_width` implementation on those libstdc++ versions. Keep `std::bit_ceil`, `std::bit_floor`, and `std::has_single_bit` in use, and retain the standard `bit_width` alias on unaffected libraries. This follows the existing workaround for older libc++ rotation signatures. Fixes #1890. Validation on Ubuntu 22.04 with GCC/libstdc++ 11.4, CMake 3.22.1, and GoogleTest v1.17.0: - Before the fix, compiling the existing `bits_test.cc` in C++20 fails its four return-type assertions; C++17 passes. No duplicate regression test was added. - Configured Release builds with `ABSL_BUILD_TESTING=ON` and `CMAKE_CXX_STANDARD=17` and `20`; built `absl_bits_test` and `absl_int128_test` with their dependencies. Both CTest targets pass in each mode: 22 bit-operation tests and 92 int128 tests per mode. - A standalone consumer's return-type and constexpr-value assertions pass in both modes with `-Wall -Wextra -Werror -pedantic`. - Changed-line clang-format 18.1.8 check and `git diff --check` pass. The [GCC 12.5](https://github.com/gcc-mirror/gcc/blob/releases/gcc-12.5.0/libstdc%2B%2B-v3/include/std/bit) and [GCC 13.1](https://github.com/gcc-mirror/gcc/blob/releases/gcc-13.1.0/libstdc%2B%2B-v3/include/std/bit) release headers confirm the version boundary. The full Abseil suite and other compiler, standard-library, and platform combinations were not run locally. Investigation, implementation, and validation used Codex assistance. Merge 93faa1f60c7f1773439e0b68eb7109a4d98233d6 into 80264aff67d041e1a4ca7f4fd123079ff99c975e Merging this change closes #2536 PiperOrigin-RevId: 1023745353
diff --git a/absl/numeric/bits.h b/absl/numeric/bits.h index 8eca392..902b2d2 100644 --- a/absl/numeric/bits.h +++ b/absl/numeric/bits.h
@@ -136,11 +136,29 @@ #endif +// https://github.com/abseil/abseil-cpp/issues/1890 +// libstdc++ had the wrong return type for std::bit_width prior to version 13. +#if (defined(__cpp_lib_int_pow2) && __cpp_lib_int_pow2 >= 202002L) && \ + (!defined(_GLIBCXX_RELEASE) || _GLIBCXX_RELEASE >= 13) + +using std::bit_width; + +#else + +// Returns: If x == 0, 0; otherwise one plus the base-2 logarithm of x, with any +// fractional part discarded. +template <class T> +ABSL_INTERNAL_CONSTEXPR_CLZ inline std::enable_if_t<std::is_unsigned_v<T>, int> +bit_width(T x) noexcept { + return std::numeric_limits<T>::digits - countl_zero(x); +} + +#endif + #if (defined(__cpp_lib_int_pow2) && __cpp_lib_int_pow2 >= 202002L) using std::bit_ceil; using std::bit_floor; -using std::bit_width; using std::has_single_bit; #else @@ -152,14 +170,6 @@ return x != 0 && (x & (x - 1)) == 0; } -// Returns: If x == 0, 0; otherwise one plus the base-2 logarithm of x, with any -// fractional part discarded. -template <class T> -ABSL_INTERNAL_CONSTEXPR_CLZ inline std::enable_if_t<std::is_unsigned_v<T>, int> -bit_width(T x) noexcept { - return std::numeric_limits<T>::digits - countl_zero(x); -} - // Returns: If x == 0, 0; otherwise the maximal value y such that // has_single_bit(y) is true and y <= x. template <class T>