constexpr T constexpr_log2_ceil_(T l2, T t) {
return l2 + T(T(1) << l2 < t ? 1 : 0);
}
+
+template <typename T>
+constexpr T constexpr_square_(T t) {
+ return t * t;
+}
} // namespace detail
template <typename T>
: ((t + (t < T(0) ? T(0) : round - T(1))) / round) * round;
}
+template <typename T>
+constexpr T constexpr_pow(T base, std::size_t exp) {
+ return exp == 0
+ ? T(1)
+ : exp == 1 ? base
+ : detail::constexpr_square_(constexpr_pow(base, exp / 2)) *
+ (exp % 2 ? base : T(1));
+}
+
} // namespace folly
EXPECT_EQ(-20ll, rounded);
}
}
+
+TEST_F(ConstexprMathTest, constexpr_pow) {
+ {
+ constexpr auto a = folly::constexpr_pow(uint64_t(0), 15);
+ EXPECT_EQ(0, a);
+ }
+ {
+ constexpr auto a = folly::constexpr_pow(uint64_t(15), 0);
+ EXPECT_EQ(1, a);
+ }
+ {
+ constexpr auto a = folly::constexpr_pow(uint64_t(2), 6);
+ EXPECT_EQ(64, a);
+ }
+}