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

Side by Side Diff: third_party/WebKit/Source/platform/LayoutUnit.h

Issue 2615713007: Change LayoutUnit to use saturated_cast (Closed)
Patch Set: Fix includes Created 3 years, 11 months 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 unified diff | Download patch
« no previous file with comments | « third_party/WebKit/Source/platform/DEPS ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (c) 2012, Google Inc. All rights reserved. 2 * Copyright (c) 2012, Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are 5 * modification, are permitted provided that the following conditions are
6 * met: 6 * met:
7 * 7 *
8 * * Redistributions of source code must retain the above copyright 8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above 10 * * Redistributions in binary form must reproduce the above
(...skipping 13 matching lines...) Expand all
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */ 29 */
30 30
31 #ifndef LayoutUnit_h 31 #ifndef LayoutUnit_h
32 #define LayoutUnit_h 32 #define LayoutUnit_h
33 33
34 #include "base/numerics/safe_conversions.h"
34 #include "platform/PlatformExport.h" 35 #include "platform/PlatformExport.h"
35 #include "wtf/Allocator.h" 36 #include "wtf/Allocator.h"
36 #include "wtf/Assertions.h" 37 #include "wtf/Assertions.h"
37 #include "wtf/MathExtras.h"
38 #include "wtf/SaturatedArithmetic.h" 38 #include "wtf/SaturatedArithmetic.h"
39 #include "wtf/text/WTFString.h" 39 #include "wtf/text/WTFString.h"
40 #include <algorithm> 40 #include <algorithm>
41 #include <limits.h> 41 #include <limits.h>
42 #include <limits> 42 #include <limits>
43 #include <math.h>
43 #include <stdlib.h> 44 #include <stdlib.h>
44 45
45 namespace blink { 46 namespace blink {
46 47
47 #if DCHECK_IS_ON() 48 #if DCHECK_IS_ON()
48 #define REPORT_OVERFLOW(doesOverflow) \ 49 #define REPORT_OVERFLOW(doesOverflow) \
49 DLOG_IF(ERROR, !(doesOverflow)) << "LayoutUnit overflow !(" << #doesOverflow \ 50 DLOG_IF(ERROR, !(doesOverflow)) << "LayoutUnit overflow !(" << #doesOverflow \
50 << ") in " << WTF_PRETTY_FUNCTION 51 << ") in " << WTF_PRETTY_FUNCTION
51 #else 52 #else
52 #define REPORT_OVERFLOW(doesOverflow) ((void)0) 53 #define REPORT_OVERFLOW(doesOverflow) ((void)0)
(...skipping 11 matching lines...) Expand all
64 65
65 class LayoutUnit { 66 class LayoutUnit {
66 DISALLOW_NEW_EXCEPT_PLACEMENT_NEW(); 67 DISALLOW_NEW_EXCEPT_PLACEMENT_NEW();
67 68
68 public: 69 public:
69 LayoutUnit() : m_value(0) {} 70 LayoutUnit() : m_value(0) {}
70 explicit LayoutUnit(int value) { setValue(value); } 71 explicit LayoutUnit(int value) { setValue(value); }
71 explicit LayoutUnit(unsigned short value) { setValue(value); } 72 explicit LayoutUnit(unsigned short value) { setValue(value); }
72 explicit LayoutUnit(unsigned value) { setValue(value); } 73 explicit LayoutUnit(unsigned value) { setValue(value); }
73 explicit LayoutUnit(unsigned long value) { 74 explicit LayoutUnit(unsigned long value) {
74 m_value = clampTo<int>(value * kFixedPointDenominator); 75 m_value = base::saturated_cast<int>(value * kFixedPointDenominator);
75 } 76 }
76 explicit LayoutUnit(unsigned long long value) { 77 explicit LayoutUnit(unsigned long long value) {
77 m_value = clampTo<int>(value * kFixedPointDenominator); 78 m_value = base::saturated_cast<int>(value * kFixedPointDenominator);
78 } 79 }
79 explicit LayoutUnit(float value) { 80 explicit LayoutUnit(float value) {
80 m_value = clampTo<int>(value * kFixedPointDenominator); 81 m_value = base::saturated_cast<int>(value * kFixedPointDenominator);
81 } 82 }
82 explicit LayoutUnit(double value) { 83 explicit LayoutUnit(double value) {
83 m_value = clampTo<int>(value * kFixedPointDenominator); 84 m_value = base::saturated_cast<int>(value * kFixedPointDenominator);
84 } 85 }
85 86
86 static LayoutUnit fromFloatCeil(float value) { 87 static LayoutUnit fromFloatCeil(float value) {
87 LayoutUnit v; 88 LayoutUnit v;
88 v.m_value = clampTo<int>(ceilf(value * kFixedPointDenominator)); 89 v.m_value =
90 base::saturated_cast<int>(ceilf(value * kFixedPointDenominator));
89 return v; 91 return v;
90 } 92 }
91 93
92 static LayoutUnit fromFloatFloor(float value) { 94 static LayoutUnit fromFloatFloor(float value) {
93 LayoutUnit v; 95 LayoutUnit v;
94 v.m_value = clampTo<int>(floorf(value * kFixedPointDenominator)); 96 v.m_value =
97 base::saturated_cast<int>(floorf(value * kFixedPointDenominator));
95 return v; 98 return v;
96 } 99 }
97 100
98 static LayoutUnit fromFloatRound(float value) { 101 static LayoutUnit fromFloatRound(float value) {
99 if (value >= 0) 102 LayoutUnit v;
100 return clamp(value + epsilon() / 2.0f); 103 v.m_value =
101 return clamp(value - epsilon() / 2.0f); 104 base::saturated_cast<int>(roundf(value * kFixedPointDenominator));
105 return v;
102 } 106 }
103 107
104 int toInt() const { return m_value / kFixedPointDenominator; } 108 int toInt() const { return m_value / kFixedPointDenominator; }
105 float toFloat() const { 109 float toFloat() const {
106 return static_cast<float>(m_value) / kFixedPointDenominator; 110 return static_cast<float>(m_value) / kFixedPointDenominator;
107 } 111 }
108 double toDouble() const { 112 double toDouble() const {
109 return static_cast<double>(m_value) / kFixedPointDenominator; 113 return static_cast<double>(m_value) / kFixedPointDenominator;
110 } 114 }
111 unsigned toUnsigned() const { 115 unsigned toUnsigned() const {
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
203 LayoutUnit m; 207 LayoutUnit m;
204 m.m_value = std::numeric_limits<int>::max() - kFixedPointDenominator / 2; 208 m.m_value = std::numeric_limits<int>::max() - kFixedPointDenominator / 2;
205 return m; 209 return m;
206 } 210 }
207 static const LayoutUnit nearlyMin() { 211 static const LayoutUnit nearlyMin() {
208 LayoutUnit m; 212 LayoutUnit m;
209 m.m_value = std::numeric_limits<int>::min() + kFixedPointDenominator / 2; 213 m.m_value = std::numeric_limits<int>::min() + kFixedPointDenominator / 2;
210 return m; 214 return m;
211 } 215 }
212 216
213 static LayoutUnit clamp(double value) { 217 static LayoutUnit clamp(double value) { return fromFloatFloor(value); }
214 return clampTo<LayoutUnit>(value, LayoutUnit::min(), LayoutUnit::max());
215 }
216 218
217 String toString() const; 219 String toString() const;
218 220
219 private: 221 private:
220 static bool isInBounds(int value) { 222 static bool isInBounds(int value) {
221 return ::abs(value) <= 223 return ::abs(value) <=
222 std::numeric_limits<int>::max() / kFixedPointDenominator; 224 std::numeric_limits<int>::max() / kFixedPointDenominator;
223 } 225 }
224 static bool isInBounds(unsigned value) { 226 static bool isInBounds(unsigned value) {
225 return value <= static_cast<unsigned>(std::numeric_limits<int>::max()) / 227 return value <= static_cast<unsigned>(std::numeric_limits<int>::max()) /
(...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after
446 } 448 }
447 449
448 inline double operator*(const double a, const LayoutUnit& b) { 450 inline double operator*(const double a, const LayoutUnit& b) {
449 return a * b.toDouble(); 451 return a * b.toDouble();
450 } 452 }
451 453
452 inline LayoutUnit operator/(const LayoutUnit& a, const LayoutUnit& b) { 454 inline LayoutUnit operator/(const LayoutUnit& a, const LayoutUnit& b) {
453 LayoutUnit returnVal; 455 LayoutUnit returnVal;
454 long long rawVal = static_cast<long long>(kFixedPointDenominator) * 456 long long rawVal = static_cast<long long>(kFixedPointDenominator) *
455 a.rawValue() / b.rawValue(); 457 a.rawValue() / b.rawValue();
456 returnVal.setRawValue(clampTo<int>(rawVal)); 458 returnVal.setRawValue(base::saturated_cast<int>(rawVal));
457 return returnVal; 459 return returnVal;
458 } 460 }
459 461
460 inline float operator/(const LayoutUnit& a, float b) { 462 inline float operator/(const LayoutUnit& a, float b) {
461 return a.toFloat() / b; 463 return a.toFloat() / b;
462 } 464 }
463 465
464 inline double operator/(const LayoutUnit& a, double b) { 466 inline double operator/(const LayoutUnit& a, double b) {
465 return a.toDouble() / b; 467 return a.toDouble() / b;
466 } 468 }
(...skipping 232 matching lines...) Expand 10 before | Expand all | Expand 10 after
699 } 701 }
700 702
701 inline LayoutUnit layoutMod(const LayoutUnit& numerator, int denominator) { 703 inline LayoutUnit layoutMod(const LayoutUnit& numerator, int denominator) {
702 return numerator % LayoutUnit(denominator); 704 return numerator % LayoutUnit(denominator);
703 } 705 }
704 706
705 inline bool isIntegerValue(const LayoutUnit value) { 707 inline bool isIntegerValue(const LayoutUnit value) {
706 return value.toInt() == value; 708 return value.toInt() == value;
707 } 709 }
708 710
709 inline LayoutUnit clampToLayoutUnit(LayoutUnit value,
710 LayoutUnit min,
711 LayoutUnit max) {
712 if (value >= max)
713 return max;
714 if (value <= min)
715 return min;
716 return value;
717 }
718
719 inline std::ostream& operator<<(std::ostream& stream, const LayoutUnit& value) { 711 inline std::ostream& operator<<(std::ostream& stream, const LayoutUnit& value) {
720 return stream << value.toString(); 712 return stream << value.toString();
721 } 713 }
722 714
723 } // namespace blink 715 } // namespace blink
724 716
725 #endif // LayoutUnit_h 717 #endif // LayoutUnit_h
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/platform/DEPS ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698