Revert "[libc] Use best-fit binary trie to make malloc logarithmic" (#117065)
[llvm-project.git] / libcxx / include / __bit / byteswap.h
blobd761e6a6fdb46ed35b8c8f69071e14d6bc7827a2
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 //===----------------------------------------------------------------------===//
10 #ifndef _LIBCPP___BIT_BYTESWAP_H
11 #define _LIBCPP___BIT_BYTESWAP_H
13 #include <__concepts/arithmetic.h>
14 #include <__config>
15 #include <cstdint>
17 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
18 # pragma GCC system_header
19 #endif
21 _LIBCPP_BEGIN_NAMESPACE_STD
23 #if _LIBCPP_STD_VER >= 23
25 template <integral _Tp>
26 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp byteswap(_Tp __val) noexcept {
27 if constexpr (sizeof(_Tp) == 1) {
28 return __val;
29 } else if constexpr (sizeof(_Tp) == 2) {
30 return __builtin_bswap16(__val);
31 } else if constexpr (sizeof(_Tp) == 4) {
32 return __builtin_bswap32(__val);
33 } else if constexpr (sizeof(_Tp) == 8) {
34 return __builtin_bswap64(__val);
35 # if _LIBCPP_HAS_INT128
36 } else if constexpr (sizeof(_Tp) == 16) {
37 # if __has_builtin(__builtin_bswap128)
38 return __builtin_bswap128(__val);
39 # else
40 return static_cast<_Tp>(byteswap(static_cast<uint64_t>(__val))) << 64 |
41 static_cast<_Tp>(byteswap(static_cast<uint64_t>(__val >> 64)));
42 # endif // __has_builtin(__builtin_bswap128)
43 # endif // _LIBCPP_HAS_INT128
44 } else {
45 static_assert(sizeof(_Tp) == 0, "byteswap is unimplemented for integral types of this size");
49 #endif // _LIBCPP_STD_VER >= 23
51 _LIBCPP_END_NAMESPACE_STD
53 #endif // _LIBCPP___BIT_BYTESWAP_H