| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 UI_GFX_GEOMETRY_SAFE_INTEGER_CONVERSIONS_H_ | 5 #ifndef UI_GFX_GEOMETRY_SAFE_INTEGER_CONVERSIONS_H_ |
| 6 #define UI_GFX_GEOMETRY_SAFE_INTEGER_CONVERSIONS_H_ | 6 #define UI_GFX_GEOMETRY_SAFE_INTEGER_CONVERSIONS_H_ |
| 7 | 7 |
| 8 #include <cmath> | 8 #include <cmath> |
| 9 #include <limits> | 9 #include <limits> |
| 10 | 10 |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 50 inline bool IsExpressibleAsInt(float value) { | 50 inline bool IsExpressibleAsInt(float value) { |
| 51 if (value != value) | 51 if (value != value) |
| 52 return false; // no int NaN. | 52 return false; // no int NaN. |
| 53 if (value > std::numeric_limits<int>::max()) | 53 if (value > std::numeric_limits<int>::max()) |
| 54 return false; | 54 return false; |
| 55 if (value < std::numeric_limits<int>::min()) | 55 if (value < std::numeric_limits<int>::min()) |
| 56 return false; | 56 return false; |
| 57 return true; | 57 return true; |
| 58 } | 58 } |
| 59 | 59 |
| 60 // Returns true iff a+b would overflow max int. | |
| 61 constexpr bool AddWouldOverflow(int a, int b) { | |
| 62 // In this function, GCC tries to make optimizations that would only | |
| 63 // work if max - a wouldn't overflow but it isn't smart enough to notice that | |
| 64 // a > 0. So cast everything to unsigned to avoid this. As it is guaranteed | |
| 65 // that max - a and b are both already positive, the cast is a noop. | |
| 66 // | |
| 67 // This is intended to be: a > 0 && max - a < b | |
| 68 return a > 0 && b > 0 && | |
| 69 static_cast<unsigned>(std::numeric_limits<int>::max() - a) < | |
| 70 static_cast<unsigned>(b); | |
| 71 } | |
| 72 | |
| 73 // Returns true iff a+b would underflow min int. | |
| 74 constexpr bool AddWouldUnderflow(int a, int b) { | |
| 75 return a < 0 && b < std::numeric_limits<int>::min() - a; | |
| 76 } | |
| 77 | |
| 78 // Returns true iff a-b would overflow max int. | |
| 79 constexpr bool SubtractWouldOverflow(int a, int b) { | |
| 80 return b < 0 && a > std::numeric_limits<int>::max() + b; | |
| 81 } | |
| 82 | |
| 83 // Returns true iff a-b would underflow min int. | |
| 84 constexpr bool SubtractWouldUnderflow(int a, int b) { | |
| 85 return b > 0 && a < std::numeric_limits<int>::min() + b; | |
| 86 } | |
| 87 | |
| 88 // Safely adds a+b without integer overflow/underflow. Exceeding these | |
| 89 // bounds will clamp to the max or min int limit. | |
| 90 constexpr int SafeAdd(int a, int b) { | |
| 91 return AddWouldOverflow(a, b) | |
| 92 ? std::numeric_limits<int>::max() | |
| 93 : AddWouldUnderflow(a, b) ? std::numeric_limits<int>::min() | |
| 94 : a + b; | |
| 95 } | |
| 96 | |
| 97 // Safely subtracts a-b without integer overflow/underflow. Exceeding these | |
| 98 // bounds will clamp to the max or min int limit. | |
| 99 constexpr int SafeSubtract(int a, int b) { | |
| 100 return SubtractWouldOverflow(a, b) | |
| 101 ? std::numeric_limits<int>::max() | |
| 102 : SubtractWouldUnderflow(a, b) ? std::numeric_limits<int>::min() | |
| 103 : a - b; | |
| 104 } | |
| 105 | |
| 106 } // namespace gfx | 60 } // namespace gfx |
| 107 | 61 |
| 108 #endif // UI_GFX_GEOMETRY_SAFE_INTEGER_CONVERSIONS_H_ | 62 #endif // UI_GFX_GEOMETRY_SAFE_INTEGER_CONVERSIONS_H_ |
| OLD | NEW |