xref: /freebsd/contrib/llvm-project/libcxx/include/__bit/byteswap.h (revision e32fecd0c2c3ee37c47ee100f169e7eb0282a873)
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 #include <cstdlib>
17 
18 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
19 #  pragma GCC system_header
20 #endif
21 
22 _LIBCPP_BEGIN_NAMESPACE_STD
23 
24 #if _LIBCPP_STD_VER > 20
25 
26 template <integral _Tp>
27 _LIBCPP_HIDE_FROM_ABI constexpr _Tp byteswap(_Tp __val) noexcept {
28 
29   if constexpr (sizeof(_Tp) == 1) {
30     return __val;
31   } else if constexpr (sizeof(_Tp) == 2) {
32     return __builtin_bswap16(__val);
33   } else if constexpr (sizeof(_Tp) == 4) {
34     return __builtin_bswap32(__val);
35   } else if constexpr (sizeof(_Tp) == 8) {
36     return __builtin_bswap64(__val);
37 #ifndef _LIBCPP_HAS_NO_INT128
38   } else if constexpr (sizeof(_Tp) == 16) {
39 #if __has_builtin(__builtin_bswap128)
40     return __builtin_bswap128(__val);
41 #else
42     return static_cast<_Tp>(byteswap(static_cast<uint64_t>(__val))) << 64 |
43            static_cast<_Tp>(byteswap(static_cast<uint64_t>(__val >> 64)));
44 #endif // __has_builtin(__builtin_bswap128)
45 #endif // _LIBCPP_HAS_NO_INT128
46   } else {
47     static_assert(sizeof(_Tp) == 0, "byteswap is unimplemented for integral types of this size");
48   }
49 }
50 
51 #endif // _LIBCPP_STD_VER > 20
52 
53 _LIBCPP_END_NAMESPACE_STD
54 
55 #endif // _LIBCPP___BIT_BYTESWAP_H
56