| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
#ifndef RTC_BASE_NUMERICS_SAFE_MINMAX_H_ |
| 77 |
#define RTC_BASE_NUMERICS_SAFE_MINMAX_H_ |
| 78 |
|
| 79 |
#include <limits> |
| 80 |
#include <type_traits> |
| 81 |
|
| 82 |
#include "rtc_base/checks.h" |
| 83 |
#include "rtc_base/numerics/safe_compare.h" |
| 84 |
#include "rtc_base/type_traits.h" |
| 85 |
|
| 86 |
namespace rtc { |
| 87 |
|
| 88 |
namespace safe_minmax_impl { |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
template <typename T> |
| 94 |
struct Limits { |
| 95 |
static constexpr T lowest = std::numeric_limits<T>::lowest(); |
| 96 |
static constexpr T max = std::numeric_limits<T>::max(); |
| 97 |
}; |
| 98 |
|
| 99 |
template <typename T, bool is_enum = std::is_enum<T>::value> |
| 100 |
struct UnderlyingType; |
| 101 |
|
| 102 |
template <typename T> |
| 103 |
struct UnderlyingType<T, false> { |
| 104 |
using type = T; |
| 105 |
}; |
| 106 |
|
| 107 |
template <typename T> |
| 108 |
struct UnderlyingType<T, true> { |
| 109 |
using type = typename std::underlying_type<T>::type; |
| 110 |
}; |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
template <typename T1, |
| 115 |
typename T2, |
| 116 |
bool int1 = IsIntlike<T1>::value, |
| 117 |
bool int2 = IsIntlike<T2>::value> |
| 118 |
struct MType { |
| 119 |
static_assert(int1 == int2, |
| 120 |
"You may not mix integral and floating-point arguments"); |
| 121 |
}; |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
template <typename T1, typename T2> |
| 126 |
struct MType<T1, T2, false, false> { |
| 127 |
using min_t = typename std::common_type<T1, T2>::type; |
| 128 |
static_assert(std::is_same<min_t, T1>::value || |
| 129 |
std::is_same<min_t, T2>::value, |
| 130 |
""); |
| 131 |
|
| 132 |
using max_t = typename std::common_type<T1, T2>::type; |
| 133 |
static_assert(std::is_same<max_t, T1>::value || |
| 134 |
std::is_same<max_t, T2>::value, |
| 135 |
""); |
| 136 |
}; |
| 137 |
|
| 138 |
|
| 139 |
template <typename T1, typename T2> |
| 140 |
struct MType<T1, T2, true, true> { |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
using min_t = typename std::conditional< |
| 145 |
SafeLt(Limits<T1>::lowest, Limits<T2>::lowest), |
| 146 |
T1, |
| 147 |
typename std::conditional< |
| 148 |
SafeGt(Limits<T1>::lowest, Limits<T2>::lowest), |
| 149 |
T2, |
| 150 |
typename std::conditional<SafeLe(Limits<T1>::max, Limits<T2>::max), |
| 151 |
T1, |
| 152 |
T2>::type>::type>::type; |
| 153 |
static_assert(std::is_same<min_t, T1>::value || |
| 154 |
std::is_same<min_t, T2>::value, |
| 155 |
""); |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
static_assert(SafeNe(Limits<T1>::max, Limits<T2>::max) || |
| 161 |
SafeEq(Limits<T1>::lowest, Limits<T2>::lowest), |
| 162 |
"integer types with the same max should have the same min"); |
| 163 |
using max_t = typename std:: |
| 164 |
conditional<SafeGe(Limits<T1>::max, Limits<T2>::max), T1, T2>::type; |
| 165 |
static_assert(std::is_same<max_t, T1>::value || |
| 166 |
std::is_same<max_t, T2>::value, |
| 167 |
""); |
| 168 |
}; |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
struct DefaultType; |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
template <typename A, typename B> |
| 177 |
struct TypeOr { |
| 178 |
using type = typename std:: |
| 179 |
conditional<std::is_same<A, DefaultType>::value, B, A>::type; |
| 180 |
static_assert(SafeLe(Limits<type>::lowest, Limits<B>::lowest) && |
| 181 |
SafeGe(Limits<type>::max, Limits<B>::max), |
| 182 |
"The specified type isn't large enough"); |
| 183 |
static_assert(IsIntlike<type>::value == IsIntlike<B>::value && |
| 184 |
std::is_floating_point<type>::value == |
| 185 |
std::is_floating_point<type>::value, |
| 186 |
"float<->int conversions not allowed"); |
| 187 |
}; |
| 188 |
|
| 189 |
} |
| 190 |
|
| 191 |
template < |
| 192 |
typename R = safe_minmax_impl::DefaultType, |
| 193 |
typename T1 = safe_minmax_impl::DefaultType, |
| 194 |
typename T2 = safe_minmax_impl::DefaultType, |
| 195 |
typename R2 = typename safe_minmax_impl::TypeOr< |
| 196 |
R, |
| 197 |
typename safe_minmax_impl::MType< |
| 198 |
typename safe_minmax_impl::UnderlyingType<T1>::type, |
| 199 |
typename safe_minmax_impl::UnderlyingType<T2>::type>::min_t>::type> |
| 200 |
constexpr R2 SafeMin(T1 a, T2 b) { |
| 201 |
static_assert(IsIntlike<T1>::value || std::is_floating_point<T1>::value, |
| 202 |
"The first argument must be integral or floating-point"); |
| 203 |
static_assert(IsIntlike<T2>::value || std::is_floating_point<T2>::value, |
| 204 |
"The second argument must be integral or floating-point"); |
| 205 |
return SafeLt(a, b) ? static_cast<R2>(a) : static_cast<R2>(b); |
| 206 |
} |
| 207 |
|
| 208 |
template < |
| 209 |
typename R = safe_minmax_impl::DefaultType, |
| 210 |
typename T1 = safe_minmax_impl::DefaultType, |
| 211 |
typename T2 = safe_minmax_impl::DefaultType, |
| 212 |
typename R2 = typename safe_minmax_impl::TypeOr< |
| 213 |
R, |
| 214 |
typename safe_minmax_impl::MType< |
| 215 |
typename safe_minmax_impl::UnderlyingType<T1>::type, |
| 216 |
typename safe_minmax_impl::UnderlyingType<T2>::type>::max_t>::type> |
| 217 |
constexpr R2 SafeMax(T1 a, T2 b) { |
| 218 |
static_assert(IsIntlike<T1>::value || std::is_floating_point<T1>::value, |
| 219 |
"The first argument must be integral or floating-point"); |
| 220 |
static_assert(IsIntlike<T2>::value || std::is_floating_point<T2>::value, |
| 221 |
"The second argument must be integral or floating-point"); |
| 222 |
return SafeGt(a, b) ? static_cast<R2>(a) : static_cast<R2>(b); |
| 223 |
} |
| 224 |
|
| 225 |
namespace safe_minmax_impl { |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
template <typename T, |
| 230 |
typename L, |
| 231 |
typename H, |
| 232 |
bool int1 = IsIntlike<T>::value, |
| 233 |
bool int2 = IsIntlike<L>::value, |
| 234 |
bool int3 = IsIntlike<H>::value> |
| 235 |
struct ClampType { |
| 236 |
static_assert(int1 == int2 && int1 == int3, |
| 237 |
"You may not mix integral and floating-point arguments"); |
| 238 |
}; |
| 239 |
|
| 240 |
|
| 241 |
template <typename T, typename L, typename H> |
| 242 |
struct ClampType<T, L, H, false, false, false> { |
| 243 |
using type = typename std::common_type<T, L, H>::type; |
| 244 |
}; |
| 245 |
|
| 246 |
|
| 247 |
template <typename T, typename L, typename H> |
| 248 |
struct ClampType<T, L, H, true, true, true> { |
| 249 |
private: |
| 250 |
|
| 251 |
|
| 252 |
static constexpr auto r_min = |
| 253 |
SafeMax(Limits<L>::lowest, SafeMin(Limits<H>::lowest, Limits<T>::lowest)); |
| 254 |
static constexpr auto r_max = |
| 255 |
SafeMin(Limits<H>::max, SafeMax(Limits<L>::max, Limits<T>::max)); |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
template <typename A> |
| 261 |
struct AcceptableType { |
| 262 |
private: |
| 263 |
static constexpr bool not_too_large = sizeof(A) <= sizeof(L) || |
| 264 |
sizeof(A) <= sizeof(H) || |
| 265 |
sizeof(A) <= sizeof(T); |
| 266 |
static constexpr bool range_contained = |
| 267 |
SafeLe(Limits<A>::lowest, r_min) && SafeLe(r_max, Limits<A>::max); |
| 268 |
|
| 269 |
public: |
| 270 |
static constexpr bool value = not_too_large && range_contained; |
| 271 |
}; |
| 272 |
|
| 273 |
using best_signed_type = typename std::conditional< |
| 274 |
AcceptableType<int8_t>::value, |
| 275 |
int8_t, |
| 276 |
typename std::conditional< |
| 277 |
AcceptableType<int16_t>::value, |
| 278 |
int16_t, |
| 279 |
typename std::conditional<AcceptableType<int32_t>::value, |
| 280 |
int32_t, |
| 281 |
int64_t>::type>::type>::type; |
| 282 |
|
| 283 |
using best_unsigned_type = typename std::conditional< |
| 284 |
AcceptableType<uint8_t>::value, |
| 285 |
uint8_t, |
| 286 |
typename std::conditional< |
| 287 |
AcceptableType<uint16_t>::value, |
| 288 |
uint16_t, |
| 289 |
typename std::conditional<AcceptableType<uint32_t>::value, |
| 290 |
uint32_t, |
| 291 |
uint64_t>::type>::type>::type; |
| 292 |
|
| 293 |
public: |
| 294 |
|
| 295 |
|
| 296 |
using type = typename std::conditional< |
| 297 |
std::is_signed<T>::value, |
| 298 |
typename std::conditional<AcceptableType<best_signed_type>::value, |
| 299 |
best_signed_type, |
| 300 |
best_unsigned_type>::type, |
| 301 |
typename std::conditional<AcceptableType<best_unsigned_type>::value, |
| 302 |
best_unsigned_type, |
| 303 |
best_signed_type>::type>::type; |
| 304 |
static_assert(AcceptableType<type>::value, ""); |
| 305 |
}; |
| 306 |
|
| 307 |
} |
| 308 |
|
| 309 |
template < |
| 310 |
typename R = safe_minmax_impl::DefaultType, |
| 311 |
typename T = safe_minmax_impl::DefaultType, |
| 312 |
typename L = safe_minmax_impl::DefaultType, |
| 313 |
typename H = safe_minmax_impl::DefaultType, |
| 314 |
typename R2 = typename safe_minmax_impl::TypeOr< |
| 315 |
R, |
| 316 |
typename safe_minmax_impl::ClampType< |
| 317 |
typename safe_minmax_impl::UnderlyingType<T>::type, |
| 318 |
typename safe_minmax_impl::UnderlyingType<L>::type, |
| 319 |
typename safe_minmax_impl::UnderlyingType<H>::type>::type>::type> |
| 320 |
R2 SafeClamp(T x, L min, H max) { |
| 321 |
static_assert(IsIntlike<H>::value || std::is_floating_point<H>::value, |
| 322 |
"The first argument must be integral or floating-point"); |
| 323 |
static_assert(IsIntlike<T>::value || std::is_floating_point<T>::value, |
| 324 |
"The second argument must be integral or floating-point"); |
| 325 |
static_assert(IsIntlike<L>::value || std::is_floating_point<L>::value, |
| 326 |
"The third argument must be integral or floating-point"); |
| 327 |
RTC_DCHECK_LE(min, max); |
| 328 |
return SafeLe(x, min) |
| 329 |
? static_cast<R2>(min) |
| 330 |
: SafeGe(x, max) ? static_cast<R2>(max) : static_cast<R2>(x); |
| 331 |
} |
| 332 |
|
| 333 |
} |
| 334 |
|
| 335 |
#endif |