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

Side by Side Diff: third_party/WebKit/Source/platform/LayoutUnit.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 unified diff | Download patch
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 141 matching lines...) Expand 10 before | Expand all | Expand 10 after
152 } 152 }
153 int ceil() const { 153 int ceil() const {
154 if (UNLIKELY(m_value >= INT_MAX - kFixedPointDenominator + 1)) 154 if (UNLIKELY(m_value >= INT_MAX - kFixedPointDenominator + 1))
155 return intMaxForLayoutUnit; 155 return intMaxForLayoutUnit;
156 156
157 if (m_value >= 0) 157 if (m_value >= 0)
158 return (m_value + kFixedPointDenominator - 1) / kFixedPointDenominator; 158 return (m_value + kFixedPointDenominator - 1) / kFixedPointDenominator;
159 return toInt(); 159 return toInt();
160 } 160 }
161 ALWAYS_INLINE int round() const { 161 ALWAYS_INLINE int round() const {
162 return saturatedAddition(rawValue(), kFixedPointDenominator / 2) >> 162 return SaturatedAddition(rawValue(), kFixedPointDenominator / 2) >>
163 kLayoutUnitFractionalBits; 163 kLayoutUnitFractionalBits;
164 } 164 }
165 165
166 int floor() const { 166 int floor() const {
167 if (UNLIKELY(m_value <= INT_MIN + kFixedPointDenominator - 1)) 167 if (UNLIKELY(m_value <= INT_MIN + kFixedPointDenominator - 1))
168 return intMinForLayoutUnit; 168 return intMinForLayoutUnit;
169 169
170 return m_value >> kLayoutUnitFractionalBits; 170 return m_value >> kLayoutUnitFractionalBits;
171 } 171 }
172 172
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
232 static bool isInBounds(unsigned value) { 232 static bool isInBounds(unsigned value) {
233 return value <= static_cast<unsigned>(std::numeric_limits<int>::max()) / 233 return value <= static_cast<unsigned>(std::numeric_limits<int>::max()) /
234 kFixedPointDenominator; 234 kFixedPointDenominator;
235 } 235 }
236 static bool isInBounds(double value) { 236 static bool isInBounds(double value) {
237 return ::fabs(value) <= 237 return ::fabs(value) <=
238 std::numeric_limits<int>::max() / kFixedPointDenominator; 238 std::numeric_limits<int>::max() / kFixedPointDenominator;
239 } 239 }
240 240
241 ALWAYS_INLINE void setValue(int value) { 241 ALWAYS_INLINE void setValue(int value) {
242 m_value = saturatedSet<kLayoutUnitFractionalBits>(value); 242 m_value = SaturatedSet<kLayoutUnitFractionalBits>(value);
243 } 243 }
244 244
245 inline void setValue(unsigned value) { 245 inline void setValue(unsigned value) {
246 m_value = saturatedSet<kLayoutUnitFractionalBits>(value); 246 m_value = SaturatedSet<kLayoutUnitFractionalBits>(value);
247 } 247 }
248 248
249 int m_value; 249 int m_value;
250 }; 250 };
251 251
252 inline bool operator<=(const LayoutUnit& a, const LayoutUnit& b) { 252 inline bool operator<=(const LayoutUnit& a, const LayoutUnit& b) {
253 return a.rawValue() <= b.rawValue(); 253 return a.rawValue() <= b.rawValue();
254 } 254 }
255 255
256 inline bool operator<=(const LayoutUnit& a, float b) { 256 inline bool operator<=(const LayoutUnit& a, float b) {
(...skipping 259 matching lines...) Expand 10 before | Expand all | Expand 10 after
516 inline LayoutUnit operator/(unsigned long a, const LayoutUnit& b) { 516 inline LayoutUnit operator/(unsigned long a, const LayoutUnit& b) {
517 return LayoutUnit(a) / b; 517 return LayoutUnit(a) / b;
518 } 518 }
519 519
520 inline LayoutUnit operator/(unsigned long long a, const LayoutUnit& b) { 520 inline LayoutUnit operator/(unsigned long long a, const LayoutUnit& b) {
521 return LayoutUnit(a) / b; 521 return LayoutUnit(a) / b;
522 } 522 }
523 523
524 ALWAYS_INLINE LayoutUnit operator+(const LayoutUnit& a, const LayoutUnit& b) { 524 ALWAYS_INLINE LayoutUnit operator+(const LayoutUnit& a, const LayoutUnit& b) {
525 LayoutUnit returnVal; 525 LayoutUnit returnVal;
526 returnVal.setRawValue(saturatedAddition(a.rawValue(), b.rawValue())); 526 returnVal.setRawValue(SaturatedAddition(a.rawValue(), b.rawValue()));
527 return returnVal; 527 return returnVal;
528 } 528 }
529 529
530 inline LayoutUnit operator+(const LayoutUnit& a, int b) { 530 inline LayoutUnit operator+(const LayoutUnit& a, int b) {
531 return a + LayoutUnit(b); 531 return a + LayoutUnit(b);
532 } 532 }
533 533
534 inline float operator+(const LayoutUnit& a, float b) { 534 inline float operator+(const LayoutUnit& a, float b) {
535 return a.toFloat() + b; 535 return a.toFloat() + b;
536 } 536 }
537 537
538 inline double operator+(const LayoutUnit& a, double b) { 538 inline double operator+(const LayoutUnit& a, double b) {
539 return a.toDouble() + b; 539 return a.toDouble() + b;
540 } 540 }
541 541
542 inline LayoutUnit operator+(const int a, const LayoutUnit& b) { 542 inline LayoutUnit operator+(const int a, const LayoutUnit& b) {
543 return LayoutUnit(a) + b; 543 return LayoutUnit(a) + b;
544 } 544 }
545 545
546 inline float operator+(const float a, const LayoutUnit& b) { 546 inline float operator+(const float a, const LayoutUnit& b) {
547 return a + b.toFloat(); 547 return a + b.toFloat();
548 } 548 }
549 549
550 inline double operator+(const double a, const LayoutUnit& b) { 550 inline double operator+(const double a, const LayoutUnit& b) {
551 return a + b.toDouble(); 551 return a + b.toDouble();
552 } 552 }
553 553
554 ALWAYS_INLINE LayoutUnit operator-(const LayoutUnit& a, const LayoutUnit& b) { 554 ALWAYS_INLINE LayoutUnit operator-(const LayoutUnit& a, const LayoutUnit& b) {
555 LayoutUnit returnVal; 555 LayoutUnit returnVal;
556 returnVal.setRawValue(saturatedSubtraction(a.rawValue(), b.rawValue())); 556 returnVal.setRawValue(SaturatedSubtraction(a.rawValue(), b.rawValue()));
557 return returnVal; 557 return returnVal;
558 } 558 }
559 559
560 inline LayoutUnit operator-(const LayoutUnit& a, int b) { 560 inline LayoutUnit operator-(const LayoutUnit& a, int b) {
561 return a - LayoutUnit(b); 561 return a - LayoutUnit(b);
562 } 562 }
563 563
564 inline LayoutUnit operator-(const LayoutUnit& a, unsigned b) { 564 inline LayoutUnit operator-(const LayoutUnit& a, unsigned b) {
565 return a - LayoutUnit(b); 565 return a - LayoutUnit(b);
566 } 566 }
567 567
568 inline float operator-(const LayoutUnit& a, float b) { 568 inline float operator-(const LayoutUnit& a, float b) {
569 return a.toFloat() - b; 569 return a.toFloat() - b;
570 } 570 }
571 571
572 inline double operator-(const LayoutUnit& a, double b) { 572 inline double operator-(const LayoutUnit& a, double b) {
573 return a.toDouble() - b; 573 return a.toDouble() - b;
574 } 574 }
575 575
576 inline LayoutUnit operator-(const int a, const LayoutUnit& b) { 576 inline LayoutUnit operator-(const int a, const LayoutUnit& b) {
577 return LayoutUnit(a) - b; 577 return LayoutUnit(a) - b;
578 } 578 }
579 579
580 inline float operator-(const float a, const LayoutUnit& b) { 580 inline float operator-(const float a, const LayoutUnit& b) {
581 return a - b.toFloat(); 581 return a - b.toFloat();
582 } 582 }
583 583
584 inline LayoutUnit operator-(const LayoutUnit& a) { 584 inline LayoutUnit operator-(const LayoutUnit& a) {
585 LayoutUnit returnVal; 585 LayoutUnit returnVal;
586 returnVal.setRawValue(saturatedNegative(a.rawValue())); 586 returnVal.setRawValue(SaturatedNegative(a.rawValue()));
587 return returnVal; 587 return returnVal;
588 } 588 }
589 589
590 // For returning the remainder after a division with integer results. 590 // For returning the remainder after a division with integer results.
591 inline LayoutUnit intMod(const LayoutUnit& a, const LayoutUnit& b) { 591 inline LayoutUnit intMod(const LayoutUnit& a, const LayoutUnit& b) {
592 // This calculates the modulo so that: a = static_cast<int>(a / b) * b + 592 // This calculates the modulo so that: a = static_cast<int>(a / b) * b +
593 // intMod(a, b). 593 // intMod(a, b).
594 LayoutUnit returnVal; 594 LayoutUnit returnVal;
595 returnVal.setRawValue(a.rawValue() % b.rawValue()); 595 returnVal.setRawValue(a.rawValue() % b.rawValue());
596 return returnVal; 596 return returnVal;
(...skipping 11 matching lines...) Expand all
608 608
609 inline LayoutUnit operator%(const LayoutUnit& a, int b) { 609 inline LayoutUnit operator%(const LayoutUnit& a, int b) {
610 return a % LayoutUnit(b); 610 return a % LayoutUnit(b);
611 } 611 }
612 612
613 inline LayoutUnit operator%(int a, const LayoutUnit& b) { 613 inline LayoutUnit operator%(int a, const LayoutUnit& b) {
614 return LayoutUnit(a) % b; 614 return LayoutUnit(a) % b;
615 } 615 }
616 616
617 inline LayoutUnit& operator+=(LayoutUnit& a, const LayoutUnit& b) { 617 inline LayoutUnit& operator+=(LayoutUnit& a, const LayoutUnit& b) {
618 a.setRawValue(saturatedAddition(a.rawValue(), b.rawValue())); 618 a.setRawValue(SaturatedAddition(a.rawValue(), b.rawValue()));
619 return a; 619 return a;
620 } 620 }
621 621
622 inline LayoutUnit& operator+=(LayoutUnit& a, int b) { 622 inline LayoutUnit& operator+=(LayoutUnit& a, int b) {
623 a = a + LayoutUnit(b); 623 a = a + LayoutUnit(b);
624 return a; 624 return a;
625 } 625 }
626 626
627 inline LayoutUnit& operator+=(LayoutUnit& a, float b) { 627 inline LayoutUnit& operator+=(LayoutUnit& a, float b) {
628 a = LayoutUnit(a + b); 628 a = LayoutUnit(a + b);
629 return a; 629 return a;
630 } 630 }
631 631
632 inline float& operator+=(float& a, const LayoutUnit& b) { 632 inline float& operator+=(float& a, const LayoutUnit& b) {
633 a = a + b; 633 a = a + b;
634 return a; 634 return a;
635 } 635 }
636 636
637 inline LayoutUnit& operator-=(LayoutUnit& a, int b) { 637 inline LayoutUnit& operator-=(LayoutUnit& a, int b) {
638 a = a - LayoutUnit(b); 638 a = a - LayoutUnit(b);
639 return a; 639 return a;
640 } 640 }
641 641
642 inline LayoutUnit& operator-=(LayoutUnit& a, const LayoutUnit& b) { 642 inline LayoutUnit& operator-=(LayoutUnit& a, const LayoutUnit& b) {
643 a.setRawValue(saturatedSubtraction(a.rawValue(), b.rawValue())); 643 a.setRawValue(SaturatedSubtraction(a.rawValue(), b.rawValue()));
644 return a; 644 return a;
645 } 645 }
646 646
647 inline LayoutUnit& operator-=(LayoutUnit& a, float b) { 647 inline LayoutUnit& operator-=(LayoutUnit& a, float b) {
648 a = LayoutUnit(a - b); 648 a = LayoutUnit(a - b);
649 return a; 649 return a;
650 } 650 }
651 651
652 inline float& operator-=(float& a, const LayoutUnit& b) { 652 inline float& operator-=(float& a, const LayoutUnit& b) {
653 a = a - b; 653 a = a - b;
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
724 return value; 724 return value;
725 } 725 }
726 726
727 inline std::ostream& operator<<(std::ostream& stream, const LayoutUnit& value) { 727 inline std::ostream& operator<<(std::ostream& stream, const LayoutUnit& value) {
728 return stream << value.toString(); 728 return stream << value.toString();
729 } 729 }
730 730
731 } // namespace blink 731 } // namespace blink
732 732
733 #endif // LayoutUnit_h 733 #endif // LayoutUnit_h
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/core/style/ComputedStyle.cpp ('k') | third_party/WebKit/Source/platform/geometry/IntPoint.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698