cmake: fix path to FindGLog.cmake
[folly.git] / folly / ConstexprMath.h
1 /*
2  * Copyright 2017-present Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #pragma once
18
19 #include <type_traits>
20
21 namespace folly {
22
23 // TLDR: Prefer using operator< for ordering. And when
24 // a and b are equivalent objects, we return b to make
25 // sorting stable.
26 // See http://stepanovpapers.com/notes.pdf for details.
27 template <typename T>
28 constexpr T constexpr_max(T a) {
29   return a;
30 }
31 template <typename T, typename... Ts>
32 constexpr T constexpr_max(T a, T b, Ts... ts) {
33   return b < a ? constexpr_max(a, ts...) : constexpr_max(b, ts...);
34 }
35
36 // When a and b are equivalent objects, we return a to
37 // make sorting stable.
38 template <typename T>
39 constexpr T constexpr_min(T a) {
40   return a;
41 }
42 template <typename T, typename... Ts>
43 constexpr T constexpr_min(T a, T b, Ts... ts) {
44   return b < a ? constexpr_min(b, ts...) : constexpr_min(a, ts...);
45 }
46
47 template <typename T, typename Less>
48 constexpr T const&
49 constexpr_clamp(T const& v, T const& lo, T const& hi, Less less) {
50   return less(v, lo) ? lo : less(hi, v) ? hi : v;
51 }
52
53 template <typename T>
54 constexpr T const& constexpr_clamp(T const& v, T const& lo, T const& hi) {
55   struct Less {
56     constexpr bool operator()(T const& a, T const& b) const {
57       return a < b;
58     }
59   };
60   return constexpr_clamp(v, lo, hi, Less{});
61 }
62
63 namespace detail {
64
65 template <typename T, typename = void>
66 struct constexpr_abs_helper {};
67
68 template <typename T>
69 struct constexpr_abs_helper<
70     T,
71     typename std::enable_if<std::is_floating_point<T>::value>::type> {
72   static constexpr T go(T t) {
73     return t < static_cast<T>(0) ? -t : t;
74   }
75 };
76
77 template <typename T>
78 struct constexpr_abs_helper<
79     T,
80     typename std::enable_if<
81         std::is_integral<T>::value && !std::is_same<T, bool>::value &&
82         std::is_unsigned<T>::value>::type> {
83   static constexpr T go(T t) {
84     return t;
85   }
86 };
87
88 template <typename T>
89 struct constexpr_abs_helper<
90     T,
91     typename std::enable_if<
92         std::is_integral<T>::value && !std::is_same<T, bool>::value &&
93         std::is_signed<T>::value>::type> {
94   static constexpr typename std::make_unsigned<T>::type go(T t) {
95     return typename std::make_unsigned<T>::type(t < static_cast<T>(0) ? -t : t);
96   }
97 };
98 } // namespace detail
99
100 template <typename T>
101 constexpr auto constexpr_abs(T t)
102     -> decltype(detail::constexpr_abs_helper<T>::go(t)) {
103   return detail::constexpr_abs_helper<T>::go(t);
104 }
105
106 namespace detail {
107 template <typename T>
108 constexpr T constexpr_log2_(T a, T e) {
109   return e == T(1) ? a : constexpr_log2_(a + T(1), e / T(2));
110 }
111
112 template <typename T>
113 constexpr T constexpr_log2_ceil_(T l2, T t) {
114   return l2 + T(T(1) << l2 < t ? 1 : 0);
115 }
116
117 template <typename T>
118 constexpr T constexpr_square_(T t) {
119   return t * t;
120 }
121 } // namespace detail
122
123 template <typename T>
124 constexpr T constexpr_log2(T t) {
125   return detail::constexpr_log2_(T(0), t);
126 }
127
128 template <typename T>
129 constexpr T constexpr_log2_ceil(T t) {
130   return detail::constexpr_log2_ceil_(constexpr_log2(t), t);
131 }
132
133 template <typename T>
134 constexpr T constexpr_ceil(T t, T round) {
135   return round == T(0)
136       ? t
137       : ((t + (t < T(0) ? T(0) : round - T(1))) / round) * round;
138 }
139
140 template <typename T>
141 constexpr T constexpr_pow(T base, std::size_t exp) {
142   return exp == 0
143       ? T(1)
144       : exp == 1 ? base
145                  : detail::constexpr_square_(constexpr_pow(base, exp / 2)) *
146               (exp % 2 ? base : T(1));
147 }
148
149 } // namespace folly