Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef BASE_SAFE_CONVERSIONS_IMPL_H_ | 5 #ifndef BASE_SAFE_CONVERSIONS_IMPL_H_ |
| 6 #define BASE_SAFE_CONVERSIONS_IMPL_H_ | 6 #define BASE_SAFE_CONVERSIONS_IMPL_H_ |
| 7 | 7 |
| 8 #include <limits> | 8 #include <limits> |
| 9 | 9 |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| 11 #include "base/template_util.h" | |
| 11 | 12 |
| 12 namespace base { | 13 namespace base { |
| 13 namespace internal { | 14 namespace internal { |
| 14 | 15 |
| 15 enum DstSign { | 16 using std::numeric_limits; |
| 16 DST_UNSIGNED, | 17 |
| 17 DST_SIGNED | 18 enum DstSignId { |
| 19 DST_UNSIGNED = 0, | |
| 20 DST_SIGNED = 1 | |
| 18 }; | 21 }; |
| 19 | 22 |
| 20 enum SrcSign { | 23 enum SrcSignId { |
| 21 SRC_UNSIGNED, | 24 SRC_UNSIGNED = 0, |
| 22 SRC_SIGNED | 25 SRC_SIGNED = 1 |
|
Ryan Sleevi
2014/02/10 23:42:41
Why use these enums versus a bool?
jschuh
2014/02/11 00:43:28
It makes the template instantiations easier to fol
| |
| 23 }; | 26 }; |
| 24 | 27 |
| 25 enum DstRange { | 28 enum DstRangeId { |
| 26 OVERLAPS_RANGE, | 29 OVERLAPS_RANGE, |
| 27 CONTAINS_RANGE | 30 CONTAINS_RANGE |
|
Ryan Sleevi
2014/02/10 23:42:41
Why no literals for these, compared with 19/20 and
jschuh
2014/02/11 00:43:28
Forgot.
| |
| 28 }; | 31 }; |
| 29 | 32 |
| 33 // Retrieve the max exponent for floating types and compute it for integrals. | |
| 34 template <typename NumericType> | |
| 35 struct MaxExponent { | |
| 36 static const int value = numeric_limits<NumericType>::is_iec559 ? | |
| 37 numeric_limits<NumericType>::max_exponent : | |
| 38 (sizeof(NumericType) * 8 + 1 - | |
| 39 numeric_limits<NumericType>::is_signed); | |
| 40 }; | |
| 41 | |
| 30 // Helper templates to statically determine if our destination type can contain | 42 // Helper templates to statically determine if our destination type can contain |
| 31 // all values represented by the source type. | 43 // maximum and minimum values represented by the source type. |
| 32 | 44 |
| 33 template <typename Dst, typename Src, | 45 template <typename Dst, typename Src, |
|
Ryan Sleevi
2014/02/10 23:42:41
We traditionally format template arguments as with
jschuh
2014/02/11 00:43:28
Done.
| |
| 34 DstSign IsDstSigned = std::numeric_limits<Dst>::is_signed ? | 46 DstSignId DstSign = numeric_limits<Dst>::is_signed ? |
| 35 DST_SIGNED : DST_UNSIGNED, | 47 DST_SIGNED : DST_UNSIGNED, |
| 36 SrcSign IsSrcSigned = std::numeric_limits<Src>::is_signed ? | 48 SrcSignId SrcSign = numeric_limits<Src>::is_signed ? |
| 37 SRC_SIGNED : SRC_UNSIGNED> | 49 SRC_SIGNED : SRC_UNSIGNED> |
| 38 struct StaticRangeCheck {}; | 50 struct StaticRangeCheck {}; |
| 39 | 51 |
| 52 // Both signed, narrowing. | |
| 40 template <typename Dst, typename Src> | 53 template <typename Dst, typename Src> |
| 41 struct StaticRangeCheck<Dst, Src, DST_SIGNED, SRC_SIGNED> { | 54 struct StaticRangeCheck<Dst, Src, DST_SIGNED, SRC_SIGNED> { |
| 42 typedef std::numeric_limits<Dst> DstLimits; | 55 static const DstRangeId value = MaxExponent<Dst>::value >= |
| 43 typedef std::numeric_limits<Src> SrcLimits; | 56 MaxExponent<Src>::value ? |
| 44 // Compare based on max_exponent, which we must compute for integrals. | 57 CONTAINS_RANGE : OVERLAPS_RANGE; |
| 45 static const size_t kDstMaxExponent = DstLimits::is_iec559 ? | |
| 46 DstLimits::max_exponent : | |
| 47 (sizeof(Dst) * 8 - 1); | |
| 48 static const size_t kSrcMaxExponent = SrcLimits::is_iec559 ? | |
| 49 SrcLimits::max_exponent : | |
| 50 (sizeof(Src) * 8 - 1); | |
| 51 static const DstRange value = kDstMaxExponent >= kSrcMaxExponent ? | |
| 52 CONTAINS_RANGE : OVERLAPS_RANGE; | |
| 53 }; | 58 }; |
| 54 | 59 |
| 60 // Both unsigned, narrowing (handled same as both signed narrowing). | |
| 55 template <typename Dst, typename Src> | 61 template <typename Dst, typename Src> |
| 56 struct StaticRangeCheck<Dst, Src, DST_UNSIGNED, SRC_UNSIGNED> { | 62 struct StaticRangeCheck<Dst, Src, DST_UNSIGNED, SRC_UNSIGNED> : |
| 57 static const DstRange value = sizeof(Dst) >= sizeof(Src) ? | 63 StaticRangeCheck<Dst, Src, DST_SIGNED, SRC_SIGNED> {}; |
|
Ryan Sleevi
2014/02/10 23:42:41
Doesn't this actually leave ::value unspecified?
jschuh
2014/02/11 00:43:28
No, it's inheriting value from the DST_SIGNED, SRC
| |
| 58 CONTAINS_RANGE : OVERLAPS_RANGE; | 64 |
| 65 // Unsigned to signed, overlapping. | |
| 66 template <typename Dst, typename Src> | |
| 67 struct StaticRangeCheck<Dst, Src, DST_SIGNED, SRC_UNSIGNED> { | |
| 68 typedef numeric_limits<Dst> DstLimits; | |
| 69 typedef numeric_limits<Src> SrcLimits; | |
|
Ryan Sleevi
2014/02/10 23:42:41
Unused?
jschuh
2014/02/11 00:43:28
Done.
| |
| 70 static const DstRangeId value = MaxExponent<Dst>::value > | |
| 71 MaxExponent<Src>::value ? | |
| 72 CONTAINS_RANGE : OVERLAPS_RANGE; | |
| 59 }; | 73 }; |
| 60 | 74 |
| 61 template <typename Dst, typename Src> | 75 // Signed to unsigned, overlapping. |
| 62 struct StaticRangeCheck<Dst, Src, DST_SIGNED, SRC_UNSIGNED> { | |
| 63 typedef std::numeric_limits<Dst> DstLimits; | |
| 64 typedef std::numeric_limits<Src> SrcLimits; | |
| 65 // Compare based on max_exponent, which we must compute for integrals. | |
| 66 static const size_t kDstMaxExponent = DstLimits::is_iec559 ? | |
| 67 DstLimits::max_exponent : | |
| 68 (sizeof(Dst) * 8 - 1); | |
| 69 static const size_t kSrcMaxExponent = sizeof(Src) * 8; | |
| 70 static const DstRange value = kDstMaxExponent >= kSrcMaxExponent ? | |
| 71 CONTAINS_RANGE : OVERLAPS_RANGE; | |
| 72 }; | |
| 73 | |
| 74 template <typename Dst, typename Src> | 76 template <typename Dst, typename Src> |
| 75 struct StaticRangeCheck<Dst, Src, DST_UNSIGNED, SRC_SIGNED> { | 77 struct StaticRangeCheck<Dst, Src, DST_UNSIGNED, SRC_SIGNED> { |
| 76 static const DstRange value = OVERLAPS_RANGE; | 78 static const DstRangeId value = OVERLAPS_RANGE; |
| 77 }; | 79 }; |
| 78 | 80 |
| 79 | 81 |
| 80 enum RangeCheckResult { | 82 enum RangeCheckId { |
| 81 TYPE_VALID = 0, // Value can be represented by the destination type. | 83 TYPE_VALID = 0, // Value can be represented by the destination type. |
| 82 TYPE_UNDERFLOW = 1, // Value would overflow. | 84 TYPE_UNDERFLOW = 1, // Value would overflow. |
| 83 TYPE_OVERFLOW = 2, // Value would underflow. | 85 TYPE_OVERFLOW = 2, // Value would underflow. |
| 84 TYPE_INVALID = 3 // Source value is invalid (i.e. NaN). | 86 TYPE_INVALID = 3 // Source value is invalid (i.e. NaN). |
| 85 }; | 87 }; |
| 86 | 88 |
| 87 // This macro creates a RangeCheckResult from an upper and lower bound | 89 // This macro creates a RangeCheckId from an upper and lower bound |
| 88 // check by taking advantage of the fact that only NaN can be out of range in | 90 // check by taking advantage of the fact that only NaN can be out of range in |
| 89 // both directions at once. | 91 // both directions at once. |
| 90 #define BASE_NUMERIC_RANGE_CHECK_RESULT(is_in_upper_bound, is_in_lower_bound) \ | 92 #define BASE_NUMERIC_RANGE_CHECK_RESULT(is_in_upper_bound, is_in_lower_bound) \ |
| 91 RangeCheckResult(((is_in_upper_bound) ? 0 : TYPE_OVERFLOW) | \ | 93 RangeCheckId(((is_in_upper_bound) ? 0 : TYPE_OVERFLOW) | \ |
| 92 ((is_in_lower_bound) ? 0 : TYPE_UNDERFLOW)) | 94 ((is_in_lower_bound) ? 0 : TYPE_UNDERFLOW)) |
| 93 | 95 |
| 94 template <typename Dst, | 96 template <typename Dst, |
| 95 typename Src, | 97 typename Src, |
| 96 DstSign IsDstSigned = std::numeric_limits<Dst>::is_signed ? | 98 DstSignId DstSign = numeric_limits<Dst>::is_signed ? |
| 97 DST_SIGNED : DST_UNSIGNED, | 99 DST_SIGNED : DST_UNSIGNED, |
| 98 SrcSign IsSrcSigned = std::numeric_limits<Src>::is_signed ? | 100 SrcSignId SrcSign = numeric_limits<Src>::is_signed ? |
| 99 SRC_SIGNED : SRC_UNSIGNED, | 101 SRC_SIGNED : SRC_UNSIGNED, |
| 100 DstRange IsSrcRangeContained = StaticRangeCheck<Dst, Src>::value> | 102 DstRangeId DstRange = StaticRangeCheck<Dst, Src>::value> |
| 101 struct RangeCheckImpl {}; | 103 struct RangeCheckImpl {}; |
| 102 | 104 |
| 103 // The following templates are for ranges that must be verified at runtime. We | 105 // The following templates are for ranges that must be verified at runtime. We |
| 104 // split it into checks based on signedness to avoid confusing casts and | 106 // split it into checks based on signedness to avoid confusing casts and |
| 105 // compiler warnings on signed an unsigned comparisons. | 107 // compiler warnings on signed an unsigned comparisons. |
| 106 | 108 |
| 107 // Dst range always contains the result: nothing to check. | 109 // Dst range always contains the result: nothing to check. |
| 108 template <typename Dst, typename Src, DstSign IsDstSigned, SrcSign IsSrcSigned> | 110 template <typename Dst, typename Src, DstSignId DstSign, SrcSignId SrcSign> |
| 109 struct RangeCheckImpl<Dst, Src, IsDstSigned, IsSrcSigned, CONTAINS_RANGE> { | 111 struct RangeCheckImpl<Dst, Src, DstSign, SrcSign, CONTAINS_RANGE> { |
| 110 static RangeCheckResult Check(Src value) { | 112 static RangeCheckId Check(Src value) { |
| 111 return TYPE_VALID; | 113 return TYPE_VALID; |
| 112 } | 114 } |
| 113 }; | 115 }; |
| 114 | 116 |
| 115 // Signed to signed narrowing. | 117 // Signed to signed narrowing. |
| 116 template <typename Dst, typename Src> | 118 template <typename Dst, typename Src> |
| 117 struct RangeCheckImpl<Dst, Src, DST_SIGNED, SRC_SIGNED, OVERLAPS_RANGE> { | 119 struct RangeCheckImpl<Dst, Src, DST_SIGNED, SRC_SIGNED, OVERLAPS_RANGE> { |
| 118 static RangeCheckResult Check(Src value) { | 120 static RangeCheckId Check(Src value) { |
| 119 typedef std::numeric_limits<Dst> DstLimits; | 121 return numeric_limits<Dst>::is_iec559 ? |
| 120 return DstLimits::is_iec559 ? | |
| 121 BASE_NUMERIC_RANGE_CHECK_RESULT( | 122 BASE_NUMERIC_RANGE_CHECK_RESULT( |
| 122 value <= static_cast<Src>(DstLimits::max()), | 123 value <= numeric_limits<Dst>::max(), |
| 123 value >= static_cast<Src>(DstLimits::max() * -1)) : | 124 value >= -numeric_limits<Dst>::max()) : |
| 124 BASE_NUMERIC_RANGE_CHECK_RESULT( | 125 BASE_NUMERIC_RANGE_CHECK_RESULT( |
| 125 value <= static_cast<Src>(DstLimits::max()), | 126 value <= numeric_limits<Dst>::max(), |
| 126 value >= static_cast<Src>(DstLimits::min())); | 127 value >= numeric_limits<Dst>::min()); |
| 127 } | 128 } |
| 128 }; | 129 }; |
| 129 | 130 |
| 130 // Unsigned to unsigned narrowing. | 131 // Unsigned to unsigned narrowing. |
| 131 template <typename Dst, typename Src> | 132 template <typename Dst, typename Src> |
| 132 struct RangeCheckImpl<Dst, Src, DST_UNSIGNED, SRC_UNSIGNED, OVERLAPS_RANGE> { | 133 struct RangeCheckImpl<Dst, Src, DST_UNSIGNED, SRC_UNSIGNED, OVERLAPS_RANGE> { |
| 133 static RangeCheckResult Check(Src value) { | 134 static RangeCheckId Check(Src value) { |
| 134 typedef std::numeric_limits<Dst> DstLimits; | |
| 135 return BASE_NUMERIC_RANGE_CHECK_RESULT( | 135 return BASE_NUMERIC_RANGE_CHECK_RESULT( |
| 136 value <= static_cast<Src>(DstLimits::max()), true); | 136 value <= numeric_limits<Dst>::max(), true); |
| 137 } | 137 } |
| 138 }; | 138 }; |
| 139 | 139 |
| 140 // Unsigned to signed. | 140 // Unsigned to signed. |
| 141 template <typename Dst, typename Src> | 141 template <typename Dst, typename Src> |
| 142 struct RangeCheckImpl<Dst, Src, DST_SIGNED, SRC_UNSIGNED, OVERLAPS_RANGE> { | 142 struct RangeCheckImpl<Dst, Src, DST_SIGNED, SRC_UNSIGNED, OVERLAPS_RANGE> { |
| 143 static RangeCheckResult Check(Src value) { | 143 static RangeCheckId Check(Src value) { |
| 144 typedef std::numeric_limits<Dst> DstLimits; | |
| 145 return sizeof(Dst) > sizeof(Src) ? TYPE_VALID : | 144 return sizeof(Dst) > sizeof(Src) ? TYPE_VALID : |
| 146 BASE_NUMERIC_RANGE_CHECK_RESULT( | 145 BASE_NUMERIC_RANGE_CHECK_RESULT( |
| 147 value <= static_cast<Src>(DstLimits::max()), true); | 146 value <= static_cast<Src>(numeric_limits<Dst>::max()), |
| 147 true); | |
| 148 } | 148 } |
| 149 }; | 149 }; |
| 150 | 150 |
| 151 // Signed to unsigned. | 151 // Signed to unsigned. |
| 152 template <typename Dst, typename Src> | 152 template <typename Dst, typename Src> |
| 153 struct RangeCheckImpl<Dst, Src, DST_UNSIGNED, SRC_SIGNED, OVERLAPS_RANGE> { | 153 struct RangeCheckImpl<Dst, Src, DST_UNSIGNED, SRC_SIGNED, OVERLAPS_RANGE> { |
| 154 static RangeCheckResult Check(Src value) { | 154 static RangeCheckId Check(Src value) { |
| 155 typedef std::numeric_limits<Dst> DstLimits; | 155 return (MaxExponent<Dst>::value >= MaxExponent<Src>::value) ? |
| 156 typedef std::numeric_limits<Src> SrcLimits; | |
| 157 // Compare based on max_exponent, which we must compute for integrals. | |
| 158 static const size_t kDstMaxExponent = sizeof(Dst) * 8; | |
| 159 static const size_t kSrcMaxExponent = SrcLimits::is_iec559 ? | |
| 160 SrcLimits::max_exponent : | |
| 161 (sizeof(Src) * 8 - 1); | |
| 162 return (kDstMaxExponent >= kSrcMaxExponent) ? | |
| 163 BASE_NUMERIC_RANGE_CHECK_RESULT(true, value >= static_cast<Src>(0)) : | 156 BASE_NUMERIC_RANGE_CHECK_RESULT(true, value >= static_cast<Src>(0)) : |
| 164 BASE_NUMERIC_RANGE_CHECK_RESULT( | 157 BASE_NUMERIC_RANGE_CHECK_RESULT( |
| 165 value <= static_cast<Src>(DstLimits::max()), | 158 value <= static_cast<Src>(numeric_limits<Dst>::max()), |
| 166 value >= static_cast<Src>(0)); | 159 value >= static_cast<Src>(0)); |
| 167 } | 160 } |
| 168 }; | 161 }; |
| 169 | 162 |
| 170 template <typename Dst, typename Src> | 163 template <typename Dst, typename Src> |
| 171 inline RangeCheckResult RangeCheck(Src value) { | 164 inline RangeCheckId RangeCheck(Src value) { |
| 172 COMPILE_ASSERT(std::numeric_limits<Src>::is_specialized, | 165 COMPILE_ASSERT(numeric_limits<Src>::is_specialized, argument_must_be_numeric); |
| 173 argument_must_be_numeric); | 166 COMPILE_ASSERT(numeric_limits<Dst>::is_specialized, result_must_be_numeric); |
| 174 COMPILE_ASSERT(std::numeric_limits<Dst>::is_specialized, | |
| 175 result_must_be_numeric); | |
| 176 return RangeCheckImpl<Dst, Src>::Check(value); | 167 return RangeCheckImpl<Dst, Src>::Check(value); |
| 177 } | 168 } |
| 178 | 169 |
| 179 } // namespace internal | 170 } // namespace internal |
| 180 } // namespace base | 171 } // namespace base |
| 181 | 172 |
| 182 #endif // BASE_SAFE_CONVERSIONS_IMPL_H_ | 173 #endif // BASE_SAFE_CONVERSIONS_IMPL_H_ |
| 183 | 174 |
| OLD | NEW |