Disable zerocopy if we're notified about deferred copies, add a isZeroCopyWriteInProg...
[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 namespace detail {
48
49 template <typename T, typename = void>
50 struct constexpr_abs_helper {};
51
52 template <typename T>
53 struct constexpr_abs_helper<
54     T,
55     typename std::enable_if<std::is_floating_point<T>::value>::type> {
56   static constexpr T go(T t) {
57     return t < static_cast<T>(0) ? -t : t;
58   }
59 };
60
61 template <typename T>
62 struct constexpr_abs_helper<
63     T,
64     typename std::enable_if<
65         std::is_integral<T>::value && !std::is_same<T, bool>::value &&
66         std::is_unsigned<T>::value>::type> {
67   static constexpr T go(T t) {
68     return t;
69   }
70 };
71
72 template <typename T>
73 struct constexpr_abs_helper<
74     T,
75     typename std::enable_if<
76         std::is_integral<T>::value && !std::is_same<T, bool>::value &&
77         std::is_signed<T>::value>::type> {
78   static constexpr typename std::make_unsigned<T>::type go(T t) {
79     return typename std::make_unsigned<T>::type(t < static_cast<T>(0) ? -t : t);
80   }
81 };
82 } // namespace detail
83
84 template <typename T>
85 constexpr auto constexpr_abs(T t)
86     -> decltype(detail::constexpr_abs_helper<T>::go(t)) {
87   return detail::constexpr_abs_helper<T>::go(t);
88 }
89
90 namespace detail {
91 template <typename T>
92 constexpr T constexpr_log2(T a, T e) {
93   return e == T(1) ? a : constexpr_log2(a + T(1), e / T(2));
94 }
95 } // namespace detail
96
97 template <typename T>
98 constexpr T constexpr_log2(T t) {
99   return detail::constexpr_log2(T(0), t);
100 }
101
102 } // namespace folly