benchmark_residual.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /* libFLAC - Free Lossless Audio Codec library
  2. * Copyright (C) 2000-2009 Josh Coalson
  3. * Copyright (C) 2011-2023 Xiph.Org Foundation
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * - Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. *
  12. * - Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * - Neither the name of the Xiph.org Foundation nor the names of its
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
  24. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  25. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  26. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  27. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  28. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  29. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  30. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. #ifdef HAVE_CONFIG_H
  33. # include <config.h>
  34. #endif
  35. #include <stdio.h>
  36. #include <math.h>
  37. #include <string.h>
  38. #include "FLAC/ordinals.h"
  39. #include "share/compat.h"
  40. #include "private/bitmath.h"
  41. #include "private/fixed.h"
  42. #include "private/macros.h"
  43. #include "FLAC/assert.h"
  44. #include "util.h"
  45. static void FLAC__fixed_compute_residual_shift(const FLAC__int32 data[], unsigned data_len, unsigned order, FLAC__int32 residual[])
  46. {
  47. const int idata_len = (int) data_len;
  48. int i;
  49. switch(order) {
  50. case 0:
  51. FLAC__ASSERT(sizeof(residual[0]) == sizeof(data[0]));
  52. memcpy(residual, data, sizeof(residual[0])*data_len);
  53. break;
  54. case 1:
  55. for(i = 0; i < idata_len; i++)
  56. residual[i] = data[i] - data[i-1];
  57. break;
  58. case 2:
  59. for(i = 0; i < idata_len; i++)
  60. residual[i] = data[i] - (data[i-1] << 1) + data[i-2];
  61. break;
  62. case 3:
  63. for(i = 0; i < idata_len; i++)
  64. residual[i] = data[i] - (((data[i-1]-data[i-2])<<1) + (data[i-1]-data[i-2])) - data[i-3];
  65. break;
  66. case 4:
  67. for(i = 0; i < idata_len; i++)
  68. residual[i] = data[i] - ((data[i-1]+data[i-3])<<2) + ((data[i-2]<<2) + (data[i-2]<<1)) + data[i-4];
  69. break;
  70. default:
  71. FLAC__ASSERT(0);
  72. }
  73. }
  74. static void FLAC__fixed_compute_residual_mult(const FLAC__int32 data[], unsigned data_len, unsigned order, FLAC__int32 residual[])
  75. {
  76. const int idata_len = (int)data_len;
  77. int i;
  78. switch(order) {
  79. case 0:
  80. FLAC__ASSERT(sizeof(residual[0]) == sizeof(data[0]));
  81. memcpy(residual, data, sizeof(residual[0])*data_len);
  82. break;
  83. case 1:
  84. for(i = 0; i < idata_len; i++)
  85. residual[i] = data[i] - data[i-1];
  86. break;
  87. case 2:
  88. for(i = 0; i < idata_len; i++)
  89. residual[i] = data[i] - 2*data[i-1] + data[i-2];
  90. break;
  91. case 3:
  92. for(i = 0; i < idata_len; i++)
  93. residual[i] = data[i] - 3*data[i-1] + 3*data[i-2] - data[i-3];
  94. break;
  95. case 4:
  96. for(i = 0; i < idata_len; i++)
  97. residual[i] = data[i] - 4*data[i-1] + 6*data[i-2] - 4*data[i-3] + data[i-4];
  98. break;
  99. default:
  100. FLAC__ASSERT(0);
  101. }
  102. }
  103. static FLAC__int32 data [200000] ;
  104. static FLAC__int32 residual [200000] ;
  105. static unsigned bench_order = 0 ;
  106. static void
  107. bench_shift (void)
  108. { FLAC__fixed_compute_residual_shift (data, ARRAY_LEN (data), bench_order, residual) ;
  109. }
  110. static void
  111. bench_mult (void)
  112. { FLAC__fixed_compute_residual_mult (data, ARRAY_LEN (data), bench_order, residual) ;
  113. }
  114. int
  115. main (void)
  116. { bench_stats stats ;
  117. puts ("") ;
  118. for (bench_order = 2 ; bench_order <= 4 ; bench_order ++) {
  119. memset (&stats, 0, sizeof (stats)) ;
  120. stats.testfunc = bench_shift ;
  121. stats.run_count = 100 ;
  122. stats.loop_count = 10 ;
  123. benchmark_stats (&stats) ;
  124. printf ("shift order %u : %f %f %f %f\n", bench_order, stats.min_time, stats.median_time, stats.mean_time, stats.max_time) ;
  125. memset (&stats, 0, sizeof (stats)) ;
  126. stats.testfunc = bench_mult ;
  127. stats.run_count = 100 ;
  128. stats.loop_count = 10 ;
  129. benchmark_stats (&stats) ;
  130. printf ("mult order %u : %f %f %f %f\n\n", bench_order, stats.min_time, stats.median_time, stats.mean_time, stats.max_time) ;
  131. }
  132. return 0 ;
  133. }