1*d9f75844SAndroid Build Coastguard Worker /* 2*d9f75844SAndroid Build Coastguard Worker * Copyright 2017 The WebRTC Project Authors. All rights reserved. 3*d9f75844SAndroid Build Coastguard Worker * 4*d9f75844SAndroid Build Coastguard Worker * Use of this source code is governed by a BSD-style license 5*d9f75844SAndroid Build Coastguard Worker * that can be found in the LICENSE file in the root of the source 6*d9f75844SAndroid Build Coastguard Worker * tree. An additional intellectual property rights grant can be found 7*d9f75844SAndroid Build Coastguard Worker * in the file PATENTS. All contributing project authors may 8*d9f75844SAndroid Build Coastguard Worker * be found in the AUTHORS file in the root of the source tree. 9*d9f75844SAndroid Build Coastguard Worker */ 10*d9f75844SAndroid Build Coastguard Worker 11*d9f75844SAndroid Build Coastguard Worker // Minimum and maximum 12*d9f75844SAndroid Build Coastguard Worker // =================== 13*d9f75844SAndroid Build Coastguard Worker // 14*d9f75844SAndroid Build Coastguard Worker // rtc::SafeMin(x, y) 15*d9f75844SAndroid Build Coastguard Worker // rtc::SafeMax(x, y) 16*d9f75844SAndroid Build Coastguard Worker // 17*d9f75844SAndroid Build Coastguard Worker // (These are both constexpr.) 18*d9f75844SAndroid Build Coastguard Worker // 19*d9f75844SAndroid Build Coastguard Worker // Accept two arguments of either any two integral or any two floating-point 20*d9f75844SAndroid Build Coastguard Worker // types, and return the smaller and larger value, respectively, with no 21*d9f75844SAndroid Build Coastguard Worker // truncation or wrap-around. If only one of the input types is statically 22*d9f75844SAndroid Build Coastguard Worker // guaranteed to be able to represent the result, the return type is that type; 23*d9f75844SAndroid Build Coastguard Worker // if either one would do, the result type is the smaller type. (One of these 24*d9f75844SAndroid Build Coastguard Worker // two cases always applies.) 25*d9f75844SAndroid Build Coastguard Worker // 26*d9f75844SAndroid Build Coastguard Worker // * The case with one floating-point and one integral type is not allowed, 27*d9f75844SAndroid Build Coastguard Worker // because the floating-point type will have greater range, but may not 28*d9f75844SAndroid Build Coastguard Worker // have sufficient precision to represent the integer value exactly.) 29*d9f75844SAndroid Build Coastguard Worker // 30*d9f75844SAndroid Build Coastguard Worker // Clamp (a.k.a. constrain to a given interval) 31*d9f75844SAndroid Build Coastguard Worker // ============================================ 32*d9f75844SAndroid Build Coastguard Worker // 33*d9f75844SAndroid Build Coastguard Worker // rtc::SafeClamp(x, a, b) 34*d9f75844SAndroid Build Coastguard Worker // 35*d9f75844SAndroid Build Coastguard Worker // Accepts three arguments of any mix of integral types or any mix of 36*d9f75844SAndroid Build Coastguard Worker // floating-point types, and returns the value in the closed interval [a, b] 37*d9f75844SAndroid Build Coastguard Worker // that is closest to x (that is, if x < a it returns a; if x > b it returns b; 38*d9f75844SAndroid Build Coastguard Worker // and if a <= x <= b it returns x). As for SafeMin() and SafeMax(), there is 39*d9f75844SAndroid Build Coastguard Worker // no truncation or wrap-around. The result type 40*d9f75844SAndroid Build Coastguard Worker // 41*d9f75844SAndroid Build Coastguard Worker // 1. is statically guaranteed to be able to represent the result; 42*d9f75844SAndroid Build Coastguard Worker // 43*d9f75844SAndroid Build Coastguard Worker // 2. is no larger than the largest of the three argument types; and 44*d9f75844SAndroid Build Coastguard Worker // 45*d9f75844SAndroid Build Coastguard Worker // 3. has the same signedness as the type of the first argument, if this is 46*d9f75844SAndroid Build Coastguard Worker // possible without violating the First or Second Law. 47*d9f75844SAndroid Build Coastguard Worker // 48*d9f75844SAndroid Build Coastguard Worker // There is always at least one type that meets criteria 1 and 2. If more than 49*d9f75844SAndroid Build Coastguard Worker // one type meets these criteria equally well, the result type is one of the 50*d9f75844SAndroid Build Coastguard Worker // types that is smallest. Note that unlike SafeMin() and SafeMax(), 51*d9f75844SAndroid Build Coastguard Worker // SafeClamp() will sometimes pick a return type that isn't the type of any of 52*d9f75844SAndroid Build Coastguard Worker // its arguments. 53*d9f75844SAndroid Build Coastguard Worker // 54*d9f75844SAndroid Build Coastguard Worker // * In this context, a type A is smaller than a type B if it has a smaller 55*d9f75844SAndroid Build Coastguard Worker // range; that is, if A::max() - A::min() < B::max() - B::min(). For 56*d9f75844SAndroid Build Coastguard Worker // example, int8_t < int16_t == uint16_t < int32_t, and all integral types 57*d9f75844SAndroid Build Coastguard Worker // are smaller than all floating-point types.) 58*d9f75844SAndroid Build Coastguard Worker // 59*d9f75844SAndroid Build Coastguard Worker // * As for SafeMin and SafeMax, mixing integer and floating-point arguments 60*d9f75844SAndroid Build Coastguard Worker // is not allowed, because floating-point types have greater range than 61*d9f75844SAndroid Build Coastguard Worker // integer types, but do not have sufficient precision to represent the 62*d9f75844SAndroid Build Coastguard Worker // values of most integer types exactly. 63*d9f75844SAndroid Build Coastguard Worker // 64*d9f75844SAndroid Build Coastguard Worker // Requesting a specific return type 65*d9f75844SAndroid Build Coastguard Worker // ================================= 66*d9f75844SAndroid Build Coastguard Worker // 67*d9f75844SAndroid Build Coastguard Worker // All three functions allow callers to explicitly specify the return type as a 68*d9f75844SAndroid Build Coastguard Worker // template parameter, overriding the default return type. E.g. 69*d9f75844SAndroid Build Coastguard Worker // 70*d9f75844SAndroid Build Coastguard Worker // rtc::SafeMin<int>(x, y) // returns an int 71*d9f75844SAndroid Build Coastguard Worker // 72*d9f75844SAndroid Build Coastguard Worker // If the requested type is statically guaranteed to be able to represent the 73*d9f75844SAndroid Build Coastguard Worker // result, then everything's fine, and the return type is as requested. But if 74*d9f75844SAndroid Build Coastguard Worker // the requested type is too small, a static_assert is triggered. 75*d9f75844SAndroid Build Coastguard Worker 76*d9f75844SAndroid Build Coastguard Worker #ifndef RTC_BASE_NUMERICS_SAFE_MINMAX_H_ 77*d9f75844SAndroid Build Coastguard Worker #define RTC_BASE_NUMERICS_SAFE_MINMAX_H_ 78*d9f75844SAndroid Build Coastguard Worker 79*d9f75844SAndroid Build Coastguard Worker #include <limits> 80*d9f75844SAndroid Build Coastguard Worker #include <type_traits> 81*d9f75844SAndroid Build Coastguard Worker 82*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/checks.h" 83*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/numerics/safe_compare.h" 84*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/type_traits.h" 85*d9f75844SAndroid Build Coastguard Worker 86*d9f75844SAndroid Build Coastguard Worker namespace rtc { 87*d9f75844SAndroid Build Coastguard Worker 88*d9f75844SAndroid Build Coastguard Worker namespace safe_minmax_impl { 89*d9f75844SAndroid Build Coastguard Worker 90*d9f75844SAndroid Build Coastguard Worker // Make the range of a type available via something other than a constexpr 91*d9f75844SAndroid Build Coastguard Worker // function, to work around MSVC limitations. See 92*d9f75844SAndroid Build Coastguard Worker // https://blogs.msdn.microsoft.com/vcblog/2015/12/02/partial-support-for-expression-sfinae-in-vs-2015-update-1/ 93*d9f75844SAndroid Build Coastguard Worker template <typename T> 94*d9f75844SAndroid Build Coastguard Worker struct Limits { 95*d9f75844SAndroid Build Coastguard Worker static constexpr T lowest = std::numeric_limits<T>::lowest(); 96*d9f75844SAndroid Build Coastguard Worker static constexpr T max = std::numeric_limits<T>::max(); 97*d9f75844SAndroid Build Coastguard Worker }; 98*d9f75844SAndroid Build Coastguard Worker 99*d9f75844SAndroid Build Coastguard Worker template <typename T, bool is_enum = std::is_enum<T>::value> 100*d9f75844SAndroid Build Coastguard Worker struct UnderlyingType; 101*d9f75844SAndroid Build Coastguard Worker 102*d9f75844SAndroid Build Coastguard Worker template <typename T> 103*d9f75844SAndroid Build Coastguard Worker struct UnderlyingType<T, false> { 104*d9f75844SAndroid Build Coastguard Worker using type = T; 105*d9f75844SAndroid Build Coastguard Worker }; 106*d9f75844SAndroid Build Coastguard Worker 107*d9f75844SAndroid Build Coastguard Worker template <typename T> 108*d9f75844SAndroid Build Coastguard Worker struct UnderlyingType<T, true> { 109*d9f75844SAndroid Build Coastguard Worker using type = typename std::underlying_type<T>::type; 110*d9f75844SAndroid Build Coastguard Worker }; 111*d9f75844SAndroid Build Coastguard Worker 112*d9f75844SAndroid Build Coastguard Worker // Given two types T1 and T2, find types that can hold the smallest (in 113*d9f75844SAndroid Build Coastguard Worker // ::min_t) and the largest (in ::max_t) of the two values. 114*d9f75844SAndroid Build Coastguard Worker template <typename T1, 115*d9f75844SAndroid Build Coastguard Worker typename T2, 116*d9f75844SAndroid Build Coastguard Worker bool int1 = IsIntlike<T1>::value, 117*d9f75844SAndroid Build Coastguard Worker bool int2 = IsIntlike<T2>::value> 118*d9f75844SAndroid Build Coastguard Worker struct MType { 119*d9f75844SAndroid Build Coastguard Worker static_assert(int1 == int2, 120*d9f75844SAndroid Build Coastguard Worker "You may not mix integral and floating-point arguments"); 121*d9f75844SAndroid Build Coastguard Worker }; 122*d9f75844SAndroid Build Coastguard Worker 123*d9f75844SAndroid Build Coastguard Worker // Specialization for when neither type is integral (and therefore presumably 124*d9f75844SAndroid Build Coastguard Worker // floating-point). 125*d9f75844SAndroid Build Coastguard Worker template <typename T1, typename T2> 126*d9f75844SAndroid Build Coastguard Worker struct MType<T1, T2, false, false> { 127*d9f75844SAndroid Build Coastguard Worker using min_t = typename std::common_type<T1, T2>::type; 128*d9f75844SAndroid Build Coastguard Worker static_assert(std::is_same<min_t, T1>::value || 129*d9f75844SAndroid Build Coastguard Worker std::is_same<min_t, T2>::value, 130*d9f75844SAndroid Build Coastguard Worker ""); 131*d9f75844SAndroid Build Coastguard Worker 132*d9f75844SAndroid Build Coastguard Worker using max_t = typename std::common_type<T1, T2>::type; 133*d9f75844SAndroid Build Coastguard Worker static_assert(std::is_same<max_t, T1>::value || 134*d9f75844SAndroid Build Coastguard Worker std::is_same<max_t, T2>::value, 135*d9f75844SAndroid Build Coastguard Worker ""); 136*d9f75844SAndroid Build Coastguard Worker }; 137*d9f75844SAndroid Build Coastguard Worker 138*d9f75844SAndroid Build Coastguard Worker // Specialization for when both types are integral. 139*d9f75844SAndroid Build Coastguard Worker template <typename T1, typename T2> 140*d9f75844SAndroid Build Coastguard Worker struct MType<T1, T2, true, true> { 141*d9f75844SAndroid Build Coastguard Worker // The type with the lowest minimum value. In case of a tie, the type with 142*d9f75844SAndroid Build Coastguard Worker // the lowest maximum value. In case that too is a tie, the types have the 143*d9f75844SAndroid Build Coastguard Worker // same range, and we arbitrarily pick T1. 144*d9f75844SAndroid Build Coastguard Worker using min_t = typename std::conditional< 145*d9f75844SAndroid Build Coastguard Worker SafeLt(Limits<T1>::lowest, Limits<T2>::lowest), 146*d9f75844SAndroid Build Coastguard Worker T1, 147*d9f75844SAndroid Build Coastguard Worker typename std::conditional< 148*d9f75844SAndroid Build Coastguard Worker SafeGt(Limits<T1>::lowest, Limits<T2>::lowest), 149*d9f75844SAndroid Build Coastguard Worker T2, 150*d9f75844SAndroid Build Coastguard Worker typename std::conditional<SafeLe(Limits<T1>::max, Limits<T2>::max), 151*d9f75844SAndroid Build Coastguard Worker T1, 152*d9f75844SAndroid Build Coastguard Worker T2>::type>::type>::type; 153*d9f75844SAndroid Build Coastguard Worker static_assert(std::is_same<min_t, T1>::value || 154*d9f75844SAndroid Build Coastguard Worker std::is_same<min_t, T2>::value, 155*d9f75844SAndroid Build Coastguard Worker ""); 156*d9f75844SAndroid Build Coastguard Worker 157*d9f75844SAndroid Build Coastguard Worker // The type with the highest maximum value. In case of a tie, the types have 158*d9f75844SAndroid Build Coastguard Worker // the same range (because in C++, integer types with the same maximum also 159*d9f75844SAndroid Build Coastguard Worker // have the same minimum). 160*d9f75844SAndroid Build Coastguard Worker static_assert(SafeNe(Limits<T1>::max, Limits<T2>::max) || 161*d9f75844SAndroid Build Coastguard Worker SafeEq(Limits<T1>::lowest, Limits<T2>::lowest), 162*d9f75844SAndroid Build Coastguard Worker "integer types with the same max should have the same min"); 163*d9f75844SAndroid Build Coastguard Worker using max_t = typename std:: 164*d9f75844SAndroid Build Coastguard Worker conditional<SafeGe(Limits<T1>::max, Limits<T2>::max), T1, T2>::type; 165*d9f75844SAndroid Build Coastguard Worker static_assert(std::is_same<max_t, T1>::value || 166*d9f75844SAndroid Build Coastguard Worker std::is_same<max_t, T2>::value, 167*d9f75844SAndroid Build Coastguard Worker ""); 168*d9f75844SAndroid Build Coastguard Worker }; 169*d9f75844SAndroid Build Coastguard Worker 170*d9f75844SAndroid Build Coastguard Worker // A dummy type that we pass around at compile time but never actually use. 171*d9f75844SAndroid Build Coastguard Worker // Declared but not defined. 172*d9f75844SAndroid Build Coastguard Worker struct DefaultType; 173*d9f75844SAndroid Build Coastguard Worker 174*d9f75844SAndroid Build Coastguard Worker // ::type is A, except we fall back to B if A is DefaultType. We static_assert 175*d9f75844SAndroid Build Coastguard Worker // that the chosen type can hold all values that B can hold. 176*d9f75844SAndroid Build Coastguard Worker template <typename A, typename B> 177*d9f75844SAndroid Build Coastguard Worker struct TypeOr { 178*d9f75844SAndroid Build Coastguard Worker using type = typename std:: 179*d9f75844SAndroid Build Coastguard Worker conditional<std::is_same<A, DefaultType>::value, B, A>::type; 180*d9f75844SAndroid Build Coastguard Worker static_assert(SafeLe(Limits<type>::lowest, Limits<B>::lowest) && 181*d9f75844SAndroid Build Coastguard Worker SafeGe(Limits<type>::max, Limits<B>::max), 182*d9f75844SAndroid Build Coastguard Worker "The specified type isn't large enough"); 183*d9f75844SAndroid Build Coastguard Worker static_assert(IsIntlike<type>::value == IsIntlike<B>::value && 184*d9f75844SAndroid Build Coastguard Worker std::is_floating_point<type>::value == 185*d9f75844SAndroid Build Coastguard Worker std::is_floating_point<type>::value, 186*d9f75844SAndroid Build Coastguard Worker "float<->int conversions not allowed"); 187*d9f75844SAndroid Build Coastguard Worker }; 188*d9f75844SAndroid Build Coastguard Worker 189*d9f75844SAndroid Build Coastguard Worker } // namespace safe_minmax_impl 190*d9f75844SAndroid Build Coastguard Worker 191*d9f75844SAndroid Build Coastguard Worker template < 192*d9f75844SAndroid Build Coastguard Worker typename R = safe_minmax_impl::DefaultType, 193*d9f75844SAndroid Build Coastguard Worker typename T1 = safe_minmax_impl::DefaultType, 194*d9f75844SAndroid Build Coastguard Worker typename T2 = safe_minmax_impl::DefaultType, 195*d9f75844SAndroid Build Coastguard Worker typename R2 = typename safe_minmax_impl::TypeOr< 196*d9f75844SAndroid Build Coastguard Worker R, 197*d9f75844SAndroid Build Coastguard Worker typename safe_minmax_impl::MType< 198*d9f75844SAndroid Build Coastguard Worker typename safe_minmax_impl::UnderlyingType<T1>::type, 199*d9f75844SAndroid Build Coastguard Worker typename safe_minmax_impl::UnderlyingType<T2>::type>::min_t>::type> 200*d9f75844SAndroid Build Coastguard Worker constexpr R2 SafeMin(T1 a, T2 b) { 201*d9f75844SAndroid Build Coastguard Worker static_assert(IsIntlike<T1>::value || std::is_floating_point<T1>::value, 202*d9f75844SAndroid Build Coastguard Worker "The first argument must be integral or floating-point"); 203*d9f75844SAndroid Build Coastguard Worker static_assert(IsIntlike<T2>::value || std::is_floating_point<T2>::value, 204*d9f75844SAndroid Build Coastguard Worker "The second argument must be integral or floating-point"); 205*d9f75844SAndroid Build Coastguard Worker return SafeLt(a, b) ? static_cast<R2>(a) : static_cast<R2>(b); 206*d9f75844SAndroid Build Coastguard Worker } 207*d9f75844SAndroid Build Coastguard Worker 208*d9f75844SAndroid Build Coastguard Worker template < 209*d9f75844SAndroid Build Coastguard Worker typename R = safe_minmax_impl::DefaultType, 210*d9f75844SAndroid Build Coastguard Worker typename T1 = safe_minmax_impl::DefaultType, 211*d9f75844SAndroid Build Coastguard Worker typename T2 = safe_minmax_impl::DefaultType, 212*d9f75844SAndroid Build Coastguard Worker typename R2 = typename safe_minmax_impl::TypeOr< 213*d9f75844SAndroid Build Coastguard Worker R, 214*d9f75844SAndroid Build Coastguard Worker typename safe_minmax_impl::MType< 215*d9f75844SAndroid Build Coastguard Worker typename safe_minmax_impl::UnderlyingType<T1>::type, 216*d9f75844SAndroid Build Coastguard Worker typename safe_minmax_impl::UnderlyingType<T2>::type>::max_t>::type> 217*d9f75844SAndroid Build Coastguard Worker constexpr R2 SafeMax(T1 a, T2 b) { 218*d9f75844SAndroid Build Coastguard Worker static_assert(IsIntlike<T1>::value || std::is_floating_point<T1>::value, 219*d9f75844SAndroid Build Coastguard Worker "The first argument must be integral or floating-point"); 220*d9f75844SAndroid Build Coastguard Worker static_assert(IsIntlike<T2>::value || std::is_floating_point<T2>::value, 221*d9f75844SAndroid Build Coastguard Worker "The second argument must be integral or floating-point"); 222*d9f75844SAndroid Build Coastguard Worker return SafeGt(a, b) ? static_cast<R2>(a) : static_cast<R2>(b); 223*d9f75844SAndroid Build Coastguard Worker } 224*d9f75844SAndroid Build Coastguard Worker 225*d9f75844SAndroid Build Coastguard Worker namespace safe_minmax_impl { 226*d9f75844SAndroid Build Coastguard Worker 227*d9f75844SAndroid Build Coastguard Worker // Given three types T, L, and H, let ::type be a suitable return value for 228*d9f75844SAndroid Build Coastguard Worker // SafeClamp(T, L, H). See the docs at the top of this file for details. 229*d9f75844SAndroid Build Coastguard Worker template <typename T, 230*d9f75844SAndroid Build Coastguard Worker typename L, 231*d9f75844SAndroid Build Coastguard Worker typename H, 232*d9f75844SAndroid Build Coastguard Worker bool int1 = IsIntlike<T>::value, 233*d9f75844SAndroid Build Coastguard Worker bool int2 = IsIntlike<L>::value, 234*d9f75844SAndroid Build Coastguard Worker bool int3 = IsIntlike<H>::value> 235*d9f75844SAndroid Build Coastguard Worker struct ClampType { 236*d9f75844SAndroid Build Coastguard Worker static_assert(int1 == int2 && int1 == int3, 237*d9f75844SAndroid Build Coastguard Worker "You may not mix integral and floating-point arguments"); 238*d9f75844SAndroid Build Coastguard Worker }; 239*d9f75844SAndroid Build Coastguard Worker 240*d9f75844SAndroid Build Coastguard Worker // Specialization for when all three types are floating-point. 241*d9f75844SAndroid Build Coastguard Worker template <typename T, typename L, typename H> 242*d9f75844SAndroid Build Coastguard Worker struct ClampType<T, L, H, false, false, false> { 243*d9f75844SAndroid Build Coastguard Worker using type = typename std::common_type<T, L, H>::type; 244*d9f75844SAndroid Build Coastguard Worker }; 245*d9f75844SAndroid Build Coastguard Worker 246*d9f75844SAndroid Build Coastguard Worker // Specialization for when all three types are integral. 247*d9f75844SAndroid Build Coastguard Worker template <typename T, typename L, typename H> 248*d9f75844SAndroid Build Coastguard Worker struct ClampType<T, L, H, true, true, true> { 249*d9f75844SAndroid Build Coastguard Worker private: 250*d9f75844SAndroid Build Coastguard Worker // Range of the return value. The return type must be able to represent this 251*d9f75844SAndroid Build Coastguard Worker // full range. 252*d9f75844SAndroid Build Coastguard Worker static constexpr auto r_min = 253*d9f75844SAndroid Build Coastguard Worker SafeMax(Limits<L>::lowest, SafeMin(Limits<H>::lowest, Limits<T>::lowest)); 254*d9f75844SAndroid Build Coastguard Worker static constexpr auto r_max = 255*d9f75844SAndroid Build Coastguard Worker SafeMin(Limits<H>::max, SafeMax(Limits<L>::max, Limits<T>::max)); 256*d9f75844SAndroid Build Coastguard Worker 257*d9f75844SAndroid Build Coastguard Worker // Is the given type an acceptable return type? (That is, can it represent 258*d9f75844SAndroid Build Coastguard Worker // all possible return values, and is it no larger than the largest of the 259*d9f75844SAndroid Build Coastguard Worker // input types?) 260*d9f75844SAndroid Build Coastguard Worker template <typename A> 261*d9f75844SAndroid Build Coastguard Worker struct AcceptableType { 262*d9f75844SAndroid Build Coastguard Worker private: 263*d9f75844SAndroid Build Coastguard Worker static constexpr bool not_too_large = sizeof(A) <= sizeof(L) || 264*d9f75844SAndroid Build Coastguard Worker sizeof(A) <= sizeof(H) || 265*d9f75844SAndroid Build Coastguard Worker sizeof(A) <= sizeof(T); 266*d9f75844SAndroid Build Coastguard Worker static constexpr bool range_contained = 267*d9f75844SAndroid Build Coastguard Worker SafeLe(Limits<A>::lowest, r_min) && SafeLe(r_max, Limits<A>::max); 268*d9f75844SAndroid Build Coastguard Worker 269*d9f75844SAndroid Build Coastguard Worker public: 270*d9f75844SAndroid Build Coastguard Worker static constexpr bool value = not_too_large && range_contained; 271*d9f75844SAndroid Build Coastguard Worker }; 272*d9f75844SAndroid Build Coastguard Worker 273*d9f75844SAndroid Build Coastguard Worker using best_signed_type = typename std::conditional< 274*d9f75844SAndroid Build Coastguard Worker AcceptableType<int8_t>::value, 275*d9f75844SAndroid Build Coastguard Worker int8_t, 276*d9f75844SAndroid Build Coastguard Worker typename std::conditional< 277*d9f75844SAndroid Build Coastguard Worker AcceptableType<int16_t>::value, 278*d9f75844SAndroid Build Coastguard Worker int16_t, 279*d9f75844SAndroid Build Coastguard Worker typename std::conditional<AcceptableType<int32_t>::value, 280*d9f75844SAndroid Build Coastguard Worker int32_t, 281*d9f75844SAndroid Build Coastguard Worker int64_t>::type>::type>::type; 282*d9f75844SAndroid Build Coastguard Worker 283*d9f75844SAndroid Build Coastguard Worker using best_unsigned_type = typename std::conditional< 284*d9f75844SAndroid Build Coastguard Worker AcceptableType<uint8_t>::value, 285*d9f75844SAndroid Build Coastguard Worker uint8_t, 286*d9f75844SAndroid Build Coastguard Worker typename std::conditional< 287*d9f75844SAndroid Build Coastguard Worker AcceptableType<uint16_t>::value, 288*d9f75844SAndroid Build Coastguard Worker uint16_t, 289*d9f75844SAndroid Build Coastguard Worker typename std::conditional<AcceptableType<uint32_t>::value, 290*d9f75844SAndroid Build Coastguard Worker uint32_t, 291*d9f75844SAndroid Build Coastguard Worker uint64_t>::type>::type>::type; 292*d9f75844SAndroid Build Coastguard Worker 293*d9f75844SAndroid Build Coastguard Worker public: 294*d9f75844SAndroid Build Coastguard Worker // Pick the best type, preferring the same signedness as T but falling back 295*d9f75844SAndroid Build Coastguard Worker // to the other one if necessary. 296*d9f75844SAndroid Build Coastguard Worker using type = typename std::conditional< 297*d9f75844SAndroid Build Coastguard Worker std::is_signed<T>::value, 298*d9f75844SAndroid Build Coastguard Worker typename std::conditional<AcceptableType<best_signed_type>::value, 299*d9f75844SAndroid Build Coastguard Worker best_signed_type, 300*d9f75844SAndroid Build Coastguard Worker best_unsigned_type>::type, 301*d9f75844SAndroid Build Coastguard Worker typename std::conditional<AcceptableType<best_unsigned_type>::value, 302*d9f75844SAndroid Build Coastguard Worker best_unsigned_type, 303*d9f75844SAndroid Build Coastguard Worker best_signed_type>::type>::type; 304*d9f75844SAndroid Build Coastguard Worker static_assert(AcceptableType<type>::value, ""); 305*d9f75844SAndroid Build Coastguard Worker }; 306*d9f75844SAndroid Build Coastguard Worker 307*d9f75844SAndroid Build Coastguard Worker } // namespace safe_minmax_impl 308*d9f75844SAndroid Build Coastguard Worker 309*d9f75844SAndroid Build Coastguard Worker template < 310*d9f75844SAndroid Build Coastguard Worker typename R = safe_minmax_impl::DefaultType, 311*d9f75844SAndroid Build Coastguard Worker typename T = safe_minmax_impl::DefaultType, 312*d9f75844SAndroid Build Coastguard Worker typename L = safe_minmax_impl::DefaultType, 313*d9f75844SAndroid Build Coastguard Worker typename H = safe_minmax_impl::DefaultType, 314*d9f75844SAndroid Build Coastguard Worker typename R2 = typename safe_minmax_impl::TypeOr< 315*d9f75844SAndroid Build Coastguard Worker R, 316*d9f75844SAndroid Build Coastguard Worker typename safe_minmax_impl::ClampType< 317*d9f75844SAndroid Build Coastguard Worker typename safe_minmax_impl::UnderlyingType<T>::type, 318*d9f75844SAndroid Build Coastguard Worker typename safe_minmax_impl::UnderlyingType<L>::type, 319*d9f75844SAndroid Build Coastguard Worker typename safe_minmax_impl::UnderlyingType<H>::type>::type>::type> 320*d9f75844SAndroid Build Coastguard Worker R2 SafeClamp(T x, L min, H max) { 321*d9f75844SAndroid Build Coastguard Worker static_assert(IsIntlike<H>::value || std::is_floating_point<H>::value, 322*d9f75844SAndroid Build Coastguard Worker "The first argument must be integral or floating-point"); 323*d9f75844SAndroid Build Coastguard Worker static_assert(IsIntlike<T>::value || std::is_floating_point<T>::value, 324*d9f75844SAndroid Build Coastguard Worker "The second argument must be integral or floating-point"); 325*d9f75844SAndroid Build Coastguard Worker static_assert(IsIntlike<L>::value || std::is_floating_point<L>::value, 326*d9f75844SAndroid Build Coastguard Worker "The third argument must be integral or floating-point"); 327*d9f75844SAndroid Build Coastguard Worker RTC_DCHECK_LE(min, max); 328*d9f75844SAndroid Build Coastguard Worker return SafeLe(x, min) 329*d9f75844SAndroid Build Coastguard Worker ? static_cast<R2>(min) 330*d9f75844SAndroid Build Coastguard Worker : SafeGe(x, max) ? static_cast<R2>(max) : static_cast<R2>(x); 331*d9f75844SAndroid Build Coastguard Worker } 332*d9f75844SAndroid Build Coastguard Worker 333*d9f75844SAndroid Build Coastguard Worker } // namespace rtc 334*d9f75844SAndroid Build Coastguard Worker 335*d9f75844SAndroid Build Coastguard Worker #endif // RTC_BASE_NUMERICS_SAFE_MINMAX_H_ 336