OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2017 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2017 The WebRTC Project Authors. All rights reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 // Minimum and maximum | 11 // Minimum and maximum |
12 // =================== | 12 // =================== |
13 // | 13 // |
14 // rtc::SafeMin(x, y) | 14 // rtc::SafeMin(x, y) |
15 // rtc::SafeMax(x, y) | 15 // rtc::SafeMax(x, y) |
16 // | 16 // |
| 17 // (These are both constexpr.) |
| 18 // |
17 // Accept two arguments of either any two integral or any two floating-point | 19 // 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 | 20 // 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 | 21 // 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; | 22 // 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 | 23 // if either one would do, the result type is the smaller type. (One of these |
22 // two cases always applies.) | 24 // two cases always applies.) |
23 // | 25 // |
24 // (The case with one floating-point and one integral type is not allowed, | 26 // * 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 | 27 // because the floating-point type will have greater range, but may not |
26 // sufficient precision to represent the integer value exactly.) | 28 // have sufficient precision to represent the integer value exactly.) |
| 29 // |
| 30 // Clamp (a.k.a. constrain to a given interval) |
| 31 // ============================================ |
| 32 // |
| 33 // rtc::SafeClamp(x, a, b) |
| 34 // |
| 35 // Accepts three arguments of any mix of integral types or any mix of |
| 36 // floating-point types, and returns the value in the closed interval [a, b] |
| 37 // that is closest to x (that is, if x < a it returns a; if x > b it returns b; |
| 38 // and if a <= x <= b it returns x). As for SafeMin() and SafeMax(), there is |
| 39 // no truncation or wrap-around. The result type |
| 40 // |
| 41 // 1. is statically guaranteed to be able to represent the result; |
| 42 // |
| 43 // 2. is no larger than the largest of the three argument types; and |
| 44 // |
| 45 // 3. has the same signedness as the type of the third argument, if this is |
| 46 // possible without violating the First or Second Law. |
| 47 // |
| 48 // There is always at least one type that meets criteria 1 and 2. If more than |
| 49 // one type meets these criteria equally well, the result type is one of the |
| 50 // types that is smallest. Note that unlike SafeMin() and SafeMax(), |
| 51 // SafeClamp() will sometimes pick a return type that isn't the type of any of |
| 52 // its arguments. |
| 53 // |
| 54 // * In this context, a type A is smaller than a type B if it has a smaller |
| 55 // range; that is, if A::max() - A::min() < B::max() - B::min(). For |
| 56 // example, int8_t < int16_t == uint16_t < int32_t, and all integral types |
| 57 // are smaller than all floating-point types.) |
| 58 // |
| 59 // * As for SafeMin and SafeMax, mixing integer and floating-point arguments |
| 60 // is not allowed, because floating-point types have greater range than |
| 61 // integer types, but do not have sufficient precision to represent the |
| 62 // values of most integer types exactly. |
27 // | 63 // |
28 // Requesting a specific return type | 64 // Requesting a specific return type |
29 // ================================= | 65 // ================================= |
30 // | 66 // |
31 // Both functions allow callers to explicitly specify the return type as a | 67 // All three functions allow callers to explicitly specify the return type as a |
32 // template parameter, overriding the default return type. E.g. | 68 // template parameter, overriding the default return type. E.g. |
33 // | 69 // |
34 // rtc::SafeMin<int>(x, y) // returns an int | 70 // rtc::SafeMin<int>(x, y) // returns an int |
35 // | 71 // |
36 // If the requested type is statically guaranteed to be able to represent the | 72 // 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 | 73 // 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. | 74 // the requested type is too small, a static_assert is triggered. |
39 | 75 |
40 #ifndef WEBRTC_BASE_SAFE_MINMAX_H_ | 76 #ifndef WEBRTC_BASE_SAFE_MINMAX_H_ |
41 #define WEBRTC_BASE_SAFE_MINMAX_H_ | 77 #define WEBRTC_BASE_SAFE_MINMAX_H_ |
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
180 typename safe_minmax_impl::UnderlyingType<T1>::type, | 216 typename safe_minmax_impl::UnderlyingType<T1>::type, |
181 typename safe_minmax_impl::UnderlyingType<T2>::type>::max_t>::type> | 217 typename safe_minmax_impl::UnderlyingType<T2>::type>::max_t>::type> |
182 constexpr R2 SafeMax(T1 a, T2 b) { | 218 constexpr R2 SafeMax(T1 a, T2 b) { |
183 static_assert(IsIntlike<T1>::value || std::is_floating_point<T1>::value, | 219 static_assert(IsIntlike<T1>::value || std::is_floating_point<T1>::value, |
184 "The first argument must be integral or floating-point"); | 220 "The first argument must be integral or floating-point"); |
185 static_assert(IsIntlike<T2>::value || std::is_floating_point<T2>::value, | 221 static_assert(IsIntlike<T2>::value || std::is_floating_point<T2>::value, |
186 "The second argument must be integral or floating-point"); | 222 "The second argument must be integral or floating-point"); |
187 return safe_cmp::Gt(a, b) ? static_cast<R2>(a) : static_cast<R2>(b); | 223 return safe_cmp::Gt(a, b) ? static_cast<R2>(a) : static_cast<R2>(b); |
188 } | 224 } |
189 | 225 |
| 226 namespace safe_minmax_impl { |
| 227 |
| 228 // Given three types T, L, and H, let ::type be a suitable return value for |
| 229 // SafeClamp(T, L, H). See the docs at the top of this file for details. |
| 230 template <typename T, |
| 231 typename L, |
| 232 typename H, |
| 233 bool int1 = IsIntlike<T>::value, |
| 234 bool int2 = IsIntlike<L>::value, |
| 235 bool int3 = IsIntlike<H>::value> |
| 236 struct ClampType { |
| 237 static_assert(int1 == int2 && int1 == int3, |
| 238 "You may not mix integral and floating-point arguments"); |
| 239 }; |
| 240 |
| 241 // Specialization for when all three types are floating-point. |
| 242 template <typename T, typename L, typename H> |
| 243 struct ClampType<T, L, H, false, false, false> { |
| 244 using type = typename std::common_type<T, L, H>::type; |
| 245 }; |
| 246 |
| 247 // Specialization for when all three types are integral. |
| 248 template <typename T, typename L, typename H> |
| 249 struct ClampType<T, L, H, true, true, true> { |
| 250 private: |
| 251 // Range of the return value. The return type must be able to represent this |
| 252 // full range. |
| 253 static constexpr auto r_min = |
| 254 SafeMax(Limits<L>::lowest, SafeMin(Limits<H>::lowest, Limits<T>::lowest)); |
| 255 static constexpr auto r_max = |
| 256 SafeMin(Limits<H>::max, SafeMax(Limits<L>::max, Limits<T>::max)); |
| 257 |
| 258 // Is the given type an acceptable return type? (That is, can it represent |
| 259 // all possible return values, and is it no larger than the largest of the |
| 260 // input types?) |
| 261 template <typename A> |
| 262 struct AcceptableType { |
| 263 private: |
| 264 static constexpr bool not_too_large = sizeof(A) <= sizeof(L) || |
| 265 sizeof(A) <= sizeof(H) || |
| 266 sizeof(A) <= sizeof(T); |
| 267 static constexpr bool range_contained = |
| 268 safe_cmp::Le(Limits<A>::lowest, r_min) && |
| 269 safe_cmp::Le(r_max, Limits<A>::max); |
| 270 |
| 271 public: |
| 272 static constexpr bool value = not_too_large && range_contained; |
| 273 }; |
| 274 |
| 275 using best_signed_type = typename std::conditional< |
| 276 AcceptableType<int8_t>::value, |
| 277 int8_t, |
| 278 typename std::conditional< |
| 279 AcceptableType<int16_t>::value, |
| 280 int16_t, |
| 281 typename std::conditional<AcceptableType<int32_t>::value, |
| 282 int32_t, |
| 283 int64_t>::type>::type>::type; |
| 284 |
| 285 using best_unsigned_type = typename std::conditional< |
| 286 AcceptableType<uint8_t>::value, |
| 287 uint8_t, |
| 288 typename std::conditional< |
| 289 AcceptableType<uint16_t>::value, |
| 290 uint16_t, |
| 291 typename std::conditional<AcceptableType<uint32_t>::value, |
| 292 uint32_t, |
| 293 uint64_t>::type>::type>::type; |
| 294 |
| 295 public: |
| 296 // Pick the best type, preferring the same signedness as T but falling back |
| 297 // to the other one if necessary. |
| 298 using type = typename std::conditional< |
| 299 std::is_signed<T>::value, |
| 300 typename std::conditional<AcceptableType<best_signed_type>::value, |
| 301 best_signed_type, |
| 302 best_unsigned_type>::type, |
| 303 typename std::conditional<AcceptableType<best_unsigned_type>::value, |
| 304 best_unsigned_type, |
| 305 best_signed_type>::type>::type; |
| 306 static_assert(AcceptableType<type>::value, ""); |
| 307 }; |
| 308 |
| 309 } // namespace safe_minmax_impl |
| 310 |
| 311 template < |
| 312 typename R = safe_minmax_impl::DefaultType, |
| 313 typename T = safe_minmax_impl::DefaultType, |
| 314 typename L = safe_minmax_impl::DefaultType, |
| 315 typename H = safe_minmax_impl::DefaultType, |
| 316 typename R2 = typename safe_minmax_impl::TypeOr< |
| 317 R, |
| 318 typename safe_minmax_impl::ClampType< |
| 319 typename safe_minmax_impl::UnderlyingType<T>::type, |
| 320 typename safe_minmax_impl::UnderlyingType<L>::type, |
| 321 typename safe_minmax_impl::UnderlyingType<H>::type>::type>::type> |
| 322 R2 SafeClamp(T x, L min, H max) { |
| 323 static_assert(IsIntlike<H>::value || std::is_floating_point<H>::value, |
| 324 "The first argument must be integral or floating-point"); |
| 325 static_assert(IsIntlike<T>::value || std::is_floating_point<T>::value, |
| 326 "The second argument must be integral or floating-point"); |
| 327 static_assert(IsIntlike<L>::value || std::is_floating_point<L>::value, |
| 328 "The third argument must be integral or floating-point"); |
| 329 RTC_DCHECK_LE(min, max); |
| 330 return safe_cmp::Le(x, min) |
| 331 ? static_cast<R2>(min) |
| 332 : safe_cmp::Ge(x, max) ? static_cast<R2>(max) : static_cast<R2>(x); |
| 333 } |
| 334 |
190 } // namespace rtc | 335 } // namespace rtc |
191 | 336 |
192 #endif // WEBRTC_BASE_SAFE_MINMAX_H_ | 337 #endif // WEBRTC_BASE_SAFE_MINMAX_H_ |
OLD | NEW |