float_dsp.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. static void test_vector_dmul(const double *src0, const double *src1)
  56. {
  57. LOCAL_ALIGNED_32(double, cdst, [LEN]);
  58. LOCAL_ALIGNED_32(double, odst, [LEN]);
  59. int i;
  60. declare_func(void, double *dst, const double *src0, const double *src1,
  61. int len);
  62. call_ref(cdst, src0, src1, LEN);
  63. call_new(odst, src0, src1, LEN);
  64. for (i = 0; i < LEN; i++) {
  65. if (!double_near_abs_eps(cdst[i], odst[i], DBL_EPSILON)) {
  66. fprintf(stderr, "%d: %- .12f - %- .12f = % .12g\n",
  67. i, cdst[i], odst[i], cdst[i] - odst[i]);
  68. fail();
  69. break;
  70. }
  71. }
  72. bench_new(odst, src0, src1, LEN);
  73. }
  74. #define ARBITRARY_FMUL_ADD_CONST 0.005
  75. static void test_vector_fmul_add(const float *src0, const float *src1, const float *src2)
  76. {
  77. LOCAL_ALIGNED_32(float, cdst, [LEN]);
  78. LOCAL_ALIGNED_32(float, odst, [LEN]);
  79. int i;
  80. declare_func(void, float *dst, const float *src0, const float *src1,
  81. const float *src2, int len);
  82. call_ref(cdst, src0, src1, src2, LEN);
  83. call_new(odst, src0, src1, src2, LEN);
  84. for (i = 0; i < LEN; i++) {
  85. if (!float_near_abs_eps(cdst[i], odst[i], ARBITRARY_FMUL_ADD_CONST)) {
  86. fprintf(stderr, "%d: %- .12f - %- .12f = % .12g\n",
  87. i, cdst[i], odst[i], cdst[i] - odst[i]);
  88. fail();
  89. break;
  90. }
  91. }
  92. bench_new(odst, src0, src1, src2, LEN);
  93. }
  94. static void test_vector_fmul_scalar(const float *src0, const float *src1)
  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 *src, float mul, int len);
  100. call_ref(cdst, src0, src1[0], LEN);
  101. call_new(odst, src0, src1[0], LEN);
  102. for (i = 0; i < LEN; i++) {
  103. if (!float_near_abs_eps(cdst[i], odst[i], FLT_EPSILON)) {
  104. fprintf(stderr, "%d: %- .12f - %- .12f = % .12g\n",
  105. i, cdst[i], odst[i], cdst[i] - odst[i]);
  106. fail();
  107. break;
  108. }
  109. }
  110. bench_new(odst, src0, src1[0], LEN);
  111. }
  112. #define ARBITRARY_FMUL_WINDOW_CONST 0.008
  113. static void test_vector_fmul_window(const float *src0, const float *src1, const float *win)
  114. {
  115. LOCAL_ALIGNED_16(float, cdst, [LEN]);
  116. LOCAL_ALIGNED_16(float, odst, [LEN]);
  117. int i;
  118. declare_func(void, float *dst, const float *src0, const float *src1,
  119. const float *win, int len);
  120. call_ref(cdst, src0, src1, win, LEN / 2);
  121. call_new(odst, src0, src1, win, LEN / 2);
  122. for (i = 0; i < LEN; i++) {
  123. if (!float_near_abs_eps(cdst[i], odst[i], ARBITRARY_FMUL_WINDOW_CONST)) {
  124. fprintf(stderr, "%d: %- .12f - %- .12f = % .12g\n",
  125. i, cdst[i], odst[i], cdst[i] - odst[i]);
  126. fail();
  127. break;
  128. }
  129. }
  130. bench_new(odst, src0, src1, win, LEN / 2);
  131. }
  132. #define ARBITRARY_FMAC_SCALAR_CONST 0.005
  133. static void test_vector_fmac_scalar(const float *src0, const float *src1, const float *src2)
  134. {
  135. LOCAL_ALIGNED_32(float, cdst, [LEN]);
  136. LOCAL_ALIGNED_32(float, odst, [LEN]);
  137. int i;
  138. declare_func(void, float *dst, const float *src, float mul, int len);
  139. memcpy(cdst, src2, LEN * sizeof(*src2));
  140. memcpy(odst, src2, LEN * sizeof(*src2));
  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 (!float_near_abs_eps(cdst[i], odst[i], ARBITRARY_FMAC_SCALAR_CONST)) {
  145. fprintf(stderr, "%d: %- .12f - %- .12f = % .12g\n",
  146. i, cdst[i], odst[i], cdst[i] - odst[i]);
  147. fail();
  148. break;
  149. }
  150. }
  151. memcpy(odst, src2, LEN * sizeof(*src2));
  152. bench_new(odst, src0, src1[0], LEN);
  153. }
  154. static void test_vector_dmul_scalar(const double *src0, const double *src1)
  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. call_ref(cdst, src0, src1[0], LEN);
  161. call_new(odst, src0, src1[0], LEN);
  162. for (i = 0; i < LEN; i++) {
  163. double t = fabs(src1[0]) + fabs(src0[i]) + fabs(src1[0] * src0[i]) + 1.0;
  164. if (!double_near_abs_eps(cdst[i], odst[i], t * 2 * DBL_EPSILON)) {
  165. fprintf(stderr, "%d: %- .12f - %- .12f = % .12g\n", i,
  166. cdst[i], odst[i], cdst[i] - odst[i]);
  167. fail();
  168. break;
  169. }
  170. }
  171. bench_new(odst, src0, src1[0], LEN);
  172. }
  173. #define ARBITRARY_DMAC_SCALAR_CONST 0.005
  174. static void test_vector_dmac_scalar(const double *src0, const double *src1, const double *src2)
  175. {
  176. LOCAL_ALIGNED_32(double, cdst, [LEN]);
  177. LOCAL_ALIGNED_32(double, odst, [LEN]);
  178. int i;
  179. declare_func(void, double *dst, const double *src, double mul, int len);
  180. memcpy(cdst, src2, LEN * sizeof(*src2));
  181. memcpy(odst, src2, LEN * sizeof(*src2));
  182. call_ref(cdst, src0, src1[0], LEN);
  183. call_new(odst, src0, src1[0], LEN);
  184. for (i = 0; i < LEN; i++) {
  185. if (!double_near_abs_eps(cdst[i], odst[i], ARBITRARY_DMAC_SCALAR_CONST)) {
  186. fprintf(stderr, "%d: %- .12f - %- .12f = % .12g\n",
  187. i, cdst[i], odst[i], cdst[i] - odst[i]);
  188. fail();
  189. break;
  190. }
  191. }
  192. memcpy(odst, src2, LEN * sizeof(*src2));
  193. bench_new(odst, src0, src1[0], LEN);
  194. }
  195. static void test_butterflies_float(const float *src0, const float *src1)
  196. {
  197. LOCAL_ALIGNED_16(float, cdst, [LEN]);
  198. LOCAL_ALIGNED_16(float, odst, [LEN]);
  199. LOCAL_ALIGNED_16(float, cdst1, [LEN]);
  200. LOCAL_ALIGNED_16(float, odst1, [LEN]);
  201. int i;
  202. declare_func(void, float *av_restrict src0, float *av_restrict src1,
  203. int len);
  204. memcpy(cdst, src0, LEN * sizeof(*src0));
  205. memcpy(cdst1, src1, LEN * sizeof(*src1));
  206. memcpy(odst, src0, LEN * sizeof(*src0));
  207. memcpy(odst1, src1, LEN * sizeof(*src1));
  208. call_ref(cdst, cdst1, LEN);
  209. call_new(odst, odst1, LEN);
  210. for (i = 0; i < LEN; i++) {
  211. if (!float_near_abs_eps(cdst[i], odst[i], FLT_EPSILON) ||
  212. !float_near_abs_eps(cdst1[i], odst1[i], FLT_EPSILON)) {
  213. fprintf(stderr, "%d: %- .12f - %- .12f = % .12g\n",
  214. i, cdst[i], odst[i], cdst[i] - odst[i]);
  215. fprintf(stderr, "%d: %- .12f - %- .12f = % .12g\n",
  216. i, cdst1[i], odst1[i], cdst1[i] - odst1[i]);
  217. fail();
  218. break;
  219. }
  220. }
  221. memcpy(odst, src0, LEN * sizeof(*src0));
  222. memcpy(odst1, src1, LEN * sizeof(*src1));
  223. bench_new(odst, odst1, LEN);
  224. }
  225. #define ARBITRARY_SCALARPRODUCT_CONST 0.2
  226. static void test_scalarproduct_float(const float *src0, const float *src1)
  227. {
  228. float cprod, oprod;
  229. declare_func_float(float, const float *src0, const float *src1, int len);
  230. cprod = call_ref(src0, src1, LEN);
  231. oprod = call_new(src0, src1, LEN);
  232. if (!float_near_abs_eps(cprod, oprod, ARBITRARY_SCALARPRODUCT_CONST)) {
  233. fprintf(stderr, "%- .12f - %- .12f = % .12g\n",
  234. cprod, oprod, cprod - oprod);
  235. fail();
  236. }
  237. bench_new(src0, src1, LEN);
  238. }
  239. void checkasm_check_float_dsp(void)
  240. {
  241. LOCAL_ALIGNED_32(float, src0, [LEN]);
  242. LOCAL_ALIGNED_32(float, src1, [LEN]);
  243. LOCAL_ALIGNED_32(float, src2, [LEN]);
  244. LOCAL_ALIGNED_16(float, src3, [LEN]);
  245. LOCAL_ALIGNED_16(float, src4, [LEN]);
  246. LOCAL_ALIGNED_16(float, src5, [LEN]);
  247. LOCAL_ALIGNED_32(double, dbl_src0, [LEN]);
  248. LOCAL_ALIGNED_32(double, dbl_src1, [LEN]);
  249. LOCAL_ALIGNED_32(double, dbl_src2, [LEN]);
  250. AVFloatDSPContext *fdsp = avpriv_float_dsp_alloc(1);
  251. if (!fdsp) {
  252. fprintf(stderr, "floatdsp: Out of memory error\n");
  253. return;
  254. }
  255. randomize_buffer(src0);
  256. randomize_buffer(src1);
  257. randomize_buffer(src2);
  258. randomize_buffer(src3);
  259. randomize_buffer(src4);
  260. randomize_buffer(src5);
  261. randomize_buffer(dbl_src0);
  262. randomize_buffer(dbl_src1);
  263. randomize_buffer(dbl_src2);
  264. if (check_func(fdsp->vector_fmul, "vector_fmul"))
  265. test_vector_fmul(src0, src1);
  266. if (check_func(fdsp->vector_fmul_add, "vector_fmul_add"))
  267. test_vector_fmul_add(src0, src1, src2);
  268. if (check_func(fdsp->vector_fmul_scalar, "vector_fmul_scalar"))
  269. test_vector_fmul_scalar(src3, src4);
  270. if (check_func(fdsp->vector_fmul_reverse, "vector_fmul_reverse"))
  271. test_vector_fmul(src0, src1);
  272. if (check_func(fdsp->vector_fmul_window, "vector_fmul_window"))
  273. test_vector_fmul_window(src3, src4, src5);
  274. report("vector_fmul");
  275. if (check_func(fdsp->vector_fmac_scalar, "vector_fmac_scalar"))
  276. test_vector_fmac_scalar(src0, src1, src2);
  277. report("vector_fmac");
  278. if (check_func(fdsp->vector_dmul, "vector_dmul"))
  279. test_vector_dmul(dbl_src0, dbl_src1);
  280. if (check_func(fdsp->vector_dmul_scalar, "vector_dmul_scalar"))
  281. test_vector_dmul_scalar(dbl_src0, dbl_src1);
  282. report("vector_dmul");
  283. if (check_func(fdsp->vector_dmac_scalar, "vector_dmac_scalar"))
  284. test_vector_dmac_scalar(dbl_src0, dbl_src1, dbl_src2);
  285. report("vector_dmac");
  286. if (check_func(fdsp->butterflies_float, "butterflies_float"))
  287. test_butterflies_float(src3, src4);
  288. report("butterflies_float");
  289. if (check_func(fdsp->scalarproduct_float, "scalarproduct_float"))
  290. test_scalarproduct_float(src3, src4);
  291. report("scalarproduct_float");
  292. av_freep(&fdsp);
  293. }