-/// CountLeadingZeros_32 - this function performs the platform optimal form of
-/// counting the number of zeros from the most significant bit to the first one
-/// bit. Ex. CountLeadingZeros_32(0x00F000FF) == 8.
-/// Returns 32 if the word is zero.
-inline unsigned CountLeadingZeros_32(uint32_t Value) {
- unsigned Count; // result
-#if __GNUC__ >= 4
- // PowerPC is defined for __builtin_clz(0)
-#if !defined(__ppc__) && !defined(__ppc64__)
- if (!Value) return 32;
-#endif
- Count = __builtin_clz(Value);
-#else
- if (!Value) return 32;
- Count = 0;
- // bisection method for count leading zeros
- for (unsigned Shift = 32 >> 1; Shift; Shift >>= 1) {
- uint32_t Tmp = Value >> Shift;
- if (Tmp) {
- Value = Tmp;
- } else {
- Count |= Shift;
- }
- }
-#endif
- return Count;
-}
-
-/// CountLeadingOnes_32 - this function performs the operation of
-/// counting the number of ones from the most significant bit to the first zero
-/// bit. Ex. CountLeadingOnes_32(0xFF0FFF00) == 8.
-/// Returns 32 if the word is all ones.
-inline unsigned CountLeadingOnes_32(uint32_t Value) {
- return CountLeadingZeros_32(~Value);
-}
-
-/// CountLeadingZeros_64 - This function performs the platform optimal form
-/// of counting the number of zeros from the most significant bit to the first
-/// one bit (64 bit edition.)
-/// Returns 64 if the word is zero.
-inline unsigned CountLeadingZeros_64(uint64_t Value) {
- unsigned Count; // result
-#if __GNUC__ >= 4
- // PowerPC is defined for __builtin_clzll(0)
-#if !defined(__ppc__) && !defined(__ppc64__)
- if (!Value) return 64;
-#endif
- Count = __builtin_clzll(Value);
-#else
- if (sizeof(long) == sizeof(int64_t)) {
- if (!Value) return 64;
- Count = 0;
- // bisection method for count leading zeros
- for (unsigned Shift = 64 >> 1; Shift; Shift >>= 1) {
- uint64_t Tmp = Value >> Shift;
- if (Tmp) {
- Value = Tmp;
- } else {
- Count |= Shift;
- }
- }
- } else {
- // get hi portion
- uint32_t Hi = Hi_32(Value);
-
- // if some bits in hi portion
- if (Hi) {
- // leading zeros in hi portion plus all bits in lo portion
- Count = CountLeadingZeros_32(Hi);
- } else {
- // get lo portion
- uint32_t Lo = Lo_32(Value);
- // same as 32 bit value
- Count = CountLeadingZeros_32(Lo)+32;
- }
- }
-#endif
- return Count;
-}
-
-/// CountLeadingOnes_64 - This function performs the operation
-/// of counting the number of ones from the most significant bit to the first
-/// zero bit (64 bit edition.)
-/// Returns 64 if the word is all ones.
-inline unsigned CountLeadingOnes_64(uint64_t Value) {
- return CountLeadingZeros_64(~Value);
+/// \brief Count the number of ones from the most significant bit to the first
+/// zero bit.
+///
+/// Ex. CountLeadingOnes(0xFF0FFF00) == 8.
+/// Only unsigned integral types are allowed.
+///
+/// \param ZB the behavior on an input of all ones. Only ZB_Width and
+/// ZB_Undefined are valid arguments.
+template <typename T>
+std::size_t countLeadingOnes(T Value, ZeroBehavior ZB = ZB_Width) {
+ static_assert(std::numeric_limits<T>::is_integer &&
+ !std::numeric_limits<T>::is_signed,
+ "Only unsigned integral types are allowed.");
+ return countLeadingZeros(~Value, ZB);