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.

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