]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/testsuite/26_numerics/bit/bitops.rot/rotl.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 26_numerics / bit / bitops.rot / rotl.cc
CommitLineData
fbd26352 1// Copyright (C) 2018-2019 Free Software Foundation, Inc.
927fb597 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
18// { dg-do run { target c++11 } }
19
20// { dg-options "-std=gnu++2a" }
21// { dg-do compile { target c++2a } }
22
23#include <bit>
24
25template<typename UInt>
26constexpr auto
27test(UInt x)
28-> decltype(std::rotl(x, 0u))
29{
30 static_assert( noexcept(std::rotl(x, 0u)) );
31
32 constexpr unsigned digits = std::numeric_limits<UInt>::digits;
33
34 static_assert( std::rotl((UInt)0, 0) == 0 );
35 static_assert( std::rotl((UInt)0, 1) == 0 );
36 static_assert( std::rotl((UInt)0, 4) == 0 );
37 static_assert( std::rotl((UInt)0, 8) == 0 );
38 static_assert( std::rotl((UInt)-1, 0) == (UInt)-1 );
39 static_assert( std::rotl((UInt)-1, 1) == (UInt)-1 );
40 static_assert( std::rotl((UInt)-1, 4) == (UInt)-1 );
41 static_assert( std::rotl((UInt)-1, 8) == (UInt)-1 );
42
43 static_assert( std::rotl((UInt)1, 0) == (UInt)1 << 0 );
44 static_assert( std::rotl((UInt)1, 1) == (UInt)1 << 1 );
45 static_assert( std::rotl((UInt)1, 4) == (UInt)1 << 4 );
46 static_assert( std::rotl((UInt)1, digits) == (UInt)1 );
47 static_assert( std::rotl((UInt)7, digits) == (UInt)7 );
48 static_assert( std::rotl((UInt)6, digits - 1) == (UInt)3 );
49 static_assert( std::rotl((UInt)3, 6) == (UInt)3 << 6 );
50
51 static_assert( std::rotl((UInt)0b0110'1100, 1) == 0b1101'1000 );
52 static_assert( std::rotl((UInt)0b0110'1100, digits - 1) == 0b0011'0110 );
53
54 static_assert( std::rotl((UInt)0x01, 0 ) == 0x01 );
55 static_assert( std::rotl((UInt)0x10, 0 ) == 0x10 );
56 static_assert( std::rotl((UInt)0x10, 1 ) == 0x20 );
57 static_assert( std::rotl((UInt)0x10, 2 ) == 0x40 );
58 static_assert( std::rotl((UInt)0x10, 3 ) == 0x80 );
59 static_assert( std::rotl((UInt)0x11, 1 ) == 0x22 );
60 static_assert( std::rotl((UInt)0x11, 2 ) == 0x44 );
61
62 if constexpr (std::numeric_limits<UInt>::digits > 8)
63 {
64 static_assert( std::rotl((UInt)0b0011'0111, 3) == 0b1'1011'1000 );
65 static_assert( std::rotl((UInt)0b1010'0101, 4) == 0b1010'0101'0000 );
66 }
67
68 return true;
69}
70
71static_assert( test( (unsigned char)0 ) );
72static_assert( test( (unsigned short)0 ) );
73static_assert( test( (unsigned int)0 ) );
74static_assert( test( (unsigned long)0 ) );
75static_assert( test( (unsigned long long)0 ) );
76
77// std::rotl(T) shall not participate in overload resolution
78// unless T is an unsigned integer type.
79struct X { constexpr bool did_not_match() { return true; } };
80constexpr X test(...) { return X{}; }
81static_assert( test( (bool)0 ).did_not_match() );
82static_assert( test( (char)0 ).did_not_match() );
83static_assert( test( (int)0 ).did_not_match() );
84static_assert( test( (char16_t)0 ).did_not_match() );
85static_assert( test( (float)0 ).did_not_match() );
86static_assert( test( (void*)0 ).did_not_match() );
87static_assert( test( X{} ).did_not_match() );
88enum E : unsigned { e };
89static_assert( test( e ).did_not_match() );
90
91#ifndef __STRICT_ANSI__
92#include <cstddef>
93static_assert( std::rotl(std::byte{0}, 4) == std::byte{0} );
94static_assert( std::rotl(std::byte{0x01}, 4) == std::byte{0x10} );
95static_assert( std::rotl(std::byte{0x02}, 3) == std::byte{0x10} );
96static_assert( std::rotl(std::byte{0x03}, 2) == std::byte{0x0c} );
97static_assert( std::rotl(std::byte{0x30}, 2) == std::byte{0xc0} );
98static_assert( std::rotl(std::byte{0x40}, 1) == std::byte{0x80} );
99static_assert( std::rotl(std::byte{0x41}, 9) == std::byte{0x82} );
100#else
101static_assert( test( (std::byte)0 ).did_not_match() );
102#endif
103
104#if !defined(__STRICT_ANSI__) && defined _GLIBCXX_USE_INT128
105static_assert( test( (unsigned __int128)0 ) );
106static_assert( test( (__int128)0 ).did_not_match() );
107#endif
108#if defined(__GLIBCXX_TYPE_INT_N_0)
109static_assert( test( (unsigned __GLIBCXX_TYPE_INT_N_0)0 ) );
110static_assert( test( (__GLIBCXX_TYPE_INT_N_0)0 ).did_not_match() );
111#endif
112#if defined(__GLIBCXX_TYPE_INT_N_1)
113static_assert( test( (unsigned __GLIBCXX_TYPE_INT_N_1)0 ) );
114static_assert( test( (__GLIBCXX_TYPE_INT_N_1)0 ).did_not_match() );
115#endif
116#if defined(__GLIBCXX_TYPE_INT_N_2)
117static_assert( test( (unsigned __GLIBCXX_TYPE_INT_N_2)0 ) );
118static_assert( test( (__GLIBCXX_TYPE_INT_N_2)0 ).did_not_match() );
119#endif