Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ui / gfx / geometry / safe_integer_conversions.h
blob5efe134f0793ba51f1ba0908e04b9c987e638073
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_
8 #include <cmath>
9 #include <limits>
11 #include "base/numerics/safe_conversions.h"
12 #include "ui/gfx/gfx_export.h"
14 namespace gfx {
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) {
33 float rounded;
34 if (value >= 0.0f)
35 rounded = std::floor(value + 0.5f);
36 else
37 rounded = std::ceil(value - 0.5f);
38 return base::saturated_cast<int>(rounded);
41 inline int ToRoundedInt(double value) {
42 double rounded;
43 if (value >= 0.0)
44 rounded = std::floor(value + 0.5);
45 else
46 rounded = std::ceil(value - 0.5);
47 return base::saturated_cast<int>(rounded);
50 inline bool IsExpressibleAsInt(float value) {
51 if (value != value)
52 return false; // no int NaN.
53 if (value > std::numeric_limits<int>::max())
54 return false;
55 if (value < std::numeric_limits<int>::min())
56 return false;
57 return true;
60 } // namespace gfx
62 #endif // UI_GFX_GEOMETRY_SAFE_INTEGER_CONVERSIONS_H_