1/**
2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#ifndef LIBPANDABASE_UTILS_BIT_HELPERS_H
17#define LIBPANDABASE_UTILS_BIT_HELPERS_H
18
19#include "macros.h"
20
21#include <cstddef>
22#include <cstdint>
23
24#include <limits>
25#include <type_traits>
26
27namespace panda::helpers {
28
29template <size_t width>
30struct UnsignedTypeHelper {
31    using type = std::conditional_t<
32        width <= std::numeric_limits<uint8_t>::digits, uint8_t,
33        std::conditional_t<
34            width <= std::numeric_limits<uint16_t>::digits, uint16_t,
35            std::conditional_t<width <= std::numeric_limits<uint32_t>::digits, uint32_t,
36                               std::conditional_t<width <= std::numeric_limits<uint64_t>::digits, uint64_t, void>>>>;
37};
38
39template <size_t width>
40using UnsignedTypeHelperT = typename UnsignedTypeHelper<width>::type;
41
42template <size_t width, bool is_signed>
43struct TypeHelper {
44    using type =
45        std::conditional_t<is_signed, std::make_signed_t<UnsignedTypeHelperT<width>>, UnsignedTypeHelperT<width>>;
46};
47
48template <size_t width, bool is_signed>
49using TypeHelperT = typename TypeHelper<width, is_signed>::type;
50
51}  // namespace panda::helpers
52
53#endif  // LIBPANDABASE_UTILS_BIT_HELPERS_H
54