Path: blob/main/contrib/llvm-project/libcxx/include/__bit/byteswap.h
35260 views
// -*- C++ -*-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-exception6//7//===----------------------------------------------------------------------===//89#ifndef _LIBCPP___BIT_BYTESWAP_H10#define _LIBCPP___BIT_BYTESWAP_H1112#include <__concepts/arithmetic.h>13#include <__config>14#include <cstdint>1516#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)17# pragma GCC system_header18#endif1920_LIBCPP_BEGIN_NAMESPACE_STD2122#if _LIBCPP_STD_VER >= 232324template <integral _Tp>25[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp byteswap(_Tp __val) noexcept {26if constexpr (sizeof(_Tp) == 1) {27return __val;28} else if constexpr (sizeof(_Tp) == 2) {29return __builtin_bswap16(__val);30} else if constexpr (sizeof(_Tp) == 4) {31return __builtin_bswap32(__val);32} else if constexpr (sizeof(_Tp) == 8) {33return __builtin_bswap64(__val);34# ifndef _LIBCPP_HAS_NO_INT12835} else if constexpr (sizeof(_Tp) == 16) {36# if __has_builtin(__builtin_bswap128)37return __builtin_bswap128(__val);38# else39return static_cast<_Tp>(byteswap(static_cast<uint64_t>(__val))) << 64 |40static_cast<_Tp>(byteswap(static_cast<uint64_t>(__val >> 64)));41# endif // __has_builtin(__builtin_bswap128)42# endif // _LIBCPP_HAS_NO_INT12843} else {44static_assert(sizeof(_Tp) == 0, "byteswap is unimplemented for integral types of this size");45}46}4748#endif // _LIBCPP_STD_VER >= 234950_LIBCPP_END_NAMESPACE_STD5152#endif // _LIBCPP___BIT_BYTESWAP_H535455