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.h> | 8 #include <limits.h> |
9 #include <stdint.h> | 9 #include <stdint.h> |
10 | 10 |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
85 }; | 85 }; |
86 | 86 |
87 enum RangeConstraint { | 87 enum RangeConstraint { |
88 RANGE_VALID = 0x0, // Value can be represented by the destination type. | 88 RANGE_VALID = 0x0, // Value can be represented by the destination type. |
89 RANGE_UNDERFLOW = 0x1, // Value would overflow. | 89 RANGE_UNDERFLOW = 0x1, // Value would overflow. |
90 RANGE_OVERFLOW = 0x2, // Value would underflow. | 90 RANGE_OVERFLOW = 0x2, // Value would underflow. |
91 RANGE_INVALID = RANGE_UNDERFLOW | RANGE_OVERFLOW // Invalid (i.e. NaN). | 91 RANGE_INVALID = RANGE_UNDERFLOW | RANGE_OVERFLOW // Invalid (i.e. NaN). |
92 }; | 92 }; |
93 | 93 |
94 // Helper function for coercing an int back to a RangeContraint. | 94 // Helper function for coercing an int back to a RangeContraint. |
95 inline RangeConstraint GetRangeConstraint(int integer_range_constraint) { | 95 inline constexpr RangeConstraint GetRangeConstraint( |
96 DCHECK(integer_range_constraint >= RANGE_VALID && | 96 int integer_range_constraint) { |
97 integer_range_constraint <= RANGE_INVALID); | 97 // TODO(jschuh): Once we get full C++14 support we want this |
| 98 // assert(integer_range_constraint >= RANGE_VALID && |
| 99 // integer_range_constraint <= RANGE_INVALID) |
98 return static_cast<RangeConstraint>(integer_range_constraint); | 100 return static_cast<RangeConstraint>(integer_range_constraint); |
99 } | 101 } |
100 | 102 |
101 // This function creates a RangeConstraint from an upper and lower bound | 103 // This function creates a RangeConstraint from an upper and lower bound |
102 // check by taking advantage of the fact that only NaN can be out of range in | 104 // check by taking advantage of the fact that only NaN can be out of range in |
103 // both directions at once. | 105 // both directions at once. |
104 inline RangeConstraint GetRangeConstraint(bool is_in_upper_bound, | 106 constexpr inline RangeConstraint GetRangeConstraint(bool is_in_upper_bound, |
105 bool is_in_lower_bound) { | 107 bool is_in_lower_bound) { |
106 return GetRangeConstraint((is_in_upper_bound ? 0 : RANGE_OVERFLOW) | | 108 return GetRangeConstraint((is_in_upper_bound ? 0 : RANGE_OVERFLOW) | |
107 (is_in_lower_bound ? 0 : RANGE_UNDERFLOW)); | 109 (is_in_lower_bound ? 0 : RANGE_UNDERFLOW)); |
108 } | 110 } |
109 | 111 |
110 // The following helper template addresses a corner case in range checks for | 112 // The following helper template addresses a corner case in range checks for |
111 // conversion from a floating-point type to an integral type of smaller range | 113 // conversion from a floating-point type to an integral type of smaller range |
112 // but larger precision (e.g. float -> unsigned). The problem is as follows: | 114 // but larger precision (e.g. float -> unsigned). The problem is as follows: |
113 // 1. Integral maximum is always one less than a power of two, so it must be | 115 // 1. Integral maximum is always one less than a power of two, so it must be |
114 // truncated to fit the mantissa of the floating point. The direction of | 116 // truncated to fit the mantissa of the floating point. The direction of |
115 // rounding is implementation defined, but by default it's always IEEE | 117 // rounding is implementation defined, but by default it's always IEEE |
116 // floats, which round to nearest and thus result in a value of larger | 118 // floats, which round to nearest and thus result in a value of larger |
117 // magnitude than the integral value. | 119 // magnitude than the integral value. |
118 // Example: float f = UINT_MAX; // f is 4294967296f but UINT_MAX | 120 // Example: float f = UINT_MAX; // f is 4294967296f but UINT_MAX |
119 // // is 4294967295u. | 121 // // is 4294967295u. |
120 // 2. If the floating point value is equal to the promoted integral maximum | 122 // 2. If the floating point value is equal to the promoted integral maximum |
121 // value, a range check will erroneously pass. | 123 // value, a range check will erroneously pass. |
122 // Example: (4294967296f <= 4294967295u) // This is true due to a precision | 124 // Example: (4294967296f <= 4294967295u) // This is true due to a precision |
123 // // loss in rounding up to float. | 125 // // loss in rounding up to float. |
124 // 3. When the floating point value is then converted to an integral, the | 126 // 3. When the floating point value is then converted to an integral, the |
125 // resulting value is out of range for the target integral type and | 127 // resulting value is out of range for the target integral type and |
126 // thus is implementation defined. | 128 // thus is implementation defined. |
127 // Example: unsigned u = (float)INT_MAX; // u will typically overflow to 0. | 129 // Example: unsigned u = (float)INT_MAX; // u will typically overflow to 0. |
128 // To fix this bug we manually truncate the maximum value when the destination | 130 // To fix this bug we manually truncate the maximum value when the destination |
129 // type is an integral of larger precision than the source floating-point type, | 131 // type is an integral of larger precision than the source floating-point type, |
130 // such that the resulting maximum is represented exactly as a floating point. | 132 // such that the resulting maximum is represented exactly as a floating point. |
131 template <typename Dst, typename Src> | 133 template <typename Dst, typename Src> |
132 struct NarrowingRange { | 134 struct NarrowingRange { |
133 typedef typename std::numeric_limits<Src> SrcLimits; | 135 typedef typename std::numeric_limits<Src> SrcLimits; |
134 typedef typename std::numeric_limits<Dst> DstLimits; | 136 typedef typename std::numeric_limits<Dst> DstLimits; |
| 137 // The following logic avoids warnings where the max function is |
| 138 // instantiated with invalid values for a bit shift (even though |
| 139 // such a function can never be called). |
| 140 static const int shift = (MaxExponent<Src>::value > MaxExponent<Dst>::value && |
| 141 SrcLimits::digits < DstLimits::digits && |
| 142 SrcLimits::is_iec559 && |
| 143 DstLimits::is_integer) |
| 144 ? (DstLimits::digits - SrcLimits::digits) |
| 145 : 0; |
135 | 146 |
136 static Dst max() { | 147 static constexpr Dst max() { |
137 // The following logic avoids warnings where the max function is | |
138 // instantiated with invalid values for a bit shift (even though | |
139 // such a function can never be called). | |
140 static const int shift = | |
141 (MaxExponent<Src>::value > MaxExponent<Dst>::value && | |
142 SrcLimits::digits < DstLimits::digits && SrcLimits::is_iec559 && | |
143 DstLimits::is_integer) | |
144 ? (DstLimits::digits - SrcLimits::digits) | |
145 : 0; | |
146 | |
147 // We use UINTMAX_C below to avoid compiler warnings about shifting floating | 148 // We use UINTMAX_C below to avoid compiler warnings about shifting floating |
148 // points. Since it's a compile time calculation, it shouldn't have any | 149 // points. Since it's a compile time calculation, it shouldn't have any |
149 // performance impact. | 150 // performance impact. |
150 return DstLimits::max() - static_cast<Dst>((UINTMAX_C(1) << shift) - 1); | 151 return DstLimits::max() - static_cast<Dst>((UINTMAX_C(1) << shift) - 1); |
151 } | 152 } |
152 | 153 |
153 static Dst min() { | 154 static constexpr Dst min() { |
154 return std::numeric_limits<Dst>::is_iec559 ? -DstLimits::max() | 155 return std::numeric_limits<Dst>::is_iec559 ? -DstLimits::max() |
155 : DstLimits::min(); | 156 : DstLimits::min(); |
156 } | 157 } |
157 }; | 158 }; |
158 | 159 |
159 template < | 160 template < |
160 typename Dst, | 161 typename Dst, |
161 typename Src, | 162 typename Src, |
162 IntegerRepresentation DstSign = std::numeric_limits<Dst>::is_signed | 163 IntegerRepresentation DstSign = std::numeric_limits<Dst>::is_signed |
163 ? INTEGER_REPRESENTATION_SIGNED | 164 ? INTEGER_REPRESENTATION_SIGNED |
(...skipping 12 matching lines...) Expand all Loading... |
176 // Dst range is statically determined to contain Src: Nothing to check. | 177 // Dst range is statically determined to contain Src: Nothing to check. |
177 template <typename Dst, | 178 template <typename Dst, |
178 typename Src, | 179 typename Src, |
179 IntegerRepresentation DstSign, | 180 IntegerRepresentation DstSign, |
180 IntegerRepresentation SrcSign> | 181 IntegerRepresentation SrcSign> |
181 struct DstRangeRelationToSrcRangeImpl<Dst, | 182 struct DstRangeRelationToSrcRangeImpl<Dst, |
182 Src, | 183 Src, |
183 DstSign, | 184 DstSign, |
184 SrcSign, | 185 SrcSign, |
185 NUMERIC_RANGE_CONTAINED> { | 186 NUMERIC_RANGE_CONTAINED> { |
186 static RangeConstraint Check(Src value) { return RANGE_VALID; } | 187 static constexpr RangeConstraint Check(Src value) { return RANGE_VALID; } |
187 }; | 188 }; |
188 | 189 |
189 // Signed to signed narrowing: Both the upper and lower boundaries may be | 190 // Signed to signed narrowing: Both the upper and lower boundaries may be |
190 // exceeded. | 191 // exceeded. |
191 template <typename Dst, typename Src> | 192 template <typename Dst, typename Src> |
192 struct DstRangeRelationToSrcRangeImpl<Dst, | 193 struct DstRangeRelationToSrcRangeImpl<Dst, |
193 Src, | 194 Src, |
194 INTEGER_REPRESENTATION_SIGNED, | 195 INTEGER_REPRESENTATION_SIGNED, |
195 INTEGER_REPRESENTATION_SIGNED, | 196 INTEGER_REPRESENTATION_SIGNED, |
196 NUMERIC_RANGE_NOT_CONTAINED> { | 197 NUMERIC_RANGE_NOT_CONTAINED> { |
197 static RangeConstraint Check(Src value) { | 198 static constexpr RangeConstraint Check(Src value) { |
198 return GetRangeConstraint((value <= NarrowingRange<Dst, Src>::max()), | 199 return GetRangeConstraint((value <= NarrowingRange<Dst, Src>::max()), |
199 (value >= NarrowingRange<Dst, Src>::min())); | 200 (value >= NarrowingRange<Dst, Src>::min())); |
200 } | 201 } |
201 }; | 202 }; |
202 | 203 |
203 // Unsigned to unsigned narrowing: Only the upper boundary can be exceeded. | 204 // Unsigned to unsigned narrowing: Only the upper boundary can be exceeded. |
204 template <typename Dst, typename Src> | 205 template <typename Dst, typename Src> |
205 struct DstRangeRelationToSrcRangeImpl<Dst, | 206 struct DstRangeRelationToSrcRangeImpl<Dst, |
206 Src, | 207 Src, |
207 INTEGER_REPRESENTATION_UNSIGNED, | 208 INTEGER_REPRESENTATION_UNSIGNED, |
208 INTEGER_REPRESENTATION_UNSIGNED, | 209 INTEGER_REPRESENTATION_UNSIGNED, |
209 NUMERIC_RANGE_NOT_CONTAINED> { | 210 NUMERIC_RANGE_NOT_CONTAINED> { |
210 static RangeConstraint Check(Src value) { | 211 static constexpr RangeConstraint Check(Src value) { |
211 return GetRangeConstraint(value <= NarrowingRange<Dst, Src>::max(), true); | 212 return GetRangeConstraint(value <= NarrowingRange<Dst, Src>::max(), true); |
212 } | 213 } |
213 }; | 214 }; |
214 | 215 |
215 // Unsigned to signed: The upper boundary may be exceeded. | 216 // Unsigned to signed: The upper boundary may be exceeded. |
216 template <typename Dst, typename Src> | 217 template <typename Dst, typename Src> |
217 struct DstRangeRelationToSrcRangeImpl<Dst, | 218 struct DstRangeRelationToSrcRangeImpl<Dst, |
218 Src, | 219 Src, |
219 INTEGER_REPRESENTATION_SIGNED, | 220 INTEGER_REPRESENTATION_SIGNED, |
220 INTEGER_REPRESENTATION_UNSIGNED, | 221 INTEGER_REPRESENTATION_UNSIGNED, |
221 NUMERIC_RANGE_NOT_CONTAINED> { | 222 NUMERIC_RANGE_NOT_CONTAINED> { |
222 static RangeConstraint Check(Src value) { | 223 static constexpr RangeConstraint Check(Src value) { |
223 return sizeof(Dst) > sizeof(Src) | 224 return sizeof(Dst) > sizeof(Src) |
224 ? RANGE_VALID | 225 ? RANGE_VALID |
225 : GetRangeConstraint( | 226 : GetRangeConstraint( |
226 value <= static_cast<Src>(NarrowingRange<Dst, Src>::max()), | 227 value <= static_cast<Src>(NarrowingRange<Dst, Src>::max()), |
227 true); | 228 true); |
228 } | 229 } |
229 }; | 230 }; |
230 | 231 |
231 // Signed to unsigned: The upper boundary may be exceeded for a narrower Dst, | 232 // Signed to unsigned: The upper boundary may be exceeded for a narrower Dst, |
232 // and any negative value exceeds the lower boundary. | 233 // and any negative value exceeds the lower boundary. |
233 template <typename Dst, typename Src> | 234 template <typename Dst, typename Src> |
234 struct DstRangeRelationToSrcRangeImpl<Dst, | 235 struct DstRangeRelationToSrcRangeImpl<Dst, |
235 Src, | 236 Src, |
236 INTEGER_REPRESENTATION_UNSIGNED, | 237 INTEGER_REPRESENTATION_UNSIGNED, |
237 INTEGER_REPRESENTATION_SIGNED, | 238 INTEGER_REPRESENTATION_SIGNED, |
238 NUMERIC_RANGE_NOT_CONTAINED> { | 239 NUMERIC_RANGE_NOT_CONTAINED> { |
239 static RangeConstraint Check(Src value) { | 240 static constexpr RangeConstraint Check(Src value) { |
240 return (MaxExponent<Dst>::value >= MaxExponent<Src>::value) | 241 return (MaxExponent<Dst>::value >= MaxExponent<Src>::value) |
241 ? GetRangeConstraint(true, value >= static_cast<Src>(0)) | 242 ? GetRangeConstraint(true, value >= static_cast<Src>(0)) |
242 : GetRangeConstraint( | 243 : GetRangeConstraint( |
243 value <= static_cast<Src>(NarrowingRange<Dst, Src>::max()), | 244 value <= static_cast<Src>(NarrowingRange<Dst, Src>::max()), |
244 value >= static_cast<Src>(0)); | 245 value >= static_cast<Src>(0)); |
245 } | 246 } |
246 }; | 247 }; |
247 | 248 |
248 template <typename Dst, typename Src> | 249 template <typename Dst, typename Src> |
249 inline RangeConstraint DstRangeRelationToSrcRange(Src value) { | 250 inline constexpr RangeConstraint DstRangeRelationToSrcRange(Src value) { |
250 static_assert(std::numeric_limits<Src>::is_specialized, | 251 static_assert(std::numeric_limits<Src>::is_specialized, |
251 "Argument must be numeric."); | 252 "Argument must be numeric."); |
252 static_assert(std::numeric_limits<Dst>::is_specialized, | 253 static_assert(std::numeric_limits<Dst>::is_specialized, |
253 "Result must be numeric."); | 254 "Result must be numeric."); |
254 return DstRangeRelationToSrcRangeImpl<Dst, Src>::Check(value); | 255 return DstRangeRelationToSrcRangeImpl<Dst, Src>::Check(value); |
255 } | 256 } |
256 | 257 |
257 } // namespace internal | 258 } // namespace internal |
258 } // namespace base | 259 } // namespace base |
259 | 260 |
260 #endif // BASE_NUMERICS_SAFE_CONVERSIONS_IMPL_H_ | 261 #endif // BASE_NUMERICS_SAFE_CONVERSIONS_IMPL_H_ |
OLD | NEW |