Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
smallest_integer_for.hpp
Go to the documentation of this file.
1// Copyright 2024, Toyota Motor Corporation
2//
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4
5#ifndef INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_TYPE_MANIPULATION_SMALLEST_INTEGER_FOR_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_TYPE_MANIPULATION_SMALLEST_INTEGER_FOR_HPP_
7
8// parasoft-begin-suppress AUTOSAR-A16_2_2-a-2 "Arene Base aggregate headers permitted by A16-2-2 Permit #1"
10#include "arene/base/stdlib_choice/climits.hpp"
11#include "arene/base/stdlib_choice/cstdint.hpp"
12#include "arene/base/stdlib_choice/enable_if.hpp"
13#include "arene/base/stdlib_choice/numeric_limits.hpp"
15
16namespace arene {
17namespace base {
18
19// Note: limits are extracted into constexpr variables to work around a GCC 9 bug where nested
20// conditional_t aliases produce incorrect results when conditions contain parenthesized function
21// calls like (std::numeric_limits<T>::max)().
22namespace smallest_integer_for_detail {
32} // namespace smallest_integer_for_detail
33
34/// @brief Calculates the smallest unsigned integer type that can be used to hold
35/// values up to and including @c MaxValue.
36/// @tparam MaxValue The maximum value to store
37template <std::uint64_t MaxValue>
39 (MaxValue <= smallest_integer_for_detail::max_uint8),
42 (MaxValue <= smallest_integer_for_detail::max_uint16),
44 conditional_t<(MaxValue <= smallest_integer_for_detail::max_uint32), std::uint32_t, std::uint64_t>>>;
45
46/// @brief Calculates the smallest unsigned integer type that can be used to hold values of type T
47/// @tparam T The type of value to store
48template <class T>
49using smallest_unsigned_integer_for_type = smallest_unsigned_integer_for<1ULL << (sizeof(T) * CHAR_BIT - 1)>;
50
51/// @brief Calculates the smallest unsigned integer type that can be used to hold
52/// values from MinValue up to and including MaxValue.
53/// @tparam MinValue The minimum value to store
54/// @tparam MaxValue The maximum value to store, must be greater than or equal to @c MinValue
55template <std::int64_t MinValue, std::int64_t MaxValue, constraints<std::enable_if_t<(MaxValue >= MinValue)>> = nullptr>
57 (MaxValue <= smallest_integer_for_detail::max_int8) && (MinValue >= smallest_integer_for_detail::min_int8),
60 (MaxValue <= smallest_integer_for_detail::max_int16) && (MinValue >= smallest_integer_for_detail::min_int16),
63 (MaxValue <= smallest_integer_for_detail::max_int32) &&
64 (MinValue >= smallest_integer_for_detail::min_int32),
66 std::int64_t>>>;
67
68} // namespace base
69} // namespace arene
70
71#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_TYPE_MANIPULATION_SMALLEST_INTEGER_FOR_HPP_
static constexpr auto min() noexcept -> T
The minimum finite value; the minimum positive normalized value for floating point types.
Definition numeric_limits.hpp:235
static constexpr auto max() noexcept -> T
The maximum finite value.
Definition numeric_limits.hpp:230
Definition accumulate.hpp:37
typename static_if< B >::template then_else< T, F > conditional_t
lazily select between two types based on a boolean condition
Definition conditional.hpp:28
conditional_t<(MaxValue<=smallest_integer_for_detail::max_int8) &&(MinValue >=smallest_integer_for_detail::min_int8), std::int8_t, conditional_t<(MaxValue<=smallest_integer_for_detail::max_int16) &&(MinValue >=smallest_integer_for_detail::min_int16), std::int16_t, conditional_t<(MaxValue<=smallest_integer_for_detail::max_int32) &&(MinValue >=smallest_integer_for_detail::min_int32), std::int32_t, std::int64_t > > > smallest_signed_integer_for
Calculates the smallest unsigned integer type that can be used to hold values from MinValue up to and...
Definition smallest_integer_for.hpp:56
conditional_t<(MaxValue<=smallest_integer_for_detail::max_uint8), std::uint8_t, conditional_t<(MaxValue<=smallest_integer_for_detail::max_uint16), std::uint16_t, conditional_t<(MaxValue<=smallest_integer_for_detail::max_uint32), std::uint32_t, std::uint64_t > > > smallest_unsigned_integer_for
Calculates the smallest unsigned integer type that can be used to hold values up to and including Max...
Definition smallest_integer_for.hpp:38
smallest_unsigned_integer_for< 1ULL<<(sizeof(T) *CHAR_BIT - 1)> smallest_unsigned_integer_for_type
Calculates the smallest unsigned integer type that can be used to hold values of type T.
Definition smallest_integer_for.hpp:49
Definition accumulate.hpp:36
::int32_t int32_t
A 32-bit signed integer type.
Definition cstdint.hpp:66
::int16_t int16_t
A 16-bit signed integer type.
Definition cstdint.hpp:61
::uint8_t uint8_t
An 8-bit unsigned integer type.
Definition cstdint.hpp:30
::uint16_t uint16_t
A 16-bit unsigned integer type.
Definition cstdint.hpp:36
::uint64_t uint64_t
A 64-bit unsigned integer type.
Definition cstdint.hpp:47
::int8_t int8_t
An 8-bit signed integer type.
Definition cstdint.hpp:55
::int64_t int64_t
A 64-bit signed integer type.
Definition cstdint.hpp:71