You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

465 lines
13KB

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