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.

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