]>
Commit | Line | Data |
---|---|---|
a945c346 | 1 | // Copyright (C) 2021-2024 Free Software Foundation, Inc. |
7393fa8b JJ |
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 | ||
7393fa8b | 18 | // { dg-do compile { target c++23 } } |
f4ab6846 | 19 | // { dg-add-options no_pch } |
7393fa8b JJ |
20 | |
21 | #include <bit> | |
22 | ||
23 | #ifndef __cpp_lib_byteswap | |
24 | # error "Feature-test macro for byteswap missing in <bit>" | |
25 | #elif __cpp_lib_byteswap != 202110L | |
26 | # error "Feature-test macro for byteswap has wrong value in <bit>" | |
27 | #endif | |
28 | ||
29 | #include <cstdint> | |
30 | #include <cstring> | |
31 | #include <testsuite_hooks.h> | |
32 | ||
33 | void | |
34 | test01() | |
35 | { | |
36 | static_assert( std::byteswap<int8_t>(0x12) == 0x12 ); | |
37 | static_assert( std::byteswap<int16_t>(0x1234) == 0x3412 ); | |
38 | static_assert( std::byteswap<int32_t>(0x12345678) == 0x78563412 ); | |
39 | static_assert( std::byteswap<int64_t>(0x123456789abcdef0) | |
40 | == static_cast<int64_t>(0xf0debc9a78563412) ); | |
41 | static_assert( std::byteswap<uint8_t>(0x21) == 0x21 ); | |
42 | static_assert( std::byteswap<uint16_t>(0x4321) == 0x2143 ); | |
43 | static_assert( std::byteswap<uint32_t>(0x87654321) == 0x21436587 ); | |
44 | static_assert( std::byteswap<uint64_t>(0xfedcba9876543210) | |
45 | == static_cast<uint64_t>(0x1032547698badcfe) ); | |
46 | #if !defined(__STRICT_ANSI__) && defined __SIZEOF_INT128__ | |
47 | constexpr __int128_t c1 = (static_cast<__int128_t>(0x0102030405060708) << 64 | |
48 | | 0x090a0b0c0d0e0f10); | |
49 | constexpr __int128_t c2 = (static_cast<__int128_t>(0x100f0e0d0c0b0a09) << 64 | |
50 | | 0x0807060504030201); | |
51 | constexpr __int128_t c3 = (static_cast<__int128_t>(0xf1e2d3c4b5a69788) << 64 | |
52 | | 0x796a5b4c3d2e1f10); | |
53 | constexpr __int128_t c4 = (static_cast<__int128_t>(0x101f2e3d4c5b6a79) << 64 | |
54 | | 0x8897a6b5c4d3e2f1); | |
55 | static_assert( std::byteswap(c1) == c2 ); | |
56 | static_assert( std::byteswap(static_cast<__uint128_t>(c1)) | |
57 | == static_cast<__uint128_t>(c2) ); | |
58 | static_assert( std::byteswap(c3) == c4 ); | |
59 | #endif | |
60 | static_assert( std::byteswap<const uint32_t>(0xdeadbeef) == 0xefbeadde ); | |
61 | static_assert( std::byteswap<volatile uint32_t>(0xdeadbeef) == 0xefbeadde ); | |
62 | static_assert( std::byteswap<int32_t>(0xdeadbeef) | |
63 | == static_cast<int32_t>(0xefbeadde) ); | |
64 | } | |
65 | ||
66 | void | |
67 | test02() | |
68 | { | |
69 | volatile int8_t a = 0x12; | |
70 | volatile int16_t b = 0x1234; | |
71 | volatile int32_t c = 0x12345678; | |
72 | volatile int64_t d = 0x123456789abcdef0; | |
73 | volatile uint8_t e = 0x21; | |
74 | volatile uint16_t f = 0x4321; | |
75 | volatile uint32_t g = 0x87654321; | |
76 | volatile uint64_t h = 0xfedcba9876543210; | |
77 | VERIFY ( std::byteswap<int8_t>(a) == 0x12 ); | |
78 | VERIFY ( std::byteswap<int16_t>(b) == 0x3412 ); | |
79 | VERIFY ( std::byteswap(c) == 0x78563412 ); | |
80 | VERIFY ( std::byteswap(d) == 0xf0debc9a78563412 ); | |
81 | VERIFY ( std::byteswap<uint8_t>(e) == 0x21 ); | |
82 | VERIFY ( std::byteswap<uint16_t>(f) == 0x2143 ); | |
83 | VERIFY ( std::byteswap(g) == 0x21436587 ); | |
84 | VERIFY ( std::byteswap(h) == 0x1032547698badcfe ); | |
85 | VERIFY ( std::byteswap(std::byteswap<int8_t>(a)) == a ); | |
86 | VERIFY ( std::byteswap(std::byteswap<int16_t>(b)) == b ); | |
87 | VERIFY ( std::byteswap(std::byteswap(c)) == c ); | |
88 | VERIFY ( std::byteswap(std::byteswap(d)) == d ); | |
89 | VERIFY ( std::byteswap(std::byteswap<uint8_t>(e)) == e ); | |
90 | VERIFY ( std::byteswap(std::byteswap<uint16_t>(f)) == f ); | |
91 | VERIFY ( std::byteswap(std::byteswap(g)) == g ); | |
92 | VERIFY ( std::byteswap(std::byteswap(h)) == h ); | |
93 | #if !defined(__STRICT_ANSI__) && defined __SIZEOF_INT128__ | |
94 | volatile __int128_t c1 = (static_cast<__int128_t>(0x0102030405060708) << 64 | |
95 | | 0x090a0b0c0d0e0f10); | |
96 | volatile __int128_t c2 = (static_cast<__int128_t>(0x100f0e0d0c0b0a09) << 64 | |
97 | | 0x0807060504030201); | |
98 | VERIFY ( std::byteswap(c1) == c2 ); | |
99 | VERIFY ( std::byteswap<__uint128_t>(c1) == static_cast<__uint128_t>(c2) ); | |
100 | VERIFY ( std::byteswap(std::byteswap(c1)) == c1 ); | |
101 | VERIFY ( std::byteswap(std::byteswap<__uint128_t>(c2)) | |
102 | == static_cast<__uint128_t>(c2) ); | |
103 | #endif | |
104 | VERIFY ( std::byteswap<const uint32_t>(0xdeadbeef) == 0xefbeadde ); | |
105 | VERIFY ( std::byteswap<volatile uint32_t>(0xdeadbeef) == 0xefbeadde ); | |
106 | } | |
107 | ||
108 | int main() | |
109 | { | |
110 | test01(); | |
111 | test02(); | |
112 | } |