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 default: 45 assert(false); 46 return {}; // for MSVC, whose `assert` is tragically not [[noreturn]] 47 } 48 } 49 50 template <class T> 51 constexpr void test_implementation_defined_size() { 52 const auto [in, expected] = get_test_data<T>(); 53 test_num<T>(in, expected); 54 } 55 56 constexpr bool test() { 57 test_num<uint8_t>(0xAB, 0xAB); 58 test_num<uint16_t>(0xCDEF, 0xEFCD); 59 test_num<uint32_t>(0x01234567, 0x67452301); 60 test_num<uint64_t>(0x0123456789ABCDEF, 0xEFCDAB8967452301); 61 62 test_num<int8_t>(static_cast<int8_t>(0xAB), static_cast<int8_t>(0xAB)); 63 test_num<int16_t>(static_cast<int16_t>(0xCDEF), static_cast<int16_t>(0xEFCD)); 64 test_num<int32_t>(0x01234567, 0x67452301); 65 test_num<int64_t>(0x0123456789ABCDEF, 0xEFCDAB8967452301); 66 67 #ifndef TEST_HAS_NO_INT128 68 const auto in = static_cast<__uint128_t>(0x0123456789ABCDEF) << 64 | 0x13579BDF02468ACE; 69 const auto expected = static_cast<__uint128_t>(0xCE8A4602DF9B5713) << 64 | 0xEFCDAB8967452301; 70 test_num<__uint128_t>(in, expected); 71 test_num<__int128_t>(in, expected); 72 #endif 73 74 test_num<bool>(true, true); 75 test_num<bool>(false, false); 76 test_num<char>(static_cast<char>(0xCD), static_cast<char>(0xCD)); 77 test_num<unsigned char>(0xEF, 0xEF); 78 test_num<signed char>(0x45, 0x45); 79 test_num<char8_t>(0xAB, 0xAB); 80 test_num<char16_t>(0xABCD, 0xCDAB); 81 test_num<char32_t>(0xABCDEF01, 0x01EFCDAB); 82 #ifndef TEST_HAS_NO_WIDE_CHARACTERS 83 test_implementation_defined_size<wchar_t>(); 84 #endif 85 86 test_implementation_defined_size<short>(); 87 test_implementation_defined_size<unsigned short>(); 88 test_implementation_defined_size<int>(); 89 test_implementation_defined_size<unsigned int>(); 90 test_implementation_defined_size<long>(); 91 test_implementation_defined_size<unsigned long>(); 92 test_implementation_defined_size<long long>(); 93 test_implementation_defined_size<unsigned long long>(); 94 return true; 95 } 96 97 int main(int, char**) { 98 test(); 99 static_assert(test()); 100 101 return 0; 102 } 103