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.

425 lines
12KB

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