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.

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