Use simpler tags for ctor dispatch in exception_wrapper
[folly.git] / folly / ConstexprMath.h
1 /*
2  * Copyright 2017 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 } // namespace detail
112
113 template <typename T>
114 constexpr T constexpr_log2(T t) {
115   return detail::constexpr_log2(T(0), t);
116 }
117
118 } // namespace folly