| 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 ALWAYS_INLINE int saturatedSet(int value, int FractionalShift) | 85 ALWAYS_INLINE int saturatedSet(int value, int FractionalShift) { |
| 90 { | 86 const int intMaxForLayoutUnit = |
| 91 const int intMaxForLayoutUnit = | 87 std::numeric_limits<int>::max() >> FractionalShift; |
| 92 std::numeric_limits<int>::max() >> FractionalShift; | |
| 93 | 88 |
| 94 const int intMinForLayoutUnit = | 89 const int intMinForLayoutUnit = |
| 95 std::numeric_limits<int>::min() >> FractionalShift; | 90 std::numeric_limits<int>::min() >> FractionalShift; |
| 96 | 91 |
| 97 if (value > intMaxForLayoutUnit) | 92 if (value > intMaxForLayoutUnit) |
| 98 return std::numeric_limits<int>::max(); | 93 return std::numeric_limits<int>::max(); |
| 99 | 94 |
| 100 if (value < intMinForLayoutUnit) | 95 if (value < intMinForLayoutUnit) |
| 101 return std::numeric_limits<int>::min(); | 96 return std::numeric_limits<int>::min(); |
| 102 | 97 |
| 103 return value << FractionalShift; | 98 return value << FractionalShift; |
| 104 } | 99 } |
| 105 | 100 |
| 101 ALWAYS_INLINE int saturatedSet(unsigned value, int FractionalShift) { |
| 102 const unsigned intMaxForLayoutUnit = |
| 103 std::numeric_limits<int>::max() >> FractionalShift; |
| 106 | 104 |
| 107 ALWAYS_INLINE int saturatedSet(unsigned value, int FractionalShift) | 105 if (value >= intMaxForLayoutUnit) |
| 108 { | 106 return std::numeric_limits<int>::max(); |
| 109 const unsigned intMaxForLayoutUnit = | |
| 110 std::numeric_limits<int>::max() >> FractionalShift; | |
| 111 | 107 |
| 112 if (value >= intMaxForLayoutUnit) | 108 return value << FractionalShift; |
| 113 return std::numeric_limits<int>::max(); | |
| 114 | |
| 115 return value << FractionalShift; | |
| 116 } | 109 } |
| 117 | 110 |
| 118 #endif // CPU(ARM) && COMPILER(GCC) | 111 #endif // CPU(ARM) && COMPILER(GCC) |
| 119 #endif // SaturatedArithmetic_h | 112 #endif // SaturatedArithmetic_h |
| OLD | NEW |