|
| 1 | +/*******************************<GINKGO LICENSE>****************************** |
| 2 | +Copyright (c) 2017-2021, the Ginkgo authors |
| 3 | +All rights reserved. |
| 4 | +
|
| 5 | +Redistribution and use in source and binary forms, with or without |
| 6 | +modification, are permitted provided that the following conditions |
| 7 | +are met: |
| 8 | +
|
| 9 | +1. Redistributions of source code must retain the above copyright |
| 10 | +notice, this list of conditions and the following disclaimer. |
| 11 | +
|
| 12 | +2. Redistributions in binary form must reproduce the above copyright |
| 13 | +notice, this list of conditions and the following disclaimer in the |
| 14 | +documentation and/or other materials provided with the distribution. |
| 15 | +
|
| 16 | +3. Neither the name of the copyright holder nor the names of its |
| 17 | +contributors may be used to endorse or promote products derived from |
| 18 | +this software without specific prior written permission. |
| 19 | +
|
| 20 | +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
| 21 | +IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
| 22 | +TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A |
| 23 | +PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | +HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | +******************************<GINKGO LICENSE>*******************************/ |
| 32 | + |
| 33 | +#ifndef GKO_CORE_BASE_TYPES_HPP_ |
| 34 | +#define GKO_CORE_BASE_TYPES_HPP_ |
| 35 | + |
| 36 | + |
| 37 | +#include <array> |
| 38 | +#include <cstdint> |
| 39 | +#include <type_traits> |
| 40 | + |
| 41 | + |
| 42 | +namespace gko { |
| 43 | +namespace detail { |
| 44 | + |
| 45 | + |
| 46 | +/** |
| 47 | + * mask gives the integer with Size activated bits in the end |
| 48 | + * |
| 49 | + * @tparam Size the number of activated bits |
| 50 | + * @tparam ValueType the type of mask, which uses std::uint32_t as default |
| 51 | + * |
| 52 | + * @return the ValueType with Size activated bits in the end |
| 53 | + */ |
| 54 | +template <int Size, typename ValueType = std::uint32_t> |
| 55 | +constexpr std::enable_if_t<(Size < sizeof(ValueType) * 8), ValueType> mask() |
| 56 | +{ |
| 57 | + return (ValueType{1} << Size) - 1; |
| 58 | +} |
| 59 | + |
| 60 | +/** |
| 61 | + * @copydoc mask() |
| 62 | + * |
| 63 | + * @note this is special case for the Size = the number of bits of ValueType |
| 64 | + */ |
| 65 | +template <int Size, typename ValueType = std::uint32_t> |
| 66 | +constexpr std::enable_if_t<Size == sizeof(ValueType) * 8, ValueType> mask() |
| 67 | +{ |
| 68 | + return ~ValueType{}; |
| 69 | +} |
| 70 | + |
| 71 | + |
| 72 | +/** |
| 73 | + * shift calculates the number of bits for shifting |
| 74 | + * |
| 75 | + * @tparam current_shift the current position of shifting |
| 76 | + * @tparam num_groups the number of elements in array |
| 77 | + * |
| 78 | + * @return the number of shifting bits |
| 79 | + * |
| 80 | + * @note this is the last case of nested template |
| 81 | + */ |
| 82 | +template <int current_shift, int num_groups> |
| 83 | +constexpr std::enable_if_t<(num_groups == current_shift + 1), int> shift( |
| 84 | + const std::array<unsigned char, num_groups> &bits) |
| 85 | +{ |
| 86 | + return 0; |
| 87 | +} |
| 88 | + |
| 89 | +/** |
| 90 | + * @copydoc shift(const std::array<char, num_groups>) |
| 91 | + * |
| 92 | + * @note this is the usual case of nested template |
| 93 | + */ |
| 94 | +template <int current_shift, int num_groups> |
| 95 | +constexpr std::enable_if_t<(num_groups > current_shift + 1), int> shift( |
| 96 | + const std::array<unsigned char, num_groups> &bits) |
| 97 | +{ |
| 98 | + return bits[current_shift + 1] + |
| 99 | + shift<(current_shift + 1), num_groups>(bits); |
| 100 | +} |
| 101 | + |
| 102 | + |
| 103 | +} // namespace detail |
| 104 | + |
| 105 | + |
| 106 | +/** |
| 107 | + * ConfigSet is a way to embed several information into one integer by given |
| 108 | + * certain bits. |
| 109 | + * |
| 110 | + * The usage will be the following |
| 111 | + * Set the method with bits Cfg = ConfigSet<b_0, b_1, ..., b_k> |
| 112 | + * Encode the given infomation encoded = Cfg::encode(x_0, x_1, ..., x_k) |
| 113 | + * Decode the specific position information x_t = Cfg::decode<t>(encoded) |
| 114 | + * The encoded result will use 32 bits to record |
| 115 | + * rrrrr0..01....1...k..k, which 1/2/.../k means the bits store the information |
| 116 | + * for 1/2/.../k position and r is for rest of unused bits. |
| 117 | + * |
| 118 | + * Denote $B_t = \sum_{i = t+1}^k b_i$ and $F(X) = Cfg::encode(x_0, ..., x_k)$. |
| 119 | + * Have $F(X) = \sum_{i = 0}^k (x_i << B_i) = \sum_{i = 0}^k (x_i * 2^{B_i})$. |
| 120 | + * For all i, we have $0 <= x_i < 2^{b_i}$. |
| 121 | + * $x_i$, $2^{B_i}$ are non-negative, so |
| 122 | + * $F(X) = 0$ <=> $X = \{0\}$, $x_i = 0$ for all i. |
| 123 | + * Assume $F(X) = F(Y)$, then |
| 124 | + * $0 = |F(X) - F(Y)| = |F(X-Y)| = F(|X - Y|)$. |
| 125 | + * $|x_i - y_i|$ is still in the same range $0 <= |x_i - y_i| < 2^{b_i}$. |
| 126 | + * Thus, $F(|X - Y|) = 0$ -> $|X - Y| = \{0\}$, $x_i - y_i = 0$ -> $X = Y$. |
| 127 | + * F is one-to-one function if $0 <= x_i < 2^{b_i}$ for all i. |
| 128 | + * For any encoded result R, we can use the following to get the decoded series. |
| 129 | + * for i = k to 0; |
| 130 | + * $x_i = R % b_i$; |
| 131 | + * $R = R / bi$; |
| 132 | + * endfor; |
| 133 | + * For any R in the range $[0, 2^{B_0})$, we have X such that $F(X) = R$. |
| 134 | + * F is onto function. |
| 135 | + * Thus, F is bijection. |
| 136 | + * |
| 137 | + * @tparam num_bits... the number of bits for each position. |
| 138 | + * |
| 139 | + * @note the num_bit is required at least $ceil(log_2(maxval) + 1)$ |
| 140 | + */ |
| 141 | +template <unsigned char... num_bits> |
| 142 | +class ConfigSet { |
| 143 | +public: |
| 144 | + static constexpr unsigned num_groups = sizeof...(num_bits); |
| 145 | + static constexpr std::array<unsigned char, num_groups> bits{num_bits...}; |
| 146 | + |
| 147 | + /** |
| 148 | + * Decodes the `position` information from encoded |
| 149 | + * |
| 150 | + * @tparam position the position of desired information |
| 151 | + * |
| 152 | + * @param encoded the encoded integer |
| 153 | + * |
| 154 | + * @return the decoded information at position |
| 155 | + */ |
| 156 | + template <int position> |
| 157 | + static constexpr std::uint32_t decode(std::uint32_t encoded) |
| 158 | + { |
| 159 | + static_assert(position < num_groups, |
| 160 | + "This position is over the bounds."); |
| 161 | + constexpr int shift = detail::shift<position, num_groups>(bits); |
| 162 | + constexpr auto mask = detail::mask<bits[position]>(); |
| 163 | + return (encoded >> shift) & mask; |
| 164 | + } |
| 165 | + |
| 166 | + /** |
| 167 | + * Encodes the information with given bit set to encoded integer. |
| 168 | + * |
| 169 | + * @note the last case of nested template. |
| 170 | + */ |
| 171 | + template <unsigned current_iter> |
| 172 | + static constexpr std::enable_if_t<(current_iter == num_groups), |
| 173 | + std::uint32_t> |
| 174 | + encode() |
| 175 | + { |
| 176 | + return 0; |
| 177 | + } |
| 178 | + |
| 179 | + /** |
| 180 | + * Encodes the information with given bit set to encoded integer. |
| 181 | + * |
| 182 | + * @tparam current_iter the encoded place |
| 183 | + * @tparam Rest... the rest type |
| 184 | + * |
| 185 | + * @param first the current encoded information |
| 186 | + * @param rest... the rest of other information waiting for encoding |
| 187 | + * |
| 188 | + * @return the encoded integer |
| 189 | + */ |
| 190 | + template <unsigned current_iter = 0, typename... Rest> |
| 191 | + static constexpr std::enable_if_t<(current_iter < num_groups), |
| 192 | + std::uint32_t> |
| 193 | + encode(std::uint32_t first, Rest &&... rest) |
| 194 | + { |
| 195 | + constexpr int shift = detail::shift<current_iter, num_groups>(bits); |
| 196 | + if (current_iter == 0) { |
| 197 | + static_assert( |
| 198 | + bits[current_iter] + shift <= sizeof(std::uint32_t) * 8, |
| 199 | + "the total bits usage is larger than std::uint32_t bits"); |
| 200 | + } |
| 201 | + return (first << shift) | |
| 202 | + encode<current_iter + 1>(std::forward<Rest>(rest)...); |
| 203 | + } |
| 204 | +}; |
| 205 | + |
| 206 | + |
| 207 | +} // namespace gko |
| 208 | + |
| 209 | +#endif // GKO_CORE_BASE_TYPES_HPP_ |
0 commit comments