math_approx.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /* Copyright (C) 2002 Jean-Marc Valin */
  2. /**
  3. @file math_approx.h
  4. @brief Various math approximation functions for Speex
  5. */
  6. /*
  7. Redistribution and use in source and binary forms, with or without
  8. modification, are permitted provided that the following conditions
  9. are met:
  10. - Redistributions of source code must retain the above copyright
  11. notice, this list of conditions and the following disclaimer.
  12. - Redistributions in binary form must reproduce the above copyright
  13. notice, this list of conditions and the following disclaimer in the
  14. documentation and/or other materials provided with the distribution.
  15. - Neither the name of the Xiph.org Foundation nor the names of its
  16. contributors may be used to endorse or promote products derived from
  17. this software without specific prior written permission.
  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 FOUNDATION OR
  22. 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 MATH_APPROX_H
  31. #define MATH_APPROX_H
  32. #include "arch.h"
  33. #include "os_support.h"
  34. #ifndef FIXED_POINT
  35. #define spx_sqrt sqrt
  36. #define spx_acos acos
  37. #define spx_exp exp
  38. #define spx_cos_norm(x) (cos((.5f*M_PI)*(x)))
  39. #define spx_atan atan
  40. /** Generate a pseudo-random number */
  41. static SPEEX_INLINE spx_word16_t speex_rand(spx_word16_t std, spx_int32_t *seed)
  42. {
  43. const unsigned int jflone = 0x3f800000;
  44. const unsigned int jflmsk = 0x007fffff;
  45. union {int i; float f;} ran;
  46. *seed = 1664525 * *seed + 1013904223;
  47. ran.i = jflone | (jflmsk & *seed);
  48. ran.f -= 1.5;
  49. return 3.4642*std*ran.f;
  50. }
  51. #endif
  52. static SPEEX_INLINE spx_int16_t spx_ilog2(spx_uint32_t x)
  53. {
  54. int r=0;
  55. if (x>=(spx_int32_t)65536)
  56. {
  57. x >>= 16;
  58. r += 16;
  59. }
  60. if (x>=256)
  61. {
  62. x >>= 8;
  63. r += 8;
  64. }
  65. if (x>=16)
  66. {
  67. x >>= 4;
  68. r += 4;
  69. }
  70. if (x>=4)
  71. {
  72. x >>= 2;
  73. r += 2;
  74. }
  75. if (x>=2)
  76. {
  77. r += 1;
  78. }
  79. return r;
  80. }
  81. static SPEEX_INLINE spx_int16_t spx_ilog4(spx_uint32_t x)
  82. {
  83. int r=0;
  84. if (x>=(spx_int32_t)65536)
  85. {
  86. x >>= 16;
  87. r += 8;
  88. }
  89. if (x>=256)
  90. {
  91. x >>= 8;
  92. r += 4;
  93. }
  94. if (x>=16)
  95. {
  96. x >>= 4;
  97. r += 2;
  98. }
  99. if (x>=4)
  100. {
  101. r += 1;
  102. }
  103. return r;
  104. }
  105. #ifdef FIXED_POINT
  106. /** Generate a pseudo-random number */
  107. static SPEEX_INLINE spx_word16_t speex_rand(spx_word16_t std, spx_int32_t *seed)
  108. {
  109. spx_word32_t res;
  110. *seed = 1664525 * *seed + 1013904223;
  111. res = MULT16_16(EXTRACT16(SHR32(*seed,16)),std);
  112. return EXTRACT16(PSHR32(SUB32(res, SHR32(res, 3)),14));
  113. }
  114. /* sqrt(x) ~= 0.22178 + 1.29227*x - 0.77070*x^2 + 0.25723*x^3 (for .25 < x < 1) */
  115. /*#define C0 3634
  116. #define C1 21173
  117. #define C2 -12627
  118. #define C3 4215*/
  119. /* sqrt(x) ~= 0.22178 + 1.29227*x - 0.77070*x^2 + 0.25659*x^3 (for .25 < x < 1) */
  120. #define C0 3634
  121. #define C1 21173
  122. #define C2 -12627
  123. #define C3 4204
  124. static SPEEX_INLINE spx_word16_t spx_sqrt(spx_word32_t x)
  125. {
  126. int k;
  127. spx_word32_t rt;
  128. k = spx_ilog4(x)-6;
  129. x = VSHR32(x, (k<<1));
  130. rt = ADD16(C0, MULT16_16_Q14(x, ADD16(C1, MULT16_16_Q14(x, ADD16(C2, MULT16_16_Q14(x, (C3)))))));
  131. rt = VSHR32(rt,7-k);
  132. return rt;
  133. }
  134. /* log(x) ~= -2.18151 + 4.20592*x - 2.88938*x^2 + 0.86535*x^3 (for .5 < x < 1) */
  135. #define A1 16469
  136. #define A2 2242
  137. #define A3 1486
  138. static SPEEX_INLINE spx_word16_t spx_acos(spx_word16_t x)
  139. {
  140. int s=0;
  141. spx_word16_t ret;
  142. spx_word16_t sq;
  143. if (x<0)
  144. {
  145. s=1;
  146. x = NEG16(x);
  147. }
  148. x = SUB16(16384,x);
  149. x = x >> 1;
  150. sq = MULT16_16_Q13(x, ADD16(A1, MULT16_16_Q13(x, ADD16(A2, MULT16_16_Q13(x, (A3))))));
  151. ret = spx_sqrt(SHL32(EXTEND32(sq),13));
  152. /*ret = spx_sqrt(67108864*(-1.6129e-04 + 2.0104e+00*f + 2.7373e-01*f*f + 1.8136e-01*f*f*f));*/
  153. if (s)
  154. ret = SUB16(25736,ret);
  155. return ret;
  156. }
  157. #define K1 8192
  158. #define K2 -4096
  159. #define K3 340
  160. #define K4 -10
  161. static SPEEX_INLINE spx_word16_t spx_cos(spx_word16_t x)
  162. {
  163. spx_word16_t x2;
  164. if (x<12868)
  165. {
  166. x2 = MULT16_16_P13(x,x);
  167. return ADD32(K1, MULT16_16_P13(x2, ADD32(K2, MULT16_16_P13(x2, ADD32(K3, MULT16_16_P13(K4, x2))))));
  168. } else {
  169. x = SUB16(25736,x);
  170. x2 = MULT16_16_P13(x,x);
  171. return SUB32(-K1, MULT16_16_P13(x2, ADD32(K2, MULT16_16_P13(x2, ADD32(K3, MULT16_16_P13(K4, x2))))));
  172. }
  173. }
  174. #define L1 32767
  175. #define L2 -7651
  176. #define L3 8277
  177. #define L4 -626
  178. static SPEEX_INLINE spx_word16_t _spx_cos_pi_2(spx_word16_t x)
  179. {
  180. spx_word16_t x2;
  181. x2 = MULT16_16_P15(x,x);
  182. return ADD16(1,MIN16(32766,ADD32(SUB16(L1,x2), MULT16_16_P15(x2, ADD32(L2, MULT16_16_P15(x2, ADD32(L3, MULT16_16_P15(L4, x2))))))));
  183. }
  184. static SPEEX_INLINE spx_word16_t spx_cos_norm(spx_word32_t x)
  185. {
  186. x = x&0x0001ffff;
  187. if (x>SHL32(EXTEND32(1), 16))
  188. x = SUB32(SHL32(EXTEND32(1), 17),x);
  189. if (x&0x00007fff)
  190. {
  191. if (x<SHL32(EXTEND32(1), 15))
  192. {
  193. return _spx_cos_pi_2(EXTRACT16(x));
  194. } else {
  195. return NEG32(_spx_cos_pi_2(EXTRACT16(65536-x)));
  196. }
  197. } else {
  198. if (x&0x0000ffff)
  199. return 0;
  200. else if (x&0x0001ffff)
  201. return -32767;
  202. else
  203. return 32767;
  204. }
  205. }
  206. /*
  207. K0 = 1
  208. K1 = log(2)
  209. K2 = 3-4*log(2)
  210. K3 = 3*log(2) - 2
  211. */
  212. #define D0 16384
  213. #define D1 11356
  214. #define D2 3726
  215. #define D3 1301
  216. /* Input in Q11 format, output in Q16 */
  217. static SPEEX_INLINE spx_word32_t spx_exp2(spx_word16_t x)
  218. {
  219. int integer;
  220. spx_word16_t frac;
  221. integer = SHR16(x,11);
  222. if (integer>14)
  223. return 0x7fffffff;
  224. else if (integer < -15)
  225. return 0;
  226. frac = SHL16(x-SHL16(integer,11),3);
  227. frac = ADD16(D0, MULT16_16_Q14(frac, ADD16(D1, MULT16_16_Q14(frac, ADD16(D2 , MULT16_16_Q14(D3,frac))))));
  228. return VSHR32(EXTEND32(frac), -integer-2);
  229. }
  230. /* Input in Q11 format, output in Q16 */
  231. static SPEEX_INLINE spx_word32_t spx_exp(spx_word16_t x)
  232. {
  233. if (x>21290)
  234. return 0x7fffffff;
  235. else if (x<-21290)
  236. return 0;
  237. else
  238. return spx_exp2(MULT16_16_P14(23637,x));
  239. }
  240. #define M1 32767
  241. #define M2 -21
  242. #define M3 -11943
  243. #define M4 4936
  244. static SPEEX_INLINE spx_word16_t spx_atan01(spx_word16_t x)
  245. {
  246. return MULT16_16_P15(x, ADD32(M1, MULT16_16_P15(x, ADD32(M2, MULT16_16_P15(x, ADD32(M3, MULT16_16_P15(M4, x)))))));
  247. }
  248. #undef M1
  249. #undef M2
  250. #undef M3
  251. #undef M4
  252. /* Input in Q15, output in Q14 */
  253. static SPEEX_INLINE spx_word16_t spx_atan(spx_word32_t x)
  254. {
  255. if (x <= 32767)
  256. {
  257. return SHR16(spx_atan01(x),1);
  258. } else {
  259. int e = spx_ilog2(x);
  260. if (e>=29)
  261. return 25736;
  262. x = DIV32_16(SHL32(EXTEND32(32767),29-e), EXTRACT16(SHR32(x, e-14)));
  263. return SUB16(25736, SHR16(spx_atan01(x),1));
  264. }
  265. }
  266. #else
  267. #ifndef M_PI
  268. #define M_PI 3.14159265358979323846 /* pi */
  269. #endif
  270. #define C1 0.9999932946f
  271. #define C2 -0.4999124376f
  272. #define C3 0.0414877472f
  273. #define C4 -0.0012712095f
  274. #define SPX_PI_2 1.5707963268
  275. static SPEEX_INLINE spx_word16_t spx_cos(spx_word16_t x)
  276. {
  277. if (x<SPX_PI_2)
  278. {
  279. x *= x;
  280. return C1 + x*(C2+x*(C3+C4*x));
  281. } else {
  282. x = M_PI-x;
  283. x *= x;
  284. return NEG16(C1 + x*(C2+x*(C3+C4*x)));
  285. }
  286. }
  287. #endif
  288. #endif