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