blob: 8d1fc8776fe3b0779ab945d2f535697926b5bae9 [file] [log] [blame]
kwiberg7885d3f2017-04-25 12:35:07 -07001/*
2 * Copyright 2017 The WebRTC Project Authors. All rights reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11// Minimum and maximum
12// ===================
13//
14// rtc::SafeMin(x, y)
15// rtc::SafeMax(x, y)
16//
17// Accept two arguments of either any two integral or any two floating-point
18// types, and return the smaller and larger value, respectively, with no
19// truncation or wrap-around. If only one of the input types is statically
20// guaranteed to be able to represent the result, the return type is that type;
21// if either one would do, the result type is the smaller type. (One of these
22// two cases always applies.)
23//
24// (The case with one floating-point and one integral type is not allowed,
25// because the floating-point type will have greater range, but may not have
26// sufficient precision to represent the integer value exactly.)
27//
28// Requesting a specific return type
29// =================================
30//
31// Both functions allow callers to explicitly specify the return type as a
32// template parameter, overriding the default return type. E.g.
33//
34// rtc::SafeMin<int>(x, y) // returns an int
35//
36// If the requested type is statically guaranteed to be able to represent the
37// result, then everything's fine, and the return type is as requested. But if
38// the requested type is too small, a static_assert is triggered.
39
40#ifndef WEBRTC_BASE_SAFE_MINMAX_H_
41#define WEBRTC_BASE_SAFE_MINMAX_H_
42
43#include <limits>
44#include <type_traits>
45
46#include "webrtc/base/checks.h"
47#include "webrtc/base/safe_compare.h"
48#include "webrtc/base/type_traits.h"
49
50namespace rtc {
51
52namespace safe_minmax_impl {
53
54// Make the range of a type available via something other than a constexpr
55// function, to work around MSVC limitations. See
56// https://blogs.msdn.microsoft.com/vcblog/2015/12/02/partial-support-for-expression-sfinae-in-vs-2015-update-1/
57template <typename T>
58struct Limits {
59 static constexpr T lowest = std::numeric_limits<T>::lowest();
60 static constexpr T max = std::numeric_limits<T>::max();
61};
62
63template <typename T, bool is_enum = std::is_enum<T>::value>
64struct UnderlyingType;
65
66template <typename T>
67struct UnderlyingType<T, false> {
68 using type = T;
69};
70
71template <typename T>
72struct UnderlyingType<T, true> {
73 using type = typename std::underlying_type<T>::type;
74};
75
76// Given two types T1 and T2, find types that can hold the smallest (in
77// ::min_t) and the largest (in ::max_t) of the two values.
78template <typename T1,
79 typename T2,
80 bool int1 = IsIntlike<T1>::value,
81 bool int2 = IsIntlike<T2>::value>
82struct MType {
83 static_assert(int1 == int2,
84 "You may not mix integral and floating-point arguments");
85};
86
87// Specialization for when neither type is integral (and therefore presumably
88// floating-point).
89template <typename T1, typename T2>
90struct MType<T1, T2, false, false> {
91 using min_t = typename std::common_type<T1, T2>::type;
92 static_assert(std::is_same<min_t, T1>::value ||
93 std::is_same<min_t, T2>::value,
94 "");
95
96 using max_t = typename std::common_type<T1, T2>::type;
97 static_assert(std::is_same<max_t, T1>::value ||
98 std::is_same<max_t, T2>::value,
99 "");
100};
101
102// Specialization for when both types are integral.
103template <typename T1, typename T2>
104struct MType<T1, T2, true, true> {
105 // The type with the lowest minimum value. In case of a tie, the type with
106 // the lowest maximum value. In case that too is a tie, the types have the
107 // same range, and we arbitrarily pick T1.
108 using min_t = typename std::conditional<
109 safe_cmp::Lt(Limits<T1>::lowest, Limits<T2>::lowest),
110 T1,
111 typename std::conditional<
112 safe_cmp::Gt(Limits<T1>::lowest, Limits<T2>::lowest),
113 T2,
114 typename std::conditional<safe_cmp::Le(Limits<T1>::max,
115 Limits<T2>::max),
116 T1,
117 T2>::type>::type>::type;
118 static_assert(std::is_same<min_t, T1>::value ||
119 std::is_same<min_t, T2>::value,
120 "");
121
122 // The type with the highest maximum value. In case of a tie, the types have
123 // the same range (because in C++, integer types with the same maximum also
124 // have the same minimum).
125 static_assert(safe_cmp::Ne(Limits<T1>::max, Limits<T2>::max) ||
126 safe_cmp::Eq(Limits<T1>::lowest, Limits<T2>::lowest),
127 "integer types with the same max should have the same min");
128 using max_t = typename std::
129 conditional<safe_cmp::Ge(Limits<T1>::max, Limits<T2>::max), T1, T2>::type;
130 static_assert(std::is_same<max_t, T1>::value ||
131 std::is_same<max_t, T2>::value,
132 "");
133};
134
135// A dummy type that we pass around at compile time but never actually use.
136// Declared but not defined.
137struct DefaultType;
138
139// ::type is A, except we fall back to B if A is DefaultType. We static_assert
140// that the chosen type can hold all values that B can hold.
141template <typename A, typename B>
142struct TypeOr {
143 using type = typename std::
144 conditional<std::is_same<A, DefaultType>::value, B, A>::type;
145 static_assert(safe_cmp::Le(Limits<type>::lowest, Limits<B>::lowest) &&
146 safe_cmp::Ge(Limits<type>::max, Limits<B>::max),
147 "The specified type isn't large enough");
148 static_assert(IsIntlike<type>::value == IsIntlike<B>::value &&
149 std::is_floating_point<type>::value ==
150 std::is_floating_point<type>::value,
151 "float<->int conversions not allowed");
152};
153
154} // namespace safe_minmax_impl
155
156template <typename R = safe_minmax_impl::DefaultType,
157 typename T1 = safe_minmax_impl::DefaultType,
158 typename T2 = safe_minmax_impl::DefaultType,
159 typename R2 = typename safe_minmax_impl::TypeOr<
160 R,
161 typename safe_minmax_impl::UnderlyingType<
162 typename safe_minmax_impl::MType<T1, T2>::min_t>::type>::type>
163constexpr R2 SafeMin(T1 a, T2 b) {
164 static_assert(IsIntlike<T1>::value || std::is_floating_point<T1>::value,
165 "The first argument must be integral or floating-point");
166 static_assert(IsIntlike<T2>::value || std::is_floating_point<T2>::value,
167 "The second argument must be integral or floating-point");
168 return safe_cmp::Lt(a, b) ? static_cast<R2>(a) : static_cast<R2>(b);
169}
170
171template <typename R = safe_minmax_impl::DefaultType,
172 typename T1 = safe_minmax_impl::DefaultType,
173 typename T2 = safe_minmax_impl::DefaultType,
174 typename R2 = typename safe_minmax_impl::TypeOr<
175 R,
176 typename safe_minmax_impl::UnderlyingType<
177 typename safe_minmax_impl::MType<T1, T2>::max_t>::type>::type>
178constexpr R2 SafeMax(T1 a, T2 b) {
179 static_assert(IsIntlike<T1>::value || std::is_floating_point<T1>::value,
180 "The first argument must be integral or floating-point");
181 static_assert(IsIntlike<T2>::value || std::is_floating_point<T2>::value,
182 "The second argument must be integral or floating-point");
183 return safe_cmp::Gt(a, b) ? static_cast<R2>(a) : static_cast<R2>(b);
184}
185
186} // namespace rtc
187
188#endif // WEBRTC_BASE_SAFE_MINMAX_H_