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"
16#include "arene/base/constraints/constraints.hpp"
17#include "arene/base/contracts/contract.hpp"
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
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>
67constexpr auto make_power_of_2_sequence(std::index_sequence<>, std::index_sequence<Powers...>) noexcept {
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
75make_power_of_2_sequence(std::index_sequence<First, IgnoredPowers...>, std::index_sequence<First, Powers...>) noexcept {
76 return make_power_of_2_sequence(std::index_sequence<IgnoredPowers...>{}, std::index_sequence<Powers...>{});
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>
90
91} // namespace base
92} // namespace arene
93
94#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_MATH_POWER_OF_2_HPP_
Definition array_exceptions_disabled.cpp:11
Copyright 2026, Toyota Motor Corporation.
Definition array_exceptions_disabled.cpp:10