af_afir.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 "config.h"
  19. #include <float.h>
  20. #include <stdint.h>
  21. #include "libavfilter/af_afirdsp.h"
  22. #include "libavutil/internal.h"
  23. #include "libavutil/mem_internal.h"
  24. #include "checkasm.h"
  25. #define LEN 256
  26. #define randomize_buffer(buf) \
  27. do { \
  28. int i; \
  29. double bmg[2], stddev = 10.0, mean = 0.0; \
  30. \
  31. for (i = 0; i < BUF_SIZE; i += 2) { \
  32. av_bmg_get(&checkasm_lfg, bmg); \
  33. buf[i] = bmg[0] * stddev + mean; \
  34. buf[i + 1] = bmg[1] * stddev + mean; \
  35. } \
  36. } while(0);
  37. static void test_fcmul_add(AudioFIRDSPContext *fir)
  38. {
  39. #define BUF_SIZE LEN*2+8
  40. LOCAL_ALIGNED_32(float, src0, [BUF_SIZE]);
  41. LOCAL_ALIGNED_32(float, src1, [BUF_SIZE]);
  42. LOCAL_ALIGNED_32(float, src2, [BUF_SIZE]);
  43. randomize_buffer(src0);
  44. randomize_buffer(src1);
  45. randomize_buffer(src2);
  46. if (check_func(fir->fcmul_add, "fcmul_add")) {
  47. LOCAL_ALIGNED_32(float, cdst, [BUF_SIZE]);
  48. LOCAL_ALIGNED_32(float, odst, [BUF_SIZE]);
  49. int i;
  50. declare_func(void, float *sum, const float *t, const float *c,
  51. ptrdiff_t len);
  52. memcpy(cdst, src0, (BUF_SIZE) * sizeof(float));
  53. memcpy(odst, src0, (BUF_SIZE) * sizeof(float));
  54. call_ref(cdst, src1, src2, LEN);
  55. call_new(odst, src1, src2, LEN);
  56. for (i = 0; i <= LEN*2; i++) {
  57. int idx = i & ~1;
  58. float cre = src2[idx];
  59. float cim = src2[idx + 1];
  60. float tre = src1[idx];
  61. float tim = src1[idx + 1];
  62. double t = fabs(src0[i]) +
  63. fabs(tre) + fabs(tim) + fabs(cre) + fabs(cim) +
  64. fabs(tre * cre) + fabs(tim * cim) +
  65. fabs(tre * cim) + fabs(tim * cre) +
  66. fabs(tre * cre - tim * cim) +
  67. fabs(tre * cim + tim * cre) +
  68. fabs(cdst[i]) + 1.0;
  69. if (!float_near_abs_eps(cdst[i], odst[i], t * 2 * FLT_EPSILON)) {
  70. fprintf(stderr, "%d: %- .12f - %- .12f = % .12g\n",
  71. i, cdst[i], odst[i], cdst[i] - odst[i]);
  72. fail();
  73. break;
  74. }
  75. }
  76. memcpy(odst, src0, (BUF_SIZE) * sizeof(float));
  77. bench_new(odst, src1, src2, LEN);
  78. }
  79. report("fcmul_add");
  80. }
  81. static void test_dcmul_add(AudioFIRDSPContext *fir)
  82. {
  83. #define BUF_SIZE LEN*2+8
  84. LOCAL_ALIGNED_32(double, src0, [BUF_SIZE]);
  85. LOCAL_ALIGNED_32(double, src1, [BUF_SIZE]);
  86. LOCAL_ALIGNED_32(double, src2, [BUF_SIZE]);
  87. randomize_buffer(src0);
  88. randomize_buffer(src1);
  89. randomize_buffer(src2);
  90. if (check_func(fir->dcmul_add, "dcmul_add")) {
  91. LOCAL_ALIGNED_32(double, cdst, [BUF_SIZE]);
  92. LOCAL_ALIGNED_32(double, odst, [BUF_SIZE]);
  93. int i;
  94. declare_func(void, double *sum, const double *t, const double *c,
  95. ptrdiff_t len);
  96. memcpy(cdst, src0, (BUF_SIZE) * sizeof(double));
  97. memcpy(odst, src0, (BUF_SIZE) * sizeof(double));
  98. call_ref(cdst, src1, src2, LEN);
  99. call_new(odst, src1, src2, LEN);
  100. for (i = 0; i <= LEN*2; i++) {
  101. int idx = i & ~1;
  102. double cre = src2[idx];
  103. double cim = src2[idx + 1];
  104. double tre = src1[idx];
  105. double tim = src1[idx + 1];
  106. double t = fabs(src0[i]) +
  107. fabs(tre) + fabs(tim) + fabs(cre) + fabs(cim) +
  108. fabs(tre * cre) + fabs(tim * cim) +
  109. fabs(tre * cim) + fabs(tim * cre) +
  110. fabs(tre * cre - tim * cim) +
  111. fabs(tre * cim + tim * cre) +
  112. fabs(cdst[i]) + 1.0;
  113. if (!double_near_abs_eps(cdst[i], odst[i], t * 2 * FLT_EPSILON)) {
  114. fprintf(stderr, "%d: %- .12f - %- .12f = % .12g\n",
  115. i, cdst[i], odst[i], cdst[i] - odst[i]);
  116. fail();
  117. break;
  118. }
  119. }
  120. memcpy(odst, src0, (BUF_SIZE) * sizeof(double));
  121. bench_new(odst, src1, src2, LEN);
  122. }
  123. report("dcmul_add");
  124. }
  125. void checkasm_check_afir(void)
  126. {
  127. AudioFIRDSPContext fir = { 0 };
  128. ff_afir_init(&fir);
  129. test_fcmul_add(&fir);
  130. test_dcmul_add(&fir);
  131. }