Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
power_of_2.hpp
Go to the documentation of this file.
1// parasoft-begin-suppress AUTOSAR-A2_8_1-a-2 "False positive: also defines arene::base::power_of_2"
2
3// Copyright 2024, Toyota Motor Corporation
4//
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
7#ifndef INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_MATH_POWER_OF_2_HPP_
8#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_MATH_POWER_OF_2_HPP_
9
10// IWYU pragma: private, include "arene/base/math.hpp"
11// IWYU pragma: friend "(arene/base(?!/tests)|stdlib/include/stdlib_detail)/.*"
12
13// parasoft-begin-suppress AUTOSAR-A7_1_5-a-2 "Trailing return syntax pmeritted by A7-1-5 Permit #1 v1.0.0"
14
15// parasoft-begin-suppress AUTOSAR-A16_2_2-a-2 "Arene Base aggregate headers permitted by A16-2-2 Permit #1"
18#include "arene/base/stdlib_choice/climits.hpp"
19#include "arene/base/stdlib_choice/cstddef.hpp"
20#include "arene/base/stdlib_choice/cstdint.hpp"
21#include "arene/base/stdlib_choice/enable_if.hpp"
22#include "arene/base/stdlib_choice/integer_sequence.hpp"
23#include "arene/base/stdlib_choice/is_integral.hpp"
24#include "arene/base/stdlib_choice/make_unsigned.hpp"
25// parasoft-end-suppress AUTOSAR-A16_2_2-a-2
26
27namespace arene {
28namespace base {
29
30/// @brief Check if an integer is a positive power-of-two.
31/// @tparam Integer An integral type.
32/// @param value The value to test for a power-of-two.
33/// @return @c true if the value is a positive power of two, @c false otherwise.
34template <typename Integer, constraints<std::enable_if_t<std::is_integral<Integer>::value>> = nullptr>
35constexpr auto is_power_of_2(Integer value) noexcept -> bool {
36 /// @brief The unsigned type corresponding to the specific integer type
37 using unsigned_integer = std::make_unsigned_t<Integer>;
38
39 constexpr Integer zero{static_cast<Integer>(0)};
40 constexpr unsigned_integer unsigned_zero{0U};
41
42 // Note we need to explicitly check for zero here as otherwise zero would
43 // be reported as a power-of-2 if just using the bitand operation alone.
44 return (value > zero) && (static_cast<unsigned_integer>(
45 static_cast<unsigned_integer>(value) & (static_cast<unsigned_integer>(value) - 1U)
46 ) == unsigned_zero);
47}
48
49/// @brief Raises 2 to the power of a value
50/// @param value The value to raise two to the power of.
51/// @return std::uint64_t @c 2^value
52/// @pre value < the number of bits in @c std::uint64_t , or else precondition violation.
53/// @note std::size_t is 32 bits on some platforms that define std::uint64_t
54inline constexpr auto power_of_2(std::uint64_t const value) noexcept -> std::uint64_t {
55 ARENE_PRECONDITION(value < static_cast<std::uint64_t>(sizeof(std::uint64_t) * static_cast<std::uint64_t>(CHAR_BIT)));
56 constexpr std::uint64_t one{1U};
57 // parasoft-begin-suppress AUTOSAR-M5_8_1-a-2 "False positive: precondition above guarantees value is in range"
58 return one << value;
59 // parasoft-end-suppress AUTOSAR-M5_8_1-a-2
60}
61
62namespace power_of_2_detail {
63
64/// @brief Return the sequence of 2^N for each N in Powers
65/// @return An @c std::index_sequence holding the values
66template <std::size_t... Powers>
68 return std::index_sequence<power_of_2(Powers)...>();
69}
70
71/// @brief Return the sequence of 2^N for each N in Powers that is not in IgnoredPowers
72/// @return An @c std::index_sequence holding the values
73template <std::size_t First, std::size_t... IgnoredPowers, std::size_t... Powers>
74constexpr auto
77}
78
79} // namespace power_of_2_detail
80
81/// @brief Return the sequence of 2^N for each N in the half-open range [Begin,End)
82/// @return An @c std::index_sequence holding the values
83template <std::size_t Begin, std::size_t End, constraints<std::enable_if_t<(End >= Begin)>> = nullptr>
84constexpr auto make_power_of_2_sequence() noexcept {
85 return power_of_2_detail::make_power_of_2_sequence(
88 );
89}
90
91} // namespace base
92} // namespace arene
93
94#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_MATH_POWER_OF_2_HPP_
#define ARENE_PRECONDITION(condition)
Check that a precondition holds, and terminate the application if it does not.
Definition contract.hpp:150
Definition accumulate.hpp:37
constexpr auto is_power_of_2(Integer value) noexcept -> bool
Check if an integer is a positive power-of-two.
Definition power_of_2.hpp:35
constexpr auto power_of_2(std::uint64_t const value) noexcept -> std::uint64_t
Raises 2 to the power of a value.
Definition power_of_2.hpp:54
constexpr auto make_power_of_2_sequence() noexcept
Return the sequence of 2^N for each N in the half-open range [Begin,End)
Definition power_of_2.hpp:84
Definition accumulate.hpp:36
make_integer_sequence< size_t, Size > make_index_sequence
A helper alias template for make_integer_sequence<size_t, Size>
Definition integer_sequence.hpp:224
integer_sequence< size_t, Ints... > index_sequence
A helper alias template for integer_sequence<size_t, Ints>
Definition integer_sequence.hpp:218
::size_t size_t
A type suitable for holding the size of an object.
Definition cstddef.hpp:29
::uint64_t uint64_t
A 64-bit unsigned integer type.
Definition cstdint.hpp:47
typename make_unsigned< Type >::type make_unsigned_t
A type alias for the equivalent unsigned type for integral and enumeration types other than bool.
Definition make_unsigned.hpp:252