2 * Copyright 2005 Balatoni Denes
3 * Copyright 2006 Loren Merritt
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 #include "attributes.h"
24 #include "float_dsp.h"
27 static void vector_fmul_c(float *dst, const float *src0, const float *src1,
31 for (i = 0; i < len; i++)
32 dst[i] = src0[i] * src1[i];
35 static void vector_fmac_scalar_c(float *dst, const float *src, float mul,
39 for (i = 0; i < len; i++)
40 dst[i] += src[i] * mul;
43 static void vector_fmul_scalar_c(float *dst, const float *src, float mul,
47 for (i = 0; i < len; i++)
48 dst[i] = src[i] * mul;
51 static void vector_dmul_scalar_c(double *dst, const double *src, double mul,
55 for (i = 0; i < len; i++)
56 dst[i] = src[i] * mul;
59 static void vector_fmul_window_c(float *dst, const float *src0,
60 const float *src1, const float *win, int len)
68 for (i = -len, j = len - 1; i < 0; i++, j--) {
73 dst[i] = s0 * wj - s1 * wi;
74 dst[j] = s0 * wi + s1 * wj;
78 static void vector_fmul_add_c(float *dst, const float *src0, const float *src1,
79 const float *src2, int len){
82 for (i = 0; i < len; i++)
83 dst[i] = src0[i] * src1[i] + src2[i];
86 static void vector_fmul_reverse_c(float *dst, const float *src0,
87 const float *src1, int len)
92 for (i = 0; i < len; i++)
93 dst[i] = src0[i] * src1[-i];
96 static void butterflies_float_c(float *av_restrict v1, float *av_restrict v2,
101 for (i = 0; i < len; i++) {
102 float t = v1[i] - v2[i];
108 float avpriv_scalarproduct_float_c(const float *v1, const float *v2, int len)
113 for (i = 0; i < len; i++)
119 av_cold AVFloatDSPContext *avpriv_float_dsp_alloc(int bit_exact)
121 AVFloatDSPContext *fdsp = av_mallocz(sizeof(AVFloatDSPContext));
125 fdsp->vector_fmul = vector_fmul_c;
126 fdsp->vector_fmac_scalar = vector_fmac_scalar_c;
127 fdsp->vector_fmul_scalar = vector_fmul_scalar_c;
128 fdsp->vector_dmul_scalar = vector_dmul_scalar_c;
129 fdsp->vector_fmul_window = vector_fmul_window_c;
130 fdsp->vector_fmul_add = vector_fmul_add_c;
131 fdsp->vector_fmul_reverse = vector_fmul_reverse_c;
132 fdsp->butterflies_float = butterflies_float_c;
133 fdsp->scalarproduct_float = avpriv_scalarproduct_float_c;
136 ff_float_dsp_init_aarch64(fdsp);
138 ff_float_dsp_init_arm(fdsp);
140 ff_float_dsp_init_ppc(fdsp, bit_exact);
142 ff_float_dsp_init_x86(fdsp);
144 ff_float_dsp_init_mips(fdsp);
157 #include <unistd.h> /* for getopt */
160 #include "compat/getopt.c"
165 #include "internal.h"
168 #include "random_seed.h"
172 static void fill_float_array(AVLFG *lfg, float *a, int len)
175 double bmg[2], stddev = 10.0, mean = 0.0;
177 for (i = 0; i < len; i += 2) {
178 av_bmg_get(lfg, bmg);
179 a[i] = bmg[0] * stddev + mean;
180 a[i + 1] = bmg[1] * stddev + mean;
183 static int compare_floats(const float *a, const float *b, int len,
187 for (i = 0; i < len; i++) {
188 if (fabsf(a[i] - b[i]) > max_diff) {
189 av_log(NULL, AV_LOG_ERROR, "%d: %- .12f - %- .12f = % .12g\n",
190 i, a[i], b[i], a[i] - b[i]);
197 static void fill_double_array(AVLFG *lfg, double *a, int len)
200 double bmg[2], stddev = 10.0, mean = 0.0;
202 for (i = 0; i < len; i += 2) {
203 av_bmg_get(lfg, bmg);
204 a[i] = bmg[0] * stddev + mean;
205 a[i + 1] = bmg[1] * stddev + mean;
209 static int compare_doubles(const double *a, const double *b, int len,
214 for (i = 0; i < len; i++) {
215 if (fabs(a[i] - b[i]) > max_diff) {
216 av_log(NULL, AV_LOG_ERROR, "%d: %- .12f - %- .12f = % .12g\n",
217 i, a[i], b[i], a[i] - b[i]);
224 static int test_vector_fmul(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp,
225 const float *v1, const float *v2)
227 LOCAL_ALIGNED(32, float, cdst, [LEN]);
228 LOCAL_ALIGNED(32, float, odst, [LEN]);
231 cdsp->vector_fmul(cdst, v1, v2, LEN);
232 fdsp->vector_fmul(odst, v1, v2, LEN);
234 if (ret = compare_floats(cdst, odst, LEN, FLT_EPSILON))
235 av_log(NULL, AV_LOG_ERROR, "vector_fmul failed\n");
240 #define ARBITRARY_FMAC_SCALAR_CONST 0.005
241 static int test_vector_fmac_scalar(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp,
242 const float *v1, const float *src0, float scale)
244 LOCAL_ALIGNED(32, float, cdst, [LEN]);
245 LOCAL_ALIGNED(32, float, odst, [LEN]);
248 memcpy(cdst, v1, LEN * sizeof(*v1));
249 memcpy(odst, v1, LEN * sizeof(*v1));
251 cdsp->vector_fmac_scalar(cdst, src0, scale, LEN);
252 fdsp->vector_fmac_scalar(odst, src0, scale, LEN);
254 if (ret = compare_floats(cdst, odst, LEN, ARBITRARY_FMAC_SCALAR_CONST))
255 av_log(NULL, AV_LOG_ERROR, "vector_fmac_scalar failed\n");
260 static int test_vector_fmul_scalar(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp,
261 const float *v1, float scale)
263 LOCAL_ALIGNED(32, float, cdst, [LEN]);
264 LOCAL_ALIGNED(32, float, odst, [LEN]);
267 cdsp->vector_fmul_scalar(cdst, v1, scale, LEN);
268 fdsp->vector_fmul_scalar(odst, v1, scale, LEN);
270 if (ret = compare_floats(cdst, odst, LEN, FLT_EPSILON))
271 av_log(NULL, AV_LOG_ERROR, "vector_fmul_scalar failed\n");
276 static int test_vector_dmul_scalar(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp,
277 const double *v1, double scale)
279 LOCAL_ALIGNED(32, double, cdst, [LEN]);
280 LOCAL_ALIGNED(32, double, odst, [LEN]);
283 cdsp->vector_dmul_scalar(cdst, v1, scale, LEN);
284 fdsp->vector_dmul_scalar(odst, v1, scale, LEN);
286 if (ret = compare_doubles(cdst, odst, LEN, DBL_EPSILON))
287 av_log(NULL, AV_LOG_ERROR, "vector_dmul_scalar failed\n");
292 #define ARBITRARY_FMUL_WINDOW_CONST 0.008
293 static int test_vector_fmul_window(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp,
294 const float *v1, const float *v2, const float *v3)
296 LOCAL_ALIGNED(32, float, cdst, [LEN]);
297 LOCAL_ALIGNED(32, float, odst, [LEN]);
300 cdsp->vector_fmul_window(cdst, v1, v2, v3, LEN / 2);
301 fdsp->vector_fmul_window(odst, v1, v2, v3, LEN / 2);
303 if (ret = compare_floats(cdst, odst, LEN, ARBITRARY_FMUL_WINDOW_CONST))
304 av_log(NULL, AV_LOG_ERROR, "vector_fmul_window failed\n");
309 #define ARBITRARY_FMUL_ADD_CONST 0.005
310 static int test_vector_fmul_add(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp,
311 const float *v1, const float *v2, const float *v3)
313 LOCAL_ALIGNED(32, float, cdst, [LEN]);
314 LOCAL_ALIGNED(32, float, odst, [LEN]);
317 cdsp->vector_fmul_add(cdst, v1, v2, v3, LEN);
318 fdsp->vector_fmul_add(odst, v1, v2, v3, LEN);
320 if (ret = compare_floats(cdst, odst, LEN, ARBITRARY_FMUL_ADD_CONST))
321 av_log(NULL, AV_LOG_ERROR, "vector_fmul_add failed\n");
326 static int test_vector_fmul_reverse(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp,
327 const float *v1, const float *v2)
329 LOCAL_ALIGNED(32, float, cdst, [LEN]);
330 LOCAL_ALIGNED(32, float, odst, [LEN]);
333 cdsp->vector_fmul_reverse(cdst, v1, v2, LEN);
334 fdsp->vector_fmul_reverse(odst, v1, v2, LEN);
336 if (ret = compare_floats(cdst, odst, LEN, FLT_EPSILON))
337 av_log(NULL, AV_LOG_ERROR, "vector_fmul_reverse failed\n");
342 static int test_butterflies_float(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp,
343 const float *v1, const float *v2)
345 LOCAL_ALIGNED(32, float, cv1, [LEN]);
346 LOCAL_ALIGNED(32, float, cv2, [LEN]);
347 LOCAL_ALIGNED(32, float, ov1, [LEN]);
348 LOCAL_ALIGNED(32, float, ov2, [LEN]);
351 memcpy(cv1, v1, LEN * sizeof(*v1));
352 memcpy(cv2, v2, LEN * sizeof(*v2));
353 memcpy(ov1, v1, LEN * sizeof(*v1));
354 memcpy(ov2, v2, LEN * sizeof(*v2));
356 cdsp->butterflies_float(cv1, cv2, LEN);
357 fdsp->butterflies_float(ov1, ov2, LEN);
359 if ((ret = compare_floats(cv1, ov1, LEN, FLT_EPSILON)) ||
360 (ret = compare_floats(cv2, ov2, LEN, FLT_EPSILON)))
361 av_log(NULL, AV_LOG_ERROR, "butterflies_float failed\n");
366 #define ARBITRARY_SCALARPRODUCT_CONST 0.2
367 static int test_scalarproduct_float(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp,
368 const float *v1, const float *v2)
373 cprod = cdsp->scalarproduct_float(v1, v2, LEN);
374 oprod = fdsp->scalarproduct_float(v1, v2, LEN);
376 if (ret = compare_floats(&cprod, &oprod, 1, ARBITRARY_SCALARPRODUCT_CONST))
377 av_log(NULL, AV_LOG_ERROR, "scalarproduct_float failed\n");
382 int main(int argc, char **argv)
384 int ret = 0, seeded = 0;
386 AVFloatDSPContext *fdsp, *cdsp;
389 LOCAL_ALIGNED(32, float, src0, [LEN]);
390 LOCAL_ALIGNED(32, float, src1, [LEN]);
391 LOCAL_ALIGNED(32, float, src2, [LEN]);
392 LOCAL_ALIGNED(32, double, dbl_src0, [LEN]);
393 LOCAL_ALIGNED(32, double, dbl_src1, [LEN]);
396 int arg = getopt(argc, argv, "s:c:");
401 seed = strtoul(optarg, NULL, 10);
406 int cpuflags = av_get_cpu_flags();
408 if (av_parse_cpu_caps(&cpuflags, optarg) < 0)
411 av_force_cpu_flags(cpuflags);
417 seed = av_get_random_seed();
419 av_log(NULL, AV_LOG_INFO, "float_dsp-test: %s %u\n", seeded ? "seed" : "random seed", seed);
421 fdsp = avpriv_float_dsp_alloc(1);
422 av_force_cpu_flags(0);
423 cdsp = avpriv_float_dsp_alloc(1);
425 if (!fdsp || !cdsp) {
430 av_lfg_init(&lfg, seed);
432 fill_float_array(&lfg, src0, LEN);
433 fill_float_array(&lfg, src1, LEN);
434 fill_float_array(&lfg, src2, LEN);
436 fill_double_array(&lfg, dbl_src0, LEN);
437 fill_double_array(&lfg, dbl_src1, LEN);
439 if (test_vector_fmul(fdsp, cdsp, src0, src1))
441 if (test_vector_fmac_scalar(fdsp, cdsp, src2, src0, src1[0]))
443 if (test_vector_fmul_scalar(fdsp, cdsp, src0, src1[0]))
445 if (test_vector_fmul_window(fdsp, cdsp, src0, src1, src2))
447 if (test_vector_fmul_add(fdsp, cdsp, src0, src1, src2))
449 if (test_vector_fmul_reverse(fdsp, cdsp, src0, src1))
451 if (test_butterflies_float(fdsp, cdsp, src0, src1))
453 if (test_scalarproduct_float(fdsp, cdsp, src0, src1))
455 if (test_vector_dmul_scalar(fdsp, cdsp, dbl_src0, dbl_src1[0]))