math_approx.h 7.4 KB

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