File: ops.cc

package info (click to toggle)
gcc-arm-none-eabi 15%3A8-2019-q3-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 571,828 kB
  • sloc: ansic: 2,937,651; cpp: 881,644; ada: 597,189; makefile: 65,528; asm: 56,499; xml: 46,621; exp: 24,747; sh: 19,684; python: 7,256; pascal: 4,370; awk: 3,497; perl: 2,695; yacc: 316; ml: 285; f90: 234; lex: 198; objc: 194; haskell: 119
file content (224 lines) | stat: -rw-r--r-- 6,657 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// Copyright (C) 2017-2018 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library.  This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.

// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING3.  If not see
// <http://www.gnu.org/licenses/>.

// { dg-options "-std=gnu++17" }
// { dg-do compile { target c++17 } }

#include <cstddef>

constexpr bool is_byte(std::byte) { return true; }
template<typename T> constexpr bool is_byte(T) { return false; }

template<typename T>
constexpr bool test_lshift_assign(unsigned char c, T t)
{
  std::byte b{c};
  b <<= t;
  return b == std::byte(c << t);
}

static_assert( test_lshift_assign(0, 1) );
static_assert( test_lshift_assign(0, 1u) );
static_assert( test_lshift_assign(0, 1ll) );
static_assert( test_lshift_assign(4, 1) );
static_assert( test_lshift_assign(9, 2u) );
static_assert( test_lshift_assign(16, 3ll) );
static_assert( test_lshift_assign(127, 1) );
static_assert( test_lshift_assign(255, 2u) );
static_assert( test_lshift_assign(63, 3ll) );

template<typename T>
constexpr bool test_lshift(unsigned char c, T t)
{
  const std::byte b{c};
  const std::byte b2 = b << t;
  return b2 == std::byte(c << t);
}

static_assert( test_lshift(0, 1) );
static_assert( test_lshift(0, 1u) );
static_assert( test_lshift(0, 1ll) );
static_assert( test_lshift(4, 1) );
static_assert( test_lshift(9, 2u) );
static_assert( test_lshift(16, 3ll) );
static_assert( test_lshift(127, 1) );
static_assert( test_lshift(255, 2u) );
static_assert( test_lshift(63, 3ll) );

template<typename T>
constexpr bool test_rshift_assign(unsigned char c, T t)
{
  std::byte b{c};
  b >>= t;
  return b == std::byte(c >> t);
}

static_assert( test_rshift_assign(0, 1) );
static_assert( test_rshift_assign(0, 1u) );
static_assert( test_rshift_assign(0, 1ll) );
static_assert( test_rshift_assign(4, 1) );
static_assert( test_rshift_assign(9, 2u) );
static_assert( test_rshift_assign(16, 3ll) );
static_assert( test_rshift_assign(127, 1) );
static_assert( test_rshift_assign(255, 2u) );
static_assert( test_rshift_assign(63, 3ll) );

template<typename T>
constexpr bool test_rshift(unsigned char c, T t)
{
  const std::byte b{c};
  const std::byte b2 = b >> t;
  return b2 == std::byte(c >> t);
}

static_assert( test_rshift(0, 1) );
static_assert( test_rshift(0, 1u) );
static_assert( test_rshift(0, 1ll) );
static_assert( test_rshift(4, 1) );
static_assert( test_rshift(9, 2u) );
static_assert( test_rshift(16, 3ll) );
static_assert( test_rshift(127, 1) );
static_assert( test_rshift(255, 2u) );
static_assert( test_rshift(63, 3ll) );

constexpr bool test_or_assign(unsigned char l, unsigned char r)
{
  std::byte b{l};
  b |= std::byte{r};
  return b == std::byte(l | r);
}

static_assert( test_or_assign(0, 1) );
static_assert( test_or_assign(4, 1) );
static_assert( test_or_assign(9, 2) );
static_assert( test_or_assign(16, 3) );
static_assert( test_or_assign(63, 3) );
static_assert( test_or_assign(127, 1) );
static_assert( test_or_assign(255, 2) );

