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.

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