]> git.sesse.net Git - ffmpeg/blob - libavutil/tests/float_dsp.c
Merge commit 'e7de05f98f630b5b3a5e441c8fa763e6d89b8851'
[ffmpeg] / libavutil / tests / float_dsp.c
1 /*
2  * This file is part of FFmpeg.
3  *
4  * FFmpeg is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * FFmpeg is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with FFmpeg; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18
19 #include "config.h"
20
21 #include <float.h>
22 #include <math.h>
23 #include <stdint.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #if HAVE_UNISTD_H
27 #include <unistd.h> /* for getopt */
28 #endif
29 #if !HAVE_GETOPT
30 #include "compat/getopt.c"
31 #endif
32
33 #include "libavutil/common.h"
34 #include "libavutil/cpu.h"
35 #include "libavutil/internal.h"
36 #include "libavutil/lfg.h"
37 #include "libavutil/log.h"
38 #include "libavutil/random_seed.h"
39 #include "libavutil/float_dsp.h"
40
41 #define LEN 240
42
43 static void fill_float_array(AVLFG *lfg, float *a, int len)
44 {
45     int i;
46     double bmg[2], stddev = 10.0, mean = 0.0;
47
48     for (i = 0; i < len; i += 2) {
49         av_bmg_get(lfg, bmg);
50         a[i]     = bmg[0] * stddev + mean;
51         a[i + 1] = bmg[1] * stddev + mean;
52     }
53 }
54 static int compare_floats(const float *a, const float *b, int len,
55                           float max_diff)
56 {
57     int i;
58     for (i = 0; i < len; i++) {
59         if (fabsf(a[i] - b[i]) > max_diff) {
60             av_log(NULL, AV_LOG_ERROR, "%d: %- .12f - %- .12f = % .12g\n",
61                    i, a[i], b[i], a[i] - b[i]);
62             return -1;
63         }
64     }
65     return 0;
66 }
67
68 static void fill_double_array(AVLFG *lfg, double *a, int len)
69 {
70     int i;
71     double bmg[2], stddev = 10.0, mean = 0.0;
72
73     for (i = 0; i < len; i += 2) {
74         av_bmg_get(lfg, bmg);
75         a[i]     = bmg[0] * stddev + mean;
76         a[i + 1] = bmg[1] * stddev + mean;
77     }
78 }
79
80 static int compare_doubles(const double *a, const double *b, int len,
81                            double max_diff)
82 {
83     int i;
84
85     for (i = 0; i < len; i++) {
86         if (fabs(a[i] - b[i]) > max_diff) {
87             av_log(NULL, AV_LOG_ERROR, "%d: %- .12f - %- .12f = % .12g\n",
88                    i, a[i], b[i], a[i] - b[i]);
89             return -1;
90         }
91     }
92     return 0;
93 }
94
95 static int test_vector_fmul(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp,
96                             const float *v1, const float *v2)
97 {
98     LOCAL_ALIGNED(32, float, cdst, [LEN]);
99     LOCAL_ALIGNED(32, float, odst, [LEN]);
100     int ret;
101
102     cdsp->vector_fmul(cdst, v1, v2, LEN);
103     fdsp->vector_fmul(odst, v1, v2, LEN);
104
105     if (ret = compare_floats(cdst, odst, LEN, FLT_EPSILON))
106         av_log(NULL, AV_LOG_ERROR, "vector_fmul failed\n");
107
108     return ret;
109 }
110
111 #define ARBITRARY_FMAC_SCALAR_CONST 0.005
112 static int test_vector_fmac_scalar(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp,
113                                    const float *v1, const float *src0, float scale)
114 {
115     LOCAL_ALIGNED(32, float, cdst, [LEN]);
116     LOCAL_ALIGNED(32, float, odst, [LEN]);
117     int ret;
118
119     memcpy(cdst, v1, LEN * sizeof(*v1));
120     memcpy(odst, v1, LEN * sizeof(*v1));
121
122     cdsp->vector_fmac_scalar(cdst, src0, scale, LEN);
123     fdsp->vector_fmac_scalar(odst, src0, scale, LEN);
124
125     if (ret = compare_floats(cdst, odst, LEN, ARBITRARY_FMAC_SCALAR_CONST))
126         av_log(NULL, AV_LOG_ERROR, "vector_fmac_scalar failed\n");
127
128     return ret;
129 }
130
131 static int test_vector_fmul_scalar(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp,
132                                    const float *v1, float scale)
133 {
134     LOCAL_ALIGNED(32, float, cdst, [LEN]);
135     LOCAL_ALIGNED(32, float, odst, [LEN]);
136     int ret;
137
138     cdsp->vector_fmul_scalar(cdst, v1, scale, LEN);
139     fdsp->vector_fmul_scalar(odst, v1, scale, LEN);
140
141     if (ret = compare_floats(cdst, odst, LEN, FLT_EPSILON))
142         av_log(NULL, AV_LOG_ERROR, "vector_fmul_scalar failed\n");
143
144     return ret;
145 }
146
147 #define ARBITRARY_DMAC_SCALAR_CONST 0.005
148 static int test_vector_dmac_scalar(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp,
149                                    const double *v1, const double *src0, double scale)
150 {
151     LOCAL_ALIGNED(32, double, cdst, [LEN]);
152     LOCAL_ALIGNED(32, double, odst, [LEN]);
153     int ret;
154
155     memcpy(cdst, v1, LEN * sizeof(*v1));
156     memcpy(odst, v1, LEN * sizeof(*v1));
157
158     cdsp->vector_dmac_scalar(cdst, src0, scale, LEN);
159     fdsp->vector_dmac_scalar(odst, src0, scale, LEN);
160
161     if (ret = compare_doubles(cdst, odst, LEN, ARBITRARY_DMAC_SCALAR_CONST))
162         av_log(NULL, AV_LOG_ERROR, "vector_dmac_scalar failed\n");
163
164     return ret;
165 }
166
167 static int test_vector_dmul_scalar(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp,
168                                    const double *v1, double scale)
169 {
170     LOCAL_ALIGNED(32, double, cdst, [LEN]);
171     LOCAL_ALIGNED(32, double, odst, [LEN]);
172     int ret;
173
174     cdsp->vector_dmul_scalar(cdst, v1, scale, LEN);
175     fdsp->vector_dmul_scalar(odst, v1, scale, LEN);
176
177     if (ret = compare_doubles(cdst, odst, LEN, DBL_EPSILON))
178         av_log(NULL, AV_LOG_ERROR, "vector_dmul_scalar failed\n");
179
180     return ret;
181 }
182
183 #define ARBITRARY_FMUL_WINDOW_CONST 0.008
184 static int test_vector_fmul_window(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp,
185                                    const float *v1, const float *v2, const float *v3)
186 {
187     LOCAL_ALIGNED(32, float, cdst, [LEN]);
188     LOCAL_ALIGNED(32, float, odst, [LEN]);
189     int ret;
190
191     cdsp->vector_fmul_window(cdst, v1, v2, v3, LEN / 2);
192     fdsp->vector_fmul_window(odst, v1, v2, v3, LEN / 2);
193
194     if (ret = compare_floats(cdst, odst, LEN, ARBITRARY_FMUL_WINDOW_CONST))
195         av_log(NULL, AV_LOG_ERROR, "vector_fmul_window failed\n");
196
197     return ret;
198 }
199
200 #define ARBITRARY_FMUL_ADD_CONST 0.005
201 static int test_vector_fmul_add(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp,
202                                 const float *v1, const float *v2, const float *v3)
203 {
204     LOCAL_ALIGNED(32, float, cdst, [LEN]);
205     LOCAL_ALIGNED(32, float, odst, [LEN]);
206     int ret;
207
208     cdsp->vector_fmul_add(cdst, v1, v2, v3, LEN);
209     fdsp->vector_fmul_add(odst, v1, v2, v3, LEN);
210
211     if (ret = compare_floats(cdst, odst, LEN, ARBITRARY_FMUL_ADD_CONST))
212         av_log(NULL, AV_LOG_ERROR, "vector_fmul_add failed\n");
213
214     return ret;
215 }
216
217 static int test_vector_fmul_reverse(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp,
218                                     const float *v1, const float *v2)
219 {
220     LOCAL_ALIGNED(32, float, cdst, [LEN]);
221     LOCAL_ALIGNED(32, float, odst, [LEN]);
222     int ret;
223
224     cdsp->vector_fmul_reverse(cdst, v1, v2, LEN);
225     fdsp->vector_fmul_reverse(odst, v1, v2, LEN);
226
227     if (ret = compare_floats(cdst, odst, LEN, FLT_EPSILON))
228         av_log(NULL, AV_LOG_ERROR, "vector_fmul_reverse failed\n");
229
230     return ret;
231 }
232
233 static int test_butterflies_float(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp,
234                                   const float *v1, const float *v2)
235 {
236     LOCAL_ALIGNED(32, float, cv1, [LEN]);
237     LOCAL_ALIGNED(32, float, cv2, [LEN]);
238     LOCAL_ALIGNED(32, float, ov1, [LEN]);
239     LOCAL_ALIGNED(32, float, ov2, [LEN]);
240     int ret;
241
242     memcpy(cv1, v1, LEN * sizeof(*v1));
243     memcpy(cv2, v2, LEN * sizeof(*v2));
244     memcpy(ov1, v1, LEN * sizeof(*v1));
245     memcpy(ov2, v2, LEN * sizeof(*v2));
246
247     cdsp->butterflies_float(cv1, cv2, LEN);
248     fdsp->butterflies_float(ov1, ov2, LEN);
249
250     if ((ret = compare_floats(cv1, ov1, LEN, FLT_EPSILON)) ||
251         (ret = compare_floats(cv2, ov2, LEN, FLT_EPSILON)))
252         av_log(NULL, AV_LOG_ERROR, "butterflies_float failed\n");
253
254     return ret;
255 }
256
257 #define ARBITRARY_SCALARPRODUCT_CONST 0.2
258 static int test_scalarproduct_float(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp,
259                                     const float *v1, const float *v2)
260 {
261     float cprod, oprod;
262     int ret;
263
264     cprod = cdsp->scalarproduct_float(v1, v2, LEN);
265     oprod = fdsp->scalarproduct_float(v1, v2, LEN);
266
267     if (ret = compare_floats(&cprod, &oprod, 1, ARBITRARY_SCALARPRODUCT_CONST))
268         av_log(NULL, AV_LOG_ERROR, "scalarproduct_float failed\n");
269
270     return ret;
271 }
272
273 int main(int argc, char **argv)
274 {
275     int ret = 0, seeded = 0;
276     uint32_t seed;
277     AVFloatDSPContext *fdsp, *cdsp;
278     AVLFG lfg;
279
280     LOCAL_ALIGNED(32, float, src0, [LEN]);
281     LOCAL_ALIGNED(32, float, src1, [LEN]);
282     LOCAL_ALIGNED(32, float, src2, [LEN]);
283     LOCAL_ALIGNED(32, double, dbl_src0, [LEN]);
284     LOCAL_ALIGNED(32, double, dbl_src1, [LEN]);
285     LOCAL_ALIGNED(32, double, dbl_src2, [LEN]);
286
287     for (;;) {
288         int arg = getopt(argc, argv, "s:c:");
289         if (arg == -1)
290             break;
291         switch (arg) {
292         case 's':
293             seed = strtoul(optarg, NULL, 10);
294             seeded = 1;
295             break;
296         case 'c':
297         {
298             int cpuflags = av_get_cpu_flags();
299
300             if (av_parse_cpu_caps(&cpuflags, optarg) < 0)
301                 return 1;
302
303             av_force_cpu_flags(cpuflags);
304             break;
305         }
306         }
307     }
308     if (!seeded)
309         seed = av_get_random_seed();
310
311     av_log(NULL, AV_LOG_INFO, "float_dsp-test: %s %u\n", seeded ? "seed" : "random seed", seed);
312
313     fdsp = avpriv_float_dsp_alloc(1);
314     av_force_cpu_flags(0);
315     cdsp = avpriv_float_dsp_alloc(1);
316
317     if (!fdsp || !cdsp) {
318         ret = 1;
319         goto end;
320     }
321
322     av_lfg_init(&lfg, seed);
323
324     fill_float_array(&lfg, src0, LEN);
325     fill_float_array(&lfg, src1, LEN);
326     fill_float_array(&lfg, src2, LEN);
327
328     fill_double_array(&lfg, dbl_src0, LEN);
329     fill_double_array(&lfg, dbl_src1, LEN);
330     fill_double_array(&lfg, dbl_src2, LEN);
331
332     if (test_vector_fmul(fdsp, cdsp, src0, src1))
333         ret -= 1 << 0;
334     if (test_vector_fmac_scalar(fdsp, cdsp, src2, src0, src1[0]))
335         ret -= 1 << 1;
336     if (test_vector_fmul_scalar(fdsp, cdsp, src0, src1[0]))
337         ret -= 1 << 2;
338     if (test_vector_fmul_window(fdsp, cdsp, src0, src1, src2))
339         ret -= 1 << 3;
340     if (test_vector_fmul_add(fdsp, cdsp, src0, src1, src2))
341         ret -= 1 << 4;
342     if (test_vector_fmul_reverse(fdsp, cdsp, src0, src1))
343         ret -= 1 << 5;
344     if (test_butterflies_float(fdsp, cdsp, src0, src1))
345         ret -= 1 << 6;
346     if (test_scalarproduct_float(fdsp, cdsp, src0, src1))
347         ret -= 1 << 7;
348     if (test_vector_dmul_scalar(fdsp, cdsp, dbl_src0, dbl_src1[0]))
349         ret -= 1 << 8;
350     if (test_vector_dmac_scalar(fdsp, cdsp, dbl_src2, dbl_src0, dbl_src1[0]))
351         ret -= 1 << 9;
352
353 end:
354     av_freep(&fdsp);
355     av_freep(&cdsp);
356     return ret;
357 }