• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2013 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef ANDROID_AUDIO_RESAMPLER_FIR_OPS_H
18 #define ANDROID_AUDIO_RESAMPLER_FIR_OPS_H
19 
20 namespace android {
21 
22 #if defined(__arm__) && !defined(__thumb__)
23 #define USE_INLINE_ASSEMBLY (true)
24 #else
25 #define USE_INLINE_ASSEMBLY (false)
26 #endif
27 
28 #if defined(__aarch64__) || defined(__ARM_NEON__)
29 #ifndef USE_NEON
30 #define USE_NEON (true)
31 #endif
32 #else
33 #define USE_NEON (false)
34 #endif
35 #if USE_NEON
36 #include <arm_neon.h>
37 #endif
38 
39 #if defined(__AVX2__)  // Should be supported in x86 ABI for both 32 & 64-bit.
40 #define USE_AVX2 (true)  // Inference AVX2/FMA Intrinsics
41 #define USE_SSE (true)
42 #include <immintrin.h>
43 #elif defined(__SSSE3__)  // Should be supported in x86 ABI for both 32 & 64-bit.
44 #define USE_SSE (true)  // Inference SSE Intrinsics
45 #define USE_AVX2 (false)
46 #include <tmmintrin.h>
47 #else
48 #define USE_SSE (false)
49 #define USE_AVX2(false)
50 #endif
51 
52 
53 template<typename T, typename U>
54 struct is_same
55 {
56     static const bool value = false;
57 };
58 
59 template<typename T>
60 struct is_same<T, T>  // partial specialization
61 {
62     static const bool value = true;
63 };
64 
65 static inline
66 int32_t mulRL(int left, int32_t in, uint32_t vRL)
67 {
68 #if USE_INLINE_ASSEMBLY
69     int32_t out;
70     if (left) {
71         asm( "smultb %[out], %[in], %[vRL] \n"
72              : [out]"=r"(out)
73              : [in]"%r"(in), [vRL]"r"(vRL)
74              : );
75     } else {
76         asm( "smultt %[out], %[in], %[vRL] \n"
77              : [out]"=r"(out)
78              : [in]"%r"(in), [vRL]"r"(vRL)
79              : );
80     }
81     return out;
82 #else
83     int16_t v = left ? static_cast<int16_t>(vRL) : static_cast<int16_t>(vRL>>16);
84     return static_cast<int32_t>((static_cast<int64_t>(in) * v) >> 16);
85 #endif
86 }
87 
88 static inline
89 int32_t mulAdd(int16_t in, int16_t v, int32_t a)
90 {
91 #if USE_INLINE_ASSEMBLY
92     int32_t out;
93     asm( "smlabb %[out], %[v], %[in], %[a] \n"
94          : [out]"=r"(out)
95          : [in]"%r"(in), [v]"r"(v), [a]"r"(a)
96          : );
97     return out;
98 #else
99     return a + v * in;
100 #endif
101 }
102 
103 static inline
104 int32_t mulAdd(int16_t in, int32_t v, int32_t a)
105 {
106 #if USE_INLINE_ASSEMBLY
107     int32_t out;
108     asm( "smlawb %[out], %[v], %[in], %[a] \n"
109          : [out]"=r"(out)
110          : [in]"%r"(in), [v]"r"(v), [a]"r"(a)
111          : );
112     return out;
113 #else
114     return a + static_cast<int32_t>((static_cast<int64_t>(v) * in) >> 16);
115 #endif
116 }
117 
118 static inline
119 int32_t mulAdd(int32_t in, int32_t v, int32_t a)
120 {
121 #if USE_INLINE_ASSEMBLY
122     int32_t out;
123     asm( "smmla %[out], %[v], %[in], %[a] \n"
124          : [out]"=r"(out)
125          : [in]"%r"(in), [v]"r"(v), [a]"r"(a)
126          : );
127     return out;
128 #else
129     return a + static_cast<int32_t>((static_cast<int64_t>(v) * in) >> 32);
130 #endif
131 }
132 
133 static inline
134 int32_t mulAddRL(int left, uint32_t inRL, int16_t v, int32_t a)
135 {
136 #if 0 // USE_INLINE_ASSEMBLY Seems to fail with Clang b/34110890
137     int32_t out;
138     if (left) {
139         asm( "smlabb %[out], %[v], %[inRL], %[a] \n"
140              : [out]"=r"(out)
141              : [inRL]"%r"(inRL), [v]"r"(v), [a]"r"(a)
142              : );
143     } else {
144         asm( "smlabt %[out], %[v], %[inRL], %[a] \n"
145              : [out]"=r"(out)
146              : [inRL]"%r"(inRL), [v]"r"(v), [a]"r"(a)
147              : );
148     }
149     return out;
150 #else
151     int16_t s = left ? static_cast<int16_t>(inRL) : static_cast<int16_t>(inRL>>16);
152     return a + v * s;
153 #endif
154 }
155 
156 static inline
157 int32_t mulAddRL(int left, uint32_t inRL, int32_t v, int32_t a)
158 {
159 #if 0 // USE_INLINE_ASSEMBLY Seems to fail with Clang b/34110890
160     int32_t out;
161     if (left) {
162         asm( "smlawb %[out], %[v], %[inRL], %[a] \n"
163              : [out]"=r"(out)
164              : [inRL]"%r"(inRL), [v]"r"(v), [a]"r"(a)
165              : );
166     } else {
167         asm( "smlawt %[out], %[v], %[inRL], %[a] \n"
168              : [out]"=r"(out)
169              : [inRL]"%r"(inRL), [v]"r"(v), [a]"r"(a)
170              : );
171     }
172     return out;
173 #else
174     int16_t s = left ? static_cast<int16_t>(inRL) : static_cast<int16_t>(inRL>>16);
175     return a + static_cast<int32_t>((static_cast<int64_t>(v) * s) >> 16);
176 #endif
177 }
178 
179 } // namespace android
180 
181 #endif /*ANDROID_AUDIO_RESAMPLER_FIR_OPS_H*/
182