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_NUMERICS_SAFE_CONVERSIONS_IMPL_H_ | 5 #ifndef BASE_NUMERICS_SAFE_CONVERSIONS_IMPL_H_ |
| 6 #define BASE_NUMERICS_SAFE_CONVERSIONS_IMPL_H_ | 6 #define BASE_NUMERICS_SAFE_CONVERSIONS_IMPL_H_ |
| 7 | 7 |
| 8 #include <limits> | 8 #include <limits> |
| 9 | 9 |
| 10 #include "base/template_util.h" | 10 #include "base/template_util.h" |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 101 | 101 |
| 102 // This function creates a RangeConstraint from an upper and lower bound | 102 // This function creates a RangeConstraint from an upper and lower bound |
| 103 // check by taking advantage of the fact that only NaN can be out of range in | 103 // check by taking advantage of the fact that only NaN can be out of range in |
| 104 // both directions at once. | 104 // both directions at once. |
| 105 inline RangeConstraint GetRangeConstraint(bool is_in_upper_bound, | 105 inline RangeConstraint GetRangeConstraint(bool is_in_upper_bound, |
| 106 bool is_in_lower_bound) { | 106 bool is_in_lower_bound) { |
| 107 return GetRangeConstraint((is_in_upper_bound ? 0 : RANGE_OVERFLOW) | | 107 return GetRangeConstraint((is_in_upper_bound ? 0 : RANGE_OVERFLOW) | |
| 108 (is_in_lower_bound ? 0 : RANGE_UNDERFLOW)); | 108 (is_in_lower_bound ? 0 : RANGE_UNDERFLOW)); |
| 109 } | 109 } |
| 110 | 110 |
| 111 // The following helper template addresses a corner case in range checks for | |
| 112 // conversion from a floating-point type to an integral type of smaller range | |
| 113 // but larger precision (e.g. float -> int32_t). The problem is as follows: | |
| 114 // 1. Integral maximum is always one less than a power of two, so it must be | |
| 115 // truncated to fit the mantissa of the floating point. The direction of | |
| 116 // rounding is implementation defined, but in practice it's always IEEE | |
| 117 // floats, which round to nearest and thus result in a value of larger | |
|
brucedawson
2015/09/11 23:23:27
The FPU can be set to various rounding modes, some
jschuh
2015/09/12 00:43:12
Yes, this should be reliable and completely portab
| |
| 118 // magnitude than the integral value (maximum + 1). | |
| 119 // 2. If the floating point value is equal to the promoted integral maximum | |
| 120 // value, a range check will erroneously pass. | |
| 121 // 3. When the floating point value is then converted to an integer, the | |
| 122 // resulting value is out of range for the target integral type and | |
| 123 // thus is implementation defined. | |
| 124 // To fix this bug we manually truncate the maximum value when the destination | |
| 125 // type is an integral of larger precision than a source floating-point type. | |
|
brucedawson
2015/09/11 23:23:27
Could clarify what it is truncated to, perhaps wit
jschuh
2015/09/12 00:43:12
Done.
| |
| 126 template <typename Dst, typename Src> | |
| 127 struct NarrowingRange { | |
| 128 typedef typename std::numeric_limits<Src> SrcLimits; | |
| 129 typedef typename std::numeric_limits<Dst> DstLimits; | |
| 130 | |
| 131 static Dst max() { | |
| 132 // The following logic avoids warnings where the max function is | |
| 133 // instantiated with invalid values for a bit shift (even though | |
| 134 // the such a function can never be called). | |
| 135 static const int shift = | |
| 136 (MaxExponent<Src>::value > MaxExponent<Dst>::value && | |
| 137 SrcLimits::digits < DstLimits::digits && SrcLimits::is_iec559 && | |
| 138 !DstLimits::is_iec559) | |
| 139 ? (DstLimits::digits - SrcLimits::digits) | |
| 140 : 0; | |
| 141 return DstLimits::max() - static_cast<Dst>((UINTMAX_C(1) << shift) - 1); | |
| 142 } | |
| 143 | |
| 144 static Dst min() { | |
| 145 return std::numeric_limits<Dst>::is_iec559 ? -DstLimits::max() | |
| 146 : DstLimits::min(); | |
| 147 } | |
| 148 }; | |
| 149 | |
| 111 template < | 150 template < |
| 112 typename Dst, | 151 typename Dst, |
| 113 typename Src, | 152 typename Src, |
| 114 IntegerRepresentation DstSign = std::numeric_limits<Dst>::is_signed | 153 IntegerRepresentation DstSign = std::numeric_limits<Dst>::is_signed |
| 115 ? INTEGER_REPRESENTATION_SIGNED | 154 ? INTEGER_REPRESENTATION_SIGNED |
| 116 : INTEGER_REPRESENTATION_UNSIGNED, | 155 : INTEGER_REPRESENTATION_UNSIGNED, |
| 117 IntegerRepresentation SrcSign = std::numeric_limits<Src>::is_signed | 156 IntegerRepresentation SrcSign = std::numeric_limits<Src>::is_signed |
| 118 ? INTEGER_REPRESENTATION_SIGNED | 157 ? INTEGER_REPRESENTATION_SIGNED |
| 119 : INTEGER_REPRESENTATION_UNSIGNED, | 158 : INTEGER_REPRESENTATION_UNSIGNED, |
| 120 NumericRangeRepresentation DstRange = | 159 NumericRangeRepresentation DstRange = |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 140 | 179 |
| 141 // Signed to signed narrowing: Both the upper and lower boundaries may be | 180 // Signed to signed narrowing: Both the upper and lower boundaries may be |
| 142 // exceeded. | 181 // exceeded. |
| 143 template <typename Dst, typename Src> | 182 template <typename Dst, typename Src> |
| 144 struct DstRangeRelationToSrcRangeImpl<Dst, | 183 struct DstRangeRelationToSrcRangeImpl<Dst, |
| 145 Src, | 184 Src, |
| 146 INTEGER_REPRESENTATION_SIGNED, | 185 INTEGER_REPRESENTATION_SIGNED, |
| 147 INTEGER_REPRESENTATION_SIGNED, | 186 INTEGER_REPRESENTATION_SIGNED, |
| 148 NUMERIC_RANGE_NOT_CONTAINED> { | 187 NUMERIC_RANGE_NOT_CONTAINED> { |
| 149 static RangeConstraint Check(Src value) { | 188 static RangeConstraint Check(Src value) { |
| 150 return std::numeric_limits<Dst>::is_iec559 | 189 return GetRangeConstraint((value <= NarrowingRange<Dst, Src>::max()), |
| 151 ? GetRangeConstraint((value < std::numeric_limits<Dst>::max()), | 190 (value >= NarrowingRange<Dst, Src>::min())); |
| 152 (value > -std::numeric_limits<Dst>::max())) | |
| 153 : GetRangeConstraint((value < std::numeric_limits<Dst>::max()), | |
| 154 (value > std::numeric_limits<Dst>::min())); | |
| 155 } | 191 } |
| 156 }; | 192 }; |
| 157 | 193 |
| 158 // Unsigned to unsigned narrowing: Only the upper boundary can be exceeded. | 194 // Unsigned to unsigned narrowing: Only the upper boundary can be exceeded. |
| 159 template <typename Dst, typename Src> | 195 template <typename Dst, typename Src> |
| 160 struct DstRangeRelationToSrcRangeImpl<Dst, | 196 struct DstRangeRelationToSrcRangeImpl<Dst, |
| 161 Src, | 197 Src, |
| 162 INTEGER_REPRESENTATION_UNSIGNED, | 198 INTEGER_REPRESENTATION_UNSIGNED, |
| 163 INTEGER_REPRESENTATION_UNSIGNED, | 199 INTEGER_REPRESENTATION_UNSIGNED, |
| 164 NUMERIC_RANGE_NOT_CONTAINED> { | 200 NUMERIC_RANGE_NOT_CONTAINED> { |
| 165 static RangeConstraint Check(Src value) { | 201 static RangeConstraint Check(Src value) { |
| 166 return GetRangeConstraint(value < std::numeric_limits<Dst>::max(), true); | 202 return GetRangeConstraint(value <= NarrowingRange<Dst, Src>::max(), true); |
| 167 } | 203 } |
| 168 }; | 204 }; |
| 169 | 205 |
| 170 // Unsigned to signed: The upper boundary may be exceeded. | 206 // Unsigned to signed: The upper boundary may be exceeded. |
| 171 template <typename Dst, typename Src> | 207 template <typename Dst, typename Src> |
| 172 struct DstRangeRelationToSrcRangeImpl<Dst, | 208 struct DstRangeRelationToSrcRangeImpl<Dst, |
| 173 Src, | 209 Src, |
| 174 INTEGER_REPRESENTATION_SIGNED, | 210 INTEGER_REPRESENTATION_SIGNED, |
| 175 INTEGER_REPRESENTATION_UNSIGNED, | 211 INTEGER_REPRESENTATION_UNSIGNED, |
| 176 NUMERIC_RANGE_NOT_CONTAINED> { | 212 NUMERIC_RANGE_NOT_CONTAINED> { |
| 177 static RangeConstraint Check(Src value) { | 213 static RangeConstraint Check(Src value) { |
| 178 return sizeof(Dst) > sizeof(Src) | 214 return sizeof(Dst) > sizeof(Src) |
| 179 ? RANGE_VALID | 215 ? RANGE_VALID |
| 180 : GetRangeConstraint( | 216 : GetRangeConstraint( |
| 181 value < static_cast<Src>(std::numeric_limits<Dst>::max()), | 217 value <= static_cast<Src>(NarrowingRange<Dst, Src>::max()), |
| 182 true); | 218 true); |
| 183 } | 219 } |
| 184 }; | 220 }; |
| 185 | 221 |
| 186 // Signed to unsigned: The upper boundary may be exceeded for a narrower Dst, | 222 // Signed to unsigned: The upper boundary may be exceeded for a narrower Dst, |
| 187 // and any negative value exceeds the lower boundary. | 223 // and any negative value exceeds the lower boundary. |
| 188 template <typename Dst, typename Src> | 224 template <typename Dst, typename Src> |
| 189 struct DstRangeRelationToSrcRangeImpl<Dst, | 225 struct DstRangeRelationToSrcRangeImpl<Dst, |
| 190 Src, | 226 Src, |
| 191 INTEGER_REPRESENTATION_UNSIGNED, | 227 INTEGER_REPRESENTATION_UNSIGNED, |
| 192 INTEGER_REPRESENTATION_SIGNED, | 228 INTEGER_REPRESENTATION_SIGNED, |
| 193 NUMERIC_RANGE_NOT_CONTAINED> { | 229 NUMERIC_RANGE_NOT_CONTAINED> { |
| 194 static RangeConstraint Check(Src value) { | 230 static RangeConstraint Check(Src value) { |
| 195 return (MaxExponent<Dst>::value >= MaxExponent<Src>::value) | 231 return (MaxExponent<Dst>::value >= MaxExponent<Src>::value) |
| 196 ? GetRangeConstraint(true, value >= static_cast<Src>(0)) | 232 ? GetRangeConstraint(true, value >= static_cast<Src>(0)) |
| 197 : GetRangeConstraint( | 233 : GetRangeConstraint( |
| 198 value < static_cast<Src>(std::numeric_limits<Dst>::max()), | 234 value <= static_cast<Src>(NarrowingRange<Dst, Src>::max()), |
| 199 value >= static_cast<Src>(0)); | 235 value >= static_cast<Src>(0)); |
| 200 } | 236 } |
| 201 }; | 237 }; |
| 202 | 238 |
| 203 template <typename Dst, typename Src> | 239 template <typename Dst, typename Src> |
| 204 inline RangeConstraint DstRangeRelationToSrcRange(Src value) { | 240 inline RangeConstraint DstRangeRelationToSrcRange(Src value) { |
| 205 static_assert(std::numeric_limits<Src>::is_specialized, | 241 static_assert(std::numeric_limits<Src>::is_specialized, |
| 206 "Argument must be numeric."); | 242 "Argument must be numeric."); |
| 207 static_assert(std::numeric_limits<Dst>::is_specialized, | 243 static_assert(std::numeric_limits<Dst>::is_specialized, |
| 208 "Result must be numeric."); | 244 "Result must be numeric."); |
| 209 return DstRangeRelationToSrcRangeImpl<Dst, Src>::Check(value); | 245 return DstRangeRelationToSrcRangeImpl<Dst, Src>::Check(value); |
| 210 } | 246 } |
| 211 | 247 |
| 212 } // namespace internal | 248 } // namespace internal |
| 213 } // namespace base | 249 } // namespace base |
| 214 | 250 |
| 215 #endif // BASE_NUMERICS_SAFE_CONVERSIONS_IMPL_H_ | 251 #endif // BASE_NUMERICS_SAFE_CONVERSIONS_IMPL_H_ |
| OLD | NEW |