float_dsp.c 12 KB

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