1 //===-- A self contained equivalent of cstddef ------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_LIBC_SRC_SUPPORT_CPP_BYTE_H
10 #define LLVM_LIBC_SRC_SUPPORT_CPP_BYTE_H
12 #include "type_traits.h" // For enable_if_t, is_integral_v.
14 namespace __llvm_libc::cpp
{
16 enum class byte
: unsigned char {};
18 template <class IntegerType
>
19 constexpr enable_if_t
<is_integral_v
<IntegerType
>, byte
>
20 operator>>(byte b
, IntegerType shift
) noexcept
{
21 return static_cast<byte
>(static_cast<unsigned char>(b
) >> shift
);
23 template <class IntegerType
>
24 constexpr enable_if_t
<is_integral_v
<IntegerType
>, byte
&>
25 operator>>=(byte
&b
, IntegerType shift
) noexcept
{
26 return b
= b
>> shift
;
28 template <class IntegerType
>
29 constexpr enable_if_t
<is_integral_v
<IntegerType
>, byte
>
30 operator<<(byte b
, IntegerType shift
) noexcept
{
31 return static_cast<byte
>(static_cast<unsigned char>(b
) << shift
);
33 template <class IntegerType
>
34 constexpr enable_if_t
<is_integral_v
<IntegerType
>, byte
&>
35 operator<<=(byte
&b
, IntegerType shift
) noexcept
{
36 return b
= b
<< shift
;
38 constexpr byte
operator|(byte l
, byte r
) noexcept
{
39 return static_cast<byte
>(static_cast<unsigned char>(l
) |
40 static_cast<unsigned char>(r
));
42 constexpr byte
&operator|=(byte
&l
, byte r
) noexcept
{ return l
= l
| r
; }
43 constexpr byte
operator&(byte l
, byte r
) noexcept
{
44 return static_cast<byte
>(static_cast<unsigned char>(l
) &
45 static_cast<unsigned char>(r
));
47 constexpr byte
&operator&=(byte
&l
, byte r
) noexcept
{ return l
= l
& r
; }
48 constexpr byte
operator^(byte l
, byte r
) noexcept
{
49 return static_cast<byte
>(static_cast<unsigned char>(l
) ^
50 static_cast<unsigned char>(r
));
52 constexpr byte
&operator^=(byte
&l
, byte r
) noexcept
{ return l
= l
^ r
; }
53 constexpr byte
operator~(byte b
) noexcept
{
54 return static_cast<byte
>(~static_cast<unsigned char>(b
));
56 template <typename IntegerType
>
57 constexpr enable_if_t
<is_integral_v
<IntegerType
>, IntegerType
>
58 to_integer(byte b
) noexcept
{
59 return static_cast<IntegerType
>(b
);
62 } // namespace __llvm_libc::cpp
64 #endif // LLVM_LIBC_SRC_SUPPORT_CPP_BYTE_H