Tatooine
pow.h
Go to the documentation of this file.
1#ifndef TATOOINE_POW_H
2#define TATOOINE_POW_H
3//==============================================================================
4#include <concepts>
5//==============================================================================
6namespace tatooine {
7//==============================================================================
8template <std::integral Int>
9constexpr auto pow(Int x, std::unsigned_integral auto const p) -> Int {
10 if (p == 0) {
11 return 1;
12 }
13 if (p == 1) {
14 return x;
15 }
16
17 auto const tmp = pow(x, p / 2);
18 if (p % 2 == 0) {
19 return tmp * tmp;
20 } else {
21 return x * tmp * tmp;
22 }
23}
24//==============================================================================
25} // namespace tatooine
26//==============================================================================
27#endif
Definition: algorithm.h:6
constexpr auto pow(arithmetic auto const x)
Definition: math.h:30