lls.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * This file is part of FFmpeg.
  3. *
  4. * FFmpeg is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * FFmpeg 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
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along
  15. * with FFmpeg; if not, write to the Free Software Foundation, Inc.,
  16. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  17. */
  18. #include <float.h>
  19. #include "libavutil/lls.h"
  20. #include "checkasm.h"
  21. #define randomize_buffer(buf) \
  22. do { \
  23. double bmg[2], stddev = 10.0; \
  24. \
  25. for (size_t i = 0; i < MAX_VARS_ALIGN; i += 2) { \
  26. av_bmg_get(&checkasm_lfg, bmg); \
  27. buf[i] = bmg[0] * stddev; \
  28. buf[i + 1] = bmg[1] * stddev; \
  29. } \
  30. } while(0);
  31. static void test_update(LLSModel *lls, const double *var)
  32. {
  33. double refcovar[MAX_VARS][MAX_VARS];
  34. declare_func(void, LLSModel *, const double *);
  35. call_ref(lls, var);
  36. for (size_t i = 0; i < MAX_VARS; i++)
  37. for (size_t j = 0; j < MAX_VARS; j++)
  38. refcovar[i][j] = lls->covariance[i][j];
  39. memset(lls->covariance, 0, sizeof (lls->covariance));
  40. call_new(lls, var);
  41. for (size_t i = 0; i < lls->indep_count; i++)
  42. for (size_t j = i; j < lls->indep_count; j++)
  43. if (!double_near_abs_eps(refcovar[i][j], lls->covariance[i][j],
  44. 2 * DBL_EPSILON)) {
  45. fprintf(stderr, "%zu, %zu: %- .12f - %- .12f = % .12g\n", i, j,
  46. refcovar[i][j], lls->covariance[i][j],
  47. refcovar[i][j] - lls->covariance[i][j]);
  48. fail();
  49. }
  50. bench_new(lls, var);
  51. }
  52. #define EPS 0.2
  53. static void test_evaluate(LLSModel *lls, const double *param, int order)
  54. {
  55. double refprod, newprod;
  56. declare_func_float(double, LLSModel *, const double *, int);
  57. refprod = call_ref(lls, param, order);
  58. newprod = call_new(lls, param, order);
  59. if (!double_near_abs_eps(refprod, newprod, EPS)) {
  60. fprintf(stderr, "%- .12f - %- .12f = % .12g\n",
  61. refprod, newprod, refprod - newprod);
  62. fail();
  63. }
  64. if (order == lls->indep_count)
  65. bench_new(lls, param, order);
  66. }
  67. void checkasm_check_lls(void)
  68. {
  69. static const unsigned char counts[] = { 8, 12, MAX_VARS, };
  70. for (size_t i = 0; i < FF_ARRAY_ELEMS(counts); i++) {
  71. LOCAL_ALIGNED_32(double, var, [MAX_VARS_ALIGN]);
  72. LOCAL_ALIGNED_32(double, param, [FFALIGN(MAX_VARS+2,4)]);
  73. LLSModel lls;
  74. avpriv_init_lls(&lls, counts[i]);
  75. randomize_buffer(var);
  76. randomize_buffer(param);
  77. if (check_func(lls.update_lls, "update_lls_%d", counts[i]))
  78. test_update(&lls, var);
  79. for (size_t j = 0; j <= i; j++)
  80. if (check_func(lls.evaluate_lls, "evaluate_lls_%d_%d", counts[i],
  81. counts[j]))
  82. test_evaluate(&lls, param + 1, counts[j]);
  83. }
  84. report("lls");
  85. }