2 * This file is part of Libav.
4 * Libav 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 * Libav 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 Libav; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 #include "attributes.h"
21 #include "float_dsp.h"
23 static void vector_fmul_c(float *dst, const float *src0, const float *src1,
27 for (i = 0; i < len; i++)
28 dst[i] = src0[i] * src1[i];
31 static void vector_fmac_scalar_c(float *dst, const float *src, float mul,
35 for (i = 0; i < len; i++)
36 dst[i] += src[i] * mul;
39 static void vector_fmul_scalar_c(float *dst, const float *src, float mul,
43 for (i = 0; i < len; i++)
44 dst[i] = src[i] * mul;
47 static void vector_dmul_scalar_c(double *dst, const double *src, double mul,
51 for (i = 0; i < len; i++)
52 dst[i] = src[i] * mul;
55 static void vector_fmul_window_c(float *dst, const float *src0,
56 const float *src1, const float *win, int len)
64 for (i = -len, j = len - 1; i < 0; i++, j--) {
69 dst[i] = s0 * wj - s1 * wi;
70 dst[j] = s0 * wi + s1 * wj;
74 static void vector_fmul_add_c(float *dst, const float *src0, const float *src1,
75 const float *src2, int len){
78 for (i = 0; i < len; i++)
79 dst[i] = src0[i] * src1[i] + src2[i];
82 static void vector_fmul_reverse_c(float *dst, const float *src0,
83 const float *src1, int len)
88 for (i = 0; i < len; i++)
89 dst[i] = src0[i] * src1[-i];
92 static void butterflies_float_c(float *restrict v1, float *restrict v2,
97 for (i = 0; i < len; i++) {
98 float t = v1[i] - v2[i];
104 float avpriv_scalarproduct_float_c(const float *v1, const float *v2, int len)
109 for (i = 0; i < len; i++)
115 av_cold void avpriv_float_dsp_init(AVFloatDSPContext *fdsp, int bit_exact)
117 fdsp->vector_fmul = vector_fmul_c;
118 fdsp->vector_fmac_scalar = vector_fmac_scalar_c;
119 fdsp->vector_fmul_scalar = vector_fmul_scalar_c;
120 fdsp->vector_dmul_scalar = vector_dmul_scalar_c;
121 fdsp->vector_fmul_window = vector_fmul_window_c;
122 fdsp->vector_fmul_add = vector_fmul_add_c;
123 fdsp->vector_fmul_reverse = vector_fmul_reverse_c;
124 fdsp->butterflies_float = butterflies_float_c;
125 fdsp->scalarproduct_float = avpriv_scalarproduct_float_c;
128 ff_float_dsp_init_aarch64(fdsp);
130 ff_float_dsp_init_arm(fdsp);
132 ff_float_dsp_init_ppc(fdsp, bit_exact);
134 ff_float_dsp_init_x86(fdsp);
147 #include "internal.h"
151 #include "random_seed.h"
155 static void fill_float_array(AVLFG *lfg, float *a, int len)
158 double bmg[2], stddev = 10.0, mean = 0.0;
160 for (i = 0; i < len; i += 2) {
161 av_bmg_get(lfg, bmg);
162 a[i] = bmg[0] * stddev + mean;
163 a[i + 1] = bmg[1] * stddev + mean;
166 static int compare_floats(const float *a, const float *b, int len,
170 for (i = 0; i < len; i++) {
171 if (fabsf(a[i] - b[i]) > max_diff) {
172 av_log(NULL, AV_LOG_ERROR, "%d: %- .12f - %- .12f = % .12g\n",
173 i, a[i], b[i], a[i] - b[i]);
180 static void fill_double_array(AVLFG *lfg, double *a, int len)
183 double bmg[2], stddev = 10.0, mean = 0.0;
185 for (i = 0; i < len; i += 2) {
186 av_bmg_get(lfg, bmg);
187 a[i] = bmg[0] * stddev + mean;
188 a[i + 1] = bmg[1] * stddev + mean;
192 static int compare_doubles(const double *a, const double *b, int len,
197 for (i = 0; i < len; i++) {
198 if (fabs(a[i] - b[i]) > max_diff) {
199 av_log(NULL, AV_LOG_ERROR, "%d: %- .12f - %- .12f = % .12g\n",
200 i, a[i], b[i], a[i] - b[i]);
207 static int test_vector_fmul(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp,
208 const float *v1, const float *v2)
210 LOCAL_ALIGNED(32, float, cdst, [LEN]);
211 LOCAL_ALIGNED(32, float, odst, [LEN]);
214 cdsp->vector_fmul(cdst, v1, v2, LEN);
215 fdsp->vector_fmul(odst, v1, v2, LEN);
217 if (ret = compare_floats(cdst, odst, LEN, FLT_EPSILON))
218 av_log(NULL, AV_LOG_ERROR, "vector_fmul failed\n");
223 #define ARBITRARY_FMAC_SCALAR_CONST 0.005
224 static int test_vector_fmac_scalar(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp,
225 const float *v1, const float *src0, float scale)
227 LOCAL_ALIGNED(32, float, cdst, [LEN]);
228 LOCAL_ALIGNED(32, float, odst, [LEN]);
231 memcpy(cdst, v1, LEN * sizeof(*v1));
232 memcpy(odst, v1, LEN * sizeof(*v1));
234 cdsp->vector_fmac_scalar(cdst, src0, scale, LEN);
235 fdsp->vector_fmac_scalar(odst, src0, scale, LEN);
237 if (ret = compare_floats(cdst, odst, LEN, ARBITRARY_FMAC_SCALAR_CONST))
238 av_log(NULL, AV_LOG_ERROR, "vector_fmac_scalar failed\n");
243 static int test_vector_fmul_scalar(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp,
244 const float *v1, float scale)
246 LOCAL_ALIGNED(32, float, cdst, [LEN]);
247 LOCAL_ALIGNED(32, float, odst, [LEN]);
250 cdsp->vector_fmul_scalar(cdst, v1, scale, LEN);
251 fdsp->vector_fmul_scalar(odst, v1, scale, LEN);
253 if (ret = compare_floats(cdst, odst, LEN, FLT_EPSILON))
254 av_log(NULL, AV_LOG_ERROR, "vector_fmul_scalar failed\n");
259 static int test_vector_dmul_scalar(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp,
260 const double *v1, double scale)
262 LOCAL_ALIGNED(32, double, cdst, [LEN]);
263 LOCAL_ALIGNED(32, double, odst, [LEN]);
266 cdsp->vector_dmul_scalar(cdst, v1, scale, LEN);
267 fdsp->vector_dmul_scalar(odst, v1, scale, LEN);
269 if (ret = compare_doubles(cdst, odst, LEN, DBL_EPSILON))
270 av_log(NULL, AV_LOG_ERROR, "vector_dmul_scalar failed\n");
275 #define ARBITRARY_FMUL_WINDOW_CONST 0.008
276 static int test_vector_fmul_window(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp,
277 const float *v1, const float *v2, const float *v3)
279 LOCAL_ALIGNED(32, float, cdst, [LEN]);
280 LOCAL_ALIGNED(32, float, odst, [LEN]);
283 cdsp->vector_fmul_window(cdst, v1, v2, v3, LEN / 2);
284 fdsp->vector_fmul_window(odst, v1, v2, v3, LEN / 2);
286 if (ret = compare_floats(cdst, odst, LEN, ARBITRARY_FMUL_WINDOW_CONST))
287 av_log(NULL, AV_LOG_ERROR, "vector_fmul_window failed\n");
292 #define ARBITRARY_FMUL_ADD_CONST 0.005
293 static int test_vector_fmul_add(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_add(cdst, v1, v2, v3, LEN);
301 fdsp->vector_fmul_add(odst, v1, v2, v3, LEN);
303 if (ret = compare_floats(cdst, odst, LEN, ARBITRARY_FMUL_ADD_CONST))
304 av_log(NULL, AV_LOG_ERROR, "vector_fmul_add failed\n");
309 static int test_vector_fmul_reverse(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp,
310 const float *v1, const float *v2)
312 LOCAL_ALIGNED(32, float, cdst, [LEN]);
313 LOCAL_ALIGNED(32, float, odst, [LEN]);
316 cdsp->vector_fmul_reverse(cdst, v1, v2, LEN);
317 fdsp->vector_fmul_reverse(odst, v1, v2, LEN);
319 if (ret = compare_floats(cdst, odst, LEN, FLT_EPSILON))
320 av_log(NULL, AV_LOG_ERROR, "vector_fmul_reverse failed\n");
325 static int test_butterflies_float(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp,
326 const float *v1, const float *v2)
328 LOCAL_ALIGNED(32, float, cv1, [LEN]);
329 LOCAL_ALIGNED(32, float, cv2, [LEN]);
330 LOCAL_ALIGNED(32, float, ov1, [LEN]);
331 LOCAL_ALIGNED(32, float, ov2, [LEN]);
334 memcpy(cv1, v1, LEN * sizeof(*v1));
335 memcpy(cv2, v2, LEN * sizeof(*v2));
336 memcpy(ov1, v1, LEN * sizeof(*v1));
337 memcpy(ov2, v2, LEN * sizeof(*v2));
339 cdsp->butterflies_float(cv1, cv2, LEN);
340 fdsp->butterflies_float(ov1, ov2, LEN);
342 if ((ret = compare_floats(cv1, ov1, LEN, FLT_EPSILON)) ||
343 (ret = compare_floats(cv2, ov2, LEN, FLT_EPSILON)))
344 av_log(NULL, AV_LOG_ERROR, "butterflies_float failed\n");
349 #define ARBITRARY_SCALARPRODUCT_CONST 0.2
350 static int test_scalarproduct_float(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp,
351 const float *v1, const float *v2)
356 cprod = cdsp->scalarproduct_float(v1, v2, LEN);
357 oprod = fdsp->scalarproduct_float(v1, v2, LEN);
359 if (ret = compare_floats(&cprod, &oprod, 1, ARBITRARY_SCALARPRODUCT_CONST))
360 av_log(NULL, AV_LOG_ERROR, "scalarproduct_float failed\n");
365 int main(int argc, char **argv)
369 AVFloatDSPContext fdsp, cdsp;
372 LOCAL_ALIGNED(32, float, src0, [LEN]);
373 LOCAL_ALIGNED(32, float, src1, [LEN]);
374 LOCAL_ALIGNED(32, float, src2, [LEN]);
375 LOCAL_ALIGNED(32, double, dbl_src0, [LEN]);
376 LOCAL_ALIGNED(32, double, dbl_src1, [LEN]);
378 if (argc > 2 && !strcmp(argv[1], "-s"))
379 seed = strtoul(argv[2], NULL, 10);
381 seed = av_get_random_seed();
383 av_log(NULL, AV_LOG_INFO, "float_dsp-test: random seed %u\n", seed);
385 av_lfg_init(&lfg, seed);
387 fill_float_array(&lfg, src0, LEN);
388 fill_float_array(&lfg, src1, LEN);
389 fill_float_array(&lfg, src2, LEN);
391 fill_double_array(&lfg, dbl_src0, LEN);
392 fill_double_array(&lfg, dbl_src1, LEN);
394 avpriv_float_dsp_init(&fdsp, 1);
395 av_set_cpu_flags_mask(0);
396 avpriv_float_dsp_init(&cdsp, 1);
398 if (test_vector_fmul(&fdsp, &cdsp, src0, src1))
400 if (test_vector_fmac_scalar(&fdsp, &cdsp, src2, src0, src1[0]))
402 if (test_vector_fmul_scalar(&fdsp, &cdsp, src0, src1[0]))
404 if (test_vector_fmul_window(&fdsp, &cdsp, src0, src1, src2))
406 if (test_vector_fmul_add(&fdsp, &cdsp, src0, src1, src2))
408 if (test_vector_fmul_reverse(&fdsp, &cdsp, src0, src1))
410 if (test_butterflies_float(&fdsp, &cdsp, src0, src1))
412 if (test_scalarproduct_float(&fdsp, &cdsp, src0, src1))
414 if (test_vector_dmul_scalar(&fdsp, &cdsp, dbl_src0, dbl_src1[0]))