236
|
1 // -*- C++ -*-
|
|
2 //===----------------------------------------------------------------------===//
|
|
3 //
|
|
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
5 // See https://llvm.org/LICENSE.txt for license information.
|
|
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
7 //
|
|
8 //===----------------------------------------------------------------------===//
|
|
9
|
|
10 #ifndef _LIBCPP___BIT_BYTESWAP_H
|
|
11 #define _LIBCPP___BIT_BYTESWAP_H
|
|
12
|
|
13 #include <__concepts/arithmetic.h>
|
|
14 #include <__config>
|
|
15 #include <cstdint>
|
|
16
|
|
17 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
|
|
18 # pragma GCC system_header
|
|
19 #endif
|
|
20
|
|
21 _LIBCPP_BEGIN_NAMESPACE_STD
|
|
22
|
252
|
23 #if _LIBCPP_STD_VER >= 23
|
236
|
24
|
|
25 template <integral _Tp>
|
252
|
26 _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI constexpr _Tp byteswap(_Tp __val) noexcept {
|
236
|
27
|
|
28 if constexpr (sizeof(_Tp) == 1) {
|
|
29 return __val;
|
|
30 } else if constexpr (sizeof(_Tp) == 2) {
|
|
31 return __builtin_bswap16(__val);
|
|
32 } else if constexpr (sizeof(_Tp) == 4) {
|
|
33 return __builtin_bswap32(__val);
|
|
34 } else if constexpr (sizeof(_Tp) == 8) {
|
|
35 return __builtin_bswap64(__val);
|
|
36 #ifndef _LIBCPP_HAS_NO_INT128
|
|
37 } else if constexpr (sizeof(_Tp) == 16) {
|
|
38 #if __has_builtin(__builtin_bswap128)
|
|
39 return __builtin_bswap128(__val);
|
|
40 #else
|
|
41 return static_cast<_Tp>(byteswap(static_cast<uint64_t>(__val))) << 64 |
|
|
42 static_cast<_Tp>(byteswap(static_cast<uint64_t>(__val >> 64)));
|
|
43 #endif // __has_builtin(__builtin_bswap128)
|
|
44 #endif // _LIBCPP_HAS_NO_INT128
|
|
45 } else {
|
|
46 static_assert(sizeof(_Tp) == 0, "byteswap is unimplemented for integral types of this size");
|
|
47 }
|
|
48 }
|
|
49
|
252
|
50 #endif // _LIBCPP_STD_VER >= 23
|
236
|
51
|
|
52 _LIBCPP_END_NAMESPACE_STD
|
|
53
|
|
54 #endif // _LIBCPP___BIT_BYTESWAP_H
|