float_dsp.c 12 KB

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