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.

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