2 * Copyright 2017 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <type_traits>
23 // TLDR: Prefer using operator< for ordering. And when
24 // a and b are equivalent objects, we return b to make
26 // See http://stepanovpapers.com/notes.pdf for details.
28 constexpr T constexpr_max(T a) {
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...);
36 // When a and b are equivalent objects, we return a to
37 // make sorting stable.
39 constexpr T constexpr_min(T a) {
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...);
47 template <typename T, typename Less>
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;
54 constexpr T const& constexpr_clamp(T const& v, T const& lo, T const& hi) {
56 constexpr bool operator()(T const& a, T const& b) const {
60 return constexpr_clamp(v, lo, hi, Less{});
65 template <typename T, typename = void>
66 struct constexpr_abs_helper {};
69 struct constexpr_abs_helper<
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;
78 struct constexpr_abs_helper<
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) {
89 struct constexpr_abs_helper<
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);
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);
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));
112 template <typename T>
113 constexpr T constexpr_log2_ceil(T l2, T t) {
114 return l2 + T(T(1) << l2 < t ? 1 : 0);
116 } // namespace detail
118 template <typename T>
119 constexpr T constexpr_log2(T t) {
120 return detail::constexpr_log2(T(0), t);
123 template <typename T>
124 constexpr T constexpr_log2_ceil(T t) {
125 return detail::constexpr_log2_ceil(constexpr_log2(t), t);
128 template <typename T>
129 constexpr T constexpr_ceil(T t, T round) {
132 : ((t + (t < T(0) ? T(0) : round - T(1))) / round) * round;