aboutsummaryrefslogtreecommitdiff
path: root/libc/utils
diff options
context:
space:
mode:
authorcgyurgyik <gyurgyikcp@gmail.com>2020-08-05 16:36:52 -0400
committercgyurgyik <gyurgyikcp@gmail.com>2020-08-05 16:48:38 -0400
commitf3b41502554f2948ad00531dde7c3f53973de960 (patch)
treead092e2600416195455a040926b8058daa4bec08 /libc/utils
parent40470eb27a5c97b01e89d8825626487b0682abec (diff)
downloadllvm-f3b41502554f2948ad00531dde7c3f53973de960.zip
llvm-f3b41502554f2948ad00531dde7c3f53973de960.tar.gz
llvm-f3b41502554f2948ad00531dde7c3f53973de960.tar.bz2
[libc] Add strspn implementation and std::bitset
Reviewed By: sivachandra, abrachet Differential Revision: https://reviews.llvm.org/D85103
Diffstat (limited to 'libc/utils')
-rw-r--r--libc/utils/CPP/Bitset.h39
-rw-r--r--libc/utils/CPP/CMakeLists.txt1
2 files changed, 40 insertions, 0 deletions
diff --git a/libc/utils/CPP/Bitset.h b/libc/utils/CPP/Bitset.h
new file mode 100644
index 0000000..304a6fe
--- /dev/null
+++ b/libc/utils/CPP/Bitset.h
@@ -0,0 +1,39 @@
+//===-- A self contained equivalent of std::bitset --------------*- 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_UTILS_CPP_BITSET_H
+#define LLVM_LIBC_UTILS_CPP_BITSET_H
+
+#include <stddef.h> // For size_t.
+#include <stdint.h> // For uintptr_t.
+
+namespace __llvm_libc {
+namespace cpp {
+
+template <size_t NumberOfBits> struct Bitset {
+ static_assert(NumberOfBits != 0,
+ "Cannot create a __llvm_libc::cpp::Bitset of size 0.");
+
+ constexpr void set(size_t Index) {
+ Data[Index / BitsPerUnit] |= (uintptr_t{1} << (Index % BitsPerUnit));
+ }
+
+ constexpr bool test(size_t Index) const {
+ return Data[Index / BitsPerUnit] & (uintptr_t{1} << (Index % BitsPerUnit));
+ }
+
+private:
+ static constexpr size_t BitsPerByte = 8;
+ static constexpr size_t BitsPerUnit = BitsPerByte * sizeof(uintptr_t);
+ uintptr_t Data[(NumberOfBits + BitsPerUnit - 1) / BitsPerUnit] = {0};
+};
+
+} // namespace cpp
+} // namespace __llvm_libc
+
+#endif // LLVM_LIBC_UTILS_CPP_BITSET_H
diff --git a/libc/utils/CPP/CMakeLists.txt b/libc/utils/CPP/CMakeLists.txt
index 4c7f5e9..60975fe 100644
--- a/libc/utils/CPP/CMakeLists.txt
+++ b/libc/utils/CPP/CMakeLists.txt
@@ -3,6 +3,7 @@ add_header_library(
HDRS
Array.h
ArrayRef.h
+ Bitset.h
Functional.h
StringRef.h
TypeTraits.h