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[[nodiscard]] 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
31inline constexpr byte& operator|=(byte& __lhs, byte __rhs) noexcept { return __lhs = __lhs | __rhs; }
32
33[[nodiscard]] inline constexpr byte operator&(byte __lhs, byte __rhs) noexcept {
34 return static_cast<byte>(
35 static_cast<unsigned char>(static_cast<unsigned int>(__lhs) & static_cast<unsigned int>(__rhs)));
36}
37
38inline constexpr byte& operator&=(byte& __lhs, byte __rhs) noexcept { return __lhs = __lhs & __rhs; }
39
40[[nodiscard]] inline constexpr byte operator^(byte __lhs, byte __rhs) noexcept {
41 return static_cast<byte>(
42 static_cast<unsigned char>(static_cast<unsigned int>(__lhs) ^ static_cast<unsigned int>(__rhs)));
43}
44
45inline constexpr byte& operator^=(byte& __lhs, byte __rhs) noexcept { return __lhs = __lhs ^ __rhs; }
46
47[[nodiscard]] inline constexpr byte operator~(byte __b) noexcept {
48 return static_cast<byte>(static_cast<unsigned char>(~static_cast<unsigned int>(__b)));
49}
50
51template <class _Integer, __enable_if_t<is_integral<_Integer>::value, int> = 0>
52constexpr byte& operator<<=(byte& __lhs, _Integer __shift) noexcept {
53 return __lhs = __lhs << __shift;
54}
55
56template <class _Integer, __enable_if_t<is_integral<_Integer>::value, int> = 0>
57[[nodiscard]] constexpr byte operator<<(byte __lhs, _Integer __shift) noexcept {
58 return static_cast<byte>(static_cast<unsigned char>(static_cast<unsigned int>(__lhs) << __shift));
59}
60
61template <class _Integer, __enable_if_t<is_integral<_Integer>::value, int> = 0>
62constexpr byte& operator>>=(byte& __lhs, _Integer __shift) noexcept {
63 return __lhs = __lhs >> __shift;
64}
65
66template <class _Integer, __enable_if_t<is_integral<_Integer>::value, int> = 0>
67[[nodiscard]] constexpr byte operator>>(byte __lhs, _Integer __shift) noexcept {
68 return static_cast<byte>(static_cast<unsigned char>(static_cast<unsigned int>(__lhs) >> __shift));
69}
70
71template <class _Integer, __enable_if_t<is_integral<_Integer>::value, int> = 0>
72[[nodiscard]] constexpr _Integer to_integer(byte __b) noexcept {
73 return static_cast<_Integer>(__b);
74}
75
76_LIBCPP_END_UNVERSIONED_NAMESPACE_STD
77#endif // _LIBCPP_STD_VER >= 17
78
79#endif // _LIBCPP___CSTDDEF_BYTE_H
80