| 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 29 matching lines...) Expand all Loading... |
| 40 | 40 |
| 41 inline int ToRoundedInt(float value) { | 41 inline int ToRoundedInt(float value) { |
| 42 float rounded; | 42 float rounded; |
| 43 if (value >= 0.0f) | 43 if (value >= 0.0f) |
| 44 rounded = std::floor(value + 0.5f); | 44 rounded = std::floor(value + 0.5f); |
| 45 else | 45 else |
| 46 rounded = std::ceil(value - 0.5f); | 46 rounded = std::ceil(value - 0.5f); |
| 47 return ClampToInt(rounded); | 47 return ClampToInt(rounded); |
| 48 } | 48 } |
| 49 | 49 |
| 50 inline int ToRoundedInt(double value) { |
| 51 double rounded; |
| 52 if (value >= 0.0) |
| 53 rounded = std::floor(value + 0.5); |
| 54 else |
| 55 rounded = std::ceil(value - 0.5); |
| 56 return ClampToInt(rounded); |
| 57 } |
| 58 |
| 50 inline bool IsExpressibleAsInt(float value) { | 59 inline bool IsExpressibleAsInt(float value) { |
| 51 if (value != value) | 60 if (value != value) |
| 52 return false; // no int NaN. | 61 return false; // no int NaN. |
| 53 if (value > std::numeric_limits<int>::max()) | 62 if (value > std::numeric_limits<int>::max()) |
| 54 return false; | 63 return false; |
| 55 if (value < std::numeric_limits<int>::min()) | 64 if (value < std::numeric_limits<int>::min()) |
| 56 return false; | 65 return false; |
| 57 return true; | 66 return true; |
| 58 } | 67 } |
| 59 | 68 |
| 60 } // namespace gfx | 69 } // namespace gfx |
| 61 | 70 |
| 62 #endif // UI_GFX_GEOMETRY_SAFE_INTEGER_CONVERSIONS_H_ | 71 #endif // UI_GFX_GEOMETRY_SAFE_INTEGER_CONVERSIONS_H_ |
| OLD | NEW |