Chromium Code Reviews| 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 <limits> | 35 #include <limits> |
| 35 #include <stdint.h> | 36 #include <stdint.h> |
| 36 | 37 |
| 38 #if CPU(ARM_TRADITIONAL) && COMPILER(GCC) | |
| 39 | |
| 40 // If we're building ARM on GCC we replace the C++ versions with some | |
| 41 // native arm assembly for speed. | |
| 42 #include "wtf/asm/SaturatedArithmeticAsm.h" | |
|
eseidel
2014/06/20 16:30:20
I probably would have called it wtf/SaturatedArith
picksi
2014/06/23 15:30:43
Done.
| |
| 43 | |
| 44 #else | |
| 45 | |
| 37 ALWAYS_INLINE int32_t saturatedAddition(int32_t a, int32_t b) | 46 ALWAYS_INLINE int32_t saturatedAddition(int32_t a, int32_t b) |
| 38 { | 47 { |
| 39 uint32_t ua = a; | 48 uint32_t ua = a; |
| 40 uint32_t ub = b; | 49 uint32_t ub = b; |
| 41 uint32_t result = ua + ub; | 50 uint32_t result = ua + ub; |
| 42 | 51 |
| 43 // Can only overflow if the signed bit of the two values match. If the signe d | 52 // Can only overflow if the signed bit of the two values match. If the |
| 44 // bit of the result and one of the values differ it did overflow. | 53 // signed bit of the result and one of the values differ it overflowed. |
| 45 if (!((ua ^ ub) >> 31) & (result ^ ua) >> 31) | 54 |
| 55 if (~(ua ^ ub) & (result ^ ua) & (1 << 31)) { | |
| 46 result = std::numeric_limits<int>::max() + (ua >> 31); | 56 result = std::numeric_limits<int>::max() + (ua >> 31); |
|
Stephen Chennney
2014/06/20 14:46:56
No braces and immediately "return std::num..."
picksi
2014/06/23 15:30:43
Done.
| |
| 57 } | |
| 47 | 58 |
| 48 return result; | 59 return result; |
| 49 } | 60 } |
| 50 | 61 |
| 51 ALWAYS_INLINE int32_t saturatedSubtraction(int32_t a, int32_t b) | 62 ALWAYS_INLINE int32_t saturatedSubtraction(int32_t a, int32_t b) |
| 52 { | 63 { |
| 53 uint32_t ua = a; | 64 uint32_t ua = a; |
| 54 uint32_t ub = b; | 65 uint32_t ub = b; |
| 55 uint32_t result = ua - ub; | 66 uint32_t result = ua - ub; |
| 56 | 67 |
| 57 // Can only overflow if the signed bit of the two values do not match. If th e | 68 // Can only overflow if the signed bit of the two input values differ. If |
| 58 // signed bit of the result and the first value differ it did overflow. | 69 // the signed bit of the result and the first value differ it overflowed. |
| 59 if ((ua ^ ub) >> 31 & (result ^ ua) >> 31) | 70 |
| 71 if ((ua ^ ub) & (result ^ ua) & (1 << 31)) { | |
| 60 result = std::numeric_limits<int>::max() + (ua >> 31); | 72 result = std::numeric_limits<int>::max() + (ua >> 31); |
|
Stephen Chennney
2014/06/20 14:46:56
Ditto.
picksi
2014/06/23 15:30:43
Done.
| |
| 73 } | |
| 61 | 74 |
| 62 return result; | 75 return result; |
| 63 } | 76 } |
| 64 | 77 |
| 78 inline int getMaxSaturatedSetResultForTesting(int FractionalShift) | |
| 79 { | |
| 80 // For C version the set function maxes out to max int, this differs from | |
| 81 // the ARM asm version, see SaturatedArithmetiAsm.h for the equivlent asm | |
| 82 // version. | |
| 83 return std::numeric_limits<int>::max(); | |
| 84 } | |
| 85 | |
| 86 inline int getMinSaturatedSetResultForTesting(int FractionalShift) | |
| 87 { | |
| 88 return std::numeric_limits<int>::min(); | |
| 89 } | |
| 90 | |
| 91 ALWAYS_INLINE int saturatedSet(int value, int FractionalShift) | |
| 92 { | |
| 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 } | |
|
Stephen Chennney
2014/06/20 14:46:56
No braces on single line statements in Blink.
picksi
2014/06/23 15:30:43
Done.
| |
| 102 | |
| 103 if (value <= intMinForLayoutUnit) { | |
| 104 return std::numeric_limits<int>::min(); | |
| 105 } | |
|
Stephen Chennney
2014/06/20 14:46:56
Ditto.
picksi
2014/06/23 15:30:43
Done.
| |
| 106 | |
| 107 return value << FractionalShift; | |
| 108 } | |
| 109 | |
| 110 | |
| 111 ALWAYS_INLINE int saturatedSet(unsigned value, int FractionalShift) | |
| 112 { | |
| 113 const unsigned intMaxForLayoutUnit = | |
| 114 std::numeric_limits<int>::max() >> FractionalShift; | |
| 115 | |
| 116 if (value >= intMaxForLayoutUnit) { | |
| 117 return std::numeric_limits<int>::max(); | |
| 118 } | |
|
Stephen Chennney
2014/06/20 14:46:56
Ditto,
picksi
2014/06/23 15:30:43
Done.
| |
| 119 | |
| 120 return value << FractionalShift; | |
| 121 } | |
| 122 | |
| 123 #endif // CPU(ARM_TRADITIONAL) && COMPILER(GCC) | |
| 65 #endif // SaturatedArithmetic_h | 124 #endif // SaturatedArithmetic_h |
| OLD | NEW |