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 #ifndef _LIBCPP___CSTDDEF_BYTE_H 10 #define _LIBCPP___CSTDDEF_BYTE_H 11 12 #include <__config> 13 #include <__fwd/byte.h> 14 #include <__type_traits/enable_if.h> 15 #include <__type_traits/is_integral.h> 16 17 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 18 # pragma GCC system_header 19 #endif 20 21 #if _LIBCPP_STD_VER >= 17 22 namespace std { // purposefully not versioned 23 24 enum class byte : unsigned char {}; 25 26 _LIBCPP_HIDE_FROM_ABI inline constexpr byte operator|(byte __lhs, byte __rhs) noexcept { 27 return static_cast<byte>( 28 static_cast<unsigned char>(static_cast<unsigned int>(__lhs) | static_cast<unsigned int>(__rhs))); 29 } 30 31 _LIBCPP_HIDE_FROM_ABI inline constexpr byte& operator|=(byte& __lhs, byte __rhs) noexcept { 32 return __lhs = __lhs | __rhs; 33 } 34 35 _LIBCPP_HIDE_FROM_ABI inline constexpr byte operator&(byte __lhs, byte __rhs) noexcept { 36 return static_cast<byte>( 37 static_cast<unsigned char>(static_cast<unsigned int>(__lhs) & static_cast<unsigned int>(__rhs))); 38 } 39 40 _LIBCPP_HIDE_FROM_ABI inline constexpr byte& operator&=(byte& __lhs, byte __rhs) noexcept { 41 return __lhs = __lhs & __rhs; 42 } 43 44 _LIBCPP_HIDE_FROM_ABI inline constexpr byte operator^(byte __lhs, byte __rhs) noexcept { 45 return static_cast<byte>( 46 static_cast<unsigned char>(static_cast<unsigned int>(__lhs) ^ static_cast<unsigned int>(__rhs))); 47 } 48 49 _LIBCPP_HIDE_FROM_ABI inline constexpr byte& operator^=(byte& __lhs, byte __rhs) noexcept { 50 return __lhs = __lhs ^ __rhs; 51 } 52 53 _LIBCPP_HIDE_FROM_ABI inline constexpr byte operator~(byte __b) noexcept { 54 return static_cast<byte>(static_cast<unsigned char>(~static_cast<unsigned int>(__b))); 55 } 56 57 template <class _Integer, __enable_if_t<is_integral<_Integer>::value, int> = 0> 58 _LIBCPP_HIDE_FROM_ABI constexpr byte& operator<<=(byte& __lhs, _Integer __shift) noexcept { 59 return __lhs = __lhs << __shift; 60 } 61 62 template <class _Integer, __enable_if_t<is_integral<_Integer>::value, int> = 0> 63 _LIBCPP_HIDE_FROM_ABI constexpr byte operator<<(byte __lhs, _Integer __shift) noexcept { 64 return static_cast<byte>(static_cast<unsigned char>(static_cast<unsigned int>(__lhs) << __shift)); 65 } 66 67 template <class _Integer, __enable_if_t<is_integral<_Integer>::value, int> = 0> 68 _LIBCPP_HIDE_FROM_ABI constexpr byte& operator>>=(byte& __lhs, _Integer __shift) noexcept { 69 return __lhs = __lhs >> __shift; 70 } 71 72 template <class _Integer, __enable_if_t<is_integral<_Integer>::value, int> = 0> 73 _LIBCPP_HIDE_FROM_ABI constexpr byte operator>>(byte __lhs, _Integer __shift) noexcept { 74 return static_cast<byte>(static_cast<unsigned char>(static_cast<unsigned int>(__lhs) >> __shift)); 75 } 76 77 template <class _Integer, __enable_if_t<is_integral<_Integer>::value, int> = 0> 78 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Integer to_integer(byte __b) noexcept { 79 return static_cast<_Integer>(__b); 80 } 81 82 } // namespace std 83 #endif // _LIBCPP_STD_VER >= 17 84 85 #endif // _LIBCPP___CSTDDEF_BYTE_H 86