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