Path: blob/main/contrib/llvm-project/libc/src/__support/HashTable/bitmask.h
213799 views
//===-- HashTable BitMasks --------------------------------------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#ifndef LLVM_LIBC_SRC___SUPPORT_HASHTABLE_BITMASK_H9#define LLVM_LIBC_SRC___SUPPORT_HASHTABLE_BITMASK_H1011#include "src/__support/CPP/bit.h"12#include "src/__support/macros/config.h"13#include "src/__support/macros/properties/cpu_features.h"14#include <stddef.h> // size_t15#include <stdint.h> // uint8_t, uint64_t1617namespace LIBC_NAMESPACE_DECL {18namespace internal {1920// Implementations of the bitmask.21// The backend word type may vary depending on different microarchitectures.22// For example, with X86 SSE2, the bitmask is just the 16bit unsigned integer23// corresponding to lanes in a SIMD register.24//25// Notice that this implementation is simplified from traditional swisstable:26// since we do not support deletion, we only need to care about if the highest27// bit is set or not:28// =============================29// | Slot Status | Bitmask |30// =============================31// | Available | 0b1xxx'xxxx |32// | Occupied | 0b0xxx'xxxx |33// =============================34template <typename T, size_t WORD_STRIDE> struct BitMaskAdaptor {35// A stride in the bitmask may use multiple bits.36LIBC_INLINE_VAR constexpr static size_t STRIDE = WORD_STRIDE;3738T word;3940// Check if any bit is set inside the word.41LIBC_INLINE constexpr bool any_bit_set() const { return word != 0; }4243// Count trailing zeros with respect to stride. (Assume the bitmask is none44// zero.)45LIBC_INLINE constexpr size_t lowest_set_bit_nonzero() const {46return cpp::countr_zero<T>(word) / WORD_STRIDE;47}48};4950// Not all bitmasks are iterable --- only those who has only MSB set in each51// lane. Hence, we make the types nomially different to distinguish them.52template <class BitMask> struct IteratableBitMaskAdaptor : public BitMask {53// Use the bitmask as an iterator. Update the state and return current lowest54// set bit. To make the bitmask iterable, each stride must contain 0 or exact55// 1 set bit.56LIBC_INLINE void remove_lowest_bit() {57// Remove the last set bit inside the word:58// word = 011110100 (original value)59// word - 1 = 011110011 (invert all bits up to the last set bit)60// word & (word - 1) = 011110000 (value with the last bit cleared)61this->word = this->word & (this->word - 1);62}63using value_type = size_t;64using iterator = BitMask;65using const_iterator = BitMask;66LIBC_INLINE size_t operator*() const {67return this->lowest_set_bit_nonzero();68}69LIBC_INLINE IteratableBitMaskAdaptor &operator++() {70this->remove_lowest_bit();71return *this;72}73LIBC_INLINE IteratableBitMaskAdaptor begin() { return *this; }74LIBC_INLINE IteratableBitMaskAdaptor end() { return {BitMask{0}}; }75LIBC_INLINE bool operator==(const IteratableBitMaskAdaptor &other) {76return this->word == other.word;77}78LIBC_INLINE bool operator!=(const IteratableBitMaskAdaptor &other) {79return this->word != other.word;80}81};8283} // namespace internal84} // namespace LIBC_NAMESPACE_DECL8586#if defined(LIBC_TARGET_CPU_HAS_SSE2) && defined(__LIBC_EXPLICIT_SIMD_OPT)87#include "sse2/bitmask_impl.inc"88#else89#include "generic/bitmask_impl.inc"90#endif9192#endif // LLVM_LIBC_SRC___SUPPORT_HASHTABLE_BITMASK_H939495