2 * Copyright (c) 2012, Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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.
34 #include "wtf/Assertions.h"
35 #include "wtf/MathExtras.h"
36 #include "wtf/SaturatedArithmetic.h"
45 #define REPORT_OVERFLOW(doesOverflow) ((void)0)
49 #define REPORT_OVERFLOW(doesOverflow) do \
50 if (!(doesOverflow)) { \
51 WTFReportError(__FILE__, __LINE__, WTF_PRETTY_FUNCTION, "!(%s)", #doesOverflow); \
57 static const int kLayoutUnitFractionalBits = 6;
58 static const int kFixedPointDenominator = 1 << kLayoutUnitFractionalBits;
60 const int intMaxForLayoutUnit = INT_MAX / kFixedPointDenominator;
61 const int intMinForLayoutUnit = INT_MIN / kFixedPointDenominator;
65 LayoutUnit() : m_value(0) { }
66 LayoutUnit(int value) { setValue(value); }
67 LayoutUnit(unsigned short value) { setValue(value); }
68 LayoutUnit(unsigned value) { setValue(value); }
69 LayoutUnit(unsigned long value) { m_value = clampTo<int>(value * kFixedPointDenominator); }
70 LayoutUnit(unsigned long long value) { m_value = clampTo<int>(value * kFixedPointDenominator); }
71 LayoutUnit(float value) { m_value = clampTo<float>(value * kFixedPointDenominator, static_cast<float>(INT_MIN), static_cast<float>(INT_MAX)); }
72 LayoutUnit(double value) { m_value = clampTo<double>(value * kFixedPointDenominator, static_cast<double>(INT_MIN), static_cast<double>(INT_MAX)); }
74 static LayoutUnit fromFloatCeil(float value)
77 v.m_value = clampToInteger(ceilf(value * kFixedPointDenominator));
81 static LayoutUnit fromFloatFloor(float value)
84 v.m_value = clampToInteger(floorf(value * kFixedPointDenominator));
88 static LayoutUnit fromFloatRound(float value)
91 return clamp(value + epsilon() / 2.0f);
92 return clamp(value - epsilon() / 2.0f);
95 int toInt() const { return m_value / kFixedPointDenominator; }
96 float toFloat() const { return static_cast<float>(m_value) / kFixedPointDenominator; }
97 double toDouble() const { return static_cast<double>(m_value) / kFixedPointDenominator; }
98 float ceilToFloat() const
100 float floatValue = toFloat();
101 if (static_cast<int>(floatValue * kFixedPointDenominator) == m_value)
104 return nextafterf(floatValue, std::numeric_limits<float>::max());
105 return nextafterf(floatValue, std::numeric_limits<float>::min());
107 unsigned toUnsigned() const { REPORT_OVERFLOW(m_value >= 0); return toInt(); }
109 operator int() const { return toInt(); }
110 operator unsigned() const { return toUnsigned(); }
111 operator double() const { return toDouble(); }
112 operator bool() const { return m_value; }
114 LayoutUnit operator++(int)
116 m_value += kFixedPointDenominator;
120 inline int rawValue() const { return m_value; }
121 inline void setRawValue(int value) { m_value = value; }
122 void setRawValue(long long value)
124 REPORT_OVERFLOW(value > std::numeric_limits<int>::min() && value < std::numeric_limits<int>::max());
125 m_value = static_cast<int>(value);
128 LayoutUnit abs() const
130 LayoutUnit returnValue;
131 returnValue.setRawValue(::abs(m_value));
140 if (UNLIKELY(m_value >= INT_MAX - kFixedPointDenominator + 1))
141 return intMaxForLayoutUnit;
144 return (m_value + kFixedPointDenominator - 1) / kFixedPointDenominator;
149 return saturatedAddition(rawValue(), kFixedPointDenominator / 2) >> kLayoutUnitFractionalBits;
154 if (UNLIKELY(m_value <= INT_MIN + kFixedPointDenominator - 1))
155 return intMinForLayoutUnit;
157 return m_value >> kLayoutUnitFractionalBits;
160 LayoutUnit fraction() const
162 // Add the fraction to the size (as opposed to the full location) to avoid overflows.
163 // Compute fraction using the mod operator to preserve the sign of the value as it may affect rounding.
165 fraction.setRawValue(rawValue() % kFixedPointDenominator);
169 bool mightBeSaturated() const
171 return rawValue() == std::numeric_limits<int>::max()
172 || rawValue() == std::numeric_limits<int>::min();
175 static float epsilon() { return 1.0f / kFixedPointDenominator; }
177 static const LayoutUnit max()
180 m.m_value = std::numeric_limits<int>::max();
183 static const LayoutUnit min()
186 m.m_value = std::numeric_limits<int>::min();
190 // Versions of max/min that are slightly smaller/larger than max/min() to allow for roinding without overflowing.
191 static const LayoutUnit nearlyMax()
194 m.m_value = std::numeric_limits<int>::max() - kFixedPointDenominator / 2;
197 static const LayoutUnit nearlyMin()
200 m.m_value = std::numeric_limits<int>::min() + kFixedPointDenominator / 2;
204 static LayoutUnit clamp(double value)
206 return clampTo<LayoutUnit>(value, LayoutUnit::min(), LayoutUnit::max());
210 static bool isInBounds(int value)
212 return ::abs(value) <= std::numeric_limits<int>::max() / kFixedPointDenominator;
214 static bool isInBounds(unsigned value)
216 return value <= static_cast<unsigned>(std::numeric_limits<int>::max()) / kFixedPointDenominator;
218 static bool isInBounds(double value)
220 return ::fabs(value) <= std::numeric_limits<int>::max() / kFixedPointDenominator;
223 inline void setValue(int value)
225 if (value > intMaxForLayoutUnit)
226 m_value = std::numeric_limits<int>::max();
227 else if (value < intMinForLayoutUnit)
228 m_value = std::numeric_limits<int>::min();
230 m_value = value * kFixedPointDenominator;
232 inline void setValue(unsigned value)
234 if (value >= static_cast<unsigned>(intMaxForLayoutUnit))
235 m_value = std::numeric_limits<int>::max();
237 m_value = value * kFixedPointDenominator;
243 inline bool operator<=(const LayoutUnit& a, const LayoutUnit& b)
245 return a.rawValue() <= b.rawValue();
248 inline bool operator<=(const LayoutUnit& a, float b)
250 return a.toFloat() <= b;
253 inline bool operator<=(const LayoutUnit& a, int b)
255 return a <= LayoutUnit(b);
258 inline bool operator<=(const float a, const LayoutUnit& b)
260 return a <= b.toFloat();
263 inline bool operator<=(const int a, const LayoutUnit& b)
265 return LayoutUnit(a) <= b;
268 inline bool operator>=(const LayoutUnit& a, const LayoutUnit& b)
270 return a.rawValue() >= b.rawValue();
273 inline bool operator>=(const LayoutUnit& a, int b)
275 return a >= LayoutUnit(b);
278 inline bool operator>=(const float a, const LayoutUnit& b)
280 return a >= b.toFloat();
283 inline bool operator>=(const LayoutUnit& a, float b)
285 return a.toFloat() >= b;
288 inline bool operator>=(const int a, const LayoutUnit& b)
290 return LayoutUnit(a) >= b;
293 inline bool operator<(const LayoutUnit& a, const LayoutUnit& b)
295 return a.rawValue() < b.rawValue();
298 inline bool operator<(const LayoutUnit& a, int b)
300 return a < LayoutUnit(b);
303 inline bool operator<(const LayoutUnit& a, float b)
305 return a.toFloat() < b;
308 inline bool operator<(const LayoutUnit& a, double b)
310 return a.toDouble() < b;
313 inline bool operator<(const int a, const LayoutUnit& b)
315 return LayoutUnit(a) < b;
318 inline bool operator<(const float a, const LayoutUnit& b)
320 return a < b.toFloat();
323 inline bool operator>(const LayoutUnit& a, const LayoutUnit& b)
325 return a.rawValue() > b.rawValue();
328 inline bool operator>(const LayoutUnit& a, double b)
330 return a.toDouble() > b;
333 inline bool operator>(const LayoutUnit& a, float b)
335 return a.toFloat() > b;
338 inline bool operator>(const LayoutUnit& a, int b)
340 return a > LayoutUnit(b);
343 inline bool operator>(const int a, const LayoutUnit& b)
345 return LayoutUnit(a) > b;
348 inline bool operator>(const float a, const LayoutUnit& b)
350 return a > b.toFloat();
353 inline bool operator>(const double a, const LayoutUnit& b)
355 return a > b.toDouble();
358 inline bool operator!=(const LayoutUnit& a, const LayoutUnit& b)
360 return a.rawValue() != b.rawValue();
363 inline bool operator!=(const LayoutUnit& a, float b)
365 return a != LayoutUnit(b);
368 inline bool operator!=(const int a, const LayoutUnit& b)
370 return LayoutUnit(a) != b;
373 inline bool operator!=(const LayoutUnit& a, int b)
375 return a != LayoutUnit(b);
378 inline bool operator==(const LayoutUnit& a, const LayoutUnit& b)
380 return a.rawValue() == b.rawValue();
383 inline bool operator==(const LayoutUnit& a, int b)
385 return a == LayoutUnit(b);
388 inline bool operator==(const int a, const LayoutUnit& b)
390 return LayoutUnit(a) == b;
393 inline bool operator==(const LayoutUnit& a, float b)
395 return a.toFloat() == b;
398 inline bool operator==(const float a, const LayoutUnit& b)
400 return a == b.toFloat();
403 // For multiplication that's prone to overflow, this bounds it to LayoutUnit::max() and ::min()
404 inline LayoutUnit boundedMultiply(const LayoutUnit& a, const LayoutUnit& b)
406 int64_t result = static_cast<int64_t>(a.rawValue()) * static_cast<int64_t>(b.rawValue()) / kFixedPointDenominator;
407 int32_t high = static_cast<int32_t>(result >> 32);
408 int32_t low = static_cast<int32_t>(result);
409 uint32_t saturated = (static_cast<uint32_t>(a.rawValue() ^ b.rawValue()) >> 31) + std::numeric_limits<int>::max();
410 // If the higher 32 bits does not match the lower 32 with sign extension the operation overflowed.
411 if (high != low >> 31)
414 LayoutUnit returnVal;
415 returnVal.setRawValue(static_cast<int>(result));
419 inline LayoutUnit operator*(const LayoutUnit& a, const LayoutUnit& b)
421 return boundedMultiply(a, b);
424 inline double operator*(const LayoutUnit& a, double b)
426 return a.toDouble() * b;
429 inline float operator*(const LayoutUnit& a, float b)
431 return a.toFloat() * b;
434 inline LayoutUnit operator*(const LayoutUnit& a, int b)
436 return a * LayoutUnit(b);
439 inline LayoutUnit operator*(const LayoutUnit& a, unsigned short b)
441 return a * LayoutUnit(b);
444 inline LayoutUnit operator*(const LayoutUnit& a, unsigned b)
446 return a * LayoutUnit(b);
449 inline LayoutUnit operator*(const LayoutUnit& a, unsigned long b)
451 return a * LayoutUnit(b);
454 inline LayoutUnit operator*(const LayoutUnit& a, unsigned long long b)
456 return a * LayoutUnit(b);
459 inline LayoutUnit operator*(unsigned short a, const LayoutUnit& b)
461 return LayoutUnit(a) * b;
464 inline LayoutUnit operator*(unsigned a, const LayoutUnit& b)
466 return LayoutUnit(a) * b;
469 inline LayoutUnit operator*(unsigned long a, const LayoutUnit& b)
471 return LayoutUnit(a) * b;
474 inline LayoutUnit operator*(unsigned long long a, const LayoutUnit& b)
476 return LayoutUnit(a) * b;
479 inline LayoutUnit operator*(const int a, const LayoutUnit& b)
481 return LayoutUnit(a) * b;
484 inline float operator*(const float a, const LayoutUnit& b)
486 return a * b.toFloat();
489 inline double operator*(const double a, const LayoutUnit& b)
491 return a * b.toDouble();
494 inline LayoutUnit operator/(const LayoutUnit& a, const LayoutUnit& b)
496 LayoutUnit returnVal;
497 long long rawVal = static_cast<long long>(kFixedPointDenominator) * a.rawValue() / b.rawValue();
498 returnVal.setRawValue(clampTo<int>(rawVal));
502 inline float operator/(const LayoutUnit& a, float b)
504 return a.toFloat() / b;
507 inline double operator/(const LayoutUnit& a, double b)
509 return a.toDouble() / b;
512 inline LayoutUnit operator/(const LayoutUnit& a, int b)
514 return a / LayoutUnit(b);
517 inline LayoutUnit operator/(const LayoutUnit& a, unsigned short b)
519 return a / LayoutUnit(b);
522 inline LayoutUnit operator/(const LayoutUnit& a, unsigned b)
524 return a / LayoutUnit(b);
527 inline LayoutUnit operator/(const LayoutUnit& a, unsigned long b)
529 return a / LayoutUnit(b);
532 inline LayoutUnit operator/(const LayoutUnit& a, unsigned long long b)
534 return a / LayoutUnit(b);
537 inline float operator/(const float a, const LayoutUnit& b)
539 return a / b.toFloat();
542 inline double operator/(const double a, const LayoutUnit& b)
544 return a / b.toDouble();
547 inline LayoutUnit operator/(const int a, const LayoutUnit& b)
549 return LayoutUnit(a) / b;
552 inline LayoutUnit operator/(unsigned short a, const LayoutUnit& b)
554 return LayoutUnit(a) / b;
557 inline LayoutUnit operator/(unsigned a, const LayoutUnit& b)
559 return LayoutUnit(a) / b;
562 inline LayoutUnit operator/(unsigned long a, const LayoutUnit& b)
564 return LayoutUnit(a) / b;
567 inline LayoutUnit operator/(unsigned long long a, const LayoutUnit& b)
569 return LayoutUnit(a) / b;
572 inline LayoutUnit operator+(const LayoutUnit& a, const LayoutUnit& b)
574 LayoutUnit returnVal;
575 returnVal.setRawValue(saturatedAddition(a.rawValue(), b.rawValue()));
579 inline LayoutUnit operator+(const LayoutUnit& a, int b)
581 return a + LayoutUnit(b);
584 inline float operator+(const LayoutUnit& a, float b)
586 return a.toFloat() + b;
589 inline double operator+(const LayoutUnit& a, double b)
591 return a.toDouble() + b;
594 inline LayoutUnit operator+(const int a, const LayoutUnit& b)
596 return LayoutUnit(a) + b;
599 inline float operator+(const float a, const LayoutUnit& b)
601 return a + b.toFloat();
604 inline double operator+(const double a, const LayoutUnit& b)
606 return a + b.toDouble();
609 inline LayoutUnit operator-(const LayoutUnit& a, const LayoutUnit& b)
611 LayoutUnit returnVal;
612 returnVal.setRawValue(saturatedSubtraction(a.rawValue(), b.rawValue()));
616 inline LayoutUnit operator-(const LayoutUnit& a, int b)
618 return a - LayoutUnit(b);
621 inline LayoutUnit operator-(const LayoutUnit& a, unsigned b)
623 return a - LayoutUnit(b);
626 inline float operator-(const LayoutUnit& a, float b)
628 return a.toFloat() - b;
631 inline LayoutUnit operator-(const int a, const LayoutUnit& b)
633 return LayoutUnit(a) - b;
636 inline float operator-(const float a, const LayoutUnit& b)
638 return a - b.toFloat();
641 inline LayoutUnit operator-(const LayoutUnit& a)
643 LayoutUnit returnVal;
644 returnVal.setRawValue(-a.rawValue());
648 // For returning the remainder after a division with integer results.
649 inline LayoutUnit intMod(const LayoutUnit& a, const LayoutUnit& b)
651 // This calculates the modulo so that: a = static_cast<int>(a / b) * b + intMod(a, b).
652 LayoutUnit returnVal;
653 returnVal.setRawValue(a.rawValue() % b.rawValue());
657 inline LayoutUnit operator%(const LayoutUnit& a, const LayoutUnit& b)
659 // This calculates the modulo so that: a = (a / b) * b + a % b.
660 LayoutUnit returnVal;
661 long long rawVal = (static_cast<long long>(kFixedPointDenominator) * a.rawValue()) % b.rawValue();
662 returnVal.setRawValue(rawVal / kFixedPointDenominator);
666 inline LayoutUnit operator%(const LayoutUnit& a, int b)
668 return a % LayoutUnit(b);
671 inline LayoutUnit operator%(int a, const LayoutUnit& b)
673 return LayoutUnit(a) % b;
676 inline LayoutUnit& operator+=(LayoutUnit& a, const LayoutUnit& b)
678 a.setRawValue(saturatedAddition(a.rawValue(), b.rawValue()));
682 inline LayoutUnit& operator+=(LayoutUnit& a, int b)
688 inline LayoutUnit& operator+=(LayoutUnit& a, float b)
694 inline float& operator+=(float& a, const LayoutUnit& b)
700 inline LayoutUnit& operator-=(LayoutUnit& a, int b)
706 inline LayoutUnit& operator-=(LayoutUnit& a, const LayoutUnit& b)
708 a.setRawValue(saturatedSubtraction(a.rawValue(), b.rawValue()));
712 inline LayoutUnit& operator-=(LayoutUnit& a, float b)
718 inline float& operator-=(float& a, const LayoutUnit& b)
724 inline LayoutUnit& operator*=(LayoutUnit& a, const LayoutUnit& b)
729 // operator*=(LayoutUnit& a, int b) is supported by the operator above plus LayoutUnit(int).
731 inline LayoutUnit& operator*=(LayoutUnit& a, float b)
737 inline float& operator*=(float& a, const LayoutUnit& b)
743 inline LayoutUnit& operator/=(LayoutUnit& a, const LayoutUnit& b)
748 // operator/=(LayoutUnit& a, int b) is supported by the operator above plus LayoutUnit(int).
750 inline LayoutUnit& operator/=(LayoutUnit& a, float b)
756 inline float& operator/=(float& a, const LayoutUnit& b)
762 inline int snapSizeToPixel(LayoutUnit size, LayoutUnit location)
764 LayoutUnit fraction = location.fraction();
765 return (fraction + size).round() - fraction.round();
768 inline int roundToInt(LayoutUnit value)
770 return value.round();
773 inline int floorToInt(LayoutUnit value)
775 return value.floor();
778 inline LayoutUnit absoluteValue(const LayoutUnit& value)
783 inline LayoutUnit layoutMod(const LayoutUnit& numerator, const LayoutUnit& denominator)
785 return numerator % denominator;
788 inline bool isIntegerValue(const LayoutUnit value)
790 return value.toInt() == value;
793 inline LayoutUnit clampToLayoutUnit(LayoutUnit value, LayoutUnit min, LayoutUnit max)
802 } // namespace WebCore
804 #endif // LayoutUnit_h