[libc-commits] [libc] [libc] `first_trailing_one(0)` should be `0`. (PR #130155)

Connector Switch via libc-commits libc-commits at lists.llvm.org
Thu Mar 6 18:11:22 PST 2025


================
@@ -146,8 +146,9 @@ first_trailing_zero(T value) {
 template <typename T>
 [[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_unsigned_v<T>, int>
 first_trailing_one(T value) {
-  return value == cpp::numeric_limits<T>::max() ? 0
-                                                : cpp::countr_zero(value) + 1;
+  return (value == 0 || value == cpp::numeric_limits<T>::max())
----------------
c8ef wrote:

It will fail with inputs like `USHRT_MAX` because we need it to return 0, but `cpp::countr_zero(USHRT_MAX) + 1 = 1`.

https://github.com/llvm/llvm-project/pull/130155


More information about the libc-commits mailing list