cmake: fix path to FindGLog.cmake
[folly.git] / folly / ConstexprMath.h
index 34f7664e2c1cfc4fa21009573e0fe88f4fdd4b3f..f8defbd043bf4a2879c93926475b14dc78c102fe 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2017 Facebook, Inc.
+ * Copyright 2017-present Facebook, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -105,24 +105,29 @@ constexpr auto constexpr_abs(T t)
 
 namespace detail {
 template <typename T>
-constexpr T constexpr_log2(T a, T e) {
-  return e == T(1) ? a : constexpr_log2(a + T(1), e / T(2));
+constexpr T constexpr_log2_(T a, T e) {
+  return e == T(1) ? a : constexpr_log2_(a + T(1), e / T(2));
 }
 
 template <typename T>
-constexpr T constexpr_log2_ceil(T l2, T t) {
+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>
 constexpr T constexpr_log2(T t) {
-  return detail::constexpr_log2(T(0), t);
+  return detail::constexpr_log2_(T(0), t);
 }
 
 template <typename T>
 constexpr T constexpr_log2_ceil(T t) {
-  return detail::constexpr_log2_ceil(constexpr_log2(t), t);
+  return detail::constexpr_log2_ceil_(constexpr_log2(t), t);
 }
 
 template <typename T>
@@ -132,4 +137,13 @@ constexpr T constexpr_ceil(T t, T round) {
       : ((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