2 * This file is part of FFmpeg.
4 * FFmpeg is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (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
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with FFmpeg; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include "libavutil/float_dsp.h"
25 #include "libavutil/internal.h"
26 #include "libavutil/mem_internal.h"
32 #define randomize_buffer(buf) \
35 double bmg[2], stddev = 10.0, mean = 0.0; \
37 for (i = 0; i < LEN; i += 2) { \
38 av_bmg_get(&checkasm_lfg, bmg); \
39 buf[i] = bmg[0] * stddev + mean; \
40 buf[i + 1] = bmg[1] * stddev + mean; \
44 static void test_vector_fmul(const float *src0, const float *src1)
46 LOCAL_ALIGNED_32(float, cdst, [LEN]);
47 LOCAL_ALIGNED_32(float, odst, [LEN]);
50 declare_func(void, float *dst, const float *src0, const float *src1,
53 call_ref(cdst, src0, src1, LEN);
54 call_new(odst, src0, src1, LEN);
55 for (i = 0; i < LEN; i++) {
56 double t = fabs(src0[i]) + fabs(src1[i]) + fabs(src0[i] * src1[i]) + 1.0;
57 if (!float_near_abs_eps(cdst[i], odst[i], t * 2 * FLT_EPSILON)) {
58 fprintf(stderr, "%d: %- .12f - %- .12f = % .12g\n",
59 i, cdst[i], odst[i], cdst[i] - odst[i]);
64 bench_new(odst, src0, src1, LEN);
67 static void test_vector_dmul(const double *src0, const double *src1)
69 LOCAL_ALIGNED_32(double, cdst, [LEN]);
70 LOCAL_ALIGNED_32(double, odst, [LEN]);
73 declare_func(void, double *dst, const double *src0, const double *src1,
76 call_ref(cdst, src0, src1, LEN);
77 call_new(odst, src0, src1, LEN);
78 for (i = 0; i < LEN; i++) {
79 double t = fabs(src0[i]) + fabs(src1[i]) + fabs(src0[i] * src1[i]) + 1.0;
80 if (!double_near_abs_eps(cdst[i], odst[i], t * 2 * DBL_EPSILON)) {
81 fprintf(stderr, "%d: %- .12f - %- .12f = % .12g\n",
82 i, cdst[i], odst[i], cdst[i] - odst[i]);
87 bench_new(odst, src0, src1, LEN);
90 #define ARBITRARY_FMUL_ADD_CONST 0.005
91 static void test_vector_fmul_add(const float *src0, const float *src1, const float *src2)
93 LOCAL_ALIGNED_32(float, cdst, [LEN]);
94 LOCAL_ALIGNED_32(float, odst, [LEN]);
97 declare_func(void, float *dst, const float *src0, const float *src1,
98 const float *src2, int len);
100 call_ref(cdst, src0, src1, src2, LEN);
101 call_new(odst, src0, src1, src2, LEN);
102 for (i = 0; i < LEN; i++) {
103 if (!float_near_abs_eps(cdst[i], odst[i], ARBITRARY_FMUL_ADD_CONST)) {
104 fprintf(stderr, "%d: %- .12f - %- .12f = % .12g\n",
105 i, cdst[i], odst[i], cdst[i] - odst[i]);
110 bench_new(odst, src0, src1, src2, LEN);
113 static void test_vector_fmul_scalar(const float *src0, const float *src1)
115 LOCAL_ALIGNED_16(float, cdst, [LEN]);
116 LOCAL_ALIGNED_16(float, odst, [LEN]);
119 declare_func(void, float *dst, const float *src, float mul, int len);
121 call_ref(cdst, src0, src1[0], LEN);
122 call_new(odst, src0, src1[0], LEN);
123 for (i = 0; i < LEN; i++) {
124 double t = fabs(src0[i]) + fabs(src1[0]) + fabs(src0[i] * src1[0]) + 1.0;
125 if (!float_near_abs_eps(cdst[i], odst[i], t * 2 * FLT_EPSILON)) {
126 fprintf(stderr, "%d: %- .12f - %- .12f = % .12g\n",
127 i, cdst[i], odst[i], cdst[i] - odst[i]);
132 bench_new(odst, src0, src1[0], LEN);
135 #define ARBITRARY_FMUL_WINDOW_CONST 0.008
136 static void test_vector_fmul_window(const float *src0, const float *src1, const float *win)
138 LOCAL_ALIGNED_16(float, cdst, [LEN]);
139 LOCAL_ALIGNED_16(float, odst, [LEN]);
142 declare_func(void, float *dst, const float *src0, const float *src1,
143 const float *win, int len);
145 call_ref(cdst, src0, src1, win, LEN / 2);
146 call_new(odst, src0, src1, win, LEN / 2);
147 for (i = 0; i < LEN; i++) {
148 if (!float_near_abs_eps(cdst[i], odst[i], ARBITRARY_FMUL_WINDOW_CONST)) {
149 fprintf(stderr, "%d: %- .12f - %- .12f = % .12g\n",
150 i, cdst[i], odst[i], cdst[i] - odst[i]);
155 bench_new(odst, src0, src1, win, LEN / 2);
158 #define ARBITRARY_FMAC_SCALAR_CONST 0.005
159 static void test_vector_fmac_scalar(const float *src0, const float *src1, const float *src2)
161 LOCAL_ALIGNED_32(float, cdst, [LEN]);
162 LOCAL_ALIGNED_32(float, odst, [LEN]);
165 declare_func(void, float *dst, const float *src, float mul, int len);
167 memcpy(cdst, src2, LEN * sizeof(*src2));
168 memcpy(odst, src2, LEN * sizeof(*src2));
170 call_ref(cdst, src0, src1[0], LEN);
171 call_new(odst, src0, src1[0], LEN);
172 for (i = 0; i < LEN; i++) {
173 if (!float_near_abs_eps(cdst[i], odst[i], ARBITRARY_FMAC_SCALAR_CONST)) {
174 fprintf(stderr, "%d: %- .12f - %- .12f = % .12g\n",
175 i, cdst[i], odst[i], cdst[i] - odst[i]);
180 memcpy(odst, src2, LEN * sizeof(*src2));
181 bench_new(odst, src0, src1[0], LEN);
184 static void test_vector_dmul_scalar(const double *src0, const double *src1)
186 LOCAL_ALIGNED_32(double, cdst, [LEN]);
187 LOCAL_ALIGNED_32(double, odst, [LEN]);
190 declare_func(void, double *dst, const double *src, double mul, int len);
192 call_ref(cdst, src0, src1[0], LEN);
193 call_new(odst, src0, src1[0], LEN);
194 for (i = 0; i < LEN; i++) {
195 double t = fabs(src1[0]) + fabs(src0[i]) + fabs(src1[0] * src0[i]) + 1.0;
196 if (!double_near_abs_eps(cdst[i], odst[i], t * 2 * DBL_EPSILON)) {
197 fprintf(stderr, "%d: %- .12f - %- .12f = % .12g\n", i,
198 cdst[i], odst[i], cdst[i] - odst[i]);
203 bench_new(odst, src0, src1[0], LEN);
206 #define ARBITRARY_DMAC_SCALAR_CONST 0.005
207 static void test_vector_dmac_scalar(const double *src0, const double *src1, const double *src2)
209 LOCAL_ALIGNED_32(double, cdst, [LEN]);
210 LOCAL_ALIGNED_32(double, odst, [LEN]);
213 declare_func(void, double *dst, const double *src, double mul, int len);
215 memcpy(cdst, src2, LEN * sizeof(*src2));
216 memcpy(odst, src2, LEN * sizeof(*src2));
217 call_ref(cdst, src0, src1[0], LEN);
218 call_new(odst, src0, src1[0], LEN);
219 for (i = 0; i < LEN; i++) {
220 if (!double_near_abs_eps(cdst[i], odst[i], ARBITRARY_DMAC_SCALAR_CONST)) {
221 fprintf(stderr, "%d: %- .12f - %- .12f = % .12g\n",
222 i, cdst[i], odst[i], cdst[i] - odst[i]);
227 memcpy(odst, src2, LEN * sizeof(*src2));
228 bench_new(odst, src0, src1[0], LEN);
231 static void test_butterflies_float(const float *src0, const float *src1)
233 LOCAL_ALIGNED_16(float, cdst, [LEN]);
234 LOCAL_ALIGNED_16(float, odst, [LEN]);
235 LOCAL_ALIGNED_16(float, cdst1, [LEN]);
236 LOCAL_ALIGNED_16(float, odst1, [LEN]);
239 declare_func(void, float *av_restrict src0, float *av_restrict src1,
242 memcpy(cdst, src0, LEN * sizeof(*src0));
243 memcpy(cdst1, src1, LEN * sizeof(*src1));
244 memcpy(odst, src0, LEN * sizeof(*src0));
245 memcpy(odst1, src1, LEN * sizeof(*src1));
247 call_ref(cdst, cdst1, LEN);
248 call_new(odst, odst1, LEN);
249 for (i = 0; i < LEN; i++) {
250 if (!float_near_abs_eps(cdst[i], odst[i], FLT_EPSILON) ||
251 !float_near_abs_eps(cdst1[i], odst1[i], FLT_EPSILON)) {
252 fprintf(stderr, "%d: %- .12f - %- .12f = % .12g\n",
253 i, cdst[i], odst[i], cdst[i] - odst[i]);
254 fprintf(stderr, "%d: %- .12f - %- .12f = % .12g\n",
255 i, cdst1[i], odst1[i], cdst1[i] - odst1[i]);
260 memcpy(odst, src0, LEN * sizeof(*src0));
261 memcpy(odst1, src1, LEN * sizeof(*src1));
262 bench_new(odst, odst1, LEN);
265 #define ARBITRARY_SCALARPRODUCT_CONST 0.2
266 static void test_scalarproduct_float(const float *src0, const float *src1)
270 declare_func_float(float, const float *src0, const float *src1, int len);
272 cprod = call_ref(src0, src1, LEN);
273 oprod = call_new(src0, src1, LEN);
274 if (!float_near_abs_eps(cprod, oprod, ARBITRARY_SCALARPRODUCT_CONST)) {
275 fprintf(stderr, "%- .12f - %- .12f = % .12g\n",
276 cprod, oprod, cprod - oprod);
279 bench_new(src0, src1, LEN);
282 void checkasm_check_float_dsp(void)
284 LOCAL_ALIGNED_32(float, src0, [LEN]);
285 LOCAL_ALIGNED_32(float, src1, [LEN]);
286 LOCAL_ALIGNED_32(float, src2, [LEN]);
287 LOCAL_ALIGNED_16(float, src3, [LEN]);
288 LOCAL_ALIGNED_16(float, src4, [LEN]);
289 LOCAL_ALIGNED_16(float, src5, [LEN]);
290 LOCAL_ALIGNED_32(double, dbl_src0, [LEN]);
291 LOCAL_ALIGNED_32(double, dbl_src1, [LEN]);
292 LOCAL_ALIGNED_32(double, dbl_src2, [LEN]);
293 AVFloatDSPContext *fdsp = avpriv_float_dsp_alloc(1);
296 fprintf(stderr, "floatdsp: Out of memory error\n");
300 randomize_buffer(src0);
301 randomize_buffer(src1);
302 randomize_buffer(src2);
303 randomize_buffer(src3);
304 randomize_buffer(src4);
305 randomize_buffer(src5);
306 randomize_buffer(dbl_src0);
307 randomize_buffer(dbl_src1);
308 randomize_buffer(dbl_src2);
310 if (check_func(fdsp->vector_fmul, "vector_fmul"))
311 test_vector_fmul(src0, src1);
312 if (check_func(fdsp->vector_fmul_add, "vector_fmul_add"))
313 test_vector_fmul_add(src0, src1, src2);
314 if (check_func(fdsp->vector_fmul_scalar, "vector_fmul_scalar"))
315 test_vector_fmul_scalar(src3, src4);
316 if (check_func(fdsp->vector_fmul_reverse, "vector_fmul_reverse"))
317 test_vector_fmul(src0, src1);
318 if (check_func(fdsp->vector_fmul_window, "vector_fmul_window"))
319 test_vector_fmul_window(src3, src4, src5);
320 report("vector_fmul");
321 if (check_func(fdsp->vector_fmac_scalar, "vector_fmac_scalar"))
322 test_vector_fmac_scalar(src0, src1, src2);
323 report("vector_fmac");
324 if (check_func(fdsp->vector_dmul, "vector_dmul"))
325 test_vector_dmul(dbl_src0, dbl_src1);
326 if (check_func(fdsp->vector_dmul_scalar, "vector_dmul_scalar"))
327 test_vector_dmul_scalar(dbl_src0, dbl_src1);
328 report("vector_dmul");
329 if (check_func(fdsp->vector_dmac_scalar, "vector_dmac_scalar"))
330 test_vector_dmac_scalar(dbl_src0, dbl_src1, dbl_src2);
331 report("vector_dmac");
332 if (check_func(fdsp->butterflies_float, "butterflies_float"))
333 test_butterflies_float(src3, src4);
334 report("butterflies_float");
335 if (check_func(fdsp->scalarproduct_float, "scalarproduct_float"))
336 test_scalarproduct_float(src3, src4);
337 report("scalarproduct_float");