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
28 #include "random_seed.h"
29 #include "float_dsp.h"
33 static void fill_float_array(AVLFG *lfg, float *a, int len)
36 double bmg[2], stddev = 10.0, mean = 0.0;
38 for (i = 0; i < len; i += 2) {
40 a[i] = bmg[0] * stddev + mean;
41 a[i + 1] = bmg[1] * stddev + mean;
44 static int compare_floats(const float *a, const float *b, int len,
48 for (i = 0; i < len; i++) {
49 if (fabsf(a[i] - b[i]) > max_diff) {
50 av_log(NULL, AV_LOG_ERROR, "%d: %- .12f - %- .12f = % .12g\n",
51 i, a[i], b[i], a[i] - b[i]);
58 static void fill_double_array(AVLFG *lfg, double *a, int len)
61 double bmg[2], stddev = 10.0, mean = 0.0;
63 for (i = 0; i < len; i += 2) {
65 a[i] = bmg[0] * stddev + mean;
66 a[i + 1] = bmg[1] * stddev + mean;
70 static int compare_doubles(const double *a, const double *b, int len,
75 for (i = 0; i < len; i++) {
76 if (fabs(a[i] - b[i]) > max_diff) {
77 av_log(NULL, AV_LOG_ERROR, "%d: %- .12f - %- .12f = % .12g\n",
78 i, a[i], b[i], a[i] - b[i]);
85 static int test_vector_fmul(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp,
86 const float *v1, const float *v2)
88 LOCAL_ALIGNED(32, float, cdst, [LEN]);
89 LOCAL_ALIGNED(32, float, odst, [LEN]);
92 cdsp->vector_fmul(cdst, v1, v2, LEN);
93 fdsp->vector_fmul(odst, v1, v2, LEN);
95 if (ret = compare_floats(cdst, odst, LEN, FLT_EPSILON))
96 av_log(NULL, AV_LOG_ERROR, "vector_fmul failed\n");
101 #define ARBITRARY_FMAC_SCALAR_CONST 0.005
102 static int test_vector_fmac_scalar(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp,
103 const float *v1, const float *src0, float scale)
105 LOCAL_ALIGNED(32, float, cdst, [LEN]);
106 LOCAL_ALIGNED(32, float, odst, [LEN]);
109 memcpy(cdst, v1, LEN * sizeof(*v1));
110 memcpy(odst, v1, LEN * sizeof(*v1));
112 cdsp->vector_fmac_scalar(cdst, src0, scale, LEN);
113 fdsp->vector_fmac_scalar(odst, src0, scale, LEN);
115 if (ret = compare_floats(cdst, odst, LEN, ARBITRARY_FMAC_SCALAR_CONST))
116 av_log(NULL, AV_LOG_ERROR, "vector_fmac_scalar failed\n");
121 static int test_vector_fmul_scalar(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp,
122 const float *v1, float scale)
124 LOCAL_ALIGNED(32, float, cdst, [LEN]);
125 LOCAL_ALIGNED(32, float, odst, [LEN]);
128 cdsp->vector_fmul_scalar(cdst, v1, scale, LEN);
129 fdsp->vector_fmul_scalar(odst, v1, scale, LEN);
131 if (ret = compare_floats(cdst, odst, LEN, FLT_EPSILON))
132 av_log(NULL, AV_LOG_ERROR, "vector_fmul_scalar failed\n");
137 static int test_vector_dmul_scalar(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp,
138 const double *v1, double scale)
140 LOCAL_ALIGNED(32, double, cdst, [LEN]);
141 LOCAL_ALIGNED(32, double, odst, [LEN]);
144 cdsp->vector_dmul_scalar(cdst, v1, scale, LEN);
145 fdsp->vector_dmul_scalar(odst, v1, scale, LEN);
147 if (ret = compare_doubles(cdst, odst, LEN, DBL_EPSILON))
148 av_log(NULL, AV_LOG_ERROR, "vector_dmul_scalar failed\n");
153 #define ARBITRARY_FMUL_WINDOW_CONST 0.008
154 static int test_vector_fmul_window(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp,
155 const float *v1, const float *v2, const float *v3)
157 LOCAL_ALIGNED(32, float, cdst, [LEN]);
158 LOCAL_ALIGNED(32, float, odst, [LEN]);
161 cdsp->vector_fmul_window(cdst, v1, v2, v3, LEN / 2);
162 fdsp->vector_fmul_window(odst, v1, v2, v3, LEN / 2);
164 if (ret = compare_floats(cdst, odst, LEN, ARBITRARY_FMUL_WINDOW_CONST))
165 av_log(NULL, AV_LOG_ERROR, "vector_fmul_window failed\n");
170 #define ARBITRARY_FMUL_ADD_CONST 0.005
171 static int test_vector_fmul_add(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp,
172 const float *v1, const float *v2, const float *v3)
174 LOCAL_ALIGNED(32, float, cdst, [LEN]);
175 LOCAL_ALIGNED(32, float, odst, [LEN]);
178 cdsp->vector_fmul_add(cdst, v1, v2, v3, LEN);
179 fdsp->vector_fmul_add(odst, v1, v2, v3, LEN);
181 if (ret = compare_floats(cdst, odst, LEN, ARBITRARY_FMUL_ADD_CONST))
182 av_log(NULL, AV_LOG_ERROR, "vector_fmul_add failed\n");
187 static int test_vector_fmul_reverse(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp,
188 const float *v1, const float *v2)
190 LOCAL_ALIGNED(32, float, cdst, [LEN]);
191 LOCAL_ALIGNED(32, float, odst, [LEN]);
194 cdsp->vector_fmul_reverse(cdst, v1, v2, LEN);
195 fdsp->vector_fmul_reverse(odst, v1, v2, LEN);
197 if (ret = compare_floats(cdst, odst, LEN, FLT_EPSILON))
198 av_log(NULL, AV_LOG_ERROR, "vector_fmul_reverse failed\n");
203 static int test_butterflies_float(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp,
204 const float *v1, const float *v2)
206 LOCAL_ALIGNED(32, float, cv1, [LEN]);
207 LOCAL_ALIGNED(32, float, cv2, [LEN]);
208 LOCAL_ALIGNED(32, float, ov1, [LEN]);
209 LOCAL_ALIGNED(32, float, ov2, [LEN]);
212 memcpy(cv1, v1, LEN * sizeof(*v1));
213 memcpy(cv2, v2, LEN * sizeof(*v2));
214 memcpy(ov1, v1, LEN * sizeof(*v1));
215 memcpy(ov2, v2, LEN * sizeof(*v2));
217 cdsp->butterflies_float(cv1, cv2, LEN);
218 fdsp->butterflies_float(ov1, ov2, LEN);
220 if ((ret = compare_floats(cv1, ov1, LEN, FLT_EPSILON)) ||
221 (ret = compare_floats(cv2, ov2, LEN, FLT_EPSILON)))
222 av_log(NULL, AV_LOG_ERROR, "butterflies_float failed\n");
227 #define ARBITRARY_SCALARPRODUCT_CONST 0.2
228 static int test_scalarproduct_float(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp,
229 const float *v1, const float *v2)
234 cprod = cdsp->scalarproduct_float(v1, v2, LEN);
235 oprod = fdsp->scalarproduct_float(v1, v2, LEN);
237 if (ret = compare_floats(&cprod, &oprod, 1, ARBITRARY_SCALARPRODUCT_CONST))
238 av_log(NULL, AV_LOG_ERROR, "scalarproduct_float failed\n");
243 int main(int argc, char **argv)
247 AVFloatDSPContext fdsp, cdsp;
250 LOCAL_ALIGNED(32, float, src0, [LEN]);
251 LOCAL_ALIGNED(32, float, src1, [LEN]);
252 LOCAL_ALIGNED(32, float, src2, [LEN]);
253 LOCAL_ALIGNED(32, double, dbl_src0, [LEN]);
254 LOCAL_ALIGNED(32, double, dbl_src1, [LEN]);
256 if (argc > 2 && !strcmp(argv[1], "-s"))
257 seed = strtoul(argv[2], NULL, 10);
259 seed = av_get_random_seed();
261 av_log(NULL, AV_LOG_INFO, "float_dsp-test: random seed %u\n", seed);
263 av_lfg_init(&lfg, seed);
265 fill_float_array(&lfg, src0, LEN);
266 fill_float_array(&lfg, src1, LEN);
267 fill_float_array(&lfg, src2, LEN);
269 fill_double_array(&lfg, dbl_src0, LEN);
270 fill_double_array(&lfg, dbl_src1, LEN);
272 avpriv_float_dsp_init(&fdsp, 1);
273 av_set_cpu_flags_mask(0);
274 avpriv_float_dsp_init(&cdsp, 1);
276 if (test_vector_fmul(&fdsp, &cdsp, src0, src1))
278 if (test_vector_fmac_scalar(&fdsp, &cdsp, src2, src0, src1[0]))
280 if (test_vector_fmul_scalar(&fdsp, &cdsp, src0, src1[0]))
282 if (test_vector_fmul_window(&fdsp, &cdsp, src0, src1, src2))
284 if (test_vector_fmul_add(&fdsp, &cdsp, src0, src1, src2))
286 if (test_vector_fmul_reverse(&fdsp, &cdsp, src0, src1))
288 if (test_butterflies_float(&fdsp, &cdsp, src0, src1))
290 if (test_scalarproduct_float(&fdsp, &cdsp, src0, src1))
292 if (test_vector_dmul_scalar(&fdsp, &cdsp, dbl_src0, dbl_src1[0]))