[libc] Add cpp::byte

This provides the equivalent of std::byte.
std::byte is a distinct type that implements the concept of byte as specified in the C++ language definition.
https://en.cppreference.com/w/cpp/types/byte

Differential Revision: https://reviews.llvm.org/D136294
This commit is contained in:
Guillaume Chatelet 2022-10-19 21:40:40 +00:00
parent 446981bdb6
commit d02525cab7
5 changed files with 135 additions and 0 deletions

View File

@ -16,6 +16,14 @@ add_header_library(
bitset.h
)
add_header_library(
cstddef
HDRS
cstddef.h
DEPENDS
.type_traits
)
add_header_library(
functional
HDRS

View File

@ -0,0 +1,64 @@
//===-- A self contained equivalent of cstddef ------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_LIBC_SRC_SUPPORT_CPP_BYTE_H
#define LLVM_LIBC_SRC_SUPPORT_CPP_BYTE_H
#include "type_traits.h" // For enable_if_t, is_integral_v.
namespace __llvm_libc::cpp {
enum class byte : unsigned char {};
template <class IntegerType>
constexpr enable_if_t<is_integral_v<IntegerType>, byte>
operator>>(byte b, IntegerType shift) noexcept {
return static_cast<byte>(static_cast<unsigned char>(b) >> shift);
}
template <class IntegerType>
constexpr enable_if_t<is_integral_v<IntegerType>, byte &>
operator>>=(byte &b, IntegerType shift) noexcept {
return b = b >> shift;
}
template <class IntegerType>
constexpr enable_if_t<is_integral_v<IntegerType>, byte>
operator<<(byte b, IntegerType shift) noexcept {
return static_cast<byte>(static_cast<unsigned char>(b) << shift);
}
template <class IntegerType>
constexpr enable_if_t<is_integral_v<IntegerType>, byte &>
operator<<=(byte &b, IntegerType shift) noexcept {
return b = b << shift;
}
constexpr byte operator|(byte l, byte r) noexcept {
return static_cast<byte>(static_cast<unsigned char>(l) |
static_cast<unsigned char>(r));
}
constexpr byte &operator|=(byte &l, byte r) noexcept { return l = l | r; }
constexpr byte operator&(byte l, byte r) noexcept {
return static_cast<byte>(static_cast<unsigned char>(l) &
static_cast<unsigned char>(r));
}
constexpr byte &operator&=(byte &l, byte r) noexcept { return l = l & r; }
constexpr byte operator^(byte l, byte r) noexcept {
return static_cast<byte>(static_cast<unsigned char>(l) ^
static_cast<unsigned char>(r));
}
constexpr byte &operator^=(byte &l, byte r) noexcept { return l = l ^ r; }
constexpr byte operator~(byte b) noexcept {
return static_cast<byte>(~static_cast<unsigned char>(b));
}
template <typename IntegerType>
constexpr enable_if_t<is_integral_v<IntegerType>, IntegerType>
to_integer(byte b) noexcept {
return static_cast<IntegerType>(b);
}
} // namespace __llvm_libc::cpp
#endif // LLVM_LIBC_SRC_SUPPORT_CPP_BYTE_H

View File

@ -10,6 +10,16 @@ add_libc_unittest(
libc.src.__support.CPP.bitset
)
add_libc_unittest(
cstddef_test
SUITE
libc_cpp_utils_unittests
SRCS
cstddef_test.cpp
DEPENDS
libc.src.__support.CPP.cstddef
)
add_libc_unittest(
stringview_test
SUITE

View File

@ -0,0 +1,44 @@
//===-- Unittests for byte ------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "src/__support/CPP/cstddef.h"
#include "utils/UnitTest/Test.h"
namespace __llvm_libc::cpp {
TEST(LlvmLibcByteTest, to_integer) {
const char str[] = "abc";
const byte *const ptr = reinterpret_cast<const byte *>(str);
ASSERT_EQ(to_integer<char>(ptr[0]), 'a');
ASSERT_EQ(to_integer<char>(ptr[1]), 'b');
ASSERT_EQ(to_integer<char>(ptr[2]), 'c');
ASSERT_EQ(to_integer<char>(ptr[3]), '\0');
}
TEST(LlvmLibcByteTest, bitwise) {
byte b{42};
ASSERT_EQ(b, byte{0b00101010});
b <<= 1;
ASSERT_EQ(b, byte{0b01010100});
b >>= 1;
ASSERT_EQ((b << 1), byte{0b01010100});
ASSERT_EQ((b >> 1), byte{0b00010101});
b |= byte{0b11110000};
ASSERT_EQ(b, byte{0b11111010});
b &= byte{0b11110000};
ASSERT_EQ(b, byte{0b11110000});
b ^= byte{0b11111111};
ASSERT_EQ(b, byte{0b00001111});
}
} // namespace __llvm_libc::cpp

View File

@ -54,6 +54,15 @@ cc_library(
deps = [":libc_root"],
)
cc_library(
name = "__support_cpp_cstddef",
hdrs = ["src/__support/CPP/cstddef.h"],
deps = [
"__support_cpp_type_traits",
":libc_root",
],
)
cc_library(
name = "__support_cpp_functional",
hdrs = ["src/__support/CPP/functional.h"],