Skip to content

Commit 746b719

Browse files
[libc][__support][bit] simplify FLZ
`countl_zero(~x)` *is* `countl_one(x)`
1 parent d2d6b36 commit 746b719

File tree

1 file changed

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

1 file changed

+1
-26
lines changed

libc/src/__support/CPP/bit.h

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -238,36 +238,11 @@ LIBC_INLINE constexpr To bit_or_static_cast(const From &from) {
238238
}
239239
}
240240

241-
#define SPECIALIZE_FLZ(NAME, TYPE, BUILTIN) \
242-
template <> [[nodiscard]] LIBC_INLINE constexpr int NAME<TYPE>(TYPE value) { \
243-
static_assert(cpp::is_unsigned_v<TYPE>); \
244-
return value == cpp::numeric_limits<TYPE>::max() \
245-
? 0 \
246-
: BUILTIN(static_cast<TYPE>(~value)) + 1; \
247-
}
248-
249241
template <typename T, typename = cpp::enable_if_t<cpp::is_unsigned_v<T>>>
250242
[[nodiscard]] LIBC_INLINE constexpr int first_leading_zero(T value) {
251-
return value == cpp::numeric_limits<T>::max()
252-
? 0
253-
: countl_zero(static_cast<T>(~value)) + 1;
243+
return value == cpp::numeric_limits<T>::max() ? 0 : countl_one(value) + 1;
254244
}
255245

256-
#if LIBC_HAS_BUILTIN(__builtin_clzs)
257-
SPECIALIZE_FLZ(first_leading_zero, unsigned short, __builtin_clzs)
258-
#endif
259-
#if LIBC_HAS_BUILTIN(__builtin_clz)
260-
SPECIALIZE_FLZ(first_leading_zero, unsigned int, __builtin_clz)
261-
#endif
262-
#if LIBC_HAS_BUILTIN(__builtin_clzl)
263-
SPECIALIZE_FLZ(first_leading_zero, unsigned long, __builtin_clzl)
264-
#endif
265-
#if LIBC_HAS_BUILTIN(__builtin_clzll)
266-
SPECIALIZE_FLZ(first_leading_zero, unsigned long long, __builtin_clzll)
267-
#endif
268-
269-
#undef SPECIALIZE_FLZ
270-
271246
} // namespace LIBC_NAMESPACE::cpp
272247

273248
#endif // LLVM_LIBC_SRC___SUPPORT_CPP_BIT_H

0 commit comments

Comments
 (0)