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

via libc-commits libc-commits at lists.llvm.org
Thu Mar 6 21:06:29 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())
----------------
lntue wrote:

Why should it return 0 with `USHRT_MAX`?  The least significant index of the first bit 1 of `USHRT_MAX` is 0, so according to the spec, `stdc_first_trailing_one_us(USHRT_MAX)` should be 1.  Hence `cpp::countr_zero(USHRT_MAX) + 1` is the correct.  If our tests do not reflect that, then the tests also need to be updated.

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


More information about the libc-commits mailing list