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