| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
#ifndef BASE_NUMERICS_SAFE_CONVERSIONS_H_ |
| 6 |
#define BASE_NUMERICS_SAFE_CONVERSIONS_H_ |
| 7 |
|
| 8 |
#include <stddef.h> |
| 9 |
|
| 10 |
#include <cmath> |
| 11 |
#include <limits> |
| 12 |
#include <type_traits> |
| 13 |
|
| 14 |
#include "safe_conversions_impl.h" |
| 15 |
|
| 16 |
#if defined(__ARMEL__) && !defined(__native_client__) |
| 17 |
#include "safe_conversions_arm_impl.h" |
| 18 |
#define BASE_HAS_OPTIMIZED_SAFE_CONVERSIONS (1) |
| 19 |
#else |
| 20 |
#define BASE_HAS_OPTIMIZED_SAFE_CONVERSIONS (0) |
| 21 |
#endif |
| 22 |
|
| 23 |
#if !BASE_NUMERICS_DISABLE_OSTREAM_OPERATORS |
| 24 |
#include <ostream> |
| 25 |
#endif |
| 26 |
|
| 27 |
namespace chromium { |
| 28 |
namespace base { |
| 29 |
namespace internal { |
| 30 |
|
| 31 |
#if !BASE_HAS_OPTIMIZED_SAFE_CONVERSIONS |
| 32 |
template <typename Dst, typename Src> |
| 33 |
struct SaturateFastAsmOp { |
| 34 |
static constexpr bool is_supported = false; |
| 35 |
static constexpr Dst Do(Src) { |
| 36 |
|
| 37 |
return CheckOnFailure::template HandleFailure<Dst>(); |
| 38 |
} |
| 39 |
}; |
| 40 |
#endif |
| 41 |
#undef BASE_HAS_OPTIMIZED_SAFE_CONVERSIONS |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
template <typename Dst, typename Src, typename Enable = void> |
| 46 |
struct IsValueInRangeFastOp { |
| 47 |
static constexpr bool is_supported = false; |
| 48 |
static constexpr bool Do(Src value) { |
| 49 |
(void)value; |
| 50 |
|
| 51 |
return CheckOnFailure::template HandleFailure<bool>(); |
| 52 |
} |
| 53 |
}; |
| 54 |
|
| 55 |
|
| 56 |
template <typename Dst, typename Src> |
| 57 |
struct IsValueInRangeFastOp< |
| 58 |
Dst, |
| 59 |
Src, |
| 60 |
typename std::enable_if< |
| 61 |
std::is_integral<Dst>::value && std::is_integral<Src>::value && |
| 62 |
std::is_signed<Dst>::value && std::is_signed<Src>::value && |
| 63 |
!IsTypeInRangeForNumericType<Dst, Src>::value>::type> { |
| 64 |
static constexpr bool is_supported = true; |
| 65 |
|
| 66 |
static constexpr bool Do(Src value) { |
| 67 |
|
| 68 |
|
| 69 |
return value == static_cast<Dst>(value); |
| 70 |
} |
| 71 |
}; |
| 72 |
|
| 73 |
|
| 74 |
template <typename Dst, typename Src> |
| 75 |
struct IsValueInRangeFastOp< |
| 76 |
Dst, |
| 77 |
Src, |
| 78 |
typename std::enable_if< |
| 79 |
std::is_integral<Dst>::value && std::is_integral<Src>::value && |
| 80 |
!std::is_signed<Dst>::value && std::is_signed<Src>::value && |
| 81 |
!IsTypeInRangeForNumericType<Dst, Src>::value>::type> { |
| 82 |
static constexpr bool is_supported = true; |
| 83 |
|
| 84 |
static constexpr bool Do(Src value) { |
| 85 |
|
| 86 |
|
| 87 |
return as_unsigned(value) <= as_unsigned(CommonMax<Src, Dst>()); |
| 88 |
} |
| 89 |
}; |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
template <typename Dst, typename Src> |
| 94 |
constexpr bool IsValueInRangeForNumericType(Src value) { |
| 95 |
using SrcType = typename internal::UnderlyingType<Src>::type; |
| 96 |
return internal::IsValueInRangeFastOp<Dst, SrcType>::is_supported |
| 97 |
? internal::IsValueInRangeFastOp<Dst, SrcType>::Do( |
| 98 |
static_cast<SrcType>(value)) |
| 99 |
: internal::DstRangeRelationToSrcRange<Dst>( |
| 100 |
static_cast<SrcType>(value)) |
| 101 |
.IsValid(); |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
template <typename Dst, |
| 108 |
class CheckHandler = internal::CheckOnFailure, |
| 109 |
typename Src> |
| 110 |
constexpr Dst checked_cast(Src value) { |
| 111 |
|
| 112 |
|
| 113 |
using SrcType = typename internal::UnderlyingType<Src>::type; |
| 114 |
return BASE_NUMERICS_LIKELY((IsValueInRangeForNumericType<Dst>(value))) |
| 115 |
? static_cast<Dst>(static_cast<SrcType>(value)) |
| 116 |
: CheckHandler::template HandleFailure<Dst>(); |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
template <typename T> |
| 123 |
struct SaturationDefaultLimits : public std::numeric_limits<T> { |
| 124 |
static constexpr T NaN() { |
| 125 |
if constexpr (std::numeric_limits<T>::has_quiet_NaN) { |
| 126 |
return std::numeric_limits<T>::quiet_NaN(); |
| 127 |
} else { |
| 128 |
return T(); |
| 129 |
} |
| 130 |
} |
| 131 |
using std::numeric_limits<T>::max; |
| 132 |
static constexpr T Overflow() { |
| 133 |
if constexpr (std::numeric_limits<T>::has_infinity) { |
| 134 |
return std::numeric_limits<T>::infinity(); |
| 135 |
} else { |
| 136 |
return std::numeric_limits<T>::max(); |
| 137 |
} |
| 138 |
} |
| 139 |
using std::numeric_limits<T>::lowest; |
| 140 |
static constexpr T Underflow() { |
| 141 |
if constexpr (std::numeric_limits<T>::has_infinity) { |
| 142 |
return std::numeric_limits<T>::infinity() * -1; |
| 143 |
} else { |
| 144 |
return std::numeric_limits<T>::lowest(); |
| 145 |
} |
| 146 |
} |
| 147 |
}; |
| 148 |
|
| 149 |
template <typename Dst, template <typename> class S, typename Src> |
| 150 |
constexpr Dst saturated_cast_impl(Src value, RangeCheck constraint) { |
| 151 |
|
| 152 |
|
| 153 |
return !constraint.IsOverflowFlagSet() |
| 154 |
? (!constraint.IsUnderflowFlagSet() ? static_cast<Dst>(value) |
| 155 |
: S<Dst>::Underflow()) |
| 156 |
|
| 157 |
: (std::is_integral<Src>::value || !constraint.IsUnderflowFlagSet() |
| 158 |
? S<Dst>::Overflow() |
| 159 |
: S<Dst>::NaN()); |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
template <typename Dst, typename Src, typename Enable = void> |
| 166 |
struct SaturateFastOp { |
| 167 |
static constexpr bool is_supported = false; |
| 168 |
static constexpr Dst Do(Src value) { |
| 169 |
(void)value; |
| 170 |
|
| 171 |
return CheckOnFailure::template HandleFailure<Dst>(); |
| 172 |
} |
| 173 |
}; |
| 174 |
|
| 175 |
template <typename Dst, typename Src> |
| 176 |
struct SaturateFastOp< |
| 177 |
Dst, |
| 178 |
Src, |
| 179 |
typename std::enable_if<std::is_integral<Src>::value && |
| 180 |
std::is_integral<Dst>::value && |
| 181 |
SaturateFastAsmOp<Dst, Src>::is_supported>::type> { |
| 182 |
static constexpr bool is_supported = true; |
| 183 |
static constexpr Dst Do(Src value) { |
| 184 |
return SaturateFastAsmOp<Dst, Src>::Do(value); |
| 185 |
} |
| 186 |
}; |
| 187 |
|
| 188 |
template <typename Dst, typename Src> |
| 189 |
struct SaturateFastOp< |
| 190 |
Dst, |
| 191 |
Src, |
| 192 |
typename std::enable_if<std::is_integral<Src>::value && |
| 193 |
std::is_integral<Dst>::value && |
| 194 |
!SaturateFastAsmOp<Dst, Src>::is_supported>::type> { |
| 195 |
static constexpr bool is_supported = true; |
| 196 |
static constexpr Dst Do(Src value) { |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
const Dst saturated = CommonMaxOrMin<Dst, Src>( |
| 201 |
IsMaxInRangeForNumericType<Dst, Src>() || |
| 202 |
(!IsMinInRangeForNumericType<Dst, Src>() && IsValueNegative(value))); |
| 203 |
return BASE_NUMERICS_LIKELY(IsValueInRangeForNumericType<Dst>(value)) |
| 204 |
? static_cast<Dst>(value) |
| 205 |
: saturated; |
| 206 |
} |
| 207 |
}; |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
template <typename Dst, |
| 214 |
template <typename> class SaturationHandler = SaturationDefaultLimits, |
| 215 |
typename Src> |
| 216 |
constexpr Dst saturated_cast(Src value) { |
| 217 |
using SrcType = typename UnderlyingType<Src>::type; |
| 218 |
return !IsConstantEvaluated() && SaturateFastOp<Dst, SrcType>::is_supported && |
| 219 |
std::is_same<SaturationHandler<Dst>, |
| 220 |
SaturationDefaultLimits<Dst>>::value |
| 221 |
? SaturateFastOp<Dst, SrcType>::Do(static_cast<SrcType>(value)) |
| 222 |
: saturated_cast_impl<Dst, SaturationHandler, SrcType>( |
| 223 |
static_cast<SrcType>(value), |
| 224 |
DstRangeRelationToSrcRange<Dst, SaturationHandler, SrcType>( |
| 225 |
static_cast<SrcType>(value))); |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
template <typename Dst, typename Src> |
| 232 |
constexpr Dst strict_cast(Src value) { |
| 233 |
using SrcType = typename UnderlyingType<Src>::type; |
| 234 |
static_assert(UnderlyingType<Src>::is_numeric, "Argument must be numeric."); |
| 235 |
static_assert(std::is_arithmetic<Dst>::value, "Result must be numeric."); |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
static_assert(StaticDstRangeRelationToSrcRange<Dst, SrcType>::value == |
| 244 |
NUMERIC_RANGE_CONTAINED, |
| 245 |
"The source type is out of range for the destination type. " |
| 246 |
"Please see strict_cast<> comments for more information."); |
| 247 |
|
| 248 |
return static_cast<Dst>(static_cast<SrcType>(value)); |
| 249 |
} |
| 250 |
|
| 251 |
|
| 252 |
template <typename Dst, typename Src, class Enable = void> |
| 253 |
struct IsNumericRangeContained { |
| 254 |
static constexpr bool value = false; |
| 255 |
}; |
| 256 |
|
| 257 |
template <typename Dst, typename Src> |
| 258 |
struct IsNumericRangeContained< |
| 259 |
Dst, |
| 260 |
Src, |
| 261 |
typename std::enable_if<ArithmeticOrUnderlyingEnum<Dst>::value && |
| 262 |
ArithmeticOrUnderlyingEnum<Src>::value>::type> { |
| 263 |
static constexpr bool value = |
| 264 |
StaticDstRangeRelationToSrcRange<Dst, Src>::value == |
| 265 |
NUMERIC_RANGE_CONTAINED; |
| 266 |
}; |
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
template <typename T> |
| 279 |
class StrictNumeric { |
| 280 |
public: |
| 281 |
using type = T; |
| 282 |
|
| 283 |
constexpr StrictNumeric() : value_(0) {} |
| 284 |
|
| 285 |
|
| 286 |
template <typename Src> |
| 287 |
constexpr StrictNumeric(const StrictNumeric<Src>& rhs) |
| 288 |
: value_(strict_cast<T>(rhs.value_)) {} |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
constexpr StrictNumeric(T value) : value_(value) {} |
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
template <typename Src> |
| 299 |
|
| 300 |
constexpr StrictNumeric(Src value) : value_(strict_cast<T>(value)) {} |
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
template <typename Dst, |
| 315 |
typename std::enable_if< |
| 316 |
IsNumericRangeContained<Dst, T>::value>::type* = nullptr> |
| 317 |
constexpr operator Dst() const { |
| 318 |
return static_cast<typename ArithmeticOrUnderlyingEnum<Dst>::type>(value_); |
| 319 |
} |
| 320 |
|
| 321 |
private: |
| 322 |
const T value_; |
| 323 |
}; |
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
template <typename T> |
| 328 |
constexpr StrictNumeric<typename UnderlyingType<T>::type> MakeStrictNum( |
| 329 |
const T value) { |
| 330 |
return value; |
| 331 |
} |
| 332 |
|
| 333 |
#define BASE_NUMERIC_COMPARISON_OPERATORS(CLASS, NAME, OP) \ |
| 334 |
template <typename L, typename R, \ |
| 335 |
typename std::enable_if< \ |
| 336 |
internal::Is##CLASS##Op<L, R>::value>::type* = nullptr> \ |
| 337 |
constexpr bool operator OP(const L lhs, const R rhs) { \ |
| 338 |
return SafeCompare<NAME, typename UnderlyingType<L>::type, \ |
| 339 |
typename UnderlyingType<R>::type>(lhs, rhs); \ |
| 340 |
} |
| 341 |
|
| 342 |
BASE_NUMERIC_COMPARISON_OPERATORS(Strict, IsLess, <) |
| 343 |
BASE_NUMERIC_COMPARISON_OPERATORS(Strict, IsLessOrEqual, <=) |
| 344 |
BASE_NUMERIC_COMPARISON_OPERATORS(Strict, IsGreater, >) |
| 345 |
BASE_NUMERIC_COMPARISON_OPERATORS(Strict, IsGreaterOrEqual, >=) |
| 346 |
BASE_NUMERIC_COMPARISON_OPERATORS(Strict, IsEqual, ==) |
| 347 |
BASE_NUMERIC_COMPARISON_OPERATORS(Strict, IsNotEqual, !=) |
| 348 |
|
| 349 |
} |
| 350 |
|
| 351 |
using internal::as_signed; |
| 352 |
using internal::as_unsigned; |
| 353 |
using internal::checked_cast; |
| 354 |
using internal::IsTypeInRangeForNumericType; |
| 355 |
using internal::IsValueInRangeForNumericType; |
| 356 |
using internal::IsValueNegative; |
| 357 |
using internal::MakeStrictNum; |
| 358 |
using internal::SafeUnsignedAbs; |
| 359 |
using internal::saturated_cast; |
| 360 |
using internal::strict_cast; |
| 361 |
using internal::StrictNumeric; |
| 362 |
|
| 363 |
|
| 364 |
using SizeT = StrictNumeric<size_t>; |
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
template <typename Dst = int, |
| 370 |
typename Src, |
| 371 |
typename = std::enable_if_t<std::is_integral<Dst>::value && |
| 372 |
std::is_floating_point<Src>::value>> |
| 373 |
Dst ClampFloor(Src value) { |
| 374 |
return saturated_cast<Dst>(std::floor(value)); |
| 375 |
} |
| 376 |
template <typename Dst = int, |
| 377 |
typename Src, |
| 378 |
typename = std::enable_if_t<std::is_integral<Dst>::value && |
| 379 |
std::is_floating_point<Src>::value>> |
| 380 |
Dst ClampCeil(Src value) { |
| 381 |
return saturated_cast<Dst>(std::ceil(value)); |
| 382 |
} |
| 383 |
template <typename Dst = int, |
| 384 |
typename Src, |
| 385 |
typename = std::enable_if_t<std::is_integral<Dst>::value && |
| 386 |
std::is_floating_point<Src>::value>> |
| 387 |
Dst ClampRound(Src value) { |
| 388 |
const Src rounded = |
| 389 |
(value >= 0.0f) ? std::floor(value + 0.5f) : std::ceil(value - 0.5f); |
| 390 |
return saturated_cast<Dst>(rounded); |
| 391 |
} |
| 392 |
|
| 393 |
} |
| 394 |
} |
| 395 |
|
| 396 |
#endif |