| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef SaturatedArithmeticARM_h | 5 #ifndef SaturatedArithmeticARM_h |
| 6 #define SaturatedArithmeticARM_h | 6 #define SaturatedArithmeticARM_h |
| 7 | 7 |
| 8 #include "wtf/CPU.h" | 8 #include "wtf/CPU.h" |
| 9 #include <limits> | 9 #include <limits> |
| 10 #include <stdint.h> | 10 #include <stdint.h> |
| 11 | 11 |
| 12 ALWAYS_INLINE int32_t saturatedAddition(int32_t a, int32_t b) | 12 ALWAYS_INLINE int32_t saturatedAddition(int32_t a, int32_t b) { |
| 13 { | 13 int32_t result; |
| 14 int32_t result; | |
| 15 | 14 |
| 16 asm("qadd %[output],%[first],%[second]" | 15 asm("qadd %[output],%[first],%[second]" |
| 17 : [output] "=r" (result) | 16 : [output] "=r"(result) |
| 18 : [first] "r" (a), | 17 : [first] "r"(a), [second] "r"(b)); |
| 19 [second] "r" (b)); | |
| 20 | 18 |
| 21 return result; | 19 return result; |
| 22 } | 20 } |
| 23 | 21 |
| 24 ALWAYS_INLINE int32_t saturatedSubtraction(int32_t a, int32_t b) | 22 ALWAYS_INLINE int32_t saturatedSubtraction(int32_t a, int32_t b) { |
| 25 { | 23 int32_t result; |
| 26 int32_t result; | |
| 27 | 24 |
| 28 asm("qsub %[output],%[first],%[second]" | 25 asm("qsub %[output],%[first],%[second]" |
| 29 : [output] "=r" (result) | 26 : [output] "=r"(result) |
| 30 : [first] "r" (a), | 27 : [first] "r"(a), [second] "r"(b)); |
| 31 [second] "r" (b)); | |
| 32 | 28 |
| 33 return result; | 29 return result; |
| 34 } | 30 } |
| 35 | 31 |
| 36 inline int getMaxSaturatedSetResultForTesting(int FractionalShift) | 32 inline int getMaxSaturatedSetResultForTesting(int FractionalShift) { |
| 37 { | 33 // For ARM Asm version the set function maxes out to the biggest |
| 38 // For ARM Asm version the set function maxes out to the biggest | 34 // possible integer part with the fractional part zero'd out. |
| 39 // possible integer part with the fractional part zero'd out. | 35 // e.g. 0x7fffffc0. |
| 40 // e.g. 0x7fffffc0. | 36 return std::numeric_limits<int>::max() & ~((1 << FractionalShift) - 1); |
| 41 return std::numeric_limits<int>::max() & ~((1 << FractionalShift)-1); | |
| 42 } | 37 } |
| 43 | 38 |
| 44 inline int getMinSaturatedSetResultForTesting(int FractionalShift) | 39 inline int getMinSaturatedSetResultForTesting(int FractionalShift) { |
| 45 { | 40 return std::numeric_limits<int>::min(); |
| 46 return std::numeric_limits<int>::min(); | |
| 47 } | 41 } |
| 48 | 42 |
| 49 template <int FractionalShift> | 43 template <int FractionalShift> |
| 50 ALWAYS_INLINE int saturatedSet(int value) | 44 ALWAYS_INLINE int saturatedSet(int value) { |
| 51 { | 45 // Figure out how many bits are left for storing the integer part of |
| 52 // Figure out how many bits are left for storing the integer part of | 46 // the fixed point number, and saturate our input to that |
| 53 // the fixed point number, and saturate our input to that | 47 enum { Saturate = 32 - FractionalShift }; |
| 54 enum { Saturate = 32 - FractionalShift }; | |
| 55 | 48 |
| 56 int result; | 49 int result; |
| 57 | 50 |
| 58 // The following ARM code will Saturate the passed value to the number of | 51 // The following ARM code will Saturate the passed value to the number of |
| 59 // bits used for the whole part of the fixed point representation, then | 52 // bits used for the whole part of the fixed point representation, then |
| 60 // shift it up into place. This will result in the low <FractionShift> bits | 53 // shift it up into place. This will result in the low <FractionShift> bits |
| 61 // all being 0's. When the value saturates this gives a different result | 54 // all being 0's. When the value saturates this gives a different result |
| 62 // to from the C++ case; in the C++ code a saturated value has all the low | 55 // to from the C++ case; in the C++ code a saturated value has all the low |
| 63 // bits set to 1 (for a +ve number at least). This cannot be done rapidly | 56 // bits set to 1 (for a +ve number at least). This cannot be done rapidly |
| 64 // in ARM ... we live with the difference, for the sake of speed. | 57 // in ARM ... we live with the difference, for the sake of speed. |
| 65 | 58 |
| 66 asm("ssat %[output],%[saturate],%[value]\n\t" | 59 asm("ssat %[output],%[saturate],%[value]\n\t" |
| 67 "lsl %[output],%[shift]" | 60 "lsl %[output],%[shift]" |
| 68 : [output] "=r" (result) | 61 : [output] "=r"(result) |
| 69 : [value] "r" (value), | 62 : [value] "r"(value), [saturate] "n"(Saturate), |
| 70 [saturate] "n" (Saturate), | 63 [shift] "n"(FractionalShift)); |
| 71 [shift] "n" (FractionalShift)); | |
| 72 | 64 |
| 73 return result; | 65 return result; |
| 74 } | 66 } |
| 75 | 67 |
| 68 template <int FractionalShift> |
| 69 ALWAYS_INLINE int saturatedSet(unsigned value) { |
| 70 // Here we are being passed an unsigned value to saturate, |
| 71 // even though the result is returned as a signed integer. The ARM |
| 72 // instruction for unsigned saturation therefore needs to be given one |
| 73 // less bit (i.e. the sign bit) for the saturation to work correctly; hence |
| 74 // the '31' below. |
| 75 enum { Saturate = 31 - FractionalShift }; |
| 76 | 76 |
| 77 template <int FractionalShift> | 77 // The following ARM code will Saturate the passed value to the number of |
| 78 ALWAYS_INLINE int saturatedSet(unsigned value) | 78 // bits used for the whole part of the fixed point representation, then |
| 79 { | 79 // shift it up into place. This will result in the low <FractionShift> bits |
| 80 // Here we are being passed an unsigned value to saturate, | 80 // all being 0's. When the value saturates this gives a different result |
| 81 // even though the result is returned as a signed integer. The ARM | 81 // to from the C++ case; in the C++ code a saturated value has all the low |
| 82 // instruction for unsigned saturation therefore needs to be given one | 82 // bits set to 1. This cannot be done rapidly in ARM, so we live with the |
| 83 // less bit (i.e. the sign bit) for the saturation to work correctly; hence | 83 // difference, for the sake of speed. |
| 84 // the '31' below. | |
| 85 enum { Saturate = 31 - FractionalShift }; | |
| 86 | 84 |
| 87 // The following ARM code will Saturate the passed value to the number of | 85 int result; |
| 88 // bits used for the whole part of the fixed point representation, then | |
| 89 // shift it up into place. This will result in the low <FractionShift> bits | |
| 90 // all being 0's. When the value saturates this gives a different result | |
| 91 // to from the C++ case; in the C++ code a saturated value has all the low | |
| 92 // bits set to 1. This cannot be done rapidly in ARM, so we live with the | |
| 93 // difference, for the sake of speed. | |
| 94 | 86 |
| 95 int result; | 87 asm("usat %[output],%[saturate],%[value]\n\t" |
| 88 "lsl %[output],%[shift]" |
| 89 : [output] "=r"(result) |
| 90 : [value] "r"(value), [saturate] "n"(Saturate), |
| 91 [shift] "n"(FractionalShift)); |
| 96 | 92 |
| 97 asm("usat %[output],%[saturate],%[value]\n\t" | 93 return result; |
| 98 "lsl %[output],%[shift]" | |
| 99 : [output] "=r" (result) | |
| 100 : [value] "r" (value), | |
| 101 [saturate] "n" (Saturate), | |
| 102 [shift] "n" (FractionalShift)); | |
| 103 | |
| 104 return result; | |
| 105 } | 94 } |
| 106 | 95 |
| 107 #endif // SaturatedArithmeticARM_h | 96 #endif // SaturatedArithmeticARM_h |
| OLD | NEW |