Skip to content

Commit 0f6f5bf

Browse files
[libc][__support][bit] simplify FLZ (#81678)
`countl_zero(~x)` *is* `countl_one(x)`
1 parent 4efbf52 commit 0f6f5bf

File tree

1 file changed

+1
-20
lines changed
  • libc/src/__support/CPP

1 file changed

+1
-20
lines changed

libc/src/__support/CPP/bit.h

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -226,30 +226,11 @@ LIBC_INLINE constexpr To bit_or_static_cast(const From &from) {
226226
}
227227
}
228228

229-
#define SPECIALIZE_FLZ(NAME, TYPE, BUILTIN) \
230-
template <> [[nodiscard]] LIBC_INLINE constexpr int NAME<TYPE>(TYPE value) { \
231-
static_assert(cpp::is_unsigned_v<TYPE>); \
232-
return value == cpp::numeric_limits<TYPE>::max() \
233-
? 0 \
234-
: BUILTIN(static_cast<TYPE>(~value)) + 1; \
235-
}
236-
237229
template <typename T, typename = cpp::enable_if_t<cpp::is_unsigned_v<T>>>
238230
[[nodiscard]] LIBC_INLINE constexpr int first_leading_zero(T value) {
239-
return value == cpp::numeric_limits<T>::max()
240-
? 0
241-
: countl_zero(static_cast<T>(~value)) + 1;
231+
return value == cpp::numeric_limits<T>::max() ? 0 : countl_one(value) + 1;
242232
}
243233

244-
#if LIBC_HAS_BUILTIN(__builtin_clzs)
245-
SPECIALIZE_FLZ(first_leading_zero, unsigned short, __builtin_clzs)
246-
#endif
247-
SPECIALIZE_FLZ(first_leading_zero, unsigned int, __builtin_clz)
248-
SPECIALIZE_FLZ(first_leading_zero, unsigned long, __builtin_clzl)
249-
SPECIALIZE_FLZ(first_leading_zero, unsigned long long, __builtin_clzll)
250-
251-
#undef SPECIALIZE_FLZ
252-
253234
} // namespace LIBC_NAMESPACE::cpp
254235

255236
#endif // LLVM_LIBC_SRC___SUPPORT_CPP_BIT_H

0 commit comments

Comments
 (0)