1 // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
2 // This source code is licensed under both the GPLv2 (found in the
3 // COPYING file in the root directory) and Apache 2.0 License
4 // (found in the LICENSE.Apache file in the root directory).
5
6 #pragma once
7
8 namespace folly {
9 template <typename T>
constexpr_max(T a)10 constexpr T constexpr_max(T a) {
11 return a;
12 }
13 template <typename T, typename... Ts>
constexpr_max(T a,T b,Ts...ts)14 constexpr T constexpr_max(T a, T b, Ts... ts) {
15 return b < a ? constexpr_max(a, ts...) : constexpr_max(b, ts...);
16 }
17
18 namespace detail {
19 template <typename T>
constexpr_log2_(T a,T e)20 constexpr T constexpr_log2_(T a, T e) {
21 return e == T(1) ? a : constexpr_log2_(a + T(1), e / T(2));
22 }
23
24 template <typename T>
constexpr_log2_ceil_(T l2,T t)25 constexpr T constexpr_log2_ceil_(T l2, T t) {
26 return l2 + T(T(1) << l2 < t ? 1 : 0);
27 }
28
29 template <typename T>
constexpr_square_(T t)30 constexpr T constexpr_square_(T t) {
31 return t * t;
32 }
33 } // namespace detail
34
35 template <typename T>
constexpr_log2(T t)36 constexpr T constexpr_log2(T t) {
37 return detail::constexpr_log2_(T(0), t);
38 }
39
40 template <typename T>
constexpr_log2_ceil(T t)41 constexpr T constexpr_log2_ceil(T t) {
42 return detail::constexpr_log2_ceil_(constexpr_log2(t), t);
43 }
44
45 } // namespace folly
46