[libc-commits] [libc] d02525c - [libc] Add cpp::byte

Guillaume Chatelet via libc-commits libc-commits at lists.llvm.org
Sat Oct 22 13:54:27 PDT 2022


Author: Guillaume Chatelet
Date: 2022-10-22T20:54:14Z
New Revision: d02525cab75161fceb2a4ecb1dd0a5cbac4856ed

URL: https://github.com/llvm/llvm-project/commit/d02525cab75161fceb2a4ecb1dd0a5cbac4856ed
DIFF: https://github.com/llvm/llvm-project/commit/d02525cab75161fceb2a4ecb1dd0a5cbac4856ed.diff

LOG: [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

Added: 
    libc/src/__support/CPP/cstddef.h
    libc/test/src/__support/CPP/cstddef_test.cpp

Modified: 
    libc/src/__support/CPP/CMakeLists.txt
    libc/test/src/__support/CPP/CMakeLists.txt
    utils/bazel/llvm-project-overlay/libc/BUILD.bazel

Removed: 
    


################################################################################
diff  --git a/libc/src/__support/CPP/CMakeLists.txt b/libc/src/__support/CPP/CMakeLists.txt
index dac4aff618d1a..cb24b46e69695 100644
--- a/libc/src/__support/CPP/CMakeLists.txt
+++ b/libc/src/__support/CPP/CMakeLists.txt
@@ -16,6 +16,14 @@ add_header_library(
     bitset.h
 )
 
+add_header_library(
+  cstddef
+  HDRS
+    cstddef.h
+  DEPENDS
+    .type_traits
+)
+
 add_header_library(
   functional
   HDRS

diff  --git a/libc/src/__support/CPP/cstddef.h b/libc/src/__support/CPP/cstddef.h
new file mode 100644
index 0000000000000..3a9dcea68b287
--- /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 <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

diff  --git a/libc/test/src/__support/CPP/CMakeLists.txt b/libc/test/src/__support/CPP/CMakeLists.txt
index f5298b45c4fb8..99ad849395251 100644
--- a/libc/test/src/__support/CPP/CMakeLists.txt
+++ b/libc/test/src/__support/CPP/CMakeLists.txt
@@ -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

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 0000000000000..35d6657c2b962
--- /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<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

diff  --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index 6f418428aa43e..250660f008ab9 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -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"],


        


More information about the libc-commits mailing list