summaryrefslogtreecommitdiff
path: root/libstdc++-v3/testsuite/26_numerics/bit/bit.byteswap/byteswap.cc
blob: f447af020e6539bfc5537c7405a298ae52145c08 (plain)
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
// Copyright (C) 2021 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++2b" }
// { dg-do compile { target c++23 } }

#include <bit>

#ifndef __cpp_lib_byteswap
# error "Feature-test macro for byteswap missing in <bit>"
#elif __cpp_lib_byteswap != 202110L
# error "Feature-test macro for byteswap has wrong value in <bit>"
#endif

#include <cstdint>
#include <cstring>
#include <testsuite_hooks.h>

void
test01()
{
  static_assert( std::byteswap<int8_t>(0x12) == 0x12 );
  static_assert( std::byteswap<int16_t>(0x1234) == 0x3412 );
  static_assert( std::byteswap<int32_t>(0x12345678) == 0x78563412 );
  static_assert( std::byteswap<int64_t>(0x123456789abcdef0)
		 == static_cast<int64_t>(0xf0debc9a78563412) );
  static_assert( std::byteswap<uint8_t>(0x21) == 0x21 );
  static_assert( std::byteswap<uint16_t>(0x4321) == 0x2143 );
  static_assert( std::byteswap<uint32_t>(0x87654321) == 0x21436587 );
  static_assert( std::byteswap<uint64_t>(0xfedcba9876543210)
		 == static_cast<uint64_t>(0x1032547698badcfe) );
#if !defined(__STRICT_ANSI__) && defined __SIZEOF_INT128__
  constexpr __int128_t c1 = (static_cast<__int128_t>(0x0102030405060708) << 64
			     | 0x090a0b0c0d0e0f10);
  constexpr __int128_t c2 = (static_cast<__int128_t>(0x100f0e0d0c0b0a09) << 64
			     | 0x0807060504030201);
  constexpr __int128_t c3 = (static_cast<__int128_t>(0xf1e2d3c4b5a69788) << 64
			     | 0x796a5b4c3d2e1f10);
  constexpr __int128_t c4 = (static_cast<__int128_t>(0x101f2e3d4c5b6a79) << 64
			     | 0x8897a6b5c4d3e2f1);
  static_assert( std::byteswap(c1) == c2 );
  static_assert( std::byteswap(static_cast<__uint128_t>(c1))
		 == static_cast<__uint128_t>(c2) );
  static_assert( std::byteswap(c3) == c4 );
#endif
  static_assert( std::byteswap<const uint32_t>(0xdeadbeef) == 0xefbeadde );
  static_assert( std::byteswap<volatile uint32_t>(0xdeadbeef) == 0xefbeadde );
  static_assert( std::byteswap<int32_t>(0xdeadbeef)
		 == static_cast<int32_t>(0xefbeadde) );
}

void
test02()
{
  volatile int8_t a = 0x12;
  volatile int16_t b = 0x1234;
  volatile int32_t c = 0x12345678;
  volatile int64_t d = 0x123456789abcdef0;
  volatile uint8_t e = 0x21;
  volatile uint16_t f = 0x4321;
  volatile uint32_t g = 0x87654321;
  volatile uint64_t h = 0xfedcba9876543210;
  VERIFY ( std::byteswap<int8_t>(a) == 0x12 );
  VERIFY ( std::byteswap<int16_t>(b) == 0x3412 );
  VERIFY ( std::byteswap(c) == 0x78563412 );
  VERIFY ( std::byteswap(d) == 0xf0debc9a78563412 );
  VERIFY ( std::byteswap<uint8_t>(e) == 0x21 );
  VERIFY ( std::byteswap<uint16_t>(f) == 0x2143 );
  VERIFY ( std::byteswap(g) == 0x21436587 );
  VERIFY ( std::byteswap(h) == 0x1032547698badcfe );
  VERIFY ( std::byteswap(std::byteswap<int8_t>(a)) == a );
  VERIFY ( std::byteswap(std::byteswap<int16_t>(b)) == b );
  VERIFY ( std::byteswap(std::byteswap(c)) == c );
  VERIFY ( std::byteswap(std::byteswap(d)) == d );
  VERIFY ( std::byteswap(std::byteswap<uint8_t>(e)) == e );
  VERIFY ( std::byteswap(std::byteswap<uint16_t>(f)) == f );
  VERIFY ( std::byteswap(std::byteswap(g)) == g );
  VERIFY ( std::byteswap(std::byteswap(h)) == h );
#if !defined(__STRICT_ANSI__) && defined __SIZEOF_INT128__
  volatile __int128_t c1 = (static_cast<__int128_t>(0x0102030405060708) << 64
			    | 0x090a0b0c0d0e0f10);
  volatile __int128_t c2 = (static_cast<__int128_t>(0x100f0e0d0c0b0a09) << 64
			    | 0x0807060504030201);
  VERIFY ( std::byteswap(c1) == c2 );
  VERIFY ( std::byteswap<__uint128_t>(c1) == static_cast<__uint128_t>(c2) );
  VERIFY ( std::byteswap(std::byteswap(c1)) == c1 );
  VERIFY ( std::byteswap(std::byteswap<__uint128_t>(c2))
	   == static_cast<__uint128_t>(c2) );
#endif
  VERIFY ( std::byteswap<const uint32_t>(0xdeadbeef) == 0xefbeadde );
  VERIFY ( std::byteswap<volatile uint32_t>(0xdeadbeef) == 0xefbeadde );
}

int main()
{
  test01();
  test02();
}