| 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> |
| (...skipping 28 matching lines...) Expand all Loading... |
| 39 // possible integer part with the fractional part zero'd out. | 39 // possible integer part with the fractional part zero'd out. |
| 40 // e.g. 0x7fffffc0. | 40 // e.g. 0x7fffffc0. |
| 41 return std::numeric_limits<int>::max() & ~((1 << FractionalShift)-1); | 41 return std::numeric_limits<int>::max() & ~((1 << FractionalShift)-1); |
| 42 } | 42 } |
| 43 | 43 |
| 44 inline int getMinSaturatedSetResultForTesting(int FractionalShift) | 44 inline int getMinSaturatedSetResultForTesting(int FractionalShift) |
| 45 { | 45 { |
| 46 return std::numeric_limits<int>::min(); | 46 return std::numeric_limits<int>::min(); |
| 47 } | 47 } |
| 48 | 48 |
| 49 ALWAYS_INLINE int saturatedSet(int value, int FractionalShift) | 49 template <int FractionalShift> |
| 50 ALWAYS_INLINE int saturatedSet(int value) |
| 50 { | 51 { |
| 51 // 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 |
| 52 // the fixed point number, and saturate our input to that | 53 // the fixed point number, and saturate our input to that |
| 53 const int saturate = 32 - FractionalShift; | 54 enum { Saturate = 32 - FractionalShift }; |
| 54 | 55 |
| 55 int result; | 56 int result; |
| 56 | 57 |
| 57 // The following ARM code will Saturate the passed value to the number of | 58 // 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 | 59 // 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 | 60 // 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 | 61 // 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 | 62 // 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 | 63 // 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. | 64 // in ARM ... we live with the difference, for the sake of speed. |
| 64 | 65 |
| 65 asm("ssat %[output],%[saturate],%[value]\n\t" | 66 asm("ssat %[output],%[saturate],%[value]\n\t" |
| 66 "lsl %[output],%[shift]" | 67 "lsl %[output],%[shift]" |
| 67 : [output] "=r" (result) | 68 : [output] "=r" (result) |
| 68 : [value] "r" (value), | 69 : [value] "r" (value), |
| 69 [saturate] "n" (saturate), | 70 [saturate] "n" (Saturate), |
| 70 [shift] "n" (FractionalShift)); | 71 [shift] "n" (FractionalShift)); |
| 71 | 72 |
| 72 return result; | 73 return result; |
| 73 } | 74 } |
| 74 | 75 |
| 75 | 76 |
| 76 ALWAYS_INLINE int saturatedSet(unsigned value, int FractionalShift) | 77 template <int FractionalShift> |
| 78 ALWAYS_INLINE int saturatedSet(unsigned value) |
| 77 { | 79 { |
| 78 // Here we are being passed an unsigned value to saturate, | 80 // Here we are being passed an unsigned value to saturate, |
| 79 // even though the result is returned as a signed integer. The ARM | 81 // even though the result is returned as a signed integer. The ARM |
| 80 // instruction for unsigned saturation therefore needs to be given one | 82 // instruction for unsigned saturation therefore needs to be given one |
| 81 // less bit (i.e. the sign bit) for the saturation to work correctly; hence | 83 // less bit (i.e. the sign bit) for the saturation to work correctly; hence |
| 82 // the '31' below. | 84 // the '31' below. |
| 83 const int saturate = 31 - FractionalShift; | 85 enum { Saturate = 31 - FractionalShift }; |
| 84 | 86 |
| 85 // The following ARM code will Saturate the passed value to the number of | 87 // The following ARM code will Saturate the passed value to the number of |
| 86 // bits used for the whole part of the fixed point representation, then | 88 // 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 | 89 // 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 | 90 // 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 | 91 // 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 | 92 // bits set to 1. This cannot be done rapidly in ARM, so we live with the |
| 91 // difference, for the sake of speed. | 93 // difference, for the sake of speed. |
| 92 | 94 |
| 93 int result; | 95 int result; |
| 94 | 96 |
| 95 asm("usat %[output],%[saturate],%[value]\n\t" | 97 asm("usat %[output],%[saturate],%[value]\n\t" |
| 96 "lsl %[output],%[shift]" | 98 "lsl %[output],%[shift]" |
| 97 : [output] "=r" (result) | 99 : [output] "=r" (result) |
| 98 : [value] "r" (value), | 100 : [value] "r" (value), |
| 99 [saturate] "n" (saturate), | 101 [saturate] "n" (Saturate), |
| 100 [shift] "n" (FractionalShift)); | 102 [shift] "n" (FractionalShift)); |
| 101 | 103 |
| 102 return result; | 104 return result; |
| 103 } | 105 } |
| 104 | 106 |
| 105 #endif // SaturatedArithmeticARM_h | 107 #endif // SaturatedArithmeticARM_h |
| OLD | NEW |