Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
log2.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_MATH_LOG2_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_MATH_LOG2_HPP_
7
8// IWYU pragma: private, include "arene/base/math.hpp"
9// IWYU pragma: friend "(arene/base(?!/tests)|stdlib/include/stdlib_detail)/.*"
10
11// parasoft-begin-suppress AUTOSAR-A7_1_5-a-2 "Trailing return syntax pmeritted by A7-1-5 Permit #1 v1.0.0"
12// parasoft-begin-suppress AUTOSAR-M17_0_3-a-2 "False positive: names are in a different namespace"
13// parasoft-begin-suppress CERT_CPP-DCL51-f-3 "False positive: no reserved names are used"
14
15#include "arene/base/contracts/contract.hpp"
16#include "arene/base/stdlib_choice/cstddef.hpp"
17
18// parasoft-begin-suppress AUTOSAR-A16_2_2-a-2 "Arene Base aggregate headers permitted by A16-2-2 Permit #1"
19// parasoft-end-suppress AUTOSAR-A16_2_2-a-2
20
21namespace arene {
22namespace base {
23
24/// @brief Computes the integer base-2 log of a value.
25/// @param value The value to compute the base-2 log of.
26/// @return std::size_t The greatest value. @c x , for which @c 2^x<=value .
27/// @pre @c value must be greater than zero.
28inline constexpr auto log2(std::size_t value) noexcept -> std::size_t { // CODEQLFP(DCL51-CPP)
29 ARENE_PRECONDITION(value > std::size_t{});
30
31 std::size_t res{0U};
32 constexpr std::size_t two{2U};
33 while (value >= two) {
34 value /= two;
35 ++res;
36 }
37 return res;
38}
39
40} // namespace base
41} // namespace arene
42
43#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_MATH_LOG2_HPP_
Definition array_exceptions_disabled.cpp:11
constexpr auto log2(std::size_t value) noexcept -> std::size_t
Computes the integer base-2 log of a value.
Definition log2.hpp:28
Copyright 2026, Toyota Motor Corporation.
Definition array_exceptions_disabled.cpp:10