OLD | NEW |
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 Loading... |
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 SaturatedArithmetic_h | 31 #ifndef SaturatedArithmetic_h |
32 #define SaturatedArithmetic_h | 32 #define SaturatedArithmetic_h |
33 | 33 |
34 #include "wtf/CPU.h" | 34 #include "base/numerics/saturated_arithmetic.h" |
35 #include <limits> | |
36 #include <stdint.h> | |
37 | 35 |
38 #if CPU(ARM) && COMPILER(GCC) && __OPTIMIZE__ | 36 namespace WTF { |
| 37 using base::SaturatedAddition; |
| 38 using base::SaturatedSubtraction; |
| 39 using base::SaturatedNegative; |
| 40 using base::SaturatedSet; |
| 41 } // namespace WTF |
39 | 42 |
40 // If we're building ARM on GCC we replace the C++ versions with some | 43 using WTF::SaturatedAddition; |
41 // native ARM assembly for speed. | 44 using WTF::SaturatedSubtraction; |
42 #include "wtf/asm/SaturatedArithmeticARM.h" | 45 using WTF::SaturatedNegative; |
| 46 using WTF::SaturatedSet; |
43 | 47 |
44 #else | |
45 | |
46 ALWAYS_INLINE int32_t saturatedAddition(int32_t a, int32_t b) { | |
47 uint32_t ua = a; | |
48 uint32_t ub = b; | |
49 uint32_t result = ua + ub; | |
50 | |
51 // Can only overflow if the signed bit of the two values match. If the | |
52 // signed bit of the result and one of the values differ it overflowed. | |
53 // The branch compiles to a CMOVNS instruction on x86. | |
54 if (~(ua ^ ub) & (result ^ ua) & (1 << 31)) | |
55 return std::numeric_limits<int>::max() + (ua >> 31); | |
56 | |
57 return result; | |
58 } | |
59 | |
60 ALWAYS_INLINE int32_t saturatedSubtraction(int32_t a, int32_t b) { | |
61 uint32_t ua = a; | |
62 uint32_t ub = b; | |
63 uint32_t result = ua - ub; | |
64 | |
65 // Can only overflow if the signed bit of the two input values differ. If | |
66 // the signed bit of the result and the first value differ it overflowed. | |
67 // The branch compiles to a CMOVNS instruction on x86. | |
68 if ((ua ^ ub) & (result ^ ua) & (1 << 31)) | |
69 return std::numeric_limits<int>::max() + (ua >> 31); | |
70 | |
71 return result; | |
72 } | |
73 | |
74 ALWAYS_INLINE int32_t saturatedNegative(int32_t a) { | |
75 if (UNLIKELY(a == std::numeric_limits<int>::min())) | |
76 return std::numeric_limits<int>::max(); | |
77 return -a; | |
78 } | |
79 | |
80 inline int getMaxSaturatedSetResultForTesting(int FractionalShift) { | |
81 // For C version the set function maxes out to max int, this differs from | |
82 // the ARM asm version, see SaturatedArithmetiARM.h for the equivalent asm | |
83 // version. | |
84 return std::numeric_limits<int>::max(); | |
85 } | |
86 | |
87 inline int getMinSaturatedSetResultForTesting(int FractionalShift) { | |
88 return std::numeric_limits<int>::min(); | |
89 } | |
90 | |
91 template <int FractionalShift> | |
92 ALWAYS_INLINE int saturatedSet(int value) { | |
93 const int intMaxForLayoutUnit = | |
94 std::numeric_limits<int>::max() >> FractionalShift; | |
95 | |
96 const int intMinForLayoutUnit = | |
97 std::numeric_limits<int>::min() >> FractionalShift; | |
98 | |
99 if (value > intMaxForLayoutUnit) | |
100 return std::numeric_limits<int>::max(); | |
101 | |
102 if (value < intMinForLayoutUnit) | |
103 return std::numeric_limits<int>::min(); | |
104 | |
105 return value << FractionalShift; | |
106 } | |
107 | |
108 template <int FractionalShift> | |
109 ALWAYS_INLINE int saturatedSet(unsigned value) { | |
110 const unsigned intMaxForLayoutUnit = | |
111 std::numeric_limits<int>::max() >> FractionalShift; | |
112 | |
113 if (value >= intMaxForLayoutUnit) | |
114 return std::numeric_limits<int>::max(); | |
115 | |
116 return value << FractionalShift; | |
117 } | |
118 | |
119 #endif // CPU(ARM) && COMPILER(GCC) | |
120 #endif // SaturatedArithmetic_h | 48 #endif // SaturatedArithmetic_h |
OLD | NEW |