float_dsp.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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. #include "mem.h"
  25. static void vector_fmul_c(float *dst, const float *src0, const float *src1,
  26. int len)
  27. {
  28. int i;
  29. for (i = 0; i < len; i++)
  30. dst[i] = src0[i] * src1[i];
  31. }
  32. static void vector_fmac_scalar_c(float *dst, const float *src, float mul,
  33. int len)
  34. {
  35. int i;
  36. for (i = 0; i < len; i++)
  37. dst[i] += src[i] * mul;
  38. }
  39. static void vector_fmul_scalar_c(float *dst, const float *src, float mul,
  40. int len)
  41. {
  42. int i;
  43. for (i = 0; i < len; i++)
  44. dst[i] = src[i] * mul;
  45. }
  46. static void vector_dmul_scalar_c(double *dst, const double *src, double mul,
  47. int len)
  48. {
  49. int i;
  50. for (i = 0; i < len; i++)
  51. dst[i] = src[i] * mul;
  52. }
  53. static void vector_fmul_window_c(float *dst, const float *src0,
  54. const float *src1, const float *win, int len)
  55. {
  56. int i, j;
  57. dst += len;
  58. win += len;
  59. src0 += len;
  60. for (i = -len, j = len - 1; i < 0; i++, j--) {
  61. float s0 = src0[i];
  62. float s1 = src1[j];
  63. float wi = win[i];
  64. float wj = win[j];
  65. dst[i] = s0 * wj - s1 * wi;
  66. dst[j] = s0 * wi + s1 * wj;
  67. }
  68. }
  69. static void vector_fmul_add_c(float *dst, const float *src0, const float *src1,
  70. const float *src2, int len){
  71. int i;
  72. for (i = 0; i < len; i++)
  73. dst[i] = src0[i] * src1[i] + src2[i];
  74. }
  75. static void vector_fmul_reverse_c(float *dst, const float *src0,
  76. const float *src1, int len)
  77. {
  78. int i;
  79. src1 += len-1;
  80. for (i = 0; i < len; i++)
  81. dst[i] = src0[i] * src1[-i];
  82. }
  83. static void butterflies_float_c(float *av_restrict v1, float *av_restrict v2,
  84. int len)
  85. {
  86. int i;
  87. for (i = 0; i < len; i++) {
  88. float t = v1[i] - v2[i];
  89. v1[i] += v2[i];
  90. v2[i] = t;
  91. }
  92. }
  93. float avpriv_scalarproduct_float_c(const float *v1, const float *v2, int len)
  94. {
  95. float p = 0.0;
  96. int i;
  97. for (i = 0; i < len; i++)
  98. p += v1[i] * v2[i];
  99. return p;
  100. }
  101. av_cold void avpriv_float_dsp_init(AVFloatDSPContext *fdsp, int bit_exact)
  102. {
  103. fdsp->vector_fmul = vector_fmul_c;
  104. fdsp->vector_fmac_scalar = vector_fmac_scalar_c;
  105. fdsp->vector_fmul_scalar = vector_fmul_scalar_c;
  106. fdsp->vector_dmul_scalar = vector_dmul_scalar_c;
  107. fdsp->vector_fmul_window = vector_fmul_window_c;
  108. fdsp->vector_fmul_add = vector_fmul_add_c;
  109. fdsp->vector_fmul_reverse = vector_fmul_reverse_c;
  110. fdsp->butterflies_float = butterflies_float_c;
  111. fdsp->scalarproduct_float = avpriv_scalarproduct_float_c;
  112. if (ARCH_AARCH64)
  113. ff_float_dsp_init_aarch64(fdsp);
  114. if (ARCH_ARM)
  115. ff_float_dsp_init_arm(fdsp);
  116. if (ARCH_PPC)
  117. ff_float_dsp_init_ppc(fdsp, bit_exact);
  118. if (ARCH_X86)
  119. ff_float_dsp_init_x86(fdsp);
  120. if (ARCH_MIPS)
  121. ff_float_dsp_init_mips(fdsp);
  122. }
  123. av_cold AVFloatDSPContext *avpriv_float_dsp_alloc(int bit_exact)
  124. {
  125. AVFloatDSPContext *ret = av_mallocz(sizeof(AVFloatDSPContext));
  126. if (ret)
  127. avpriv_float_dsp_init(ret, bit_exact);
  128. return ret;
  129. }
  130. #ifdef TEST
  131. #include <float.h>
  132. #include <math.h>
  133. #include <stdint.h>
  134. #include <stdlib.h>
  135. #include <string.h>
  136. #if HAVE_UNISTD_H
  137. #include <unistd.h> /* for getopt */
  138. #endif
  139. #if !HAVE_GETOPT
  140. #include "compat/getopt.c"
  141. #endif
  142. #include "common.h"
  143. #include "cpu.h"
  144. #include "internal.h"
  145. #include "lfg.h"
  146. #include "log.h"
  147. #include "random_seed.h"
  148. #define LEN 240
  149. static void fill_float_array(AVLFG *lfg, float *a, int len)
  150. {
  151. int i;
  152. double bmg[2], stddev = 10.0, mean = 0.0;
  153. for (i = 0; i < len; i += 2) {
  154. av_bmg_get(lfg, bmg);
  155. a[i] = bmg[0] * stddev + mean;
  156. a[i + 1] = bmg[1] * stddev + mean;
  157. }
  158. }
  159. static int compare_floats(const float *a, const float *b, int len,
  160. float max_diff)
  161. {
  162. int i;
  163. for (i = 0; i < len; i++) {
  164. if (fabsf(a[i] - b[i]) > max_diff) {
  165. av_log(NULL, AV_LOG_ERROR, "%d: %- .12f - %- .12f = % .12g\n",
  166. i, a[i], b[i], a[i] - b[i]);
  167. return -1;
  168. }
  169. }
  170. return 0;
  171. }
  172. static void fill_double_array(AVLFG *lfg, double *a, int len)
  173. {
  174. int i;
  175. double bmg[2], stddev = 10.0, mean = 0.0;
  176. for (i = 0; i < len; i += 2) {
  177. av_bmg_get(lfg, bmg);
  178. a[i] = bmg[0] * stddev + mean;
  179. a[i + 1] = bmg[1] * stddev + mean;
  180. }
  181. }
  182. static int compare_doubles(const double *a, const double *b, int len,
  183. double max_diff)
  184. {
  185. int i;
  186. for (i = 0; i < len; i++) {
  187. if (fabs(a[i] - b[i]) > max_diff) {
  188. av_log(NULL, AV_LOG_ERROR, "%d: %- .12f - %- .12f = % .12g\n",
  189. i, a[i], b[i], a[i] - b[i]);
  190. return -1;
  191. }
  192. }
  193. return 0;
  194. }
  195. static int test_vector_fmul(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp,
  196. const float *v1, const float *v2)
  197. {
  198. LOCAL_ALIGNED(32, float, cdst, [LEN]);
  199. LOCAL_ALIGNED(32, float, odst, [LEN]);
  200. int ret;
  201. cdsp->vector_fmul(cdst, v1, v2, LEN);
  202. fdsp->vector_fmul(odst, v1, v2, LEN);
  203. if (ret = compare_floats(cdst, odst, LEN, FLT_EPSILON))
  204. av_log(NULL, AV_LOG_ERROR, "vector_fmul failed\n");
  205. return ret;
  206. }
  207. #define ARBITRARY_FMAC_SCALAR_CONST 0.005
  208. static int test_vector_fmac_scalar(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp,
  209. const float *v1, const float *src0, float scale)
  210. {
  211. LOCAL_ALIGNED(32, float, cdst, [LEN]);
  212. LOCAL_ALIGNED(32, float, odst, [LEN]);
  213. int ret;
  214. memcpy(cdst, v1, LEN * sizeof(*v1));
  215. memcpy(odst, v1, LEN * sizeof(*v1));
  216. cdsp->vector_fmac_scalar(cdst, src0, scale, LEN);
  217. fdsp->vector_fmac_scalar(odst, src0, scale, LEN);
  218. if (ret = compare_floats(cdst, odst, LEN, ARBITRARY_FMAC_SCALAR_CONST))
  219. av_log(NULL, AV_LOG_ERROR, "vector_fmac_scalar failed\n");
  220. return ret;
  221. }
  222. static int test_vector_fmul_scalar(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp,
  223. const float *v1, float scale)
  224. {
  225. LOCAL_ALIGNED(32, float, cdst, [LEN]);
  226. LOCAL_ALIGNED(32, float, odst, [LEN]);
  227. int ret;
  228. cdsp->vector_fmul_scalar(cdst, v1, scale, LEN);
  229. fdsp->vector_fmul_scalar(odst, v1, scale, LEN);
  230. if (ret = compare_floats(cdst, odst, LEN, FLT_EPSILON))
  231. av_log(NULL, AV_LOG_ERROR, "vector_fmul_scalar failed\n");
  232. return ret;
  233. }
  234. static int test_vector_dmul_scalar(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp,
  235. const double *v1, double scale)
  236. {
  237. LOCAL_ALIGNED(32, double, cdst, [LEN]);
  238. LOCAL_ALIGNED(32, double, odst, [LEN]);
  239. int ret;
  240. cdsp->vector_dmul_scalar(cdst, v1, scale, LEN);
  241. fdsp->vector_dmul_scalar(odst, v1, scale, LEN);
  242. if (ret = compare_doubles(cdst, odst, LEN, DBL_EPSILON))
  243. av_log(NULL, AV_LOG_ERROR, "vector_dmul_scalar failed\n");
  244. return ret;
  245. }
  246. #define ARBITRARY_FMUL_WINDOW_CONST 0.008
  247. static int test_vector_fmul_window(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_window(cdst, v1, v2, v3, LEN / 2);
  254. fdsp->vector_fmul_window(odst, v1, v2, v3, LEN / 2);
  255. if (ret = compare_floats(cdst, odst, LEN, ARBITRARY_FMUL_WINDOW_CONST))
  256. av_log(NULL, AV_LOG_ERROR, "vector_fmul_window failed\n");
  257. return ret;
  258. }
  259. #define ARBITRARY_FMUL_ADD_CONST 0.005
  260. static int test_vector_fmul_add(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp,
  261. const float *v1, const float *v2, const float *v3)
  262. {
  263. LOCAL_ALIGNED(32, float, cdst, [LEN]);
  264. LOCAL_ALIGNED(32, float, odst, [LEN]);
  265. int ret;
  266. cdsp->vector_fmul_add(cdst, v1, v2, v3, LEN);
  267. fdsp->vector_fmul_add(odst, v1, v2, v3, LEN);
  268. if (ret = compare_floats(cdst, odst, LEN, ARBITRARY_FMUL_ADD_CONST))
  269. av_log(NULL, AV_LOG_ERROR, "vector_fmul_add failed\n");
  270. return ret;
  271. }
  272. static int test_vector_fmul_reverse(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp,
  273. const float *v1, const float *v2)
  274. {
  275. LOCAL_ALIGNED(32, float, cdst, [LEN]);
  276. LOCAL_ALIGNED(32, float, odst, [LEN]);
  277. int ret;
  278. cdsp->vector_fmul_reverse(cdst, v1, v2, LEN);
  279. fdsp->vector_fmul_reverse(odst, v1, v2, LEN);
  280. if (ret = compare_floats(cdst, odst, LEN, FLT_EPSILON))
  281. av_log(NULL, AV_LOG_ERROR, "vector_fmul_reverse failed\n");
  282. return ret;
  283. }
  284. static int test_butterflies_float(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp,
  285. const float *v1, const float *v2)
  286. {
  287. LOCAL_ALIGNED(32, float, cv1, [LEN]);
  288. LOCAL_ALIGNED(32, float, cv2, [LEN]);
  289. LOCAL_ALIGNED(32, float, ov1, [LEN]);
  290. LOCAL_ALIGNED(32, float, ov2, [LEN]);
  291. int ret;
  292. memcpy(cv1, v1, LEN * sizeof(*v1));
  293. memcpy(cv2, v2, LEN * sizeof(*v2));
  294. memcpy(ov1, v1, LEN * sizeof(*v1));
  295. memcpy(ov2, v2, LEN * sizeof(*v2));
  296. cdsp->butterflies_float(cv1, cv2, LEN);
  297. fdsp->butterflies_float(ov1, ov2, LEN);
  298. if ((ret = compare_floats(cv1, ov1, LEN, FLT_EPSILON)) ||
  299. (ret = compare_floats(cv2, ov2, LEN, FLT_EPSILON)))
  300. av_log(NULL, AV_LOG_ERROR, "butterflies_float failed\n");
  301. return ret;
  302. }
  303. #define ARBITRARY_SCALARPRODUCT_CONST 0.2
  304. static int test_scalarproduct_float(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp,
  305. const float *v1, const float *v2)
  306. {
  307. float cprod, oprod;
  308. int ret;
  309. cprod = cdsp->scalarproduct_float(v1, v2, LEN);
  310. oprod = fdsp->scalarproduct_float(v1, v2, LEN);
  311. if (ret = compare_floats(&cprod, &oprod, 1, ARBITRARY_SCALARPRODUCT_CONST))
  312. av_log(NULL, AV_LOG_ERROR, "scalarproduct_float failed\n");
  313. return ret;
  314. }
  315. int main(int argc, char **argv)
  316. {
  317. int ret = 0, seeded = 0;
  318. uint32_t seed;
  319. AVFloatDSPContext fdsp, cdsp;
  320. AVLFG lfg;
  321. LOCAL_ALIGNED(32, float, src0, [LEN]);
  322. LOCAL_ALIGNED(32, float, src1, [LEN]);
  323. LOCAL_ALIGNED(32, float, src2, [LEN]);
  324. LOCAL_ALIGNED(32, double, dbl_src0, [LEN]);
  325. LOCAL_ALIGNED(32, double, dbl_src1, [LEN]);
  326. for (;;) {
  327. int arg = getopt(argc, argv, "s:c:");
  328. if (arg == -1)
  329. break;
  330. switch (arg) {
  331. case 's':
  332. seed = strtoul(optarg, NULL, 10);
  333. seeded = 1;
  334. break;
  335. case 'c':
  336. {
  337. int cpuflags = av_get_cpu_flags();
  338. if (av_parse_cpu_caps(&cpuflags, optarg) < 0)
  339. return 1;
  340. av_force_cpu_flags(cpuflags);
  341. break;
  342. }
  343. }
  344. }
  345. if (!seeded)
  346. seed = av_get_random_seed();
  347. av_log(NULL, AV_LOG_INFO, "float_dsp-test: %s %u\n", seeded ? "seed" : "random seed", seed);
  348. av_lfg_init(&lfg, seed);
  349. fill_float_array(&lfg, src0, LEN);
  350. fill_float_array(&lfg, src1, LEN);
  351. fill_float_array(&lfg, src2, LEN);
  352. fill_double_array(&lfg, dbl_src0, LEN);
  353. fill_double_array(&lfg, dbl_src1, LEN);
  354. avpriv_float_dsp_init(&fdsp, 1);
  355. av_set_cpu_flags_mask(0);
  356. avpriv_float_dsp_init(&cdsp, 1);
  357. if (test_vector_fmul(&fdsp, &cdsp, src0, src1))
  358. ret -= 1 << 0;
  359. if (test_vector_fmac_scalar(&fdsp, &cdsp, src2, src0, src1[0]))
  360. ret -= 1 << 1;
  361. if (test_vector_fmul_scalar(&fdsp, &cdsp, src0, src1[0]))
  362. ret -= 1 << 2;
  363. if (test_vector_fmul_window(&fdsp, &cdsp, src0, src1, src2))
  364. ret -= 1 << 3;
  365. if (test_vector_fmul_add(&fdsp, &cdsp, src0, src1, src2))
  366. ret -= 1 << 4;
  367. if (test_vector_fmul_reverse(&fdsp, &cdsp, src0, src1))
  368. ret -= 1 << 5;
  369. if (test_butterflies_float(&fdsp, &cdsp, src0, src1))
  370. ret -= 1 << 6;
  371. if (test_scalarproduct_float(&fdsp, &cdsp, src0, src1))
  372. ret -= 1 << 7;
  373. if (test_vector_dmul_scalar(&fdsp, &cdsp, dbl_src0, dbl_src1[0]))
  374. ret -= 1 << 8;
  375. return ret;
  376. }
  377. #endif /* TEST */