From f3b41502554f2948ad00531dde7c3f53973de960 Mon Sep 17 00:00:00 2001 From: cgyurgyik Date: Wed, 5 Aug 2020 16:36:52 -0400 Subject: [libc] Add strspn implementation and std::bitset Reviewed By: sivachandra, abrachet Differential Revision: https://reviews.llvm.org/D85103 --- libc/utils/CPP/Bitset.h | 39 +++++++++++++++++++++++++++++++++++++++ libc/utils/CPP/CMakeLists.txt | 1 + 2 files changed, 40 insertions(+) create mode 100644 libc/utils/CPP/Bitset.h (limited to 'libc/utils') 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 // For size_t. +#include // For uintptr_t. + +namespace __llvm_libc { +namespace cpp { + +template 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 -- cgit v1.1