tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
ipow.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSL-1.0
2// SPDX-FileCopyrightText: Copyright (C) 2023 Tobias Hienzsch
3
4#ifndef TETL_MATH_IPOW_HPP
5#define TETL_MATH_IPOW_HPP
6
7#include <etl/_concepts/builtin_integer.hpp>
8#include <etl/_type_traits/make_unsigned.hpp>
9
10namespace etl {
11
12template <builtin_integer Int>
13[[nodiscard]] constexpr auto ipow(Int base, Int exponent) noexcept -> Int
14{
15 auto result = Int(1);
16 for (auto i = Int(0); i < exponent; ++i) {
17 result *= base;
18 }
19 return result;
20}
21
22template <unsigned long long Base, builtin_integer Int>
23[[nodiscard]] constexpr auto ipow(Int exponent) noexcept -> Int
24{
25 using UInt = etl::make_unsigned_t<Int>;
26
27 if constexpr (Base == 2ULL) {
28 return static_cast<Int>(UInt(1) << UInt(exponent));
29 } else if constexpr (Base == 4ULL) {
30 return static_cast<Int>(UInt(1) << UInt(exponent * Int(2)));
31 } else if constexpr (Base == 8ULL) {
32 return static_cast<Int>(UInt(1) << UInt(exponent * Int(3)));
33 } else if constexpr (Base == 16ULL) {
34 return static_cast<Int>(UInt(1) << UInt(exponent * Int(4)));
35 } else if constexpr (Base == 32ULL) {
36 return static_cast<Int>(UInt(1) << UInt(exponent * Int(5)));
37 } else {
38 return etl::ipow(static_cast<Int>(Base), exponent);
39 }
40}
41
42} // namespace etl
43
44#endif // TETL_MATH_IPOW_HPP
Definition adjacent_find.hpp:9
constexpr auto ipow(Int base, Int exponent) noexcept -> Int
Definition ipow.hpp:13
constexpr auto ipow(Int exponent) noexcept -> Int
Definition ipow.hpp:23