Commit a7d1c04d authored by jamesr@chromium.org's avatar jamesr@chromium.org

Inline gfx::Vector2d* ctors and integer conversions

This speeds up cc_perftests by 2.85%. 0.7% is from the vector constructors, the
remainder is from inlining the safe_integer_conversion routines.

BUG=


Review URL: https://chromiumcodereview.appspot.com/11414284

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@170775 0039d316-1c4b-4281-b951-d872f2087c98
parent 69177a72
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/gfx/safe_integer_conversions.h"
#include <cmath>
#include <limits>
namespace gfx {
int ClampToInt(float value) {
if (value != value)
return 0; // no int NaN.
if (value >= std::numeric_limits<int>::max())
return std::numeric_limits<int>::max();
if (value <= std::numeric_limits<int>::min())
return std::numeric_limits<int>::min();
return static_cast<int>(value);
}
int ToFlooredInt(float value) {
return ClampToInt(std::floor(value));
}
int ToCeiledInt(float value) {
return ClampToInt(std::ceil(value));
}
int ToRoundedInt(float value) {
float rounded;
if (value >= 0.0f)
rounded = std::floor(value + 0.5f);
else
rounded = std::ceil(value - 0.5f);
return ClampToInt(rounded);
}
bool IsExpressibleAsInt(float value) {
if (value != value)
return false; // no int NaN.
if (value > std::numeric_limits<int>::max())
return false;
if (value < std::numeric_limits<int>::min())
return false;
return true;
}
} // namespace gfx
......@@ -5,15 +5,49 @@
#ifndef UI_GFX_SAFE_INTEGER_CONVERSIONS_H_
#define UI_GFX_SAFE_INTEGER_CONVERSIONS_H_
#include <cmath>
#include <limits>
#include "ui/base/ui_export.h"
namespace gfx {
UI_EXPORT int ClampToInt(float value);
UI_EXPORT int ToFlooredInt(float value);
UI_EXPORT int ToCeiledInt(float value);
UI_EXPORT int ToRoundedInt(float value);
UI_EXPORT bool IsExpressibleAsInt(float value);
inline int ClampToInt(float value) {
if (value != value)
return 0; // no int NaN.
if (value >= std::numeric_limits<int>::max())
return std::numeric_limits<int>::max();
if (value <= std::numeric_limits<int>::min())
return std::numeric_limits<int>::min();
return static_cast<int>(value);
}
inline int ToFlooredInt(float value) {
return ClampToInt(std::floor(value));
}
inline int ToCeiledInt(float value) {
return ClampToInt(std::ceil(value));
}
inline int ToRoundedInt(float value) {
float rounded;
if (value >= 0.0f)
rounded = std::floor(value + 0.5f);
else
rounded = std::ceil(value - 0.5f);
return ClampToInt(rounded);
}
inline bool IsExpressibleAsInt(float value) {
if (value != value)
return false; // no int NaN.
if (value > std::numeric_limits<int>::max())
return false;
if (value < std::numeric_limits<int>::min())
return false;
return true;
}
} // namespace gfx
......
......@@ -10,12 +10,6 @@
namespace gfx {
Vector2d::Vector2d() : x_(0), y_(0) {
}
Vector2d::Vector2d(int x, int y) : x_(x), y_(y) {
}
bool Vector2d::IsZero() const {
return x_ == 0 && y_ == 0;
}
......
......@@ -20,8 +20,8 @@ namespace gfx {
class UI_EXPORT Vector2d {
public:
Vector2d();
Vector2d(int x, int y);
Vector2d() : x_(0), y_(0) {}
Vector2d(int x, int y) : x_(x), y_(y) {}
int x() const { return x_; }
void set_x(int x) { x_ = x; }
......
......@@ -10,16 +10,6 @@
namespace gfx {
Vector2dF::Vector2dF()
: x_(0),
y_(0) {
}
Vector2dF::Vector2dF(float x, float y)
: x_(x),
y_(y) {
}
std::string Vector2dF::ToString() const {
return base::StringPrintf("[%f %f]", x_, y_);
}
......
......@@ -18,8 +18,8 @@ namespace gfx {
class UI_EXPORT Vector2dF {
public:
Vector2dF();
Vector2dF(float x, float y);
Vector2dF() : x_(0), y_(0) {}
Vector2dF(float x, float y) : x_(x), y_(y) {}
float x() const { return x_; }
void set_x(float x) { x_ = x; }
......
......@@ -468,7 +468,6 @@
'gfx/render_text_mac.h',
'gfx/render_text_win.cc',
'gfx/render_text_win.h',
'gfx/safe_integer_conversions.cc',
'gfx/safe_integer_conversions.h',
'gfx/scoped_cg_context_save_gstate_mac.h',
'gfx/scoped_ns_graphics_context_save_gstate_mac.h',
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment