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

Side by Side Diff: ui/gfx/geometry/safe_integer_conversions.h

Issue 1164063005: Replace gfx::ClampToInt with base::saturated_cast. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: clamp: . Created 5 years, 6 months 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 | « ui/gfx/geometry/rect_unittest.cc ('k') | ui/gfx/geometry/safe_integer_conversions_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 (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
11 #include "base/numerics/safe_conversions.h"
11 #include "ui/gfx/gfx_export.h" 12 #include "ui/gfx/gfx_export.h"
12 13
13 namespace gfx { 14 namespace gfx {
14 15
15 inline int ClampToInt(float value) {
16 if (value != value)
17 return 0; // no int NaN.
18 if (value >= std::numeric_limits<int>::max())
19 return std::numeric_limits<int>::max();
20 if (value <= std::numeric_limits<int>::min())
21 return std::numeric_limits<int>::min();
22 return static_cast<int>(value);
23 }
24
25 inline int ToFlooredInt(float value) { 16 inline int ToFlooredInt(float value) {
26 return ClampToInt(std::floor(value)); 17 return base::saturated_cast<int>(std::floor(value));
27 } 18 }
28 19
29 inline int ToCeiledInt(float value) { 20 inline int ToCeiledInt(float value) {
30 return ClampToInt(std::ceil(value)); 21 return base::saturated_cast<int>(std::ceil(value));
31 } 22 }
32 23
33 inline int ToFlooredInt(double value) { 24 inline int ToFlooredInt(double value) {
34 return ClampToInt(std::floor(value)); 25 return base::saturated_cast<int>(std::floor(value));
35 } 26 }
36 27
37 inline int ToCeiledInt(double value) { 28 inline int ToCeiledInt(double value) {
38 return ClampToInt(std::ceil(value)); 29 return base::saturated_cast<int>(std::ceil(value));
39 } 30 }
40 31
41 inline int ToRoundedInt(float value) { 32 inline int ToRoundedInt(float value) {
42 float rounded; 33 float rounded;
43 if (value >= 0.0f) 34 if (value >= 0.0f)
44 rounded = std::floor(value + 0.5f); 35 rounded = std::floor(value + 0.5f);
45 else 36 else
46 rounded = std::ceil(value - 0.5f); 37 rounded = std::ceil(value - 0.5f);
47 return ClampToInt(rounded); 38 return base::saturated_cast<int>(rounded);
48 } 39 }
49 40
50 inline int ToRoundedInt(double value) { 41 inline int ToRoundedInt(double value) {
51 double rounded; 42 double rounded;
52 if (value >= 0.0) 43 if (value >= 0.0)
53 rounded = std::floor(value + 0.5); 44 rounded = std::floor(value + 0.5);
54 else 45 else
55 rounded = std::ceil(value - 0.5); 46 rounded = std::ceil(value - 0.5);
56 return ClampToInt(rounded); 47 return base::saturated_cast<int>(rounded);
57 } 48 }
58 49
59 inline bool IsExpressibleAsInt(float value) { 50 inline bool IsExpressibleAsInt(float value) {
60 if (value != value) 51 if (value != value)
61 return false; // no int NaN. 52 return false; // no int NaN.
62 if (value > std::numeric_limits<int>::max()) 53 if (value > std::numeric_limits<int>::max())
63 return false; 54 return false;
64 if (value < std::numeric_limits<int>::min()) 55 if (value < std::numeric_limits<int>::min())
65 return false; 56 return false;
66 return true; 57 return true;
67 } 58 }
68 59
69 } // namespace gfx 60 } // namespace gfx
70 61
71 #endif // UI_GFX_GEOMETRY_SAFE_INTEGER_CONVERSIONS_H_ 62 #endif // UI_GFX_GEOMETRY_SAFE_INTEGER_CONVERSIONS_H_
OLDNEW
« no previous file with comments | « ui/gfx/geometry/rect_unittest.cc ('k') | ui/gfx/geometry/safe_integer_conversions_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698