1 //===----------------------------------------------------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is dual licensed under the MIT and the University of Illinois Open 6 // Source Licenses. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // UNSUPPORTED: c++98, c++03, c++11, c++14, c++17 10 11 // template <class T> 12 // constexpr int popcount(T x) noexcept; 13 14 // Returns: The number of bits set to one in the value of x. 15 // 16 // Remarks: This function shall not participate in overload resolution unless 17 // T is an unsigned integer type 18 19 #include <bit> 20 #include <cstdint> 21 #include <type_traits> 22 #include <cassert> 23 24 #include "test_macros.h" 25 26 class A{}; 27 enum E1 : unsigned char { rEd }; 28 enum class E2 : unsigned char { red }; 29 30 template <typename T> 31 constexpr bool constexpr_test() 32 { 33 return std::popcount(T(0)) == 0 34 && std::popcount(T(1)) == 1 35 && std::popcount(T(2)) == 1 36 && std::popcount(T(3)) == 2 37 && std::popcount(T(4)) == 1 38 && std::popcount(T(5)) == 2 39 && std::popcount(T(6)) == 2 40 && std::popcount(T(7)) == 3 41 && std::popcount(T(8)) == 1 42 && std::popcount(T(9)) == 2 43 && std::popcount(std::numeric_limits<T>::max()) == std::numeric_limits<T>::digits 44 ; 45 } 46 47 48 template <typename T> 49 void runtime_test() 50 { 51 ASSERT_SAME_TYPE(int, decltype(std::popcount(T(0)))); 52 ASSERT_NOEXCEPT( std::popcount(T(0))); 53 54 assert( std::popcount(T(121)) == 5); 55 assert( std::popcount(T(122)) == 5); 56 assert( std::popcount(T(123)) == 6); 57 assert( std::popcount(T(124)) == 5); 58 assert( std::popcount(T(125)) == 6); 59 assert( std::popcount(T(126)) == 6); 60 assert( std::popcount(T(127)) == 7); 61 assert( std::popcount(T(128)) == 1); 62 assert( std::popcount(T(129)) == 2); 63 assert( std::popcount(T(130)) == 2); 64 } 65 66 int main() 67 { 68 69 { 70 auto lambda = [](auto x) -> decltype(std::popcount(x)) {}; 71 using L = decltype(lambda); 72 73 static_assert( std::is_invocable_v<L, unsigned char>, ""); 74 static_assert( std::is_invocable_v<L, unsigned int>, ""); 75 static_assert( std::is_invocable_v<L, unsigned long>, ""); 76 static_assert( std::is_invocable_v<L, unsigned long long>, ""); 77 78 static_assert( std::is_invocable_v<L, uint8_t>, ""); 79 static_assert( std::is_invocable_v<L, uint16_t>, ""); 80 static_assert( std::is_invocable_v<L, uint32_t>, ""); 81 static_assert( std::is_invocable_v<L, uint64_t>, ""); 82 static_assert( std::is_invocable_v<L, size_t>, ""); 83 84 static_assert( std::is_invocable_v<L, uintmax_t>, ""); 85 static_assert( std::is_invocable_v<L, uintptr_t>, ""); 86 87 88 static_assert(!std::is_invocable_v<L, int>, ""); 89 static_assert(!std::is_invocable_v<L, signed int>, ""); 90 static_assert(!std::is_invocable_v<L, long>, ""); 91 static_assert(!std::is_invocable_v<L, long long>, ""); 92 93 static_assert(!std::is_invocable_v<L, int8_t>, ""); 94 static_assert(!std::is_invocable_v<L, int16_t>, ""); 95 static_assert(!std::is_invocable_v<L, int32_t>, ""); 96 static_assert(!std::is_invocable_v<L, int64_t>, ""); 97 static_assert(!std::is_invocable_v<L, ptrdiff_t>, ""); 98 99 static_assert(!std::is_invocable_v<L, bool>, ""); 100 static_assert(!std::is_invocable_v<L, signed char>, ""); 101 static_assert(!std::is_invocable_v<L, char16_t>, ""); 102 static_assert(!std::is_invocable_v<L, char32_t>, ""); 103 104 #ifndef _LIBCPP_HAS_NO_INT128 105 static_assert( std::is_invocable_v<L, __uint128_t>, ""); 106 static_assert(!std::is_invocable_v<L, __int128_t>, ""); 107 #endif 108 109 static_assert(!std::is_invocable_v<L, A>, ""); 110 static_assert(!std::is_invocable_v<L, E1>, ""); 111 static_assert(!std::is_invocable_v<L, E2>, ""); 112 } 113 114 static_assert(constexpr_test<unsigned char>(), ""); 115 static_assert(constexpr_test<unsigned short>(), ""); 116 static_assert(constexpr_test<unsigned>(), ""); 117 static_assert(constexpr_test<unsigned long>(), ""); 118 static_assert(constexpr_test<unsigned long long>(), ""); 119 120 static_assert(constexpr_test<uint8_t>(), ""); 121 static_assert(constexpr_test<uint16_t>(), ""); 122 static_assert(constexpr_test<uint32_t>(), ""); 123 static_assert(constexpr_test<uint64_t>(), ""); 124 static_assert(constexpr_test<size_t>(), ""); 125 static_assert(constexpr_test<uintmax_t>(), ""); 126 static_assert(constexpr_test<uintptr_t>(), ""); 127 128 #ifndef _LIBCPP_HAS_NO_INT128 129 static_assert(constexpr_test<__uint128_t>(), ""); 130 #endif 131 132 133 runtime_test<unsigned char>(); 134 runtime_test<unsigned>(); 135 runtime_test<unsigned short>(); 136 runtime_test<unsigned long>(); 137 runtime_test<unsigned long long>(); 138 139 runtime_test<uint8_t>(); 140 runtime_test<uint16_t>(); 141 runtime_test<uint32_t>(); 142 runtime_test<uint64_t>(); 143 runtime_test<size_t>(); 144 runtime_test<uintmax_t>(); 145 runtime_test<uintptr_t>(); 146 147 #ifndef _LIBCPP_HAS_NO_INT128 148 runtime_test<__uint128_t>(); 149 150 { 151 __uint128_t val = 128; 152 153 val <<= 32; 154 assert( std::popcount(val-1) == 39); 155 assert( std::popcount(val) == 1); 156 assert( std::popcount(val+1) == 2); 157 val <<= 2; 158 assert( std::popcount(val-1) == 41); 159 assert( std::popcount(val) == 1); 160 assert( std::popcount(val+1) == 2); 161 val <<= 3; 162 assert( std::popcount(val-1) == 44); 163 assert( std::popcount(val) == 1); 164 assert( std::popcount(val+1) == 2); 165 } 166 #endif 167 } 168