From b3e5d1e22483e925ba8abf072ebab7eeca9772a4 Mon Sep 17 00:00:00 2001 From: Justin Schuh Date: Sat, 15 Jul 2017 07:12:07 +0900 Subject: Replace Saturated*() calls with Clamp*() templates This replaces the calls but leaves the saturated_arithmetic headers where they are. Those will be moved in the next CL. Bug: 672489 Change-Id: If901e49240b2e8d1f85155b9b395d9bb584300cf Reviewed-on: https://chromium-review.googlesource.com/571317 Commit-Queue: Justin Schuh Reviewed-by: Emil A Eklund Cr-Commit-Position: refs/heads/master@{#486905} CrOS-Libchrome-Original-Commit: 191d11489cb9f83e1e1575a29abf3904e7efa87b --- ui/gfx/geometry/point.h | 18 +++++++++--------- ui/gfx/geometry/rect.cc | 19 +++++++++---------- ui/gfx/geometry/rect.h | 2 +- ui/gfx/geometry/size.cc | 6 +++--- ui/gfx/geometry/vector2d.cc | 10 +++++----- 5 files changed, 27 insertions(+), 28 deletions(-) (limited to 'ui') diff --git a/ui/gfx/geometry/point.h b/ui/gfx/geometry/point.h index bb248d5432..b1ba5065de 100644 --- a/ui/gfx/geometry/point.h +++ b/ui/gfx/geometry/point.h @@ -9,7 +9,7 @@ #include #include -#include "base/numerics/saturated_arithmetic.h" +#include "base/numerics/clamped_math.h" #include "build/build_config.h" #include "ui/gfx/geometry/vector2d.h" #include "ui/gfx/gfx_export.h" @@ -56,18 +56,18 @@ class GFX_EXPORT Point { } void Offset(int delta_x, int delta_y) { - x_ = base::SaturatedAddition(x_, delta_x); - y_ = base::SaturatedAddition(y_, delta_y); + x_ = base::ClampAdd(x_, delta_x); + y_ = base::ClampAdd(y_, delta_y); } void operator+=(const Vector2d& vector) { - x_ = base::SaturatedAddition(x_, vector.x()); - y_ = base::SaturatedAddition(y_, vector.y()); + x_ = base::ClampAdd(x_, vector.x()); + y_ = base::ClampAdd(y_, vector.y()); } void operator-=(const Vector2d& vector) { - x_ = base::SaturatedSubtraction(x_, vector.x()); - y_ = base::SaturatedSubtraction(y_, vector.y()); + x_ = base::ClampSub(x_, vector.x()); + y_ = base::ClampSub(y_, vector.y()); } void SetToMin(const Point& other); @@ -116,8 +116,8 @@ inline Point operator-(const Point& lhs, const Vector2d& rhs) { } inline Vector2d operator-(const Point& lhs, const Point& rhs) { - return Vector2d(base::SaturatedSubtraction(lhs.x(), rhs.x()), - base::SaturatedSubtraction(lhs.y(), rhs.y())); + return Vector2d(base::ClampSub(lhs.x(), rhs.x()), + base::ClampSub(lhs.y(), rhs.y())); } inline Point PointAtOffsetFromOrigin(const Vector2d& offset_from_origin) { diff --git a/ui/gfx/geometry/rect.cc b/ui/gfx/geometry/rect.cc index b5ceda5829..a1735d028c 100644 --- a/ui/gfx/geometry/rect.cc +++ b/ui/gfx/geometry/rect.cc @@ -15,7 +15,7 @@ #endif #include "base/logging.h" -#include "base/numerics/saturated_arithmetic.h" +#include "base/numerics/clamped_math.h" #include "base/strings/stringprintf.h" #include "build/build_config.h" #include "ui/gfx/geometry/insets.h" @@ -69,8 +69,8 @@ static void SaturatedClampRange(int min, int max, int* origin, int* span) { return; } - int effective_span = base::SaturatedSubtraction(max, min); - int span_loss = base::SaturatedSubtraction(max, min + effective_span); + int effective_span = base::ClampSub(max, min); + int span_loss = base::ClampSub(max, base::ClampAdd(min, effective_span)); // If the desired width is within the limits of ints, we can just // use the simple computations to represent the range precisely. @@ -83,12 +83,13 @@ static void SaturatedClampRange(int min, int max, int* origin, int* span) { // Now we have to approximate. If one of min or max is close enough // to zero we choose to represent that one precisely. The other side is // probably practically "infinite", so we move it. - if (base::SaturatedAbsolute(max) < std::numeric_limits::max() / 2) { + if (base::SafeUnsignedAbs(max) < + base::as_unsigned(std::numeric_limits::max() / 2)) { // Maintain origin + span == max. *span = effective_span; *origin = max - effective_span; - } else if (base::SaturatedAbsolute(min) < - std::numeric_limits::max() / 2) { + } else if (base::SafeUnsignedAbs(min) < + base::as_unsigned(std::numeric_limits::max() / 2)) { // Maintain origin == min. *span = effective_span; *origin = min; @@ -116,10 +117,8 @@ void Rect::Inset(int left, int top, int right, int bottom) { origin_ += Vector2d(left, top); // left+right might overflow/underflow, but width() - (left+right) might // overflow as well. - set_width(base::SaturatedSubtraction(width(), - base::SaturatedAddition(left, right))); - set_height(base::SaturatedSubtraction(height(), - base::SaturatedAddition(top, bottom))); + set_width(base::ClampSub(width(), base::ClampAdd(left, right))); + set_height(base::ClampSub(height(), base::ClampAdd(top, bottom))); } void Rect::Offset(int horizontal, int vertical) { diff --git a/ui/gfx/geometry/rect.h b/ui/gfx/geometry/rect.h index 1858d44d2c..4e914b8ca6 100644 --- a/ui/gfx/geometry/rect.h +++ b/ui/gfx/geometry/rect.h @@ -227,7 +227,7 @@ class GFX_EXPORT Rect { // Clamp the size to avoid integer overflow in bottom() and right(). // This returns the width given an origin and a width. - // TODO(enne): this should probably use base::SaturatedAddition, but that + // TODO(enne): this should probably use base::ClampAdd, but that // function is not a constexpr. static constexpr int GetClampedValue(int origin, int size) { return AddWouldOverflow(origin, size) diff --git a/ui/gfx/geometry/size.cc b/ui/gfx/geometry/size.cc index 69486723a1..781b84d7f5 100644 --- a/ui/gfx/geometry/size.cc +++ b/ui/gfx/geometry/size.cc @@ -12,8 +12,8 @@ #include #endif +#include "base/numerics/clamped_math.h" #include "base/numerics/safe_math.h" -#include "base/numerics/saturated_arithmetic.h" #include "base/strings/stringprintf.h" #include "build/build_config.h" #include "ui/gfx/geometry/safe_integer_conversions.h" @@ -58,8 +58,8 @@ base::CheckedNumeric Size::GetCheckedArea() const { } void Size::Enlarge(int grow_width, int grow_height) { - SetSize(base::SaturatedAddition(width(), grow_width), - base::SaturatedAddition(height(), grow_height)); + SetSize(base::ClampAdd(width(), grow_width), + base::ClampAdd(height(), grow_height)); } void Size::SetToMin(const Size& other) { diff --git a/ui/gfx/geometry/vector2d.cc b/ui/gfx/geometry/vector2d.cc index 2b4875c39c..0ce3b20baa 100644 --- a/ui/gfx/geometry/vector2d.cc +++ b/ui/gfx/geometry/vector2d.cc @@ -6,7 +6,7 @@ #include -#include "base/numerics/saturated_arithmetic.h" +#include "base/numerics/clamped_math.h" #include "base/strings/stringprintf.h" namespace gfx { @@ -16,13 +16,13 @@ bool Vector2d::IsZero() const { } void Vector2d::Add(const Vector2d& other) { - x_ = base::SaturatedAddition(other.x_, x_); - y_ = base::SaturatedAddition(other.y_, y_); + x_ = base::ClampAdd(other.x_, x_); + y_ = base::ClampAdd(other.y_, y_); } void Vector2d::Subtract(const Vector2d& other) { - x_ = base::SaturatedSubtraction(x_, other.x_); - y_ = base::SaturatedSubtraction(y_, other.y_); + x_ = base::ClampSub(x_, other.x_); + y_ = base::ClampSub(y_, other.y_); } int64_t Vector2d::LengthSquared() const { -- cgit v1.2.3