You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

358 lines
10KB

  1. /*
  2. * This file is part of FFmpeg.
  3. *
  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.
  8. *
  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.
  13. *
  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
  17. */
  18. #include "config.h"
  19. #include <float.h>
  20. #include <math.h>
  21. #include <stdint.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #if HAVE_UNISTD_H
  25. #include <unistd.h> /* for getopt */
  26. #endif
  27. #if !HAVE_GETOPT
  28. #include "compat/getopt.c"
  29. #endif
  30. #include "libavutil/common.h"
  31. #include "libavutil/cpu.h"
  32. #include "libavutil/internal.h"
  33. #include "libavutil/lfg.h"
  34. #include "libavutil/log.h"
  35. #include "libavutil/random_seed.h"
  36. #include "libavutil/float_dsp.h"
  37. #define LEN 240
  38. static void fill_float_array(AVLFG *lfg, float *a, int len)
  39. {
  40. int i;
  41. double bmg[2], stddev = 10.0, mean = 0.0;
  42. for (i = 0; i < len; i += 2) {
  43. av_bmg_get(lfg, bmg);
  44. a[i] = bmg[0] * stddev + mean;
  45. a[i + 1] = bmg[1] * stddev + mean;
  46. }
  47. }
  48. static int compare_floats(const float *a, const float *b, int len,
  49. float max_diff)
  50. {
  51. int i;
  52. for (i = 0; i < len; i++) {
  53. if (fabsf(a[i] - b[i]) > max_diff) {
  54. av_log(NULL, AV_LOG_ERROR, "%d: %- .12f - %- .12f = % .12g\n",
  55. i, a[i], b[i], a[i] - b[i]);
  56. return -1;
  57. }
  58. }
  59. return 0;
  60. }
  61. static void fill_double_array(AVLFG *lfg, double *a, int len)
  62. {
  63. int i;
  64. double bmg[2], stddev = 10.0, mean = 0.0;
  65. for (i = 0; i < len; i += 2) {
  66. av_bmg_get(lfg, bmg);
  67. a[i] = bmg[0] * stddev + mean;
  68. a[i + 1] = bmg[1] * stddev + mean;
  69. }
  70. }
  71. static int compare_doubles(const double *a, const double *b, int len,
  72. double max_diff)
  73. {
  74. int i;
  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]);
  79. return -1;
  80. }
  81. }
  82. return 0;
  83. }
  84. static int test_vector_fmul(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp,
  85. const float *v1, const float *v2)
  86. {
  87. LOCAL_ALIGNED(32, float, cdst, [LEN]);
  88. LOCAL_ALIGNED(32, float, odst, [LEN]);
  89. int ret;
  90. cdsp->vector_fmul(cdst, v1, v2, LEN);
  91. fdsp->vector_fmul(odst, v1, v2, LEN);
  92. if (ret = compare_floats(cdst, odst, LEN, FLT_EPSILON))
  93. av_log(NULL, AV_LOG_ERROR, "vector_fmul failed\n");
  94. return ret;
  95. }
  96. #define ARBITRARY_FMAC_SCALAR_CONST 0.005
  97. static int test_vector_fmac_scalar(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp,
  98. const float *v1, const float *src0, float scale)
  99. {
  100. LOCAL_ALIGNED(32, float, cdst, [LEN]);
  101. LOCAL_ALIGNED(32, float, odst, [LEN]);
  102. int ret;
  103. memcpy(cdst, v1, LEN * sizeof(*v1));
  104. memcpy(odst, v1, LEN * sizeof(*v1));
  105. cdsp->vector_fmac_scalar(cdst, src0, scale, LEN);
  106. fdsp->vector_fmac_scalar(odst, src0, scale, LEN);
  107. if (ret = compare_floats(cdst, odst, LEN, ARBITRARY_FMAC_SCALAR_CONST))
  108. av_log(NULL, AV_LOG_ERROR, "vector_fmac_scalar failed\n");
  109. return ret;
  110. }
  111. static int test_vector_fmul_scalar(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp,
  112. const float *v1, float scale)
  113. {
  114. LOCAL_ALIGNED(32, float, cdst, [LEN]);
  115. LOCAL_ALIGNED(32, float, odst, [LEN]);
  116. int ret;
  117. cdsp->vector_fmul_scalar(cdst, v1, scale, LEN);
  118. fdsp->vector_fmul_scalar(odst, v1, scale, LEN);
  119. if (ret = compare_floats(cdst, odst, LEN, FLT_EPSILON))
  120. av_log(NULL, AV_LOG_ERROR, "vector_fmul_scalar failed\n");
  121. return ret;
  122. }
  123. #define ARBITRARY_DMAC_SCALAR_CONST 0.005
  124. static int test_vector_dmac_scalar(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp,
  125. const double *v1, const double *src0, double scale)
  126. {
  127. LOCAL_ALIGNED(32, double, cdst, [LEN]);
  128. LOCAL_ALIGNED(32, double, odst, [LEN]);
  129. int ret;
  130. memcpy(cdst, v1, LEN * sizeof(*v1));
  131. memcpy(odst, v1, LEN * sizeof(*v1));
  132. cdsp->vector_dmac_scalar(cdst, src0, scale, LEN);
  133. fdsp->vector_dmac_scalar(odst, src0, scale, LEN);
  134. if (ret = compare_doubles(cdst, odst, LEN, ARBITRARY_DMAC_SCALAR_CONST))
  135. av_log(NULL, AV_LOG_ERROR, "vector_dmac_scalar failed\n");
  136. return ret;
  137. }
  138. static int test_vector_dmul_scalar(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp,
  139. const double *v1, double scale)
  140. {
  141. LOCAL_ALIGNED(32, double, cdst, [LEN]);
  142. LOCAL_ALIGNED(32, double, odst, [LEN]);
  143. int ret;
  144. cdsp->vector_dmul_scalar(cdst, v1, scale, LEN);
  145. fdsp->vector_dmul_scalar(odst, v1, scale, LEN);
  146. if (ret = compare_doubles(cdst, odst, LEN, DBL_EPSILON))
  147. av_log(NULL, AV_LOG_ERROR, "vector_dmul_scalar failed\n");
  148. return ret;
  149. }
  150. #define ARBITRARY_FMUL_WINDOW_CONST 0.008
  151. static int test_vector_fmul_window(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp,
  152. const float *v1, const float *v2, const float *v3)
  153. {
  154. LOCAL_ALIGNED(32, float, cdst, [LEN]);
  155. LOCAL_ALIGNED(32, float, odst, [LEN]);
  156. int ret;
  157. cdsp->vector_fmul_window(cdst, v1, v2, v3, LEN / 2);
  158. fdsp->vector_fmul_window(odst, v1, v2, v3, LEN / 2);
  159. if (ret = compare_floats(cdst, odst, LEN, ARBITRARY_FMUL_WINDOW_CONST))
  160. av_log(NULL, AV_LOG_ERROR, "vector_fmul_window failed\n");
  161. return ret;
  162. }
  163. #define ARBITRARY_FMUL_ADD_CONST 0.005
  164. static int test_vector_fmul_add(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp,
  165. const float *v1, const float *v2, const float *v3)
  166. {
  167. LOCAL_ALIGNED(32, float, cdst, [LEN]);
  168. LOCAL_ALIGNED(32, float, odst, [LEN]);
  169. int ret;
  170. cdsp->vector_fmul_add(cdst, v1, v2, v3, LEN);
  171. fdsp->vector_fmul_add(odst, v1, v2, v3, LEN);
  172. if (ret = compare_floats(cdst, odst, LEN, ARBITRARY_FMUL_ADD_CONST))
  173. av_log(NULL, AV_LOG_ERROR, "vector_fmul_add failed\n");
  174. return ret;
  175. }
  176. static int test_vector_fmul_reverse(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp,
  177. const float *v1, const float *v2)
  178. {
  179. LOCAL_ALIGNED(32, float, cdst, [LEN]);
  180. LOCAL_ALIGNED(32, float, odst, [LEN]);
  181. int ret;
  182. cdsp->vector_fmul_reverse(cdst, v1, v2, LEN);
  183. fdsp->vector_fmul_reverse(odst, v1, v2, LEN);
  184. if (ret = compare_floats(cdst, odst, LEN, FLT_EPSILON))
  185. av_log(NULL, AV_LOG_ERROR, "vector_fmul_reverse failed\n");
  186. return ret;
  187. }
  188. static int test_butterflies_float(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp,
  189. const float *v1, const float *v2)
  190. {
  191. LOCAL_ALIGNED(32, float, cv1, [LEN]);
  192. LOCAL_ALIGNED(32, float, cv2, [LEN]);
  193. LOCAL_ALIGNED(32, float, ov1, [LEN]);
  194. LOCAL_ALIGNED(32, float, ov2, [LEN]);
  195. int ret;
  196. memcpy(cv1, v1, LEN * sizeof(*v1));
  197. memcpy(cv2, v2, LEN * sizeof(*v2));
  198. memcpy(ov1, v1, LEN * sizeof(*v1));
  199. memcpy(ov2, v2, LEN * sizeof(*v2));
  200. cdsp->butterflies_float(cv1, cv2, LEN);
  201. fdsp->butterflies_float(ov1, ov2, LEN);
  202. if ((ret = compare_floats(cv1, ov1, LEN, FLT_EPSILON)) ||
  203. (ret = compare_floats(cv2, ov2, LEN, FLT_EPSILON)))
  204. av_log(NULL, AV_LOG_ERROR, "butterflies_float failed\n");
  205. return ret;
  206. }
  207. #define ARBITRARY_SCALARPRODUCT_CONST 0.2
  208. static int test_scalarproduct_float(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp,
  209. const float *v1, const float *v2)
  210. {
  211. float cprod, oprod;
  212. int ret;
  213. cprod = cdsp->scalarproduct_float(v1, v2, LEN);
  214. oprod = fdsp->scalarproduct_float(v1, v2, LEN);
  215. if (ret = compare_floats(&cprod, &oprod, 1, ARBITRARY_SCALARPRODUCT_CONST))
  216. av_log(NULL, AV_LOG_ERROR, "scalarproduct_float failed\n");
  217. return ret;
  218. }
  219. int main(int argc, char **argv)
  220. {
  221. int ret = 0, seeded = 0;
  222. uint32_t seed;
  223. AVFloatDSPContext *fdsp, *cdsp;
  224. AVLFG lfg;
  225. LOCAL_ALIGNED(32, float, src0, [LEN]);
  226. LOCAL_ALIGNED(32, float, src1, [LEN]);
  227. LOCAL_ALIGNED(32, float, src2, [LEN]);
  228. LOCAL_ALIGNED(32, double, dbl_src0, [LEN]);
  229. LOCAL_ALIGNED(32, double, dbl_src1, [LEN]);
  230. LOCAL_ALIGNED(32, double, dbl_src2, [LEN]);
  231. for (;;) {
  232. int arg = getopt(argc, argv, "s:c:");
  233. if (arg == -1)
  234. break;
  235. switch (arg) {
  236. case 's':
  237. seed = strtoul(optarg, NULL, 10);
  238. seeded = 1;
  239. break;
  240. case 'c':
  241. {
  242. int cpuflags = av_get_cpu_flags();
  243. if (av_parse_cpu_caps(&cpuflags, optarg) < 0)
  244. return 1;
  245. av_force_cpu_flags(cpuflags);
  246. break;
  247. }
  248. }
  249. }
  250. if (!seeded)
  251. seed = av_get_random_seed();
  252. av_log(NULL, AV_LOG_INFO, "float_dsp-test: %s %u\n", seeded ? "seed" : "random seed", seed);
  253. fdsp = avpriv_float_dsp_alloc(1);
  254. av_force_cpu_flags(0);
  255. cdsp = avpriv_float_dsp_alloc(1);
  256. if (!fdsp || !cdsp) {
  257. ret = 1;
  258. goto end;
  259. }
  260. av_lfg_init(&lfg, seed);
  261. fill_float_array(&lfg, src0, LEN);
  262. fill_float_array(&lfg, src1, LEN);
  263. fill_float_array(&lfg, src2, LEN);
  264. fill_double_array(&lfg, dbl_src0, LEN);
  265. fill_double_array(&lfg, dbl_src1, LEN);
  266. fill_double_array(&lfg, dbl_src2, LEN);
  267. if (test_vector_fmul(fdsp, cdsp, src0, src1))
  268. ret -= 1 << 0;
  269. if (test_vector_fmac_scalar(fdsp, cdsp, src2, src0, src1[0]))
  270. ret -= 1 << 1;
  271. if (test_vector_fmul_scalar(fdsp, cdsp, src0, src1[0]))
  272. ret -= 1 << 2;
  273. if (test_vector_fmul_window(fdsp, cdsp, src0, src1, src2))
  274. ret -= 1 << 3;
  275. if (test_vector_fmul_add(fdsp, cdsp, src0, src1, src2))
  276. ret -= 1 << 4;
  277. if (test_vector_fmul_reverse(fdsp, cdsp, src0, src1))
  278. ret -= 1 << 5;
  279. if (test_butterflies_float(fdsp, cdsp, src0, src1))
  280. ret -= 1 << 6;
  281. if (test_scalarproduct_float(fdsp, cdsp, src0, src1))
  282. ret -= 1 << 7;
  283. if (test_vector_dmul_scalar(fdsp, cdsp, dbl_src0, dbl_src1[0]))
  284. ret -= 1 << 8;
  285. if (test_vector_dmac_scalar(fdsp, cdsp, dbl_src2, dbl_src0, dbl_src1[0]))
  286. ret -= 1 << 9;
  287. end:
  288. av_freep(&fdsp);
  289. av_freep(&cdsp);
  290. return ret;
  291. }