| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
#ifndef RTC_BASE_NUMERICS_SAFE_CONVERSIONS_IMPL_H_ |
| 14 |
#define RTC_BASE_NUMERICS_SAFE_CONVERSIONS_IMPL_H_ |
| 15 |
|
| 16 |
#include <limits> |
| 17 |
|
| 18 |
namespace rtc { |
| 19 |
namespace internal { |
| 20 |
|
| 21 |
enum DstSign { DST_UNSIGNED, DST_SIGNED }; |
| 22 |
|
| 23 |
enum SrcSign { SRC_UNSIGNED, SRC_SIGNED }; |
| 24 |
|
| 25 |
enum DstRange { OVERLAPS_RANGE, CONTAINS_RANGE }; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
template <typename Dst, |
| 31 |
typename Src, |
| 32 |
DstSign IsDstSigned = |
| 33 |
std::numeric_limits<Dst>::is_signed ? DST_SIGNED : DST_UNSIGNED, |
| 34 |
SrcSign IsSrcSigned = |
| 35 |
std::numeric_limits<Src>::is_signed ? SRC_SIGNED : SRC_UNSIGNED> |
| 36 |
struct StaticRangeCheck {}; |
| 37 |
|
| 38 |
template <typename Dst, typename Src> |
| 39 |
struct StaticRangeCheck<Dst, Src, DST_SIGNED, SRC_SIGNED> { |
| 40 |
typedef std::numeric_limits<Dst> DstLimits; |
| 41 |
typedef std::numeric_limits<Src> SrcLimits; |
| 42 |
|
| 43 |
static const size_t kDstMaxExponent = |
| 44 |
DstLimits::is_iec559 ? DstLimits::max_exponent : (sizeof(Dst) * 8 - 1); |
| 45 |
static const size_t kSrcMaxExponent = |
| 46 |
SrcLimits::is_iec559 ? SrcLimits::max_exponent : (sizeof(Src) * 8 - 1); |
| 47 |
static const DstRange value = |
| 48 |
kDstMaxExponent >= kSrcMaxExponent ? CONTAINS_RANGE : OVERLAPS_RANGE; |
| 49 |
}; |
| 50 |
|
| 51 |
template <typename Dst, typename Src> |
| 52 |
struct StaticRangeCheck<Dst, Src, DST_UNSIGNED, SRC_UNSIGNED> { |
| 53 |
static const DstRange value = |
| 54 |
sizeof(Dst) >= sizeof(Src) ? CONTAINS_RANGE : OVERLAPS_RANGE; |
| 55 |
}; |
| 56 |
|
| 57 |
template <typename Dst, typename Src> |
| 58 |
struct StaticRangeCheck<Dst, Src, DST_SIGNED, SRC_UNSIGNED> { |
| 59 |
typedef std::numeric_limits<Dst> DstLimits; |
| 60 |
typedef std::numeric_limits<Src> SrcLimits; |
| 61 |
|
| 62 |
static const size_t kDstMaxExponent = |
| 63 |
DstLimits::is_iec559 ? DstLimits::max_exponent : (sizeof(Dst) * 8 - 1); |
| 64 |
static const size_t kSrcMaxExponent = sizeof(Src) * 8; |
| 65 |
static const DstRange value = |
| 66 |
kDstMaxExponent >= kSrcMaxExponent ? CONTAINS_RANGE : OVERLAPS_RANGE; |
| 67 |
}; |
| 68 |
|
| 69 |
template <typename Dst, typename Src> |
| 70 |
struct StaticRangeCheck<Dst, Src, DST_UNSIGNED, SRC_SIGNED> { |
| 71 |
static const DstRange value = OVERLAPS_RANGE; |
| 72 |
}; |
| 73 |
|
| 74 |
enum RangeCheckResult { |
| 75 |
TYPE_VALID = 0, |
| 76 |
TYPE_UNDERFLOW = 1, |
| 77 |
TYPE_OVERFLOW = 2, |
| 78 |
TYPE_INVALID = 3 |
| 79 |
}; |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
#define BASE_NUMERIC_RANGE_CHECK_RESULT(is_in_upper_bound, is_in_lower_bound) \ |
| 85 |
RangeCheckResult(((is_in_upper_bound) ? 0 : TYPE_OVERFLOW) | \ |
| 86 |
((is_in_lower_bound) ? 0 : TYPE_UNDERFLOW)) |
| 87 |
|
| 88 |
template <typename Dst, |
| 89 |
typename Src, |
| 90 |
DstSign IsDstSigned = |
| 91 |
std::numeric_limits<Dst>::is_signed ? DST_SIGNED : DST_UNSIGNED, |
| 92 |
SrcSign IsSrcSigned = |
| 93 |
std::numeric_limits<Src>::is_signed ? SRC_SIGNED : SRC_UNSIGNED, |
| 94 |
DstRange IsSrcRangeContained = StaticRangeCheck<Dst, Src>::value> |
| 95 |
struct RangeCheckImpl {}; |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
template <typename Dst, typename Src, DstSign IsDstSigned, SrcSign IsSrcSigned> |
| 103 |
struct RangeCheckImpl<Dst, Src, IsDstSigned, IsSrcSigned, CONTAINS_RANGE> { |
| 104 |
static constexpr RangeCheckResult Check(Src value) { return TYPE_VALID; } |
| 105 |
}; |
| 106 |
|
| 107 |
|
| 108 |
template <typename Dst, typename Src> |
| 109 |
struct RangeCheckImpl<Dst, Src, DST_SIGNED, SRC_SIGNED, OVERLAPS_RANGE> { |
| 110 |
static constexpr RangeCheckResult Check(Src value) { |
| 111 |
typedef std::numeric_limits<Dst> DstLimits; |
| 112 |
return DstLimits::is_iec559 |
| 113 |
? BASE_NUMERIC_RANGE_CHECK_RESULT( |
| 114 |
value <= static_cast<Src>(DstLimits::max()), |
| 115 |
value >= static_cast<Src>(DstLimits::max() * -1)) |
| 116 |
: BASE_NUMERIC_RANGE_CHECK_RESULT( |
| 117 |
value <= static_cast<Src>(DstLimits::max()), |
| 118 |
value >= static_cast<Src>(DstLimits::min())); |
| 119 |
} |
| 120 |
}; |
| 121 |
|
| 122 |
|
| 123 |
template <typename Dst, typename Src> |
| 124 |
struct RangeCheckImpl<Dst, Src, DST_UNSIGNED, SRC_UNSIGNED, OVERLAPS_RANGE> { |
| 125 |
static constexpr RangeCheckResult Check(Src value) { |
| 126 |
typedef std::numeric_limits<Dst> DstLimits; |
| 127 |
return BASE_NUMERIC_RANGE_CHECK_RESULT( |
| 128 |
value <= static_cast<Src>(DstLimits::max()), true); |
| 129 |
} |
| 130 |
}; |
| 131 |
|
| 132 |
|
| 133 |
template <typename Dst, typename Src> |
| 134 |
struct RangeCheckImpl<Dst, Src, DST_SIGNED, SRC_UNSIGNED, OVERLAPS_RANGE> { |
| 135 |
static constexpr RangeCheckResult Check(Src value) { |
| 136 |
typedef std::numeric_limits<Dst> DstLimits; |
| 137 |
return sizeof(Dst) > sizeof(Src) |
| 138 |
? TYPE_VALID |
| 139 |
: BASE_NUMERIC_RANGE_CHECK_RESULT( |
| 140 |
value <= static_cast<Src>(DstLimits::max()), true); |
| 141 |
} |
| 142 |
}; |
| 143 |
|
| 144 |
|
| 145 |
template <typename Dst, typename Src> |
| 146 |
struct RangeCheckImpl<Dst, Src, DST_UNSIGNED, SRC_SIGNED, OVERLAPS_RANGE> { |
| 147 |
typedef std::numeric_limits<Dst> DstLimits; |
| 148 |
typedef std::numeric_limits<Src> SrcLimits; |
| 149 |
|
| 150 |
static constexpr size_t DstMaxExponent() { return sizeof(Dst) * 8; } |
| 151 |
static constexpr size_t SrcMaxExponent() { |
| 152 |
return SrcLimits::is_iec559 ? SrcLimits::max_exponent |
| 153 |
: (sizeof(Src) * 8 - 1); |
| 154 |
} |
| 155 |
static constexpr RangeCheckResult Check(Src value) { |
| 156 |
return (DstMaxExponent() >= SrcMaxExponent()) |
| 157 |
? BASE_NUMERIC_RANGE_CHECK_RESULT(true, |
| 158 |
value >= static_cast<Src>(0)) |
| 159 |
: BASE_NUMERIC_RANGE_CHECK_RESULT( |
| 160 |
value <= static_cast<Src>(DstLimits::max()), |
| 161 |
value >= static_cast<Src>(0)); |
| 162 |
} |
| 163 |
}; |
| 164 |
|
| 165 |
template <typename Dst, typename Src> |
| 166 |
inline constexpr RangeCheckResult RangeCheck(Src value) { |
| 167 |
static_assert(std::numeric_limits<Src>::is_specialized, |
| 168 |
"argument must be numeric"); |
| 169 |
static_assert(std::numeric_limits<Dst>::is_specialized, |
| 170 |
"result must be numeric"); |
| 171 |
return RangeCheckImpl<Dst, Src>::Check(value); |
| 172 |
} |
| 173 |
|
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
#endif |