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_LIBCPP_BEGIN_UNVERSIONED_NAMESPACE_STD
23
24enum 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
57template <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
62template <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
67template <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
72template <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
77template <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_LIBCPP_END_UNVERSIONED_NAMESPACE_STD
83#endif // _LIBCPP_STD_VER >= 17
84
85#endif // _LIBCPP___CSTDDEF_BYTE_H
86