mathops.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /* Copyright (c) 2002-2008 Jean-Marc Valin
  2. Copyright (c) 2007-2008 CSIRO
  3. Copyright (c) 2007-2009 Xiph.Org Foundation
  4. Written by Jean-Marc Valin */
  5. /**
  6. @file mathops.h
  7. @brief Various math functions
  8. */
  9. /*
  10. Redistribution and use in source and binary forms, with or without
  11. modification, are permitted provided that the following conditions
  12. are met:
  13. - Redistributions of source code must retain the above copyright
  14. notice, this list of conditions and the following disclaimer.
  15. - Redistributions in binary form must reproduce the above copyright
  16. notice, this list of conditions and the following disclaimer in the
  17. documentation and/or other materials provided with the distribution.
  18. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
  22. OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  23. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  24. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  25. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  26. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  27. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #ifndef MATHOPS_H
  31. #define MATHOPS_H
  32. #include "arch.h"
  33. #include "entcode.h"
  34. #include "os_support.h"
  35. #define PI 3.141592653f
  36. /* Multiplies two 16-bit fractional values. Bit-exactness of this macro is important */
  37. #define FRAC_MUL16(a,b) ((16384+((opus_int32)(opus_int16)(a)*(opus_int16)(b)))>>15)
  38. unsigned isqrt32(opus_uint32 _val);
  39. /* CELT doesn't need it for fixed-point, by analysis.c does. */
  40. #if !defined(FIXED_POINT) || defined(ANALYSIS_C)
  41. #define cA 0.43157974f
  42. #define cB 0.67848403f
  43. #define cC 0.08595542f
  44. #define cE ((float)PI/2)
  45. static OPUS_INLINE float fast_atan2f(float y, float x) {
  46. float x2, y2;
  47. x2 = x*x;
  48. y2 = y*y;
  49. /* For very small values, we don't care about the answer, so
  50. we can just return 0. */
  51. if (x2 + y2 < 1e-18f)
  52. {
  53. return 0;
  54. }
  55. if(x2<y2){
  56. float den = (y2 + cB*x2) * (y2 + cC*x2);
  57. return -x*y*(y2 + cA*x2) / den + (y<0 ? -cE : cE);
  58. }else{
  59. float den = (x2 + cB*y2) * (x2 + cC*y2);
  60. return x*y*(x2 + cA*y2) / den + (y<0 ? -cE : cE) - (x*y<0 ? -cE : cE);
  61. }
  62. }
  63. #undef cA
  64. #undef cB
  65. #undef cC
  66. #undef cE
  67. #endif
  68. #ifndef OVERRIDE_CELT_MAXABS16
  69. static OPUS_INLINE opus_val32 celt_maxabs16(const opus_val16 *x, int len)
  70. {
  71. int i;
  72. opus_val16 maxval = 0;
  73. opus_val16 minval = 0;
  74. for (i=0;i<len;i++)
  75. {
  76. maxval = MAX16(maxval, x[i]);
  77. minval = MIN16(minval, x[i]);
  78. }
  79. return MAX32(EXTEND32(maxval),-EXTEND32(minval));
  80. }
  81. #endif
  82. #ifndef OVERRIDE_CELT_MAXABS32
  83. #ifdef FIXED_POINT
  84. static OPUS_INLINE opus_val32 celt_maxabs32(const opus_val32 *x, int len)
  85. {
  86. int i;
  87. opus_val32 maxval = 0;
  88. opus_val32 minval = 0;
  89. for (i=0;i<len;i++)
  90. {
  91. maxval = MAX32(maxval, x[i]);
  92. minval = MIN32(minval, x[i]);
  93. }
  94. return MAX32(maxval, -minval);
  95. }
  96. #else
  97. #define celt_maxabs32(x,len) celt_maxabs16(x,len)
  98. #endif
  99. #endif
  100. #ifndef FIXED_POINT
  101. #define celt_sqrt(x) ((float)sqrt(x))
  102. #define celt_rsqrt(x) (1.f/celt_sqrt(x))
  103. #define celt_rsqrt_norm(x) (celt_rsqrt(x))
  104. #define celt_cos_norm(x) ((float)cos((.5f*PI)*(x)))
  105. #define celt_rcp(x) (1.f/(x))
  106. #define celt_div(a,b) ((a)/(b))
  107. #define frac_div32(a,b) ((float)(a)/(b))
  108. #ifdef FLOAT_APPROX
  109. /* Note: This assumes radix-2 floating point with the exponent at bits 23..30 and an offset of 127
  110. denorm, +/- inf and NaN are *not* handled */
  111. /** Base-2 log approximation (log2(x)). */
  112. static OPUS_INLINE float celt_log2(float x)
  113. {
  114. int integer;
  115. float frac;
  116. union {
  117. float f;
  118. opus_uint32 i;
  119. } in;
  120. in.f = x;
  121. integer = (in.i>>23)-127;
  122. in.i -= integer<<23;
  123. frac = in.f - 1.5f;
  124. frac = -0.41445418f + frac*(0.95909232f
  125. + frac*(-0.33951290f + frac*0.16541097f));
  126. return 1+integer+frac;
  127. }
  128. /** Base-2 exponential approximation (2^x). */
  129. static OPUS_INLINE float celt_exp2(float x)
  130. {
  131. int integer;
  132. float frac;
  133. union {
  134. float f;
  135. opus_uint32 i;
  136. } res;
  137. integer = floor(x);
  138. if (integer < -50)
  139. return 0;
  140. frac = x-integer;
  141. /* K0 = 1, K1 = log(2), K2 = 3-4*log(2), K3 = 3*log(2) - 2 */
  142. res.f = 0.99992522f + frac * (0.69583354f
  143. + frac * (0.22606716f + 0.078024523f*frac));
  144. res.i = (res.i + (integer<<23)) & 0x7fffffff;
  145. return res.f;
  146. }
  147. #else
  148. #define celt_log2(x) ((float)(1.442695040888963387*log(x)))
  149. #define celt_exp2(x) ((float)exp(0.6931471805599453094*(x)))
  150. #endif
  151. #endif
  152. #ifdef FIXED_POINT
  153. #include "os_support.h"
  154. #ifndef OVERRIDE_CELT_ILOG2
  155. /** Integer log in base2. Undefined for zero and negative numbers */
  156. static OPUS_INLINE opus_int16 celt_ilog2(opus_int32 x)
  157. {
  158. celt_sig_assert(x>0);
  159. return EC_ILOG(x)-1;
  160. }
  161. #endif
  162. /** Integer log in base2. Defined for zero, but not for negative numbers */
  163. static OPUS_INLINE opus_int16 celt_zlog2(opus_val32 x)
  164. {
  165. return x <= 0 ? 0 : celt_ilog2(x);
  166. }
  167. opus_val16 celt_rsqrt_norm(opus_val32 x);
  168. opus_val32 celt_sqrt(opus_val32 x);
  169. opus_val16 celt_cos_norm(opus_val32 x);
  170. /** Base-2 logarithm approximation (log2(x)). (Q14 input, Q10 output) */
  171. static OPUS_INLINE opus_val16 celt_log2(opus_val32 x)
  172. {
  173. int i;
  174. opus_val16 n, frac;
  175. /* -0.41509302963303146, 0.9609890551383969, -0.31836011537636605,
  176. 0.15530808010959576, -0.08556153059057618 */
  177. static const opus_val16 C[5] = {-6801+(1<<(13-DB_SHIFT)), 15746, -5217, 2545, -1401};
  178. if (x==0)
  179. return -32767;
  180. i = celt_ilog2(x);
  181. n = VSHR32(x,i-15)-32768-16384;
  182. frac = ADD16(C[0], MULT16_16_Q15(n, ADD16(C[1], MULT16_16_Q15(n, ADD16(C[2], MULT16_16_Q15(n, ADD16(C[3], MULT16_16_Q15(n, C[4]))))))));
  183. return SHL16(i-13,DB_SHIFT)+SHR16(frac,14-DB_SHIFT);
  184. }
  185. /*
  186. K0 = 1
  187. K1 = log(2)
  188. K2 = 3-4*log(2)
  189. K3 = 3*log(2) - 2
  190. */
  191. #define D0 16383
  192. #define D1 22804
  193. #define D2 14819
  194. #define D3 10204
  195. static OPUS_INLINE opus_val32 celt_exp2_frac(opus_val16 x)
  196. {
  197. opus_val16 frac;
  198. frac = SHL16(x, 4);
  199. return ADD16(D0, MULT16_16_Q15(frac, ADD16(D1, MULT16_16_Q15(frac, ADD16(D2 , MULT16_16_Q15(D3,frac))))));
  200. }
  201. /** Base-2 exponential approximation (2^x). (Q10 input, Q16 output) */
  202. static OPUS_INLINE opus_val32 celt_exp2(opus_val16 x)
  203. {
  204. int integer;
  205. opus_val16 frac;
  206. integer = SHR16(x,10);
  207. if (integer>14)
  208. return 0x7f000000;
  209. else if (integer < -15)
  210. return 0;
  211. frac = celt_exp2_frac(x-SHL16(integer,10));
  212. return VSHR32(EXTEND32(frac), -integer-2);
  213. }
  214. opus_val32 celt_rcp(opus_val32 x);
  215. #define celt_div(a,b) MULT32_32_Q31((opus_val32)(a),celt_rcp(b))
  216. opus_val32 frac_div32(opus_val32 a, opus_val32 b);
  217. #define M1 32767
  218. #define M2 -21
  219. #define M3 -11943
  220. #define M4 4936
  221. /* Atan approximation using a 4th order polynomial. Input is in Q15 format
  222. and normalized by pi/4. Output is in Q15 format */
  223. static OPUS_INLINE opus_val16 celt_atan01(opus_val16 x)
  224. {
  225. return MULT16_16_P15(x, ADD32(M1, MULT16_16_P15(x, ADD32(M2, MULT16_16_P15(x, ADD32(M3, MULT16_16_P15(M4, x)))))));
  226. }
  227. #undef M1
  228. #undef M2
  229. #undef M3
  230. #undef M4
  231. /* atan2() approximation valid for positive input values */
  232. static OPUS_INLINE opus_val16 celt_atan2p(opus_val16 y, opus_val16 x)
  233. {
  234. if (y < x)
  235. {
  236. opus_val32 arg;
  237. arg = celt_div(SHL32(EXTEND32(y),15),x);
  238. if (arg >= 32767)
  239. arg = 32767;
  240. return SHR16(celt_atan01(EXTRACT16(arg)),1);
  241. } else {
  242. opus_val32 arg;
  243. arg = celt_div(SHL32(EXTEND32(x),15),y);
  244. if (arg >= 32767)
  245. arg = 32767;
  246. return 25736-SHR16(celt_atan01(EXTRACT16(arg)),1);
  247. }
  248. }
  249. #endif /* FIXED_POINT */
  250. #endif /* MATHOPS_H */