From d02525cab75161fceb2a4ecb1dd0a5cbac4856ed Mon Sep 17 00:00:00 2001 From: Guillaume Chatelet Date: Wed, 19 Oct 2022 21:40:40 +0000 Subject: [PATCH] [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 --- libc/src/__support/CPP/CMakeLists.txt | 8 +++ libc/src/__support/CPP/cstddef.h | 64 +++++++++++++++++++++++ libc/test/src/__support/CPP/CMakeLists.txt | 10 ++++ libc/test/src/__support/CPP/cstddef_test.cpp | 44 ++++++++++++++++ utils/bazel/llvm-project-overlay/libc/BUILD.bazel | 9 ++++ 5 files changed, 135 insertions(+) create mode 100644 libc/src/__support/CPP/cstddef.h create mode 100644 libc/test/src/__support/CPP/cstddef_test.cpp diff --git a/libc/src/__support/CPP/CMakeLists.txt b/libc/src/__support/CPP/CMakeLists.txt index dac4aff..cb24b46 100644 --- a/libc/src/__support/CPP/CMakeLists.txt +++ b/libc/src/__support/CPP/CMakeLists.txt @@ -17,6 +17,14 @@ add_header_library( ) add_header_library( + cstddef + HDRS + cstddef.h + DEPENDS + .type_traits +) + +add_header_library( functional HDRS functional.h diff --git a/libc/src/__support/CPP/cstddef.h b/libc/src/__support/CPP/cstddef.h new file mode 100644 index 0000000..3a9dcea --- /dev/null +++ b/libc/src/__support/CPP/cstddef.h @@ -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 +constexpr enable_if_t, byte> +operator>>(byte b, IntegerType shift) noexcept { + return static_cast(static_cast(b) >> shift); +} +template +constexpr enable_if_t, byte &> +operator>>=(byte &b, IntegerType shift) noexcept { + return b = b >> shift; +} +template +constexpr enable_if_t, byte> +operator<<(byte b, IntegerType shift) noexcept { + return static_cast(static_cast(b) << shift); +} +template +constexpr enable_if_t, byte &> +operator<<=(byte &b, IntegerType shift) noexcept { + return b = b << shift; +} +constexpr byte operator|(byte l, byte r) noexcept { + return static_cast(static_cast(l) | + static_cast(r)); +} +constexpr byte &operator|=(byte &l, byte r) noexcept { return l = l | r; } +constexpr byte operator&(byte l, byte r) noexcept { + return static_cast(static_cast(l) & + static_cast(r)); +} +constexpr byte &operator&=(byte &l, byte r) noexcept { return l = l & r; } +constexpr byte operator^(byte l, byte r) noexcept { + return static_cast(static_cast(l) ^ + static_cast(r)); +} +constexpr byte &operator^=(byte &l, byte r) noexcept { return l = l ^ r; } +constexpr byte operator~(byte b) noexcept { + return static_cast(~static_cast(b)); +} +template +constexpr enable_if_t, IntegerType> +to_integer(byte b) noexcept { + return static_cast(b); +} + +} // namespace __llvm_libc::cpp + +#endif // LLVM_LIBC_SRC_SUPPORT_CPP_BYTE_H diff --git a/libc/test/src/__support/CPP/CMakeLists.txt b/libc/test/src/__support/CPP/CMakeLists.txt index f5298b4..99ad849 100644 --- a/libc/test/src/__support/CPP/CMakeLists.txt +++ b/libc/test/src/__support/CPP/CMakeLists.txt @@ -11,6 +11,16 @@ add_libc_unittest( ) 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 libc_cpp_utils_unittests diff --git a/libc/test/src/__support/CPP/cstddef_test.cpp b/libc/test/src/__support/CPP/cstddef_test.cpp new file mode 100644 index 0000000..35d6657 --- /dev/null +++ b/libc/test/src/__support/CPP/cstddef_test.cpp @@ -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(str); + ASSERT_EQ(to_integer(ptr[0]), 'a'); + ASSERT_EQ(to_integer(ptr[1]), 'b'); + ASSERT_EQ(to_integer(ptr[2]), 'c'); + ASSERT_EQ(to_integer(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 diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel index 6f41842..250660f 100644 --- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel +++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel @@ -55,6 +55,15 @@ cc_library( ) 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"], deps = [":libc_root"], -- 2.7.4