Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
log2.hpp
Go to the documentation of this file.
1// parasoft-begin-suppress AUTOSAR-A2_8_1-a "False positive: also defines arene::base::log2"
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_LOG2_HPP_
8#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_MATH_LOG2_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// parasoft-begin-suppress AUTOSAR-M17_0_3-a-2 "False positive: names are in a different namespace"
15// parasoft-begin-suppress CERT_CPP-DCL51-f-3 "False positive: no reserved names are used"
16
18#include "arene/base/stdlib_choice/cstddef.hpp"
19
20// parasoft-begin-suppress AUTOSAR-A16_2_2-a-2 "Arene Base aggregate headers permitted by A16-2-2 Permit #1"
21// parasoft-end-suppress AUTOSAR-A16_2_2-a-2
22
23namespace arene {
24namespace base {
25
26/// @brief Computes the integer base-2 log of a value.
27/// @param value The value to compute the base-2 log of.
28/// @return std::size_t The greatest value. @c x , for which @c 2^x<=value .
29/// @pre @c value must be greater than zero.
30inline constexpr auto log2(std::size_t value) noexcept -> std::size_t { // CODEQLFP(DCL51-CPP)
32
33 std::size_t res{0U};
34 constexpr std::size_t two{2U};
35 while (value >= two) {
36 value /= two;
37 ++res;
38 }
39 return res;
40}
41
42} // namespace base
43} // namespace arene
44
45#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_MATH_LOG2_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 log2(std::size_t value) noexcept -> std::size_t
Computes the integer base-2 log of a value.
Definition log2.hpp:30
Definition accumulate.hpp:36
::size_t size_t
A type suitable for holding the size of an object.
Definition cstddef.hpp:29