blob: 1da257450b40c88f2d24130c937eaff9420ae2a9 [file] [log] [blame]
Vernon Mauerye7329c72018-10-08 12:05:16 -07001/**
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>
Tim Leeb4905912022-06-18 02:51:13 +080020#include <boost/version.hpp>
Vernon Mauerye7329c72018-10-08 12:05:16 -070021#include <ipmid/utility.hpp>
22#include <tuple>
23
Tim Leeb4905912022-06-18 02:51:13 +080024#if BOOST_VERSION < 107900
25using bitcount_t = unsigned;
26#else
27using bitcount_t = std::size_t;
28#endif
29
Vernon Mauerye7329c72018-10-08 12:05:16 -070030// unsigned fixed-bit sizes
Tim Leeb4905912022-06-18 02:51:13 +080031template <bitcount_t N>
Vernon Mauerye7329c72018-10-08 12:05:16 -070032using fixed_uint_t =
33 boost::multiprecision::number<boost::multiprecision::cpp_int_backend<
34 N, N, boost::multiprecision::unsigned_magnitude,
35 boost::multiprecision::unchecked, void>>;
36// signed fixed-bit sizes
Tim Leeb4905912022-06-18 02:51:13 +080037template <bitcount_t N>
Vernon Mauerye7329c72018-10-08 12:05:16 -070038using fixed_int_t =
39 boost::multiprecision::number<boost::multiprecision::cpp_int_backend<
40 N, N, boost::multiprecision::signed_magnitude,
41 boost::multiprecision::unchecked, void>>;
42
43using uint1_t = fixed_uint_t<1>;
44using uint2_t = fixed_uint_t<2>;
45using uint3_t = fixed_uint_t<3>;
46using uint4_t = fixed_uint_t<4>;
47using uint5_t = fixed_uint_t<5>;
48using uint6_t = fixed_uint_t<6>;
49using uint7_t = fixed_uint_t<7>;
50// native uint8_t
51using uint9_t = fixed_uint_t<9>;
52using uint10_t = fixed_uint_t<10>;
53using uint11_t = fixed_uint_t<11>;
54using uint12_t = fixed_uint_t<12>;
55using uint13_t = fixed_uint_t<13>;
56using uint14_t = fixed_uint_t<14>;
57using uint15_t = fixed_uint_t<15>;
58// native uint16_t
59using uint24_t = fixed_uint_t<24>;
60
61// signed fixed-bit sizes
62using int2_t = fixed_int_t<2>;
63using int3_t = fixed_int_t<3>;
64using int4_t = fixed_int_t<4>;
65using int5_t = fixed_int_t<5>;
66using int6_t = fixed_int_t<6>;
67using int7_t = fixed_int_t<7>;
68// native int8_t
69using int9_t = fixed_int_t<9>;
70using int10_t = fixed_int_t<10>;
71using int11_t = fixed_int_t<11>;
72using int12_t = fixed_int_t<12>;
73using int13_t = fixed_int_t<13>;
74using int14_t = fixed_int_t<14>;
75using int15_t = fixed_int_t<15>;
76// native int16_t
77using int24_t = fixed_int_t<24>;
78
79// bool is more efficient than a uint1_t
80using bit = bool;
81
82// Mechanism for going from uint7_t, int7_t, or std::bitset<7> to 7 bits
83// use nrFixedBits<uint7_t> or nrFixedBits<decltype(u7)>
84namespace types
85{
86namespace details
87{
88
Tim Leeb4905912022-06-18 02:51:13 +080089template <bitcount_t N>
Vernon Mauerye7329c72018-10-08 12:05:16 -070090struct Size
91{
Tim Leeb4905912022-06-18 02:51:13 +080092 static constexpr bitcount_t value = N;
Vernon Mauerye7329c72018-10-08 12:05:16 -070093};
94
Tim Leeb4905912022-06-18 02:51:13 +080095template <bitcount_t Bits>
Vernon Mauerye7329c72018-10-08 12:05:16 -070096constexpr auto getNrBits(const fixed_int_t<Bits>&) -> Size<Bits>;
Tim Leeb4905912022-06-18 02:51:13 +080097template <bitcount_t Bits>
Vernon Mauerye7329c72018-10-08 12:05:16 -070098constexpr auto getNrBits(const fixed_uint_t<Bits>&) -> Size<Bits>;
Tim Leeb4905912022-06-18 02:51:13 +080099template <bitcount_t Bits>
Vernon Mauerye7329c72018-10-08 12:05:16 -0700100constexpr auto getNrBits(const std::bitset<Bits>&) -> Size<Bits>;
101
William A. Kennington III7a0e5df2021-05-19 13:31:29 -0700102template <typename U>
103using underlying_t =
104 typename std::conditional_t<std::is_enum_v<U>, std::underlying_type<U>,
105 std::enable_if<true, U>>::type;
106
Vernon Mauerye7329c72018-10-08 12:05:16 -0700107} // namespace details
108
109/**
110 * @brief mechanism to get N from a type like fixed_int_t<N>
111 *
112 * helper template to extract N from a fixed_(u)int_t variable
113 *
114 * @tparam T - a type of fixed_int_t<N> or fixed_unint_t<N>
115 *
116 * @return size_t - evaluates to a constexpr size_t of N
117 */
118template <typename T>
119constexpr auto nrFixedBits =
120 decltype(details::getNrBits(std::declval<T>()))::value;
121
William A. Kennington III7a0e5df2021-05-19 13:31:29 -0700122/**
123 * @brief Converts a number or enum class to another
124 * @tparam R - The output type
125 * @tparam T - The input type
126 * @param t - An enum or integer value to cast
127 * @return The value in R form
128 */
129template <typename R, typename T>
130inline R enum_cast(T t)
131{
132 auto tu = static_cast<details::underlying_t<T>>(t);
133 auto ru = static_cast<details::underlying_t<R>>(tu);
134 return static_cast<R>(ru);
135}
136
Vernon Mauerye7329c72018-10-08 12:05:16 -0700137} // namespace types