1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // UNSUPPORTED: c++03, c++11, c++14, c++17
12 // constexpr T bit_floor(T x) noexcept;
14 // Constraints: T is an unsigned integer type
15 // Returns: If x == 0, 0; otherwise the maximal value y such that bit_floor(y) is true and y <= x.
22 #include <type_traits>
24 #include "test_macros.h"
27 enum E1
: unsigned char { rEd
};
28 enum class E2
: unsigned char { red
};
33 ASSERT_SAME_TYPE(decltype(std::bit_floor(T())), T
);
34 LIBCPP_ASSERT_NOEXCEPT(std::bit_floor(T()));
35 T max
= std::numeric_limits
<T
>::max();
37 assert(std::bit_floor(T(0)) == T(0));
38 assert(std::bit_floor(T(1)) == T(1));
39 assert(std::bit_floor(T(2)) == T(2));
40 assert(std::bit_floor(T(3)) == T(2));
41 assert(std::bit_floor(T(4)) == T(4));
42 assert(std::bit_floor(T(5)) == T(4));
43 assert(std::bit_floor(T(6)) == T(4));
44 assert(std::bit_floor(T(7)) == T(4));
45 assert(std::bit_floor(T(8)) == T(8));
46 assert(std::bit_floor(T(9)) == T(8));
47 assert(std::bit_floor(T(125)) == T(64));
48 assert(std::bit_floor(T(126)) == T(64));
49 assert(std::bit_floor(T(127)) == T(64));
50 assert(std::bit_floor(T(128)) == T(128));
51 assert(std::bit_floor(T(129)) == T(128));
52 assert(std::bit_floor(max
) == T(max
- (max
>> 1)));
54 #ifndef TEST_HAS_NO_INT128
55 if constexpr (std::is_same_v
<T
, __uint128_t
>) {
57 assert(std::bit_floor(val
-1) == val
/2);
58 assert(std::bit_floor(val
) == val
);
59 assert(std::bit_floor(val
+1) == val
);
61 assert(std::bit_floor(val
-1) == val
/2);
62 assert(std::bit_floor(val
) == val
);
63 assert(std::bit_floor(val
+1) == val
);
73 auto lambda
= [](auto x
) -> decltype(std::bit_floor(x
)) {};
74 using L
= decltype(lambda
);
76 static_assert(!std::is_invocable_v
<L
, signed char>);
77 static_assert(!std::is_invocable_v
<L
, short>);
78 static_assert(!std::is_invocable_v
<L
, int>);
79 static_assert(!std::is_invocable_v
<L
, long>);
80 static_assert(!std::is_invocable_v
<L
, long long>);
81 #ifndef TEST_HAS_NO_INT128
82 static_assert(!std::is_invocable_v
<L
, __int128_t
>);
85 static_assert(!std::is_invocable_v
<L
, std::int8_t>);
86 static_assert(!std::is_invocable_v
<L
, std::int16_t>);
87 static_assert(!std::is_invocable_v
<L
, std::int32_t>);
88 static_assert(!std::is_invocable_v
<L
, std::int64_t>);
89 static_assert(!std::is_invocable_v
<L
, std::intmax_t>);
90 static_assert(!std::is_invocable_v
<L
, std::intptr_t>);
91 static_assert(!std::is_invocable_v
<L
, std::ptrdiff_t>);
93 static_assert(!std::is_invocable_v
<L
, bool>);
94 static_assert(!std::is_invocable_v
<L
, char>);
95 static_assert(!std::is_invocable_v
<L
, wchar_t>);
96 #ifndef TEST_HAS_NO_CHAR8_T
97 static_assert(!std::is_invocable_v
<L
, char8_t
>);
99 static_assert(!std::is_invocable_v
<L
, char16_t
>);
100 static_assert(!std::is_invocable_v
<L
, char32_t
>);
102 static_assert(!std::is_invocable_v
<L
, A
>);
103 static_assert(!std::is_invocable_v
<L
, A
*>);
104 static_assert(!std::is_invocable_v
<L
, E1
>);
105 static_assert(!std::is_invocable_v
<L
, E2
>);
108 static_assert(test
<unsigned char>());
109 static_assert(test
<unsigned short>());
110 static_assert(test
<unsigned int>());
111 static_assert(test
<unsigned long>());
112 static_assert(test
<unsigned long long>());
113 #ifndef TEST_HAS_NO_INT128
114 static_assert(test
<__uint128_t
>());
117 static_assert(test
<std::uint8_t>());
118 static_assert(test
<std::uint16_t>());
119 static_assert(test
<std::uint32_t>());
120 static_assert(test
<std::uint64_t>());
121 static_assert(test
<std::uintmax_t>());
122 static_assert(test
<std::uintptr_t>());
123 static_assert(test
<std::size_t>());
125 test
<unsigned char>();
126 test
<unsigned short>();
127 test
<unsigned int>();
128 test
<unsigned long>();
129 test
<unsigned long long>();
130 #ifndef TEST_HAS_NO_INT128
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>();