/[svn]/linuxsampler/trunk/benchmarks/triang.cpp
ViewVC logotype

Diff of /linuxsampler/trunk/benchmarks/triang.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 714 by schoenebeck, Sat Jul 23 21:55:38 2005 UTC revision 3118 by schoenebeck, Fri Apr 21 13:33:03 2017 UTC
# Line 4  Line 4 
4      This is a benchmark for comparison between a integer math, table lookup      This is a benchmark for comparison between a integer math, table lookup
5      and numeric sine wave harmonics solution.      and numeric sine wave harmonics solution.
6    
7      Copyright (C) 2005 Christian Schoenebeck <cuse@users.sf.net>      Copyright (C) 2005 - 2017 Christian Schoenebeck <cuse@users.sf.net>
8  */  */
9    
10  #include <math.h>  #include <math.h>
11  #include <time.h>  #include <time.h>
12  #include <stdio.h>  #include <stdio.h>
13    
14    #include "../src/engines/common/LFOTriangleIntMath.h"
15    #include "../src/engines/common/LFOTriangleIntAbsMath.h"
16    #include "../src/engines/common/LFOTriangleDiHarmonic.h"
17    
18  // whether we should not show any messages on the console  // whether we should not show any messages on the console
19  #ifndef SILENT  #ifndef SILENT
20  # define SILENT 0  # define SILENT 0
# Line 38  Line 42 
42  # define SIGNED                 1  # define SIGNED                 1
43  #endif  #endif
44    
45    // maximum value of the LFO output (also depends on SIGNED above)
46    #ifndef MAX
47    # define MAX                    1.0f
48    #endif
49    
50  // pro forma  // pro forma
51  #ifndef SAMPLING_RATE  #ifndef SAMPLING_RATE
52  # define SAMPLING_RATE          44100.0f  # define SAMPLING_RATE          44100.0f
# Line 48  Line 57 
57  #define INT_MATH_SOLUTION       2  /* we don't start with 1, as this is reserved for unknown errors */  #define INT_MATH_SOLUTION       2  /* we don't start with 1, as this is reserved for unknown errors */
58  #define DI_HARMONIC_SOLUTION    3  #define DI_HARMONIC_SOLUTION    3
59  #define TABLE_LOOKUP_SOLUTION   4  /* table lookup solution is currently disabled in this benchmark, see below */  #define TABLE_LOOKUP_SOLUTION   4  /* table lookup solution is currently disabled in this benchmark, see below */
60    #define INT_MATH_ABS_SOLUTION   5  /* integer math with abs() */
61  #define INVALID_RESULT          -1  #define INVALID_RESULT          -1
62    
63  // we use 32 bit single precision floating point as sample point format  // we use 32 bit single precision floating point as sample point format
64  typedef float sample_t;  typedef float smpl_t; // (sample_t is already defined as int16_t by global_private.h)
65    
66    using namespace LinuxSampler;
67    
68    #if SIGNED
69    LFOTriangleIntMath<range_signed>*    pIntLFO        = NULL;
70    LFOTriangleIntAbsMath<range_signed>* pIntAbsLFO     = NULL;
71    LFOTriangleDiHarmonic<range_signed>* pDiHarmonicLFO = NULL;
72    #else // unsigned
73    LFOTriangleIntMath<range_unsigned>*    pIntLFO        = NULL;
74    LFOTriangleIntAbsMath<range_unsigned>* pIntAbsLFO     = NULL;
75    LFOTriangleDiHarmonic<range_unsigned>* pDiHarmonicLFO = NULL;
76    #endif
77    
78  // integer math solution  // integer math solution
79  float int_math(sample_t* pDestinationBuffer, float* pAmp, const int steps, const float frequency) {  float int_math(smpl_t* pDestinationBuffer, float* pAmp, const int steps, const float frequency) {
80      // pro forma      // pro forma
81      const float r = frequency / SAMPLING_RATE; // frequency alteration quotient      pIntLFO->trigger(frequency, start_level_max, 1200 /* max. internal depth */, 0, false, (unsigned int) SAMPLING_RATE);
82      unsigned int maxvalue = (unsigned int) -1; // all 0xFFFF...  
83      #if SIGNED      clock_t stop_time;
84      const float normalizer = 1.0f / ((float) maxvalue / 4.0f);      clock_t start_time = clock();
85      #else // unsigned  
86      const float normalizer = 1.0f / ((float) maxvalue / 2.0f);      for (int run = 0; run < RUNS; run++) {
87            pIntLFO->updateByMIDICtrlValue(127); // pro forma
88            for (int i = 0; i < steps; ++i) {
89                pDestinationBuffer[i] = pIntLFO->render() * pAmp[i]; // * pAmp[i] just to simulate some memory load
90            }
91        }
92    
93        stop_time = clock();
94        float elapsed_time = (stop_time - start_time) / (double(CLOCKS_PER_SEC) / 1000.0);
95        #if ! SILENT
96        printf("int math solution elapsed time: %1.0f ms\n", elapsed_time);
97      #endif      #endif
     const int c = (int) (maxvalue * r);  
     const int signshifts = (sizeof(int) * 8) - 1;  
98    
99      int iSign;      return elapsed_time;
100    }
101    
102    // integer math abs solution
103    float int_math_abs(smpl_t* pDestinationBuffer, float* pAmp, const int steps, const float frequency) {
104        // pro forma
105        pIntAbsLFO->trigger(frequency, start_level_max, 1200 /* max. internal depth */, 0, false, (unsigned int) SAMPLING_RATE);
106    
107      clock_t stop_time;      clock_t stop_time;
108      clock_t start_time = clock();      clock_t start_time = clock();
109    
110      for (int run = 0; run < RUNS; run++) {      for (int run = 0; run < RUNS; run++) {
111          int iLevel = 0;          pIntAbsLFO->updateByMIDICtrlValue(0); // pro forma
112          for (int i = 0; i < steps; ++i) {          for (int i = 0; i < steps; ++i) {
113              iLevel += c;              pDestinationBuffer[i] = pIntAbsLFO->render() * pAmp[i]; // * pAmp[i] just to simulate some memory load
             iSign  = (iLevel >> signshifts) | 1;  
             #if SIGNED  
             pDestinationBuffer[i] = (normalizer * (float) (iSign * iLevel) - 1.0f) * pAmp[i]; // * pAmp[i] just to simulate some memory load  
             #else // unsigned  
             pDestinationBuffer[i] = normalizer * (float) (iSign * iLevel) * pAmp[i]; // * pAmp[i] just to simulate some memory load  
             #endif  
114          }          }
115      }      }
116    
117      stop_time = clock();      stop_time = clock();
118      float elapsed_time = (stop_time - start_time) / (double(CLOCKS_PER_SEC) / 1000.0);      float elapsed_time = (stop_time - start_time) / (double(CLOCKS_PER_SEC) / 1000.0);
119      #if ! SILENT      #if ! SILENT
120      printf("int math solution elapsed time: %1.0f ms\n", elapsed_time);      printf("int math abs solution elapsed time: %1.0f ms\n", elapsed_time);
121      #endif      #endif
122    
123      return elapsed_time;      return elapsed_time;
# Line 101  float int_math(sample_t* pDestinationBuf Line 131  float int_math(sample_t* pDestinationBuf
131  // anyway. If you found an architecture where this seems to be the best  // anyway. If you found an architecture where this seems to be the best
132  // solution, please let us know!  // solution, please let us know!
133  #if 0  #if 0
134  float table_lookup(sample_t* pDestinationBuffer, float* pAmp, const int steps, const float frequency) {  float table_lookup(smpl_t* pDestinationBuffer, float* pAmp, const int steps, const float frequency) {
135      // pro forma      // pro forma
136      const float r = frequency / SAMPLING_RATE; // frequency alteration quotient      const float r = frequency / SAMPLING_RATE; // frequency alteration quotient
137      #if SIGNED      #if SIGNED
# Line 164  float table_lookup(sample_t* pDestinatio Line 194  float table_lookup(sample_t* pDestinatio
194  #endif  #endif
195    
196  // numeric, di-harmonic solution  // numeric, di-harmonic solution
197  float numeric_di_harmonic_solution(sample_t* pDestinationBuffer, float* pAmp, const int steps, const float frequency) {  float numeric_di_harmonic_solution(smpl_t* pDestinationBuffer, float* pAmp, const int steps, const float frequency) {
198      // we approximate the triangular wave by adding 2 harmonics      // pro forma
199      const float c1   = 2.0f * M_PI * frequency / SAMPLING_RATE;      pDiHarmonicLFO->trigger(frequency, start_level_max, 1200 /* max. internal depth */, 0, false, (unsigned int) SAMPLING_RATE);
     const float phi1 = 0.0f; // phase displacement  
     const float c2   = 2.0f * M_PI * frequency / SAMPLING_RATE * 3.0f;  
     const float phi2 = 0.0f; // phase displacement  
   
     // amplitue for the 2nd harmonic (to approximate the triangular wave)  
     const float amp2 = 0.1f;  
   
     // initial values for real and imaginary part  
     float real1 = cos(phi1);  
     float imag1 = sin(phi1);  
     float real2 = cos(phi2);  
     float imag2 = sin(phi2);  
200    
201      clock_t stop_time;      clock_t stop_time;
202      clock_t start_time = clock();      clock_t start_time = clock();
203    
204      for (int run = 0; run < RUNS; run++) {      for (int run = 0; run < RUNS; run++) {
205          for (int i = 0; i < steps; i++) {          pDiHarmonicLFO->updateByMIDICtrlValue(127); // pro forma
206              #if SIGNED          for (int i = 0; i < steps; ++i) {
207              pDestinationBuffer[i] = (real1 + real2 * amp2) * pAmp[i]; // * pAmp[i] just to simulate some memory load              pDestinationBuffer[i] = pDiHarmonicLFO->render() * pAmp[i]; // * pAmp[i] just to simulate some memory load
             #else // unsigned  
             pDestinationBuffer[i] = ((real1 + real2 * amp2) * 0.5f + 0.5f) * pAmp[i]; // * pAmp[i] just to simulate some memory load  
             #endif  
             real1 -= c1 * imag1;  
             imag1 += c1 * real1;  
             real2 -= c2 * imag2;  
             imag2 += c2 * real2;  
208          }          }
209      }      }
210    
# Line 207  float numeric_di_harmonic_solution(sampl Line 218  float numeric_di_harmonic_solution(sampl
218  }  }
219    
220  // output calculated values as RAW audio format (32 bit floating point, mono) file  // output calculated values as RAW audio format (32 bit floating point, mono) file
221  void output_as_raw_file(const char* filename, sample_t* pOutputBuffer, int steps) {  void output_as_raw_file(const char* filename, smpl_t* pOutputBuffer, int steps) {
222      FILE* file = fopen(filename, "w");      FILE* file = fopen(filename, "w");
223      if (file) {      if (file) {
224          fwrite((void*) pOutputBuffer, sizeof(float), steps, file);          fwrite((void*) pOutputBuffer, sizeof(float), steps, file);
# Line 231  int main() { Line 242  int main() {
242      # endif      # endif
243      #endif      #endif
244    
245        #if SIGNED
246        pIntLFO        = new LFOTriangleIntMath<range_signed>(MAX);
247        pIntAbsLFO     = new LFOTriangleIntAbsMath<range_signed>(MAX);
248        pDiHarmonicLFO = new LFOTriangleDiHarmonic<range_signed>(MAX);
249        #else // unsigned
250        pIntLFO        = new LFOTriangleIntMath<range_unsigned>(MAX);
251        pIntAbsLFO     = new LFOTriangleIntAbsMath<range_unsigned>(MAX);
252        pDiHarmonicLFO = new LFOTriangleDiHarmonic<range_unsigned>(MAX);
253        #endif
254    
255      // output buffer for the calculated sinusoid wave      // output buffer for the calculated sinusoid wave
256      sample_t* pOutputBuffer = new sample_t[steps];      smpl_t* pOutputBuffer = new smpl_t[steps];
257      // just an arbitrary amplitude envelope to simulate a bit higher memory bandwidth      // just an arbitrary amplitude envelope to simulate a bit higher memory bandwidth
258      float* pAmplitude  = new float[steps];      float* pAmplitude  = new float[steps];
259    
# Line 241  int main() { Line 262  int main() {
262          pAmplitude[i] = (float) i / (float) steps;          pAmplitude[i] = (float) i / (float) steps;
263    
264      // how long each solution took (in seconds)      // how long each solution took (in seconds)
265      float int_math_result, /*table_lookup_result,*/ numeric_di_harmonic_result;      float int_math_result, int_math_abs_result, /*table_lookup_result,*/ numeric_di_harmonic_result;
266    
267      int_math_result = int_math(pOutputBuffer, pAmplitude, steps, sinusoidFrequency);      int_math_result = int_math(pOutputBuffer, pAmplitude, steps, sinusoidFrequency);
268      #if OUTPUT_AS_RAW_WAVE      #if OUTPUT_AS_RAW_WAVE
269        output_as_raw_file("bench_int_math.raw", pOutputBuffer, steps);        output_as_raw_file("bench_int_math.raw", pOutputBuffer, steps);
270      #endif      #endif
271    
272        int_math_abs_result = int_math_abs(pOutputBuffer, pAmplitude, steps, sinusoidFrequency);
273        #if OUTPUT_AS_RAW_WAVE
274          output_as_raw_file("bench_int_math_abs.raw", pOutputBuffer, steps);
275        #endif
276      //table_lookup_result = table_lookup(pOutputBuffer, pAmplitude, steps, sinusoidFrequency);      //table_lookup_result = table_lookup(pOutputBuffer, pAmplitude, steps, sinusoidFrequency);
277      //#if OUTPUT_AS_RAW_WAVE      //#if OUTPUT_AS_RAW_WAVE
278      //  output_as_raw_file("bench_table_lookup.raw", pOutputBuffer, steps);      //  output_as_raw_file("bench_table_lookup.raw", pOutputBuffer, steps);
# Line 261  int main() { Line 287  int main() {
287      #endif      #endif
288    
289      int_math_result            += int_math(pOutputBuffer, pAmplitude, steps, sinusoidFrequency);      int_math_result            += int_math(pOutputBuffer, pAmplitude, steps, sinusoidFrequency);
290        int_math_abs_result = int_math_abs(pOutputBuffer, pAmplitude, steps, sinusoidFrequency);
291      //table_lookup_result        += table_lookup(pOutputBuffer, pAmplitude, steps, sinusoidFrequency);      //table_lookup_result        += table_lookup(pOutputBuffer, pAmplitude, steps, sinusoidFrequency);
292      numeric_di_harmonic_result += numeric_di_harmonic_solution(pOutputBuffer, pAmplitude, steps, sinusoidFrequency);      numeric_di_harmonic_result += numeric_di_harmonic_solution(pOutputBuffer, pAmplitude, steps, sinusoidFrequency);
293    
294      if (pAmplitude)    delete[] pAmplitude;      if (pAmplitude)    delete[] pAmplitude;
295      if (pOutputBuffer) delete[] pOutputBuffer;      if (pOutputBuffer) delete[] pOutputBuffer;
296    
297      if (/*int_math_result <= table_lookup_result &&*/ int_math_result <= numeric_di_harmonic_result) return INT_MATH_SOLUTION;      if (pIntLFO)        delete pIntLFO;
298      if (/*numeric_di_harmonic_result <= table_lookup_result &&*/ numeric_di_harmonic_result <= int_math_result) return DI_HARMONIC_SOLUTION;      if (pDiHarmonicLFO) delete pDiHarmonicLFO;
299      //if (table_lookup_result <= int_math_result && table_lookup_result <= numeric_di_harmonic_result) return TABLE_LOOKUP_SOLUTION;  
300        if (int_math_abs_result <= int_math_result && int_math_abs_result <= numeric_di_harmonic_result) return INT_MATH_ABS_SOLUTION;
301        if (int_math_result <= int_math_abs_result && int_math_result <= numeric_di_harmonic_result) return INT_MATH_SOLUTION;
302        if (numeric_di_harmonic_result <= int_math_abs_result && numeric_di_harmonic_result <= int_math_result) return DI_HARMONIC_SOLUTION;
303    
304      return INVALID_RESULT; // error      return INVALID_RESULT; // error
305  }  }

Legend:
Removed from v.714  
changed lines
  Added in v.3118

  ViewVC Help
Powered by ViewVC