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