Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(558)

Unified Diff: ui/gfx/geometry/rect.h

Issue 2499783002: Move SaturatedArithmetic from Blink to base (Closed)
Patch Set: Revert flag addition Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « ui/gfx/geometry/point.h ('k') | ui/gfx/geometry/rect.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: ui/gfx/geometry/rect.h
diff --git a/ui/gfx/geometry/rect.h b/ui/gfx/geometry/rect.h
index d9b3165bf82418ce3937dc63b443fb1a5ff71f49..2f7b037e41576f58c3a26f3f67fd9c84342dc14d 100644
--- a/ui/gfx/geometry/rect.h
+++ b/ui/gfx/geometry/rect.h
@@ -206,19 +206,28 @@ class GFX_EXPORT Rect {
gfx::Point origin_;
gfx::Size size_;
+ // Returns true iff a+b would overflow max int.
+ static constexpr bool AddWouldOverflow(int a, int b) {
+ // In this function, GCC tries to make optimizations that would only work if
+ // max - a wouldn't overflow but it isn't smart enough to notice that a > 0.
+ // So cast everything to unsigned to avoid this. As it is guaranteed that
+ // max - a and b are both already positive, the cast is a noop.
+ //
+ // This is intended to be: a > 0 && max - a < b
+ return a > 0 && b > 0 &&
+ static_cast<unsigned>(std::numeric_limits<int>::max() - a) <
+ static_cast<unsigned>(b);
+ }
+
// 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
+ // function is not a constexpr.
static constexpr int GetClampedValue(int origin, int size) {
return AddWouldOverflow(origin, size)
? std::numeric_limits<int>::max() - origin
: size;
}
-
- // Returns a clamped width given a right and a left, assuming right > left.
- static constexpr int GetClampedWidthFromExtents(int left, int right) {
- return SubtractWouldOverflow(right, left) ? std::numeric_limits<int>::max()
- : right - left;
- }
};
inline bool operator==(const Rect& lhs, const Rect& rhs) {
« no previous file with comments | « ui/gfx/geometry/point.h ('k') | ui/gfx/geometry/rect.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698