float_dsp.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. /*
  2. * Copyright 2005 Balatoni Denes
  3. * Copyright 2006 Loren Merritt
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "config.h"
  22. #include "attributes.h"
  23. #include "float_dsp.h"
  24. static void vector_fmul_c(float *dst, const float *src0, const float *src1,
  25. int len)
  26. {
  27. int i;
  28. for (i = 0; i < len; i++)
  29. dst[i] = src0[i] * src1[i];
  30. }
  31. static void vector_fmac_scalar_c(float *dst, const float *src, float mul,
  32. int len)
  33. {
  34. int i;
  35. for (i = 0; i < len; i++)
  36. dst[i] += src[i] * mul;
  37. }
  38. static void vector_fmul_scalar_c(float *dst, const float *src, float mul,
  39. int len)
  40. {
  41. int i;
  42. for (i = 0; i < len; i++)
  43. dst[i] = src[i] * mul;
  44. }
  45. static void vector_dmul_scalar_c(double *dst, const double *src, double mul,
  46. int len)
  47. {
  48. int i;
  49. for (i = 0; i < len; i++)
  50. dst[i] = src[i] * mul;
  51. }
  52. static void vector_fmul_window_c(float *dst, const float *src0,
  53. const float *src1, const float *win, int len)
  54. {
  55. int i, j;
  56. dst += len;
  57. win += len;
  58. src0 += len;
  59. for (i = -len, j = len - 1; i < 0; i++, j--) {
  60. float s0 = src0[i];
  61. float s1 = src1[j];
  62. float wi = win[i];
  63. float wj = win[j];
  64. dst[i] = s0 * wj - s1 * wi;
  65. dst[j] = s0 * wi + s1 * wj;
  66. }
  67. }
  68. static void vector_fmul_add_c(float *dst, const float *src0, const float *src1,
  69. const float *src2, int len){
  70. int i;
  71. for (i = 0; i < len; i++)
  72. dst[i] = src0[i] * src1[i] + src2[i];
  73. }
  74. static void vector_fmul_reverse_c(float *dst, const float *src0,
  75. const float *src1, int len)
  76. {
  77. int i;
  78. src1 += len-1;
  79. for (i = 0; i < len; i++)
  80. dst[i] = src0[i] * src1[-i];
  81. }
  82. static void butterflies_float_c(float *av_restrict v1, float *av_restrict v2,
  83. int len)
  84. {
  85. int i;
  86. for (i = 0; i < len; i++) {
  87. float t = v1[i] - v2[i];
  88. v1[i] += v2[i];
  89. v2[i] = t;
  90. }
  91. }
  92. float avpriv_scalarproduct_float_c(const float *v1, const float *v2, int len)
  93. {
  94. float p = 0.0;
  95. int i;
  96. for (i = 0; i < len; i++)
  97. p += v1[i] * v2[i];
  98. return p;
  99. }
  100. av_cold void avpriv_float_dsp_init(AVFloatDSPContext *fdsp, int bit_exact)
  101. {
  102. fdsp->vector_fmul = vector_fmul_c;
  103. fdsp->vector_fmac_scalar = vector_fmac_scalar_c;
  104. fdsp->vector_fmul_scalar = vector_fmul_scalar_c;
  105. fdsp->vector_dmul_scalar = vector_dmul_scalar_c;
  106. fdsp->vector_fmul_window = vector_fmul_window_c;
  107. fdsp->vector_fmul_add = vector_fmul_add_c;
  108. fdsp->vector_fmul_reverse = vector_fmul_reverse_c;
  109. fdsp->butterflies_float = butterflies_float_c;
  110. fdsp->scalarproduct_float = avpriv_scalarproduct_float_c;
  111. if (ARCH_AARCH64)
  112. ff_float_dsp_init_aarch64(fdsp);
  113. if (ARCH_ARM)
  114. ff_float_dsp_init_arm(fdsp);
  115. if (ARCH_PPC)
  116. ff_float_dsp_init_ppc(fdsp, bit_exact);
  117. if (ARCH_X86)
  118. ff_float_dsp_init_x86(fdsp);
  119. if (ARCH_MIPS)
  120. ff_float_dsp_init_mips(fdsp);
  121. }
  122. #ifdef TEST
  123. #include <float.h>
  124. #include <math.h>
  125. #include <stdint.h>
  126. #include <stdlib.h>
  127. #include <string.h>
  128. #include "common.h"
  129. #include "cpu.h"
  130. #include "internal.h"
  131. #include "lfg.h"
  132. #include "log.h"
  133. #include "mem.h"
  134. #include "random_seed.h"
  135. #define LEN 240
  136. static void fill_float_array(AVLFG *lfg, float *a, int len)
  137. {
  138. int i;
  139. double bmg[2], stddev = 10.0, mean = 0.0;
  140. for (i = 0; i < len; i += 2) {
  141. av_bmg_get(lfg, bmg);
  142. a[i] = bmg[0] * stddev + mean;
  143. a[i + 1] = bmg[1] * stddev + mean;
  144. }
  145. }
  146. static int compare_floats(const float *a, const float *b, int len,
  147. float max_diff)
  148. {
  149. int i;
  150. for (i = 0; i < len; i++) {
  151. if (fabsf(a[i] - b[i]) > max_diff) {
  152. av_log(NULL, AV_LOG_ERROR, "%d: %- .12f - %- .12f = % .12g\n",
  153. i, a[i], b[i], a[i] - b[i]);
  154. return -1;
  155. }
  156. }
  157. return 0;
  158. }
  159. static void fill_double_array(AVLFG *lfg, double *a, int len)
  160. {
  161. int i;
  162. double bmg[2], stddev = 10.0, mean = 0.0;
  163. for (i = 0; i < len; i += 2) {
  164. av_bmg_get(lfg, bmg);
  165. a[i] = bmg[0] * stddev + mean;
  166. a[i + 1] = bmg[1] * stddev + mean;
  167. }
  168. }
  169. static int compare_doubles(const double *a, const double *b, int len,
  170. double max_diff)
  171. {
  172. int i;
  173. for (i = 0; i < len; i++) {
  174. if (fabs(a[i] - b[i]) > max_diff) {
  175. av_log(NULL, AV_LOG_ERROR, "%d: %- .12f - %- .12f = % .12g\n",
  176. i, a[i], b[i], a[i] - b[i]);
  177. return -1;
  178. }
  179. }
  180. return 0;
  181. }
  182. static int test_vector_fmul(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp,
  183. const float *v1, const float *v2)
  184. {
  185. LOCAL_ALIGNED(32, float, cdst, [LEN]);
  186. LOCAL_ALIGNED(32, float, odst, [LEN]);
  187. int ret;
  188. cdsp->vector_fmul(cdst, v1, v2, LEN);
  189. fdsp->vector_fmul(odst, v1, v2, LEN);
  190. if (ret = compare_floats(cdst, odst, LEN, FLT_EPSILON))
  191. av_log(NULL, AV_LOG_ERROR, "vector_fmul failed\n");
  192. return ret;
  193. }
  194. #define ARBITRARY_FMAC_SCALAR_CONST 0.005
  195. static int test_vector_fmac_scalar(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp,
  196. const float *v1, const float *src0, float scale)
  197. {
  198. LOCAL_ALIGNED(32, float, cdst, [LEN]);
  199. LOCAL_ALIGNED(32, float, odst, [LEN]);
  200. int ret;
  201. memcpy(cdst, v1, LEN * sizeof(*v1));
  202. memcpy(odst, v1, LEN * sizeof(*v1));
  203. cdsp->vector_fmac_scalar(cdst, src0, scale, LEN);
  204. fdsp->vector_fmac_scalar(odst, src0, scale, LEN);
  205. if (ret = compare_floats(cdst, odst, LEN, ARBITRARY_FMAC_SCALAR_CONST))
  206. av_log(NULL, AV_LOG_ERROR, "vector_fmac_scalar failed\n");
  207. return ret;
  208. }
  209. static int test_vector_fmul_scalar(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp,
  210. const float *v1, float scale)
  211. {
  212. LOCAL_ALIGNED(32, float, cdst, [LEN]);
  213. LOCAL_ALIGNED(32, float, odst, [LEN]);
  214. int ret;
  215. cdsp->vector_fmul_scalar(cdst, v1, scale, LEN);
  216. fdsp->vector_fmul_scalar(odst, v1, scale, LEN);
  217. if (ret = compare_floats(cdst, odst, LEN, FLT_EPSILON))
  218. av_log(NULL, AV_LOG_ERROR, "vector_fmul_scalar failed\n");
  219. return ret;
  220. }
  221. static int test_vector_dmul_scalar(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp,
  222. const double *v1, double scale)
  223. {
  224. LOCAL_ALIGNED(32, double, cdst, [LEN]);
  225. LOCAL_ALIGNED(32, double, odst, [LEN]);
  226. int ret;
  227. cdsp->vector_dmul_scalar(cdst, v1, scale, LEN);
  228. fdsp->vector_dmul_scalar(odst, v1, scale, LEN);
  229. if (ret = compare_doubles(cdst, odst, LEN, DBL_EPSILON))
  230. av_log(NULL, AV_LOG_ERROR, "vector_dmul_scalar failed\n");
  231. return ret;
  232. }
  233. #define ARBITRARY_FMUL_WINDOW_CONST 0.008
  234. static int test_vector_fmul_window(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp,
  235. const float *v1, const float *v2, const float *v3)
  236. {
  237. LOCAL_ALIGNED(32, float, cdst, [LEN]);
  238. LOCAL_ALIGNED(32, float, odst, [LEN]);
  239. int ret;
  240. cdsp->vector_fmul_window(cdst, v1, v2, v3, LEN / 2);
  241. fdsp->vector_fmul_window(odst, v1, v2, v3, LEN / 2);
  242. if (ret = compare_floats(cdst, odst, LEN, ARBITRARY_FMUL_WINDOW_CONST))
  243. av_log(NULL, AV_LOG_ERROR, "vector_fmul_window failed\n");
  244. return ret;
  245. }
  246. #define ARBITRARY_FMUL_ADD_CONST 0.005
  247. static int test_vector_fmul_add(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp,
  248. const float *v1, const float *v2, const float *v3)
  249. {
  250. LOCAL_ALIGNED(32, float, cdst, [LEN]);
  251. LOCAL_ALIGNED(32, float, odst, [LEN]);
  252. int ret;
  253. cdsp->vector_fmul_add(cdst, v1, v2, v3, LEN);
  254. fdsp->vector_fmul_add(odst, v1, v2, v3, LEN);
  255. if (ret = compare_floats(cdst, odst, LEN, ARBITRARY_FMUL_ADD_CONST))
  256. av_log(NULL, AV_LOG_ERROR, "vector_fmul_add failed\n");
  257. return ret;
  258. }
  259. static int test_vector_fmul_reverse(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp,
  260. const float *v1, const float *v2)
  261. {
  262. LOCAL_ALIGNED(32, float, cdst, [LEN]);
  263. LOCAL_ALIGNED(32, float, odst, [LEN]);
  264. int ret;
  265. cdsp->vector_fmul_reverse(cdst, v1, v2, LEN);
  266. fdsp->vector_fmul_reverse(odst, v1, v2, LEN);
  267. if (ret = compare_floats(cdst, odst, LEN, FLT_EPSILON))
  268. av_log(NULL, AV_LOG_ERROR, "vector_fmul_reverse failed\n");
  269. return ret;
  270. }
  271. static int test_butterflies_float(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp,
  272. const float *v1, const float *v2)
  273. {
  274. LOCAL_ALIGNED(32, float, cv1, [LEN]);
  275. LOCAL_ALIGNED(32, float, cv2, [LEN]);
  276. LOCAL_ALIGNED(32, float, ov1, [LEN]);
  277. LOCAL_ALIGNED(32, float, ov2, [LEN]);
  278. int ret;
  279. memcpy(cv1, v1, LEN * sizeof(*v1));
  280. memcpy(cv2, v2, LEN * sizeof(*v2));
  281. memcpy(ov1, v1, LEN * sizeof(*v1));
  282. memcpy(ov2, v2, LEN * sizeof(*v2));
  283. cdsp->butterflies_float(cv1, cv2, LEN);
  284. fdsp->butterflies_float(ov1, ov2, LEN);
  285. if ((ret = compare_floats(cv1, ov1, LEN, FLT_EPSILON)) ||
  286. (ret = compare_floats(cv2, ov2, LEN, FLT_EPSILON)))
  287. av_log(NULL, AV_LOG_ERROR, "butterflies_float failed\n");
  288. return ret;
  289. }
  290. #define ARBITRARY_SCALARPRODUCT_CONST 0.2
  291. static int test_scalarproduct_float(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp,
  292. const float *v1, const float *v2)
  293. {
  294. float cprod, oprod;
  295. int ret;
  296. cprod = cdsp->scalarproduct_float(v1, v2, LEN);
  297. oprod = fdsp->scalarproduct_float(v1, v2, LEN);
  298. if (ret = compare_floats(&cprod, &oprod, 1, ARBITRARY_SCALARPRODUCT_CONST))
  299. av_log(NULL, AV_LOG_ERROR, "scalarproduct_float failed\n");
  300. return ret;
  301. }
  302. int main(int argc, char **argv)
  303. {
  304. int ret = 0;
  305. uint32_t seed;
  306. AVFloatDSPContext fdsp, cdsp;
  307. AVLFG lfg;
  308. LOCAL_ALIGNED(32, float, src0, [LEN]);
  309. LOCAL_ALIGNED(32, float, src1, [LEN]);
  310. LOCAL_ALIGNED(32, float, src2, [LEN]);
  311. LOCAL_ALIGNED(32, double, dbl_src0, [LEN]);
  312. LOCAL_ALIGNED(32, double, dbl_src1, [LEN]);
  313. if (argc > 2 && !strcmp(argv[1], "-s"))
  314. seed = strtoul(argv[2], NULL, 10);
  315. else
  316. seed = av_get_random_seed();
  317. av_log(NULL, AV_LOG_INFO, "float_dsp-test: random seed %u\n", seed);
  318. av_lfg_init(&lfg, seed);
  319. fill_float_array(&lfg, src0, LEN);
  320. fill_float_array(&lfg, src1, LEN);
  321. fill_float_array(&lfg, src2, LEN);
  322. fill_double_array(&lfg, dbl_src0, LEN);
  323. fill_double_array(&lfg, dbl_src1, LEN);
  324. avpriv_float_dsp_init(&fdsp, 1);
  325. av_set_cpu_flags_mask(0);
  326. avpriv_float_dsp_init(&cdsp, 1);
  327. if (test_vector_fmul(&fdsp, &cdsp, src0, src1))
  328. ret -= 1 << 0;
  329. if (test_vector_fmac_scalar(&fdsp, &cdsp, src2, src0, src1[0]))
  330. ret -= 1 << 1;
  331. if (test_vector_fmul_scalar(&fdsp, &cdsp, src0, src1[0]))
  332. ret -= 1 << 2;
  333. if (test_vector_fmul_window(&fdsp, &cdsp, src0, src1, src2))
  334. ret -= 1 << 3;
  335. if (test_vector_fmul_add(&fdsp, &cdsp, src0, src1, src2))
  336. ret -= 1 << 4;
  337. if (test_vector_fmul_reverse(&fdsp, &cdsp, src0, src1))
  338. ret -= 1 << 5;
  339. if (test_butterflies_float(&fdsp, &cdsp, src0, src1))
  340. ret -= 1 << 6;
  341. if (test_scalarproduct_float(&fdsp, &cdsp, src0, src1))
  342. ret -= 1 << 7;
  343. if (test_vector_dmul_scalar(&fdsp, &cdsp, dbl_src0, dbl_src1[0]))
  344. ret -= 1 << 8;
  345. return ret;
  346. }
  347. #endif /* TEST */