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.

421 lines
12KB

  1. /*
  2. * This file is part of Libav.
  3. *
  4. * Libav is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * Libav is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with Libav; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "config.h"
  19. #include "attributes.h"
  20. #include "float_dsp.h"
  21. static void vector_fmul_c(float *dst, const float *src0, const float *src1,
  22. int len)
  23. {
  24. int i;
  25. for (i = 0; i < len; i++)
  26. dst[i] = src0[i] * src1[i];
  27. }
  28. static void vector_fmac_scalar_c(float *dst, const float *src, float mul,
  29. int len)
  30. {
  31. int i;
  32. for (i = 0; i < len; i++)
  33. dst[i] += src[i] * mul;
  34. }
  35. static void vector_fmul_scalar_c(float *dst, const float *src, float mul,
  36. int len)
  37. {
  38. int i;
  39. for (i = 0; i < len; i++)
  40. dst[i] = src[i] * mul;
  41. }
  42. static void vector_dmul_scalar_c(double *dst, const double *src, double mul,
  43. int len)
  44. {
  45. int i;
  46. for (i = 0; i < len; i++)
  47. dst[i] = src[i] * mul;
  48. }
  49. static void vector_fmul_window_c(float *dst, const float *src0,
  50. const float *src1, const float *win, int len)
  51. {
  52. int i, j;
  53. dst += len;
  54. win += len;
  55. src0 += len;
  56. for (i = -len, j = len - 1; i < 0; i++, j--) {
  57. float s0 = src0[i];
  58. float s1 = src1[j];
  59. float wi = win[i];
  60. float wj = win[j];
  61. dst[i] = s0 * wj - s1 * wi;
  62. dst[j] = s0 * wi + s1 * wj;
  63. }
  64. }
  65. static void vector_fmul_add_c(float *dst, const float *src0, const float *src1,
  66. const float *src2, int len){
  67. int i;
  68. for (i = 0; i < len; i++)
  69. dst[i] = src0[i] * src1[i] + src2[i];
  70. }
  71. static void vector_fmul_reverse_c(float *dst, const float *src0,
  72. const float *src1, int len)
  73. {
  74. int i;
  75. src1 += len-1;
  76. for (i = 0; i < len; i++)
  77. dst[i] = src0[i] * src1[-i];
  78. }
  79. static void butterflies_float_c(float *restrict v1, float *restrict v2,
  80. int len)
  81. {
  82. int i;
  83. for (i = 0; i < len; i++) {
  84. float t = v1[i] - v2[i];
  85. v1[i] += v2[i];
  86. v2[i] = t;
  87. }
  88. }
  89. float avpriv_scalarproduct_float_c(const float *v1, const float *v2, int len)
  90. {
  91. float p = 0.0;
  92. int i;
  93. for (i = 0; i < len; i++)
  94. p += v1[i] * v2[i];
  95. return p;
  96. }
  97. av_cold void avpriv_float_dsp_init(AVFloatDSPContext *fdsp, int bit_exact)
  98. {
  99. fdsp->vector_fmul = vector_fmul_c;
  100. fdsp->vector_fmac_scalar = vector_fmac_scalar_c;
  101. fdsp->vector_fmul_scalar = vector_fmul_scalar_c;
  102. fdsp->vector_dmul_scalar = vector_dmul_scalar_c;
  103. fdsp->vector_fmul_window = vector_fmul_window_c;
  104. fdsp->vector_fmul_add = vector_fmul_add_c;
  105. fdsp->vector_fmul_reverse = vector_fmul_reverse_c;
  106. fdsp->butterflies_float = butterflies_float_c;
  107. fdsp->scalarproduct_float = avpriv_scalarproduct_float_c;
  108. if (ARCH_AARCH64)
  109. ff_float_dsp_init_aarch64(fdsp);
  110. if (ARCH_ARM)
  111. ff_float_dsp_init_arm(fdsp);
  112. if (ARCH_PPC)
  113. ff_float_dsp_init_ppc(fdsp, bit_exact);
  114. if (ARCH_X86)
  115. ff_float_dsp_init_x86(fdsp);
  116. }
  117. #ifdef TEST
  118. #include <float.h>
  119. #include <math.h>
  120. #include <stdint.h>
  121. #include <stdlib.h>
  122. #include <string.h>
  123. #include "common.h"
  124. #include "cpu.h"
  125. #include "internal.h"
  126. #include "lfg.h"
  127. #include "log.h"
  128. #include "mem.h"
  129. #include "random_seed.h"
  130. #define LEN 240
  131. static void fill_float_array(AVLFG *lfg, float *a, int len)
  132. {
  133. int i;
  134. double bmg[2], stddev = 10.0, mean = 0.0;
  135. for (i = 0; i < len; i += 2) {
  136. av_bmg_get(lfg, bmg);
  137. a[i] = bmg[0] * stddev + mean;
  138. a[i + 1] = bmg[1] * stddev + mean;
  139. }
  140. }
  141. static int compare_floats(const float *a, const float *b, int len,
  142. float max_diff)
  143. {
  144. int i;
  145. for (i = 0; i < len; i++) {
  146. if (fabsf(a[i] - b[i]) > max_diff) {
  147. av_log(NULL, AV_LOG_ERROR, "%d: %- .12f - %- .12f = % .12g\n",
  148. i, a[i], b[i], a[i] - b[i]);
  149. return -1;
  150. }
  151. }
  152. return 0;
  153. }
  154. static void fill_double_array(AVLFG *lfg, double *a, int len)
  155. {
  156. int i;
  157. double bmg[2], stddev = 10.0, mean = 0.0;
  158. for (i = 0; i < len; i += 2) {
  159. av_bmg_get(lfg, bmg);
  160. a[i] = bmg[0] * stddev + mean;
  161. a[i + 1] = bmg[1] * stddev + mean;
  162. }
  163. }
  164. static int compare_doubles(const double *a, const double *b, int len,
  165. double max_diff)
  166. {
  167. int i;
  168. for (i = 0; i < len; i++) {
  169. if (fabs(a[i] - b[i]) > max_diff) {
  170. av_log(NULL, AV_LOG_ERROR, "%d: %- .12f - %- .12f = % .12g\n",
  171. i, a[i], b[i], a[i] - b[i]);
  172. return -1;
  173. }
  174. }
  175. return 0;
  176. }
  177. static int test_vector_fmul(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp,
  178. const float *v1, const float *v2)
  179. {
  180. LOCAL_ALIGNED(32, float, cdst, [LEN]);
  181. LOCAL_ALIGNED(32, float, odst, [LEN]);
  182. int ret;
  183. cdsp->vector_fmul(cdst, v1, v2, LEN);
  184. fdsp->vector_fmul(odst, v1, v2, LEN);
  185. if (ret = compare_floats(cdst, odst, LEN, FLT_EPSILON))
  186. av_log(NULL, AV_LOG_ERROR, "vector_fmul failed\n");
  187. return ret;
  188. }
  189. #define ARBITRARY_FMAC_SCALAR_CONST 0.005
  190. static int test_vector_fmac_scalar(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp,
  191. const float *v1, const float *src0, float scale)
  192. {
  193. LOCAL_ALIGNED(32, float, cdst, [LEN]);
  194. LOCAL_ALIGNED(32, float, odst, [LEN]);
  195. int ret;
  196. memcpy(cdst, v1, LEN * sizeof(*v1));
  197. memcpy(odst, v1, LEN * sizeof(*v1));
  198. cdsp->vector_fmac_scalar(cdst, src0, scale, LEN);
  199. fdsp->vector_fmac_scalar(odst, src0, scale, LEN);
  200. if (ret = compare_floats(cdst, odst, LEN, ARBITRARY_FMAC_SCALAR_CONST))
  201. av_log(NULL, AV_LOG_ERROR, "vector_fmac_scalar failed\n");
  202. return ret;
  203. }
  204. static int test_vector_fmul_scalar(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp,
  205. const float *v1, float scale)
  206. {
  207. LOCAL_ALIGNED(32, float, cdst, [LEN]);
  208. LOCAL_ALIGNED(32, float, odst, [LEN]);
  209. int ret;
  210. cdsp->vector_fmul_scalar(cdst, v1, scale, LEN);
  211. fdsp->vector_fmul_scalar(odst, v1, scale, LEN);
  212. if (ret = compare_floats(cdst, odst, LEN, FLT_EPSILON))
  213. av_log(NULL, AV_LOG_ERROR, "vector_fmul_scalar failed\n");
  214. return ret;
  215. }
  216. static int test_vector_dmul_scalar(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp,
  217. const double *v1, double scale)
  218. {
  219. LOCAL_ALIGNED(32, double, cdst, [LEN]);
  220. LOCAL_ALIGNED(32, double, odst, [LEN]);
  221. int ret;
  222. cdsp->vector_dmul_scalar(cdst, v1, scale, LEN);
  223. fdsp->vector_dmul_scalar(odst, v1, scale, LEN);
  224. if (ret = compare_doubles(cdst, odst, LEN, DBL_EPSILON))
  225. av_log(NULL, AV_LOG_ERROR, "vector_dmul_scalar failed\n");
  226. return ret;
  227. }
  228. #define ARBITRARY_FMUL_WINDOW_CONST 0.008
  229. static int test_vector_fmul_window(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp,
  230. const float *v1, const float *v2, const float *v3)
  231. {
  232. LOCAL_ALIGNED(32, float, cdst, [LEN]);
  233. LOCAL_ALIGNED(32, float, odst, [LEN]);
  234. int ret;
  235. cdsp->vector_fmul_window(cdst, v1, v2, v3, LEN / 2);
  236. fdsp->vector_fmul_window(odst, v1, v2, v3, LEN / 2);
  237. if (ret = compare_floats(cdst, odst, LEN, ARBITRARY_FMUL_WINDOW_CONST))
  238. av_log(NULL, AV_LOG_ERROR, "vector_fmul_window failed\n");
  239. return ret;
  240. }
  241. #define ARBITRARY_FMUL_ADD_CONST 0.005
  242. static int test_vector_fmul_add(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp,
  243. const float *v1, const float *v2, const float *v3)
  244. {
  245. LOCAL_ALIGNED(32, float, cdst, [LEN]);
  246. LOCAL_ALIGNED(32, float, odst, [LEN]);
  247. int ret;
  248. cdsp->vector_fmul_add(cdst, v1, v2, v3, LEN);
  249. fdsp->vector_fmul_add(odst, v1, v2, v3, LEN);
  250. if (ret = compare_floats(cdst, odst, LEN, ARBITRARY_FMUL_ADD_CONST))
  251. av_log(NULL, AV_LOG_ERROR, "vector_fmul_add failed\n");
  252. return ret;
  253. }
  254. static int test_vector_fmul_reverse(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp,
  255. const float *v1, const float *v2)
  256. {
  257. LOCAL_ALIGNED(32, float, cdst, [LEN]);
  258. LOCAL_ALIGNED(32, float, odst, [LEN]);
  259. int ret;
  260. cdsp->vector_fmul_reverse(cdst, v1, v2, LEN);
  261. fdsp->vector_fmul_reverse(odst, v1, v2, LEN);
  262. if (ret = compare_floats(cdst, odst, LEN, FLT_EPSILON))
  263. av_log(NULL, AV_LOG_ERROR, "vector_fmul_reverse failed\n");
  264. return ret;
  265. }
  266. static int test_butterflies_float(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp,
  267. const float *v1, const float *v2)
  268. {
  269. LOCAL_ALIGNED(32, float, cv1, [LEN]);
  270. LOCAL_ALIGNED(32, float, cv2, [LEN]);
  271. LOCAL_ALIGNED(32, float, ov1, [LEN]);
  272. LOCAL_ALIGNED(32, float, ov2, [LEN]);
  273. int ret;
  274. memcpy(cv1, v1, LEN * sizeof(*v1));
  275. memcpy(cv2, v2, LEN * sizeof(*v2));
  276. memcpy(ov1, v1, LEN * sizeof(*v1));
  277. memcpy(ov2, v2, LEN * sizeof(*v2));
  278. cdsp->butterflies_float(cv1, cv2, LEN);
  279. fdsp->butterflies_float(ov1, ov2, LEN);
  280. if ((ret = compare_floats(cv1, ov1, LEN, FLT_EPSILON)) ||
  281. (ret = compare_floats(cv2, ov2, LEN, FLT_EPSILON)))
  282. av_log(NULL, AV_LOG_ERROR, "butterflies_float failed\n");
  283. return ret;
  284. }
  285. #define ARBITRARY_SCALARPRODUCT_CONST 0.2
  286. static int test_scalarproduct_float(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp,
  287. const float *v1, const float *v2)
  288. {
  289. float cprod, oprod;
  290. int ret;
  291. cprod = cdsp->scalarproduct_float(v1, v2, LEN);
  292. oprod = fdsp->scalarproduct_float(v1, v2, LEN);
  293. if (ret = compare_floats(&cprod, &oprod, 1, ARBITRARY_SCALARPRODUCT_CONST))
  294. av_log(NULL, AV_LOG_ERROR, "scalarproduct_float failed\n");
  295. return ret;
  296. }
  297. int main(int argc, char **argv)
  298. {
  299. int ret = 0;
  300. uint32_t seed;
  301. AVFloatDSPContext fdsp, cdsp;
  302. AVLFG lfg;
  303. LOCAL_ALIGNED(32, float, src0, [LEN]);
  304. LOCAL_ALIGNED(32, float, src1, [LEN]);
  305. LOCAL_ALIGNED(32, float, src2, [LEN]);
  306. LOCAL_ALIGNED(32, double, dbl_src0, [LEN]);
  307. LOCAL_ALIGNED(32, double, dbl_src1, [LEN]);
  308. if (argc > 2 && !strcmp(argv[1], "-s"))
  309. seed = strtoul(argv[2], NULL, 10);
  310. else
  311. seed = av_get_random_seed();
  312. av_log(NULL, AV_LOG_INFO, "float_dsp-test: random seed %u\n", seed);
  313. av_lfg_init(&lfg, seed);
  314. fill_float_array(&lfg, src0, LEN);
  315. fill_float_array(&lfg, src1, LEN);
  316. fill_float_array(&lfg, src2, LEN);
  317. fill_double_array(&lfg, dbl_src0, LEN);
  318. fill_double_array(&lfg, dbl_src1, LEN);
  319. avpriv_float_dsp_init(&fdsp, 1);
  320. av_set_cpu_flags_mask(0);
  321. avpriv_float_dsp_init(&cdsp, 1);
  322. if (test_vector_fmul(&fdsp, &cdsp, src0, src1))
  323. ret -= 1 << 0;
  324. if (test_vector_fmac_scalar(&fdsp, &cdsp, src2, src0, src1[0]))
  325. ret -= 1 << 1;
  326. if (test_vector_fmul_scalar(&fdsp, &cdsp, src0, src1[0]))
  327. ret -= 1 << 2;
  328. if (test_vector_fmul_window(&fdsp, &cdsp, src0, src1, src2))
  329. ret -= 1 << 3;
  330. if (test_vector_fmul_add(&fdsp, &cdsp, src0, src1, src2))
  331. ret -= 1 << 4;
  332. if (test_vector_fmul_reverse(&fdsp, &cdsp, src0, src1))
  333. ret -= 1 << 5;
  334. if (test_butterflies_float(&fdsp, &cdsp, src0, src1))
  335. ret -= 1 << 6;
  336. if (test_scalarproduct_float(&fdsp, &cdsp, src0, src1))
  337. ret -= 1 << 7;
  338. if (test_vector_dmul_scalar(&fdsp, &cdsp, dbl_src0, dbl_src1[0]))
  339. ret -= 1 << 8;
  340. return ret;
  341. }
  342. #endif /* TEST */