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.

297 lines
8.9KB

  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 <float.h>
  19. #include <stdint.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include "cpu.h"
  23. #include "internal.h"
  24. #include "lfg.h"
  25. #include "log.h"
  26. #include "random_seed.h"
  27. #include "float_dsp.h"
  28. #define LEN 240
  29. static void fill_float_array(AVLFG *lfg, float *a, int len)
  30. {
  31. int i;
  32. double bmg[2], stddev = 10.0, mean = 0.0;
  33. for (i = 0; i < len; i += 2) {
  34. av_bmg_get(lfg, bmg);
  35. a[i] = bmg[0] * stddev + mean;
  36. a[i + 1] = bmg[1] * stddev + mean;
  37. }
  38. }
  39. static int compare_floats(const float *a, const float *b, int len,
  40. float max_diff)
  41. {
  42. int i;
  43. for (i = 0; i < len; i++) {
  44. if (fabsf(a[i] - b[i]) > max_diff) {
  45. av_log(NULL, AV_LOG_ERROR, "%d: %- .12f - %- .12f = % .12g\n",
  46. i, a[i], b[i], a[i] - b[i]);
  47. return -1;
  48. }
  49. }
  50. return 0;
  51. }
  52. static void fill_double_array(AVLFG *lfg, double *a, int len)
  53. {
  54. int i;
  55. double bmg[2], stddev = 10.0, mean = 0.0;
  56. for (i = 0; i < len; i += 2) {
  57. av_bmg_get(lfg, bmg);
  58. a[i] = bmg[0] * stddev + mean;
  59. a[i + 1] = bmg[1] * stddev + mean;
  60. }
  61. }
  62. static int compare_doubles(const double *a, const double *b, int len,
  63. double max_diff)
  64. {
  65. int i;
  66. for (i = 0; i < len; i++) {
  67. if (fabs(a[i] - b[i]) > max_diff) {
  68. av_log(NULL, AV_LOG_ERROR, "%d: %- .12f - %- .12f = % .12g\n",
  69. i, a[i], b[i], a[i] - b[i]);
  70. return -1;
  71. }
  72. }
  73. return 0;
  74. }
  75. static int test_vector_fmul(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp,
  76. const float *v1, const float *v2)
  77. {
  78. LOCAL_ALIGNED(32, float, cdst, [LEN]);
  79. LOCAL_ALIGNED(32, float, odst, [LEN]);
  80. int ret;
  81. cdsp->vector_fmul(cdst, v1, v2, LEN);
  82. fdsp->vector_fmul(odst, v1, v2, LEN);
  83. if (ret = compare_floats(cdst, odst, LEN, FLT_EPSILON))
  84. av_log(NULL, AV_LOG_ERROR, "vector_fmul failed\n");
  85. return ret;
  86. }
  87. #define ARBITRARY_FMAC_SCALAR_CONST 0.005
  88. static int test_vector_fmac_scalar(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp,
  89. const float *v1, const float *src0, float scale)
  90. {
  91. LOCAL_ALIGNED(32, float, cdst, [LEN]);
  92. LOCAL_ALIGNED(32, float, odst, [LEN]);
  93. int ret;
  94. memcpy(cdst, v1, LEN * sizeof(*v1));
  95. memcpy(odst, v1, LEN * sizeof(*v1));
  96. cdsp->vector_fmac_scalar(cdst, src0, scale, LEN);
  97. fdsp->vector_fmac_scalar(odst, src0, scale, LEN);
  98. if (ret = compare_floats(cdst, odst, LEN, ARBITRARY_FMAC_SCALAR_CONST))
  99. av_log(NULL, AV_LOG_ERROR, "vector_fmac_scalar failed\n");
  100. return ret;
  101. }
  102. static int test_vector_fmul_scalar(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp,
  103. const float *v1, float scale)
  104. {
  105. LOCAL_ALIGNED(32, float, cdst, [LEN]);
  106. LOCAL_ALIGNED(32, float, odst, [LEN]);
  107. int ret;
  108. cdsp->vector_fmul_scalar(cdst, v1, scale, LEN);
  109. fdsp->vector_fmul_scalar(odst, v1, scale, LEN);
  110. if (ret = compare_floats(cdst, odst, LEN, FLT_EPSILON))
  111. av_log(NULL, AV_LOG_ERROR, "vector_fmul_scalar failed\n");
  112. return ret;
  113. }
  114. static int test_vector_dmul_scalar(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp,
  115. const double *v1, double scale)
  116. {
  117. LOCAL_ALIGNED(32, double, cdst, [LEN]);
  118. LOCAL_ALIGNED(32, double, odst, [LEN]);
  119. int ret;
  120. cdsp->vector_dmul_scalar(cdst, v1, scale, LEN);
  121. fdsp->vector_dmul_scalar(odst, v1, scale, LEN);
  122. if (ret = compare_doubles(cdst, odst, LEN, DBL_EPSILON))
  123. av_log(NULL, AV_LOG_ERROR, "vector_dmul_scalar failed\n");
  124. return ret;
  125. }
  126. #define ARBITRARY_FMUL_WINDOW_CONST 0.008
  127. static int test_vector_fmul_window(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp,
  128. const float *v1, const float *v2, const float *v3)
  129. {
  130. LOCAL_ALIGNED(32, float, cdst, [LEN]);
  131. LOCAL_ALIGNED(32, float, odst, [LEN]);
  132. int ret;
  133. cdsp->vector_fmul_window(cdst, v1, v2, v3, LEN / 2);
  134. fdsp->vector_fmul_window(odst, v1, v2, v3, LEN / 2);
  135. if (ret = compare_floats(cdst, odst, LEN, ARBITRARY_FMUL_WINDOW_CONST))
  136. av_log(NULL, AV_LOG_ERROR, "vector_fmul_window failed\n");
  137. return ret;
  138. }
  139. #define ARBITRARY_FMUL_ADD_CONST 0.005
  140. static int test_vector_fmul_add(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp,
  141. const float *v1, const float *v2, const float *v3)
  142. {
  143. LOCAL_ALIGNED(32, float, cdst, [LEN]);
  144. LOCAL_ALIGNED(32, float, odst, [LEN]);
  145. int ret;
  146. cdsp->vector_fmul_add(cdst, v1, v2, v3, LEN);
  147. fdsp->vector_fmul_add(odst, v1, v2, v3, LEN);
  148. if (ret = compare_floats(cdst, odst, LEN, ARBITRARY_FMUL_ADD_CONST))
  149. av_log(NULL, AV_LOG_ERROR, "vector_fmul_add failed\n");
  150. return ret;
  151. }
  152. static int test_vector_fmul_reverse(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp,
  153. const float *v1, const float *v2)
  154. {
  155. LOCAL_ALIGNED(32, float, cdst, [LEN]);
  156. LOCAL_ALIGNED(32, float, odst, [LEN]);
  157. int ret;
  158. cdsp->vector_fmul_reverse(cdst, v1, v2, LEN);
  159. fdsp->vector_fmul_reverse(odst, v1, v2, LEN);
  160. if (ret = compare_floats(cdst, odst, LEN, FLT_EPSILON))
  161. av_log(NULL, AV_LOG_ERROR, "vector_fmul_reverse failed\n");
  162. return ret;
  163. }
  164. static int test_butterflies_float(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp,
  165. const float *v1, const float *v2)
  166. {
  167. LOCAL_ALIGNED(32, float, cv1, [LEN]);
  168. LOCAL_ALIGNED(32, float, cv2, [LEN]);
  169. LOCAL_ALIGNED(32, float, ov1, [LEN]);
  170. LOCAL_ALIGNED(32, float, ov2, [LEN]);
  171. int ret;
  172. memcpy(cv1, v1, LEN * sizeof(*v1));
  173. memcpy(cv2, v2, LEN * sizeof(*v2));
  174. memcpy(ov1, v1, LEN * sizeof(*v1));
  175. memcpy(ov2, v2, LEN * sizeof(*v2));
  176. cdsp->butterflies_float(cv1, cv2, LEN);
  177. fdsp->butterflies_float(ov1, ov2, LEN);
  178. if ((ret = compare_floats(cv1, ov1, LEN, FLT_EPSILON)) ||
  179. (ret = compare_floats(cv2, ov2, LEN, FLT_EPSILON)))
  180. av_log(NULL, AV_LOG_ERROR, "butterflies_float failed\n");
  181. return ret;
  182. }
  183. #define ARBITRARY_SCALARPRODUCT_CONST 0.2
  184. static int test_scalarproduct_float(AVFloatDSPContext *fdsp, AVFloatDSPContext *cdsp,
  185. const float *v1, const float *v2)
  186. {
  187. float cprod, oprod;
  188. int ret;
  189. cprod = cdsp->scalarproduct_float(v1, v2, LEN);
  190. oprod = fdsp->scalarproduct_float(v1, v2, LEN);
  191. if (ret = compare_floats(&cprod, &oprod, 1, ARBITRARY_SCALARPRODUCT_CONST))
  192. av_log(NULL, AV_LOG_ERROR, "scalarproduct_float failed\n");
  193. return ret;
  194. }
  195. int main(int argc, char **argv)
  196. {
  197. int ret = 0;
  198. uint32_t seed;
  199. AVFloatDSPContext fdsp, cdsp;
  200. AVLFG lfg;
  201. LOCAL_ALIGNED(32, float, src0, [LEN]);
  202. LOCAL_ALIGNED(32, float, src1, [LEN]);
  203. LOCAL_ALIGNED(32, float, src2, [LEN]);
  204. LOCAL_ALIGNED(32, double, dbl_src0, [LEN]);
  205. LOCAL_ALIGNED(32, double, dbl_src1, [LEN]);
  206. if (argc > 2 && !strcmp(argv[1], "-s"))
  207. seed = strtoul(argv[2], NULL, 10);
  208. else
  209. seed = av_get_random_seed();
  210. av_log(NULL, AV_LOG_INFO, "float_dsp-test: random seed %u\n", seed);
  211. av_lfg_init(&lfg, seed);
  212. fill_float_array(&lfg, src0, LEN);
  213. fill_float_array(&lfg, src1, LEN);
  214. fill_float_array(&lfg, src2, LEN);
  215. fill_double_array(&lfg, dbl_src0, LEN);
  216. fill_double_array(&lfg, dbl_src1, LEN);
  217. avpriv_float_dsp_init(&fdsp, 1);
  218. av_set_cpu_flags_mask(0);
  219. avpriv_float_dsp_init(&cdsp, 1);
  220. if (test_vector_fmul(&fdsp, &cdsp, src0, src1))
  221. ret -= 1 << 0;
  222. if (test_vector_fmac_scalar(&fdsp, &cdsp, src2, src0, src1[0]))
  223. ret -= 1 << 1;
  224. if (test_vector_fmul_scalar(&fdsp, &cdsp, src0, src1[0]))
  225. ret -= 1 << 2;
  226. if (test_vector_fmul_window(&fdsp, &cdsp, src0, src1, src2))
  227. ret -= 1 << 3;
  228. if (test_vector_fmul_add(&fdsp, &cdsp, src0, src1, src2))
  229. ret -= 1 << 4;
  230. if (test_vector_fmul_reverse(&fdsp, &cdsp, src0, src1))
  231. ret -= 1 << 5;
  232. if (test_butterflies_float(&fdsp, &cdsp, src0, src1))
  233. ret -= 1 << 6;
  234. if (test_scalarproduct_float(&fdsp, &cdsp, src0, src1))
  235. ret -= 1 << 7;
  236. if (test_vector_dmul_scalar(&fdsp, &cdsp, dbl_src0, dbl_src1[0]))
  237. ret -= 1 << 8;
  238. return ret;
  239. }