fftwrap_tm.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /* Copyright (C) 2007 Hong Zhiqian */
  2. /**
  3. @file fftwrap_tm.h
  4. @author Hong Zhiqian
  5. @brief Various compatibility routines for Speex (TriMedia version)
  6. */
  7. /*
  8. Redistribution and use in source and binary forms, with or without
  9. modification, are permitted provided that the following conditions
  10. are met:
  11. - Redistributions of source code must retain the above copyright
  12. notice, this list of conditions and the following disclaimer.
  13. - Redistributions in binary form must reproduce the above copyright
  14. notice, this list of conditions and the following disclaimer in the
  15. documentation and/or other materials provided with the distribution.
  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. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
  23. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  24. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  25. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  26. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  27. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  28. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  29. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. #include <ops/custom_defs.h>
  32. #include "profile_tm.h"
  33. #ifdef FIXED_POINT
  34. #define OVERRIDE_MAXIMIZE_RANGE
  35. static int maximize_range(Int16 *in, Int16 *out, int bound, int len)
  36. {
  37. register int max_val=0;
  38. register int shift=0;
  39. register int i, j;
  40. TMDEBUG_ALIGNMEM(in);
  41. TMDEBUG_ALIGNMEM(out);
  42. MAXIMIZERANGE_START();
  43. len >>= 1;
  44. for ( i=0 ; i<len ; i+=4 )
  45. {
  46. register int x10, x32, x54, x76;
  47. x10 = ld32x(in,i);
  48. x32 = ld32x(in,i+1);
  49. x54 = ld32x(in,i+2);
  50. x76 = ld32x(in,i+3);
  51. x10 = dspidualabs(x10);
  52. x32 = dspidualabs(x32);
  53. x54 = dspidualabs(x54);
  54. x76 = dspidualabs(x76);
  55. x10 = imax(sex16(x10), asri(16,x10));
  56. x32 = imax(sex16(x32), asri(16,x32));
  57. x54 = imax(sex16(x54), asri(16,x54));
  58. x76 = imax(sex16(x76), asri(16,x76));
  59. max_val = imax(max_val,x10);
  60. max_val = imax(max_val,x32);
  61. max_val = imax(max_val,x54);
  62. max_val = imax(max_val,x76);
  63. }
  64. while ( max_val <= (bound>>1) && max_val != 0 )
  65. { max_val <<= 1;
  66. shift++;
  67. }
  68. if ( shift != 0 )
  69. {
  70. for ( i=0,j=0 ; i<len ; i+=4,j+=16 )
  71. {
  72. register int x10, x32, x54, x76;
  73. x10 = ld32x(in,i);
  74. x32 = ld32x(in,i+1);
  75. x54 = ld32x(in,i+2);
  76. x76 = ld32x(in,i+3);
  77. x10 = dualasl(x10, shift);
  78. x32 = dualasl(x32, shift);
  79. x54 = dualasl(x54, shift);
  80. x76 = dualasl(x76, shift);
  81. st32d(j,out,x10);
  82. st32d(j+4,out,x32);
  83. st32d(j+8,out,x54);
  84. st32d(j+12,out,x76);
  85. }
  86. }
  87. MAXIMIZERANGE_STOP();
  88. return shift;
  89. }
  90. #define OVERRIDE_RENORM_RANGE
  91. static void renorm_range(Int16 *in, Int16 *out, int shift, int len)
  92. {
  93. register int i, j, s, l;
  94. TMDEBUG_ALIGNMEM(in);
  95. TMDEBUG_ALIGNMEM(out);
  96. RENORMRANGE_START();
  97. s = (1<<((shift))>>1);
  98. s = pack16lsb(s,s);
  99. len >>= 1;
  100. l = len & (int)0xFFFFFFFE;
  101. for ( i=0,j=0 ; i<l; i+=2,j+=8 )
  102. {
  103. register int x10, x32;
  104. x10 = ld32x(in,i);
  105. x32 = ld32x(in,i+1);
  106. x10 = dspidualadd(x10, s);
  107. x32 = dspidualadd(x32, s);
  108. x10 = dualasr(x10, shift);
  109. x32 = dualasr(x32, shift);
  110. st32d(j,out,x10);
  111. st32d(j+4,out,x32);
  112. }
  113. if ( len & (int)0x01 )
  114. {
  115. register int x10;
  116. x10 = ld32x(in,i);
  117. x10 = dspidualadd(x10, s);
  118. x10 = dualasr(x10, shift);
  119. st32d(j,out,x10);
  120. }
  121. RENORMRANGE_STOP();
  122. }
  123. #endif
  124. #ifdef USE_COMPACT_KISS_FFT
  125. #ifdef FIXED_POINT
  126. #define OVERRIDE_POWER_SPECTRUM
  127. void power_spectrum(const spx_word16_t *X, spx_word32_t *ps, int N)
  128. {
  129. register int x10, x32, x54, x76, *x;
  130. register int i;
  131. x = (int*)(X-1);
  132. TMDEBUG_ALIGNMEM(x);
  133. POWERSPECTRUM_START();
  134. x76 = 0;
  135. ps[0] = MULT16_16(X[0],X[0]);
  136. N >>= 1;
  137. for( i=1 ; i<N ; i+=4 )
  138. {
  139. x10 = ld32x(x, i);
  140. x32 = ld32x(x, i+1);
  141. x54 = ld32x(x, i+2);
  142. x76 = ld32x(x, i+3);
  143. ps[i] = ifir16(x10,x10);
  144. ps[i+1] = ifir16(x32,x32);
  145. ps[i+2] = ifir16(x54,x54);
  146. ps[i+3] = ifir16(x76,x76);
  147. }
  148. x76 = sex16(x76);
  149. ps[N] = x76 * x76;
  150. POWERSPECTRUM_STOP();
  151. }
  152. #else
  153. #define OVERRIDE_POWER_SPECTRUM
  154. void power_spectrum(const float * restrict X, float * restrict ps, int N)
  155. {
  156. register int i, j;
  157. register float xx;
  158. POWERSPECTRUM_START();
  159. xx = X[0];
  160. ps[0]=MULT16_16(xx,xx);
  161. #pragma TCS_unroll=4
  162. #pragma TCS_unrollexact=1
  163. for (i=1,j=1;i<N-1;i+=2,j++)
  164. { register float xi, xii;
  165. xi = X[i];
  166. xii = X[i+1];
  167. ps[j] = MULT16_16(xi,xi) + MULT16_16(xii,xii);
  168. }
  169. #pragma TCS_unrollexact=0
  170. #pragma TCS_unroll=0
  171. xx = X[i];
  172. ps[j]=MULT16_16(xx,xx);
  173. POWERSPECTRUM_STOP();
  174. }
  175. #endif
  176. #endif