xref: /freebsd/contrib/llvm-project/libc/src/__support/HashTable/bitmask.h (revision bb722a7d0f1642bff6487f943ad0427799a6e5bf)
1*bb722a7dSDimitry Andric //===-- HashTable BitMasks --------------------------------------*- C++ -*-===//
2*bb722a7dSDimitry Andric //
3*bb722a7dSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*bb722a7dSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*bb722a7dSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*bb722a7dSDimitry Andric //
7*bb722a7dSDimitry Andric //===----------------------------------------------------------------------===//
8*bb722a7dSDimitry Andric 
9*bb722a7dSDimitry Andric #ifndef LLVM_LIBC_SRC___SUPPORT_HASHTABLE_BITMASK_H
10*bb722a7dSDimitry Andric #define LLVM_LIBC_SRC___SUPPORT_HASHTABLE_BITMASK_H
11*bb722a7dSDimitry Andric 
12*bb722a7dSDimitry Andric #include "src/__support/CPP/bit.h"
13*bb722a7dSDimitry Andric #include "src/__support/macros/config.h"
14*bb722a7dSDimitry Andric #include "src/__support/macros/properties/cpu_features.h"
15*bb722a7dSDimitry Andric #include <stddef.h> // size_t
16*bb722a7dSDimitry Andric #include <stdint.h> // uint8_t, uint64_t
17*bb722a7dSDimitry Andric 
18*bb722a7dSDimitry Andric namespace LIBC_NAMESPACE_DECL {
19*bb722a7dSDimitry Andric namespace internal {
20*bb722a7dSDimitry Andric 
21*bb722a7dSDimitry Andric // Implementations of the bitmask.
22*bb722a7dSDimitry Andric // The backend word type may vary depending on different microarchitectures.
23*bb722a7dSDimitry Andric // For example, with X86 SSE2, the bitmask is just the 16bit unsigned integer
24*bb722a7dSDimitry Andric // corresponding to lanes in a SIMD register.
25*bb722a7dSDimitry Andric //
26*bb722a7dSDimitry Andric // Notice that this implementation is simplified from traditional swisstable:
27*bb722a7dSDimitry Andric // since we do not support deletion, we only need to care about if the highest
28*bb722a7dSDimitry Andric // bit is set or not:
29*bb722a7dSDimitry Andric // =============================
30*bb722a7dSDimitry Andric // | Slot Status |   Bitmask   |
31*bb722a7dSDimitry Andric // =============================
32*bb722a7dSDimitry Andric // |  Available  | 0b1xxx'xxxx |
33*bb722a7dSDimitry Andric // |  Occupied   | 0b0xxx'xxxx |
34*bb722a7dSDimitry Andric // =============================
35*bb722a7dSDimitry Andric template <typename T, size_t WORD_STRIDE> struct BitMaskAdaptor {
36*bb722a7dSDimitry Andric   // A stride in the bitmask may use multiple bits.
37*bb722a7dSDimitry Andric   LIBC_INLINE_VAR constexpr static size_t STRIDE = WORD_STRIDE;
38*bb722a7dSDimitry Andric 
39*bb722a7dSDimitry Andric   T word;
40*bb722a7dSDimitry Andric 
41*bb722a7dSDimitry Andric   // Check if any bit is set inside the word.
any_bit_setBitMaskAdaptor42*bb722a7dSDimitry Andric   LIBC_INLINE constexpr bool any_bit_set() const { return word != 0; }
43*bb722a7dSDimitry Andric 
44*bb722a7dSDimitry Andric   // Count trailing zeros with respect to stride. (Assume the bitmask is none
45*bb722a7dSDimitry Andric   // zero.)
lowest_set_bit_nonzeroBitMaskAdaptor46*bb722a7dSDimitry Andric   LIBC_INLINE constexpr size_t lowest_set_bit_nonzero() const {
47*bb722a7dSDimitry Andric     return cpp::countr_zero<T>(word) / WORD_STRIDE;
48*bb722a7dSDimitry Andric   }
49*bb722a7dSDimitry Andric };
50*bb722a7dSDimitry Andric 
51*bb722a7dSDimitry Andric // Not all bitmasks are iterable --- only those who has only MSB set in each
52*bb722a7dSDimitry Andric // lane. Hence, we make the types nomially different to distinguish them.
53*bb722a7dSDimitry Andric template <class BitMask> struct IteratableBitMaskAdaptor : public BitMask {
54*bb722a7dSDimitry Andric   // Use the bitmask as an iterator. Update the state and return current lowest
55*bb722a7dSDimitry Andric   // set bit. To make the bitmask iterable, each stride must contain 0 or exact
56*bb722a7dSDimitry Andric   // 1 set bit.
remove_lowest_bitIteratableBitMaskAdaptor57*bb722a7dSDimitry Andric   LIBC_INLINE void remove_lowest_bit() {
58*bb722a7dSDimitry Andric     // Remove the last set bit inside the word:
59*bb722a7dSDimitry Andric     //    word              = 011110100 (original value)
60*bb722a7dSDimitry Andric     //    word - 1          = 011110011 (invert all bits up to the last set bit)
61*bb722a7dSDimitry Andric     //    word & (word - 1) = 011110000 (value with the last bit cleared)
62*bb722a7dSDimitry Andric     this->word = this->word & (this->word - 1);
63*bb722a7dSDimitry Andric   }
64*bb722a7dSDimitry Andric   using value_type = size_t;
65*bb722a7dSDimitry Andric   using iterator = BitMask;
66*bb722a7dSDimitry Andric   using const_iterator = BitMask;
67*bb722a7dSDimitry Andric   LIBC_INLINE size_t operator*() const {
68*bb722a7dSDimitry Andric     return this->lowest_set_bit_nonzero();
69*bb722a7dSDimitry Andric   }
70*bb722a7dSDimitry Andric   LIBC_INLINE IteratableBitMaskAdaptor &operator++() {
71*bb722a7dSDimitry Andric     this->remove_lowest_bit();
72*bb722a7dSDimitry Andric     return *this;
73*bb722a7dSDimitry Andric   }
beginIteratableBitMaskAdaptor74*bb722a7dSDimitry Andric   LIBC_INLINE IteratableBitMaskAdaptor begin() { return *this; }
endIteratableBitMaskAdaptor75*bb722a7dSDimitry Andric   LIBC_INLINE IteratableBitMaskAdaptor end() { return {BitMask{0}}; }
76*bb722a7dSDimitry Andric   LIBC_INLINE bool operator==(const IteratableBitMaskAdaptor &other) {
77*bb722a7dSDimitry Andric     return this->word == other.word;
78*bb722a7dSDimitry Andric   }
79*bb722a7dSDimitry Andric   LIBC_INLINE bool operator!=(const IteratableBitMaskAdaptor &other) {
80*bb722a7dSDimitry Andric     return this->word != other.word;
81*bb722a7dSDimitry Andric   }
82*bb722a7dSDimitry Andric };
83*bb722a7dSDimitry Andric 
84*bb722a7dSDimitry Andric } // namespace internal
85*bb722a7dSDimitry Andric } // namespace LIBC_NAMESPACE_DECL
86*bb722a7dSDimitry Andric 
87*bb722a7dSDimitry Andric #if defined(LIBC_TARGET_CPU_HAS_SSE2) && defined(__LIBC_EXPLICIT_SIMD_OPT)
88*bb722a7dSDimitry Andric #include "sse2/bitmask_impl.inc"
89*bb722a7dSDimitry Andric #else
90*bb722a7dSDimitry Andric #include "generic/bitmask_impl.inc"
91*bb722a7dSDimitry Andric #endif
92*bb722a7dSDimitry Andric 
93*bb722a7dSDimitry Andric #endif // LLVM_LIBC_SRC___SUPPORT_HASHTABLE_BITMASK_H
94