]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/testsuite/26_numerics/bit/bit.count/countr_zero.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 26_numerics / bit / bit.count / countr_zero.cc
CommitLineData
8d9254fc 1// Copyright (C) 2018-2020 Free Software Foundation, Inc.
f3e91052
JW
2//
3// This file is part of the GNU ISO C++ Library. This library is free
4// software; you can redistribute it and/or modify it under the
5// terms of the GNU General Public License as published by the
6// Free Software Foundation; either version 3, or (at your option)
7// any later version.
8
9// This library is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License along
15// with this library; see the file COPYING3. If not see
16// <http://www.gnu.org/licenses/>.
17
f3e91052
JW
18// { dg-options "-std=gnu++2a" }
19// { dg-do compile { target c++2a } }
20
21#include <bit>
22
23template<typename UInt>
24constexpr auto
25test(UInt x)
26-> decltype(std::countr_zero(x))
27{
28 static_assert( noexcept(std::countr_zero(x)) );
29
30 constexpr unsigned digits = std::numeric_limits<UInt>::digits;
31
32 static_assert( std::countr_zero((UInt)0) == digits );
33 static_assert( std::countr_zero((UInt)-1) == 0 );
34 static_assert( std::countr_zero((UInt)127) == 0 );
35
36 static_assert( std::countr_zero((UInt)1) == 0 );
37 static_assert( std::countr_zero((UInt)2) == 1 );
38 static_assert( std::countr_zero((UInt)0x70) == 4 );
39
40 if constexpr (std::numeric_limits<UInt>::digits > 8)
41 {
42 static_assert( std::countr_zero((UInt)(1u << 8)) == 8 );
43 static_assert( std::countr_zero((UInt)(4u << 9)) == 11 );
44 }
45
46 if constexpr (std::numeric_limits<UInt>::digits > 64)
47 {
48 static_assert( std::countr_zero((UInt)2 << 70) == 71 );
49 }
50
51 return true;
52}
53
54static_assert( test( (unsigned char)0 ) );
55static_assert( test( (unsigned short)0 ) );
56static_assert( test( (unsigned int)0 ) );
57static_assert( test( (unsigned long)0 ) );
58static_assert( test( (unsigned long long)0 ) );
59
60// std::countr_zero(T) shall not participate in overload resolution
61// unless T is an unsigned integer type.
62struct X { constexpr bool did_not_match() { return true; } };
63constexpr X test(...) { return X{}; }
64static_assert( test( (bool)0 ).did_not_match() );
65static_assert( test( (char)0 ).did_not_match() );
66static_assert( test( (int)0 ).did_not_match() );
67static_assert( test( (char16_t)0 ).did_not_match() );
68static_assert( test( (float)0 ).did_not_match() );
69static_assert( test( (void*)0 ).did_not_match() );
70static_assert( test( X{} ).did_not_match() );
71enum E : unsigned { e };
72static_assert( test( e ).did_not_match() );
73
f3e91052
JW
74#if !defined(__STRICT_ANSI__) && defined _GLIBCXX_USE_INT128
75static_assert( test( (unsigned __int128)0 ) );
76static_assert( test( (__int128)0 ).did_not_match() );
77#endif
78#if defined(__GLIBCXX_TYPE_INT_N_0)
79static_assert( test( (unsigned __GLIBCXX_TYPE_INT_N_0)0 ) );
80static_assert( test( (__GLIBCXX_TYPE_INT_N_0)0 ).did_not_match() );
81#endif
82#if defined(__GLIBCXX_TYPE_INT_N_1)
83static_assert( test( (unsigned __GLIBCXX_TYPE_INT_N_1)0 ) );
84static_assert( test( (__GLIBCXX_TYPE_INT_N_1)0 ).did_not_match() );
85#endif
86#if defined(__GLIBCXX_TYPE_INT_N_2)
87static_assert( test( (unsigned __GLIBCXX_TYPE_INT_N_2)0 ) );
88static_assert( test( (__GLIBCXX_TYPE_INT_N_2)0 ).did_not_match() );
89#endif
47f79054
JW
90
91#include <cstddef>
92static_assert( test( (std::byte)0 ).did_not_match() );