2 * This file is part of FFmpeg.
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.
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.
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
27 #include <unistd.h> /* for getopt */
30 #include "compat/getopt.c"
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"
43 static void fill_float_array(AVLFG *lfg, float *a, int len)
46 double bmg[2], stddev = 10.0, mean = 0.0;
48 for (i = 0; i < len; i += 2) {
50 a[i] = bmg[0] * stddev + mean;
51 a[i + 1] = bmg[1] * stddev + mean;
54 static int compare_floats(const float *a, const float *b, int len,
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]);
68 static void fill_double_array(AVLFG *lfg, double *a, int len)
71 double bmg[2], stddev = 10.0, mean = 0.0;
73 for (i = 0; i < len; i += 2) {
75 a[i] = bmg[0] * stddev + mean;
76 a[i + 1] = bmg[1] * stddev + mean;
80 static int compare_doubles(const double *a, const double *b, int len,
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]);
95 static int test_vector_fmul(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp,
96 const float *v1, const float *v2)
98 LOCAL_ALIGNED(32, float, cdst, [LEN]);
99 LOCAL_ALIGNED(32, float, odst, [LEN]);
102 cdsp->vector_fmul(cdst, v1, v2, LEN);
103 fdsp->vector_fmul(odst, v1, v2, LEN);
105 if (ret = compare_floats(cdst, odst, LEN, FLT_EPSILON))
106 av_log(NULL, AV_LOG_ERROR, "vector_fmul failed\n");
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)
115 LOCAL_ALIGNED(32, float, cdst, [LEN]);
116 LOCAL_ALIGNED(32, float, odst, [LEN]);
119 memcpy(cdst, v1, LEN * sizeof(*v1));
120 memcpy(odst, v1, LEN * sizeof(*v1));
122 cdsp->vector_fmac_scalar(cdst, src0, scale, LEN);
123 fdsp->vector_fmac_scalar(odst, src0, scale, LEN);
125 if (ret = compare_floats(cdst, odst, LEN, ARBITRARY_FMAC_SCALAR_CONST))
126 av_log(NULL, AV_LOG_ERROR, "vector_fmac_scalar failed\n");
131 static int test_vector_fmul_scalar(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp,
132 const float *v1, float scale)
134 LOCAL_ALIGNED(32, float, cdst, [LEN]);
135 LOCAL_ALIGNED(32, float, odst, [LEN]);
138 cdsp->vector_fmul_scalar(cdst, v1, scale, LEN);
139 fdsp->vector_fmul_scalar(odst, v1, scale, LEN);
141 if (ret = compare_floats(cdst, odst, LEN, FLT_EPSILON))
142 av_log(NULL, AV_LOG_ERROR, "vector_fmul_scalar failed\n");
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)
151 LOCAL_ALIGNED(32, double, cdst, [LEN]);
152 LOCAL_ALIGNED(32, double, odst, [LEN]);
155 memcpy(cdst, v1, LEN * sizeof(*v1));
156 memcpy(odst, v1, LEN * sizeof(*v1));
158 cdsp->vector_dmac_scalar(cdst, src0, scale, LEN);
159 fdsp->vector_dmac_scalar(odst, src0, scale, LEN);
161 if (ret = compare_doubles(cdst, odst, LEN, ARBITRARY_DMAC_SCALAR_CONST))
162 av_log(NULL, AV_LOG_ERROR, "vector_dmac_scalar failed\n");
167 static int test_vector_dmul_scalar(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp,
168 const double *v1, double scale)
170 LOCAL_ALIGNED(32, double, cdst, [LEN]);
171 LOCAL_ALIGNED(32, double, odst, [LEN]);
174 cdsp->vector_dmul_scalar(cdst, v1, scale, LEN);
175 fdsp->vector_dmul_scalar(odst, v1, scale, LEN);
177 if (ret = compare_doubles(cdst, odst, LEN, DBL_EPSILON))
178 av_log(NULL, AV_LOG_ERROR, "vector_dmul_scalar failed\n");
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)
187 LOCAL_ALIGNED(32, float, cdst, [LEN]);
188 LOCAL_ALIGNED(32, float, odst, [LEN]);
191 cdsp->vector_fmul_window(cdst, v1, v2, v3, LEN / 2);
192 fdsp->vector_fmul_window(odst, v1, v2, v3, LEN / 2);
194 if (ret = compare_floats(cdst, odst, LEN, ARBITRARY_FMUL_WINDOW_CONST))
195 av_log(NULL, AV_LOG_ERROR, "vector_fmul_window failed\n");
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)
204 LOCAL_ALIGNED(32, float, cdst, [LEN]);
205 LOCAL_ALIGNED(32, float, odst, [LEN]);
208 cdsp->vector_fmul_add(cdst, v1, v2, v3, LEN);
209 fdsp->vector_fmul_add(odst, v1, v2, v3, LEN);
211 if (ret = compare_floats(cdst, odst, LEN, ARBITRARY_FMUL_ADD_CONST))
212 av_log(NULL, AV_LOG_ERROR, "vector_fmul_add failed\n");
217 static int test_vector_fmul_reverse(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp,
218 const float *v1, const float *v2)
220 LOCAL_ALIGNED(32, float, cdst, [LEN]);
221 LOCAL_ALIGNED(32, float, odst, [LEN]);
224 cdsp->vector_fmul_reverse(cdst, v1, v2, LEN);
225 fdsp->vector_fmul_reverse(odst, v1, v2, LEN);
227 if (ret = compare_floats(cdst, odst, LEN, FLT_EPSILON))
228 av_log(NULL, AV_LOG_ERROR, "vector_fmul_reverse failed\n");
233 static int test_butterflies_float(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp,
234 const float *v1, const float *v2)
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]);
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));
247 cdsp->butterflies_float(cv1, cv2, LEN);
248 fdsp->butterflies_float(ov1, ov2, LEN);
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");
257 #define ARBITRARY_SCALARPRODUCT_CONST 0.2
258 static int test_scalarproduct_float(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp,
259 const float *v1, const float *v2)
264 cprod = cdsp->scalarproduct_float(v1, v2, LEN);
265 oprod = fdsp->scalarproduct_float(v1, v2, LEN);
267 if (ret = compare_floats(&cprod, &oprod, 1, ARBITRARY_SCALARPRODUCT_CONST))
268 av_log(NULL, AV_LOG_ERROR, "scalarproduct_float failed\n");
273 int main(int argc, char **argv)
275 int ret = 0, seeded = 0;
277 AVFloatDSPContext *fdsp, *cdsp;
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]);
288 int arg = getopt(argc, argv, "s:c:");
293 seed = strtoul(optarg, NULL, 10);
298 int cpuflags = av_get_cpu_flags();
300 if (av_parse_cpu_caps(&cpuflags, optarg) < 0)
303 av_force_cpu_flags(cpuflags);
309 seed = av_get_random_seed();
311 av_log(NULL, AV_LOG_INFO, "float_dsp-test: %s %u\n", seeded ? "seed" : "random seed", seed);
313 fdsp = avpriv_float_dsp_alloc(1);
314 av_force_cpu_flags(0);
315 cdsp = avpriv_float_dsp_alloc(1);
317 if (!fdsp || !cdsp) {
322 av_lfg_init(&lfg, seed);
324 fill_float_array(&lfg, src0, LEN);
325 fill_float_array(&lfg, src1, LEN);
326 fill_float_array(&lfg, src2, LEN);
328 fill_double_array(&lfg, dbl_src0, LEN);
329 fill_double_array(&lfg, dbl_src1, LEN);
330 fill_double_array(&lfg, dbl_src2, LEN);
332 if (test_vector_fmul(fdsp, cdsp, src0, src1))
334 if (test_vector_fmac_scalar(fdsp, cdsp, src2, src0, src1[0]))
336 if (test_vector_fmul_scalar(fdsp, cdsp, src0, src1[0]))
338 if (test_vector_fmul_window(fdsp, cdsp, src0, src1, src2))
340 if (test_vector_fmul_add(fdsp, cdsp, src0, src1, src2))
342 if (test_vector_fmul_reverse(fdsp, cdsp, src0, src1))
344 if (test_butterflies_float(fdsp, cdsp, src0, src1))
346 if (test_scalarproduct_float(fdsp, cdsp, src0, src1))
348 if (test_vector_dmul_scalar(fdsp, cdsp, dbl_src0, dbl_src1[0]))
350 if (test_vector_dmac_scalar(fdsp, cdsp, dbl_src2, dbl_src0, dbl_src1[0]))