constexpr bool test_or(unsigned char l, unsigned char r)
{
  const std::byte b1{l};
  const std::byte b2{r};
  return (b1 | b2) == std::byte(l | r);
}

static_assert( test_or(0, 1) );
static_assert( test_or(0, 1u) );
static_assert( test_or(0, 1ll) );
static_assert( test_or(4, 1) );
static_assert( test_or(9, 2u) );
static_assert( test_or(16, 3ll) );
static_assert( test_or(127, 1) );
static_assert( test_or(255, 2u) );
static_assert( test_or(63, 3ll) );

constexpr bool test_and_assign(unsigned char l, unsigned char r)
{
  std::byte b{l};
  b &= std::byte{r};
  return b == std::byte(l & r);
}

static_assert( test_and_assign(0, 1) );
static_assert( test_and_assign(0, 1u) );
static_assert( test_and_assign(0, 1ll) );
static_assert( test_and_assign(4, 1) );
static_assert( test_and_assign(9, 2u) );
static_assert( test_and_assign(16, 3ll) );
static_assert( test_and_assign(127, 1) );
static_assert( test_and_assign(255, 2u) );
static_assert( test_and_assign(63, 3ll) );

constexpr bool test_and(unsigned char l, unsigned char r)
{
  const std::byte b1{l};
  const std::byte b2{r};
  return (b1 & b2) == std::byte(l & r);
}

static_assert( test_and(0, 1) );
static_assert( test_and(0, 1u) );
static_assert( test_and(0, 1ll) );
static_assert( test_and(4, 1) );
static_assert( test_and(9, 2u) );
static_assert( test_and(16, 3ll) );
static_assert( test_and(127, 1) );
static_assert( test_and(255, 2u) );
static_assert( test_and(63, 3ll) );

constexpr bool test_xor_assign(unsigned char l, unsigned char r)
{
  std::byte b{l};
  b ^= std::byte{r};
  return b == std::byte(l ^ r);
}

static_assert( test_xor_assign(0, 1) );
static_assert( test_xor_assign(0, 1u) );
static_assert( test_xor_assign(0, 1ll) );
static_assert( test_xor_assign(4, 1) );
static_assert( test_xor_assign(9, 2u) );
static_assert( test_xor_assign(16, 3ll) );
static_assert( test_xor_assign(127, 1) );
static_assert( test_xor_assign(255, 2u) );
static_assert( test_xor_assign(63, 3ll) );

constexpr bool test_xor(unsigned char l, unsigned char r)
{
  const std::byte b1{l};
  const std::byte b2{r};
  return (b1 ^ b2) == std::byte(l ^ r);
}

static_assert( test_xor(0, 1) );
static_assert( test_xor(0, 1u) );
static_assert( test_xor(0, 1ll) );
static_assert( test_xor(4, 1) );
static_assert( test_xor(9, 2u) );
static_assert( test_xor(16, 3ll) );
static_assert( test_xor(127, 1) );
static_assert( test_xor(255, 2u) );
static_assert( test_xor(63, 3ll) );

constexpr bool test_complement(unsigned char c)
{
  const std::byte b{c};
  return ~b == std::byte(~c);
}

static_assert( test_complement(0) );
static_assert( test_complement(4) );
static_assert( test_complement(9) );
static_assert( test_complement(16) );
static_assert( test_complement(63) );
static_assert( test_complement(127) );
static_assert( test_complement(255) );

template<typename T>
constexpr bool test_to_integer(unsigned char c)
{
  std::byte b{c};
  return std::to_integer<T>(b) == T(c);
}

static_assert( test_to_integer<int>(0) );
static_assert( test_to_integer<int>(255) );
static_assert( test_to_integer<signed char>(255) );
static_assert( test_to_integer<unsigned>(0) );
static_assert( test_to_integer<unsigned>(255) );