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 static int test_vector_dmul_scalar(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp,
148 const double *v1, double scale)
150 LOCAL_ALIGNED(32, double, cdst, [LEN]);
151 LOCAL_ALIGNED(32, double, odst, [LEN]);
154 cdsp->vector_dmul_scalar(cdst, v1, scale, LEN);
155 fdsp->vector_dmul_scalar(odst, v1, scale, LEN);
157 if (ret = compare_doubles(cdst, odst, LEN, DBL_EPSILON))
158 av_log(NULL, AV_LOG_ERROR, "vector_dmul_scalar failed\n");
163 #define ARBITRARY_FMUL_WINDOW_CONST 0.008
164 static int test_vector_fmul_window(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp,
165 const float *v1, const float *v2, const float *v3)
167 LOCAL_ALIGNED(32, float, cdst, [LEN]);
168 LOCAL_ALIGNED(32, float, odst, [LEN]);
171 cdsp->vector_fmul_window(cdst, v1, v2, v3, LEN / 2);
172 fdsp->vector_fmul_window(odst, v1, v2, v3, LEN / 2);
174 if (ret = compare_floats(cdst, odst, LEN, ARBITRARY_FMUL_WINDOW_CONST))
175 av_log(NULL, AV_LOG_ERROR, "vector_fmul_window failed\n");
180 #define ARBITRARY_FMUL_ADD_CONST 0.005
181 static int test_vector_fmul_add(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp,
182 const float *v1, const float *v2, const float *v3)
184 LOCAL_ALIGNED(32, float, cdst, [LEN]);
185 LOCAL_ALIGNED(32, float, odst, [LEN]);
188 cdsp->vector_fmul_add(cdst, v1, v2, v3, LEN);
189 fdsp->vector_fmul_add(odst, v1, v2, v3, LEN);
191 if (ret = compare_floats(cdst, odst, LEN, ARBITRARY_FMUL_ADD_CONST))
192 av_log(NULL, AV_LOG_ERROR, "vector_fmul_add failed\n");
197 static int test_vector_fmul_reverse(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp,
198 const float *v1, const float *v2)
200 LOCAL_ALIGNED(32, float, cdst, [LEN]);
201 LOCAL_ALIGNED(32, float, odst, [LEN]);
204 cdsp->vector_fmul_reverse(cdst, v1, v2, LEN);
205 fdsp->vector_fmul_reverse(odst, v1, v2, LEN);
207 if (ret = compare_floats(cdst, odst, LEN, FLT_EPSILON))
208 av_log(NULL, AV_LOG_ERROR, "vector_fmul_reverse failed\n");
213 static int test_butterflies_float(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp,
214 const float *v1, const float *v2)
216 LOCAL_ALIGNED(32, float, cv1, [LEN]);
217 LOCAL_ALIGNED(32, float, cv2, [LEN]);
218 LOCAL_ALIGNED(32, float, ov1, [LEN]);
219 LOCAL_ALIGNED(32, float, ov2, [LEN]);
222 memcpy(cv1, v1, LEN * sizeof(*v1));
223 memcpy(cv2, v2, LEN * sizeof(*v2));
224 memcpy(ov1, v1, LEN * sizeof(*v1));
225 memcpy(ov2, v2, LEN * sizeof(*v2));
227 cdsp->butterflies_float(cv1, cv2, LEN);
228 fdsp->butterflies_float(ov1, ov2, LEN);
230 if ((ret = compare_floats(cv1, ov1, LEN, FLT_EPSILON)) ||
231 (ret = compare_floats(cv2, ov2, LEN, FLT_EPSILON)))
232 av_log(NULL, AV_LOG_ERROR, "butterflies_float failed\n");
237 #define ARBITRARY_SCALARPRODUCT_CONST 0.2
238 static int test_scalarproduct_float(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp,
239 const float *v1, const float *v2)
244 cprod = cdsp->scalarproduct_float(v1, v2, LEN);
245 oprod = fdsp->scalarproduct_float(v1, v2, LEN);
247 if (ret = compare_floats(&cprod, &oprod, 1, ARBITRARY_SCALARPRODUCT_CONST))
248 av_log(NULL, AV_LOG_ERROR, "scalarproduct_float failed\n");
253 int main(int argc, char **argv)
255 int ret = 0, seeded = 0;
257 AVFloatDSPContext *fdsp, *cdsp;
260 LOCAL_ALIGNED(32, float, src0, [LEN]);
261 LOCAL_ALIGNED(32, float, src1, [LEN]);
262 LOCAL_ALIGNED(32, float, src2, [LEN]);
263 LOCAL_ALIGNED(32, double, dbl_src0, [LEN]);
264 LOCAL_ALIGNED(32, double, dbl_src1, [LEN]);
267 int arg = getopt(argc, argv, "s:c:");
272 seed = strtoul(optarg, NULL, 10);
277 int cpuflags = av_get_cpu_flags();
279 if (av_parse_cpu_caps(&cpuflags, optarg) < 0)
282 av_force_cpu_flags(cpuflags);
288 seed = av_get_random_seed();
290 av_log(NULL, AV_LOG_INFO, "float_dsp-test: %s %u\n", seeded ? "seed" : "random seed", seed);
292 fdsp = avpriv_float_dsp_alloc(1);
293 av_force_cpu_flags(0);
294 cdsp = avpriv_float_dsp_alloc(1);
296 if (!fdsp || !cdsp) {
301 av_lfg_init(&lfg, seed);
303 fill_float_array(&lfg, src0, LEN);
304 fill_float_array(&lfg, src1, LEN);
305 fill_float_array(&lfg, src2, LEN);
307 fill_double_array(&lfg, dbl_src0, LEN);
308 fill_double_array(&lfg, dbl_src1, LEN);
310 if (test_vector_fmul(fdsp, cdsp, src0, src1))
312 if (test_vector_fmac_scalar(fdsp, cdsp, src2, src0, src1[0]))
314 if (test_vector_fmul_scalar(fdsp, cdsp, src0, src1[0]))
316 if (test_vector_fmul_window(fdsp, cdsp, src0, src1, src2))
318 if (test_vector_fmul_add(fdsp, cdsp, src0, src1, src2))
320 if (test_vector_fmul_reverse(fdsp, cdsp, src0, src1))
322 if (test_butterflies_float(fdsp, cdsp, src0, src1))
324 if (test_scalarproduct_float(fdsp, cdsp, src0, src1))
326 if (test_vector_dmul_scalar(fdsp, cdsp, dbl_src0, dbl_src1[0]))