blob: a3f5ff54e6666b4e75f673aafa85b74cf8fd54f5 [file] [log] [blame]
Andy Hung86eae0e2013-12-09 12:12:46 -08001/*
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
20namespace 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
Andy Hung6b667dd2015-02-06 15:05:37 -080028#if defined(__aarch64__) || defined(__ARM_NEON__)
Glenn Kasten4699a6a2016-02-16 10:49:09 -080029#ifndef USE_NEON
Andy Hung86eae0e2013-12-09 12:12:46 -080030#define USE_NEON (true)
Glenn Kasten4699a6a2016-02-16 10:49:09 -080031#endif
Andy Hung86eae0e2013-12-09 12:12:46 -080032#else
33#define USE_NEON (false)
34#endif
Glenn Kasten4699a6a2016-02-16 10:49:09 -080035#if USE_NEON
36#include <arm_neon.h>
37#endif
Andy Hung86eae0e2013-12-09 12:12:46 -080038
jaishank8070d8d2019-10-09 14:32:42 +053039#if defined(__AVX2__) // Should be supported in x86 ABI for both 32 & 64-bit.
40#define USE_AVX2 (true) // Inference AVX2/FMA Intrinsics
Henrik Smiding841920d2016-02-15 16:20:45 +010041#define USE_SSE (true)
jaishank8070d8d2019-10-09 14:32:42 +053042#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)
Henrik Smiding841920d2016-02-15 16:20:45 +010046#include <tmmintrin.h>
47#else
48#define USE_SSE (false)
jaishank8070d8d2019-10-09 14:32:42 +053049#define USE_AVX2(false)
Henrik Smiding841920d2016-02-15 16:20:45 +010050#endif
51
jaishank8070d8d2019-10-09 14:32:42 +053052
Andy Hung86eae0e2013-12-09 12:12:46 -080053template<typename T, typename U>
54struct is_same
55{
56 static const bool value = false;
57};
58
59template<typename T>
60struct is_same<T, T> // partial specialization
61{
62 static const bool value = true;
63};
64
65static inline
66int32_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
88static inline
89int32_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
103static inline
104int32_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
118static inline
119int32_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
133static inline
134int32_t mulAddRL(int left, uint32_t inRL, int16_t v, int32_t a)
135{
Andy Hung91452022017-01-05 17:11:19 -0800136#if 0 // USE_INLINE_ASSEMBLY Seems to fail with Clang b/34110890
Andy Hung86eae0e2013-12-09 12:12:46 -0800137 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
156static inline
157int32_t mulAddRL(int left, uint32_t inRL, int32_t v, int32_t a)
158{
Andy Hung91452022017-01-05 17:11:19 -0800159#if 0 // USE_INLINE_ASSEMBLY Seems to fail with Clang b/34110890
Andy Hung86eae0e2013-12-09 12:12:46 -0800160 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
Glenn Kasten63238ef2015-03-02 15:50:29 -0800179} // namespace android
Andy Hung86eae0e2013-12-09 12:12:46 -0800180
181#endif /*ANDROID_AUDIO_RESAMPLER_FIR_OPS_H*/