1 //===-- A self contained equivalent of cstddef ------------------*- C++ -*-===//
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 LLVM_LIBC_SRC___SUPPORT_CPP_CSTDDEF_H
10 #define LLVM_LIBC_SRC___SUPPORT_CPP_CSTDDEF_H
11
12 #include "src/__support/macros/attributes.h"
13 #include "src/__support/macros/config.h"
14 #include "type_traits.h" // For enable_if_t, is_integral_v.
15
16 namespace LIBC_NAMESPACE_DECL {
17 namespace cpp {
18
19 enum class byte : unsigned char {};
20
21 template <class IntegerType>
22 LIBC_INLINE constexpr enable_if_t<is_integral_v<IntegerType>, byte>
23 operator>>(byte b, IntegerType shift) noexcept {
24 return static_cast<byte>(static_cast<unsigned char>(b) >> shift);
25 }
26 template <class IntegerType>
27 LIBC_INLINE constexpr enable_if_t<is_integral_v<IntegerType>, byte &>
28 operator>>=(byte &b, IntegerType shift) noexcept {
29 return b = b >> shift;
30 }
31 template <class IntegerType>
32 LIBC_INLINE constexpr enable_if_t<is_integral_v<IntegerType>, byte>
33 operator<<(byte b, IntegerType shift) noexcept {
34 return static_cast<byte>(static_cast<unsigned char>(b) << shift);
35 }
36 template <class IntegerType>
37 LIBC_INLINE constexpr enable_if_t<is_integral_v<IntegerType>, byte &>
38 operator<<=(byte &b, IntegerType shift) noexcept {
39 return b = b << shift;
40 }
41 LIBC_INLINE constexpr byte operator|(byte l, byte r) noexcept {
42 return static_cast<byte>(static_cast<unsigned char>(l) |
43 static_cast<unsigned char>(r));
44 }
45 LIBC_INLINE constexpr byte &operator|=(byte &l, byte r) noexcept {
46 return l = l | r;
47 }
48 LIBC_INLINE constexpr byte operator&(byte l, byte r) noexcept {
49 return static_cast<byte>(static_cast<unsigned char>(l) &
50 static_cast<unsigned char>(r));
51 }
52 LIBC_INLINE constexpr byte &operator&=(byte &l, byte r) noexcept {
53 return l = l & r;
54 }
55 LIBC_INLINE constexpr byte operator^(byte l, byte r) noexcept {
56 return static_cast<byte>(static_cast<unsigned char>(l) ^
57 static_cast<unsigned char>(r));
58 }
59 LIBC_INLINE constexpr byte &operator^=(byte &l, byte r) noexcept {
60 return l = l ^ r;
61 }
62 LIBC_INLINE constexpr byte operator~(byte b) noexcept {
63 return static_cast<byte>(~static_cast<unsigned char>(b));
64 }
65 template <typename IntegerType>
66 LIBC_INLINE constexpr enable_if_t<is_integral_v<IntegerType>, IntegerType>
to_integer(byte b)67 to_integer(byte b) noexcept {
68 return static_cast<IntegerType>(b);
69 }
70
71 } // namespace cpp
72 } // namespace LIBC_NAMESPACE_DECL
73
74 #endif // LLVM_LIBC_SRC___SUPPORT_CPP_CSTDDEF_H
75