SKP_Silk_Inlines.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /***********************************************************************
  2. Copyright (c) 2006-2012, Skype Limited. All rights reserved.
  3. Redistribution and use in source and binary forms, with or without
  4. modification, (subject to the limitations in the disclaimer below)
  5. are permitted provided that the following conditions are met:
  6. - Redistributions of source code must retain the above copyright notice,
  7. this list of conditions and the following disclaimer.
  8. - Redistributions in binary form must reproduce the above copyright
  9. notice, this list of conditions and the following disclaimer in the
  10. documentation and/or other materials provided with the distribution.
  11. - Neither the name of Skype Limited, nor the names of specific
  12. contributors, may be used to endorse or promote products derived from
  13. this software without specific prior written permission.
  14. NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED
  15. BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  16. CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
  17. BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  18. FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  19. COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  20. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  21. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  22. USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  23. ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. ***********************************************************************/
  27. /*! \file SKP_Silk_Inlines.h
  28. * \brief SKP_Silk_Inlines.h defines inline signal processing functions.
  29. */
  30. #ifndef _SKP_SILK_FIX_INLINES_H_
  31. #define _SKP_SILK_FIX_INLINES_H_
  32. #include <assert.h>
  33. #ifdef __cplusplus
  34. extern "C"
  35. {
  36. #endif
  37. /* count leading zeros of SKP_int64 */
  38. SKP_INLINE SKP_int32 SKP_Silk_CLZ64(SKP_int64 in)
  39. {
  40. SKP_int32 in_upper;
  41. in_upper = (SKP_int32)SKP_RSHIFT64(in, 32);
  42. if (in_upper == 0) {
  43. /* Search in the lower 32 bits */
  44. return 32 + SKP_Silk_CLZ32( (SKP_int32) in );
  45. } else {
  46. /* Search in the upper 32 bits */
  47. return SKP_Silk_CLZ32( in_upper );
  48. }
  49. }
  50. /* get number of leading zeros and fractional part (the bits right after the leading one */
  51. SKP_INLINE void SKP_Silk_CLZ_FRAC(SKP_int32 in, /* I: input */
  52. SKP_int32 *lz, /* O: number of leading zeros */
  53. SKP_int32 *frac_Q7) /* O: the 7 bits right after the leading one */
  54. {
  55. SKP_int32 lzeros = SKP_Silk_CLZ32(in);
  56. * lz = lzeros;
  57. * frac_Q7 = SKP_ROR32(in, 24 - lzeros) & 0x7f;
  58. }
  59. /* Approximation of square root */
  60. /* Accuracy: < +/- 10% for output values > 15 */
  61. /* < +/- 2.5% for output values > 120 */
  62. SKP_INLINE SKP_int32 SKP_Silk_SQRT_APPROX(SKP_int32 x)
  63. {
  64. SKP_int32 y, lz, frac_Q7;
  65. if( x <= 0 ) {
  66. return 0;
  67. }
  68. SKP_Silk_CLZ_FRAC(x, &lz, &frac_Q7);
  69. if( lz & 1 ) {
  70. y = 32768;
  71. } else {
  72. y = 46214; /* 46214 = sqrt(2) * 32768 */
  73. }
  74. /* get scaling right */
  75. y >>= SKP_RSHIFT(lz, 1);
  76. /* increment using fractional part of input */
  77. y = SKP_SMLAWB(y, y, SKP_SMULBB(213, frac_Q7));
  78. return y;
  79. }
  80. /* returns the number of left shifts before overflow for a 16 bit number (ITU definition with norm(0)=0) */
  81. SKP_INLINE SKP_int32 SKP_Silk_norm16(SKP_int16 a) {
  82. SKP_int32 a32;
  83. /* if ((a == 0) || (a == SKP_int16_MIN)) return(0); */
  84. if ((a << 1) == 0) return(0);
  85. a32 = a;
  86. /* if (a32 < 0) a32 = -a32 - 1; */
  87. a32 ^= SKP_RSHIFT(a32, 31);
  88. return SKP_Silk_CLZ32(a32) - 17;
  89. }
  90. /* returns the number of left shifts before overflow for a 32 bit number (ITU definition with norm(0)=0) */
  91. SKP_INLINE SKP_int32 SKP_Silk_norm32(SKP_int32 a) {
  92. /* if ((a == 0) || (a == SKP_int32_MIN)) return(0); */
  93. if ((a << 1) == 0) return(0);
  94. /* if (a < 0) a = -a - 1; */
  95. a ^= SKP_RSHIFT(a, 31);
  96. return SKP_Silk_CLZ32(a) - 1;
  97. }
  98. /* Divide two int32 values and return result as int32 in a given Q-domain */
  99. SKP_INLINE SKP_int32 SKP_DIV32_varQ( /* O returns a good approximation of "(a32 << Qres) / b32" */
  100. const SKP_int32 a32, /* I numerator (Q0) */
  101. const SKP_int32 b32, /* I denominator (Q0) */
  102. const SKP_int Qres /* I Q-domain of result (>= 0) */
  103. )
  104. {
  105. SKP_int a_headrm, b_headrm, lshift;
  106. SKP_int32 b32_inv, a32_nrm, b32_nrm, result;
  107. SKP_assert( b32 != 0 );
  108. SKP_assert( Qres >= 0 );
  109. /* Compute number of bits head room and normalize inputs */
  110. a_headrm = SKP_Silk_CLZ32( SKP_abs(a32) ) - 1;
  111. a32_nrm = SKP_LSHIFT(a32, a_headrm); /* Q: a_headrm */
  112. b_headrm = SKP_Silk_CLZ32( SKP_abs(b32) ) - 1;
  113. b32_nrm = SKP_LSHIFT(b32, b_headrm); /* Q: b_headrm */
  114. /* Inverse of b32, with 14 bits of precision */
  115. b32_inv = SKP_DIV32_16( SKP_int32_MAX >> 2, SKP_RSHIFT(b32_nrm, 16) ); /* Q: 29 + 16 - b_headrm */
  116. /* First approximation */
  117. result = SKP_SMULWB(a32_nrm, b32_inv); /* Q: 29 + a_headrm - b_headrm */
  118. /* Compute residual by subtracting product of denominator and first approximation */
  119. a32_nrm -= SKP_LSHIFT_ovflw( SKP_SMMUL(b32_nrm, result), 3 ); /* Q: a_headrm */
  120. /* Refinement */
  121. result = SKP_SMLAWB(result, a32_nrm, b32_inv); /* Q: 29 + a_headrm - b_headrm */
  122. /* Convert to Qres domain */
  123. lshift = 29 + a_headrm - b_headrm - Qres;
  124. if( lshift <= 0 ) {
  125. return SKP_LSHIFT_SAT32(result, -lshift);
  126. } else {
  127. if( lshift < 32){
  128. return SKP_RSHIFT(result, lshift);
  129. } else {
  130. /* Avoid undefined result */
  131. return 0;
  132. }
  133. }
  134. }
  135. /* Invert int32 value and return result as int32 in a given Q-domain */
  136. SKP_INLINE SKP_int32 SKP_INVERSE32_varQ( /* O returns a good approximation of "(1 << Qres) / b32" */
  137. const SKP_int32 b32, /* I denominator (Q0) */
  138. const SKP_int Qres /* I Q-domain of result (> 0) */
  139. )
  140. {
  141. SKP_int b_headrm, lshift;
  142. SKP_int32 b32_inv, b32_nrm, err_Q32, result;
  143. SKP_assert( b32 != 0 );
  144. SKP_assert( b32 != SKP_int32_MIN ); /* SKP_int32_MIN is not handled by SKP_abs */
  145. SKP_assert( Qres > 0 );
  146. /* Compute number of bits head room and normalize input */
  147. b_headrm = SKP_Silk_CLZ32( SKP_abs(b32) ) - 1;
  148. b32_nrm = SKP_LSHIFT(b32, b_headrm); /* Q: b_headrm */
  149. /* Inverse of b32, with 14 bits of precision */
  150. b32_inv = SKP_DIV32_16( SKP_int32_MAX >> 2, SKP_RSHIFT(b32_nrm, 16) ); /* Q: 29 + 16 - b_headrm */
  151. /* First approximation */
  152. result = SKP_LSHIFT(b32_inv, 16); /* Q: 61 - b_headrm */
  153. /* Compute residual by subtracting product of denominator and first approximation from one */
  154. err_Q32 = SKP_LSHIFT_ovflw( -SKP_SMULWB(b32_nrm, b32_inv), 3 ); /* Q32 */
  155. /* Refinement */
  156. result = SKP_SMLAWW(result, err_Q32, b32_inv); /* Q: 61 - b_headrm */
  157. /* Convert to Qres domain */
  158. lshift = 61 - b_headrm - Qres;
  159. if( lshift <= 0 ) {
  160. return SKP_LSHIFT_SAT32(result, -lshift);
  161. } else {
  162. if( lshift < 32){
  163. return SKP_RSHIFT(result, lshift);
  164. }else{
  165. /* Avoid undefined result */
  166. return 0;
  167. }
  168. }
  169. }
  170. #define SKP_SIN_APPROX_CONST0 (1073735400)
  171. #define SKP_SIN_APPROX_CONST1 (-82778932)
  172. #define SKP_SIN_APPROX_CONST2 (1059577)
  173. #define SKP_SIN_APPROX_CONST3 (-5013)
  174. /* Sine approximation; an input of 65536 corresponds to 2 * pi */
  175. /* Uses polynomial expansion of the input to the power 0, 2, 4 and 6 */
  176. /* The relative error is below 1e-5 */
  177. SKP_INLINE SKP_int32 SKP_Silk_SIN_APPROX_Q24( /* O returns approximately 2^24 * sin(x * 2 * pi / 65536) */
  178. SKP_int32 x
  179. )
  180. {
  181. SKP_int y_Q30;
  182. /* Keep only bottom 16 bits (the function repeats itself with period 65536) */
  183. x &= 65535;
  184. /* Split range in four quadrants */
  185. if( x <= 32768 ) {
  186. if( x < 16384 ) {
  187. /* Return cos(pi/2 - x) */
  188. x = 16384 - x;
  189. } else {
  190. /* Return cos(x - pi/2) */
  191. x -= 16384;
  192. }
  193. if( x < 1100 ) {
  194. /* Special case: high accuracy */
  195. return SKP_SMLAWB( 1 << 24, SKP_MUL( x, x ), -5053 );
  196. }
  197. x = SKP_SMULWB( SKP_LSHIFT( x, 8 ), x ); /* contains x^2 in Q20 */
  198. y_Q30 = SKP_SMLAWB( SKP_SIN_APPROX_CONST2, x, SKP_SIN_APPROX_CONST3 );
  199. y_Q30 = SKP_SMLAWW( SKP_SIN_APPROX_CONST1, x, y_Q30 );
  200. y_Q30 = SKP_SMLAWW( SKP_SIN_APPROX_CONST0 + 66, x, y_Q30 );
  201. } else {
  202. if( x < 49152 ) {
  203. /* Return -cos(3*pi/2 - x) */
  204. x = 49152 - x;
  205. } else {
  206. /* Return -cos(x - 3*pi/2) */
  207. x -= 49152;
  208. }
  209. if( x < 1100 ) {
  210. /* Special case: high accuracy */
  211. return SKP_SMLAWB( -1 << 24, SKP_MUL( x, x ), 5053 );
  212. }
  213. x = SKP_SMULWB( SKP_LSHIFT( x, 8 ), x ); /* contains x^2 in Q20 */
  214. y_Q30 = SKP_SMLAWB( -SKP_SIN_APPROX_CONST2, x, -SKP_SIN_APPROX_CONST3 );
  215. y_Q30 = SKP_SMLAWW( -SKP_SIN_APPROX_CONST1, x, y_Q30 );
  216. y_Q30 = SKP_SMLAWW( -SKP_SIN_APPROX_CONST0, x, y_Q30 );
  217. }
  218. return SKP_RSHIFT_ROUND( y_Q30, 6 );
  219. }
  220. /* Cosine approximation; an input of 65536 corresponds to 2 * pi */
  221. /* The relative error is below 1e-5 */
  222. SKP_INLINE SKP_int32 SKP_Silk_COS_APPROX_Q24( /* O returns approximately 2^24 * cos(x * 2 * pi / 65536) */
  223. SKP_int32 x
  224. )
  225. {
  226. return SKP_Silk_SIN_APPROX_Q24( x + 16384 );
  227. }
  228. #ifdef __cplusplus
  229. }
  230. #endif
  231. #endif /*_SKP_SILK_FIX_INLINES_H_*/