1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 // UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
10 
11 #include <bit>
12 #include <cassert>
13 #include <cstdint>
14 #include <utility>
15 
16 #include "test_macros.h"
17 
18 template <class T>
19 concept has_byteswap = requires(T t) {
20   std::byteswap(t);
21 };
22 
23 static_assert(!has_byteswap<void*>);
24 static_assert(!has_byteswap<float>);
25 static_assert(!has_byteswap<char[2]>);
26 static_assert(!has_byteswap<std::byte>);
27 
28 template <class T>
29 constexpr void test_num(T in, T expected) {
30   assert(std::byteswap(in) == expected);
31   ASSERT_SAME_TYPE(decltype(std::byteswap(in)), decltype(in));
32   ASSERT_NOEXCEPT(std::byteswap(in));
33 }
34 
35 template <class T>
36 constexpr std::pair<T, T> get_test_data() {
37   switch (sizeof(T)) {
38   case 2:
39     return {static_cast<T>(0x1234), static_cast<T>(0x3412)};
40   case 4:
41     return {static_cast<T>(0x60AF8503), static_cast<T>(0x0385AF60)};
42   case 8:
43     return {static_cast<T>(0xABCDFE9477936406), static_cast<T>(0x0664937794FECDAB)};
44   }
45   assert(false);
46 }
47 
48 template <class T>
49 constexpr void test_implementation_defined_size() {
50   const auto [in, expected] = get_test_data<T>();
51   test_num<T>(in, expected);
52 }
53 
54 constexpr bool test() {
55   test_num<uint8_t>(0xAB, 0xAB);
56   test_num<uint16_t>(0xCDEF, 0xEFCD);
57   test_num<uint32_t>(0x01234567, 0x67452301);
58   test_num<uint64_t>(0x0123456789ABCDEF, 0xEFCDAB8967452301);
59 
60   test_num<int8_t>(0xAB, 0xAB);
61   test_num<int16_t>(0xCDEF, 0xEFCD);
62   test_num<int32_t>(0x01234567, 0x67452301);
63   test_num<int64_t>(0x0123456789ABCDEF, 0xEFCDAB8967452301);
64 
65 #ifndef _LIBCPP_HAS_NO_INT128
66   const auto in = static_cast<__uint128_t>(0x0123456789ABCDEF) << 64 | 0x13579BDF02468ACE;
67   const auto expected = static_cast<__uint128_t>(0xCE8A4602DF9B5713) << 64 | 0xEFCDAB8967452301;
68   test_num<__uint128_t>(in, expected);
69   test_num<__int128_t>(in, expected);
70 #endif
71 
72   test_num<bool>(true, true);
73   test_num<bool>(false, false);
74   test_num<char>(0xCD, 0xCD);
75   test_num<unsigned char>(0xEF, 0xEF);
76   test_num<signed char>(0x45, 0x45);
77   test_num<char8_t>(0xAB, 0xAB);
78   test_num<char16_t>(0xABCD, 0xCDAB);
79   test_num<char32_t>(0xABCDEF01, 0x01EFCDAB);
80 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
81   test_implementation_defined_size<wchar_t>();
82 #endif
83 
84   test_implementation_defined_size<short>();
85   test_implementation_defined_size<unsigned short>();
86   test_implementation_defined_size<int>();
87   test_implementation_defined_size<unsigned int>();
88   test_implementation_defined_size<long>();
89   test_implementation_defined_size<unsigned long>();
90   test_implementation_defined_size<long long>();
91   test_implementation_defined_size<unsigned long long>();
92   return true;
93 }
94 
95 int main(int, char**) {
96   test();
97   static_assert(test());
98 
99   return 0;
100 }
101