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.

334 lines
9.6KB

  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. static int test_vector_dmul_scalar(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp,
  124. const double *v1, double scale)
  125. {
  126. LOCAL_ALIGNED(32, double, cdst, [LEN]);
  127. LOCAL_ALIGNED(32, double, odst, [LEN]);
  128. int ret;
  129. cdsp->vector_dmul_scalar(cdst, v1, scale, LEN);
  130. fdsp->vector_dmul_scalar(odst, v1, scale, LEN);
  131. if (ret = compare_doubles(cdst, odst, LEN, DBL_EPSILON))
  132. av_log(NULL, AV_LOG_ERROR, "vector_dmul_scalar failed\n");
  133. return ret;
  134. }
  135. #define ARBITRARY_FMUL_WINDOW_CONST 0.008
  136. static int test_vector_fmul_window(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp,
  137. const float *v1, const float *v2, const float *v3)
  138. {
  139. LOCAL_ALIGNED(32, float, cdst, [LEN]);
  140. LOCAL_ALIGNED(32, float, odst, [LEN]);
  141. int ret;
  142. cdsp->vector_fmul_window(cdst, v1, v2, v3, LEN / 2);
  143. fdsp->vector_fmul_window(odst, v1, v2, v3, LEN / 2);
  144. if (ret = compare_floats(cdst, odst, LEN, ARBITRARY_FMUL_WINDOW_CONST))
  145. av_log(NULL, AV_LOG_ERROR, "vector_fmul_window failed\n");
  146. return ret;
  147. }
  148. #define ARBITRARY_FMUL_ADD_CONST 0.005
  149. static int test_vector_fmul_add(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp,
  150. const float *v1, const float *v2, const float *v3)
  151. {
  152. LOCAL_ALIGNED(32, float, cdst, [LEN]);
  153. LOCAL_ALIGNED(32, float, odst, [LEN]);
  154. int ret;
  155. cdsp->vector_fmul_add(cdst, v1, v2, v3, LEN);
  156. fdsp->vector_fmul_add(odst, v1, v2, v3, LEN);
  157. if (ret = compare_floats(cdst, odst, LEN, ARBITRARY_FMUL_ADD_CONST))
  158. av_log(NULL, AV_LOG_ERROR, "vector_fmul_add failed\n");
  159. return ret;
  160. }
  161. static int test_vector_fmul_reverse(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp,
  162. const float *v1, const float *v2)
  163. {
  164. LOCAL_ALIGNED(32, float, cdst, [LEN]);
  165. LOCAL_ALIGNED(32, float, odst, [LEN]);
  166. int ret;
  167. cdsp->vector_fmul_reverse(cdst, v1, v2, LEN);
  168. fdsp->vector_fmul_reverse(odst, v1, v2, LEN);
  169. if (ret = compare_floats(cdst, odst, LEN, FLT_EPSILON))
  170. av_log(NULL, AV_LOG_ERROR, "vector_fmul_reverse failed\n");
  171. return ret;
  172. }
  173. static int test_butterflies_float(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp,
  174. const float *v1, const float *v2)
  175. {
  176. LOCAL_ALIGNED(32, float, cv1, [LEN]);
  177. LOCAL_ALIGNED(32, float, cv2, [LEN]);
  178. LOCAL_ALIGNED(32, float, ov1, [LEN]);
  179. LOCAL_ALIGNED(32, float, ov2, [LEN]);
  180. int ret;
  181. memcpy(cv1, v1, LEN * sizeof(*v1));
  182. memcpy(cv2, v2, LEN * sizeof(*v2));
  183. memcpy(ov1, v1, LEN * sizeof(*v1));
  184. memcpy(ov2, v2, LEN * sizeof(*v2));
  185. cdsp->butterflies_float(cv1, cv2, LEN);
  186. fdsp->butterflies_float(ov1, ov2, LEN);
  187. if ((ret = compare_floats(cv1, ov1, LEN, FLT_EPSILON)) ||
  188. (ret = compare_floats(cv2, ov2, LEN, FLT_EPSILON)))
  189. av_log(NULL, AV_LOG_ERROR, "butterflies_float failed\n");
  190. return ret;
  191. }
  192. #define ARBITRARY_SCALARPRODUCT_CONST 0.2
  193. static int test_scalarproduct_float(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp,
  194. const float *v1, const float *v2)
  195. {
  196. float cprod, oprod;
  197. int ret;
  198. cprod = cdsp->scalarproduct_float(v1, v2, LEN);
  199. oprod = fdsp->scalarproduct_float(v1, v2, LEN);
  200. if (ret = compare_floats(&cprod, &oprod, 1, ARBITRARY_SCALARPRODUCT_CONST))
  201. av_log(NULL, AV_LOG_ERROR, "scalarproduct_float failed\n");
  202. return ret;
  203. }
  204. int main(int argc, char **argv)
  205. {
  206. int ret = 0, seeded = 0;
  207. uint32_t seed;
  208. AVFloatDSPContext *fdsp, *cdsp;
  209. AVLFG lfg;
  210. LOCAL_ALIGNED(32, float, src0, [LEN]);
  211. LOCAL_ALIGNED(32, float, src1, [LEN]);
  212. LOCAL_ALIGNED(32, float, src2, [LEN]);
  213. LOCAL_ALIGNED(32, double, dbl_src0, [LEN]);
  214. LOCAL_ALIGNED(32, double, dbl_src1, [LEN]);
  215. for (;;) {
  216. int arg = getopt(argc, argv, "s:c:");
  217. if (arg == -1)
  218. break;
  219. switch (arg) {
  220. case 's':
  221. seed = strtoul(optarg, NULL, 10);
  222. seeded = 1;
  223. break;
  224. case 'c':
  225. {
  226. int cpuflags = av_get_cpu_flags();
  227. if (av_parse_cpu_caps(&cpuflags, optarg) < 0)
  228. return 1;
  229. av_force_cpu_flags(cpuflags);
  230. break;
  231. }
  232. }
  233. }
  234. if (!seeded)
  235. seed = av_get_random_seed();
  236. av_log(NULL, AV_LOG_INFO, "float_dsp-test: %s %u\n", seeded ? "seed" : "random seed", seed);
  237. fdsp = avpriv_float_dsp_alloc(1);
  238. av_force_cpu_flags(0);
  239. cdsp = avpriv_float_dsp_alloc(1);
  240. if (!fdsp || !cdsp) {
  241. ret = 1;
  242. goto end;
  243. }
  244. av_lfg_init(&lfg, seed);
  245. fill_float_array(&lfg, src0, LEN);
  246. fill_float_array(&lfg, src1, LEN);
  247. fill_float_array(&lfg, src2, LEN);
  248. fill_double_array(&lfg, dbl_src0, LEN);
  249. fill_double_array(&lfg, dbl_src1, LEN);
  250. if (test_vector_fmul(fdsp, cdsp, src0, src1))
  251. ret -= 1 << 0;
  252. if (test_vector_fmac_scalar(fdsp, cdsp, src2, src0, src1[0]))
  253. ret -= 1 << 1;
  254. if (test_vector_fmul_scalar(fdsp, cdsp, src0, src1[0]))
  255. ret -= 1 << 2;
  256. if (test_vector_fmul_window(fdsp, cdsp, src0, src1, src2))
  257. ret -= 1 << 3;
  258. if (test_vector_fmul_add(fdsp, cdsp, src0, src1, src2))
  259. ret -= 1 << 4;
  260. if (test_vector_fmul_reverse(fdsp, cdsp, src0, src1))
  261. ret -= 1 << 5;
  262. if (test_butterflies_float(fdsp, cdsp, src0, src1))
  263. ret -= 1 << 6;
  264. if (test_scalarproduct_float(fdsp, cdsp, src0, src1))
  265. ret -= 1 << 7;
  266. if (test_vector_dmul_scalar(fdsp, cdsp, dbl_src0, dbl_src1[0]))
  267. ret -= 1 << 8;
  268. end:
  269. av_freep(&fdsp);
  270. av_freep(&cdsp);
  271. return ret;
  272. }