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
3 // found in the LICENSE file.
5 #ifndef UI_GFX_GEOMETRY_SAFE_INTEGER_CONVERSIONS_H_
6 #define UI_GFX_GEOMETRY_SAFE_INTEGER_CONVERSIONS_H_
11 #include "base/numerics/safe_conversions.h"
12 #include "ui/gfx/gfx_export.h"
16 inline int ToFlooredInt(float value
) {
17 return base::saturated_cast
<int>(std::floor(value
));
20 inline int ToCeiledInt(float value
) {
21 return base::saturated_cast
<int>(std::ceil(value
));
24 inline int ToFlooredInt(double value
) {
25 return base::saturated_cast
<int>(std::floor(value
));
28 inline int ToCeiledInt(double value
) {
29 return base::saturated_cast
<int>(std::ceil(value
));
32 inline int ToRoundedInt(float value
) {
35 rounded
= std::floor(value
+ 0.5f
);
37 rounded
= std::ceil(value
- 0.5f
);
38 return base::saturated_cast
<int>(rounded
);
41 inline int ToRoundedInt(double value
) {
44 rounded
= std::floor(value
+ 0.5);
46 rounded
= std::ceil(value
- 0.5);
47 return base::saturated_cast
<int>(rounded
);
50 inline bool IsExpressibleAsInt(float value
) {
52 return false; // no int NaN.
53 if (value
> std::numeric_limits
<int>::max())
55 if (value
< std::numeric_limits
<int>::min())
62 #endif // UI_GFX_GEOMETRY_SAFE_INTEGER_CONVERSIONS_H_