Debian Codegraph @

modules/lsp-dsp-lib/src/test/ptest/fft/fastconv.cpp

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 #include <lsp-plug.in/common/alloc.h>
23 #include <lsp-plug.in/common/types.h>
24 #include <lsp-plug.in/dsp/dsp.h>
25 #include <lsp-plug.in/test-fw/helpers.h>
26 #include <lsp-plug.in/test-fw/ptest.h>
27
28 #define MIN_RANK 8
29 #define MAX_RANK 16
30
31 namespace lsp
32 {
33 namespace generic
34 {
35 void direct_fft(float *dst_re, float *dst_im, const float *src_re, const float *src_im, size_t rank);
36 void reverse_fft(float *dst_re, float *dst_im, const float *src_re, const float *src_im, size_t rank);
37 void complex_mul3(float *dst_re, float *dst_im, const float *src1_re, const float *src1_im, const float *src2_re, const float *src2_im, size_t count);
38 void add2(float *dst, const float *src, size_t count);
39
40 void fastconv_parse(float *dst, const float *src, size_t rank);
41 void fastconv_parse_apply(float *dst, float *tmp, const float *c, const float *src, size_t rank);
42 }
43
44 IF_ARCH_X86(
45 namespace sse
46 {
47 void direct_fft(float *dst_re, float *dst_im, const float *src_re, const float *src_im, size_t rank);
48 void reverse_fft(float *dst_re, float *dst_im, const float *src_re, const float *src_im, size_t rank);
49 void complex_mul3(float *dst_re, float *dst_im, const float *src1_re, const float *src1_im, const float *src2_re, const float *src2_im, size_t count);
50 void add2(float *dst, const float *src, size_t count);
51
52 void fastconv_parse(float *dst, const float *src, size_t rank);
53 void fastconv_parse_apply(float *dst, float *tmp, const float *c, const float *src, size_t rank);
54 }
55
56 namespace avx
57 {
58 void direct_fft(float *dst_re, float *dst_im, const float *src_re, const float *src_im, size_t rank);
59 void reverse_fft(float *dst_re, float *dst_im, const float *src_re, const float *src_im, size_t rank);
60 void complex_mul3(float *dst_re, float *dst_im, const float *src1_re, const float *src1_im, const float *src2_re, const float *src2_im, size_t count);
61 void add2(float *dst, const float *src, size_t count);
62
63 void fastconv_parse(float *dst, const float *src, size_t rank);
64 void fastconv_parse_apply(float *dst, float *tmp, const float *c, const float *src, size_t rank);
65
66 void direct_fft_fma3(float *dst_re, float *dst_im, const float *src_re, const float *src_im, size_t rank);
67 void reverse_fft_fma3(float *dst_re, float *dst_im, const float *src_re, const float *src_im, size_t rank);
68 void complex_mul3_fma3(float *dst_re, float *dst_im, const float *src1_re, const float *src1_im, const float *src2_re, const float *src2_im, size_t count);
69
70 void fastconv_parse_fma3(float *dst, const float *src, size_t rank);
71 void fastconv_parse_apply_fma3(float *dst, float *tmp, const float *c, const float *src, size_t rank);
72 }
73 )
74
75 IF_ARCH_ARM(
76 namespace neon_d32
77 {
78 void direct_fft(float *dst_re, float *dst_im, const float *src_re, const float *src_im, size_t rank);
79 void reverse_fft(float *dst_re, float *dst_im, const float *src_re, const float *src_im, size_t rank);
80 void complex_mul3(float *dst_re, float *dst_im, const float *src1_re, const float *src1_im, const float *src2_re, const float *src2_im, size_t count);
81 void add2(float *dst, const float *src, size_t count);
82
83 void fastconv_parse(float *dst, const float *src, size_t rank);
84 void fastconv_parse_apply(float *dst, float *tmp, const float *c, const float *src, size_t rank);
85 }
86 )
87
88 IF_ARCH_AARCH64(
89 namespace asimd
90 {
91 void direct_fft(float *dst_re, float *dst_im, const float *src_re, const float *src_im, size_t rank);
92 void reverse_fft(float *dst_re, float *dst_im, const float *src_re, const float *src_im, size_t rank);
93 void complex_mul3(float *dst_re, float *dst_im, const float *src1_re, const float *src1_im, const float *src2_re, const float *src2_im, size_t count);
94 void add2(float *dst, const float *src, size_t count);
95
96 void fastconv_parse(float *dst, const float *src, size_t rank);
97 void fastconv_parse_apply(float *dst, float *tmp, const float *c, const float *src, size_t rank);
98 }
99 )
100
101 typedef void (* direct_fft_t)(float *dst_re, float *dst_im, const float *src_re, const float *src_im, size_t rank);
102 typedef void (* reverse_fft_t)(float *dst_re, float *dst_im, const float *src_re, const float *src_im, size_t rank);
103 typedef void (* complex_mul3_t)(float *dst_re, float *dst_im, const float *src1_re, const float *src1_im, const float *src2_re, const float *src2_im, size_t count);
104 typedef void (* add2_t)(float *dst, const float *src, size_t count);
105
106 typedef void (* fastconv_parse_t)(float *dst, const float *src, size_t rank);
107 typedef void (* fastconv_parse_apply_t)(float *dst, float *tmp, const float *c, const float *src, size_t rank);
108 }
109
110 //-----------------------------------------------------------------------------
111 // Performance test for complex multiplication
112 PTEST_BEGIN("dsp.fft", fastconv, 10, 1000)
113
114 void call(
115 const char *label,
116 float *out, float *tmp, float *tmp2, float *conv, const float *in, const float *cv, size_t rank,
117 direct_fft_t direct, complex_mul3_t cmul, reverse_fft_t reverse, add2_t add2
118 )
119 {
120 if (!(PTEST_SUPPORTED(direct) && (PTEST_SUPPORTED(cmul)) && (PTEST_SUPPORTED(reverse)) && (PTEST_SUPPORTED(add2))))
121 return;
122
123 char buf[80];
124 snprintf(buf, sizeof(buf), "%s x %d", label, int(1 << rank));
125 printf("Testing %s samples (rank = %d)...\n", buf, int(rank));
126 size_t bin_size = 1 << rank;
127
128 // Prepare data
129 dsp::fill_zero(out, bin_size);
130 direct(conv, &conv[bin_size], cv, &cv[bin_size], rank);
131
132 // Run test
133 PTEST_LOOP(buf,
134 direct(tmp, &tmp[bin_size], in, &in[bin_size], rank);
135 cmul(tmp, &tmp[bin_size], tmp, &tmp[bin_size], conv, &conv[bin_size], bin_size);
136 reverse(tmp2, &tmp2[bin_size], tmp, &tmp[bin_size], rank);
137 add2(out, tmp2, bin_size);
138 )
139 }
140
141 void call(
142 const char *label,
143 float *out, float *tmp, float *conv, const float *in, const float *cv, size_t rank,
144 fastconv_parse_t parse, fastconv_parse_apply_t apply
145 )
146 {
147 if (!(PTEST_SUPPORTED(parse) && (PTEST_SUPPORTED(apply))))
148 return;
149
150 char buf[80];
151 snprintf(buf, sizeof(buf), "%s x %d", label, int(1 << rank));
152 printf("Testing %s samples (rank = %d)...\n", buf, int(rank));
153
154 // Prepare data
155 dsp::fill_zero(out, 1 << rank);
156 parse(conv, cv, rank);
157
158 PTEST_LOOP(buf,
159 apply(out, tmp, conv, in, rank);
160 );
161 }
162
163 PTEST_MAIN
164 {
165 size_t fft_size = 1 << MAX_RANK;
166 size_t alloc = fft_size * 2 * 2 // in + cv size
167 + fft_size * 2 // conv size
168 + fft_size * 2 // tmp size
169 + fft_size * 2 // tmp2 size
170 + fft_size; // out size
171
172 uint8_t *data = NULL;
173 float *in = alloc_aligned<float>(data, alloc, 64);
174 dsp::fill_zero(in, alloc);
175
176 float *cv = &in[fft_size * 2];
177 float *conv = &cv[fft_size * 2];
178 float *tmp = &conv[fft_size * 2];
179 float *tmp2 = &tmp[fft_size * 2];
180 float *out = &tmp2[fft_size * 2];
181
182 for (size_t i=0; i < (fft_size / 2); ++i)
183 {
184 in[i] = randf(0.0f, 1.0f);
185 cv[i] = randf(0.0f, 1.0f);
186 }
187
188 for (size_t rank=MIN_RANK; rank <= MAX_RANK; ++rank)
189 {
190 call("generic::fft", out, tmp, tmp2, conv, in, cv, rank,
191 generic::direct_fft, generic::complex_mul3, generic::reverse_fft, generic::add2);
192 call("generic::fastconv_fft", out, tmp, conv, in, cv, rank,
193 generic::fastconv_parse, generic::fastconv_parse_apply);
194
195 IF_ARCH_X86(
196 call("sse::fft", out, tmp, tmp2, conv, in, cv, rank,
197 sse::direct_fft, sse::complex_mul3, sse::reverse_fft, sse::add2);
198 call("avx::fft", out, tmp, tmp2, conv, in, cv, rank,
199 avx::direct_fft, avx::complex_mul3, avx::reverse_fft, avx::add2);
200 call("avx::fft_fma3", out, tmp, tmp2, conv, in, cv, rank,
201 avx::direct_fft_fma3, avx::complex_mul3_fma3, avx::reverse_fft_fma3, avx::add2);
202
203 call("sse::fastconv_fft", out, tmp, conv, in, cv, rank,
204 sse::fastconv_parse, sse::fastconv_parse_apply);
205 call("avx::fastconv_fft", out, tmp, conv, in, cv, rank,
206 avx::fastconv_parse, avx::fastconv_parse_apply);
207 call("avx::fastconv_fft_fma3", out, tmp, conv, in, cv, rank,
208 avx::fastconv_parse_fma3, avx::fastconv_parse_apply_fma3);
209 )
210
211 IF_ARCH_ARM(
212 call("neon_d32::fft", out, tmp, tmp2, conv, in, cv, rank,
213 neon_d32::direct_fft, neon_d32::complex_mul3, neon_d32::reverse_fft, neon_d32::add2);
214 call("neon_d32::fastconv_fft", out, tmp, conv, in, cv, rank,
215 neon_d32::fastconv_parse, neon_d32::fastconv_parse_apply);
216 )
217
218 IF_ARCH_AARCH64(
219 call("asimd::fft", out, tmp, tmp2, conv, in, cv, rank,
220 asimd::direct_fft, asimd::complex_mul3, asimd::reverse_fft, asimd::add2);
221 call("asimd::fastconv_fft", out, tmp, conv, in, cv, rank,
222 asimd::fastconv_parse, asimd::fastconv_parse_apply);
223 )
224
225 PTEST_SEPARATOR;
226 }
227
228 free_aligned(data);
229 }
230
231 PTEST_END
232
233