Vernon Mauery | e7329c7 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 1 | /** |
| 2 | * Copyright © 2018 Intel Corporation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | #pragma once |
| 17 | |
| 18 | #include <bitset> |
| 19 | #include <boost/multiprecision/cpp_int.hpp> |
| 20 | #include <ipmid/utility.hpp> |
| 21 | #include <tuple> |
| 22 | |
| 23 | // unsigned fixed-bit sizes |
| 24 | template <unsigned N> |
| 25 | using fixed_uint_t = |
| 26 | boost::multiprecision::number<boost::multiprecision::cpp_int_backend< |
| 27 | N, N, boost::multiprecision::unsigned_magnitude, |
| 28 | boost::multiprecision::unchecked, void>>; |
| 29 | // signed fixed-bit sizes |
| 30 | template <unsigned N> |
| 31 | using fixed_int_t = |
| 32 | boost::multiprecision::number<boost::multiprecision::cpp_int_backend< |
| 33 | N, N, boost::multiprecision::signed_magnitude, |
| 34 | boost::multiprecision::unchecked, void>>; |
| 35 | |
| 36 | using uint1_t = fixed_uint_t<1>; |
| 37 | using uint2_t = fixed_uint_t<2>; |
| 38 | using uint3_t = fixed_uint_t<3>; |
| 39 | using uint4_t = fixed_uint_t<4>; |
| 40 | using uint5_t = fixed_uint_t<5>; |
| 41 | using uint6_t = fixed_uint_t<6>; |
| 42 | using uint7_t = fixed_uint_t<7>; |
| 43 | // native uint8_t |
| 44 | using uint9_t = fixed_uint_t<9>; |
| 45 | using uint10_t = fixed_uint_t<10>; |
| 46 | using uint11_t = fixed_uint_t<11>; |
| 47 | using uint12_t = fixed_uint_t<12>; |
| 48 | using uint13_t = fixed_uint_t<13>; |
| 49 | using uint14_t = fixed_uint_t<14>; |
| 50 | using uint15_t = fixed_uint_t<15>; |
| 51 | // native uint16_t |
| 52 | using uint24_t = fixed_uint_t<24>; |
| 53 | |
| 54 | // signed fixed-bit sizes |
| 55 | using int2_t = fixed_int_t<2>; |
| 56 | using int3_t = fixed_int_t<3>; |
| 57 | using int4_t = fixed_int_t<4>; |
| 58 | using int5_t = fixed_int_t<5>; |
| 59 | using int6_t = fixed_int_t<6>; |
| 60 | using int7_t = fixed_int_t<7>; |
| 61 | // native int8_t |
| 62 | using int9_t = fixed_int_t<9>; |
| 63 | using int10_t = fixed_int_t<10>; |
| 64 | using int11_t = fixed_int_t<11>; |
| 65 | using int12_t = fixed_int_t<12>; |
| 66 | using int13_t = fixed_int_t<13>; |
| 67 | using int14_t = fixed_int_t<14>; |
| 68 | using int15_t = fixed_int_t<15>; |
| 69 | // native int16_t |
| 70 | using int24_t = fixed_int_t<24>; |
| 71 | |
| 72 | // bool is more efficient than a uint1_t |
| 73 | using bit = bool; |
| 74 | |
| 75 | // Mechanism for going from uint7_t, int7_t, or std::bitset<7> to 7 bits |
| 76 | // use nrFixedBits<uint7_t> or nrFixedBits<decltype(u7)> |
| 77 | namespace types |
| 78 | { |
| 79 | namespace details |
| 80 | { |
| 81 | |
| 82 | template <size_t N> |
| 83 | struct Size |
| 84 | { |
| 85 | static constexpr size_t value = N; |
| 86 | }; |
| 87 | |
| 88 | template <unsigned Bits> |
| 89 | constexpr auto getNrBits(const fixed_int_t<Bits>&) -> Size<Bits>; |
| 90 | template <unsigned Bits> |
| 91 | constexpr auto getNrBits(const fixed_uint_t<Bits>&) -> Size<Bits>; |
| 92 | template <size_t Bits> |
| 93 | constexpr auto getNrBits(const std::bitset<Bits>&) -> Size<Bits>; |
| 94 | |
William A. Kennington III | 7a0e5df | 2021-05-19 13:31:29 -0700 | [diff] [blame] | 95 | template <typename U> |
| 96 | using underlying_t = |
| 97 | typename std::conditional_t<std::is_enum_v<U>, std::underlying_type<U>, |
| 98 | std::enable_if<true, U>>::type; |
| 99 | |
Vernon Mauery | e7329c7 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 100 | } // namespace details |
| 101 | |
| 102 | /** |
| 103 | * @brief mechanism to get N from a type like fixed_int_t<N> |
| 104 | * |
| 105 | * helper template to extract N from a fixed_(u)int_t variable |
| 106 | * |
| 107 | * @tparam T - a type of fixed_int_t<N> or fixed_unint_t<N> |
| 108 | * |
| 109 | * @return size_t - evaluates to a constexpr size_t of N |
| 110 | */ |
| 111 | template <typename T> |
| 112 | constexpr auto nrFixedBits = |
| 113 | decltype(details::getNrBits(std::declval<T>()))::value; |
| 114 | |
William A. Kennington III | 7a0e5df | 2021-05-19 13:31:29 -0700 | [diff] [blame] | 115 | /** |
| 116 | * @brief Converts a number or enum class to another |
| 117 | * @tparam R - The output type |
| 118 | * @tparam T - The input type |
| 119 | * @param t - An enum or integer value to cast |
| 120 | * @return The value in R form |
| 121 | */ |
| 122 | template <typename R, typename T> |
| 123 | inline R enum_cast(T t) |
| 124 | { |
| 125 | auto tu = static_cast<details::underlying_t<T>>(t); |
| 126 | auto ru = static_cast<details::underlying_t<R>>(tu); |
| 127 | return static_cast<R>(ru); |
| 128 | } |
| 129 | |
Vernon Mauery | e7329c7 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 130 | } // namespace types |