@@ -40,19 +40,26 @@ _LIBCPP_BEGIN_NAMESPACE_STD
4040 return __builtin_ctzll (__x);
4141}
4242
43- #if _LIBCPP_STD_VER >= 17
44- // Implementation using constexpr if for C++ standards >= 17
43+ #ifndef _LIBCPP_CXX03_LANG
44+ // constexpr implementation for C++11 and later
4545
46- // Precondition: __t != 0 (This is guaranteed by the caller __countr_zero, which handles __t == 0 as a special case)
47- template <class _Tp , __enable_if_t <is_unsigned<_Tp>::value, int > = 0 >
48- [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 int __countr_zero_impl (_Tp __t ) _NOEXCEPT {
46+ // Precondition: __t != 0 (the caller __countr_zero handles __t == 0 as a special case)
47+ template <class _Tp >
48+ [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI constexpr int __countr_zero_impl (_Tp __t ) _NOEXCEPT {
49+ static_assert (is_unsigned<_Tp>::value, " __countr_zero_impl only works with unsigned types" );
4950 if constexpr (sizeof (_Tp) <= sizeof (unsigned int )) {
5051 return std::__libcpp_ctz (static_cast <unsigned int >(__t ));
5152 } else if constexpr (sizeof (_Tp) <= sizeof (unsigned long )) {
5253 return std::__libcpp_ctz (static_cast <unsigned long >(__t ));
5354 } else if constexpr (sizeof (_Tp) <= sizeof (unsigned long long )) {
5455 return std::__libcpp_ctz (static_cast <unsigned long long >(__t ));
5556 } else {
57+ # if _LIBCPP_STD_VER == 11
58+ // A recursive constexpr implementation for C++11
59+ unsigned long long __ull = static_cast <unsigned long long >(__t );
60+ const unsigned int __ulldigits = numeric_limits<unsigned long long >::digits;
61+ return __ull == 0ull ? __ulldigits + std::__countr_zero_impl<_Tp>(__t >> __ulldigits) : std::__libcpp_ctz (__ull);
62+ # else
5663 int __ret = 0 ;
5764 const unsigned int __ulldigits = numeric_limits<unsigned long long >::digits;
5865 while (static_cast <unsigned long long >(__t ) == 0uLL) {
@@ -61,52 +68,35 @@ template <class _Tp, __enable_if_t<is_unsigned<_Tp>::value, int> = 0>
6168 }
6269 return __ret + std::__libcpp_ctz (static_cast <unsigned long long >(__t ));
6370 }
64- }
71+ # endif // _LIBCPP_STD_VER == 11
72+ }
6573
6674#else
67- // Equivalent SFINAE-based implementation for older C++ standards < 17
75+ // Equivalent implementation using SFINAE-based overloading for C++03
6876
69- // Precondition: __t != 0 (This is guaranteed by the caller __countr_zero, which handles __t == 0 as a special case)
7077template < class _Tp , __enable_if_t <is_unsigned<_Tp>::value && sizeof (_Tp) <= sizeof (unsigned int ), int > = 0 >
71- _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR int __countr_zero_impl (_Tp __t ) _NOEXCEPT {
78+ _LIBCPP_HIDE_FROM_ABI int __countr_zero_impl (_Tp __t ) _NOEXCEPT {
7279 return std::__libcpp_ctz (static_cast <unsigned int >(__t ));
7380}
7481
75- // Precondition: __t != 0 (This is guaranteed by the caller __countr_zero)
7682template < class _Tp ,
7783 __enable_if_t <is_unsigned<_Tp>::value && (sizeof (_Tp) > sizeof (unsigned int )) &&
7884 sizeof (_Tp) <= sizeof (unsigned long ),
7985 int > = 0 >
80- _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR int __countr_zero_impl (_Tp __t ) _NOEXCEPT {
86+ _LIBCPP_HIDE_FROM_ABI int __countr_zero_impl (_Tp __t ) _NOEXCEPT {
8187 return std::__libcpp_ctz (static_cast <unsigned long >(__t ));
8288}
8389
84- // Precondition: __t != 0 (This is guaranteed by the caller __countr_zero)
8590template < class _Tp ,
8691 __enable_if_t <is_unsigned<_Tp>::value && (sizeof (_Tp) > sizeof (unsigned long )) &&
8792 sizeof (_Tp) <= sizeof (unsigned long long ),
8893 int > = 0 >
89- _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR int __countr_zero_impl (_Tp __t ) _NOEXCEPT {
94+ _LIBCPP_HIDE_FROM_ABI int __countr_zero_impl (_Tp __t ) _NOEXCEPT {
9095 return std::__libcpp_ctz (static_cast <unsigned long long >(__t ));
9196}
9297
93- # if _LIBCPP_STD_VER == 11
94-
95- // Recursive constexpr implementation for C++11 due to limited constexpr support
96- // Precondition: __t != 0 (This is guaranteed by the caller __countr_zero)
97- template < class _Tp , __enable_if_t <is_unsigned<_Tp>::value && (sizeof (_Tp) > sizeof (unsigned long long )), int > = 0 >
98- _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR int __countr_zero_impl (_Tp __t ) _NOEXCEPT {
99- unsigned long long __ull = static_cast <unsigned long long >(__t );
100- const unsigned int __ulldigits = numeric_limits<unsigned long long >::digits;
101- return __ull == 0ull ? __ulldigits + std::__countr_zero_impl<_Tp>(__t >> __ulldigits) : std::__libcpp_ctz (__ull);
102- }
103-
104- # else
105-
106- // Loop-based constexpr implementation for C++14 (and non-constexpr for C++03, 98)
107- // Precondition: __t != 0 (This is guaranteed by the caller __countr_zero)
10898template < class _Tp , __enable_if_t <is_unsigned<_Tp>::value && (sizeof (_Tp) > sizeof (unsigned long long )), int > = 0 >
109- _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 int __countr_zero_impl (_Tp __t ) _NOEXCEPT {
99+ _LIBCPP_HIDE_FROM_ABI int __countr_zero_impl (_Tp __t ) _NOEXCEPT {
110100 int __ret = 0 ;
111101 const unsigned int __ulldigits = numeric_limits<unsigned long long >::digits;
112102 while (static_cast <unsigned long long >(__t ) == 0uLL) {
@@ -116,35 +106,34 @@ _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 int __countr_zero_impl(_Tp _
116106 return __ret + std::__libcpp_ctz (static_cast <unsigned long long >(__t ));
117107}
118108
119- # endif // _LIBCPP_STD_VER == 11
120-
121- #endif // _LIBCPP_STD_VER >= 17
109+ #endif // _LIBCPP_CXX03_LANG
122110
123- template <class _Tp , __enable_if_t <is_unsigned<_Tp>::value, int > = 0 >
124- [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR int __countr_zero (_Tp __t ) _NOEXCEPT {
111+ template <class _Tp >
112+ [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR int __countr_zero (_Tp __t ) _NOEXCEPT {
113+ static_assert (is_unsigned<_Tp>::value, " __countr_zero only works with unsigned types" );
125114#if __has_builtin(__builtin_ctzg)
126- return __builtin_ctzg (__t , numeric_limits<_Tp>::digits);
115+ return __builtin_ctzg (__t , numeric_limits<_Tp>::digits);
127116#else
128- return __t != 0 ? __countr_zero_impl (__t ) : numeric_limits<_Tp>::digits;
117+ return __t != 0 ? std:: __countr_zero_impl (__t ) : numeric_limits<_Tp>::digits;
129118#endif
130- }
119+ }
131120
132121#if _LIBCPP_STD_VER >= 20
133122
134- template <__libcpp_unsigned_integer _Tp>
135- [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr int countr_zero (_Tp __t ) noexcept {
136- return std::__countr_zero (__t );
137- }
123+ template <__libcpp_unsigned_integer _Tp>
124+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr int countr_zero (_Tp __t ) noexcept {
125+ return std::__countr_zero (__t );
126+ }
138127
139- template <__libcpp_unsigned_integer _Tp>
140- [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr int countr_one (_Tp __t ) noexcept {
141- return __t != numeric_limits<_Tp>::max () ? std::countr_zero (static_cast <_Tp>(~__t )) : numeric_limits<_Tp>::digits;
142- }
128+ template <__libcpp_unsigned_integer _Tp>
129+ [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr int countr_one (_Tp __t ) noexcept {
130+ return __t != numeric_limits<_Tp>::max () ? std::countr_zero (static_cast <_Tp>(~__t )) : numeric_limits<_Tp>::digits;
131+ }
143132
144133#endif // _LIBCPP_STD_VER >= 20
145134
146- _LIBCPP_END_NAMESPACE_STD
135+ _LIBCPP_END_NAMESPACE_STD
147136
148- _LIBCPP_POP_MACROS
137+ _LIBCPP_POP_MACROS
149138
150139#endif // _LIBCPP___BIT_COUNTR_H
0 commit comments