| 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 #include "ui/gfx/safe_integer_conversions.h" |
| 6 |
| 5 #include <cmath> | 7 #include <cmath> |
| 6 #include <limits> | 8 #include <limits> |
| 7 | 9 |
| 8 namespace gfx { | 10 namespace gfx { |
| 9 | 11 |
| 10 int ClampToInt(float value) { | 12 int ClampToInt(float value) { |
| 11 if (value != value) | 13 if (value != value) |
| 12 return 0; // no int NaN. | 14 return 0; // no int NaN. |
| 13 if (value > std::numeric_limits<int>::max()) | 15 if (value >= std::numeric_limits<int>::max()) |
| 14 return std::numeric_limits<int>::max(); | 16 return std::numeric_limits<int>::max(); |
| 15 if (value < std::numeric_limits<int>::min()) | 17 if (value <= std::numeric_limits<int>::min()) |
| 16 return std::numeric_limits<int>::min(); | 18 return std::numeric_limits<int>::min(); |
| 17 return static_cast<int>(value); | 19 return static_cast<int>(value); |
| 18 } | 20 } |
| 19 | 21 |
| 20 int ToFlooredInt(float value) { | 22 int ToFlooredInt(float value) { |
| 21 return ClampToInt(std::floor(value)); | 23 return ClampToInt(std::floor(value)); |
| 22 } | 24 } |
| 23 | 25 |
| 24 int ToCeiledInt(float value) { | 26 int ToCeiledInt(float value) { |
| 25 return ClampToInt(std::ceil(value)); | 27 return ClampToInt(std::ceil(value)); |
| 26 } | 28 } |
| 27 | 29 |
| 30 int ToRoundedInt(float value) { |
| 31 float rounded; |
| 32 if (value >= 0.0f) |
| 33 rounded = std::floor(value + 0.5f); |
| 34 else |
| 35 rounded = std::ceil(value - 0.5f); |
| 36 return ClampToInt(rounded); |
| 37 } |
| 38 |
| 28 } // namespace gfx | 39 } // namespace gfx |
| 29 | |
| OLD | NEW |