limitertest.c
Upload User: zhongxx05
Upload Date: 2007-06-06
Package Size: 33641k
Code Size: 8k
Category:

Symbian

Development Platform:

C/C++

  1. /* ***** BEGIN LICENSE BLOCK ***** 
  2.  * Version: RCSL 1.0/RPSL 1.0 
  3.  *  
  4.  * Portions Copyright (c) 1995-2002 RealNetworks, Inc. All Rights Reserved. 
  5.  *      
  6.  * The contents of this file, and the files included with this file, are 
  7.  * subject to the current version of the RealNetworks Public Source License 
  8.  * Version 1.0 (the "RPSL") available at 
  9.  * http://www.helixcommunity.org/content/rpsl unless you have licensed 
  10.  * the file under the RealNetworks Community Source License Version 1.0 
  11.  * (the "RCSL") available at http://www.helixcommunity.org/content/rcsl, 
  12.  * in which case the RCSL will apply. You may also obtain the license terms 
  13.  * directly from RealNetworks.  You may not use this file except in 
  14.  * compliance with the RPSL or, if you have a valid RCSL with RealNetworks 
  15.  * applicable to this file, the RCSL.  Please see the applicable RPSL or 
  16.  * RCSL for the rights, obligations and limitations governing use of the 
  17.  * contents of the file.  
  18.  *  
  19.  * This file is part of the Helix DNA Technology. RealNetworks is the 
  20.  * developer of the Original Code and owns the copyrights in the portions 
  21.  * it created. 
  22.  *  
  23.  * This file, and the files included with this file, is distributed and made 
  24.  * available on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 
  25.  * EXPRESS OR IMPLIED, AND REALNETWORKS HEREBY DISCLAIMS ALL SUCH WARRANTIES, 
  26.  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS 
  27.  * FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 
  28.  * 
  29.  * Technology Compatibility Kit Test Suite(s) Location: 
  30.  *    http://www.helixcommunity.org/content/tck 
  31.  * 
  32.  * Contributor(s): 
  33.  *  
  34.  * ***** END LICENSE BLOCK ***** */ 
  35. /* unit test for fixed point helper routines and limiter. */
  36. #include <stdio.h>
  37. #include <stdlib.h>
  38. #include <math.h>
  39. #include <time.h>
  40. #include <string.h>
  41. #ifndef M_PI
  42. #define M_PI 3.141592654
  43. #endif
  44. #define TIMING
  45. #include "math64.h"
  46. #include "limiter.h"
  47. #include "cpuident.h"
  48. #define N 2048
  49. static float f[N] ;
  50. static int a[N] ;
  51. static int b[N] ;
  52. static int c[N] ;
  53. static int d[N] ;
  54. /* see NR in C, chap. 7 */
  55. unsigned int my_rand(unsigned int state)
  56. {
  57.   return state * 1664525L + 1013904223L ;
  58. }
  59. static void prepare_rand(int r[])
  60. {
  61.     int i ;
  62.     unsigned int randstate = 0xDEADBEEF ;
  63.     for (i = 0 ; i < N ; i++)
  64.     {
  65.         // generate random numbers between -2^16 and +2^16
  66.         r[i] = (randstate >> 16) - (1<<15) ;
  67.         randstate = my_rand(randstate) ;
  68.     }
  69. }
  70. #if 1
  71. #undef ASSERT
  72. #define ASSERT(x) if(!(x)){printf("line %d: arg1=%d,arg2=%d,res=%d,true=%dn",
  73.   __LINE__,arg1,a[i],res2,res1);exit(10);}
  74. #endif
  75. int main(void)
  76. {
  77.     int i,j ;
  78.     int res1,res2 ;
  79. LIMSTATE* lim ;
  80. clock_t t ;
  81. long cycles ;
  82. float cyclesPerSec ;
  83.     CPUInformation cpuInfo ;
  84.     CPUIdentify(&cpuInfo) ;
  85.     prepare_rand(a);
  86.     prepare_rand(b);
  87.     for (i = 0 ; i < N ; i++)
  88.     {
  89.         c[i] = a[i] ;
  90. if (!c[i]) c[i] = 1 ;
  91.         if (fabs((float)a[i] * (float)b[i] / (float)c[i]) > (float)0x7fffffffUL)
  92.   c[i] = 0x7fffffffUL ;
  93.     }
  94.     // test the 64-bit commands
  95.     for (i = 0 ; i < N ; i++)
  96.     {
  97.       signed int arg1 = (-1L<<31);
  98.         res1 = a[i] ;
  99.         res2 = -MulShift31(arg1,a[i]) ;
  100.         ASSERT(res1 == res2) ;
  101.     }
  102.     printf("MulShift31 test passed.n");
  103.     for (j = 0 ; j < 32 ; j++)
  104.     {
  105.         for (i = 0 ; i < N ; i++)
  106.         {
  107.             int arg1 = (-1L<<j) ;
  108.             res1 = a[i] ;
  109.             res2 = -MulShiftN(arg1,a[i],j) ;
  110.             ASSERT(res1 == res2) ;
  111.             res2 = -MulShiftN(a[i],arg1,j) ;
  112.             ASSERT(res1 == res2) ;
  113.         }
  114.     }
  115. #if 0 // don't test for shifts of >= 32
  116.     for (j = 32 ; j < 64 ; j++)
  117.     {
  118.         for (i = 0 ; i < N ; i++)
  119.         {
  120.             res1 = a[i]>>(j-31) ;
  121.             res2 = -MulShiftN((-1L<<31),a[i],j) ;
  122.             ASSERT(res1 == res2) ;
  123.         }
  124.     }
  125. #endif
  126.     printf("MulShiftN test passed.n");
  127.     for (j = 0 ; j < 32 ; j++)
  128.     {
  129.         for (i = 0 ; i < N ; i++)
  130.         {
  131.             signed int arg1 = (-1L<<j) ;
  132.             res1 = a[i] ;
  133.             res2 = MulDiv64(arg1,a[i],arg1) ;
  134.             ASSERT(res1 == res2) ;
  135.             if (a[i])
  136.             {
  137.               res2 = MulDiv64(arg1,a[i],a[i]) ;
  138.               ASSERT(arg1 == res2) ;
  139.             }
  140.         }
  141.     }
  142.     printf("MulDiv64 test passed.n");
  143.     printf("measuring clock frequencyn");
  144. // try to measure cpu clock
  145. t = clock() ; TICK() ;
  146.         j = 3;
  147. for (i = 0 ; i < 5000000 ; i++) j *= j ; // do something... do anything
  148. cycles = (long)TOCK(i) ;
  149. t = clock()-t ;
  150. cyclesPerSec = (float)cycles * CLOCKS_PER_SEC / t ;
  151. printf("rapproximate CPU clock: %3.1f MHzn",1E-6*cyclesPerSec) ;
  152.     // timing tests
  153.     printf("MulShift30: ");
  154.     TICK() ;
  155.     for (i = 0 ; i < N ; i+=4)
  156.     {
  157.         d[i  ] = MulShift30(a[i  ],b[i  ]) ;
  158.         d[i+1] = MulShift30(a[i+1],b[i+1]) ;
  159.         d[i+2] = MulShift30(a[i+2],b[i+2]) ;
  160.         d[i+3] = MulShift30(a[i+3],b[i+3]) ;
  161.     }
  162.     cycles = (long)TOCK(N) ;
  163.     // timing tests
  164.     printf("MulShift31: ");
  165.     TICK() ;
  166.     for (i = 0 ; i < N ; i+=4)
  167.     {
  168.         d[i  ] = MulShift31(a[i  ],b[i  ]) ;
  169.         d[i+1] = MulShift31(a[i+1],b[i+1]) ;
  170.         d[i+2] = MulShift31(a[i+2],b[i+2]) ;
  171.         d[i+3] = MulShift31(a[i+3],b[i+3]) ;
  172.     }
  173.     cycles = (long)TOCK(N) ;
  174.     // timing tests
  175.     printf("MulShift32: ");
  176.     TICK() ;
  177.     for (i = 0 ; i < N ; i+=4)
  178.     {
  179.         d[i  ] = MulShift32(a[i  ],b[i  ]) ;
  180.         d[i+1] = MulShift32(a[i+1],b[i+1]) ;
  181.         d[i+2] = MulShift32(a[i+2],b[i+2]) ;
  182.         d[i+3] = MulShift32(a[i+3],b[i+3]) ;
  183.     }
  184.     cycles = (long)TOCK(N) ;
  185.     // timing tests
  186.     printf("MulShiftN: ");
  187.     TICK() ;
  188.     for (i = 0 ; i < N ; i+=4)
  189.     {
  190.         d[i  ] = MulShiftN(a[i  ],b[i  ],31) ;
  191.         d[i+1] = MulShiftN(a[i+1],b[i+1],31) ;
  192.         d[i+2] = MulShiftN(a[i+2],b[i+2],31) ;
  193.         d[i+3] = MulShiftN(a[i+3],b[i+3],31) ;
  194.     }
  195.     TOCK(N) ;
  196.     // timing tests
  197.     printf("MulDiv64: ");
  198.     TICK() ;
  199.     for (i = 0 ; i < N ; i+=4)
  200.     {
  201.         d[i  ] = MulDiv64(a[i  ],b[i  ],c[i  ]) ;
  202.         d[i+1] = MulDiv64(a[i+1],b[i+1],c[i+1]) ;
  203.         d[i+2] = MulDiv64(a[i+2],b[i+2],c[i+2]) ;
  204.         d[i+3] = MulDiv64(a[i+3],b[i+3],c[i+3]) ;
  205.     }
  206.     TOCK(N) ;
  207. // test the limiter, no clipping
  208. for (i = 0 ; i < N ; i++)
  209. {
  210. d[i] = (signed int)((1UL<<31)*sin(i * 2.0*M_PI / N)) ;
  211. }
  212. printf("memcpy: ");
  213. TICK() ;
  214. for (i = 0 ; i < 100 ; i++)
  215. {
  216. memcpy(a,d,sizeof(d)) ; /* Flawfinder: ignore */
  217. }
  218. TOCK(100*N) ;
  219. printf("Mono limiter: ");
  220. lim = LimiterInit(44100,1,0) ; // 0dB headroom
  221. TICK() ;
  222. for (i = 0 ; i < 100 ; i++)
  223. {
  224. memcpy(a,d,sizeof(d)) ; /* Flawfinder: ignore */
  225. LimiterProcess(a,N,lim) ;
  226. }
  227. cycles = (long)TOCK(100*N) ;
  228. printf("%1.1lf%% realtimen",100.0*cycles/cyclesPerSec * (44100.0/(100*N))) ;
  229. LimiterFree(lim) ;
  230. printf("Mono limiter, overgain: ");
  231. lim = LimiterInit(44100,1,1) ; // 1dB headroom
  232. TICK() ;
  233. for (i = 0 ; i < 100 ; i++)
  234. {
  235. memcpy(a,d,sizeof(d)) ; /* Flawfinder: ignore */
  236. LimiterProcess(a,N,lim) ;
  237. }
  238. cycles = (long)TOCK(100*N) ;
  239. printf("%1.1lf%% realtimen",100.0*cycles/cyclesPerSec * (44100.0/(100*N))) ;
  240. LimiterFree(lim) ;
  241. printf("Stereo limiter: ");
  242. lim = LimiterInit(44100,2,0) ; // 0dB headroom
  243. TICK() ;
  244. for (i = 0 ; i < 100 ; i++)
  245. {
  246. memcpy(a,d,sizeof(d)) ; /* Flawfinder: ignore */
  247. LimiterProcess(a,N,lim) ;
  248. }
  249. cycles = (long)TOCK(50*N) ;
  250. printf("%1.1lf%% realtimen",100.0*cycles/cyclesPerSec * (44100.0/(50*N))) ;
  251. LimiterFree(lim) ;
  252. printf("Stereo limiter, overgain: ");
  253. lim = LimiterInit(44100,2,1) ; // 1dB headroom
  254. TICK() ;
  255. for (i = 0 ; i < 100 ; i++)
  256. {
  257. memcpy(a,d,sizeof(d)) ; /* Flawfinder: ignore */
  258. LimiterProcess(a,N,lim) ;
  259. }
  260. cycles = (long)TOCK(50*N) ;
  261. printf("%1.1lf%% realtimen",100.0*cycles/cyclesPerSec * (44100.0/(50*N))) ;
  262. LimiterFree(lim) ;
  263.     return 0;
  264. }