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_CSTDDEF_H
10 #define LLVM_LIBC_SRC___SUPPORT_CPP_CSTDDEF_H
12 #include "src/__support/macros/attributes.h"
13 #include "type_traits.h" // For enable_if_t, is_integral_v.
15 namespace LIBC_NAMESPACE::cpp
{
17 enum class byte
: unsigned char {};
19 template <class IntegerType
>
20 LIBC_INLINE
constexpr enable_if_t
<is_integral_v
<IntegerType
>, byte
>
21 operator>>(byte b
, IntegerType shift
) noexcept
{
22 return static_cast<byte
>(static_cast<unsigned char>(b
) >> shift
);
24 template <class IntegerType
>
25 LIBC_INLINE
constexpr enable_if_t
<is_integral_v
<IntegerType
>, byte
&>
26 operator>>=(byte
&b
, IntegerType shift
) noexcept
{
27 return b
= b
>> shift
;
29 template <class IntegerType
>
30 LIBC_INLINE
constexpr enable_if_t
<is_integral_v
<IntegerType
>, byte
>
31 operator<<(byte b
, IntegerType shift
) noexcept
{
32 return static_cast<byte
>(static_cast<unsigned char>(b
) << shift
);
34 template <class IntegerType
>
35 LIBC_INLINE
constexpr enable_if_t
<is_integral_v
<IntegerType
>, byte
&>
36 operator<<=(byte
&b
, IntegerType shift
) noexcept
{
37 return b
= b
<< shift
;
39 LIBC_INLINE
constexpr byte
operator|(byte l
, byte r
) noexcept
{
40 return static_cast<byte
>(static_cast<unsigned char>(l
) |
41 static_cast<unsigned char>(r
));
43 LIBC_INLINE
constexpr byte
&operator|=(byte
&l
, byte r
) noexcept
{
46 LIBC_INLINE
constexpr byte
operator&(byte l
, byte r
) noexcept
{
47 return static_cast<byte
>(static_cast<unsigned char>(l
) &
48 static_cast<unsigned char>(r
));
50 LIBC_INLINE
constexpr byte
&operator&=(byte
&l
, byte r
) noexcept
{
53 LIBC_INLINE
constexpr byte
operator^(byte l
, byte r
) noexcept
{
54 return static_cast<byte
>(static_cast<unsigned char>(l
) ^
55 static_cast<unsigned char>(r
));
57 LIBC_INLINE
constexpr byte
&operator^=(byte
&l
, byte r
) noexcept
{
60 LIBC_INLINE
constexpr byte
operator~(byte b
) noexcept
{
61 return static_cast<byte
>(~static_cast<unsigned char>(b
));
63 template <typename IntegerType
>
64 LIBC_INLINE
constexpr enable_if_t
<is_integral_v
<IntegerType
>, IntegerType
>
65 to_integer(byte b
) noexcept
{
66 return static_cast<IntegerType
>(b
);
69 } // namespace LIBC_NAMESPACE::cpp
71 #endif // LLVM_LIBC_SRC___SUPPORT_CPP_CSTDDEF_H