|
| 1 | +#ifndef ZOO_SWAR_SWARWITHSUBLANES_H |
| 2 | +#define ZOO_SWAR_SWARWITHSUBLANES_H |
| 3 | + |
| 4 | +#include "zoo/swar/SWAR.h" |
| 5 | + |
| 6 | +namespace zoo { namespace swar { |
| 7 | + |
| 8 | +/// \brief Allows SWAR Lanes to be treated both as a whole or something with |
| 9 | +/// internal structure. |
| 10 | + |
| 11 | +/// Example: Robin Hood "Haystack" metadata composed of hoisted hash bits and |
| 12 | +/// PSL (probe sequence lengths), that are used together or separately. |
| 13 | +/// SWAR is a useful abstraction for performing computations in lanes overlaid |
| 14 | +/// over any given integral type. |
| 15 | +/// To prevent the normal integer operations in a lane to disrrupt the operation |
| 16 | +/// in the adjoining lanes, some precautions must be maintained. For example |
| 17 | +/// upon an addition of lanes, we either need that the domain of our values |
| 18 | +/// does not use the most significant bit (guaranteeing normal addition of |
| 19 | +/// lanes won't cross to the upper lane) or that this possibility is explicitly |
| 20 | +/// taken into account (see "full addition"). This applies to all operations, |
| 21 | +/// including comparisons. |
| 22 | +/// Similarly, doing multiplications via SWAR techniques require double bits per |
| 23 | +/// lane (unless you can guarantee the values of the input lanes are half lane |
| 24 | +/// size). |
| 25 | +/// This leads to a useful technique (which we use in the Robin Hood table) |
| 26 | +/// where we interleave two related small bit count integers inside of a lane of |
| 27 | +/// swar. More generally, this is useful because it sometimes allows fast |
| 28 | +/// operations on side "a" of some lane if side "b" is blitted out, and vice |
| 29 | +/// versa. In the spirit of separation of concerns, we provide a cut-lane-SWAR |
| 30 | +/// abstraction here. |
| 31 | +template<int NBitsLeast_, int NBitsMost_, typename T = uint64_t> |
| 32 | +struct SWARWithSubLanes: SWAR<NBitsLeast_ + NBitsMost_ , T> { |
| 33 | + static constexpr inline auto NBitsLeast = NBitsLeast_; |
| 34 | + static constexpr inline auto NBitsMost = NBitsMost_; |
| 35 | + |
| 36 | + using Base = SWAR<NBitsMost + NBitsLeast, T>; |
| 37 | + static constexpr inline auto Available = sizeof(T); |
| 38 | + static constexpr inline auto LaneBits = NBitsLeast + NBitsMost; |
| 39 | + |
| 40 | + using Base::Base; |
| 41 | + constexpr SWARWithSubLanes(Base b) noexcept: Base(b) {} |
| 42 | + constexpr SWARWithSubLanes(T most, T least) noexcept: |
| 43 | + Base((most << NBitsLeast) | least) |
| 44 | + {} |
| 45 | + |
| 46 | + // M is most significant bits slice, L is least significant bits slice. |
| 47 | + // 0x....M2L2M1L1 or MN|LN||...||M2|L2||M1|L1 |
| 48 | + using SL = SWARWithSubLanes<NBitsLeast, NBitsMost, T>; |
| 49 | + |
| 50 | + static constexpr inline auto LeastOnes = |
| 51 | + Base(meta::BitmaskMaker<T, Base{1}.value(), LaneBits>::value); |
| 52 | + static constexpr inline auto MostOnes = |
| 53 | + Base(LeastOnes.value() << NBitsLeast); |
| 54 | + static constexpr inline auto LeastMask = MostOnes - LeastOnes; |
| 55 | + static constexpr inline auto MostMask = ~LeastMask; |
| 56 | + |
| 57 | + constexpr auto least() const noexcept { |
| 58 | + return SL{LeastMask & *this}; |
| 59 | + } |
| 60 | + |
| 61 | + // Isolate the least significant bits of the lane at the specified position. |
| 62 | + constexpr auto least(int pos) const noexcept { |
| 63 | + constexpr auto Filter = SL((T(1) << NBitsLeast) - 1); |
| 64 | + return Filter.shiftLanesLeft(pos) & *this; |
| 65 | + } |
| 66 | + |
| 67 | + // Returns only the least significant bits at specified position, 'decoded' to their integer value. |
| 68 | + constexpr auto leastFlat(int pos) const noexcept { |
| 69 | + return least().at(pos); |
| 70 | + } |
| 71 | + |
| 72 | + constexpr auto most() const noexcept { |
| 73 | + return SL{MostMask & *this}; |
| 74 | + } |
| 75 | + |
| 76 | + // The most significant bits of the lane at the specified position. |
| 77 | + constexpr auto most(int pos) const noexcept { |
| 78 | + constexpr auto Filter = |
| 79 | + SL(((T(1) << SL::NBitsMost) - 1) << SL::NBitsLeast); |
| 80 | + return Filter.shiftLanesLeft(pos) & *this; |
| 81 | + } |
| 82 | + |
| 83 | + // The most significant bits of the lane at the specified position, |
| 84 | + // 'decoded' to their integer value. |
| 85 | + constexpr auto mostFlat(int pos) const noexcept { |
| 86 | + return most().at(pos) >> SL::NBitsLeast; |
| 87 | + } |
| 88 | + |
| 89 | + // Blits most sig bits into least significant bits. Experimental. |
| 90 | + constexpr auto flattenMostToLeast(int pos) const noexcept { |
| 91 | + return SL(this->m_v >> NBitsLeast) & LeastMask; |
| 92 | + } |
| 93 | + |
| 94 | + // Blits least sig bits into most significant bits. Experimental. |
| 95 | + constexpr auto promoteLeastToMost(int pos) const noexcept { |
| 96 | + return SL(this->m_v << NBitsMost) & MostMask; |
| 97 | + } |
| 98 | + |
| 99 | + // Sets the lsb sublane at |pos| with least significant NBitsLeast of |in| |
| 100 | + constexpr auto least(T in, int pos) const noexcept { |
| 101 | + constexpr auto filter = (T(1) << LaneBits) - 1; |
| 102 | + const auto keep = ~(filter << (LaneBits * pos)) | MostMask.value(); |
| 103 | + const auto rdyToInsert = this->m_v & keep; |
| 104 | + const auto rval = rdyToInsert | ((in & LeastMask.value()) << (LaneBits * pos)); |
| 105 | + return SL(rval); |
| 106 | + } |
| 107 | + |
| 108 | + // Sets the msb sublane at |pos| with least significant NBitsMost of |in| |
| 109 | + constexpr auto most(T in, int pos) const noexcept { |
| 110 | + constexpr auto filter = (T(1) << LaneBits) - 1; |
| 111 | + const auto keep = ~(filter << (LaneBits * pos)) | LeastMask.value(); |
| 112 | + const auto rdyToInsert = this->m_v & keep; |
| 113 | + const auto insVal = (((in<<NBitsLeast) & MostMask.value()) << (LaneBits * pos)); |
| 114 | + const auto rval = rdyToInsert | insVal; |
| 115 | + return SL(rval); |
| 116 | + } |
| 117 | +}; |
| 118 | + |
| 119 | +}} |
| 120 | + |
| 121 | +#endif |
0 commit comments