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.

311 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. if (!double_near_abs_eps(cdst[i], odst[i], DBL_EPSILON)) {
  145. fprintf(stderr, "%d: %- .12f - %- .12f = % .12g\n", i,
  146. cdst[i], odst[i], cdst[i] - odst[i]);
  147. fail();
  148. break;
  149. }
  150. }
  151. bench_new(odst, src0, src1[0], LEN);
  152. }
  153. #define ARBITRARY_DMAC_SCALAR_CONST 0.005
  154. static void test_vector_dmac_scalar(const double *src0, const double *src1, const double *src2)
  155. {
  156. LOCAL_ALIGNED_32(double, cdst, [LEN]);
  157. LOCAL_ALIGNED_32(double, odst, [LEN]);
  158. int i;
  159. declare_func(void, double *dst, const double *src, double mul, int len);
  160. memcpy(cdst, src2, LEN * sizeof(*src2));
  161. memcpy(odst, src2, LEN * sizeof(*src2));
  162. call_ref(cdst, src0, src1[0], LEN);
  163. call_new(odst, src0, src1[0], LEN);
  164. for (i = 0; i < LEN; i++) {
  165. if (!double_near_abs_eps(cdst[i], odst[i], ARBITRARY_DMAC_SCALAR_CONST)) {
  166. fprintf(stderr, "%d: %- .12f - %- .12f = % .12g\n",
  167. i, cdst[i], odst[i], cdst[i] - odst[i]);
  168. fail();
  169. break;
  170. }
  171. }
  172. memcpy(odst, src2, LEN * sizeof(*src2));
  173. bench_new(odst, src0, src1[0], LEN);
  174. }
  175. static void test_butterflies_float(const float *src0, const float *src1)
  176. {
  177. LOCAL_ALIGNED_16(float, cdst, [LEN]);
  178. LOCAL_ALIGNED_16(float, odst, [LEN]);
  179. LOCAL_ALIGNED_16(float, cdst1, [LEN]);
  180. LOCAL_ALIGNED_16(float, odst1, [LEN]);
  181. int i;
  182. declare_func(void, float *av_restrict src0, float *av_restrict src1,
  183. int len);
  184. memcpy(cdst, src0, LEN * sizeof(*src0));
  185. memcpy(cdst1, src1, LEN * sizeof(*src1));
  186. memcpy(odst, src0, LEN * sizeof(*src0));
  187. memcpy(odst1, src1, LEN * sizeof(*src1));
  188. call_ref(cdst, cdst1, LEN);
  189. call_new(odst, odst1, LEN);
  190. for (i = 0; i < LEN; i++) {
  191. if (!float_near_abs_eps(cdst[i], odst[i], FLT_EPSILON) ||
  192. !float_near_abs_eps(cdst1[i], odst1[i], FLT_EPSILON)) {
  193. fprintf(stderr, "%d: %- .12f - %- .12f = % .12g\n",
  194. i, cdst[i], odst[i], cdst[i] - odst[i]);
  195. fprintf(stderr, "%d: %- .12f - %- .12f = % .12g\n",
  196. i, cdst1[i], odst1[i], cdst1[i] - odst1[i]);
  197. fail();
  198. break;
  199. }
  200. }
  201. memcpy(odst, src0, LEN * sizeof(*src0));
  202. memcpy(odst1, src1, LEN * sizeof(*src1));
  203. bench_new(odst, odst1, LEN);
  204. }
  205. #define ARBITRARY_SCALARPRODUCT_CONST 0.2
  206. static void test_scalarproduct_float(const float *src0, const float *src1)
  207. {
  208. float cprod, oprod;
  209. declare_func_float(float, const float *src0, const float *src1, int len);
  210. cprod = call_ref(src0, src1, LEN);
  211. oprod = call_new(src0, src1, LEN);
  212. if (!float_near_abs_eps(cprod, oprod, ARBITRARY_SCALARPRODUCT_CONST)) {
  213. fprintf(stderr, "%- .12f - %- .12f = % .12g\n",
  214. cprod, oprod, cprod - oprod);
  215. fail();
  216. }
  217. bench_new(src0, src1, LEN);
  218. }
  219. void checkasm_check_float_dsp(void)
  220. {
  221. LOCAL_ALIGNED_32(float, src0, [LEN]);
  222. LOCAL_ALIGNED_32(float, src1, [LEN]);
  223. LOCAL_ALIGNED_32(float, src2, [LEN]);
  224. LOCAL_ALIGNED_16(float, src3, [LEN]);
  225. LOCAL_ALIGNED_16(float, src4, [LEN]);
  226. LOCAL_ALIGNED_16(float, src5, [LEN]);
  227. LOCAL_ALIGNED_32(double, dbl_src0, [LEN]);
  228. LOCAL_ALIGNED_32(double, dbl_src1, [LEN]);
  229. LOCAL_ALIGNED_32(double, dbl_src2, [LEN]);
  230. AVFloatDSPContext *fdsp = avpriv_float_dsp_alloc(1);
  231. if (!fdsp) {
  232. fprintf(stderr, "floatdsp: Out of memory error\n");
  233. return;
  234. }
  235. randomize_buffer(src0);
  236. randomize_buffer(src1);
  237. randomize_buffer(src2);
  238. randomize_buffer(src3);
  239. randomize_buffer(src4);
  240. randomize_buffer(src5);
  241. randomize_buffer(dbl_src0);
  242. randomize_buffer(dbl_src1);
  243. randomize_buffer(dbl_src2);
  244. if (check_func(fdsp->vector_fmul, "vector_fmul"))
  245. test_vector_fmul(src0, src1);
  246. if (check_func(fdsp->vector_fmul_add, "vector_fmul_add"))
  247. test_vector_fmul_add(src0, src1, src2);
  248. if (check_func(fdsp->vector_fmul_scalar, "vector_fmul_scalar"))
  249. test_vector_fmul_scalar(src3, src4);
  250. if (check_func(fdsp->vector_fmul_reverse, "vector_fmul_reverse"))
  251. test_vector_fmul(src0, src1);
  252. if (check_func(fdsp->vector_fmul_window, "vector_fmul_window"))
  253. test_vector_fmul_window(src3, src4, src5);
  254. report("vector_fmul");
  255. if (check_func(fdsp->vector_fmac_scalar, "vector_fmac_scalar"))
  256. test_vector_fmac_scalar(src0, src1, src2);
  257. report("vector_fmac");
  258. if (check_func(fdsp->vector_dmul_scalar, "vector_dmul_scalar"))
  259. test_vector_dmul_scalar(dbl_src0, dbl_src1);
  260. report("vector_dmul");
  261. if (check_func(fdsp->vector_dmac_scalar, "vector_dmac_scalar"))
  262. test_vector_dmac_scalar(dbl_src0, dbl_src1, dbl_src2);
  263. report("vector_dmac");
  264. if (check_func(fdsp->butterflies_float, "butterflies_float"))
  265. test_butterflies_float(src3, src4);
  266. report("butterflies_float");
  267. if (check_func(fdsp->scalarproduct_float, "scalarproduct_float"))
  268. test_scalarproduct_float(src3, src4);
  269. report("scalarproduct_float");
  270. av_freep(&fdsp);
  271. }