forked from OSchip/llvm-project
[libc++] Some fixes to the <bit> utilities.
Fix __bitop_unsigned_integer and rename to __libcpp_is_unsigned_integer. There are only five unsigned integer types, so we should just list them out. Also provide `__libcpp_is_signed_integer`, even though the Standard doesn't consume that trait anywhere yet. Notice that `concept uniform_random_bit_generator` is specifically specified to rely on `concept unsigned_integral` and *not* `__is_unsigned_integer`. Instantiating `std::ranges::sample` with a type `U` satisfying `uniform_random_bit_generator` where `unsigned_integral<U::result_type>` and not `__is_unsigned_integer<U::result_type>` is simply IFNDR. Orthogonally, fix an undefined behavior in std::countr_zero(__uint128_t). Orthogonally, improve tests for the <bit> manipulation functions. It was these new tests that detected the bug in countr_zero. Differential Revision: https://reviews.llvm.org/D102328
This commit is contained in:
parent
a32e39a75b
commit
e130fbe24e
libcxx
include
test/std/numerics/bit
|
@ -77,49 +77,33 @@ _LIBCPP_PUSH_MACROS
|
|||
|
||||
_LIBCPP_BEGIN_NAMESPACE_STD
|
||||
|
||||
|
||||
template <class _Tp>
|
||||
using __bitop_unsigned_integer _LIBCPP_NODEBUG_TYPE = integral_constant<bool,
|
||||
is_integral<_Tp>::value &&
|
||||
is_unsigned<_Tp>::value &&
|
||||
_IsNotSame<typename remove_cv<_Tp>::type, bool>::value &&
|
||||
_IsNotSame<typename remove_cv<_Tp>::type, signed char>::value &&
|
||||
_IsNotSame<typename remove_cv<_Tp>::type, wchar_t>::value &&
|
||||
_IsNotSame<typename remove_cv<_Tp>::type, char16_t>::value &&
|
||||
_IsNotSame<typename remove_cv<_Tp>::type, char32_t>::value
|
||||
>;
|
||||
|
||||
|
||||
template<class _Tp>
|
||||
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
|
||||
_Tp __rotl(_Tp __t, unsigned int __cnt) _NOEXCEPT
|
||||
{
|
||||
static_assert(__bitop_unsigned_integer<_Tp>::value, "__rotl requires unsigned");
|
||||
static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__rotl requires an unsigned integer type");
|
||||
const unsigned int __dig = numeric_limits<_Tp>::digits;
|
||||
if ((__cnt % __dig) == 0)
|
||||
return __t;
|
||||
return (__t << (__cnt % __dig)) | (__t >> (__dig - (__cnt % __dig)));
|
||||
}
|
||||
|
||||
|
||||
template<class _Tp>
|
||||
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
|
||||
_Tp __rotr(_Tp __t, unsigned int __cnt) _NOEXCEPT
|
||||
{
|
||||
static_assert(__bitop_unsigned_integer<_Tp>::value, "__rotr requires unsigned");
|
||||
static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__rotr requires an unsigned integer type");
|
||||
const unsigned int __dig = numeric_limits<_Tp>::digits;
|
||||
if ((__cnt % __dig) == 0)
|
||||
return __t;
|
||||
return (__t >> (__cnt % __dig)) | (__t << (__dig - (__cnt % __dig)));
|
||||
}
|
||||
|
||||
|
||||
|
||||
template<class _Tp>
|
||||
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
|
||||
int __countr_zero(_Tp __t) _NOEXCEPT
|
||||
{
|
||||
static_assert(__bitop_unsigned_integer<_Tp>::value, "__countr_zero requires unsigned");
|
||||
static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__countr_zero requires an unsigned integer type");
|
||||
if (__t == 0)
|
||||
return numeric_limits<_Tp>::digits;
|
||||
|
||||
|
@ -132,14 +116,13 @@ int __countr_zero(_Tp __t) _NOEXCEPT
|
|||
else
|
||||
{
|
||||
int __ret = 0;
|
||||
int __iter = 0;
|
||||
const unsigned int __ulldigits = numeric_limits<unsigned long long>::digits;
|
||||
while ((__iter = __libcpp_ctz(static_cast<unsigned long long>(__t))) == __ulldigits)
|
||||
while (static_cast<unsigned long long>(__t) == 0uLL)
|
||||
{
|
||||
__ret += __iter;
|
||||
__ret += __ulldigits;
|
||||
__t >>= __ulldigits;
|
||||
}
|
||||
return __ret + __iter;
|
||||
return __ret + __libcpp_ctz(static_cast<unsigned long long>(__t));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -147,7 +130,7 @@ template<class _Tp>
|
|||
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
|
||||
int __countl_zero(_Tp __t) _NOEXCEPT
|
||||
{
|
||||
static_assert(__bitop_unsigned_integer<_Tp>::value, "__countl_zero requires unsigned");
|
||||
static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__countl_zero requires an unsigned integer type");
|
||||
if (__t == 0)
|
||||
return numeric_limits<_Tp>::digits;
|
||||
|
||||
|
@ -179,30 +162,27 @@ template<class _Tp>
|
|||
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
|
||||
int __countl_one(_Tp __t) _NOEXCEPT
|
||||
{
|
||||
static_assert(__bitop_unsigned_integer<_Tp>::value, "__countl_one requires unsigned");
|
||||
static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__countl_one requires an unsigned integer type");
|
||||
return __t != numeric_limits<_Tp>::max()
|
||||
? __countl_zero(static_cast<_Tp>(~__t))
|
||||
: numeric_limits<_Tp>::digits;
|
||||
}
|
||||
|
||||
|
||||
template<class _Tp>
|
||||
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
|
||||
int __countr_one(_Tp __t) _NOEXCEPT
|
||||
{
|
||||
static_assert(__bitop_unsigned_integer<_Tp>::value, "__countr_one requires unsigned");
|
||||
static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__countr_one requires an unsigned integer type");
|
||||
return __t != numeric_limits<_Tp>::max()
|
||||
? __countr_zero(static_cast<_Tp>(~__t))
|
||||
: numeric_limits<_Tp>::digits;
|
||||
}
|
||||
|
||||
|
||||
template<class _Tp>
|
||||
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
|
||||
int
|
||||
__popcount(_Tp __t) _NOEXCEPT
|
||||
int __popcount(_Tp __t) _NOEXCEPT
|
||||
{
|
||||
static_assert(__bitop_unsigned_integer<_Tp>::value, "__libcpp_popcount requires unsigned");
|
||||
static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__popcount requires an unsigned integer type");
|
||||
if (sizeof(_Tp) <= sizeof(unsigned int))
|
||||
return __libcpp_popcount(static_cast<unsigned int>(__t));
|
||||
else if (sizeof(_Tp) <= sizeof(unsigned long))
|
||||
|
@ -221,13 +201,12 @@ __popcount(_Tp __t) _NOEXCEPT
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
// integral log base 2
|
||||
template<class _Tp>
|
||||
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
|
||||
unsigned __bit_log2(_Tp __t) _NOEXCEPT
|
||||
{
|
||||
static_assert(__bitop_unsigned_integer<_Tp>::value, "__bit_log2 requires unsigned");
|
||||
static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__bit_log2 requires an unsigned integer type");
|
||||
return numeric_limits<_Tp>::digits - 1 - __countl_zero(__t);
|
||||
}
|
||||
|
||||
|
@ -235,80 +214,71 @@ template <class _Tp>
|
|||
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
|
||||
bool __has_single_bit(_Tp __t) _NOEXCEPT
|
||||
{
|
||||
static_assert(__bitop_unsigned_integer<_Tp>::value, "__has_single_bit requires unsigned");
|
||||
static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__has_single_bit requires an unsigned integer type");
|
||||
return __t != 0 && (((__t & (__t - 1)) == 0));
|
||||
}
|
||||
|
||||
|
||||
#if _LIBCPP_STD_VER > 17
|
||||
|
||||
template<class _Tp>
|
||||
_LIBCPP_INLINE_VISIBILITY constexpr
|
||||
enable_if_t<__bitop_unsigned_integer<_Tp>::value, _Tp>
|
||||
_EnableIf<__libcpp_is_unsigned_integer<_Tp>::value, _Tp>
|
||||
rotl(_Tp __t, unsigned int __cnt) noexcept
|
||||
{
|
||||
return __rotl(__t, __cnt);
|
||||
}
|
||||
|
||||
|
||||
// rotr
|
||||
template<class _Tp>
|
||||
_LIBCPP_INLINE_VISIBILITY constexpr
|
||||
enable_if_t<__bitop_unsigned_integer<_Tp>::value, _Tp>
|
||||
_EnableIf<__libcpp_is_unsigned_integer<_Tp>::value, _Tp>
|
||||
rotr(_Tp __t, unsigned int __cnt) noexcept
|
||||
{
|
||||
return __rotr(__t, __cnt);
|
||||
}
|
||||
|
||||
|
||||
template<class _Tp>
|
||||
_LIBCPP_INLINE_VISIBILITY constexpr
|
||||
enable_if_t<__bitop_unsigned_integer<_Tp>::value, int>
|
||||
_EnableIf<__libcpp_is_unsigned_integer<_Tp>::value, int>
|
||||
countl_zero(_Tp __t) noexcept
|
||||
{
|
||||
return __countl_zero(__t);
|
||||
}
|
||||
|
||||
|
||||
template<class _Tp>
|
||||
_LIBCPP_INLINE_VISIBILITY constexpr
|
||||
enable_if_t<__bitop_unsigned_integer<_Tp>::value, int>
|
||||
_EnableIf<__libcpp_is_unsigned_integer<_Tp>::value, int>
|
||||
countl_one(_Tp __t) noexcept
|
||||
{
|
||||
return __countl_one(__t);
|
||||
}
|
||||
|
||||
|
||||
template<class _Tp>
|
||||
_LIBCPP_INLINE_VISIBILITY constexpr
|
||||
enable_if_t<__bitop_unsigned_integer<_Tp>::value, int>
|
||||
_EnableIf<__libcpp_is_unsigned_integer<_Tp>::value, int>
|
||||
countr_zero(_Tp __t) noexcept
|
||||
{
|
||||
return __countr_zero(__t);
|
||||
}
|
||||
|
||||
|
||||
template<class _Tp>
|
||||
_LIBCPP_INLINE_VISIBILITY constexpr
|
||||
enable_if_t<__bitop_unsigned_integer<_Tp>::value, int>
|
||||
_EnableIf<__libcpp_is_unsigned_integer<_Tp>::value, int>
|
||||
countr_one(_Tp __t) noexcept
|
||||
{
|
||||
return __countr_one(__t);
|
||||
}
|
||||
|
||||
|
||||
template<class _Tp>
|
||||
_LIBCPP_INLINE_VISIBILITY constexpr
|
||||
enable_if_t<__bitop_unsigned_integer<_Tp>::value, int>
|
||||
_EnableIf<__libcpp_is_unsigned_integer<_Tp>::value, int>
|
||||
popcount(_Tp __t) noexcept
|
||||
{
|
||||
return __popcount(__t);
|
||||
}
|
||||
|
||||
|
||||
template <class _Tp>
|
||||
_LIBCPP_INLINE_VISIBILITY constexpr
|
||||
enable_if_t<__bitop_unsigned_integer<_Tp>::value, bool>
|
||||
_EnableIf<__libcpp_is_unsigned_integer<_Tp>::value, bool>
|
||||
has_single_bit(_Tp __t) noexcept
|
||||
{
|
||||
return __has_single_bit(__t);
|
||||
|
@ -316,7 +286,7 @@ has_single_bit(_Tp __t) noexcept
|
|||
|
||||
template <class _Tp>
|
||||
_LIBCPP_INLINE_VISIBILITY constexpr
|
||||
enable_if_t<__bitop_unsigned_integer<_Tp>::value, _Tp>
|
||||
_EnableIf<__libcpp_is_unsigned_integer<_Tp>::value, _Tp>
|
||||
bit_floor(_Tp __t) noexcept
|
||||
{
|
||||
return __t == 0 ? 0 : _Tp{1} << __bit_log2(__t);
|
||||
|
@ -324,7 +294,7 @@ bit_floor(_Tp __t) noexcept
|
|||
|
||||
template <class _Tp>
|
||||
_LIBCPP_INLINE_VISIBILITY constexpr
|
||||
enable_if_t<__bitop_unsigned_integer<_Tp>::value, _Tp>
|
||||
_EnableIf<__libcpp_is_unsigned_integer<_Tp>::value, _Tp>
|
||||
bit_ceil(_Tp __t) noexcept
|
||||
{
|
||||
if (__t < 2) return 1;
|
||||
|
@ -343,7 +313,7 @@ bit_ceil(_Tp __t) noexcept
|
|||
|
||||
template <class _Tp>
|
||||
_LIBCPP_INLINE_VISIBILITY constexpr
|
||||
enable_if_t<__bitop_unsigned_integer<_Tp>::value, _Tp>
|
||||
_EnableIf<__libcpp_is_unsigned_integer<_Tp>::value, _Tp>
|
||||
bit_width(_Tp __t) noexcept
|
||||
{
|
||||
return __t == 0 ? 0 : __bit_log2(__t) + 1;
|
||||
|
|
|
@ -789,6 +789,33 @@ _LIBCPP_INLINE_VAR _LIBCPP_CONSTEXPR bool is_integral_v
|
|||
|
||||
#endif // __has_keyword(__is_integral)
|
||||
|
||||
// __libcpp_is_signed_integer, __libcpp_is_unsigned_integer
|
||||
|
||||
// [basic.fundamental] defines five standard signed integer types;
|
||||
// __int128_t is an extended signed integer type.
|
||||
// The signed and unsigned integer types, plus bool and the
|
||||
// five types with "char" in their name, compose the "integral" types.
|
||||
|
||||
template <class _Tp> struct __libcpp_is_signed_integer : public false_type {};
|
||||
template <> struct __libcpp_is_signed_integer<signed char> : public true_type {};
|
||||
template <> struct __libcpp_is_signed_integer<signed short> : public true_type {};
|
||||
template <> struct __libcpp_is_signed_integer<signed int> : public true_type {};
|
||||
template <> struct __libcpp_is_signed_integer<signed long> : public true_type {};
|
||||
template <> struct __libcpp_is_signed_integer<signed long long> : public true_type {};
|
||||
#ifndef _LIBCPP_HAS_NO_INT128
|
||||
template <> struct __libcpp_is_signed_integer<__int128_t> : public true_type {};
|
||||
#endif
|
||||
|
||||
template <class _Tp> struct __libcpp_is_unsigned_integer : public false_type {};
|
||||
template <> struct __libcpp_is_unsigned_integer<unsigned char> : public true_type {};
|
||||
template <> struct __libcpp_is_unsigned_integer<unsigned short> : public true_type {};
|
||||
template <> struct __libcpp_is_unsigned_integer<unsigned int> : public true_type {};
|
||||
template <> struct __libcpp_is_unsigned_integer<unsigned long> : public true_type {};
|
||||
template <> struct __libcpp_is_unsigned_integer<unsigned long long> : public true_type {};
|
||||
#ifndef _LIBCPP_HAS_NO_INT128
|
||||
template <> struct __libcpp_is_unsigned_integer<__uint128_t> : public true_type {};
|
||||
#endif
|
||||
|
||||
// is_floating_point
|
||||
|
||||
template <class _Tp> struct __libcpp_is_floating_point : public false_type {};
|
||||
|
|
|
@ -11,140 +11,133 @@
|
|||
// template <class T>
|
||||
// constexpr T bit_ceil(T x) noexcept;
|
||||
|
||||
// Constraints: T is an unsigned integer type
|
||||
// Returns: The minimal value y such that has_single_bit(y) is true and y >= x;
|
||||
// if y is not representable as a value of type T, the result is an unspecified value.
|
||||
// Remarks: This function shall not participate in overload resolution unless
|
||||
// T is an unsigned integer type
|
||||
|
||||
#include <bit>
|
||||
#include <cassert>
|
||||
#include <cstdint>
|
||||
#include <type_traits>
|
||||
#include <cassert>
|
||||
|
||||
#include "test_macros.h"
|
||||
|
||||
class A{};
|
||||
struct A {};
|
||||
enum E1 : unsigned char { rEd };
|
||||
enum class E2 : unsigned char { red };
|
||||
|
||||
template <typename T>
|
||||
constexpr bool constexpr_test()
|
||||
template <class T>
|
||||
constexpr bool test()
|
||||
{
|
||||
return std::bit_ceil(T(0)) == T(1)
|
||||
&& std::bit_ceil(T(1)) == T(1)
|
||||
&& std::bit_ceil(T(2)) == T(2)
|
||||
&& std::bit_ceil(T(3)) == T(4)
|
||||
&& std::bit_ceil(T(4)) == T(4)
|
||||
&& std::bit_ceil(T(5)) == T(8)
|
||||
&& std::bit_ceil(T(6)) == T(8)
|
||||
&& std::bit_ceil(T(7)) == T(8)
|
||||
&& std::bit_ceil(T(8)) == T(8)
|
||||
&& std::bit_ceil(T(9)) == T(16)
|
||||
;
|
||||
}
|
||||
ASSERT_SAME_TYPE(decltype(std::bit_ceil(T())), T);
|
||||
LIBCPP_ASSERT_NOEXCEPT(std::bit_ceil(T()));
|
||||
|
||||
assert(std::bit_ceil(T(0)) == T(1));
|
||||
assert(std::bit_ceil(T(1)) == T(1));
|
||||
assert(std::bit_ceil(T(2)) == T(2));
|
||||
assert(std::bit_ceil(T(3)) == T(4));
|
||||
assert(std::bit_ceil(T(4)) == T(4));
|
||||
assert(std::bit_ceil(T(5)) == T(8));
|
||||
assert(std::bit_ceil(T(6)) == T(8));
|
||||
assert(std::bit_ceil(T(7)) == T(8));
|
||||
assert(std::bit_ceil(T(8)) == T(8));
|
||||
assert(std::bit_ceil(T(9)) == T(16));
|
||||
assert(std::bit_ceil(T(60)) == T(64));
|
||||
assert(std::bit_ceil(T(61)) == T(64));
|
||||
assert(std::bit_ceil(T(62)) == T(64));
|
||||
assert(std::bit_ceil(T(63)) == T(64));
|
||||
assert(std::bit_ceil(T(64)) == T(64));
|
||||
assert(std::bit_ceil(T(65)) == T(128));
|
||||
assert(std::bit_ceil(T(66)) == T(128));
|
||||
assert(std::bit_ceil(T(67)) == T(128));
|
||||
assert(std::bit_ceil(T(68)) == T(128));
|
||||
assert(std::bit_ceil(T(69)) == T(128));
|
||||
|
||||
template <typename T>
|
||||
void runtime_test()
|
||||
{
|
||||
ASSERT_SAME_TYPE(T, decltype(std::bit_ceil(T(0))));
|
||||
LIBCPP_ASSERT_NOEXCEPT( std::bit_ceil(T(0)));
|
||||
#ifndef _LIBCPP_HAS_NO_INT128
|
||||
if constexpr (std::is_same_v<T, __uint128_t>) {
|
||||
T val = 168;
|
||||
T ceil = 256;
|
||||
|
||||
assert( std::bit_ceil(T(60)) == T( 64));
|
||||
assert( std::bit_ceil(T(61)) == T( 64));
|
||||
assert( std::bit_ceil(T(62)) == T( 64));
|
||||
assert( std::bit_ceil(T(63)) == T( 64));
|
||||
assert( std::bit_ceil(T(64)) == T( 64));
|
||||
assert( std::bit_ceil(T(65)) == T(128));
|
||||
assert( std::bit_ceil(T(66)) == T(128));
|
||||
assert( std::bit_ceil(T(67)) == T(128));
|
||||
assert( std::bit_ceil(T(68)) == T(128));
|
||||
assert( std::bit_ceil(T(69)) == T(128));
|
||||
assert(std::bit_ceil(val) == ceil);
|
||||
assert(std::bit_ceil(val << 32) == (ceil << 32));
|
||||
assert(std::bit_ceil((val << 64) | 0x1) == (ceil << 64));
|
||||
assert(std::bit_ceil((val << 72) | 0x1) == (ceil << 72));
|
||||
assert(std::bit_ceil((val << 100) | 0x1) == (ceil << 100));
|
||||
}
|
||||
#endif
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
int main(int, char**)
|
||||
{
|
||||
|
||||
{
|
||||
auto lambda = [](auto x) -> decltype(std::bit_ceil(x)) {};
|
||||
using L = decltype(lambda);
|
||||
|
||||
static_assert( std::is_invocable_v<L, unsigned char>, "");
|
||||
static_assert( std::is_invocable_v<L, unsigned int>, "");
|
||||
static_assert( std::is_invocable_v<L, unsigned long>, "");
|
||||
static_assert( std::is_invocable_v<L, unsigned long long>, "");
|
||||
|
||||
static_assert( std::is_invocable_v<L, uint8_t>, "");
|
||||
static_assert( std::is_invocable_v<L, uint16_t>, "");
|
||||
static_assert( std::is_invocable_v<L, uint32_t>, "");
|
||||
static_assert( std::is_invocable_v<L, uint64_t>, "");
|
||||
static_assert( std::is_invocable_v<L, size_t>, "");
|
||||
|
||||
static_assert( std::is_invocable_v<L, uintmax_t>, "");
|
||||
static_assert( std::is_invocable_v<L, uintptr_t>, "");
|
||||
|
||||
|
||||
static_assert(!std::is_invocable_v<L, int>, "");
|
||||
static_assert(!std::is_invocable_v<L, signed int>, "");
|
||||
static_assert(!std::is_invocable_v<L, long>, "");
|
||||
static_assert(!std::is_invocable_v<L, long long>, "");
|
||||
|
||||
static_assert(!std::is_invocable_v<L, int8_t>, "");
|
||||
static_assert(!std::is_invocable_v<L, int16_t>, "");
|
||||
static_assert(!std::is_invocable_v<L, int32_t>, "");
|
||||
static_assert(!std::is_invocable_v<L, int64_t>, "");
|
||||
static_assert(!std::is_invocable_v<L, ptrdiff_t>, "");
|
||||
|
||||
static_assert(!std::is_invocable_v<L, bool>, "");
|
||||
static_assert(!std::is_invocable_v<L, signed char>, "");
|
||||
static_assert(!std::is_invocable_v<L, char16_t>, "");
|
||||
static_assert(!std::is_invocable_v<L, char32_t>, "");
|
||||
|
||||
static_assert(!std::is_invocable_v<L, signed char>);
|
||||
static_assert(!std::is_invocable_v<L, short>);
|
||||
static_assert(!std::is_invocable_v<L, int>);
|
||||
static_assert(!std::is_invocable_v<L, long>);
|
||||
static_assert(!std::is_invocable_v<L, long long>);
|
||||
#ifndef _LIBCPP_HAS_NO_INT128
|
||||
static_assert( std::is_invocable_v<L, __uint128_t>, "");
|
||||
static_assert(!std::is_invocable_v<L, __int128_t>, "");
|
||||
static_assert(!std::is_invocable_v<L, __int128_t>);
|
||||
#endif
|
||||
|
||||
static_assert(!std::is_invocable_v<L, A>, "");
|
||||
static_assert(!std::is_invocable_v<L, E1>, "");
|
||||
static_assert(!std::is_invocable_v<L, E2>, "");
|
||||
static_assert(!std::is_invocable_v<L, int8_t>);
|
||||
static_assert(!std::is_invocable_v<L, int16_t>);
|
||||
static_assert(!std::is_invocable_v<L, int32_t>);
|
||||
static_assert(!std::is_invocable_v<L, int64_t>);
|
||||
static_assert(!std::is_invocable_v<L, intmax_t>);
|
||||
static_assert(!std::is_invocable_v<L, intptr_t>);
|
||||
static_assert(!std::is_invocable_v<L, ptrdiff_t>);
|
||||
|
||||
static_assert(!std::is_invocable_v<L, bool>);
|
||||
static_assert(!std::is_invocable_v<L, char>);
|
||||
static_assert(!std::is_invocable_v<L, wchar_t>);
|
||||
#ifndef _LIBCPP_HAS_NO_CHAR8_T
|
||||
static_assert(!std::is_invocable_v<L, char8_t>);
|
||||
#endif
|
||||
static_assert(!std::is_invocable_v<L, char16_t>);
|
||||
static_assert(!std::is_invocable_v<L, char32_t>);
|
||||
|
||||
static_assert(!std::is_invocable_v<L, A>);
|
||||
static_assert(!std::is_invocable_v<L, A*>);
|
||||
static_assert(!std::is_invocable_v<L, E1>);
|
||||
static_assert(!std::is_invocable_v<L, E2>);
|
||||
}
|
||||
|
||||
static_assert(constexpr_test<unsigned char>(), "");
|
||||
static_assert(constexpr_test<unsigned short>(), "");
|
||||
static_assert(constexpr_test<unsigned>(), "");
|
||||
static_assert(constexpr_test<unsigned long>(), "");
|
||||
static_assert(constexpr_test<unsigned long long>(), "");
|
||||
|
||||
static_assert(constexpr_test<uint8_t>(), "");
|
||||
static_assert(constexpr_test<uint16_t>(), "");
|
||||
static_assert(constexpr_test<uint32_t>(), "");
|
||||
static_assert(constexpr_test<uint64_t>(), "");
|
||||
static_assert(constexpr_test<size_t>(), "");
|
||||
static_assert(constexpr_test<uintmax_t>(), "");
|
||||
static_assert(constexpr_test<uintptr_t>(), "");
|
||||
|
||||
static_assert(test<unsigned char>());
|
||||
static_assert(test<unsigned short>());
|
||||
static_assert(test<unsigned int>());
|
||||
static_assert(test<unsigned long>());
|
||||
static_assert(test<unsigned long long>());
|
||||
#ifndef _LIBCPP_HAS_NO_INT128
|
||||
static_assert(constexpr_test<__uint128_t>(), "");
|
||||
static_assert(test<__uint128_t>());
|
||||
#endif
|
||||
static_assert(test<uint8_t>());
|
||||
static_assert(test<uint16_t>());
|
||||
static_assert(test<uint32_t>());
|
||||
static_assert(test<uint64_t>());
|
||||
static_assert(test<uintmax_t>());
|
||||
static_assert(test<uintptr_t>());
|
||||
static_assert(test<size_t>());
|
||||
|
||||
runtime_test<unsigned char>();
|
||||
runtime_test<unsigned>();
|
||||
runtime_test<unsigned short>();
|
||||
runtime_test<unsigned long>();
|
||||
runtime_test<unsigned long long>();
|
||||
|
||||
runtime_test<uint8_t>();
|
||||
runtime_test<uint16_t>();
|
||||
runtime_test<uint32_t>();
|
||||
runtime_test<uint64_t>();
|
||||
runtime_test<size_t>();
|
||||
runtime_test<uintmax_t>();
|
||||
runtime_test<uintptr_t>();
|
||||
|
||||
test<unsigned char>();
|
||||
test<unsigned short>();
|
||||
test<unsigned int>();
|
||||
test<unsigned long>();
|
||||
test<unsigned long long>();
|
||||
#ifndef _LIBCPP_HAS_NO_INT128
|
||||
runtime_test<__uint128_t>();
|
||||
test<__uint128_t>();
|
||||
#endif
|
||||
test<uint8_t>();
|
||||
test<uint16_t>();
|
||||
test<uint32_t>();
|
||||
test<uint64_t>();
|
||||
test<uintmax_t>();
|
||||
test<uintptr_t>();
|
||||
test<size_t>();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -11,155 +11,131 @@
|
|||
// template <class T>
|
||||
// constexpr T bit_floor(T x) noexcept;
|
||||
|
||||
// Constraints: T is an unsigned integer type
|
||||
// Returns: If x == 0, 0; otherwise the maximal value y such that bit_floor(y) is true and y <= x.
|
||||
// Remarks: This function shall not participate in overload resolution unless
|
||||
// T is an unsigned integer type
|
||||
|
||||
#include <bit>
|
||||
#include <cassert>
|
||||
#include <cstdint>
|
||||
#include <type_traits>
|
||||
#include <cassert>
|
||||
|
||||
#include "test_macros.h"
|
||||
|
||||
class A{};
|
||||
struct A {};
|
||||
enum E1 : unsigned char { rEd };
|
||||
enum class E2 : unsigned char { red };
|
||||
|
||||
template <typename T>
|
||||
constexpr bool constexpr_test()
|
||||
template <class T>
|
||||
constexpr bool test()
|
||||
{
|
||||
return std::bit_floor(T(0)) == T(0)
|
||||
&& std::bit_floor(T(1)) == T(1)
|
||||
&& std::bit_floor(T(2)) == T(2)
|
||||
&& std::bit_floor(T(3)) == T(2)
|
||||
&& std::bit_floor(T(4)) == T(4)
|
||||
&& std::bit_floor(T(5)) == T(4)
|
||||
&& std::bit_floor(T(6)) == T(4)
|
||||
&& std::bit_floor(T(7)) == T(4)
|
||||
&& std::bit_floor(T(8)) == T(8)
|
||||
&& std::bit_floor(T(9)) == T(8)
|
||||
;
|
||||
}
|
||||
ASSERT_SAME_TYPE(decltype(std::bit_floor(T())), T);
|
||||
LIBCPP_ASSERT_NOEXCEPT(std::bit_floor(T()));
|
||||
T max = std::numeric_limits<T>::max();
|
||||
|
||||
assert(std::bit_floor(T(0)) == T(0));
|
||||
assert(std::bit_floor(T(1)) == T(1));
|
||||
assert(std::bit_floor(T(2)) == T(2));
|
||||
assert(std::bit_floor(T(3)) == T(2));
|
||||
assert(std::bit_floor(T(4)) == T(4));
|
||||
assert(std::bit_floor(T(5)) == T(4));
|
||||
assert(std::bit_floor(T(6)) == T(4));
|
||||
assert(std::bit_floor(T(7)) == T(4));
|
||||
assert(std::bit_floor(T(8)) == T(8));
|
||||
assert(std::bit_floor(T(9)) == T(8));
|
||||
assert(std::bit_floor(T(125)) == T(64));
|
||||
assert(std::bit_floor(T(126)) == T(64));
|
||||
assert(std::bit_floor(T(127)) == T(64));
|
||||
assert(std::bit_floor(T(128)) == T(128));
|
||||
assert(std::bit_floor(T(129)) == T(128));
|
||||
assert(std::bit_floor(max) == T(max - (max >> 1)));
|
||||
|
||||
template <typename T>
|
||||
void runtime_test()
|
||||
{
|
||||
ASSERT_SAME_TYPE(T, decltype(std::bit_floor(T(0))));
|
||||
ASSERT_NOEXCEPT( std::bit_floor(T(0)));
|
||||
#ifndef _LIBCPP_HAS_NO_INT128
|
||||
if constexpr (std::is_same_v<T, __uint128_t>) {
|
||||
T val = T(128) << 32;
|
||||
assert(std::bit_floor(val-1) == val/2);
|
||||
assert(std::bit_floor(val) == val);
|
||||
assert(std::bit_floor(val+1) == val);
|
||||
val <<= 60;
|
||||
assert(std::bit_floor(val-1) == val/2);
|
||||
assert(std::bit_floor(val) == val);
|
||||
assert(std::bit_floor(val+1) == val);
|
||||
}
|
||||
#endif
|
||||
|
||||
assert( std::bit_floor(T(121)) == T(64));
|
||||
assert( std::bit_floor(T(122)) == T(64));
|
||||
assert( std::bit_floor(T(123)) == T(64));
|
||||
assert( std::bit_floor(T(124)) == T(64));
|
||||
assert( std::bit_floor(T(125)) == T(64));
|
||||
assert( std::bit_floor(T(126)) == T(64));
|
||||
assert( std::bit_floor(T(127)) == T(64));
|
||||
assert( std::bit_floor(T(128)) == T(128));
|
||||
assert( std::bit_floor(T(129)) == T(128));
|
||||
assert( std::bit_floor(T(130)) == T(128));
|
||||
return true;
|
||||
}
|
||||
|
||||
int main(int, char**)
|
||||
{
|
||||
|
||||
{
|
||||
auto lambda = [](auto x) -> decltype(std::bit_floor(x)) {};
|
||||
using L = decltype(lambda);
|
||||
|
||||
static_assert( std::is_invocable_v<L, unsigned char>, "");
|
||||
static_assert( std::is_invocable_v<L, unsigned int>, "");
|
||||
static_assert( std::is_invocable_v<L, unsigned long>, "");
|
||||
static_assert( std::is_invocable_v<L, unsigned long long>, "");
|
||||
|
||||
static_assert( std::is_invocable_v<L, uint8_t>, "");
|
||||
static_assert( std::is_invocable_v<L, uint16_t>, "");
|
||||
static_assert( std::is_invocable_v<L, uint32_t>, "");
|
||||
static_assert( std::is_invocable_v<L, uint64_t>, "");
|
||||
static_assert( std::is_invocable_v<L, size_t>, "");
|
||||
|
||||
static_assert( std::is_invocable_v<L, uintmax_t>, "");
|
||||
static_assert( std::is_invocable_v<L, uintptr_t>, "");
|
||||
|
||||
|
||||
static_assert(!std::is_invocable_v<L, int>, "");
|
||||
static_assert(!std::is_invocable_v<L, signed int>, "");
|
||||
static_assert(!std::is_invocable_v<L, long>, "");
|
||||
static_assert(!std::is_invocable_v<L, long long>, "");
|
||||
|
||||
static_assert(!std::is_invocable_v<L, int8_t>, "");
|
||||
static_assert(!std::is_invocable_v<L, int16_t>, "");
|
||||
static_assert(!std::is_invocable_v<L, int32_t>, "");
|
||||
static_assert(!std::is_invocable_v<L, int64_t>, "");
|
||||
static_assert(!std::is_invocable_v<L, ptrdiff_t>, "");
|
||||
|
||||
static_assert(!std::is_invocable_v<L, bool>, "");
|
||||
static_assert(!std::is_invocable_v<L, signed char>, "");
|
||||
static_assert(!std::is_invocable_v<L, char16_t>, "");
|
||||
static_assert(!std::is_invocable_v<L, char32_t>, "");
|
||||
|
||||
static_assert(!std::is_invocable_v<L, signed char>);
|
||||
static_assert(!std::is_invocable_v<L, short>);
|
||||
static_assert(!std::is_invocable_v<L, int>);
|
||||
static_assert(!std::is_invocable_v<L, long>);
|
||||
static_assert(!std::is_invocable_v<L, long long>);
|
||||
#ifndef _LIBCPP_HAS_NO_INT128
|
||||
static_assert( std::is_invocable_v<L, __uint128_t>, "");
|
||||
static_assert(!std::is_invocable_v<L, __int128_t>, "");
|
||||
static_assert(!std::is_invocable_v<L, __int128_t>);
|
||||
#endif
|
||||
|
||||
static_assert(!std::is_invocable_v<L, A>, "");
|
||||
static_assert(!std::is_invocable_v<L, E1>, "");
|
||||
static_assert(!std::is_invocable_v<L, E2>, "");
|
||||
static_assert(!std::is_invocable_v<L, int8_t>);
|
||||
static_assert(!std::is_invocable_v<L, int16_t>);
|
||||
static_assert(!std::is_invocable_v<L, int32_t>);
|
||||
static_assert(!std::is_invocable_v<L, int64_t>);
|
||||
static_assert(!std::is_invocable_v<L, intmax_t>);
|
||||
static_assert(!std::is_invocable_v<L, intptr_t>);
|
||||
static_assert(!std::is_invocable_v<L, ptrdiff_t>);
|
||||
|
||||
static_assert(!std::is_invocable_v<L, bool>);
|
||||
static_assert(!std::is_invocable_v<L, char>);
|
||||
static_assert(!std::is_invocable_v<L, wchar_t>);
|
||||
#ifndef _LIBCPP_HAS_NO_CHAR8_T
|
||||
static_assert(!std::is_invocable_v<L, char8_t>);
|
||||
#endif
|
||||
static_assert(!std::is_invocable_v<L, char16_t>);
|
||||
static_assert(!std::is_invocable_v<L, char32_t>);
|
||||
|
||||
static_assert(!std::is_invocable_v<L, A>);
|
||||
static_assert(!std::is_invocable_v<L, A*>);
|
||||
static_assert(!std::is_invocable_v<L, E1>);
|
||||
static_assert(!std::is_invocable_v<L, E2>);
|
||||
}
|
||||
|
||||
static_assert(constexpr_test<unsigned char>(), "");
|
||||
static_assert(constexpr_test<unsigned short>(), "");
|
||||
static_assert(constexpr_test<unsigned>(), "");
|
||||
static_assert(constexpr_test<unsigned long>(), "");
|
||||
static_assert(constexpr_test<unsigned long long>(), "");
|
||||
|
||||
static_assert(constexpr_test<uint8_t>(), "");
|
||||
static_assert(constexpr_test<uint16_t>(), "");
|
||||
static_assert(constexpr_test<uint32_t>(), "");
|
||||
static_assert(constexpr_test<uint64_t>(), "");
|
||||
static_assert(constexpr_test<size_t>(), "");
|
||||
static_assert(constexpr_test<uintmax_t>(), "");
|
||||
static_assert(constexpr_test<uintptr_t>(), "");
|
||||
|
||||
static_assert(test<unsigned char>());
|
||||
static_assert(test<unsigned short>());
|
||||
static_assert(test<unsigned int>());
|
||||
static_assert(test<unsigned long>());
|
||||
static_assert(test<unsigned long long>());
|
||||
#ifndef _LIBCPP_HAS_NO_INT128
|
||||
static_assert(constexpr_test<__uint128_t>(), "");
|
||||
static_assert(test<__uint128_t>());
|
||||
#endif
|
||||
|
||||
runtime_test<unsigned char>();
|
||||
runtime_test<unsigned>();
|
||||
runtime_test<unsigned short>();
|
||||
runtime_test<unsigned long>();
|
||||
runtime_test<unsigned long long>();
|
||||
|
||||
runtime_test<uint8_t>();
|
||||
runtime_test<uint16_t>();
|
||||
runtime_test<uint32_t>();
|
||||
runtime_test<uint64_t>();
|
||||
runtime_test<size_t>();
|
||||
runtime_test<uintmax_t>();
|
||||
runtime_test<uintptr_t>();
|
||||
static_assert(test<uint8_t>());
|
||||
static_assert(test<uint16_t>());
|
||||
static_assert(test<uint32_t>());
|
||||
static_assert(test<uint64_t>());
|
||||
static_assert(test<uintmax_t>());
|
||||
static_assert(test<uintptr_t>());
|
||||
static_assert(test<size_t>());
|
||||
|
||||
test<unsigned char>();
|
||||
test<unsigned short>();
|
||||
test<unsigned int>();
|
||||
test<unsigned long>();
|
||||
test<unsigned long long>();
|
||||
#ifndef _LIBCPP_HAS_NO_INT128
|
||||
runtime_test<__uint128_t>();
|
||||
|
||||
{
|
||||
__uint128_t val = 128;
|
||||
val <<= 32;
|
||||
assert( std::bit_floor(val-1) == val/2);
|
||||
assert( std::bit_floor(val) == val);
|
||||
assert( std::bit_floor(val+1) == val);
|
||||
val <<= 2;
|
||||
assert( std::bit_floor(val-1) == val/2);
|
||||
assert( std::bit_floor(val) == val);
|
||||
assert( std::bit_floor(val+1) == val);
|
||||
val <<= 3;
|
||||
assert( std::bit_floor(val-1) == val/2);
|
||||
assert( std::bit_floor(val) == val);
|
||||
assert( std::bit_floor(val+1) == val);
|
||||
}
|
||||
test<__uint128_t>();
|
||||
#endif
|
||||
|
||||
test<uint8_t>();
|
||||
test<uint16_t>();
|
||||
test<uint32_t>();
|
||||
test<uint64_t>();
|
||||
test<uintmax_t>();
|
||||
test<uintptr_t>();
|
||||
test<size_t>();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -11,66 +11,62 @@
|
|||
// template <class T>
|
||||
// constexpr T bit_width(T x) noexcept;
|
||||
|
||||
// If x == 0, 0; otherwise one plus the base-2 logarithm of x, with any fractional part discarded.
|
||||
// Constraints: T is an unsigned integer type
|
||||
// Returns: If x == 0, 0; otherwise one plus the base-2 logarithm of x, with any fractional part discarded.
|
||||
|
||||
// Remarks: This function shall not participate in overload resolution unless
|
||||
// T is an unsigned integer type
|
||||
|
||||
#include <bit>
|
||||
#include <cstdint>
|
||||
#include <cassert>
|
||||
#include <cstdint>
|
||||
#include <type_traits>
|
||||
|
||||
#include "test_macros.h"
|
||||
|
||||
class A{};
|
||||
struct A {};
|
||||
enum E1 : unsigned char { rEd };
|
||||
enum class E2 : unsigned char { red };
|
||||
|
||||
template <typename T>
|
||||
constexpr bool constexpr_test()
|
||||
template <class T>
|
||||
constexpr bool test()
|
||||
{
|
||||
return std::bit_width(T(0)) == T(0)
|
||||
&& std::bit_width(T(1)) == T(1)
|
||||
&& std::bit_width(T(2)) == T(2)
|
||||
&& std::bit_width(T(3)) == T(2)
|
||||
&& std::bit_width(T(4)) == T(3)
|
||||
&& std::bit_width(T(5)) == T(3)
|
||||
&& std::bit_width(T(6)) == T(3)
|
||||
&& std::bit_width(T(7)) == T(3)
|
||||
&& std::bit_width(T(8)) == T(4)
|
||||
&& std::bit_width(T(9)) == T(4)
|
||||
;
|
||||
}
|
||||
ASSERT_SAME_TYPE(decltype(std::bit_width(T())), T);
|
||||
ASSERT_NOEXCEPT(std::bit_width(T()));
|
||||
T max = std::numeric_limits<T>::max();
|
||||
|
||||
assert(std::bit_width(T(0)) == T(0));
|
||||
assert(std::bit_width(T(1)) == T(1));
|
||||
assert(std::bit_width(T(2)) == T(2));
|
||||
assert(std::bit_width(T(3)) == T(2));
|
||||
assert(std::bit_width(T(4)) == T(3));
|
||||
assert(std::bit_width(T(5)) == T(3));
|
||||
assert(std::bit_width(T(6)) == T(3));
|
||||
assert(std::bit_width(T(7)) == T(3));
|
||||
assert(std::bit_width(T(8)) == T(4));
|
||||
assert(std::bit_width(T(9)) == T(4));
|
||||
assert(std::bit_width(T(125)) == T(7));
|
||||
assert(std::bit_width(T(126)) == T(7));
|
||||
assert(std::bit_width(T(127)) == T(7));
|
||||
assert(std::bit_width(T(128)) == T(8));
|
||||
assert(std::bit_width(T(129)) == T(8));
|
||||
assert(std::bit_width(T(130)) == T(8));
|
||||
assert(std::bit_width(T(max - 1)) == T(std::numeric_limits<T>::digits));
|
||||
assert(std::bit_width(max) == T(std::numeric_limits<T>::digits));
|
||||
|
||||
template <typename T>
|
||||
void runtime_test()
|
||||
{
|
||||
ASSERT_SAME_TYPE(T, decltype(std::bit_width(T(0))));
|
||||
ASSERT_NOEXCEPT( std::bit_width(T(0)));
|
||||
#ifndef _LIBCPP_HAS_NO_INT128
|
||||
if constexpr (std::is_same_v<T, __uint128_t>) {
|
||||
T val = 128;
|
||||
val <<= 32;
|
||||
assert(std::bit_width(val-1) == 39);
|
||||
assert(std::bit_width(val) == 40);
|
||||
assert(std::bit_width(val+1) == 40);
|
||||
val <<= 60;
|
||||
assert(std::bit_width(val-1) == 99);
|
||||
assert(std::bit_width(val) == 100);
|
||||
assert(std::bit_width(val+1) == 100);
|
||||
}
|
||||
#endif
|
||||
|
||||
assert( std::bit_width(T(0)) == T(0));
|
||||
assert( std::bit_width(T(1)) == T(1));
|
||||
assert( std::bit_width(T(2)) == T(2));
|
||||
assert( std::bit_width(T(3)) == T(2));
|
||||
assert( std::bit_width(T(4)) == T(3));
|
||||
assert( std::bit_width(T(5)) == T(3));
|
||||
assert( std::bit_width(T(6)) == T(3));
|
||||
assert( std::bit_width(T(7)) == T(3));
|
||||
assert( std::bit_width(T(8)) == T(4));
|
||||
assert( std::bit_width(T(9)) == T(4));
|
||||
|
||||
|
||||
assert( std::bit_width(T(121)) == T(7));
|
||||
assert( std::bit_width(T(122)) == T(7));
|
||||
assert( std::bit_width(T(123)) == T(7));
|
||||
assert( std::bit_width(T(124)) == T(7));
|
||||
assert( std::bit_width(T(125)) == T(7));
|
||||
assert( std::bit_width(T(126)) == T(7));
|
||||
assert( std::bit_width(T(127)) == T(7));
|
||||
assert( std::bit_width(T(128)) == T(8));
|
||||
assert( std::bit_width(T(129)) == T(8));
|
||||
assert( std::bit_width(T(130)) == T(8));
|
||||
return true;
|
||||
}
|
||||
|
||||
int main(int, char**)
|
||||
|
@ -80,99 +76,69 @@ int main(int, char**)
|
|||
auto lambda = [](auto x) -> decltype(std::bit_width(x)) {};
|
||||
using L = decltype(lambda);
|
||||
|
||||
static_assert( std::is_invocable_v<L, unsigned char>, "");
|
||||
static_assert( std::is_invocable_v<L, unsigned int>, "");
|
||||
static_assert( std::is_invocable_v<L, unsigned long>, "");
|
||||
static_assert( std::is_invocable_v<L, unsigned long long>, "");
|
||||
|
||||
static_assert( std::is_invocable_v<L, uint8_t>, "");
|
||||
static_assert( std::is_invocable_v<L, uint16_t>, "");
|
||||
static_assert( std::is_invocable_v<L, uint32_t>, "");
|
||||
static_assert( std::is_invocable_v<L, uint64_t>, "");
|
||||
static_assert( std::is_invocable_v<L, size_t>, "");
|
||||
|
||||
static_assert( std::is_invocable_v<L, uintmax_t>, "");
|
||||
static_assert( std::is_invocable_v<L, uintptr_t>, "");
|
||||
|
||||
|
||||
static_assert(!std::is_invocable_v<L, int>, "");
|
||||
static_assert(!std::is_invocable_v<L, signed int>, "");
|
||||
static_assert(!std::is_invocable_v<L, long>, "");
|
||||
static_assert(!std::is_invocable_v<L, long long>, "");
|
||||
|
||||
static_assert(!std::is_invocable_v<L, int8_t>, "");
|
||||
static_assert(!std::is_invocable_v<L, int16_t>, "");
|
||||
static_assert(!std::is_invocable_v<L, int32_t>, "");
|
||||
static_assert(!std::is_invocable_v<L, int64_t>, "");
|
||||
static_assert(!std::is_invocable_v<L, ptrdiff_t>, "");
|
||||
|
||||
static_assert(!std::is_invocable_v<L, bool>, "");
|
||||
static_assert(!std::is_invocable_v<L, signed char>, "");
|
||||
static_assert(!std::is_invocable_v<L, char16_t>, "");
|
||||
static_assert(!std::is_invocable_v<L, char32_t>, "");
|
||||
|
||||
static_assert(!std::is_invocable_v<L, signed char>);
|
||||
static_assert(!std::is_invocable_v<L, short>);
|
||||
static_assert(!std::is_invocable_v<L, int>);
|
||||
static_assert(!std::is_invocable_v<L, long>);
|
||||
static_assert(!std::is_invocable_v<L, long long>);
|
||||
#ifndef _LIBCPP_HAS_NO_INT128
|
||||
static_assert( std::is_invocable_v<L, __uint128_t>, "");
|
||||
static_assert(!std::is_invocable_v<L, __int128_t>, "");
|
||||
static_assert(!std::is_invocable_v<L, __int128_t>);
|
||||
#endif
|
||||
|
||||
static_assert(!std::is_invocable_v<L, A>, "");
|
||||
static_assert(!std::is_invocable_v<L, E1>, "");
|
||||
static_assert(!std::is_invocable_v<L, E2>, "");
|
||||
static_assert(!std::is_invocable_v<L, int8_t>);
|
||||
static_assert(!std::is_invocable_v<L, int16_t>);
|
||||
static_assert(!std::is_invocable_v<L, int32_t>);
|
||||
static_assert(!std::is_invocable_v<L, int64_t>);
|
||||
static_assert(!std::is_invocable_v<L, intmax_t>);
|
||||
static_assert(!std::is_invocable_v<L, intptr_t>);
|
||||
static_assert(!std::is_invocable_v<L, ptrdiff_t>);
|
||||
|
||||
static_assert(!std::is_invocable_v<L, bool>);
|
||||
static_assert(!std::is_invocable_v<L, char>);
|
||||
static_assert(!std::is_invocable_v<L, wchar_t>);
|
||||
#ifndef _LIBCPP_HAS_NO_CHAR8_T
|
||||
static_assert(!std::is_invocable_v<L, char8_t>);
|
||||
#endif
|
||||
static_assert(!std::is_invocable_v<L, char16_t>);
|
||||
static_assert(!std::is_invocable_v<L, char32_t>);
|
||||
|
||||
static_assert(!std::is_invocable_v<L, A>);
|
||||
static_assert(!std::is_invocable_v<L, A*>);
|
||||
static_assert(!std::is_invocable_v<L, E1>);
|
||||
static_assert(!std::is_invocable_v<L, E2>);
|
||||
}
|
||||
|
||||
static_assert(constexpr_test<unsigned char>(), "");
|
||||
static_assert(constexpr_test<unsigned short>(), "");
|
||||
static_assert(constexpr_test<unsigned>(), "");
|
||||
static_assert(constexpr_test<unsigned long>(), "");
|
||||
static_assert(constexpr_test<unsigned long long>(), "");
|
||||
|
||||
static_assert(constexpr_test<uint8_t>(), "");
|
||||
static_assert(constexpr_test<uint16_t>(), "");
|
||||
static_assert(constexpr_test<uint32_t>(), "");
|
||||
static_assert(constexpr_test<uint64_t>(), "");
|
||||
static_assert(constexpr_test<size_t>(), "");
|
||||
static_assert(constexpr_test<uintmax_t>(), "");
|
||||
static_assert(constexpr_test<uintptr_t>(), "");
|
||||
|
||||
static_assert(test<unsigned char>());
|
||||
static_assert(test<unsigned short>());
|
||||
static_assert(test<unsigned int>());
|
||||
static_assert(test<unsigned long>());
|
||||
static_assert(test<unsigned long long>());
|
||||
#ifndef _LIBCPP_HAS_NO_INT128
|
||||
static_assert(constexpr_test<__uint128_t>(), "");
|
||||
static_assert(test<__uint128_t>());
|
||||
#endif
|
||||
static_assert(test<uint8_t>());
|
||||
static_assert(test<uint16_t>());
|
||||
static_assert(test<uint32_t>());
|
||||
static_assert(test<uint64_t>());
|
||||
static_assert(test<uintmax_t>());
|
||||
static_assert(test<uintptr_t>());
|
||||
static_assert(test<size_t>());
|
||||
|
||||
|
||||
runtime_test<unsigned char>();
|
||||
runtime_test<unsigned>();
|
||||
runtime_test<unsigned short>();
|
||||
runtime_test<unsigned long>();
|
||||
runtime_test<unsigned long long>();
|
||||
|
||||
runtime_test<uint8_t>();
|
||||
runtime_test<uint16_t>();
|
||||
runtime_test<uint32_t>();
|
||||
runtime_test<uint64_t>();
|
||||
runtime_test<size_t>();
|
||||
runtime_test<uintmax_t>();
|
||||
runtime_test<uintptr_t>();
|
||||
|
||||
test<unsigned char>();
|
||||
test<unsigned short>();
|
||||
test<unsigned int>();
|
||||
test<unsigned long>();
|
||||
test<unsigned long long>();
|
||||
#ifndef _LIBCPP_HAS_NO_INT128
|
||||
runtime_test<__uint128_t>();
|
||||
|
||||
{
|
||||
__uint128_t val = 128;
|
||||
val <<= 32;
|
||||
assert( std::bit_width(val-1) == 39);
|
||||
assert( std::bit_width(val) == 40);
|
||||
assert( std::bit_width(val+1) == 40);
|
||||
val <<= 2;
|
||||
assert( std::bit_width(val-1) == 41);
|
||||
assert( std::bit_width(val) == 42);
|
||||
assert( std::bit_width(val+1) == 42);
|
||||
val <<= 3;
|
||||
assert( std::bit_width(val-1) == 44);
|
||||
assert( std::bit_width(val) == 45);
|
||||
assert( std::bit_width(val+1) == 45);
|
||||
}
|
||||
test<__uint128_t>();
|
||||
#endif
|
||||
test<uint8_t>();
|
||||
test<uint16_t>();
|
||||
test<uint32_t>();
|
||||
test<uint64_t>();
|
||||
test<uintmax_t>();
|
||||
test<uintptr_t>();
|
||||
test<size_t>();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -11,153 +11,132 @@
|
|||
// template <class T>
|
||||
// constexpr bool has_single_bit(T x) noexcept;
|
||||
|
||||
// Remarks: This function shall not participate in overload resolution unless
|
||||
// T is an unsigned integer type
|
||||
// Constraints: T is an unsigned integer type
|
||||
|
||||
#include <bit>
|
||||
#include <cassert>
|
||||
#include <cstdint>
|
||||
#include <type_traits>
|
||||
#include <cassert>
|
||||
|
||||
#include "test_macros.h"
|
||||
|
||||
class A{};
|
||||
struct A {};
|
||||
enum E1 : unsigned char { rEd };
|
||||
enum class E2 : unsigned char { red };
|
||||
|
||||
template <typename T>
|
||||
constexpr bool constexpr_test()
|
||||
template <class T>
|
||||
constexpr bool test()
|
||||
{
|
||||
return std::has_single_bit(T(1))
|
||||
&& std::has_single_bit(T(2))
|
||||
&& !std::has_single_bit(T(3))
|
||||
&& std::has_single_bit(T(4))
|
||||
&& !std::has_single_bit(T(5))
|
||||
&& !std::has_single_bit(T(6))
|
||||
&& !std::has_single_bit(T(7))
|
||||
&& std::has_single_bit(T(8))
|
||||
&& !std::has_single_bit(T(9))
|
||||
;
|
||||
}
|
||||
ASSERT_SAME_TYPE(decltype(std::has_single_bit(T())), bool);
|
||||
ASSERT_NOEXCEPT(std::has_single_bit(T()));
|
||||
T max = std::numeric_limits<T>::max();
|
||||
|
||||
assert(!std::has_single_bit(T(0)));
|
||||
assert( std::has_single_bit(T(1)));
|
||||
assert( std::has_single_bit(T(2)));
|
||||
assert(!std::has_single_bit(T(3)));
|
||||
assert( std::has_single_bit(T(4)));
|
||||
assert(!std::has_single_bit(T(5)));
|
||||
assert(!std::has_single_bit(T(6)));
|
||||
assert(!std::has_single_bit(T(7)));
|
||||
assert( std::has_single_bit(T(8)));
|
||||
assert(!std::has_single_bit(T(9)));
|
||||
assert(!std::has_single_bit(T(127)));
|
||||
assert( std::has_single_bit(T(128)));
|
||||
assert(!std::has_single_bit(T(129)));
|
||||
assert(!std::has_single_bit(max));
|
||||
|
||||
template <typename T>
|
||||
void runtime_test()
|
||||
{
|
||||
ASSERT_SAME_TYPE(bool, decltype(std::has_single_bit(T(0))));
|
||||
ASSERT_NOEXCEPT( std::has_single_bit(T(0)));
|
||||
#ifndef _LIBCPP_HAS_NO_INT128
|
||||
if constexpr (std::is_same_v<T, __uint128_t>) {
|
||||
T val = T(1) << 32;
|
||||
assert(!std::has_single_bit(val-1));
|
||||
assert( std::has_single_bit(val));
|
||||
assert(!std::has_single_bit(val+1));
|
||||
val <<= 60;
|
||||
assert(!std::has_single_bit(val-1));
|
||||
assert( std::has_single_bit(val));
|
||||
assert(!std::has_single_bit(val+1));
|
||||
|
||||
assert(!std::has_single_bit(T(121)));
|
||||
assert(!std::has_single_bit(T(122)));
|
||||
assert(!std::has_single_bit(T(123)));
|
||||
assert(!std::has_single_bit(T(124)));
|
||||
assert(!std::has_single_bit(T(125)));
|
||||
assert(!std::has_single_bit(T(126)));
|
||||
assert(!std::has_single_bit(T(127)));
|
||||
assert( std::has_single_bit(T(128)));
|
||||
assert(!std::has_single_bit(T(129)));
|
||||
assert(!std::has_single_bit(T(130)));
|
||||
T x = (T(1) << 63);
|
||||
T y = (T(1) << 64);
|
||||
assert( std::has_single_bit(x));
|
||||
assert( std::has_single_bit(y));
|
||||
assert(!std::has_single_bit(x + y));
|
||||
}
|
||||
#endif
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
int main(int, char**)
|
||||
{
|
||||
|
||||
{
|
||||
auto lambda = [](auto x) -> decltype(std::has_single_bit(x)) {};
|
||||
using L = decltype(lambda);
|
||||
|
||||
static_assert( std::is_invocable_v<L, unsigned char>, "");
|
||||
static_assert( std::is_invocable_v<L, unsigned int>, "");
|
||||
static_assert( std::is_invocable_v<L, unsigned long>, "");
|
||||
static_assert( std::is_invocable_v<L, unsigned long long>, "");
|
||||
|
||||
static_assert( std::is_invocable_v<L, uint8_t>, "");
|
||||
static_assert( std::is_invocable_v<L, uint16_t>, "");
|
||||
static_assert( std::is_invocable_v<L, uint32_t>, "");
|
||||
static_assert( std::is_invocable_v<L, uint64_t>, "");
|
||||
static_assert( std::is_invocable_v<L, size_t>, "");
|
||||
|
||||
static_assert( std::is_invocable_v<L, uintmax_t>, "");
|
||||
static_assert( std::is_invocable_v<L, uintptr_t>, "");
|
||||
|
||||
|
||||
static_assert(!std::is_invocable_v<L, int>, "");
|
||||
static_assert(!std::is_invocable_v<L, signed int>, "");
|
||||
static_assert(!std::is_invocable_v<L, long>, "");
|
||||
static_assert(!std::is_invocable_v<L, long long>, "");
|
||||
|
||||
static_assert(!std::is_invocable_v<L, int8_t>, "");
|
||||
static_assert(!std::is_invocable_v<L, int16_t>, "");
|
||||
static_assert(!std::is_invocable_v<L, int32_t>, "");
|
||||
static_assert(!std::is_invocable_v<L, int64_t>, "");
|
||||
static_assert(!std::is_invocable_v<L, ptrdiff_t>, "");
|
||||
|
||||
static_assert(!std::is_invocable_v<L, bool>, "");
|
||||
static_assert(!std::is_invocable_v<L, signed char>, "");
|
||||
static_assert(!std::is_invocable_v<L, char16_t>, "");
|
||||
static_assert(!std::is_invocable_v<L, char32_t>, "");
|
||||
|
||||
static_assert(!std::is_invocable_v<L, signed char>);
|
||||
static_assert(!std::is_invocable_v<L, short>);
|
||||
static_assert(!std::is_invocable_v<L, int>);
|
||||
static_assert(!std::is_invocable_v<L, long>);
|
||||
static_assert(!std::is_invocable_v<L, long long>);
|
||||
#ifndef _LIBCPP_HAS_NO_INT128
|
||||
static_assert( std::is_invocable_v<L, __uint128_t>, "");
|
||||
static_assert(!std::is_invocable_v<L, __int128_t>, "");
|
||||
static_assert(!std::is_invocable_v<L, __int128_t>);
|
||||
#endif
|
||||
|
||||
static_assert(!std::is_invocable_v<L, A>, "");
|
||||
static_assert(!std::is_invocable_v<L, E1>, "");
|
||||
static_assert(!std::is_invocable_v<L, E2>, "");
|
||||
static_assert(!std::is_invocable_v<L, int8_t>);
|
||||
static_assert(!std::is_invocable_v<L, int16_t>);
|
||||
static_assert(!std::is_invocable_v<L, int32_t>);
|
||||
static_assert(!std::is_invocable_v<L, int64_t>);
|
||||
static_assert(!std::is_invocable_v<L, intmax_t>);
|
||||
static_assert(!std::is_invocable_v<L, intptr_t>);
|
||||
static_assert(!std::is_invocable_v<L, ptrdiff_t>);
|
||||
|
||||
static_assert(!std::is_invocable_v<L, bool>);
|
||||
static_assert(!std::is_invocable_v<L, char>);
|
||||
static_assert(!std::is_invocable_v<L, wchar_t>);
|
||||
#ifndef _LIBCPP_HAS_NO_CHAR8_T
|
||||
static_assert(!std::is_invocable_v<L, char8_t>);
|
||||
#endif
|
||||
static_assert(!std::is_invocable_v<L, char16_t>);
|
||||
static_assert(!std::is_invocable_v<L, char32_t>);
|
||||
|
||||
static_assert(!std::is_invocable_v<L, A>);
|
||||
static_assert(!std::is_invocable_v<L, A*>);
|
||||
static_assert(!std::is_invocable_v<L, E1>);
|
||||
static_assert(!std::is_invocable_v<L, E2>);
|
||||
}
|
||||
|
||||
static_assert(constexpr_test<unsigned char>(), "");
|
||||
static_assert(constexpr_test<unsigned short>(), "");
|
||||
static_assert(constexpr_test<unsigned>(), "");
|
||||
static_assert(constexpr_test<unsigned long>(), "");
|
||||
static_assert(constexpr_test<unsigned long long>(), "");
|
||||
|
||||
static_assert(constexpr_test<uint8_t>(), "");
|
||||
static_assert(constexpr_test<uint16_t>(), "");
|
||||
static_assert(constexpr_test<uint32_t>(), "");
|
||||
static_assert(constexpr_test<uint64_t>(), "");
|
||||
static_assert(constexpr_test<size_t>(), "");
|
||||
static_assert(constexpr_test<uintmax_t>(), "");
|
||||
static_assert(constexpr_test<uintptr_t>(), "");
|
||||
|
||||
static_assert(test<unsigned char>());
|
||||
static_assert(test<unsigned short>());
|
||||
static_assert(test<unsigned int>());
|
||||
static_assert(test<unsigned long>());
|
||||
static_assert(test<unsigned long long>());
|
||||
#ifndef _LIBCPP_HAS_NO_INT128
|
||||
static_assert(constexpr_test<__uint128_t>(), "");
|
||||
static_assert(test<__uint128_t>());
|
||||
#endif
|
||||
static_assert(test<uint8_t>());
|
||||
static_assert(test<uint16_t>());
|
||||
static_assert(test<uint32_t>());
|
||||
static_assert(test<uint64_t>());
|
||||
static_assert(test<uintmax_t>());
|
||||
static_assert(test<uintptr_t>());
|
||||
static_assert(test<size_t>());
|
||||
|
||||
runtime_test<unsigned char>();
|
||||
runtime_test<unsigned>();
|
||||
runtime_test<unsigned short>();
|
||||
runtime_test<unsigned long>();
|
||||
runtime_test<unsigned long long>();
|
||||
|
||||
runtime_test<uint8_t>();
|
||||
runtime_test<uint16_t>();
|
||||
runtime_test<uint32_t>();
|
||||
runtime_test<uint64_t>();
|
||||
runtime_test<size_t>();
|
||||
runtime_test<uintmax_t>();
|
||||
runtime_test<uintptr_t>();
|
||||
|
||||
test<unsigned char>();
|
||||
test<unsigned short>();
|
||||
test<unsigned int>();
|
||||
test<unsigned long>();
|
||||
test<unsigned long long>();
|
||||
#ifndef _LIBCPP_HAS_NO_INT128
|
||||
runtime_test<__uint128_t>();
|
||||
|
||||
{
|
||||
__uint128_t val = 128;
|
||||
val <<= 32;
|
||||
assert(!std::has_single_bit(val-1));
|
||||
assert( std::has_single_bit(val));
|
||||
assert(!std::has_single_bit(val+1));
|
||||
val <<= 2;
|
||||
assert(!std::has_single_bit(val-1));
|
||||
assert( std::has_single_bit(val));
|
||||
assert(!std::has_single_bit(val+1));
|
||||
val <<= 3;
|
||||
assert(!std::has_single_bit(val-1));
|
||||
assert( std::has_single_bit(val));
|
||||
assert(!std::has_single_bit(val+1));
|
||||
}
|
||||
test<__uint128_t>();
|
||||
#endif
|
||||
test<uint8_t>();
|
||||
test<uint16_t>();
|
||||
test<uint32_t>();
|
||||
test<uint64_t>();
|
||||
test<uintmax_t>();
|
||||
test<uintptr_t>();
|
||||
test<size_t>();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -11,156 +11,130 @@
|
|||
// template <class T>
|
||||
// constexpr int countl_one(T x) noexcept;
|
||||
|
||||
// Constraints: T is an unsigned integer type
|
||||
// The number of consecutive 1 bits, starting from the most significant bit.
|
||||
// [ Note: Returns N if x == std::numeric_limits<T>::max(). ]
|
||||
//
|
||||
// Remarks: This function shall not participate in overload resolution unless
|
||||
// T is an unsigned integer type
|
||||
|
||||
#include <bit>
|
||||
#include <cassert>
|
||||
#include <cstdint>
|
||||
#include <type_traits>
|
||||
#include <cassert>
|
||||
|
||||
#include "test_macros.h"
|
||||
|
||||
class A{};
|
||||
struct A {};
|
||||
enum E1 : unsigned char { rEd };
|
||||
enum class E2 : unsigned char { red };
|
||||
|
||||
template <typename T>
|
||||
constexpr bool constexpr_test()
|
||||
template <class T>
|
||||
constexpr bool test()
|
||||
{
|
||||
const int dig = std::numeric_limits<T>::digits;
|
||||
const T max = std::numeric_limits<T>::max();
|
||||
return std::countl_one(max) == dig
|
||||
&& std::countl_one(T(max - 1)) == dig - 1
|
||||
&& std::countl_one(T(max - 2)) == dig - 2
|
||||
&& std::countl_one(T(max - 3)) == dig - 2
|
||||
&& std::countl_one(T(max - 4)) == dig - 3
|
||||
&& std::countl_one(T(max - 5)) == dig - 3
|
||||
&& std::countl_one(T(max - 6)) == dig - 3
|
||||
&& std::countl_one(T(max - 7)) == dig - 3
|
||||
&& std::countl_one(T(max - 8)) == dig - 4
|
||||
&& std::countl_one(T(max - 9)) == dig - 4
|
||||
;
|
||||
}
|
||||
ASSERT_SAME_TYPE(decltype(std::countl_one(T())), int);
|
||||
ASSERT_NOEXCEPT(std::countl_one(T()));
|
||||
T max = std::numeric_limits<T>::max();
|
||||
|
||||
assert(std::countl_one(T(0)) == 0);
|
||||
assert(std::countl_one(T(1)) == 0);
|
||||
assert(std::countl_one(T(10)) == 0);
|
||||
assert(std::countl_one(T(100)) == 0);
|
||||
assert(std::countl_one(max) == std::numeric_limits<T>::digits);
|
||||
assert(std::countl_one(T(max - 1)) == std::numeric_limits<T>::digits - 1);
|
||||
assert(std::countl_one(T(max - 2)) == std::numeric_limits<T>::digits - 2);
|
||||
assert(std::countl_one(T(max - 3)) == std::numeric_limits<T>::digits - 2);
|
||||
assert(std::countl_one(T(max - 4)) == std::numeric_limits<T>::digits - 3);
|
||||
assert(std::countl_one(T(max - 5)) == std::numeric_limits<T>::digits - 3);
|
||||
assert(std::countl_one(T(max - 6)) == std::numeric_limits<T>::digits - 3);
|
||||
assert(std::countl_one(T(max - 7)) == std::numeric_limits<T>::digits - 3);
|
||||
assert(std::countl_one(T(max - 8)) == std::numeric_limits<T>::digits - 4);
|
||||
assert(std::countl_one(T(max - 9)) == std::numeric_limits<T>::digits - 4);
|
||||
assert(std::countl_one(T(max - 126)) == std::numeric_limits<T>::digits - 7);
|
||||
assert(std::countl_one(T(max - 127)) == std::numeric_limits<T>::digits - 7);
|
||||
assert(std::countl_one(T(max - 128)) == std::numeric_limits<T>::digits - 8);
|
||||
|
||||
template <typename T>
|
||||
void runtime_test()
|
||||
{
|
||||
ASSERT_SAME_TYPE(int, decltype(std::countl_one(T(0))));
|
||||
ASSERT_NOEXCEPT( std::countl_one(T(0)));
|
||||
const int dig = std::numeric_limits<T>::digits;
|
||||
#ifndef _LIBCPP_HAS_NO_INT128
|
||||
if constexpr (std::is_same_v<T, __uint128_t>) {
|
||||
T val = 128;
|
||||
assert(std::countl_one(~val) == 120);
|
||||
val <<= 32;
|
||||
assert(std::countl_one(~val) == 88);
|
||||
val <<= 60;
|
||||
assert(std::countl_one(~val) == 28);
|
||||
}
|
||||
#endif
|
||||
|
||||
assert( std::countl_one(T(~121)) == dig - 7);
|
||||
assert( std::countl_one(T(~122)) == dig - 7);
|
||||
assert( std::countl_one(T(~123)) == dig - 7);
|
||||
assert( std::countl_one(T(~124)) == dig - 7);
|
||||
assert( std::countl_one(T(~125)) == dig - 7);
|
||||
assert( std::countl_one(T(~126)) == dig - 7);
|
||||
assert( std::countl_one(T(~127)) == dig - 7);
|
||||
assert( std::countl_one(T(~128)) == dig - 8);
|
||||
assert( std::countl_one(T(~129)) == dig - 8);
|
||||
assert( std::countl_one(T(~130)) == dig - 8);
|
||||
return true;
|
||||
}
|
||||
|
||||
int main(int, char**)
|
||||
{
|
||||
{
|
||||
{
|
||||
auto lambda = [](auto x) -> decltype(std::countl_one(x)) {};
|
||||
using L = decltype(lambda);
|
||||
|
||||
static_assert( std::is_invocable_v<L, unsigned char>, "");
|
||||
static_assert( std::is_invocable_v<L, unsigned int>, "");
|
||||
static_assert( std::is_invocable_v<L, unsigned long>, "");
|
||||
static_assert( std::is_invocable_v<L, unsigned long long>, "");
|
||||
|
||||
static_assert( std::is_invocable_v<L, uint8_t>, "");
|
||||
static_assert( std::is_invocable_v<L, uint16_t>, "");
|
||||
static_assert( std::is_invocable_v<L, uint32_t>, "");
|
||||
static_assert( std::is_invocable_v<L, uint64_t>, "");
|
||||
static_assert( std::is_invocable_v<L, size_t>, "");
|
||||
|
||||
static_assert( std::is_invocable_v<L, uintmax_t>, "");
|
||||
static_assert( std::is_invocable_v<L, uintptr_t>, "");
|
||||
|
||||
|
||||
static_assert(!std::is_invocable_v<L, int>, "");
|
||||
static_assert(!std::is_invocable_v<L, signed int>, "");
|
||||
static_assert(!std::is_invocable_v<L, long>, "");
|
||||
static_assert(!std::is_invocable_v<L, long long>, "");
|
||||
|
||||
static_assert(!std::is_invocable_v<L, int8_t>, "");
|
||||
static_assert(!std::is_invocable_v<L, int16_t>, "");
|
||||
static_assert(!std::is_invocable_v<L, int32_t>, "");
|
||||
static_assert(!std::is_invocable_v<L, int64_t>, "");
|
||||
static_assert(!std::is_invocable_v<L, ptrdiff_t>, "");
|
||||
|
||||
static_assert(!std::is_invocable_v<L, bool>, "");
|
||||
static_assert(!std::is_invocable_v<L, signed char>, "");
|
||||
static_assert(!std::is_invocable_v<L, char16_t>, "");
|
||||
static_assert(!std::is_invocable_v<L, char32_t>, "");
|
||||
|
||||
static_assert(!std::is_invocable_v<L, signed char>);
|
||||
static_assert(!std::is_invocable_v<L, short>);
|
||||
static_assert(!std::is_invocable_v<L, int>);
|
||||
static_assert(!std::is_invocable_v<L, long>);
|
||||
static_assert(!std::is_invocable_v<L, long long>);
|
||||
#ifndef _LIBCPP_HAS_NO_INT128
|
||||
static_assert( std::is_invocable_v<L, __uint128_t>, "");
|
||||
static_assert(!std::is_invocable_v<L, __int128_t>, "");
|
||||
static_assert(!std::is_invocable_v<L, __int128_t>);
|
||||
#endif
|
||||
|
||||
static_assert(!std::is_invocable_v<L, A>, "");
|
||||
static_assert(!std::is_invocable_v<L, E1>, "");
|
||||
static_assert(!std::is_invocable_v<L, E2>, "");
|
||||
static_assert(!std::is_invocable_v<L, int8_t>);
|
||||
static_assert(!std::is_invocable_v<L, int16_t>);
|
||||
static_assert(!std::is_invocable_v<L, int32_t>);
|
||||
static_assert(!std::is_invocable_v<L, int64_t>);
|
||||
static_assert(!std::is_invocable_v<L, intmax_t>);
|
||||
static_assert(!std::is_invocable_v<L, intptr_t>);
|
||||
static_assert(!std::is_invocable_v<L, ptrdiff_t>);
|
||||
|
||||
static_assert(!std::is_invocable_v<L, bool>);
|
||||
static_assert(!std::is_invocable_v<L, char>);
|
||||
static_assert(!std::is_invocable_v<L, wchar_t>);
|
||||
#ifndef _LIBCPP_HAS_NO_CHAR8_T
|
||||
static_assert(!std::is_invocable_v<L, char8_t>);
|
||||
#endif
|
||||
static_assert(!std::is_invocable_v<L, char16_t>);
|
||||
static_assert(!std::is_invocable_v<L, char32_t>);
|
||||
|
||||
static_assert(!std::is_invocable_v<L, A>);
|
||||
static_assert(!std::is_invocable_v<L, A*>);
|
||||
static_assert(!std::is_invocable_v<L, E1>);
|
||||
static_assert(!std::is_invocable_v<L, E2>);
|
||||
}
|
||||
|
||||
static_assert(constexpr_test<unsigned char>(), "");
|
||||
static_assert(constexpr_test<unsigned short>(), "");
|
||||
static_assert(constexpr_test<unsigned>(), "");
|
||||
static_assert(constexpr_test<unsigned long>(), "");
|
||||
static_assert(constexpr_test<unsigned long long>(), "");
|
||||
|
||||
static_assert(constexpr_test<uint8_t>(), "");
|
||||
static_assert(constexpr_test<uint16_t>(), "");
|
||||
static_assert(constexpr_test<uint32_t>(), "");
|
||||
static_assert(constexpr_test<uint64_t>(), "");
|
||||
static_assert(constexpr_test<size_t>(), "");
|
||||
static_assert(constexpr_test<uintmax_t>(), "");
|
||||
static_assert(constexpr_test<uintptr_t>(), "");
|
||||
|
||||
static_assert(test<unsigned char>());
|
||||
static_assert(test<unsigned short>());
|
||||
static_assert(test<unsigned int>());
|
||||
static_assert(test<unsigned long>());
|
||||
static_assert(test<unsigned long long>());
|
||||
#ifndef _LIBCPP_HAS_NO_INT128
|
||||
static_assert(constexpr_test<__uint128_t>(), "");
|
||||
static_assert(test<__uint128_t>());
|
||||
#endif
|
||||
static_assert(test<uint8_t>());
|
||||
static_assert(test<uint16_t>());
|
||||
static_assert(test<uint32_t>());
|
||||
static_assert(test<uint64_t>());
|
||||
static_assert(test<uintmax_t>());
|
||||
static_assert(test<uintptr_t>());
|
||||
static_assert(test<size_t>());
|
||||
|
||||
|
||||
runtime_test<unsigned char>();
|
||||
runtime_test<unsigned>();
|
||||
runtime_test<unsigned short>();
|
||||
runtime_test<unsigned long>();
|
||||
runtime_test<unsigned long long>();
|
||||
|
||||
runtime_test<uint8_t>();
|
||||
runtime_test<uint16_t>();
|
||||
runtime_test<uint32_t>();
|
||||
runtime_test<uint64_t>();
|
||||
runtime_test<size_t>();
|
||||
runtime_test<uintmax_t>();
|
||||
runtime_test<uintptr_t>();
|
||||
|
||||
test<unsigned char>();
|
||||
test<unsigned short>();
|
||||
test<unsigned int>();
|
||||
test<unsigned long>();
|
||||
test<unsigned long long>();
|
||||
#ifndef _LIBCPP_HAS_NO_INT128
|
||||
runtime_test<__uint128_t>();
|
||||
|
||||
{
|
||||
const int dig = std::numeric_limits<__uint128_t>::digits;
|
||||
__uint128_t val = 128;
|
||||
|
||||
val <<= 32;
|
||||
assert( std::countl_one(~val) == dig - 40);
|
||||
val <<= 2;
|
||||
assert( std::countl_one(~val) == dig - 42);
|
||||
val <<= 3;
|
||||
assert( std::countl_one(~val) == dig - 45);
|
||||
}
|
||||
test<__uint128_t>();
|
||||
#endif
|
||||
test<uint8_t>();
|
||||
test<uint16_t>();
|
||||
test<uint32_t>();
|
||||
test<uint64_t>();
|
||||
test<uintmax_t>();
|
||||
test<uintptr_t>();
|
||||
test<size_t>();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
|
@ -11,163 +11,128 @@
|
|||
// template <class T>
|
||||
// constexpr int countl_zero(T x) noexcept;
|
||||
|
||||
// Constraints: T is an unsigned integer type
|
||||
// Returns: The number of consecutive 0 bits, starting from the most significant bit.
|
||||
// [ Note: Returns N if x == 0. ]
|
||||
//
|
||||
// Remarks: This function shall not participate in overload resolution unless
|
||||
// T is an unsigned integer type
|
||||
|
||||
#include <bit>
|
||||
#include <cassert>
|
||||
#include <cstdint>
|
||||
#include <type_traits>
|
||||
#include <cassert>
|
||||
|
||||
#include "test_macros.h"
|
||||
|
||||
class A{};
|
||||
struct A {};
|
||||
enum E1 : unsigned char { rEd };
|
||||
enum class E2 : unsigned char { red };
|
||||
|
||||
template <typename T>
|
||||
constexpr bool constexpr_test()
|
||||
template <class T>
|
||||
constexpr bool test()
|
||||
{
|
||||
const int dig = std::numeric_limits<T>::digits;
|
||||
return std::countl_zero(T(0)) == dig
|
||||
&& std::countl_zero(T(1)) == dig - 1
|
||||
&& std::countl_zero(T(2)) == dig - 2
|
||||
&& std::countl_zero(T(3)) == dig - 2
|
||||
&& std::countl_zero(T(4)) == dig - 3
|
||||
&& std::countl_zero(T(5)) == dig - 3
|
||||
&& std::countl_zero(T(6)) == dig - 3
|
||||
&& std::countl_zero(T(7)) == dig - 3
|
||||
&& std::countl_zero(T(8)) == dig - 4
|
||||
&& std::countl_zero(T(9)) == dig - 4
|
||||
&& std::countl_zero(std::numeric_limits<T>::max()) == 0
|
||||
;
|
||||
}
|
||||
ASSERT_SAME_TYPE(decltype(std::countl_zero(T())), int);
|
||||
ASSERT_NOEXCEPT(std::countl_zero(T()));
|
||||
T max = std::numeric_limits<T>::max();
|
||||
int dig = std::numeric_limits<T>::digits;
|
||||
|
||||
assert(std::countl_zero(T(0)) == dig);
|
||||
assert(std::countl_zero(T(1)) == dig - 1);
|
||||
assert(std::countl_zero(T(2)) == dig - 2);
|
||||
assert(std::countl_zero(T(3)) == dig - 2);
|
||||
assert(std::countl_zero(T(4)) == dig - 3);
|
||||
assert(std::countl_zero(T(5)) == dig - 3);
|
||||
assert(std::countl_zero(T(6)) == dig - 3);
|
||||
assert(std::countl_zero(T(7)) == dig - 3);
|
||||
assert(std::countl_zero(T(8)) == dig - 4);
|
||||
assert(std::countl_zero(T(9)) == dig - 4);
|
||||
assert(std::countl_zero(T(127)) == dig - 7);
|
||||
assert(std::countl_zero(T(128)) == dig - 8);
|
||||
assert(std::countl_zero(max) == 0);
|
||||
|
||||
template <typename T>
|
||||
void runtime_test()
|
||||
{
|
||||
ASSERT_SAME_TYPE(int, decltype(std::countl_zero(T(0))));
|
||||
ASSERT_NOEXCEPT( std::countl_zero(T(0)));
|
||||
const int dig = std::numeric_limits<T>::digits;
|
||||
#ifndef _LIBCPP_HAS_NO_INT128
|
||||
if constexpr (std::is_same_v<T, __uint128_t>) {
|
||||
T val = T(128) << 32;
|
||||
assert(std::countl_zero(val-1) == 89);
|
||||
assert(std::countl_zero(val) == 88);
|
||||
assert(std::countl_zero(val+1) == 88);
|
||||
val <<= 60;
|
||||
assert(std::countl_zero(val-1) == 29);
|
||||
assert(std::countl_zero(val) == 28);
|
||||
assert(std::countl_zero(val+1) == 28);
|
||||
}
|
||||
#endif
|
||||
|
||||
assert( std::countl_zero(T(121)) == dig - 7);
|
||||
assert( std::countl_zero(T(122)) == dig - 7);
|
||||
assert( std::countl_zero(T(123)) == dig - 7);
|
||||
assert( std::countl_zero(T(124)) == dig - 7);
|
||||
assert( std::countl_zero(T(125)) == dig - 7);
|
||||
assert( std::countl_zero(T(126)) == dig - 7);
|
||||
assert( std::countl_zero(T(127)) == dig - 7);
|
||||
assert( std::countl_zero(T(128)) == dig - 8);
|
||||
assert( std::countl_zero(T(129)) == dig - 8);
|
||||
assert( std::countl_zero(T(130)) == dig - 8);
|
||||
return true;
|
||||
}
|
||||
|
||||
int main(int, char**)
|
||||
{
|
||||
|
||||
{
|
||||
auto lambda = [](auto x) -> decltype(std::countl_zero(x)) {};
|
||||
using L = decltype(lambda);
|
||||
|
||||
static_assert( std::is_invocable_v<L, unsigned char>, "");
|
||||
static_assert( std::is_invocable_v<L, unsigned int>, "");
|
||||
static_assert( std::is_invocable_v<L, unsigned long>, "");
|
||||
static_assert( std::is_invocable_v<L, unsigned long long>, "");
|
||||
|
||||
static_assert( std::is_invocable_v<L, uint8_t>, "");
|
||||
static_assert( std::is_invocable_v<L, uint16_t>, "");
|
||||
static_assert( std::is_invocable_v<L, uint32_t>, "");
|
||||
static_assert( std::is_invocable_v<L, uint64_t>, "");
|
||||
static_assert( std::is_invocable_v<L, size_t>, "");
|
||||
|
||||
static_assert( std::is_invocable_v<L, uintmax_t>, "");
|
||||
static_assert( std::is_invocable_v<L, uintptr_t>, "");
|
||||
|
||||
|
||||
static_assert(!std::is_invocable_v<L, int>, "");
|
||||
static_assert(!std::is_invocable_v<L, signed int>, "");
|
||||
static_assert(!std::is_invocable_v<L, long>, "");
|
||||
static_assert(!std::is_invocable_v<L, long long>, "");
|
||||
|
||||
static_assert(!std::is_invocable_v<L, int8_t>, "");
|
||||
static_assert(!std::is_invocable_v<L, int16_t>, "");
|
||||
static_assert(!std::is_invocable_v<L, int32_t>, "");
|
||||
static_assert(!std::is_invocable_v<L, int64_t>, "");
|
||||
static_assert(!std::is_invocable_v<L, ptrdiff_t>, "");
|
||||
|
||||
static_assert(!std::is_invocable_v<L, bool>, "");
|
||||
static_assert(!std::is_invocable_v<L, signed char>, "");
|
||||
static_assert(!std::is_invocable_v<L, char16_t>, "");
|
||||
static_assert(!std::is_invocable_v<L, char32_t>, "");
|
||||
|
||||
static_assert(!std::is_invocable_v<L, signed char>);
|
||||
static_assert(!std::is_invocable_v<L, short>);
|
||||
static_assert(!std::is_invocable_v<L, int>);
|
||||
static_assert(!std::is_invocable_v<L, long>);
|
||||
static_assert(!std::is_invocable_v<L, long long>);
|
||||
#ifndef _LIBCPP_HAS_NO_INT128
|
||||
static_assert( std::is_invocable_v<L, __uint128_t>, "");
|
||||
static_assert(!std::is_invocable_v<L, __int128_t>, "");
|
||||
static_assert(!std::is_invocable_v<L, __int128_t>);
|
||||
#endif
|
||||
|
||||
static_assert(!std::is_invocable_v<L, A>, "");
|
||||
static_assert(!std::is_invocable_v<L, E1>, "");
|
||||
static_assert(!std::is_invocable_v<L, E2>, "");
|
||||
static_assert(!std::is_invocable_v<L, int8_t>);
|
||||
static_assert(!std::is_invocable_v<L, int16_t>);
|
||||
static_assert(!std::is_invocable_v<L, int32_t>);
|
||||
static_assert(!std::is_invocable_v<L, int64_t>);
|
||||
static_assert(!std::is_invocable_v<L, intmax_t>);
|
||||
static_assert(!std::is_invocable_v<L, intptr_t>);
|
||||
static_assert(!std::is_invocable_v<L, ptrdiff_t>);
|
||||
|
||||
static_assert(!std::is_invocable_v<L, bool>);
|
||||
static_assert(!std::is_invocable_v<L, char>);
|
||||
static_assert(!std::is_invocable_v<L, wchar_t>);
|
||||
#ifndef _LIBCPP_HAS_NO_CHAR8_T
|
||||
static_assert(!std::is_invocable_v<L, char8_t>);
|
||||
#endif
|
||||
static_assert(!std::is_invocable_v<L, char16_t>);
|
||||
static_assert(!std::is_invocable_v<L, char32_t>);
|
||||
|
||||
static_assert(!std::is_invocable_v<L, A>);
|
||||
static_assert(!std::is_invocable_v<L, A*>);
|
||||
static_assert(!std::is_invocable_v<L, E1>);
|
||||
static_assert(!std::is_invocable_v<L, E2>);
|
||||
}
|
||||
|
||||
static_assert(constexpr_test<unsigned char>(), "");
|
||||
static_assert(constexpr_test<unsigned short>(), "");
|
||||
static_assert(constexpr_test<unsigned>(), "");
|
||||
static_assert(constexpr_test<unsigned long>(), "");
|
||||
static_assert(constexpr_test<unsigned long long>(), "");
|
||||
|
||||
static_assert(constexpr_test<uint8_t>(), "");
|
||||
static_assert(constexpr_test<uint16_t>(), "");
|
||||
static_assert(constexpr_test<uint32_t>(), "");
|
||||
static_assert(constexpr_test<uint64_t>(), "");
|
||||
static_assert(constexpr_test<size_t>(), "");
|
||||
static_assert(constexpr_test<uintmax_t>(), "");
|
||||
static_assert(constexpr_test<uintptr_t>(), "");
|
||||
|
||||
static_assert(test<unsigned char>());
|
||||
static_assert(test<unsigned short>());
|
||||
static_assert(test<unsigned int>());
|
||||
static_assert(test<unsigned long>());
|
||||
static_assert(test<unsigned long long>());
|
||||
#ifndef _LIBCPP_HAS_NO_INT128
|
||||
static_assert(constexpr_test<__uint128_t>(), "");
|
||||
static_assert(test<__uint128_t>());
|
||||
#endif
|
||||
static_assert(test<uint8_t>());
|
||||
static_assert(test<uint16_t>());
|
||||
static_assert(test<uint32_t>());
|
||||
static_assert(test<uint64_t>());
|
||||
static_assert(test<uintmax_t>());
|
||||
static_assert(test<uintptr_t>());
|
||||
static_assert(test<size_t>());
|
||||
|
||||
|
||||
runtime_test<unsigned char>();
|
||||
runtime_test<unsigned>();
|
||||
runtime_test<unsigned short>();
|
||||
runtime_test<unsigned long>();
|
||||
runtime_test<unsigned long long>();
|
||||
|
||||
runtime_test<uint8_t>();
|
||||
runtime_test<uint16_t>();
|
||||
runtime_test<uint32_t>();
|
||||
runtime_test<uint64_t>();
|
||||
runtime_test<size_t>();
|
||||
runtime_test<uintmax_t>();
|
||||
runtime_test<uintptr_t>();
|
||||
|
||||
test<unsigned char>();
|
||||
test<unsigned short>();
|
||||
test<unsigned int>();
|
||||
test<unsigned long>();
|
||||
test<unsigned long long>();
|
||||
#ifndef _LIBCPP_HAS_NO_INT128
|
||||
runtime_test<__uint128_t>();
|
||||
|
||||
{
|
||||
const int dig = std::numeric_limits<__uint128_t>::digits;
|
||||
__uint128_t val = 128;
|
||||
|
||||
val <<= 32;
|
||||
assert( std::countl_zero(val-1) == dig - 39);
|
||||
assert( std::countl_zero(val) == dig - 40);
|
||||
assert( std::countl_zero(val+1) == dig - 40);
|
||||
val <<= 2;
|
||||
assert( std::countl_zero(val-1) == dig - 41);
|
||||
assert( std::countl_zero(val) == dig - 42);
|
||||
assert( std::countl_zero(val+1) == dig - 42);
|
||||
val <<= 3;
|
||||
assert( std::countl_zero(val-1) == dig - 44);
|
||||
assert( std::countl_zero(val) == dig - 45);
|
||||
assert( std::countl_zero(val+1) == dig - 45);
|
||||
}
|
||||
test<__uint128_t>();
|
||||
#endif
|
||||
test<uint8_t>();
|
||||
test<uint16_t>();
|
||||
test<uint32_t>();
|
||||
test<uint64_t>();
|
||||
test<uintmax_t>();
|
||||
test<uintptr_t>();
|
||||
test<size_t>();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -11,11 +11,9 @@
|
|||
// template <class T>
|
||||
// constexpr int countr_one(T x) noexcept;
|
||||
|
||||
// Constraints: T is an unsigned integer type
|
||||
// Returns: The number of consecutive 1 bits, starting from the least significant bit.
|
||||
// [ Note: Returns N if x == std::numeric_limits<T>::max(). ]
|
||||
//
|
||||
// Remarks: This function shall not participate in overload resolution unless
|
||||
// T is an unsigned integer type
|
||||
|
||||
#include <bit>
|
||||
#include <cstdint>
|
||||
|
@ -24,148 +22,122 @@
|
|||
|
||||
#include "test_macros.h"
|
||||
|
||||
class A{};
|
||||
struct A {};
|
||||
enum E1 : unsigned char { rEd };
|
||||
enum class E2 : unsigned char { red };
|
||||
|
||||
template <typename T>
|
||||
constexpr bool constexpr_test()
|
||||
template <class T>
|
||||
constexpr bool test()
|
||||
{
|
||||
const int dig = std::numeric_limits<T>::digits;
|
||||
return std::countr_one(T(0)) == 0
|
||||
&& std::countr_one(T(1)) == 1
|
||||
&& std::countr_one(T(2)) == 0
|
||||
&& std::countr_one(T(3)) == 2
|
||||
&& std::countr_one(T(4)) == 0
|
||||
&& std::countr_one(T(5)) == 1
|
||||
&& std::countr_one(T(6)) == 0
|
||||
&& std::countr_one(T(7)) == 3
|
||||
&& std::countr_one(T(8)) == 0
|
||||
&& std::countr_one(T(9)) == 1
|
||||
&& std::countr_one(std::numeric_limits<T>::max()) == dig
|
||||
;
|
||||
}
|
||||
ASSERT_SAME_TYPE(decltype(std::countr_one(T())), int);
|
||||
ASSERT_NOEXCEPT(std::countr_one(T()));
|
||||
T max = std::numeric_limits<T>::max();
|
||||
|
||||
assert(std::countr_one(T(0)) == 0);
|
||||
assert(std::countr_one(T(1)) == 1);
|
||||
assert(std::countr_one(T(2)) == 0);
|
||||
assert(std::countr_one(T(3)) == 2);
|
||||
assert(std::countr_one(T(4)) == 0);
|
||||
assert(std::countr_one(T(5)) == 1);
|
||||
assert(std::countr_one(T(6)) == 0);
|
||||
assert(std::countr_one(T(7)) == 3);
|
||||
assert(std::countr_one(T(8)) == 0);
|
||||
assert(std::countr_one(T(9)) == 1);
|
||||
assert(std::countr_one(T(126)) == 0);
|
||||
assert(std::countr_one(T(127)) == 7);
|
||||
assert(std::countr_one(T(128)) == 0);
|
||||
assert(std::countr_one(T(max - 1)) == 0);
|
||||
assert(std::countr_one(max) == std::numeric_limits<T>::digits);
|
||||
|
||||
template <typename T>
|
||||
void runtime_test()
|
||||
{
|
||||
ASSERT_SAME_TYPE(int, decltype(std::countr_one(T(0))));
|
||||
ASSERT_NOEXCEPT( std::countr_one(T(0)));
|
||||
#ifndef _LIBCPP_HAS_NO_INT128
|
||||
if constexpr (std::is_same_v<T, __uint128_t>) {
|
||||
T val = 128;
|
||||
assert(std::countr_one(val-1) == 7);
|
||||
assert(std::countr_one(val) == 0);
|
||||
assert(std::countr_one(val+1) == 1);
|
||||
val <<= 32;
|
||||
assert(std::countr_one(val-1) == 39);
|
||||
assert(std::countr_one(val) == 0);
|
||||
assert(std::countr_one(val+1) == 1);
|
||||
val <<= 60;
|
||||
assert(std::countr_one(val-1) == 99);
|
||||
assert(std::countr_one(val) == 0);
|
||||
assert(std::countr_one(val+1) == 1);
|
||||
}
|
||||
#endif
|
||||
|
||||
assert( std::countr_one(T(121)) == 1);
|
||||
assert( std::countr_one(T(122)) == 0);
|
||||
assert( std::countr_one(T(123)) == 2);
|
||||
assert( std::countr_one(T(124)) == 0);
|
||||
assert( std::countr_one(T(125)) == 1);
|
||||
assert( std::countr_one(T(126)) == 0);
|
||||
assert( std::countr_one(T(127)) == 7);
|
||||
assert( std::countr_one(T(128)) == 0);
|
||||
assert( std::countr_one(T(129)) == 1);
|
||||
assert( std::countr_one(T(130)) == 0);
|
||||
return true;
|
||||
}
|
||||
|
||||
int main(int, char**)
|
||||
{
|
||||
|
||||
{
|
||||
auto lambda = [](auto x) -> decltype(std::countr_one(x)) {};
|
||||
using L = decltype(lambda);
|
||||
|
||||
static_assert( std::is_invocable_v<L, unsigned char>, "");
|
||||
static_assert( std::is_invocable_v<L, unsigned int>, "");
|
||||
static_assert( std::is_invocable_v<L, unsigned long>, "");
|
||||
static_assert( std::is_invocable_v<L, unsigned long long>, "");
|
||||
|
||||
static_assert( std::is_invocable_v<L, uint8_t>, "");
|
||||
static_assert( std::is_invocable_v<L, uint16_t>, "");
|
||||
static_assert( std::is_invocable_v<L, uint32_t>, "");
|
||||
static_assert( std::is_invocable_v<L, uint64_t>, "");
|
||||
static_assert( std::is_invocable_v<L, size_t>, "");
|
||||
|
||||
static_assert( std::is_invocable_v<L, uintmax_t>, "");
|
||||
static_assert( std::is_invocable_v<L, uintptr_t>, "");
|
||||
|
||||
|
||||
static_assert(!std::is_invocable_v<L, int>, "");
|
||||
static_assert(!std::is_invocable_v<L, signed int>, "");
|
||||
static_assert(!std::is_invocable_v<L, long>, "");
|
||||
static_assert(!std::is_invocable_v<L, long long>, "");
|
||||
|
||||
static_assert(!std::is_invocable_v<L, int8_t>, "");
|
||||
static_assert(!std::is_invocable_v<L, int16_t>, "");
|
||||
static_assert(!std::is_invocable_v<L, int32_t>, "");
|
||||
static_assert(!std::is_invocable_v<L, int64_t>, "");
|
||||
static_assert(!std::is_invocable_v<L, ptrdiff_t>, "");
|
||||
|
||||
static_assert(!std::is_invocable_v<L, bool>, "");
|
||||
static_assert(!std::is_invocable_v<L, signed char>, "");
|
||||
static_assert(!std::is_invocable_v<L, char16_t>, "");
|
||||
static_assert(!std::is_invocable_v<L, char32_t>, "");
|
||||
|
||||
static_assert(!std::is_invocable_v<L, signed char>);
|
||||
static_assert(!std::is_invocable_v<L, short>);
|
||||
static_assert(!std::is_invocable_v<L, int>);
|
||||
static_assert(!std::is_invocable_v<L, long>);
|
||||
static_assert(!std::is_invocable_v<L, long long>);
|
||||
#ifndef _LIBCPP_HAS_NO_INT128
|
||||
static_assert( std::is_invocable_v<L, __uint128_t>, "");
|
||||
static_assert(!std::is_invocable_v<L, __int128_t>, "");
|
||||
static_assert(!std::is_invocable_v<L, __int128_t>);
|
||||
#endif
|
||||
|
||||
static_assert(!std::is_invocable_v<L, A>, "");
|
||||
static_assert(!std::is_invocable_v<L, E1>, "");
|
||||
static_assert(!std::is_invocable_v<L, E2>, "");
|
||||
static_assert(!std::is_invocable_v<L, int8_t>);
|
||||
static_assert(!std::is_invocable_v<L, int16_t>);
|
||||
static_assert(!std::is_invocable_v<L, int32_t>);
|
||||
static_assert(!std::is_invocable_v<L, int64_t>);
|
||||
static_assert(!std::is_invocable_v<L, intmax_t>);
|
||||
static_assert(!std::is_invocable_v<L, intptr_t>);
|
||||
static_assert(!std::is_invocable_v<L, ptrdiff_t>);
|
||||
|
||||
static_assert(!std::is_invocable_v<L, bool>);
|
||||
static_assert(!std::is_invocable_v<L, char>);
|
||||
static_assert(!std::is_invocable_v<L, wchar_t>);
|
||||
#ifndef _LIBCPP_HAS_NO_CHAR8_T
|
||||
static_assert(!std::is_invocable_v<L, char8_t>);
|
||||
#endif
|
||||
static_assert(!std::is_invocable_v<L, char16_t>);
|
||||
static_assert(!std::is_invocable_v<L, char32_t>);
|
||||
|
||||
static_assert(!std::is_invocable_v<L, A>);
|
||||
static_assert(!std::is_invocable_v<L, A*>);
|
||||
static_assert(!std::is_invocable_v<L, E1>);
|
||||
static_assert(!std::is_invocable_v<L, E2>);
|
||||
}
|
||||
|
||||
static_assert(constexpr_test<unsigned char>(), "");
|
||||
static_assert(constexpr_test<unsigned short>(), "");
|
||||
static_assert(constexpr_test<unsigned>(), "");
|
||||
static_assert(constexpr_test<unsigned long>(), "");
|
||||
static_assert(constexpr_test<unsigned long long>(), "");
|
||||
|
||||
static_assert(constexpr_test<uint8_t>(), "");
|
||||
static_assert(constexpr_test<uint16_t>(), "");
|
||||
static_assert(constexpr_test<uint32_t>(), "");
|
||||
static_assert(constexpr_test<uint64_t>(), "");
|
||||
static_assert(constexpr_test<size_t>(), "");
|
||||
static_assert(constexpr_test<uintmax_t>(), "");
|
||||
static_assert(constexpr_test<uintptr_t>(), "");
|
||||
|
||||
static_assert(test<unsigned char>());
|
||||
static_assert(test<unsigned short>());
|
||||
static_assert(test<unsigned int>());
|
||||
static_assert(test<unsigned long>());
|
||||
static_assert(test<unsigned long long>());
|
||||
#ifndef _LIBCPP_HAS_NO_INT128
|
||||
static_assert(constexpr_test<__uint128_t>(), "");
|
||||
static_assert(test<__uint128_t>());
|
||||
#endif
|
||||
static_assert(test<uint8_t>());
|
||||
static_assert(test<uint16_t>());
|
||||
static_assert(test<uint32_t>());
|
||||
static_assert(test<uint64_t>());
|
||||
static_assert(test<uintmax_t>());
|
||||
static_assert(test<uintptr_t>());
|
||||
static_assert(test<size_t>());
|
||||
|
||||
|
||||
runtime_test<unsigned char>();
|
||||
runtime_test<unsigned>();
|
||||
runtime_test<unsigned short>();
|
||||
runtime_test<unsigned long>();
|
||||
runtime_test<unsigned long long>();
|
||||
|
||||
runtime_test<uint8_t>();
|
||||
runtime_test<uint16_t>();
|
||||
runtime_test<uint32_t>();
|
||||
runtime_test<uint64_t>();
|
||||
runtime_test<size_t>();
|
||||
runtime_test<uintmax_t>();
|
||||
runtime_test<uintptr_t>();
|
||||
|
||||
test<unsigned char>();
|
||||
test<unsigned short>();
|
||||
test<unsigned int>();
|
||||
test<unsigned long>();
|
||||
test<unsigned long long>();
|
||||
#ifndef _LIBCPP_HAS_NO_INT128
|
||||
runtime_test<__uint128_t>();
|
||||
|
||||
{
|
||||
__uint128_t val = 128;
|
||||
|
||||
val <<= 32;
|
||||
assert( std::countr_one(val-1) == 39);
|
||||
assert( std::countr_one(val) == 0);
|
||||
assert( std::countr_one(val+1) == 1);
|
||||
val <<= 2;
|
||||
assert( std::countr_one(val-1) == 41);
|
||||
assert( std::countr_one(val) == 0);
|
||||
assert( std::countr_one(val+1) == 1);
|
||||
val <<= 3;
|
||||
assert( std::countr_one(val-1) == 44);
|
||||
assert( std::countr_one(val) == 0);
|
||||
assert( std::countr_one(val+1) == 1);
|
||||
}
|
||||
test<__uint128_t>();
|
||||
#endif
|
||||
test<uint8_t>();
|
||||
test<uint16_t>();
|
||||
test<uint32_t>();
|
||||
test<uint64_t>();
|
||||
test<uintmax_t>();
|
||||
test<uintptr_t>();
|
||||
test<size_t>();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -11,160 +11,130 @@
|
|||
// template <class T>
|
||||
// constexpr int countr_zero(T x) noexcept;
|
||||
|
||||
// Constraints: T is an unsigned integer type
|
||||
// Returns: The number of consecutive 0 bits, starting from the most significant bit.
|
||||
// [ Note: Returns N if x == 0. ]
|
||||
//
|
||||
// Remarks: This function shall not participate in overload resolution unless
|
||||
// T is an unsigned integer type
|
||||
|
||||
#include <bit>
|
||||
#include <cassert>
|
||||
#include <cstdint>
|
||||
#include <type_traits>
|
||||
#include <cassert>
|
||||
|
||||
#include "test_macros.h"
|
||||
|
||||
class A{};
|
||||
struct A {};
|
||||
enum E1 : unsigned char { rEd };
|
||||
enum class E2 : unsigned char { red };
|
||||
|
||||
template <typename T>
|
||||
constexpr bool constexpr_test()
|
||||
template <class T>
|
||||
constexpr bool test()
|
||||
{
|
||||
const int dig = std::numeric_limits<T>::digits;
|
||||
return std::countr_zero(T(0)) == dig
|
||||
&& std::countr_zero(T(1)) == 0
|
||||
&& std::countr_zero(T(2)) == 1
|
||||
&& std::countr_zero(T(3)) == 0
|
||||
&& std::countr_zero(T(4)) == 2
|
||||
&& std::countr_zero(T(5)) == 0
|
||||
&& std::countr_zero(T(6)) == 1
|
||||
&& std::countr_zero(T(7)) == 0
|
||||
&& std::countr_zero(T(8)) == 3
|
||||
&& std::countr_zero(T(9)) == 0
|
||||
&& std::countr_zero(std::numeric_limits<T>::max()) == 0
|
||||
;
|
||||
}
|
||||
ASSERT_SAME_TYPE(decltype(std::countr_zero(T())), int);
|
||||
ASSERT_NOEXCEPT(std::countr_zero(T()));
|
||||
T max = std::numeric_limits<T>::max();
|
||||
|
||||
assert(std::countr_zero(T(0)) == std::numeric_limits<T>::digits);
|
||||
assert(std::countr_zero(T(1)) == 0);
|
||||
assert(std::countr_zero(T(2)) == 1);
|
||||
assert(std::countr_zero(T(3)) == 0);
|
||||
assert(std::countr_zero(T(4)) == 2);
|
||||
assert(std::countr_zero(T(5)) == 0);
|
||||
assert(std::countr_zero(T(6)) == 1);
|
||||
assert(std::countr_zero(T(7)) == 0);
|
||||
assert(std::countr_zero(T(8)) == 3);
|
||||
assert(std::countr_zero(T(9)) == 0);
|
||||
assert(std::countr_zero(T(126)) == 1);
|
||||
assert(std::countr_zero(T(127)) == 0);
|
||||
assert(std::countr_zero(T(128)) == 7);
|
||||
assert(std::countr_zero(T(129)) == 0);
|
||||
assert(std::countr_zero(T(130)) == 1);
|
||||
assert(std::countr_zero(max) == 0);
|
||||
|
||||
template <typename T>
|
||||
void runtime_test()
|
||||
{
|
||||
ASSERT_SAME_TYPE(int, decltype(std::countr_zero(T(0))));
|
||||
ASSERT_NOEXCEPT( std::countr_zero(T(0)));
|
||||
#ifndef _LIBCPP_HAS_NO_INT128
|
||||
if constexpr (std::is_same_v<T, __uint128_t>) {
|
||||
T val = T(128) << 32;
|
||||
assert(std::countr_zero(val-1) == 0);
|
||||
assert(std::countr_zero(val) == 39);
|
||||
assert(std::countr_zero(val+1) == 0);
|
||||
val <<= 60;
|
||||
assert(std::countr_zero(val-1) == 0);
|
||||
assert(std::countr_zero(val) == 99);
|
||||
assert(std::countr_zero(val+1) == 0);
|
||||
}
|
||||
#endif
|
||||
|
||||
assert( std::countr_zero(T(121)) == 0);
|
||||
assert( std::countr_zero(T(122)) == 1);
|
||||
assert( std::countr_zero(T(123)) == 0);
|
||||
assert( std::countr_zero(T(124)) == 2);
|
||||
assert( std::countr_zero(T(125)) == 0);
|
||||
assert( std::countr_zero(T(126)) == 1);
|
||||
assert( std::countr_zero(T(127)) == 0);
|
||||
assert( std::countr_zero(T(128)) == 7);
|
||||
assert( std::countr_zero(T(129)) == 0);
|
||||
assert( std::countr_zero(T(130)) == 1);
|
||||
return true;
|
||||
}
|
||||
|
||||
int main(int, char**)
|
||||
{
|
||||
|
||||
{
|
||||
auto lambda = [](auto x) -> decltype(std::countr_zero(x)) {};
|
||||
using L = decltype(lambda);
|
||||
|
||||
static_assert( std::is_invocable_v<L, unsigned char>, "");
|
||||
static_assert( std::is_invocable_v<L, unsigned int>, "");
|
||||
static_assert( std::is_invocable_v<L, unsigned long>, "");
|
||||
static_assert( std::is_invocable_v<L, unsigned long long>, "");
|
||||
|
||||
static_assert( std::is_invocable_v<L, uint8_t>, "");
|
||||
static_assert( std::is_invocable_v<L, uint16_t>, "");
|
||||
static_assert( std::is_invocable_v<L, uint32_t>, "");
|
||||
static_assert( std::is_invocable_v<L, uint64_t>, "");
|
||||
static_assert( std::is_invocable_v<L, size_t>, "");
|
||||
|
||||
static_assert( std::is_invocable_v<L, uintmax_t>, "");
|
||||
static_assert( std::is_invocable_v<L, uintptr_t>, "");
|
||||
|
||||
|
||||
static_assert(!std::is_invocable_v<L, int>, "");
|
||||
static_assert(!std::is_invocable_v<L, signed int>, "");
|
||||
static_assert(!std::is_invocable_v<L, long>, "");
|
||||
static_assert(!std::is_invocable_v<L, long long>, "");
|
||||
|
||||
static_assert(!std::is_invocable_v<L, int8_t>, "");
|
||||
static_assert(!std::is_invocable_v<L, int16_t>, "");
|
||||
static_assert(!std::is_invocable_v<L, int32_t>, "");
|
||||
static_assert(!std::is_invocable_v<L, int64_t>, "");
|
||||
static_assert(!std::is_invocable_v<L, ptrdiff_t>, "");
|
||||
|
||||
static_assert(!std::is_invocable_v<L, bool>, "");
|
||||
static_assert(!std::is_invocable_v<L, signed char>, "");
|
||||
static_assert(!std::is_invocable_v<L, char16_t>, "");
|
||||
static_assert(!std::is_invocable_v<L, char32_t>, "");
|
||||
|
||||
static_assert(!std::is_invocable_v<L, signed char>);
|
||||
static_assert(!std::is_invocable_v<L, short>);
|
||||
static_assert(!std::is_invocable_v<L, int>);
|
||||
static_assert(!std::is_invocable_v<L, long>);
|
||||
static_assert(!std::is_invocable_v<L, long long>);
|
||||
#ifndef _LIBCPP_HAS_NO_INT128
|
||||
static_assert( std::is_invocable_v<L, __uint128_t>, "");
|
||||
static_assert(!std::is_invocable_v<L, __int128_t>, "");
|
||||
static_assert(!std::is_invocable_v<L, __int128_t>);
|
||||
#endif
|
||||
|
||||
static_assert(!std::is_invocable_v<L, A>, "");
|
||||
static_assert(!std::is_invocable_v<L, E1>, "");
|
||||
static_assert(!std::is_invocable_v<L, E2>, "");
|
||||
static_assert(!std::is_invocable_v<L, int8_t>);
|
||||
static_assert(!std::is_invocable_v<L, int16_t>);
|
||||
static_assert(!std::is_invocable_v<L, int32_t>);
|
||||
static_assert(!std::is_invocable_v<L, int64_t>);
|
||||
static_assert(!std::is_invocable_v<L, intmax_t>);
|
||||
static_assert(!std::is_invocable_v<L, intptr_t>);
|
||||
static_assert(!std::is_invocable_v<L, ptrdiff_t>);
|
||||
|
||||
static_assert(!std::is_invocable_v<L, bool>);
|
||||
static_assert(!std::is_invocable_v<L, char>);
|
||||
static_assert(!std::is_invocable_v<L, wchar_t>);
|
||||
#ifndef _LIBCPP_HAS_NO_CHAR8_T
|
||||
static_assert(!std::is_invocable_v<L, char8_t>);
|
||||
#endif
|
||||
static_assert(!std::is_invocable_v<L, char16_t>);
|
||||
static_assert(!std::is_invocable_v<L, char32_t>);
|
||||
|
||||
static_assert(!std::is_invocable_v<L, A>);
|
||||
static_assert(!std::is_invocable_v<L, A*>);
|
||||
static_assert(!std::is_invocable_v<L, E1>);
|
||||
static_assert(!std::is_invocable_v<L, E2>);
|
||||
}
|
||||
|
||||
static_assert(constexpr_test<unsigned char>(), "");
|
||||
static_assert(constexpr_test<unsigned short>(), "");
|
||||
static_assert(constexpr_test<unsigned>(), "");
|
||||
static_assert(constexpr_test<unsigned long>(), "");
|
||||
static_assert(constexpr_test<unsigned long long>(), "");
|
||||
|
||||
static_assert(constexpr_test<uint8_t>(), "");
|
||||
static_assert(constexpr_test<uint16_t>(), "");
|
||||
static_assert(constexpr_test<uint32_t>(), "");
|
||||
static_assert(constexpr_test<uint64_t>(), "");
|
||||
static_assert(constexpr_test<size_t>(), "");
|
||||
static_assert(constexpr_test<uintmax_t>(), "");
|
||||
static_assert(constexpr_test<uintptr_t>(), "");
|
||||
|
||||
static_assert(test<unsigned char>());
|
||||
static_assert(test<unsigned short>());
|
||||
static_assert(test<unsigned int>());
|
||||
static_assert(test<unsigned long>());
|
||||
static_assert(test<unsigned long long>());
|
||||
#ifndef _LIBCPP_HAS_NO_INT128
|
||||
static_assert(constexpr_test<__uint128_t>(), "");
|
||||
static_assert(test<__uint128_t>());
|
||||
#endif
|
||||
static_assert(test<uint8_t>());
|
||||
static_assert(test<uint16_t>());
|
||||
static_assert(test<uint32_t>());
|
||||
static_assert(test<uint64_t>());
|
||||
static_assert(test<uintmax_t>());
|
||||
static_assert(test<uintptr_t>());
|
||||
static_assert(test<size_t>());
|
||||
|
||||
runtime_test<unsigned char>();
|
||||
runtime_test<unsigned>();
|
||||
runtime_test<unsigned short>();
|
||||
runtime_test<unsigned long>();
|
||||
runtime_test<unsigned long long>();
|
||||
|
||||
runtime_test<uint8_t>();
|
||||
runtime_test<uint16_t>();
|
||||
runtime_test<uint32_t>();
|
||||
runtime_test<uint64_t>();
|
||||
runtime_test<size_t>();
|
||||
runtime_test<uintmax_t>();
|
||||
runtime_test<uintptr_t>();
|
||||
|
||||
test<unsigned char>();
|
||||
test<unsigned short>();
|
||||
test<unsigned int>();
|
||||
test<unsigned long>();
|
||||
test<unsigned long long>();
|
||||
#ifndef _LIBCPP_HAS_NO_INT128
|
||||
runtime_test<__uint128_t>();
|
||||
|
||||
{
|
||||
__uint128_t val = 128;
|
||||
|
||||
val <<= 32;
|
||||
assert( std::countr_zero(val-1) == 0);
|
||||
assert( std::countr_zero(val) == 39);
|
||||
assert( std::countr_zero(val+1) == 0);
|
||||
val <<= 2;
|
||||
assert( std::countr_zero(val-1) == 0);
|
||||
assert( std::countr_zero(val) == 41);
|
||||
assert( std::countr_zero(val+1) == 0);
|
||||
val <<= 3;
|
||||
assert( std::countr_zero(val-1) == 0);
|
||||
assert( std::countr_zero(val) == 44);
|
||||
assert( std::countr_zero(val+1) == 0);
|
||||
}
|
||||
test<__uint128_t>();
|
||||
#endif
|
||||
test<uint8_t>();
|
||||
test<uint16_t>();
|
||||
test<uint32_t>();
|
||||
test<uint64_t>();
|
||||
test<uintmax_t>();
|
||||
test<uintptr_t>();
|
||||
test<size_t>();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -11,159 +11,140 @@
|
|||
// template <class T>
|
||||
// constexpr int popcount(T x) noexcept;
|
||||
|
||||
// Constraints: T is an unsigned integer type
|
||||
// Returns: The number of bits set to one in the value of x.
|
||||
//
|
||||
// Remarks: This function shall not participate in overload resolution unless
|
||||
// T is an unsigned integer type
|
||||
|
||||
#include <bit>
|
||||
#include <cassert>
|
||||
#include <cstdint>
|
||||
#include <type_traits>
|
||||
#include <cassert>
|
||||
|
||||
#include "test_macros.h"
|
||||
|
||||
class A{};
|
||||
struct A {};
|
||||
enum E1 : unsigned char { rEd };
|
||||
enum class E2 : unsigned char { red };
|
||||
|
||||
template <typename T>
|
||||
constexpr bool constexpr_test()
|
||||
template <class T>
|
||||
constexpr bool test()
|
||||
{
|
||||
return std::popcount(T(0)) == 0
|
||||
&& std::popcount(T(1)) == 1
|
||||
&& std::popcount(T(2)) == 1
|
||||
&& std::popcount(T(3)) == 2
|
||||
&& std::popcount(T(4)) == 1
|
||||
&& std::popcount(T(5)) == 2
|
||||
&& std::popcount(T(6)) == 2
|
||||
&& std::popcount(T(7)) == 3
|
||||
&& std::popcount(T(8)) == 1
|
||||
&& std::popcount(T(9)) == 2
|
||||
&& std::popcount(std::numeric_limits<T>::max()) == std::numeric_limits<T>::digits
|
||||
;
|
||||
}
|
||||
ASSERT_SAME_TYPE(decltype(std::popcount(T())), int);
|
||||
ASSERT_NOEXCEPT(std::popcount(T()));
|
||||
T max = std::numeric_limits<T>::max();
|
||||
|
||||
assert(std::popcount(T(0)) == 0);
|
||||
assert(std::popcount(T(1)) == 1);
|
||||
assert(std::popcount(T(2)) == 1);
|
||||
assert(std::popcount(T(3)) == 2);
|
||||
assert(std::popcount(T(4)) == 1);
|
||||
assert(std::popcount(T(5)) == 2);
|
||||
assert(std::popcount(T(6)) == 2);
|
||||
assert(std::popcount(T(7)) == 3);
|
||||
assert(std::popcount(T(8)) == 1);
|
||||
assert(std::popcount(T(9)) == 2);
|
||||
assert(std::popcount(T(121)) == 5);
|
||||
assert(std::popcount(T(127)) == 7);
|
||||
assert(std::popcount(T(128)) == 1);
|
||||
assert(std::popcount(T(130)) == 2);
|
||||
assert(std::popcount(T(max >> 1)) == std::numeric_limits<T>::digits - 1);
|
||||
assert(std::popcount(T(max - 1)) == std::numeric_limits<T>::digits - 1);
|
||||
assert(std::popcount(max) == std::numeric_limits<T>::digits);
|
||||
|
||||
template <typename T>
|
||||
void runtime_test()
|
||||
{
|
||||
ASSERT_SAME_TYPE(int, decltype(std::popcount(T(0))));
|
||||
ASSERT_NOEXCEPT( std::popcount(T(0)));
|
||||
#ifndef _LIBCPP_HAS_NO_INT128
|
||||
if constexpr (std::is_same_v<T, __uint128_t>) {
|
||||
T val = 128;
|
||||
assert(std::popcount(val-1) == 7);
|
||||
assert(std::popcount(val) == 1);
|
||||
assert(std::popcount(val+1) == 2);
|
||||
val <<= 32;
|
||||
assert(std::popcount(val-1) == 39);
|
||||
assert(std::popcount(val) == 1);
|
||||
assert(std::popcount(val+1) == 2);
|
||||
val <<= 60;
|
||||
assert(std::popcount(val-1) == 99);
|
||||
assert(std::popcount(val) == 1);
|
||||
assert(std::popcount(val+1) == 2);
|
||||
|
||||
assert( std::popcount(T(121)) == 5);
|
||||
assert( std::popcount(T(122)) == 5);
|
||||
assert( std::popcount(T(123)) == 6);
|
||||
assert( std::popcount(T(124)) == 5);
|
||||
assert( std::popcount(T(125)) == 6);
|
||||
assert( std::popcount(T(126)) == 6);
|
||||
assert( std::popcount(T(127)) == 7);
|
||||
assert( std::popcount(T(128)) == 1);
|
||||
assert( std::popcount(T(129)) == 2);
|
||||
assert( std::popcount(T(130)) == 2);
|
||||
T x = T(1) << 63;
|
||||
T y = T(1) << 64;
|
||||
assert(std::popcount(x) == 1);
|
||||
assert(std::popcount(y) == 1);
|
||||
assert(std::popcount(x+y) == 2);
|
||||
}
|
||||
#endif
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
int main(int, char**)
|
||||
{
|
||||
|
||||
{
|
||||
auto lambda = [](auto x) -> decltype(std::popcount(x)) {};
|
||||
using L = decltype(lambda);
|
||||
|
||||
static_assert( std::is_invocable_v<L, unsigned char>, "");
|
||||
static_assert( std::is_invocable_v<L, unsigned int>, "");
|
||||
static_assert( std::is_invocable_v<L, unsigned long>, "");
|
||||
static_assert( std::is_invocable_v<L, unsigned long long>, "");
|
||||
|
||||
static_assert( std::is_invocable_v<L, uint8_t>, "");
|
||||
static_assert( std::is_invocable_v<L, uint16_t>, "");
|
||||
static_assert( std::is_invocable_v<L, uint32_t>, "");
|
||||
static_assert( std::is_invocable_v<L, uint64_t>, "");
|
||||
static_assert( std::is_invocable_v<L, size_t>, "");
|
||||
|
||||
static_assert( std::is_invocable_v<L, uintmax_t>, "");
|
||||
static_assert( std::is_invocable_v<L, uintptr_t>, "");
|
||||
|
||||
|
||||
static_assert(!std::is_invocable_v<L, int>, "");
|
||||
static_assert(!std::is_invocable_v<L, signed int>, "");
|
||||
static_assert(!std::is_invocable_v<L, long>, "");
|
||||
static_assert(!std::is_invocable_v<L, long long>, "");
|
||||
|
||||
static_assert(!std::is_invocable_v<L, int8_t>, "");
|
||||
static_assert(!std::is_invocable_v<L, int16_t>, "");
|
||||
static_assert(!std::is_invocable_v<L, int32_t>, "");
|
||||
static_assert(!std::is_invocable_v<L, int64_t>, "");
|
||||
static_assert(!std::is_invocable_v<L, ptrdiff_t>, "");
|
||||
|
||||
static_assert(!std::is_invocable_v<L, bool>, "");
|
||||
static_assert(!std::is_invocable_v<L, signed char>, "");
|
||||
static_assert(!std::is_invocable_v<L, char16_t>, "");
|
||||
static_assert(!std::is_invocable_v<L, char32_t>, "");
|
||||
|
||||
static_assert(!std::is_invocable_v<L, signed char>);
|
||||
static_assert(!std::is_invocable_v<L, short>);
|
||||
static_assert(!std::is_invocable_v<L, int>);
|
||||
static_assert(!std::is_invocable_v<L, long>);
|
||||
static_assert(!std::is_invocable_v<L, long long>);
|
||||
#ifndef _LIBCPP_HAS_NO_INT128
|
||||
static_assert( std::is_invocable_v<L, __uint128_t>, "");
|
||||
static_assert(!std::is_invocable_v<L, __int128_t>, "");
|
||||
static_assert(!std::is_invocable_v<L, __int128_t>);
|
||||
#endif
|
||||
|
||||
static_assert(!std::is_invocable_v<L, A>, "");
|
||||
static_assert(!std::is_invocable_v<L, E1>, "");
|
||||
static_assert(!std::is_invocable_v<L, E2>, "");
|
||||
static_assert(!std::is_invocable_v<L, int8_t>);
|
||||
static_assert(!std::is_invocable_v<L, int16_t>);
|
||||
static_assert(!std::is_invocable_v<L, int32_t>);
|
||||
static_assert(!std::is_invocable_v<L, int64_t>);
|
||||
static_assert(!std::is_invocable_v<L, intmax_t>);
|
||||
static_assert(!std::is_invocable_v<L, intptr_t>);
|
||||
static_assert(!std::is_invocable_v<L, ptrdiff_t>);
|
||||
|
||||
static_assert(!std::is_invocable_v<L, bool>);
|
||||
static_assert(!std::is_invocable_v<L, char>);
|
||||
static_assert(!std::is_invocable_v<L, wchar_t>);
|
||||
#ifndef _LIBCPP_HAS_NO_CHAR8_T
|
||||
static_assert(!std::is_invocable_v<L, char8_t>);
|
||||
#endif
|
||||
static_assert(!std::is_invocable_v<L, char16_t>);
|
||||
static_assert(!std::is_invocable_v<L, char32_t>);
|
||||
|
||||
static_assert(!std::is_invocable_v<L, A>);
|
||||
static_assert(!std::is_invocable_v<L, A*>);
|
||||
static_assert(!std::is_invocable_v<L, E1>);
|
||||
static_assert(!std::is_invocable_v<L, E2>);
|
||||
}
|
||||
|
||||
static_assert(constexpr_test<unsigned char>(), "");
|
||||
static_assert(constexpr_test<unsigned short>(), "");
|
||||
static_assert(constexpr_test<unsigned>(), "");
|
||||
static_assert(constexpr_test<unsigned long>(), "");
|
||||
static_assert(constexpr_test<unsigned long long>(), "");
|
||||
|
||||
static_assert(constexpr_test<uint8_t>(), "");
|
||||
static_assert(constexpr_test<uint16_t>(), "");
|
||||
static_assert(constexpr_test<uint32_t>(), "");
|
||||
static_assert(constexpr_test<uint64_t>(), "");
|
||||
static_assert(constexpr_test<size_t>(), "");
|
||||
static_assert(constexpr_test<uintmax_t>(), "");
|
||||
static_assert(constexpr_test<uintptr_t>(), "");
|
||||
|
||||
static_assert(test<unsigned char>());
|
||||
static_assert(test<unsigned short>());
|
||||
static_assert(test<unsigned int>());
|
||||
static_assert(test<unsigned long>());
|
||||
static_assert(test<unsigned long long>());
|
||||
#ifndef _LIBCPP_HAS_NO_INT128
|
||||
static_assert(constexpr_test<__uint128_t>(), "");
|
||||
static_assert(test<__uint128_t>());
|
||||
#endif
|
||||
static_assert(test<uint8_t>());
|
||||
static_assert(test<uint16_t>());
|
||||
static_assert(test<uint32_t>());
|
||||
static_assert(test<uint64_t>());
|
||||
static_assert(test<uintmax_t>());
|
||||
static_assert(test<uintptr_t>());
|
||||
static_assert(test<size_t>());
|
||||
|
||||
|
||||
runtime_test<unsigned char>();
|
||||
runtime_test<unsigned>();
|
||||
runtime_test<unsigned short>();
|
||||
runtime_test<unsigned long>();
|
||||
runtime_test<unsigned long long>();
|
||||
|
||||
runtime_test<uint8_t>();
|
||||
runtime_test<uint16_t>();
|
||||
runtime_test<uint32_t>();
|
||||
runtime_test<uint64_t>();
|
||||
runtime_test<size_t>();
|
||||
runtime_test<uintmax_t>();
|
||||
runtime_test<uintptr_t>();
|
||||
|
||||
test<unsigned char>();
|
||||
test<unsigned short>();
|
||||
test<unsigned int>();
|
||||
test<unsigned long>();
|
||||
test<unsigned long long>();
|
||||
#ifndef _LIBCPP_HAS_NO_INT128
|
||||
runtime_test<__uint128_t>();
|
||||
|
||||
{
|
||||
__uint128_t val = 128;
|
||||
|
||||
val <<= 32;
|
||||
assert( std::popcount(val-1) == 39);
|
||||
assert( std::popcount(val) == 1);
|
||||
assert( std::popcount(val+1) == 2);
|
||||
val <<= 2;
|
||||
assert( std::popcount(val-1) == 41);
|
||||
assert( std::popcount(val) == 1);
|
||||
assert( std::popcount(val+1) == 2);
|
||||
val <<= 3;
|
||||
assert( std::popcount(val-1) == 44);
|
||||
assert( std::popcount(val) == 1);
|
||||
assert( std::popcount(val+1) == 2);
|
||||
}
|
||||
test<__uint128_t>();
|
||||
#endif
|
||||
test<uint8_t>();
|
||||
test<uint16_t>();
|
||||
test<uint32_t>();
|
||||
test<uint64_t>();
|
||||
test<uintmax_t>();
|
||||
test<uintptr_t>();
|
||||
test<size_t>();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -11,158 +11,130 @@
|
|||
// template <class T>
|
||||
// constexpr int rotl(T x, unsigned int s) noexcept;
|
||||
|
||||
// Remarks: This function shall not participate in overload resolution unless
|
||||
// T is an unsigned integer type
|
||||
// Constraints: T is an unsigned integer type
|
||||
|
||||
#include <bit>
|
||||
#include <cassert>
|
||||
#include <cstdint>
|
||||
#include <type_traits>
|
||||
#include <cassert>
|
||||
|
||||
#include "test_macros.h"
|
||||
|
||||
class A{};
|
||||
struct A {};
|
||||
enum E1 : unsigned char { rEd };
|
||||
enum class E2 : unsigned char { red };
|
||||
|
||||
template <typename T>
|
||||
constexpr bool constexpr_test()
|
||||
template <class T>
|
||||
constexpr bool test()
|
||||
{
|
||||
const T max = std::numeric_limits<T>::max();
|
||||
return std::rotl(T(1), 0) == T( 1)
|
||||
&& std::rotl(T(1), 1) == T( 2)
|
||||
&& std::rotl(T(1), 2) == T( 4)
|
||||
&& std::rotl(T(1), 3) == T( 8)
|
||||
&& std::rotl(T(1), 4) == T( 16)
|
||||
&& std::rotl(T(1), 5) == T( 32)
|
||||
&& std::rotl(T(1), 6) == T( 64)
|
||||
&& std::rotl(T(1), 7) == T(128)
|
||||
&& std::rotl(max, 0) == max
|
||||
&& std::rotl(max, 1) == max
|
||||
&& std::rotl(max, 2) == max
|
||||
&& std::rotl(max, 3) == max
|
||||
&& std::rotl(max, 4) == max
|
||||
&& std::rotl(max, 5) == max
|
||||
&& std::rotl(max, 6) == max
|
||||
&& std::rotl(max, 7) == max
|
||||
;
|
||||
}
|
||||
ASSERT_SAME_TYPE(decltype(std::rotl(T(), 0)), T);
|
||||
ASSERT_NOEXCEPT(std::rotl(T(), 0));
|
||||
T max = std::numeric_limits<T>::max();
|
||||
|
||||
assert(std::rotl(T(max - 1), 0) == T(max - 1));
|
||||
assert(std::rotl(T(max - 1), 1) == T(max - 2));
|
||||
assert(std::rotl(T(max - 1), 2) == T(max - 4));
|
||||
assert(std::rotl(T(max - 1), 3) == T(max - 8));
|
||||
assert(std::rotl(T(max - 1), 4) == T(max - 16));
|
||||
assert(std::rotl(T(max - 1), 5) == T(max - 32));
|
||||
assert(std::rotl(T(max - 1), 6) == T(max - 64));
|
||||
assert(std::rotl(T(max - 1), 7) == T(max - 128));
|
||||
|
||||
template <typename T>
|
||||
void runtime_test()
|
||||
{
|
||||
ASSERT_SAME_TYPE(T, decltype(std::rotl(T(0), 0)));
|
||||
ASSERT_NOEXCEPT( std::rotl(T(0), 0));
|
||||
const T val = std::numeric_limits<T>::max() - 1;
|
||||
assert(std::rotl(T(1), 0) == T(1));
|
||||
assert(std::rotl(T(1), 1) == T(2));
|
||||
assert(std::rotl(T(1), 2) == T(4));
|
||||
assert(std::rotl(T(1), 3) == T(8));
|
||||
assert(std::rotl(T(1), 4) == T(16));
|
||||
assert(std::rotl(T(1), 5) == T(32));
|
||||
assert(std::rotl(T(1), 6) == T(64));
|
||||
assert(std::rotl(T(1), 7) == T(128));
|
||||
|
||||
assert( std::rotl(val, 0) == val);
|
||||
assert( std::rotl(val, 1) == T((val << 1) + 1));
|
||||
assert( std::rotl(val, 2) == T((val << 2) + 3));
|
||||
assert( std::rotl(val, 3) == T((val << 3) + 7));
|
||||
assert( std::rotl(val, 4) == T((val << 4) + 15));
|
||||
assert( std::rotl(val, 5) == T((val << 5) + 31));
|
||||
assert( std::rotl(val, 6) == T((val << 6) + 63));
|
||||
assert( std::rotl(val, 7) == T((val << 7) + 127));
|
||||
#ifndef _LIBCPP_HAS_NO_INT128
|
||||
if constexpr (std::is_same_v<T, __uint128_t>) {
|
||||
T val = (T(1) << 63) | (T(1) << 64);
|
||||
assert(std::rotl(val, 0) == val);
|
||||
assert(std::rotl(val, 128) == val);
|
||||
assert(std::rotl(val, 256) == val);
|
||||
assert(std::rotl(val, 1) == val << 1);
|
||||
assert(std::rotl(val, 127) == val >> 1);
|
||||
assert(std::rotl(T(3), 127) == ((T(1) << 127) | T(1)));
|
||||
}
|
||||
#endif
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
int main(int, char**)
|
||||
{
|
||||
|
||||
{
|
||||
auto lambda = [](auto x) -> decltype(std::rotl(x, 1U)) {};
|
||||
using L = decltype(lambda);
|
||||
|
||||
static_assert( std::is_invocable_v<L, unsigned char>, "");
|
||||
static_assert( std::is_invocable_v<L, unsigned int>, "");
|
||||
static_assert( std::is_invocable_v<L, unsigned long>, "");
|
||||
static_assert( std::is_invocable_v<L, unsigned long long>, "");
|
||||
|
||||
static_assert( std::is_invocable_v<L, uint8_t>, "");
|
||||
static_assert( std::is_invocable_v<L, uint16_t>, "");
|
||||
static_assert( std::is_invocable_v<L, uint32_t>, "");
|
||||
static_assert( std::is_invocable_v<L, uint64_t>, "");
|
||||
static_assert( std::is_invocable_v<L, size_t>, "");
|
||||
|
||||
static_assert( std::is_invocable_v<L, uintmax_t>, "");
|
||||
static_assert( std::is_invocable_v<L, uintptr_t>, "");
|
||||
|
||||
|
||||
static_assert(!std::is_invocable_v<L, int>, "");
|
||||
static_assert(!std::is_invocable_v<L, signed int>, "");
|
||||
static_assert(!std::is_invocable_v<L, long>, "");
|
||||
static_assert(!std::is_invocable_v<L, long long>, "");
|
||||
|
||||
static_assert(!std::is_invocable_v<L, int8_t>, "");
|
||||
static_assert(!std::is_invocable_v<L, int16_t>, "");
|
||||
static_assert(!std::is_invocable_v<L, int32_t>, "");
|
||||
static_assert(!std::is_invocable_v<L, int64_t>, "");
|
||||
static_assert(!std::is_invocable_v<L, ptrdiff_t>, "");
|
||||
|
||||
static_assert(!std::is_invocable_v<L, bool>, "");
|
||||
static_assert(!std::is_invocable_v<L, signed char>, "");
|
||||
static_assert(!std::is_invocable_v<L, char16_t>, "");
|
||||
static_assert(!std::is_invocable_v<L, char32_t>, "");
|
||||
|
||||
static_assert(!std::is_invocable_v<L, signed char>);
|
||||
static_assert(!std::is_invocable_v<L, short>);
|
||||
static_assert(!std::is_invocable_v<L, int>);
|
||||
static_assert(!std::is_invocable_v<L, long>);
|
||||
static_assert(!std::is_invocable_v<L, long long>);
|
||||
#ifndef _LIBCPP_HAS_NO_INT128
|
||||
static_assert( std::is_invocable_v<L, __uint128_t>, "");
|
||||
static_assert(!std::is_invocable_v<L, __int128_t>, "");
|
||||
static_assert(!std::is_invocable_v<L, __int128_t>);
|
||||
#endif
|
||||
|
||||
static_assert(!std::is_invocable_v<L, A>, "");
|
||||
static_assert(!std::is_invocable_v<L, E1>, "");
|
||||
static_assert(!std::is_invocable_v<L, E2>, "");
|
||||
static_assert(!std::is_invocable_v<L, int8_t>);
|
||||
static_assert(!std::is_invocable_v<L, int16_t>);
|
||||
static_assert(!std::is_invocable_v<L, int32_t>);
|
||||
static_assert(!std::is_invocable_v<L, int64_t>);
|
||||
static_assert(!std::is_invocable_v<L, intmax_t>);
|
||||
static_assert(!std::is_invocable_v<L, intptr_t>);
|
||||
static_assert(!std::is_invocable_v<L, ptrdiff_t>);
|
||||
|
||||
static_assert(!std::is_invocable_v<L, bool>);
|
||||
static_assert(!std::is_invocable_v<L, char>);
|
||||
static_assert(!std::is_invocable_v<L, wchar_t>);
|
||||
#ifndef _LIBCPP_HAS_NO_CHAR8_T
|
||||
static_assert(!std::is_invocable_v<L, char8_t>);
|
||||
#endif
|
||||
static_assert(!std::is_invocable_v<L, char16_t>);
|
||||
static_assert(!std::is_invocable_v<L, char32_t>);
|
||||
|
||||
static_assert(!std::is_invocable_v<L, A>);
|
||||
static_assert(!std::is_invocable_v<L, A*>);
|
||||
static_assert(!std::is_invocable_v<L, E1>);
|
||||
static_assert(!std::is_invocable_v<L, E2>);
|
||||
}
|
||||
|
||||
static_assert(constexpr_test<unsigned char>(), "");
|
||||
static_assert(constexpr_test<unsigned short>(), "");
|
||||
static_assert(constexpr_test<unsigned>(), "");
|
||||
static_assert(constexpr_test<unsigned long>(), "");
|
||||
static_assert(constexpr_test<unsigned long long>(), "");
|
||||
|
||||
static_assert(constexpr_test<uint8_t>(), "");
|
||||
static_assert(constexpr_test<uint16_t>(), "");
|
||||
static_assert(constexpr_test<uint32_t>(), "");
|
||||
static_assert(constexpr_test<uint64_t>(), "");
|
||||
static_assert(constexpr_test<size_t>(), "");
|
||||
static_assert(constexpr_test<uintmax_t>(), "");
|
||||
static_assert(constexpr_test<uintptr_t>(), "");
|
||||
|
||||
static_assert(test<unsigned char>());
|
||||
static_assert(test<unsigned short>());
|
||||
static_assert(test<unsigned int>());
|
||||
static_assert(test<unsigned long>());
|
||||
static_assert(test<unsigned long long>());
|
||||
#ifndef _LIBCPP_HAS_NO_INT128
|
||||
static_assert(constexpr_test<__uint128_t>(), "");
|
||||
static_assert(test<__uint128_t>());
|
||||
#endif
|
||||
|
||||
static_assert(test<uint8_t>());
|
||||
static_assert(test<uint16_t>());
|
||||
static_assert(test<uint32_t>());
|
||||
static_assert(test<uint64_t>());
|
||||
static_assert(test<uintmax_t>());
|
||||
static_assert(test<uintptr_t>());
|
||||
static_assert(test<size_t>());
|
||||
|
||||
runtime_test<unsigned char>();
|
||||
runtime_test<unsigned short>();
|
||||
runtime_test<unsigned>();
|
||||
runtime_test<unsigned long>();
|
||||
runtime_test<unsigned long long>();
|
||||
|
||||
runtime_test<uint8_t>();
|
||||
runtime_test<uint16_t>();
|
||||
runtime_test<uint32_t>();
|
||||
runtime_test<uint64_t>();
|
||||
runtime_test<size_t>();
|
||||
runtime_test<uintmax_t>();
|
||||
runtime_test<uintptr_t>();
|
||||
|
||||
test<unsigned char>();
|
||||
test<unsigned short>();
|
||||
test<unsigned int>();
|
||||
test<unsigned long>();
|
||||
test<unsigned long long>();
|
||||
#ifndef _LIBCPP_HAS_NO_INT128
|
||||
runtime_test<__uint128_t>();
|
||||
|
||||
{
|
||||
__uint128_t val = 168; // 0xA8 (aka 10101000)
|
||||
|
||||
assert( std::rotl(val, 128) == 168);
|
||||
val <<= 32;
|
||||
assert( std::rotl(val, 96) == 168);
|
||||
val <<= 2;
|
||||
assert( std::rotl(val, 95) == 336);
|
||||
val <<= 3;
|
||||
assert( std::rotl(val, 90) == 84);
|
||||
assert( std::rotl(val, 218) == 84);
|
||||
}
|
||||
test<__uint128_t>();
|
||||
#endif
|
||||
|
||||
test<uint8_t>();
|
||||
test<uint16_t>();
|
||||
test<uint32_t>();
|
||||
test<uint64_t>();
|
||||
test<uintmax_t>();
|
||||
test<uintptr_t>();
|
||||
test<size_t>();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -11,173 +11,131 @@
|
|||
// template <class T>
|
||||
// constexpr int rotr(T x, unsigned int s) noexcept;
|
||||
|
||||
// Remarks: This function shall not participate in overload resolution unless
|
||||
// T is an unsigned integer type
|
||||
// Constraints: T is an unsigned integer type
|
||||
|
||||
#include <bit>
|
||||
#include <cassert>
|
||||
#include <cstdint>
|
||||
#include <type_traits>
|
||||
#include <cassert>
|
||||
|
||||
#include "test_macros.h"
|
||||
|
||||
class A{};
|
||||
struct A {};
|
||||
enum E1 : unsigned char { rEd };
|
||||
enum class E2 : unsigned char { red };
|
||||
|
||||
template <typename T>
|
||||
constexpr bool constexpr_test()
|
||||
template <class T>
|
||||
constexpr bool test()
|
||||
{
|
||||
const T max = std::numeric_limits<T>::max();
|
||||
ASSERT_SAME_TYPE(decltype(std::rotr(T(), 0)), T);
|
||||
ASSERT_NOEXCEPT(std::rotr(T(), 0));
|
||||
T max = std::numeric_limits<T>::max();
|
||||
T highbit = std::rotr(T(1), 1);
|
||||
|
||||
return std::rotr(T(128), 0) == T(128)
|
||||
&& std::rotr(T(128), 1) == T( 64)
|
||||
&& std::rotr(T(128), 2) == T( 32)
|
||||
&& std::rotr(T(128), 3) == T( 16)
|
||||
&& std::rotr(T(128), 4) == T( 8)
|
||||
&& std::rotr(T(128), 5) == T( 4)
|
||||
&& std::rotr(T(128), 6) == T( 2)
|
||||
&& std::rotr(T(128), 7) == T( 1)
|
||||
&& std::rotr(max, 0) == max
|
||||
&& std::rotr(max, 1) == max
|
||||
&& std::rotr(max, 2) == max
|
||||
&& std::rotr(max, 3) == max
|
||||
&& std::rotr(max, 4) == max
|
||||
&& std::rotr(max, 5) == max
|
||||
&& std::rotr(max, 6) == max
|
||||
&& std::rotr(max, 7) == max
|
||||
;
|
||||
}
|
||||
assert(std::rotr(T(max - 1), 0) == T(max - 1));
|
||||
assert(std::rotr(T(max - 1), 1) == T(max - highbit));
|
||||
assert(std::rotr(T(max - 1), 2) == T(max - (highbit >> 1)));
|
||||
assert(std::rotr(T(max - 1), 3) == T(max - (highbit >> 2)));
|
||||
assert(std::rotr(T(max - 1), 4) == T(max - (highbit >> 3)));
|
||||
assert(std::rotr(T(max - 1), 5) == T(max - (highbit >> 4)));
|
||||
assert(std::rotr(T(max - 1), 6) == T(max - (highbit >> 5)));
|
||||
assert(std::rotr(T(max - 1), 7) == T(max - (highbit >> 6)));
|
||||
|
||||
assert(std::rotr(T(128), 0) == T(128));
|
||||
assert(std::rotr(T(128), 1) == T(64));
|
||||
assert(std::rotr(T(128), 2) == T(32));
|
||||
assert(std::rotr(T(128), 3) == T(16));
|
||||
assert(std::rotr(T(128), 4) == T(8));
|
||||
assert(std::rotr(T(128), 5) == T(4));
|
||||
assert(std::rotr(T(128), 6) == T(2));
|
||||
assert(std::rotr(T(128), 7) == T(1));
|
||||
|
||||
template <typename T>
|
||||
void runtime_test()
|
||||
{
|
||||
ASSERT_SAME_TYPE(T, decltype(std::rotr(T(0), 0)));
|
||||
ASSERT_NOEXCEPT( std::rotr(T(0), 0));
|
||||
const T max = std::numeric_limits<T>::max();
|
||||
const T val = std::numeric_limits<T>::max() - 1;
|
||||
#ifndef _LIBCPP_HAS_NO_INT128
|
||||
if constexpr (std::is_same_v<T, __uint128_t>) {
|
||||
T val = (T(1) << 63) | (T(1) << 64);
|
||||
assert(std::rotr(val, 0) == val);
|
||||
assert(std::rotr(val, 128) == val);
|
||||
assert(std::rotr(val, 256) == val);
|
||||
assert(std::rotr(val, 1) == val >> 1);
|
||||
assert(std::rotr(val, 127) == val << 1);
|
||||
assert(std::rotr(T(3), 1) == ((T(1) << 127) | T(1)));
|
||||
}
|
||||
#endif
|
||||
|
||||
const T uppers [] = {
|
||||
max, // not used
|
||||
max - max, // 000 .. 0
|
||||
max - (max >> 1), // 800 .. 0
|
||||
max - (max >> 2), // C00 .. 0
|
||||
max - (max >> 3), // E00 .. 0
|
||||
max - (max >> 4), // F00 .. 0
|
||||
max - (max >> 5), // F80 .. 0
|
||||
max - (max >> 6), // FC0 .. 0
|
||||
max - (max >> 7), // FE0 .. 0
|
||||
};
|
||||
|
||||
assert( std::rotr(val, 0) == val);
|
||||
assert( std::rotr(val, 1) == T((val >> 1) + uppers[1]));
|
||||
assert( std::rotr(val, 2) == T((val >> 2) + uppers[2]));
|
||||
assert( std::rotr(val, 3) == T((val >> 3) + uppers[3]));
|
||||
assert( std::rotr(val, 4) == T((val >> 4) + uppers[4]));
|
||||
assert( std::rotr(val, 5) == T((val >> 5) + uppers[5]));
|
||||
assert( std::rotr(val, 6) == T((val >> 6) + uppers[6]));
|
||||
assert( std::rotr(val, 7) == T((val >> 7) + uppers[7]));
|
||||
return true;
|
||||
}
|
||||
|
||||
int main(int, char**)
|
||||
{
|
||||
|
||||
{
|
||||
auto lambda = [](auto x) -> decltype(std::rotr(x, 1U)) {};
|
||||
auto lambda = [](auto x) -> decltype(std::rotl(x, 1U)) {};
|
||||
using L = decltype(lambda);
|
||||
|
||||
static_assert( std::is_invocable_v<L, unsigned char>, "");
|
||||
static_assert( std::is_invocable_v<L, unsigned int>, "");
|
||||
static_assert( std::is_invocable_v<L, unsigned long>, "");
|
||||
static_assert( std::is_invocable_v<L, unsigned long long>, "");
|
||||
|
||||
static_assert( std::is_invocable_v<L, uint8_t>, "");
|
||||
static_assert( std::is_invocable_v<L, uint16_t>, "");
|
||||
static_assert( std::is_invocable_v<L, uint32_t>, "");
|
||||
static_assert( std::is_invocable_v<L, uint64_t>, "");
|
||||
static_assert( std::is_invocable_v<L, size_t>, "");
|
||||
|
||||
static_assert( std::is_invocable_v<L, uintmax_t>, "");
|
||||
static_assert( std::is_invocable_v<L, uintptr_t>, "");
|
||||
|
||||
|
||||
static_assert(!std::is_invocable_v<L, int>, "");
|
||||
static_assert(!std::is_invocable_v<L, signed int>, "");
|
||||
static_assert(!std::is_invocable_v<L, long>, "");
|
||||
static_assert(!std::is_invocable_v<L, long long>, "");
|
||||
|
||||
static_assert(!std::is_invocable_v<L, int8_t>, "");
|
||||
static_assert(!std::is_invocable_v<L, int16_t>, "");
|
||||
static_assert(!std::is_invocable_v<L, int32_t>, "");
|
||||
static_assert(!std::is_invocable_v<L, int64_t>, "");
|
||||
static_assert(!std::is_invocable_v<L, ptrdiff_t>, "");
|
||||
|
||||
static_assert(!std::is_invocable_v<L, bool>, "");
|
||||
static_assert(!std::is_invocable_v<L, signed char>, "");
|
||||
static_assert(!std::is_invocable_v<L, char16_t>, "");
|
||||
static_assert(!std::is_invocable_v<L, char32_t>, "");
|
||||
|
||||
static_assert(!std::is_invocable_v<L, signed char>);
|
||||
static_assert(!std::is_invocable_v<L, short>);
|
||||
static_assert(!std::is_invocable_v<L, int>);
|
||||
static_assert(!std::is_invocable_v<L, long>);
|
||||
static_assert(!std::is_invocable_v<L, long long>);
|
||||
#ifndef _LIBCPP_HAS_NO_INT128
|
||||
static_assert( std::is_invocable_v<L, __uint128_t>, "");
|
||||
static_assert(!std::is_invocable_v<L, __int128_t>, "");
|
||||
static_assert(!std::is_invocable_v<L, __int128_t>);
|
||||
#endif
|
||||
|
||||
static_assert(!std::is_invocable_v<L, A, unsigned>, "");
|
||||
static_assert(!std::is_invocable_v<L, E1, unsigned>, "");
|
||||
static_assert(!std::is_invocable_v<L, E2, unsigned>, "");
|
||||
static_assert(!std::is_invocable_v<L, int8_t>);
|
||||
static_assert(!std::is_invocable_v<L, int16_t>);
|
||||
static_assert(!std::is_invocable_v<L, int32_t>);
|
||||
static_assert(!std::is_invocable_v<L, int64_t>);
|
||||
static_assert(!std::is_invocable_v<L, intmax_t>);
|
||||
static_assert(!std::is_invocable_v<L, intptr_t>);
|
||||
static_assert(!std::is_invocable_v<L, ptrdiff_t>);
|
||||
|
||||
static_assert(!std::is_invocable_v<L, bool>);
|
||||
static_assert(!std::is_invocable_v<L, char>);
|
||||
static_assert(!std::is_invocable_v<L, wchar_t>);
|
||||
#ifndef _LIBCPP_HAS_NO_CHAR8_T
|
||||
static_assert(!std::is_invocable_v<L, char8_t>);
|
||||
#endif
|
||||
static_assert(!std::is_invocable_v<L, char16_t>);
|
||||
static_assert(!std::is_invocable_v<L, char32_t>);
|
||||
|
||||
static_assert(!std::is_invocable_v<L, A>);
|
||||
static_assert(!std::is_invocable_v<L, A*>);
|
||||
static_assert(!std::is_invocable_v<L, E1>);
|
||||
static_assert(!std::is_invocable_v<L, E2>);
|
||||
}
|
||||
|
||||
static_assert(constexpr_test<unsigned char>(), "");
|
||||
static_assert(constexpr_test<unsigned short>(), "");
|
||||
static_assert(constexpr_test<unsigned>(), "");
|
||||
static_assert(constexpr_test<unsigned long>(), "");
|
||||
static_assert(constexpr_test<unsigned long long>(), "");
|
||||
|
||||
static_assert(constexpr_test<uint8_t>(), "");
|
||||
static_assert(constexpr_test<uint16_t>(), "");
|
||||
static_assert(constexpr_test<uint32_t>(), "");
|
||||
static_assert(constexpr_test<uint64_t>(), "");
|
||||
static_assert(constexpr_test<size_t>(), "");
|
||||
static_assert(constexpr_test<uintmax_t>(), "");
|
||||
static_assert(constexpr_test<uintptr_t>(), "");
|
||||
|
||||
static_assert(test<unsigned char>());
|
||||
static_assert(test<unsigned short>());
|
||||
static_assert(test<unsigned int>());
|
||||
static_assert(test<unsigned long>());
|
||||
static_assert(test<unsigned long long>());
|
||||
#ifndef _LIBCPP_HAS_NO_INT128
|
||||
static_assert(constexpr_test<__uint128_t>(), "");
|
||||
static_assert(test<__uint128_t>());
|
||||
#endif
|
||||
|
||||
static_assert(test<uint8_t>());
|
||||
static_assert(test<uint16_t>());
|
||||
static_assert(test<uint32_t>());
|
||||
static_assert(test<uint64_t>());
|
||||
static_assert(test<uintmax_t>());
|
||||
static_assert(test<uintptr_t>());
|
||||
static_assert(test<size_t>());
|
||||
|
||||
runtime_test<unsigned char>();
|
||||
runtime_test<unsigned>();
|
||||
runtime_test<unsigned short>();
|
||||
runtime_test<unsigned long>();
|
||||
runtime_test<unsigned long long>();
|
||||
|
||||
runtime_test<uint8_t>();
|
||||
runtime_test<uint16_t>();
|
||||
runtime_test<uint32_t>();
|
||||
runtime_test<uint64_t>();
|
||||
runtime_test<size_t>();
|
||||
runtime_test<uintmax_t>();
|
||||
runtime_test<uintptr_t>();
|
||||
|
||||
|
||||
test<unsigned char>();
|
||||
test<unsigned short>();
|
||||
test<unsigned int>();
|
||||
test<unsigned long>();
|
||||
test<unsigned long long>();
|
||||
#ifndef _LIBCPP_HAS_NO_INT128
|
||||
runtime_test<__uint128_t>();
|
||||
|
||||
{
|
||||
__uint128_t val = 168; // 0xA8 (aka 10101000)
|
||||
|
||||
assert( std::rotr(val, 128) == 168);
|
||||
val <<= 32;
|
||||
assert( std::rotr(val, 32) == 168);
|
||||
val <<= 2;
|
||||
assert( std::rotr(val, 33) == 336);
|
||||
val <<= 3;
|
||||
assert( std::rotr(val, 38) == 84);
|
||||
assert( std::rotr(val, 166) == 84);
|
||||
}
|
||||
test<__uint128_t>();
|
||||
#endif
|
||||
|
||||
test<uint8_t>();
|
||||
test<uint16_t>();
|
||||
test<uint32_t>();
|
||||
test<uint64_t>();
|
||||
test<uintmax_t>();
|
||||
test<uintptr_t>();
|
||||
test<size_t>();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue