[SyncFS] Build indexes from FileTracker entries on disk.
[chromium-blink-merge.git] / ui / gfx / geometry / safe_integer_conversions.h
blob4d289ecabafb665386b25ea58704cea22d737440
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 "ui/gfx/gfx_export.h"
13 namespace gfx {
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);
25 inline int ToFlooredInt(float value) {
26 return ClampToInt(std::floor(value));
29 inline int ToCeiledInt(float value) {
30 return ClampToInt(std::ceil(value));
33 inline int ToRoundedInt(float value) {
34 float rounded;
35 if (value >= 0.0f)
36 rounded = std::floor(value + 0.5f);
37 else
38 rounded = std::ceil(value - 0.5f);
39 return ClampToInt(rounded);
42 inline bool IsExpressibleAsInt(float value) {
43 if (value != value)
44 return false; // no int NaN.
45 if (value > std::numeric_limits<int>::max())
46 return false;
47 if (value < std::numeric_limits<int>::min())
48 return false;
49 return true;
52 } // namespace gfx
54 #endif // UI_GFX_GEOMETRY_SAFE_INTEGER_CONVERSIONS_H_