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"
26 static void vector_fmul_c(float *dst, const float *src0, const float *src1,
30 for (i = 0; i < len; i++)
31 dst[i] = src0[i] * src1[i];
34 static void vector_fmac_scalar_c(float *dst, const float *src, float mul,
38 for (i = 0; i < len; i++)
39 dst[i] += src[i] * mul;
42 static void vector_fmul_scalar_c(float *dst, const float *src, float mul,
46 for (i = 0; i < len; i++)
47 dst[i] = src[i] * mul;
50 static void vector_dmul_scalar_c(double *dst, const double *src, double mul,
54 for (i = 0; i < len; i++)
55 dst[i] = src[i] * mul;
58 static void vector_fmul_window_c(float *dst, const float *src0,
59 const float *src1, const float *win, int len)
67 for (i = -len, j = len - 1; i < 0; i++, j--) {
72 dst[i] = s0 * wj - s1 * wi;
73 dst[j] = s0 * wi + s1 * wj;
77 static void vector_fmul_add_c(float *dst, const float *src0, const float *src1,
78 const float *src2, int len){
81 for (i = 0; i < len; i++)
82 dst[i] = src0[i] * src1[i] + src2[i];
85 static void vector_fmul_reverse_c(float *dst, const float *src0,
86 const float *src1, int len)
91 for (i = 0; i < len; i++)
92 dst[i] = src0[i] * src1[-i];
95 static void butterflies_float_c(float *av_restrict v1, float *av_restrict v2,
100 for (i = 0; i < len; i++) {
101 float t = v1[i] - v2[i];
107 float avpriv_scalarproduct_float_c(const float *v1, const float *v2, int len)
112 for (i = 0; i < len; i++)
118 av_cold void avpriv_float_dsp_init(AVFloatDSPContext *fdsp, int bit_exact)
120 fdsp->vector_fmul = vector_fmul_c;
121 fdsp->vector_fmac_scalar = vector_fmac_scalar_c;
122 fdsp->vector_fmul_scalar = vector_fmul_scalar_c;
123 fdsp->vector_dmul_scalar = vector_dmul_scalar_c;
124 fdsp->vector_fmul_window = vector_fmul_window_c;
125 fdsp->vector_fmul_add = vector_fmul_add_c;
126 fdsp->vector_fmul_reverse = vector_fmul_reverse_c;
127 fdsp->butterflies_float = butterflies_float_c;
128 fdsp->scalarproduct_float = avpriv_scalarproduct_float_c;
131 ff_float_dsp_init_aarch64(fdsp);
133 ff_float_dsp_init_arm(fdsp);
135 ff_float_dsp_init_ppc(fdsp, bit_exact);
137 ff_float_dsp_init_x86(fdsp);
139 ff_float_dsp_init_mips(fdsp);
150 #include <unistd.h> /* for getopt */
153 #include "compat/getopt.c"
158 #include "internal.h"
162 #include "random_seed.h"
166 static void fill_float_array(AVLFG *lfg, float *a, int len)
169 double bmg[2], stddev = 10.0, mean = 0.0;
171 for (i = 0; i < len; i += 2) {
172 av_bmg_get(lfg, bmg);
173 a[i] = bmg[0] * stddev + mean;
174 a[i + 1] = bmg[1] * stddev + mean;
177 static int compare_floats(const float *a, const float *b, int len,
181 for (i = 0; i < len; i++) {
182 if (fabsf(a[i] - b[i]) > max_diff) {
183 av_log(NULL, AV_LOG_ERROR, "%d: %- .12f - %- .12f = % .12g\n",
184 i, a[i], b[i], a[i] - b[i]);
191 static void fill_double_array(AVLFG *lfg, double *a, int len)
194 double bmg[2], stddev = 10.0, mean = 0.0;
196 for (i = 0; i < len; i += 2) {
197 av_bmg_get(lfg, bmg);
198 a[i] = bmg[0] * stddev + mean;
199 a[i + 1] = bmg[1] * stddev + mean;
203 static int compare_doubles(const double *a, const double *b, int len,
208 for (i = 0; i < len; i++) {
209 if (fabs(a[i] - b[i]) > max_diff) {
210 av_log(NULL, AV_LOG_ERROR, "%d: %- .12f - %- .12f = % .12g\n",
211 i, a[i], b[i], a[i] - b[i]);
218 static int test_vector_fmul(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp,
219 const float *v1, const float *v2)
221 LOCAL_ALIGNED(32, float, cdst, [LEN]);
222 LOCAL_ALIGNED(32, float, odst, [LEN]);
225 cdsp->vector_fmul(cdst, v1, v2, LEN);
226 fdsp->vector_fmul(odst, v1, v2, LEN);
228 if (ret = compare_floats(cdst, odst, LEN, FLT_EPSILON))
229 av_log(NULL, AV_LOG_ERROR, "vector_fmul failed\n");
234 #define ARBITRARY_FMAC_SCALAR_CONST 0.005
235 static int test_vector_fmac_scalar(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp,
236 const float *v1, const float *src0, float scale)
238 LOCAL_ALIGNED(32, float, cdst, [LEN]);
239 LOCAL_ALIGNED(32, float, odst, [LEN]);
242 memcpy(cdst, v1, LEN * sizeof(*v1));
243 memcpy(odst, v1, LEN * sizeof(*v1));
245 cdsp->vector_fmac_scalar(cdst, src0, scale, LEN);
246 fdsp->vector_fmac_scalar(odst, src0, scale, LEN);
248 if (ret = compare_floats(cdst, odst, LEN, ARBITRARY_FMAC_SCALAR_CONST))
249 av_log(NULL, AV_LOG_ERROR, "vector_fmac_scalar failed\n");
254 static int test_vector_fmul_scalar(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp,
255 const float *v1, float scale)
257 LOCAL_ALIGNED(32, float, cdst, [LEN]);
258 LOCAL_ALIGNED(32, float, odst, [LEN]);
261 cdsp->vector_fmul_scalar(cdst, v1, scale, LEN);
262 fdsp->vector_fmul_scalar(odst, v1, scale, LEN);
264 if (ret = compare_floats(cdst, odst, LEN, FLT_EPSILON))
265 av_log(NULL, AV_LOG_ERROR, "vector_fmul_scalar failed\n");
270 static int test_vector_dmul_scalar(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp,
271 const double *v1, double scale)
273 LOCAL_ALIGNED(32, double, cdst, [LEN]);
274 LOCAL_ALIGNED(32, double, odst, [LEN]);
277 cdsp->vector_dmul_scalar(cdst, v1, scale, LEN);
278 fdsp->vector_dmul_scalar(odst, v1, scale, LEN);
280 if (ret = compare_doubles(cdst, odst, LEN, DBL_EPSILON))
281 av_log(NULL, AV_LOG_ERROR, "vector_dmul_scalar failed\n");
286 #define ARBITRARY_FMUL_WINDOW_CONST 0.008
287 static int test_vector_fmul_window(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp,
288 const float *v1, const float *v2, const float *v3)
290 LOCAL_ALIGNED(32, float, cdst, [LEN]);
291 LOCAL_ALIGNED(32, float, odst, [LEN]);
294 cdsp->vector_fmul_window(cdst, v1, v2, v3, LEN / 2);
295 fdsp->vector_fmul_window(odst, v1, v2, v3, LEN / 2);
297 if (ret = compare_floats(cdst, odst, LEN, ARBITRARY_FMUL_WINDOW_CONST))
298 av_log(NULL, AV_LOG_ERROR, "vector_fmul_window failed\n");
303 #define ARBITRARY_FMUL_ADD_CONST 0.005
304 static int test_vector_fmul_add(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp,
305 const float *v1, const float *v2, const float *v3)
307 LOCAL_ALIGNED(32, float, cdst, [LEN]);
308 LOCAL_ALIGNED(32, float, odst, [LEN]);
311 cdsp->vector_fmul_add(cdst, v1, v2, v3, LEN);
312 fdsp->vector_fmul_add(odst, v1, v2, v3, LEN);
314 if (ret = compare_floats(cdst, odst, LEN, ARBITRARY_FMUL_ADD_CONST))
315 av_log(NULL, AV_LOG_ERROR, "vector_fmul_add failed\n");
320 static int test_vector_fmul_reverse(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp,
321 const float *v1, const float *v2)
323 LOCAL_ALIGNED(32, float, cdst, [LEN]);
324 LOCAL_ALIGNED(32, float, odst, [LEN]);
327 cdsp->vector_fmul_reverse(cdst, v1, v2, LEN);
328 fdsp->vector_fmul_reverse(odst, v1, v2, LEN);
330 if (ret = compare_floats(cdst, odst, LEN, FLT_EPSILON))
331 av_log(NULL, AV_LOG_ERROR, "vector_fmul_reverse failed\n");
336 static int test_butterflies_float(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp,
337 const float *v1, const float *v2)
339 LOCAL_ALIGNED(32, float, cv1, [LEN]);
340 LOCAL_ALIGNED(32, float, cv2, [LEN]);
341 LOCAL_ALIGNED(32, float, ov1, [LEN]);
342 LOCAL_ALIGNED(32, float, ov2, [LEN]);
345 memcpy(cv1, v1, LEN * sizeof(*v1));
346 memcpy(cv2, v2, LEN * sizeof(*v2));
347 memcpy(ov1, v1, LEN * sizeof(*v1));
348 memcpy(ov2, v2, LEN * sizeof(*v2));
350 cdsp->butterflies_float(cv1, cv2, LEN);
351 fdsp->butterflies_float(ov1, ov2, LEN);
353 if ((ret = compare_floats(cv1, ov1, LEN, FLT_EPSILON)) ||
354 (ret = compare_floats(cv2, ov2, LEN, FLT_EPSILON)))
355 av_log(NULL, AV_LOG_ERROR, "butterflies_float failed\n");
360 #define ARBITRARY_SCALARPRODUCT_CONST 0.2
361 static int test_scalarproduct_float(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp,
362 const float *v1, const float *v2)
367 cprod = cdsp->scalarproduct_float(v1, v2, LEN);
368 oprod = fdsp->scalarproduct_float(v1, v2, LEN);
370 if (ret = compare_floats(&cprod, &oprod, 1, ARBITRARY_SCALARPRODUCT_CONST))
371 av_log(NULL, AV_LOG_ERROR, "scalarproduct_float failed\n");
376 int main(int argc, char **argv)
378 int ret = 0, seeded = 0;
380 AVFloatDSPContext fdsp, cdsp;
383 LOCAL_ALIGNED(32, float, src0, [LEN]);
384 LOCAL_ALIGNED(32, float, src1, [LEN]);
385 LOCAL_ALIGNED(32, float, src2, [LEN]);
386 LOCAL_ALIGNED(32, double, dbl_src0, [LEN]);
387 LOCAL_ALIGNED(32, double, dbl_src1, [LEN]);
390 int arg = getopt(argc, argv, "s:c:");
395 seed = strtoul(optarg, NULL, 10);
400 int cpuflags = av_get_cpu_flags();
402 if (av_parse_cpu_caps(&cpuflags, optarg) < 0)
405 av_force_cpu_flags(cpuflags);
411 seed = av_get_random_seed();
413 av_log(NULL, AV_LOG_INFO, "float_dsp-test: %s %u\n", seeded ? "seed" : "random seed", seed);
415 av_lfg_init(&lfg, seed);
417 fill_float_array(&lfg, src0, LEN);
418 fill_float_array(&lfg, src1, LEN);
419 fill_float_array(&lfg, src2, LEN);
421 fill_double_array(&lfg, dbl_src0, LEN);
422 fill_double_array(&lfg, dbl_src1, LEN);
424 avpriv_float_dsp_init(&fdsp, 1);
425 av_set_cpu_flags_mask(0);
426 avpriv_float_dsp_init(&cdsp, 1);
428 if (test_vector_fmul(&fdsp, &cdsp, src0, src1))
430 if (test_vector_fmac_scalar(&fdsp, &cdsp, src2, src0, src1[0]))
432 if (test_vector_fmul_scalar(&fdsp, &cdsp, src0, src1[0]))
434 if (test_vector_fmul_window(&fdsp, &cdsp, src0, src1, src2))
436 if (test_vector_fmul_add(&fdsp, &cdsp, src0, src1, src2))
438 if (test_vector_fmul_reverse(&fdsp, &cdsp, src0, src1))
440 if (test_butterflies_float(&fdsp, &cdsp, src0, src1))
442 if (test_scalarproduct_float(&fdsp, &cdsp, src0, src1))
444 if (test_vector_dmul_scalar(&fdsp, &cdsp, dbl_src0, dbl_src1[0]))