1 /* 2 * Copyright (c) 2021-2024 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 PANDA_LIBPANDABASE_UTILS_BIT_HELPERS_H_ 17 #define PANDA_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 27 namespace ark::helpers { 28 29 template <size_t WIDTH> 30 struct UnsignedTypeHelper { 31 // NOLINTNEXTLINE(readability-identifier-naming) 32 using type = std::conditional_t< 33 WIDTH <= std::numeric_limits<uint8_t>::digits, uint8_t, 34 std::conditional_t< 35 WIDTH <= std::numeric_limits<uint16_t>::digits, uint16_t, 36 std::conditional_t<WIDTH <= std::numeric_limits<uint32_t>::digits, uint32_t, 37 std::conditional_t<WIDTH <= std::numeric_limits<uint64_t>::digits, uint64_t, void>>>>; 38 }; 39 40 template <size_t WIDTH> 41 using UnsignedTypeHelperT = typename UnsignedTypeHelper<WIDTH>::type; 42 43 template <size_t WIDTH, bool IS_SIGNED> 44 struct TypeHelper { 45 // NOLINTNEXTLINE(readability-identifier-naming) 46 using type = 47 std::conditional_t<IS_SIGNED, std::make_signed_t<UnsignedTypeHelperT<WIDTH>>, UnsignedTypeHelperT<WIDTH>>; 48 }; 49 50 template <size_t WIDTH, bool IS_SIGNED> 51 using TypeHelperT = typename TypeHelper<WIDTH, IS_SIGNED>::type; 52 53 } // namespace ark::helpers 54 55 #endif // PANDA_LIBPANDABASE_UTILS_BIT_HELPERS_H_ 56