Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(751)

Side by Side Diff: base/numerics/safe_conversions_impl.h

Issue 2583053003: Improve saturated_cast performance (Closed)
Patch Set: nit Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/numerics/safe_conversions.h ('k') | base/numerics/safe_numerics_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 <stdint.h> 8 #include <stdint.h>
9 9
10 #include <limits> 10 #include <limits>
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after
160 // Signed to unsigned: Dst cannot be statically determined to contain Src. 160 // Signed to unsigned: Dst cannot be statically determined to contain Src.
161 template <typename Dst, typename Src> 161 template <typename Dst, typename Src>
162 struct StaticDstRangeRelationToSrcRange<Dst, 162 struct StaticDstRangeRelationToSrcRange<Dst,
163 Src, 163 Src,
164 INTEGER_REPRESENTATION_UNSIGNED, 164 INTEGER_REPRESENTATION_UNSIGNED,
165 INTEGER_REPRESENTATION_SIGNED> { 165 INTEGER_REPRESENTATION_SIGNED> {
166 static const NumericRangeRepresentation value = NUMERIC_RANGE_NOT_CONTAINED; 166 static const NumericRangeRepresentation value = NUMERIC_RANGE_NOT_CONTAINED;
167 }; 167 };
168 168
169 enum RangeConstraint { 169 enum RangeConstraint {
170 RANGE_VALID = 0x0, // Value can be represented by the destination type. 170 RANGE_VALID = 0x0, // Value can be represented by the destination type.
171 RANGE_UNDERFLOW = 0x1, // Value would overflow. 171 RANGE_UNDERFLOW = 0x1, // Value would underflow.
172 RANGE_OVERFLOW = 0x2, // Value would underflow. 172 RANGE_OVERFLOW = 0x2, // Value would overflow.
173 RANGE_INVALID = RANGE_UNDERFLOW | RANGE_OVERFLOW // Invalid (i.e. NaN). 173 RANGE_INVALID = RANGE_UNDERFLOW | RANGE_OVERFLOW // Invalid (i.e. NaN).
174 }; 174 };
175 175
176 // Helper function for coercing an int back to a RangeContraint. 176 // This class wraps the range constraints as separate booleans so the compiler
177 constexpr RangeConstraint GetRangeConstraint(int integer_range_constraint) { 177 // can identify constants and eliminate unused code paths.
178 // TODO(jschuh): Once we get full C++14 support we want this 178 class RangeCheck {
179 // assert(integer_range_constraint >= RANGE_VALID && 179 public:
180 // integer_range_constraint <= RANGE_INVALID) 180 constexpr RangeCheck(bool is_in_upper_bound, bool is_in_lower_bound)
181 return static_cast<RangeConstraint>(integer_range_constraint); 181 : is_overflow_(!is_in_upper_bound), is_underflow_(!is_in_lower_bound) {}
182 } 182 constexpr RangeCheck() : is_overflow_(0), is_underflow_(0) {}
183 constexpr bool IsValid() const { return !is_overflow_ && !is_underflow_; }
184 constexpr bool IsInvalid() const { return is_overflow_ && is_underflow_; }
185 constexpr bool IsOverflow() const { return is_overflow_ && !is_underflow_; }
186 constexpr bool IsUnderflow() const { return !is_overflow_ && is_underflow_; }
183 187
184 // This function creates a RangeConstraint from an upper and lower bound 188 // These are some wrappers to make the tests a bit cleaner.
185 // check by taking advantage of the fact that only NaN can be out of range in 189 constexpr operator RangeConstraint() const {
186 // both directions at once. 190 return static_cast<RangeConstraint>(static_cast<int>(is_overflow_) << 1 |
187 constexpr inline RangeConstraint GetRangeConstraint(bool is_in_upper_bound, 191 static_cast<int>(is_underflow_));
188 bool is_in_lower_bound) { 192 }
189 return GetRangeConstraint((is_in_upper_bound ? 0 : RANGE_OVERFLOW) | 193 constexpr bool operator==(const RangeConstraint rhs) const {
190 (is_in_lower_bound ? 0 : RANGE_UNDERFLOW)); 194 return rhs == static_cast<RangeConstraint>(*this);
191 } 195 }
196
197 private:
198 const bool is_overflow_;
199 const bool is_underflow_;
200 };
192 201
193 // The following helper template addresses a corner case in range checks for 202 // The following helper template addresses a corner case in range checks for
194 // conversion from a floating-point type to an integral type of smaller range 203 // conversion from a floating-point type to an integral type of smaller range
195 // but larger precision (e.g. float -> unsigned). The problem is as follows: 204 // but larger precision (e.g. float -> unsigned). The problem is as follows:
196 // 1. Integral maximum is always one less than a power of two, so it must be 205 // 1. Integral maximum is always one less than a power of two, so it must be
197 // truncated to fit the mantissa of the floating point. The direction of 206 // truncated to fit the mantissa of the floating point. The direction of
198 // rounding is implementation defined, but by default it's always IEEE 207 // rounding is implementation defined, but by default it's always IEEE
199 // floats, which round to nearest and thus result in a value of larger 208 // floats, which round to nearest and thus result in a value of larger
200 // magnitude than the integral value. 209 // magnitude than the integral value.
201 // Example: float f = UINT_MAX; // f is 4294967296f but UINT_MAX 210 // Example: float f = UINT_MAX; // f is 4294967296f but UINT_MAX
202 // // is 4294967295u. 211 // // is 4294967295u.
203 // 2. If the floating point value is equal to the promoted integral maximum 212 // 2. If the floating point value is equal to the promoted integral maximum
204 // value, a range check will erroneously pass. 213 // value, a range check will erroneously pass.
205 // Example: (4294967296f <= 4294967295u) // This is true due to a precision 214 // Example: (4294967296f <= 4294967295u) // This is true due to a precision
206 // // loss in rounding up to float. 215 // // loss in rounding up to float.
207 // 3. When the floating point value is then converted to an integral, the 216 // 3. When the floating point value is then converted to an integral, the
208 // resulting value is out of range for the target integral type and 217 // resulting value is out of range for the target integral type and
209 // thus is implementation defined. 218 // thus is implementation defined.
210 // Example: unsigned u = (float)INT_MAX; // u will typically overflow to 0. 219 // Example: unsigned u = (float)INT_MAX; // u will typically overflow to 0.
211 // To fix this bug we manually truncate the maximum value when the destination 220 // To fix this bug we manually truncate the maximum value when the destination
212 // type is an integral of larger precision than the source floating-point type, 221 // type is an integral of larger precision than the source floating-point type,
213 // such that the resulting maximum is represented exactly as a floating point. 222 // such that the resulting maximum is represented exactly as a floating point.
214 template <typename Dst, 223 template <typename Dst, typename Src, template <typename> class Bounds>
215 typename Src,
216 template <typename> class Bounds = std::numeric_limits>
217 struct NarrowingRange { 224 struct NarrowingRange {
218 using SrcLimits = typename std::numeric_limits<Src>; 225 using SrcLimits = std::numeric_limits<Src>;
219 using DstLimits = typename std::numeric_limits<Dst>; 226 using DstLimits = typename std::numeric_limits<Dst>;
220 227
221 // Computes the mask required to make an accurate comparison between types. 228 // Computes the mask required to make an accurate comparison between types.
222 static const int kShift = 229 static const int kShift =
223 (MaxExponent<Src>::value > MaxExponent<Dst>::value && 230 (MaxExponent<Src>::value > MaxExponent<Dst>::value &&
224 SrcLimits::digits < DstLimits::digits) 231 SrcLimits::digits < DstLimits::digits)
225 ? (DstLimits::digits - SrcLimits::digits) 232 ? (DstLimits::digits - SrcLimits::digits)
226 : 0; 233 : 0;
227 template < 234 template <
228 typename T, 235 typename T,
(...skipping 17 matching lines...) Expand all
246 static_assert(kShift == 0, ""); 253 static_assert(kShift == 0, "");
247 return value; 254 return value;
248 } 255 }
249 256
250 static constexpr Dst max() { return Adjust(Bounds<Dst>::max()); } 257 static constexpr Dst max() { return Adjust(Bounds<Dst>::max()); }
251 static constexpr Dst lowest() { return Adjust(Bounds<Dst>::lowest()); } 258 static constexpr Dst lowest() { return Adjust(Bounds<Dst>::lowest()); }
252 }; 259 };
253 260
254 template <typename Dst, 261 template <typename Dst,
255 typename Src, 262 typename Src,
263 template <typename> class Bounds,
256 IntegerRepresentation DstSign = std::is_signed<Dst>::value 264 IntegerRepresentation DstSign = std::is_signed<Dst>::value
257 ? INTEGER_REPRESENTATION_SIGNED 265 ? INTEGER_REPRESENTATION_SIGNED
258 : INTEGER_REPRESENTATION_UNSIGNED, 266 : INTEGER_REPRESENTATION_UNSIGNED,
259 IntegerRepresentation SrcSign = std::is_signed<Src>::value 267 IntegerRepresentation SrcSign = std::is_signed<Src>::value
260 ? INTEGER_REPRESENTATION_SIGNED 268 ? INTEGER_REPRESENTATION_SIGNED
261 : INTEGER_REPRESENTATION_UNSIGNED, 269 : INTEGER_REPRESENTATION_UNSIGNED,
262 NumericRangeRepresentation DstRange = 270 NumericRangeRepresentation DstRange =
263 StaticDstRangeRelationToSrcRange<Dst, Src>::value> 271 StaticDstRangeRelationToSrcRange<Dst, Src>::value>
264 struct DstRangeRelationToSrcRangeImpl; 272 struct DstRangeRelationToSrcRangeImpl;
265 273
266 // The following templates are for ranges that must be verified at runtime. We 274 // The following templates are for ranges that must be verified at runtime. We
267 // split it into checks based on signedness to avoid confusing casts and 275 // split it into checks based on signedness to avoid confusing casts and
268 // compiler warnings on signed an unsigned comparisons. 276 // compiler warnings on signed an unsigned comparisons.
269 277
270 // Dst range is statically determined to contain Src: Nothing to check. 278 // Same sign narrowing: The range is contained for normal limits.
271 template <typename Dst, 279 template <typename Dst,
272 typename Src, 280 typename Src,
281 template <typename> class Bounds,
273 IntegerRepresentation DstSign, 282 IntegerRepresentation DstSign,
274 IntegerRepresentation SrcSign> 283 IntegerRepresentation SrcSign>
275 struct DstRangeRelationToSrcRangeImpl<Dst, 284 struct DstRangeRelationToSrcRangeImpl<Dst,
276 Src, 285 Src,
286 Bounds,
277 DstSign, 287 DstSign,
278 SrcSign, 288 SrcSign,
279 NUMERIC_RANGE_CONTAINED> { 289 NUMERIC_RANGE_CONTAINED> {
280 static constexpr RangeConstraint Check(Src value) { return RANGE_VALID; } 290 static constexpr RangeCheck Check(Src value) {
291 using SrcLimits = std::numeric_limits<Src>;
292 using DstLimits = NarrowingRange<Dst, Src, Bounds>;
293 return RangeCheck(
294 static_cast<Dst>(SrcLimits::max()) <= DstLimits::max() ||
295 static_cast<Dst>(value) <= DstLimits::max(),
296 static_cast<Dst>(SrcLimits::lowest()) >= DstLimits::lowest() ||
297 static_cast<Dst>(value) >= DstLimits::lowest());
298 }
281 }; 299 };
282 300
283 // Signed to signed narrowing: Both the upper and lower boundaries may be 301 // Signed to signed narrowing: Both the upper and lower boundaries may be
284 // exceeded. 302 // exceeded for standard limits.
285 template <typename Dst, typename Src> 303 template <typename Dst, typename Src, template <typename> class Bounds>
286 struct DstRangeRelationToSrcRangeImpl<Dst, 304 struct DstRangeRelationToSrcRangeImpl<Dst,
287 Src, 305 Src,
306 Bounds,
288 INTEGER_REPRESENTATION_SIGNED, 307 INTEGER_REPRESENTATION_SIGNED,
289 INTEGER_REPRESENTATION_SIGNED, 308 INTEGER_REPRESENTATION_SIGNED,
290 NUMERIC_RANGE_NOT_CONTAINED> { 309 NUMERIC_RANGE_NOT_CONTAINED> {
291 static constexpr RangeConstraint Check(Src value) { 310 static constexpr RangeCheck Check(Src value) {
292 return GetRangeConstraint((value <= NarrowingRange<Dst, Src>::max()), 311 using DstLimits = NarrowingRange<Dst, Src, Bounds>;
293 (value >= NarrowingRange<Dst, Src>::lowest())); 312 return RangeCheck(value <= DstLimits::max(), value >= DstLimits::lowest());
294 } 313 }
295 }; 314 };
296 315
297 // Unsigned to unsigned narrowing: Only the upper boundary can be exceeded. 316 // Unsigned to unsigned narrowing: Only the upper bound can be exceeded for
298 template <typename Dst, typename Src> 317 // standard limits.
318 template <typename Dst, typename Src, template <typename> class Bounds>
299 struct DstRangeRelationToSrcRangeImpl<Dst, 319 struct DstRangeRelationToSrcRangeImpl<Dst,
300 Src, 320 Src,
321 Bounds,
301 INTEGER_REPRESENTATION_UNSIGNED, 322 INTEGER_REPRESENTATION_UNSIGNED,
302 INTEGER_REPRESENTATION_UNSIGNED, 323 INTEGER_REPRESENTATION_UNSIGNED,
303 NUMERIC_RANGE_NOT_CONTAINED> { 324 NUMERIC_RANGE_NOT_CONTAINED> {
304 static constexpr RangeConstraint Check(Src value) { 325 static constexpr RangeCheck Check(Src value) {
305 return GetRangeConstraint(value <= NarrowingRange<Dst, Src>::max(), true); 326 using DstLimits = NarrowingRange<Dst, Src, Bounds>;
327 return RangeCheck(
328 value <= DstLimits::max(),
329 DstLimits::lowest() == Dst(0) || value >= DstLimits::lowest());
306 } 330 }
307 }; 331 };
308 332
309 // Unsigned to signed: The upper boundary may be exceeded. 333 // Unsigned to signed: Only the upper bound can be exceeded for standard limits.
310 template <typename Dst, typename Src> 334 template <typename Dst, typename Src, template <typename> class Bounds>
311 struct DstRangeRelationToSrcRangeImpl<Dst, 335 struct DstRangeRelationToSrcRangeImpl<Dst,
312 Src, 336 Src,
337 Bounds,
313 INTEGER_REPRESENTATION_SIGNED, 338 INTEGER_REPRESENTATION_SIGNED,
314 INTEGER_REPRESENTATION_UNSIGNED, 339 INTEGER_REPRESENTATION_UNSIGNED,
315 NUMERIC_RANGE_NOT_CONTAINED> { 340 NUMERIC_RANGE_NOT_CONTAINED> {
316 static constexpr RangeConstraint Check(Src value) { 341 static constexpr RangeCheck Check(Src value) {
317 return IntegerBitsPlusSign<Dst>::value > IntegerBitsPlusSign<Src>::value 342 using DstLimits = NarrowingRange<Dst, Src, Bounds>;
318 ? RANGE_VALID 343 using Promotion = decltype(Src() + Dst());
319 : GetRangeConstraint( 344 return RangeCheck(static_cast<Promotion>(value) <=
320 value <= static_cast<Src>(NarrowingRange<Dst, Src>::max()), 345 static_cast<Promotion>(DstLimits::max()),
321 true); 346 DstLimits::lowest() <= Dst(0) ||
347 static_cast<Promotion>(value) >=
348 static_cast<Promotion>(DstLimits::lowest()));
322 } 349 }
323 }; 350 };
324 351
325 // Signed to unsigned: The upper boundary may be exceeded for a narrower Dst, 352 // Signed to unsigned: The upper boundary may be exceeded for a narrower Dst,
326 // and any negative value exceeds the lower boundary. 353 // and any negative value exceeds the lower boundary for standard limits.
327 template <typename Dst, typename Src> 354 template <typename Dst, typename Src, template <typename> class Bounds>
328 struct DstRangeRelationToSrcRangeImpl<Dst, 355 struct DstRangeRelationToSrcRangeImpl<Dst,
329 Src, 356 Src,
357 Bounds,
330 INTEGER_REPRESENTATION_UNSIGNED, 358 INTEGER_REPRESENTATION_UNSIGNED,
331 INTEGER_REPRESENTATION_SIGNED, 359 INTEGER_REPRESENTATION_SIGNED,
332 NUMERIC_RANGE_NOT_CONTAINED> { 360 NUMERIC_RANGE_NOT_CONTAINED> {
333 static constexpr RangeConstraint Check(Src value) { 361 static constexpr RangeCheck Check(Src value) {
334 return (MaxExponent<Dst>::value >= MaxExponent<Src>::value) 362 using SrcLimits = std::numeric_limits<Src>;
335 ? GetRangeConstraint(true, value >= static_cast<Src>(0)) 363 using DstLimits = NarrowingRange<Dst, Src, Bounds>;
336 : GetRangeConstraint( 364 using Promotion = decltype(Src() + Dst());
337 value <= static_cast<Src>(NarrowingRange<Dst, Src>::max()), 365 return RangeCheck(
338 value >= static_cast<Src>(0)); 366 static_cast<Promotion>(SrcLimits::max()) <=
367 static_cast<Promotion>(DstLimits::max()) ||
368 static_cast<Promotion>(value) <=
369 static_cast<Promotion>(DstLimits::max()),
370 value >= Src(0) && (DstLimits::lowest() == 0 ||
371 static_cast<Dst>(value) >= DstLimits::lowest()));
339 } 372 }
340 }; 373 };
341 374
342 template <typename Dst, typename Src> 375 template <typename Dst,
343 constexpr RangeConstraint DstRangeRelationToSrcRange(Src value) { 376 template <typename> class Bounds = std::numeric_limits,
377 typename Src>
378 constexpr RangeCheck DstRangeRelationToSrcRange(Src value) {
344 static_assert(std::is_arithmetic<Src>::value, "Argument must be numeric."); 379 static_assert(std::is_arithmetic<Src>::value, "Argument must be numeric.");
345 static_assert(std::is_arithmetic<Dst>::value, "Result must be numeric."); 380 static_assert(std::is_arithmetic<Dst>::value, "Result must be numeric.");
346 return DstRangeRelationToSrcRangeImpl<Dst, Src>::Check(value); 381 static_assert(Bounds<Dst>::lowest() < Bounds<Dst>::max(), "");
382 return DstRangeRelationToSrcRangeImpl<Dst, Src, Bounds>::Check(value);
347 } 383 }
348 384
349 // Integer promotion templates used by the portable checked integer arithmetic. 385 // Integer promotion templates used by the portable checked integer arithmetic.
350 template <size_t Size, bool IsSigned> 386 template <size_t Size, bool IsSigned>
351 struct IntegerForDigitsAndSign; 387 struct IntegerForDigitsAndSign;
352 388
353 #define INTEGER_FOR_DIGITS_AND_SIGN(I) \ 389 #define INTEGER_FOR_DIGITS_AND_SIGN(I) \
354 template <> \ 390 template <> \
355 struct IntegerForDigitsAndSign<IntegerBitsPlusSign<I>::value, \ 391 struct IntegerForDigitsAndSign<IntegerBitsPlusSign<I>::value, \
356 std::is_signed<I>::value> { \ 392 std::is_signed<I>::value> { \
(...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after
568 template <typename L, typename R> 604 template <typename L, typename R>
569 struct IsStrictOp { 605 struct IsStrictOp {
570 static const bool value = 606 static const bool value =
571 UnderlyingType<L>::is_numeric && UnderlyingType<R>::is_numeric && 607 UnderlyingType<L>::is_numeric && UnderlyingType<R>::is_numeric &&
572 (UnderlyingType<L>::is_strict || UnderlyingType<R>::is_strict); 608 (UnderlyingType<L>::is_strict || UnderlyingType<R>::is_strict);
573 }; 609 };
574 610
575 template <typename L, typename R> 611 template <typename L, typename R>
576 constexpr bool IsLessImpl(const L lhs, 612 constexpr bool IsLessImpl(const L lhs,
577 const R rhs, 613 const R rhs,
578 const RangeConstraint l_range, 614 const RangeCheck l_range,
579 const RangeConstraint r_range) { 615 const RangeCheck r_range) {
580 return l_range == RANGE_UNDERFLOW || r_range == RANGE_OVERFLOW || 616 return l_range == RANGE_UNDERFLOW || r_range == RANGE_OVERFLOW ||
581 (l_range == r_range && 617 (l_range == r_range &&
582 static_cast<decltype(lhs + rhs)>(lhs) < 618 static_cast<decltype(lhs + rhs)>(lhs) <
583 static_cast<decltype(lhs + rhs)>(rhs)); 619 static_cast<decltype(lhs + rhs)>(rhs));
584 } 620 }
585 621
586 template <typename L, typename R> 622 template <typename L, typename R>
587 struct IsLess { 623 struct IsLess {
588 static_assert(std::is_arithmetic<L>::value && std::is_arithmetic<R>::value, 624 static_assert(std::is_arithmetic<L>::value && std::is_arithmetic<R>::value,
589 "Types must be numeric."); 625 "Types must be numeric.");
590 static constexpr bool Test(const L lhs, const R rhs) { 626 static constexpr bool Test(const L lhs, const R rhs) {
591 return IsLessImpl(lhs, rhs, DstRangeRelationToSrcRange<R>(lhs), 627 return IsLessImpl(lhs, rhs, DstRangeRelationToSrcRange<R>(lhs),
592 DstRangeRelationToSrcRange<L>(rhs)); 628 DstRangeRelationToSrcRange<L>(rhs));
593 } 629 }
594 }; 630 };
595 631
596 template <typename L, typename R> 632 template <typename L, typename R>
597 constexpr bool IsLessOrEqualImpl(const L lhs, 633 constexpr bool IsLessOrEqualImpl(const L lhs,
598 const R rhs, 634 const R rhs,
599 const RangeConstraint l_range, 635 const RangeCheck l_range,
600 const RangeConstraint r_range) { 636 const RangeCheck r_range) {
601 return l_range == RANGE_UNDERFLOW || r_range == RANGE_OVERFLOW || 637 return l_range == RANGE_UNDERFLOW || r_range == RANGE_OVERFLOW ||
602 (l_range == r_range && 638 (l_range == r_range &&
603 static_cast<decltype(lhs + rhs)>(lhs) <= 639 static_cast<decltype(lhs + rhs)>(lhs) <=
604 static_cast<decltype(lhs + rhs)>(rhs)); 640 static_cast<decltype(lhs + rhs)>(rhs));
605 } 641 }
606 642
607 template <typename L, typename R> 643 template <typename L, typename R>
608 struct IsLessOrEqual { 644 struct IsLessOrEqual {
609 static_assert(std::is_arithmetic<L>::value && std::is_arithmetic<R>::value, 645 static_assert(std::is_arithmetic<L>::value && std::is_arithmetic<R>::value,
610 "Types must be numeric."); 646 "Types must be numeric.");
611 static constexpr bool Test(const L lhs, const R rhs) { 647 static constexpr bool Test(const L lhs, const R rhs) {
612 return IsLessOrEqualImpl(lhs, rhs, DstRangeRelationToSrcRange<R>(lhs), 648 return IsLessOrEqualImpl(lhs, rhs, DstRangeRelationToSrcRange<R>(lhs),
613 DstRangeRelationToSrcRange<L>(rhs)); 649 DstRangeRelationToSrcRange<L>(rhs));
614 } 650 }
615 }; 651 };
616 652
617 template <typename L, typename R> 653 template <typename L, typename R>
618 constexpr bool IsGreaterImpl(const L lhs, 654 constexpr bool IsGreaterImpl(const L lhs,
619 const R rhs, 655 const R rhs,
620 const RangeConstraint l_range, 656 const RangeCheck l_range,
621 const RangeConstraint r_range) { 657 const RangeCheck r_range) {
622 return l_range == RANGE_OVERFLOW || r_range == RANGE_UNDERFLOW || 658 return l_range == RANGE_OVERFLOW || r_range == RANGE_UNDERFLOW ||
623 (l_range == r_range && 659 (l_range == r_range &&
624 static_cast<decltype(lhs + rhs)>(lhs) > 660 static_cast<decltype(lhs + rhs)>(lhs) >
625 static_cast<decltype(lhs + rhs)>(rhs)); 661 static_cast<decltype(lhs + rhs)>(rhs));
626 } 662 }
627 663
628 template <typename L, typename R> 664 template <typename L, typename R>
629 struct IsGreater { 665 struct IsGreater {
630 static_assert(std::is_arithmetic<L>::value && std::is_arithmetic<R>::value, 666 static_assert(std::is_arithmetic<L>::value && std::is_arithmetic<R>::value,
631 "Types must be numeric."); 667 "Types must be numeric.");
632 static constexpr bool Test(const L lhs, const R rhs) { 668 static constexpr bool Test(const L lhs, const R rhs) {
633 return IsGreaterImpl(lhs, rhs, DstRangeRelationToSrcRange<R>(lhs), 669 return IsGreaterImpl(lhs, rhs, DstRangeRelationToSrcRange<R>(lhs),
634 DstRangeRelationToSrcRange<L>(rhs)); 670 DstRangeRelationToSrcRange<L>(rhs));
635 } 671 }
636 }; 672 };
637 673
638 template <typename L, typename R> 674 template <typename L, typename R>
639 constexpr bool IsGreaterOrEqualImpl(const L lhs, 675 constexpr bool IsGreaterOrEqualImpl(const L lhs,
640 const R rhs, 676 const R rhs,
641 const RangeConstraint l_range, 677 const RangeCheck l_range,
642 const RangeConstraint r_range) { 678 const RangeCheck r_range) {
643 return l_range == RANGE_OVERFLOW || r_range == RANGE_UNDERFLOW || 679 return l_range == RANGE_OVERFLOW || r_range == RANGE_UNDERFLOW ||
644 (l_range == r_range && 680 (l_range == r_range &&
645 static_cast<decltype(lhs + rhs)>(lhs) >= 681 static_cast<decltype(lhs + rhs)>(lhs) >=
646 static_cast<decltype(lhs + rhs)>(rhs)); 682 static_cast<decltype(lhs + rhs)>(rhs));
647 } 683 }
648 684
649 template <typename L, typename R> 685 template <typename L, typename R>
650 struct IsGreaterOrEqual { 686 struct IsGreaterOrEqual {
651 static_assert(std::is_arithmetic<L>::value && std::is_arithmetic<R>::value, 687 static_assert(std::is_arithmetic<L>::value && std::is_arithmetic<R>::value,
652 "Types must be numeric."); 688 "Types must be numeric.");
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
694 static_cast<BigType>(static_cast<L>(lhs)), 730 static_cast<BigType>(static_cast<L>(lhs)),
695 static_cast<BigType>(static_cast<R>(rhs))) 731 static_cast<BigType>(static_cast<R>(rhs)))
696 // Let the template functions figure it out for mixed types. 732 // Let the template functions figure it out for mixed types.
697 : C<L, R>::Test(lhs, rhs); 733 : C<L, R>::Test(lhs, rhs);
698 }; 734 };
699 735
700 } // namespace internal 736 } // namespace internal
701 } // namespace base 737 } // namespace base
702 738
703 #endif // BASE_NUMERICS_SAFE_CONVERSIONS_IMPL_H_ 739 #endif // BASE_NUMERICS_SAFE_CONVERSIONS_IMPL_H_
OLDNEW
« no previous file with comments | « base/numerics/safe_conversions.h ('k') | base/numerics/safe_numerics_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698