| 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | // UNSUPPORTED: c++03, c++11, c++14, c++17 |
| 10 | |
| 11 | // template <class T> |
| 12 | // constexpr int countr_one(T x) noexcept; |
| 13 | |
| 14 | // Constraints: T is an unsigned integer type |
| 15 | // Returns: The number of consecutive 1 bits, starting from the least significant bit. |
| 16 | // [ Note: Returns N if x == std::numeric_limits<T>::max(). ] |
| 17 | |
| 18 | #include <bit> |
| 19 | #include <cassert> |
| 20 | #include <cstddef> |
| 21 | #include <cstdint> |
| 22 | #include <limits> |
| 23 | #include <type_traits> |
| 24 | |
| 25 | #include "test_macros.h" |
| 26 | |
| 27 | struct A {}; |
| 28 | enum E1 : unsigned char { rEd }; |
| 29 | enum class E2 : unsigned char { red }; |
| 30 | |
| 31 | template <class T> |
| 32 | constexpr bool test() |
| 33 | { |
| 34 | ASSERT_SAME_TYPE(decltype(std::countr_one(T())), int); |
| 35 | ASSERT_NOEXCEPT(std::countr_one(T())); |
| 36 | T max = std::numeric_limits<T>::max(); |
| 37 | |
| 38 | assert(std::countr_one(T(0)) == 0); |
| 39 | assert(std::countr_one(T(1)) == 1); |
| 40 | assert(std::countr_one(T(2)) == 0); |
| 41 | assert(std::countr_one(T(3)) == 2); |
| 42 | assert(std::countr_one(T(4)) == 0); |
| 43 | assert(std::countr_one(T(5)) == 1); |
| 44 | assert(std::countr_one(T(6)) == 0); |
| 45 | assert(std::countr_one(T(7)) == 3); |
| 46 | assert(std::countr_one(T(8)) == 0); |
| 47 | assert(std::countr_one(T(9)) == 1); |
| 48 | assert(std::countr_one(T(126)) == 0); |
| 49 | assert(std::countr_one(T(127)) == 7); |
| 50 | assert(std::countr_one(T(128)) == 0); |
| 51 | assert(std::countr_one(T(max - 1)) == 0); |
| 52 | assert(std::countr_one(max) == std::numeric_limits<T>::digits); |
| 53 | |
| 54 | #ifndef TEST_HAS_NO_INT128 |
| 55 | if constexpr (std::is_same_v<T, __uint128_t>) { |
| 56 | T val = 128; |
| 57 | assert(std::countr_one(val-1) == 7); |
| 58 | assert(std::countr_one(val) == 0); |
| 59 | assert(std::countr_one(val+1) == 1); |
| 60 | val <<= 32; |
| 61 | assert(std::countr_one(val-1) == 39); |
| 62 | assert(std::countr_one(val) == 0); |
| 63 | assert(std::countr_one(val+1) == 1); |
| 64 | val <<= 60; |
| 65 | assert(std::countr_one(val-1) == 99); |
| 66 | assert(std::countr_one(val) == 0); |
| 67 | assert(std::countr_one(val+1) == 1); |
| 68 | } |
| 69 | #endif |
| 70 | |
| 71 | return true; |
| 72 | } |
| 73 | |
| 74 | int main(int, char**) |
| 75 | { |
| 76 | { |
| 77 | auto lambda = [](auto x) -> decltype(std::countr_one(x)) {}; |
| 78 | using L = decltype(lambda); |
| 79 | |
| 80 | static_assert(!std::is_invocable_v<L, signed char>); |
| 81 | static_assert(!std::is_invocable_v<L, short>); |
| 82 | static_assert(!std::is_invocable_v<L, int>); |
| 83 | static_assert(!std::is_invocable_v<L, long>); |
| 84 | static_assert(!std::is_invocable_v<L, long long>); |
| 85 | #ifndef TEST_HAS_NO_INT128 |
| 86 | static_assert(!std::is_invocable_v<L, __int128_t>); |
| 87 | #endif |
| 88 | |
| 89 | static_assert(!std::is_invocable_v<L, std::int8_t>); |
| 90 | static_assert(!std::is_invocable_v<L, std::int16_t>); |
| 91 | static_assert(!std::is_invocable_v<L, std::int32_t>); |
| 92 | static_assert(!std::is_invocable_v<L, std::int64_t>); |
| 93 | static_assert(!std::is_invocable_v<L, std::intmax_t>); |
| 94 | static_assert(!std::is_invocable_v<L, std::intptr_t>); |
| 95 | static_assert(!std::is_invocable_v<L, std::ptrdiff_t>); |
| 96 | |
| 97 | static_assert(!std::is_invocable_v<L, bool>); |
| 98 | static_assert(!std::is_invocable_v<L, char>); |
| 99 | static_assert(!std::is_invocable_v<L, wchar_t>); |
| 100 | #ifndef TEST_HAS_NO_CHAR8_T |
| 101 | static_assert(!std::is_invocable_v<L, char8_t>); |
| 102 | #endif |
| 103 | static_assert(!std::is_invocable_v<L, char16_t>); |
| 104 | static_assert(!std::is_invocable_v<L, char32_t>); |
| 105 | |
| 106 | static_assert(!std::is_invocable_v<L, A>); |
| 107 | static_assert(!std::is_invocable_v<L, A*>); |
| 108 | static_assert(!std::is_invocable_v<L, E1>); |
| 109 | static_assert(!std::is_invocable_v<L, E2>); |
| 110 | } |
| 111 | |
| 112 | static_assert(test<unsigned char>()); |
| 113 | static_assert(test<unsigned short>()); |
| 114 | static_assert(test<unsigned int>()); |
| 115 | static_assert(test<unsigned long>()); |
| 116 | static_assert(test<unsigned long long>()); |
| 117 | #ifndef TEST_HAS_NO_INT128 |
| 118 | static_assert(test<__uint128_t>()); |
| 119 | #endif |
| 120 | static_assert(test<std::uint8_t>()); |
| 121 | static_assert(test<std::uint16_t>()); |
| 122 | static_assert(test<std::uint32_t>()); |
| 123 | static_assert(test<std::uint64_t>()); |
| 124 | static_assert(test<std::uintmax_t>()); |
| 125 | static_assert(test<std::uintptr_t>()); |
| 126 | static_assert(test<std::size_t>()); |
| 127 | |
| 128 | test<unsigned char>(); |
| 129 | test<unsigned short>(); |
| 130 | test<unsigned int>(); |
| 131 | test<unsigned long>(); |
| 132 | test<unsigned long long>(); |
| 133 | #ifndef TEST_HAS_NO_INT128 |
| 134 | test<__uint128_t>(); |
| 135 | #endif |
| 136 | test<std::uint8_t>(); |
| 137 | test<std::uint16_t>(); |
| 138 | test<std::uint32_t>(); |
| 139 | test<std::uint64_t>(); |
| 140 | test<std::uintmax_t>(); |
| 141 | test<std::uintptr_t>(); |
| 142 | test<std::size_t>(); |
| 143 | |
| 144 | return 0; |
| 145 | } |
| 146 | |