Skip to content

[libc][__support][bit] simplify FLZ #81678

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 1 addition & 20 deletions libc/src/__support/CPP/bit.h
Original file line number Diff line number Diff line change
Expand Up @@ -226,30 +226,11 @@ LIBC_INLINE constexpr To bit_or_static_cast(const From &from) {
}
}

#define SPECIALIZE_FLZ(NAME, TYPE, BUILTIN) \
template <> [[nodiscard]] LIBC_INLINE constexpr int NAME<TYPE>(TYPE value) { \
static_assert(cpp::is_unsigned_v<TYPE>); \
return value == cpp::numeric_limits<TYPE>::max() \
? 0 \
: BUILTIN(static_cast<TYPE>(~value)) + 1; \
}

template <typename T, typename = cpp::enable_if_t<cpp::is_unsigned_v<T>>>
[[nodiscard]] LIBC_INLINE constexpr int first_leading_zero(T value) {
return value == cpp::numeric_limits<T>::max()
? 0
: countl_zero(static_cast<T>(~value)) + 1;
return value == cpp::numeric_limits<T>::max() ? 0 : countl_one(value) + 1;
}

#if LIBC_HAS_BUILTIN(__builtin_clzs)
SPECIALIZE_FLZ(first_leading_zero, unsigned short, __builtin_clzs)
#endif
SPECIALIZE_FLZ(first_leading_zero, unsigned int, __builtin_clz)
SPECIALIZE_FLZ(first_leading_zero, unsigned long, __builtin_clzl)
SPECIALIZE_FLZ(first_leading_zero, unsigned long long, __builtin_clzll)

#undef SPECIALIZE_FLZ

} // namespace LIBC_NAMESPACE::cpp

#endif // LLVM_LIBC_SRC___SUPPORT_CPP_BIT_H