Skip to content

Commit

Permalink
Use __builtin_ctzg and __builtin_clzg in the implementations of
Browse files Browse the repository at this point in the history
CountTrailingZeroesNonzero16 and CountLeadingZeroes16 when they are
available.

GCC 14 and Clang 19 adds these new builtins. The g-suffix is for
"generic". The s-suffix on __builtin_ctzs and __builtin_clzs is for
"short". GCC never implemented the short versions and #1664 reports
GCC 14 (pre-release) gives an error here, although this may be a
pre-release bug.

Fixes #1664

PiperOrigin-RevId: 630408249
Change-Id: I4aedcc82b85430f50d025f8eb1cab089c6fcd1bc
  • Loading branch information
derekmauro authored and Copybara-Service committed May 3, 2024
1 parent 7e149e4 commit c1e1b47
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions absl/numeric/internal/bits.h
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,9 @@ CountLeadingZeroes32(uint32_t x) {

ABSL_ATTRIBUTE_ALWAYS_INLINE ABSL_INTERNAL_CONSTEXPR_CLZ inline int
CountLeadingZeroes16(uint16_t x) {
#if ABSL_HAVE_BUILTIN(__builtin_clzs)
#if ABSL_HAVE_BUILTIN(__builtin_clzg)
return x == 0 ? 16 : __builtin_clzg(x);
#elif ABSL_HAVE_BUILTIN(__builtin_clzs)
static_assert(sizeof(unsigned short) == sizeof(x), // NOLINT(runtime/int)
"__builtin_clzs does not take 16-bit arg");
return x == 0 ? 16 : __builtin_clzs(x);
Expand Down Expand Up @@ -303,7 +305,9 @@ CountTrailingZeroesNonzero64(uint64_t x) {

ABSL_ATTRIBUTE_ALWAYS_INLINE ABSL_INTERNAL_CONSTEXPR_CTZ inline int
CountTrailingZeroesNonzero16(uint16_t x) {
#if ABSL_HAVE_BUILTIN(__builtin_ctzs)
#if ABSL_HAVE_BUILTIN(__builtin_ctzg)
return __builtin_ctzg(x);
#elif ABSL_HAVE_BUILTIN(__builtin_ctzs)
static_assert(sizeof(unsigned short) == sizeof(x), // NOLINT(runtime/int)
"__builtin_ctzs does not take 16-bit arg");
return __builtin_ctzs(x);
Expand Down

0 comments on commit c1e1b47

Please sign in to comment.