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/integral.hpp>
8
9namespace etl {
10
11template <integral Int>
12[[nodiscard]] constexpr auto ipow(Int base, Int exponent) noexcept -> Int
13{
14 auto result = Int(1);
15 for (auto i = Int(0); i < exponent; ++i) {
16 result *= base;
17 }
18 return result;
19}
20
21template <auto Base>
22[[nodiscard]] constexpr auto ipow(decltype(Base) exponent) noexcept -> decltype(Base)
23{
24 using Int = decltype(Base);
25
26 if constexpr (Base == Int(2)) {
27 return static_cast<Int>(Int(1) << exponent);
28 } else {
29 return ipow(Base, exponent);
30 }
31}
32
33} // namespace etl
34
35#endif // TETL_MATH_IPOW_HPP
Definition adjacent_find.hpp:9
constexpr auto ipow(Int base, Int exponent) noexcept -> Int
Definition ipow.hpp:12
constexpr auto ipow(decltype(Base) exponent) noexcept -> decltype(Base)
Definition ipow.hpp:22