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.

312 lines
10KB

  1. /*
  2. * This file is part of FFmpeg.
  3. *
  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.
  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
  12. * GNU General Public License for more details.
  13. *
  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.
  17. */
  18. #include "config.h"
  19. #include <float.h>
  20. #include <stdint.h>
  21. #include "libavutil/float_dsp.h"
  22. #include "libavutil/internal.h"
  23. #include "checkasm.h"
  24. #define LEN 256
  25. #define randomize_buffer(buf) \
  26. do { \
  27. int i; \
  28. double bmg[2], stddev = 10.0, mean = 0.0; \
  29. \
  30. for (i = 0; i < LEN; i += 2) { \
  31. av_bmg_get(&checkasm_lfg, bmg); \
  32. buf[i] = bmg[0] * stddev + mean; \
  33. buf[i + 1] = bmg[1] * stddev + mean; \
  34. } \
  35. } while(0);
  36. static void test_vector_fmul(const float *src0, const float *src1)
  37. {
  38. LOCAL_ALIGNED_32(float, cdst, [LEN]);
  39. LOCAL_ALIGNED_32(float, odst, [LEN]);
  40. int i;
  41. declare_func(void, float *dst, const float *src0, const float *src1,
  42. int len);
  43. call_ref(cdst, src0, src1, LEN);
  44. call_new(odst, src0, src1, LEN);
  45. for (i = 0; i < LEN; i++) {
  46. if (!float_near_abs_eps(cdst[i], odst[i], FLT_EPSILON)) {
  47. fprintf(stderr, "%d: %- .12f - %- .12f = % .12g\n",
  48. i, cdst[i], odst[i], cdst[i] - odst[i]);
  49. fail();
  50. break;
  51. }
  52. }
  53. bench_new(odst, src0, src1, LEN);
  54. }
  55. #define ARBITRARY_FMUL_ADD_CONST 0.005
  56. static void test_vector_fmul_add(const float *src0, const float *src1, const float *src2)
  57. {
  58. LOCAL_ALIGNED_32(float, cdst, [LEN]);
  59. LOCAL_ALIGNED_32(float, odst, [LEN]);
  60. int i;
  61. declare_func(void, float *dst, const float *src0, const float *src1,
  62. const float *src2, int len);
  63. call_ref(cdst, src0, src1, src2, LEN);
  64. call_new(odst, src0, src1, src2, LEN);
  65. for (i = 0; i < LEN; i++) {
  66. if (!float_near_abs_eps(cdst[i], odst[i], ARBITRARY_FMUL_ADD_CONST)) {
  67. fprintf(stderr, "%d: %- .12f - %- .12f = % .12g\n",
  68. i, cdst[i], odst[i], cdst[i] - odst[i]);
  69. fail();
  70. break;
  71. }
  72. }
  73. bench_new(odst, src0, src1, src2, LEN);
  74. }
  75. static void test_vector_fmul_scalar(const float *src0, const float *src1)
  76. {
  77. LOCAL_ALIGNED_16(float, cdst, [LEN]);
  78. LOCAL_ALIGNED_16(float, odst, [LEN]);
  79. int i;
  80. declare_func(void, float *dst, const float *src, float mul, int len);
  81. call_ref(cdst, src0, src1[0], LEN);
  82. call_new(odst, src0, src1[0], LEN);
  83. for (i = 0; i < LEN; i++) {
  84. if (!float_near_abs_eps(cdst[i], odst[i], FLT_EPSILON)) {
  85. fprintf(stderr, "%d: %- .12f - %- .12f = % .12g\n",
  86. i, cdst[i], odst[i], cdst[i] - odst[i]);
  87. fail();
  88. break;
  89. }
  90. }
  91. bench_new(odst, src0, src1[0], LEN);
  92. }
  93. #define ARBITRARY_FMUL_WINDOW_CONST 0.008
  94. static void test_vector_fmul_window(const float *src0, const float *src1, const float *win)
  95. {
  96. LOCAL_ALIGNED_16(float, cdst, [LEN]);
  97. LOCAL_ALIGNED_16(float, odst, [LEN]);
  98. int i;
  99. declare_func(void, float *dst, const float *src0, const float *src1,
  100. const float *win, int len);
  101. call_ref(cdst, src0, src1, win, LEN / 2);
  102. call_new(odst, src0, src1, win, LEN / 2);
  103. for (i = 0; i < LEN; i++) {
  104. if (!float_near_abs_eps(cdst[i], odst[i], ARBITRARY_FMUL_WINDOW_CONST)) {
  105. fprintf(stderr, "%d: %- .12f - %- .12f = % .12g\n",
  106. i, cdst[i], odst[i], cdst[i] - odst[i]);
  107. fail();
  108. break;
  109. }
  110. }
  111. bench_new(odst, src0, src1, win, LEN / 2);
  112. }
  113. #define ARBITRARY_FMAC_SCALAR_CONST 0.005
  114. static void test_vector_fmac_scalar(const float *src0, const float *src1, const float *src2)
  115. {
  116. LOCAL_ALIGNED_32(float, cdst, [LEN]);
  117. LOCAL_ALIGNED_32(float, odst, [LEN]);
  118. int i;
  119. declare_func(void, float *dst, const float *src, float mul, int len);
  120. memcpy(cdst, src2, LEN * sizeof(*src2));
  121. memcpy(odst, src2, LEN * sizeof(*src2));
  122. call_ref(cdst, src0, src1[0], LEN);
  123. call_new(odst, src0, src1[0], LEN);
  124. for (i = 0; i < LEN; i++) {
  125. if (!float_near_abs_eps(cdst[i], odst[i], ARBITRARY_FMAC_SCALAR_CONST)) {
  126. fprintf(stderr, "%d: %- .12f - %- .12f = % .12g\n",
  127. i, cdst[i], odst[i], cdst[i] - odst[i]);
  128. fail();
  129. break;
  130. }
  131. }
  132. memcpy(odst, src2, LEN * sizeof(*src2));
  133. bench_new(odst, src0, src1[0], LEN);
  134. }
  135. static void test_vector_dmul_scalar(const double *src0, const double *src1)
  136. {
  137. LOCAL_ALIGNED_32(double, cdst, [LEN]);
  138. LOCAL_ALIGNED_32(double, odst, [LEN]);
  139. int i;
  140. declare_func(void, double *dst, const double *src, double mul, int len);
  141. call_ref(cdst, src0, src1[0], LEN);
  142. call_new(odst, src0, src1[0], LEN);
  143. for (i = 0; i < LEN; i++) {
  144. double t = fabs(src1[0]) + fabs(src0[i]) + fabs(src1[0] * src0[i]) + 1.0;
  145. if (!double_near_abs_eps(cdst[i], odst[i], t * 2 * DBL_EPSILON)) {
  146. fprintf(stderr, "%d: %- .12f - %- .12f = % .12g\n", i,
  147. cdst[i], odst[i], cdst[i] - odst[i]);
  148. fail();
  149. break;
  150. }
  151. }
  152. bench_new(odst, src0, src1[0], LEN);
  153. }
  154. #define ARBITRARY_DMAC_SCALAR_CONST 0.005
  155. static void test_vector_dmac_scalar(const double *src0, const double *src1, const double *src2)
  156. {
  157. LOCAL_ALIGNED_32(double, cdst, [LEN]);
  158. LOCAL_ALIGNED_32(double, odst, [LEN]);
  159. int i;
  160. declare_func(void, double *dst, const double *src, double mul, int len);
  161. memcpy(cdst, src2, LEN * sizeof(*src2));
  162. memcpy(odst, src2, LEN * sizeof(*src2));
  163. call_ref(cdst, src0, src1[0], LEN);
  164. call_new(odst, src0, src1[0], LEN);
  165. for (i = 0; i < LEN; i++) {
  166. if (!double_near_abs_eps(cdst[i], odst[i], ARBITRARY_DMAC_SCALAR_CONST)) {
  167. fprintf(stderr, "%d: %- .12f - %- .12f = % .12g\n",
  168. i, cdst[i], odst[i], cdst[i] - odst[i]);
  169. fail();
  170. break;
  171. }
  172. }
  173. memcpy(odst, src2, LEN * sizeof(*src2));
  174. bench_new(odst, src0, src1[0], LEN);
  175. }
  176. static void test_butterflies_float(const float *src0, const float *src1)
  177. {
  178. LOCAL_ALIGNED_16(float, cdst, [LEN]);
  179. LOCAL_ALIGNED_16(float, odst, [LEN]);
  180. LOCAL_ALIGNED_16(float, cdst1, [LEN]);
  181. LOCAL_ALIGNED_16(float, odst1, [LEN]);
  182. int i;
  183. declare_func(void, float *av_restrict src0, float *av_restrict src1,
  184. int len);
  185. memcpy(cdst, src0, LEN * sizeof(*src0));
  186. memcpy(cdst1, src1, LEN * sizeof(*src1));
  187. memcpy(odst, src0, LEN * sizeof(*src0));
  188. memcpy(odst1, src1, LEN * sizeof(*src1));
  189. call_ref(cdst, cdst1, LEN);
  190. call_new(odst, odst1, LEN);
  191. for (i = 0; i < LEN; i++) {
  192. if (!float_near_abs_eps(cdst[i], odst[i], FLT_EPSILON) ||
  193. !float_near_abs_eps(cdst1[i], odst1[i], FLT_EPSILON)) {
  194. fprintf(stderr, "%d: %- .12f - %- .12f = % .12g\n",
  195. i, cdst[i], odst[i], cdst[i] - odst[i]);
  196. fprintf(stderr, "%d: %- .12f - %- .12f = % .12g\n",
  197. i, cdst1[i], odst1[i], cdst1[i] - odst1[i]);
  198. fail();
  199. break;
  200. }
  201. }
  202. memcpy(odst, src0, LEN * sizeof(*src0));
  203. memcpy(odst1, src1, LEN * sizeof(*src1));
  204. bench_new(odst, odst1, LEN);
  205. }
  206. #define ARBITRARY_SCALARPRODUCT_CONST 0.2
  207. static void test_scalarproduct_float(const float *src0, const float *src1)
  208. {
  209. float cprod, oprod;
  210. declare_func_float(float, const float *src0, const float *src1, int len);
  211. cprod = call_ref(src0, src1, LEN);
  212. oprod = call_new(src0, src1, LEN);
  213. if (!float_near_abs_eps(cprod, oprod, ARBITRARY_SCALARPRODUCT_CONST)) {
  214. fprintf(stderr, "%- .12f - %- .12f = % .12g\n",
  215. cprod, oprod, cprod - oprod);
  216. fail();
  217. }
  218. bench_new(src0, src1, LEN);
  219. }
  220. void checkasm_check_float_dsp(void)
  221. {
  222. LOCAL_ALIGNED_32(float, src0, [LEN]);
  223. LOCAL_ALIGNED_32(float, src1, [LEN]);
  224. LOCAL_ALIGNED_32(float, src2, [LEN]);
  225. LOCAL_ALIGNED_16(float, src3, [LEN]);
  226. LOCAL_ALIGNED_16(float, src4, [LEN]);
  227. LOCAL_ALIGNED_16(float, src5, [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. AVFloatDSPContext *fdsp = avpriv_float_dsp_alloc(1);
  232. if (!fdsp) {
  233. fprintf(stderr, "floatdsp: Out of memory error\n");
  234. return;
  235. }
  236. randomize_buffer(src0);
  237. randomize_buffer(src1);
  238. randomize_buffer(src2);
  239. randomize_buffer(src3);
  240. randomize_buffer(src4);
  241. randomize_buffer(src5);
  242. randomize_buffer(dbl_src0);
  243. randomize_buffer(dbl_src1);
  244. randomize_buffer(dbl_src2);
  245. if (check_func(fdsp->vector_fmul, "vector_fmul"))
  246. test_vector_fmul(src0, src1);
  247. if (check_func(fdsp->vector_fmul_add, "vector_fmul_add"))
  248. test_vector_fmul_add(src0, src1, src2);
  249. if (check_func(fdsp->vector_fmul_scalar, "vector_fmul_scalar"))
  250. test_vector_fmul_scalar(src3, src4);
  251. if (check_func(fdsp->vector_fmul_reverse, "vector_fmul_reverse"))
  252. test_vector_fmul(src0, src1);
  253. if (check_func(fdsp->vector_fmul_window, "vector_fmul_window"))
  254. test_vector_fmul_window(src3, src4, src5);
  255. report("vector_fmul");
  256. if (check_func(fdsp->vector_fmac_scalar, "vector_fmac_scalar"))
  257. test_vector_fmac_scalar(src0, src1, src2);
  258. report("vector_fmac");
  259. if (check_func(fdsp->vector_dmul_scalar, "vector_dmul_scalar"))
  260. test_vector_dmul_scalar(dbl_src0, dbl_src1);
  261. report("vector_dmul");
  262. if (check_func(fdsp->vector_dmac_scalar, "vector_dmac_scalar"))
  263. test_vector_dmac_scalar(dbl_src0, dbl_src1, dbl_src2);
  264. report("vector_dmac");
  265. if (check_func(fdsp->butterflies_float, "butterflies_float"))
  266. test_butterflies_float(src3, src4);
  267. report("butterflies_float");
  268. if (check_func(fdsp->scalarproduct_float, "scalarproduct_float"))
  269. test_scalarproduct_float(src3, src4);
  270. report("scalarproduct_float");
  271. av_freep(&fdsp);
  272. }