Debian Codegraph @

modules/lsp-dsp-lib/include/private/dsp/arch/x86/avx/pfft.h

raw
1 /*
2 * Copyright (C) 2020 Linux Studio Plugins Project <https://lsp-plug.in/>
3 * (C) 2020 Vladimir Sadovnikov <[email protected]>
4 *
5 * This file is part of lsp-dsp-lib
6 * Created on: 31 мар. 2020 г.
7 *
8 * lsp-dsp-lib is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * any later version.
12 *
13 * lsp-dsp-lib is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with lsp-dsp-lib. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22 #ifndef PRIVATE_DSP_ARCH_X86_AVX_PFFT_H_
23 #define PRIVATE_DSP_ARCH_X86_AVX_PFFT_H_
24
25 #ifndef PRIVATE_DSP_ARCH_X86_AVX_IMPL
26 #error "This header should not be included directly"
27 #endif /* PRIVATE_DSP_ARCH_X86_AVX_IMPL */
28
29 #include <private/dsp/arch/x86/avx/fft/const.h>
30 #include <private/dsp/arch/x86/avx/fft/p_repack.h>
31 #include <private/dsp/arch/x86/avx/fft/p_butterfly.h>
32
33 // Scrambling functions
34 #define FFT_PSCRAMBLE_SELF_DIRECT_NAME packed_scramble_self_direct8
35 #define FFT_PSCRAMBLE_SELF_REVERSE_NAME packed_scramble_self_reverse8
36 #define FFT_PSCRAMBLE_COPY_DIRECT_NAME packed_scramble_copy_direct8
37 #define FFT_PSCRAMBLE_COPY_REVERSE_NAME packed_scramble_copy_reverse8
38 #define FFT_TYPE uint8_t
39 #define FFT_FMA(a, b) a
40 #include <private/dsp/arch/x86/avx/fft/p_scramble.h>
41
42 #define FFT_PSCRAMBLE_SELF_DIRECT_NAME packed_scramble_self_direct16
43 #define FFT_PSCRAMBLE_SELF_REVERSE_NAME packed_scramble_self_reverse16
44 #define FFT_PSCRAMBLE_COPY_DIRECT_NAME packed_scramble_copy_direct16
45 #define FFT_PSCRAMBLE_COPY_REVERSE_NAME packed_scramble_copy_reverse16
46 #define FFT_TYPE uint16_t
47 #define FFT_FMA(a, b) a
48 #include <private/dsp/arch/x86/avx/fft/p_scramble.h>
49
50 #define FFT_PSCRAMBLE_SELF_DIRECT_NAME packed_scramble_self_direct8_fma3
51 #define FFT_PSCRAMBLE_SELF_REVERSE_NAME packed_scramble_self_reverse8_fma3
52 #define FFT_PSCRAMBLE_COPY_DIRECT_NAME packed_scramble_copy_direct8_fma3
53 #define FFT_PSCRAMBLE_COPY_REVERSE_NAME packed_scramble_copy_reverse8_fma3
54 #define FFT_TYPE uint8_t
55 #define FFT_FMA(a, b) b
56 #include <private/dsp/arch/x86/avx/fft/p_scramble.h>
57
58 #define FFT_PSCRAMBLE_SELF_DIRECT_NAME packed_scramble_self_direct16_fma3
59 #define FFT_PSCRAMBLE_SELF_REVERSE_NAME packed_scramble_self_reverse16_fma3
60 #define FFT_PSCRAMBLE_COPY_DIRECT_NAME packed_scramble_copy_direct16_fma3
61 #define FFT_PSCRAMBLE_COPY_REVERSE_NAME packed_scramble_copy_reverse16_fma3
62 #define FFT_TYPE uint16_t
63 #define FFT_FMA(a, b) b
64 #include <private/dsp/arch/x86/avx/fft/p_scramble.h>
65
66 namespace lsp
67 {
68 namespace avx
69 {
70 static void packed_small_direct_fft(float *dst, const float *src, size_t rank)
71 {
72 if (rank == 2)
73 {
74 float s0_re = src[0] + src[4];
75 float s1_re = src[0] - src[4];
76 float s0_im = src[1] + src[5];
77 float s1_im = src[1] - src[5];
78
79 float s2_re = src[2] + src[6];
80 float s3_re = src[2] - src[6];
81 float s2_im = src[3] + src[7];
82 float s3_im = src[3] - src[7];
83
84 dst[0] = s0_re + s2_re;
85 dst[1] = s0_im + s2_im;
86 dst[2] = s1_re + s3_im;
87 dst[3] = s1_im - s3_re;
88
89 dst[4] = s0_re - s2_re;
90 dst[5] = s0_im - s2_im;
91 dst[6] = s1_re - s3_im;
92 dst[7] = s1_im + s3_re;
93 }
94 else if (rank == 1)
95 {
96 // s0' = s0 + s1
97 // s1' = s0 - s1
98 float s1_re = src[2];
99 float s1_im = src[3];
100 dst[2] = src[0] - s1_re;
101 dst[3] = src[1] - s1_im;
102 dst[0] = src[0] + s1_re;
103 dst[1] = src[1] + s1_im;
104 }
105 else
106 {
107 dst[0] = src[0];
108 dst[1] = src[1];
109 }
110 }
111
112 static void packed_small_reverse_fft(float *dst, const float *src, size_t rank)
113 {
114 if (rank == 2)
115 {
116 float s0_re = src[0] + src[4];
117 float s1_re = src[0] - src[4];
118 float s0_im = src[1] + src[5];
119 float s1_im = src[1] - src[5];
120
121 float s2_re = src[2] + src[6];
122 float s3_re = src[2] - src[6];
123 float s2_im = src[3] + src[7];
124 float s3_im = src[3] - src[7];
125
126 dst[0] = (s0_re + s2_re)*0.25f;
127 dst[1] = (s0_im + s2_im)*0.25f;
128 dst[2] = (s1_re - s3_im)*0.25f;
129 dst[3] = (s1_im + s3_re)*0.25f;
130
131 dst[4] = (s0_re - s2_re)*0.25f;
132 dst[5] = (s0_im - s2_im)*0.25f;
133 dst[6] = (s1_re + s3_im)*0.25f;
134 dst[7] = (s1_im - s3_re)*0.25f;
135 }
136 else if (rank == 1)
137 {
138 // s0' = s0 + s1
139 // s1' = s0 - s1
140 float s1_re = src[2];
141 float s1_im = src[3];
142 dst[2] = src[0] - s1_re;
143 dst[3] = src[1] - s1_im;
144 dst[0] = src[0] + s1_re;
145 dst[1] = src[1] + s1_im;
146 }
147 else
148 {
149 dst[0] = src[0];
150 dst[1] = src[1];
151 }
152 return;
153 }
154
155 void packed_direct_fft(float *dst, const float *src, size_t rank)
156 {
157 if (rank <= 2)
158 {
159 packed_small_direct_fft(dst, src, rank);
160 return;
161 }
162
163 if ((dst == src) || (rank < 4))
164 {
165 dsp::move(dst, src, 2 << rank); // 1 << rank + 1
166 if (rank <= 8)
167 packed_scramble_self_direct8(dst, rank);
168 else
169 packed_scramble_self_direct16(dst, rank);
170 }
171 else
172 {
173 if (rank <= 12)
174 packed_scramble_copy_direct8(dst, src, rank-4);
175 else
176 packed_scramble_copy_direct16(dst, src, rank-4);
177 }
178
179 for (size_t i=3; i < rank; ++i)
180 packed_butterfly_direct8p(dst, i, 1 << (rank - i - 1));
181
182 packed_fft_repack(dst, rank);
183 }
184
185 void packed_reverse_fft(float *dst, const float *src, size_t rank)
186 {
187 if (rank <= 2)
188 {
189 packed_small_reverse_fft(dst, src, rank);
190 return;
191 }
192
193 if ((dst == src) || (rank < 4))
194 {
195 dsp::move(dst, src, 2 << rank); // 1 << rank + 1
196 if (rank <= 8)
197 packed_scramble_self_reverse8(dst, rank);
198 else
199 packed_scramble_self_reverse16(dst, rank);
200 }
201 else
202 {
203 if (rank <= 12)
204 packed_scramble_copy_reverse8(dst, src, rank-4);
205 else
206 packed_scramble_copy_reverse16(dst, src, rank-4);
207 }
208
209 for (size_t i=3; i < rank; ++i)
210 packed_butterfly_reverse8p(dst, i, 1 << (rank - i - 1));
211
212 packed_fft_repack_normalize(dst, rank);
213 }
214
215 void packed_direct_fft_fma3(float *dst, const float *src, size_t rank)
216 {
217 if (rank <= 2)
218 {
219 packed_small_direct_fft(dst, src, rank);
220 return;
221 }
222
223 if ((dst == src) || (rank < 4))
224 {
225 dsp::move(dst, src, 2 << rank); // 1 << rank + 1
226 if (rank <= 8)
227 packed_scramble_self_direct8_fma3(dst, rank);
228 else
229 packed_scramble_self_direct16_fma3(dst, rank);
230 }
231 else
232 {
233 if (rank <= 12)
234 packed_scramble_copy_direct8_fma3(dst, src, rank-4);
235 else
236 packed_scramble_copy_direct16_fma3(dst, src, rank-4);
237 }
238
239 for (size_t i=3; i < rank; ++i)
240 packed_butterfly_direct8p_fma3(dst, i, 1 << (rank - i - 1));
241
242 packed_fft_repack(dst, rank);
243 }
244
245 void packed_reverse_fft_fma3(float *dst, const float *src, size_t rank)
246 {
247 if (rank <= 2)
248 {
249 packed_small_reverse_fft(dst, src, rank);
250 return;
251 }
252
253 if ((dst == src) || (rank < 4))
254 {
255 dsp::move(dst, src, 2 << rank); // 1 << rank + 1
256 if (rank <= 8)
257 packed_scramble_self_reverse8_fma3(dst, rank);
258 else
259 packed_scramble_self_reverse16_fma3(dst, rank);
260 }
261 else
262 {
263 if (rank <= 12)
264 packed_scramble_copy_reverse8_fma3(dst, src, rank-4);
265 else
266 packed_scramble_copy_reverse16_fma3(dst, src, rank-4);
267 }
268
269 for (size_t i=3; i < rank; ++i)
270 packed_butterfly_reverse8p_fma3(dst, i, 1 << (rank - i - 1));
271
272 packed_fft_repack_normalize(dst, rank);
273 }
274 }
275 }
276
277 #endif /* PRIVATE_DSP_ARCH_X86_AVX_PFFT_H_ */