_kiss_fft_guts.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*Copyright (c) 2003-2004, Mark Borgerding
  2. All rights reserved.
  3. Redistribution and use in source and binary forms, with or without
  4. modification, are permitted provided that the following conditions are met:
  5. * Redistributions of source code must retain the above copyright notice,
  6. this list of conditions and the following disclaimer.
  7. * Redistributions in binary form must reproduce the above copyright notice,
  8. this list of conditions and the following disclaimer in the
  9. documentation and/or other materials provided with the distribution.
  10. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  11. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  12. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  13. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  14. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  15. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  16. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  17. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  18. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  19. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  20. POSSIBILITY OF SUCH DAMAGE.*/
  21. #ifndef KISS_FFT_GUTS_H
  22. #define KISS_FFT_GUTS_H
  23. #define MIN(a,b) ((a)<(b) ? (a):(b))
  24. #define MAX(a,b) ((a)>(b) ? (a):(b))
  25. /* kiss_fft.h
  26. defines kiss_fft_scalar as either short or a float type
  27. and defines
  28. typedef struct { kiss_fft_scalar r; kiss_fft_scalar i; }kiss_fft_cpx; */
  29. #include "kiss_fft.h"
  30. /*
  31. Explanation of macros dealing with complex math:
  32. C_MUL(m,a,b) : m = a*b
  33. C_FIXDIV( c , div ) : if a fixed point impl., c /= div. noop otherwise
  34. C_SUB( res, a,b) : res = a - b
  35. C_SUBFROM( res , a) : res -= a
  36. C_ADDTO( res , a) : res += a
  37. * */
  38. #ifdef FIXED_POINT
  39. #include "arch.h"
  40. #define SAMP_MAX 2147483647
  41. #define TWID_MAX 32767
  42. #define TRIG_UPSCALE 1
  43. #define SAMP_MIN -SAMP_MAX
  44. # define S_MUL(a,b) MULT16_32_Q15(b, a)
  45. # define C_MUL(m,a,b) \
  46. do{ (m).r = SUB32_ovflw(S_MUL((a).r,(b).r) , S_MUL((a).i,(b).i)); \
  47. (m).i = ADD32_ovflw(S_MUL((a).r,(b).i) , S_MUL((a).i,(b).r)); }while(0)
  48. # define C_MULC(m,a,b) \
  49. do{ (m).r = ADD32_ovflw(S_MUL((a).r,(b).r) , S_MUL((a).i,(b).i)); \
  50. (m).i = SUB32_ovflw(S_MUL((a).i,(b).r) , S_MUL((a).r,(b).i)); }while(0)
  51. # define C_MULBYSCALAR( c, s ) \
  52. do{ (c).r = S_MUL( (c).r , s ) ;\
  53. (c).i = S_MUL( (c).i , s ) ; }while(0)
  54. # define DIVSCALAR(x,k) \
  55. (x) = S_MUL( x, (TWID_MAX-((k)>>1))/(k)+1 )
  56. # define C_FIXDIV(c,div) \
  57. do { DIVSCALAR( (c).r , div); \
  58. DIVSCALAR( (c).i , div); }while (0)
  59. #define C_ADD( res, a,b)\
  60. do {(res).r=ADD32_ovflw((a).r,(b).r); (res).i=ADD32_ovflw((a).i,(b).i); \
  61. }while(0)
  62. #define C_SUB( res, a,b)\
  63. do {(res).r=SUB32_ovflw((a).r,(b).r); (res).i=SUB32_ovflw((a).i,(b).i); \
  64. }while(0)
  65. #define C_ADDTO( res , a)\
  66. do {(res).r = ADD32_ovflw((res).r, (a).r); (res).i = ADD32_ovflw((res).i,(a).i);\
  67. }while(0)
  68. #define C_SUBFROM( res , a)\
  69. do {(res).r = ADD32_ovflw((res).r,(a).r); (res).i = SUB32_ovflw((res).i,(a).i); \
  70. }while(0)
  71. #if defined(OPUS_ARM_INLINE_ASM)
  72. #include "arm/kiss_fft_armv4.h"
  73. #endif
  74. #if defined(OPUS_ARM_INLINE_EDSP)
  75. #include "arm/kiss_fft_armv5e.h"
  76. #endif
  77. #if defined(MIPSr1_ASM)
  78. #include "mips/kiss_fft_mipsr1.h"
  79. #endif
  80. #else /* not FIXED_POINT*/
  81. # define S_MUL(a,b) ( (a)*(b) )
  82. #define C_MUL(m,a,b) \
  83. do{ (m).r = (a).r*(b).r - (a).i*(b).i;\
  84. (m).i = (a).r*(b).i + (a).i*(b).r; }while(0)
  85. #define C_MULC(m,a,b) \
  86. do{ (m).r = (a).r*(b).r + (a).i*(b).i;\
  87. (m).i = (a).i*(b).r - (a).r*(b).i; }while(0)
  88. #define C_MUL4(m,a,b) C_MUL(m,a,b)
  89. # define C_FIXDIV(c,div) /* NOOP */
  90. # define C_MULBYSCALAR( c, s ) \
  91. do{ (c).r *= (s);\
  92. (c).i *= (s); }while(0)
  93. #endif
  94. #ifndef CHECK_OVERFLOW_OP
  95. # define CHECK_OVERFLOW_OP(a,op,b) /* noop */
  96. #endif
  97. #ifndef C_ADD
  98. #define C_ADD( res, a,b)\
  99. do { \
  100. CHECK_OVERFLOW_OP((a).r,+,(b).r)\
  101. CHECK_OVERFLOW_OP((a).i,+,(b).i)\
  102. (res).r=(a).r+(b).r; (res).i=(a).i+(b).i; \
  103. }while(0)
  104. #define C_SUB( res, a,b)\
  105. do { \
  106. CHECK_OVERFLOW_OP((a).r,-,(b).r)\
  107. CHECK_OVERFLOW_OP((a).i,-,(b).i)\
  108. (res).r=(a).r-(b).r; (res).i=(a).i-(b).i; \
  109. }while(0)
  110. #define C_ADDTO( res , a)\
  111. do { \
  112. CHECK_OVERFLOW_OP((res).r,+,(a).r)\
  113. CHECK_OVERFLOW_OP((res).i,+,(a).i)\
  114. (res).r += (a).r; (res).i += (a).i;\
  115. }while(0)
  116. #define C_SUBFROM( res , a)\
  117. do {\
  118. CHECK_OVERFLOW_OP((res).r,-,(a).r)\
  119. CHECK_OVERFLOW_OP((res).i,-,(a).i)\
  120. (res).r -= (a).r; (res).i -= (a).i; \
  121. }while(0)
  122. #endif /* C_ADD defined */
  123. #ifdef FIXED_POINT
  124. /*# define KISS_FFT_COS(phase) TRIG_UPSCALE*floor(MIN(32767,MAX(-32767,.5+32768 * cos (phase))))
  125. # define KISS_FFT_SIN(phase) TRIG_UPSCALE*floor(MIN(32767,MAX(-32767,.5+32768 * sin (phase))))*/
  126. # define KISS_FFT_COS(phase) floor(.5+TWID_MAX*cos (phase))
  127. # define KISS_FFT_SIN(phase) floor(.5+TWID_MAX*sin (phase))
  128. # define HALF_OF(x) ((x)>>1)
  129. #elif defined(USE_SIMD)
  130. # define KISS_FFT_COS(phase) _mm_set1_ps( cos(phase) )
  131. # define KISS_FFT_SIN(phase) _mm_set1_ps( sin(phase) )
  132. # define HALF_OF(x) ((x)*_mm_set1_ps(.5f))
  133. #else
  134. # define KISS_FFT_COS(phase) (kiss_fft_scalar) cos(phase)
  135. # define KISS_FFT_SIN(phase) (kiss_fft_scalar) sin(phase)
  136. # define HALF_OF(x) ((x)*.5f)
  137. #endif
  138. #define kf_cexp(x,phase) \
  139. do{ \
  140. (x)->r = KISS_FFT_COS(phase);\
  141. (x)->i = KISS_FFT_SIN(phase);\
  142. }while(0)
  143. #define kf_cexp2(x,phase) \
  144. do{ \
  145. (x)->r = TRIG_UPSCALE*celt_cos_norm((phase));\
  146. (x)->i = TRIG_UPSCALE*celt_cos_norm((phase)-32768);\
  147. }while(0)
  148. #endif /* KISS_FFT_GUTS_H */