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 15 matching lines...) Expand all Loading... |
26 int32_t result; | 26 int32_t result; |
27 | 27 |
28 asm("qsub %[output],%[first],%[second]" | 28 asm("qsub %[output],%[first],%[second]" |
29 : [output] "=r" (result) | 29 : [output] "=r" (result) |
30 : [first] "r" (a), | 30 : [first] "r" (a), |
31 [second] "r" (b)); | 31 [second] "r" (b)); |
32 | 32 |
33 return result; | 33 return result; |
34 } | 34 } |
35 | 35 |
36 inline int getMaxSaturatedSetResultForTesting(int FractionalShift) | 36 inline int getMaxSaturatedSetResultForTesting(int fractionalShift) |
37 { | 37 { |
38 // 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 |
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<size_t saturate, size_t fractionalShift> |
| 50 ALWAYS_INLINE int saturatedSetSigned(int value) |
50 { | 51 { |
51 // 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 const int saturate = 32 - FractionalShift; | |
54 | |
55 int result; | 52 int result; |
56 | 53 // To allow all compilers to correctly build this asm we need to use |
57 // The following ARM code will Saturate the passed value to the number of | 54 // template specialization to ensure that the "n" parameters are |
58 // bits used for the whole part of the fixed point representation, then | 55 // compile-time constant values. |
59 // 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 // 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 // in ARM ... we live with the difference, for the sake of speed. | |
64 | |
65 asm("ssat %[output],%[saturate],%[value]\n\t" | 56 asm("ssat %[output],%[saturate],%[value]\n\t" |
66 "lsl %[output],%[shift]" | 57 "lsl %[output],%[shift]" |
67 : [output] "=r" (result) | 58 : [output] "=r" (result) |
68 : [value] "r" (value), | 59 : [value] "r" (value), |
69 [saturate] "n" (saturate), | 60 [saturate] "n" (saturate), |
70 [shift] "n" (FractionalShift)); | 61 [shift] "n" (fractionalShift)); |
71 | 62 |
72 return result; | 63 return result; |
73 } | 64 } |
74 | 65 |
75 | 66 template<size_t saturate, size_t fractionalShift> |
76 ALWAYS_INLINE int saturatedSet(unsigned value, int FractionalShift) | 67 ALWAYS_INLINE int saturatedSetUnsigned(unsigned value) |
77 { | 68 { |
78 // Here we are being passed an unsigned value to saturate, | |
79 // even though the result is returned as a signed integer. The ARM | |
80 // 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 | |
82 // the '31' below. | |
83 const int saturate = 31 - FractionalShift; | |
84 | |
85 // 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 | |
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 | |
93 int result; | 69 int result; |
94 | 70 // To allow all compilers to correctly build this asm we need to use |
| 71 // template specialization to ensure that the "n" parameters are |
| 72 // compile-time constant values. |
95 asm("usat %[output],%[saturate],%[value]\n\t" | 73 asm("usat %[output],%[saturate],%[value]\n\t" |
96 "lsl %[output],%[shift]" | 74 "lsl %[output],%[shift]" |
97 : [output] "=r" (result) | 75 : [output] "=r" (result) |
98 : [value] "r" (value), | 76 : [value] "r" (value), |
99 [saturate] "n" (saturate), | 77 [saturate] "n" (saturate), |
100 [shift] "n" (FractionalShift)); | 78 [shift] "n" (fractionalShift)); |
101 | 79 |
102 return result; | 80 return result; |
103 } | 81 } |
104 | 82 |
105 #endif // SaturatedArithmeticARM_h | 83 #endif // SaturatedArithmeticARM_h |
OLD | NEW |