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 "type_traits.h" // For enable_if_t, is_integral_v.
14
15 namespace LIBC_NAMESPACE::cpp {
16
17 enum class byte : unsigned char {};
18
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);
23 }
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;
28 }
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);
33 }
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;
38 }
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));
42 }
43 LIBC_INLINE constexpr byte &operator|=(byte &l, byte r) noexcept {
44 return l = l | r;
45 }
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));
49 }
50 LIBC_INLINE constexpr byte &operator&=(byte &l, byte r) noexcept {
51 return l = l & r;
52 }
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));
56 }
57 LIBC_INLINE constexpr byte &operator^=(byte &l, byte r) noexcept {
58 return l = l ^ r;
59 }
60 LIBC_INLINE constexpr byte operator~(byte b) noexcept {
61 return static_cast<byte>(~static_cast<unsigned char>(b));
62 }
63 template <typename IntegerType>
64 LIBC_INLINE constexpr enable_if_t<is_integral_v<IntegerType>, IntegerType>
to_integer(byte b)65 to_integer(byte b) noexcept {
66 return static_cast<IntegerType>(b);
67 }
68
69 } // namespace LIBC_NAMESPACE::cpp
70
71 #endif // LLVM_LIBC_SRC___SUPPORT_CPP_CSTDDEF_H
72