@@ -40,73 +40,63 @@ _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) {
5966 __ret += __ulldigits;
6067 __t >>= __ulldigits;
6168 }
6269 return __ret + std::__libcpp_ctz (static_cast <unsigned long long >(__t ));
70+ # endif
6371 }
6472}
6573
6674#else
67- // Equivalent SFINAE-based implementation for older C++ standards < 17
75+ // implementation 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 ) {
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 ) {
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 ) {
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 ) {
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,16 +106,15 @@ _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 >
111+ template <class _Tp >
124112[[__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)
126115 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
130119}
131120
0 commit comments