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

Conversation

nickdesaulniers
Copy link
Member

countl_zero(~x) is countl_one(x)

@llvmbot
Copy link
Member

llvmbot commented Feb 13, 2024

@llvm/pr-subscribers-libc

Author: Nick Desaulniers (nickdesaulniers)

Changes

countl_zero(~x) is countl_one(x)


Full diff: https://github.com/llvm/llvm-project/pull/81678.diff

1 Files Affected:

  • (modified) libc/src/__support/CPP/bit.h (+1-26)
diff --git a/libc/src/__support/CPP/bit.h b/libc/src/__support/CPP/bit.h
index 392fbe248138ae..3439a52e98ebf4 100644
--- a/libc/src/__support/CPP/bit.h
+++ b/libc/src/__support/CPP/bit.h
@@ -238,36 +238,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
-#if LIBC_HAS_BUILTIN(__builtin_clz)
-SPECIALIZE_FLZ(first_leading_zero, unsigned int, __builtin_clz)
-#endif
-#if LIBC_HAS_BUILTIN(__builtin_clzl)
-SPECIALIZE_FLZ(first_leading_zero, unsigned long, __builtin_clzl)
-#endif
-#if LIBC_HAS_BUILTIN(__builtin_clzll)
-SPECIALIZE_FLZ(first_leading_zero, unsigned long long, __builtin_clzll)
-#endif
-
-#undef SPECIALIZE_FLZ
-
 } // namespace LIBC_NAMESPACE::cpp
 
 #endif // LLVM_LIBC_SRC___SUPPORT_CPP_BIT_H

Copy link
Contributor

@michaelrj-google michaelrj-google left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Contributor

@gchatelet gchatelet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thx for cleaning this up!

`countl_zero(~x)` *is* `countl_one(x)`
@nickdesaulniers nickdesaulniers merged commit 0f6f5bf into llvm:main Feb 14, 2024
@nickdesaulniers nickdesaulniers deleted the bit_countr_one branch February 14, 2024 16:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants