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