fixpoint_math.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. /* -----------------------------------------------------------------------------------------------------------
  2. Software License for The Fraunhofer FDK AAC Codec Library for Android
  3. © Copyright 1995 - 2015 Fraunhofer-Gesellschaft zur Förderung der angewandten Forschung e.V.
  4. All rights reserved.
  5. 1. INTRODUCTION
  6. The Fraunhofer FDK AAC Codec Library for Android ("FDK AAC Codec") is software that implements
  7. the MPEG Advanced Audio Coding ("AAC") encoding and decoding scheme for digital audio.
  8. This FDK AAC Codec software is intended to be used on a wide variety of Android devices.
  9. AAC's HE-AAC and HE-AAC v2 versions are regarded as today's most efficient general perceptual
  10. audio codecs. AAC-ELD is considered the best-performing full-bandwidth communications codec by
  11. independent studies and is widely deployed. AAC has been standardized by ISO and IEC as part
  12. of the MPEG specifications.
  13. Patent licenses for necessary patent claims for the FDK AAC Codec (including those of Fraunhofer)
  14. may be obtained through Via Licensing (www.vialicensing.com) or through the respective patent owners
  15. individually for the purpose of encoding or decoding bit streams in products that are compliant with
  16. the ISO/IEC MPEG audio standards. Please note that most manufacturers of Android devices already license
  17. these patent claims through Via Licensing or directly from the patent owners, and therefore FDK AAC Codec
  18. software may already be covered under those patent licenses when it is used for those licensed purposes only.
  19. Commercially-licensed AAC software libraries, including floating-point versions with enhanced sound quality,
  20. are also available from Fraunhofer. Users are encouraged to check the Fraunhofer website for additional
  21. applications information and documentation.
  22. 2. COPYRIGHT LICENSE
  23. Redistribution and use in source and binary forms, with or without modification, are permitted without
  24. payment of copyright license fees provided that you satisfy the following conditions:
  25. You must retain the complete text of this software license in redistributions of the FDK AAC Codec or
  26. your modifications thereto in source code form.
  27. You must retain the complete text of this software license in the documentation and/or other materials
  28. provided with redistributions of the FDK AAC Codec or your modifications thereto in binary form.
  29. You must make available free of charge copies of the complete source code of the FDK AAC Codec and your
  30. modifications thereto to recipients of copies in binary form.
  31. The name of Fraunhofer may not be used to endorse or promote products derived from this library without
  32. prior written permission.
  33. You may not charge copyright license fees for anyone to use, copy or distribute the FDK AAC Codec
  34. software or your modifications thereto.
  35. Your modified versions of the FDK AAC Codec must carry prominent notices stating that you changed the software
  36. and the date of any change. For modified versions of the FDK AAC Codec, the term
  37. "Fraunhofer FDK AAC Codec Library for Android" must be replaced by the term
  38. "Third-Party Modified Version of the Fraunhofer FDK AAC Codec Library for Android."
  39. 3. NO PATENT LICENSE
  40. NO EXPRESS OR IMPLIED LICENSES TO ANY PATENT CLAIMS, including without limitation the patents of Fraunhofer,
  41. ARE GRANTED BY THIS SOFTWARE LICENSE. Fraunhofer provides no warranty of patent non-infringement with
  42. respect to this software.
  43. You may use this FDK AAC Codec software or modifications thereto only for purposes that are authorized
  44. by appropriate patent licenses.
  45. 4. DISCLAIMER
  46. This FDK AAC Codec software is provided by Fraunhofer on behalf of the copyright holders and contributors
  47. "AS IS" and WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, including but not limited to the implied warranties
  48. of merchantability and fitness for a particular purpose. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  49. CONTRIBUTORS BE LIABLE for any direct, indirect, incidental, special, exemplary, or consequential damages,
  50. including but not limited to procurement of substitute goods or services; loss of use, data, or profits,
  51. or business interruption, however caused and on any theory of liability, whether in contract, strict
  52. liability, or tort (including negligence), arising in any way out of the use of this software, even if
  53. advised of the possibility of such damage.
  54. 5. CONTACT INFORMATION
  55. Fraunhofer Institute for Integrated Circuits IIS
  56. Attention: Audio and Multimedia Departments - FDK AAC LL
  57. Am Wolfsmantel 33
  58. 91058 Erlangen, Germany
  59. www.iis.fraunhofer.de/amm
  60. [email protected]
  61. ----------------------------------------------------------------------------------------------------------- */
  62. /*************************** Fraunhofer IIS FDK Tools **********************
  63. Author(s): M. Gayer
  64. Description: Fixed point specific mathematical functions
  65. ******************************************************************************/
  66. #ifndef __fixpoint_math_H
  67. #define __fixpoint_math_H
  68. #include "common_fix.h"
  69. #if !defined(FUNCTION_fIsLessThan)
  70. /**
  71. * \brief Compares two fixpoint values incl. scaling.
  72. * \param a_m mantissa of the first input value.
  73. * \param a_e exponent of the first input value.
  74. * \param b_m mantissa of the second input value.
  75. * \param b_e exponent of the second input value.
  76. * \return non-zero if (a_m*2^a_e) < (b_m*2^b_e), 0 otherwise
  77. */
  78. FDK_INLINE INT fIsLessThan(FIXP_DBL a_m, INT a_e, FIXP_DBL b_m, INT b_e)
  79. {
  80. if (a_e > b_e) {
  81. return (b_m >> fMin(a_e-b_e, DFRACT_BITS-1) > a_m);
  82. } else {
  83. return (a_m >> fMin(b_e-a_e, DFRACT_BITS-1) < b_m);
  84. }
  85. }
  86. FDK_INLINE INT fIsLessThan(FIXP_SGL a_m, INT a_e, FIXP_SGL b_m, INT b_e)
  87. {
  88. if (a_e > b_e) {
  89. return (b_m >> fMin(a_e-b_e, FRACT_BITS-1) > a_m);
  90. } else {
  91. return (a_m >> fMin(b_e-a_e, FRACT_BITS-1) < b_m);
  92. }
  93. }
  94. #endif
  95. #define LD_DATA_SCALING (64.0f)
  96. #define LD_DATA_SHIFT 6 /* pow(2, LD_DATA_SHIFT) = LD_DATA_SCALING */
  97. /**
  98. * \brief deprecated. Use fLog2() instead.
  99. */
  100. FIXP_DBL CalcLdData(FIXP_DBL op);
  101. void LdDataVector(FIXP_DBL *srcVector, FIXP_DBL *destVector, INT number);
  102. FIXP_DBL CalcInvLdData(FIXP_DBL op);
  103. void InitLdInt();
  104. FIXP_DBL CalcLdInt(INT i);
  105. extern const USHORT sqrt_tab[49];
  106. inline FIXP_DBL sqrtFixp_lookup(FIXP_DBL x)
  107. {
  108. UINT y = (INT)x;
  109. UCHAR is_zero=(y==0);
  110. INT zeros=fixnormz_D(y) & 0x1e;
  111. y<<=zeros;
  112. UINT idx=(y>>26)-16;
  113. USHORT frac=(y>>10)&0xffff;
  114. USHORT nfrac=0xffff^frac;
  115. UINT t=nfrac*sqrt_tab[idx]+frac*sqrt_tab[idx+1];
  116. t=t>>(zeros>>1);
  117. return(is_zero ? 0 : t);
  118. }
  119. inline FIXP_DBL sqrtFixp_lookup(FIXP_DBL x, INT *x_e)
  120. {
  121. UINT y = (INT)x;
  122. INT e;
  123. if (x == (FIXP_DBL)0) {
  124. return x;
  125. }
  126. /* Normalize */
  127. e=fixnormz_D(y);
  128. y<<=e;
  129. e = *x_e - e + 2;
  130. /* Correct odd exponent. */
  131. if (e & 1) {
  132. y >>= 1;
  133. e ++;
  134. }
  135. /* Get square root */
  136. UINT idx=(y>>26)-16;
  137. USHORT frac=(y>>10)&0xffff;
  138. USHORT nfrac=0xffff^frac;
  139. UINT t=nfrac*sqrt_tab[idx]+frac*sqrt_tab[idx+1];
  140. /* Write back exponent */
  141. *x_e = e >> 1;
  142. return (FIXP_DBL)(LONG)(t>>1);
  143. }
  144. FIXP_DBL sqrtFixp(FIXP_DBL op);
  145. void InitInvSqrtTab();
  146. FIXP_DBL invSqrtNorm2(FIXP_DBL op, INT *shift);
  147. /*****************************************************************************
  148. functionname: invFixp
  149. description: delivers 1/(op)
  150. *****************************************************************************/
  151. inline FIXP_DBL invFixp(FIXP_DBL op)
  152. {
  153. INT tmp_exp ;
  154. FIXP_DBL tmp_inv = invSqrtNorm2(op, &tmp_exp) ;
  155. FDK_ASSERT((31-(2*tmp_exp+1))>=0) ;
  156. return ( fPow2Div2( (FIXP_DBL)tmp_inv ) >> (31-(2*tmp_exp+1)) ) ;
  157. }
  158. #if defined(__mips__) && (__GNUC__==2)
  159. #define FUNCTION_schur_div
  160. inline FIXP_DBL schur_div(FIXP_DBL num,FIXP_DBL denum, INT count)
  161. {
  162. INT result, tmp ;
  163. __asm__ ("srl %1, %2, 15\n"
  164. "div %3, %1\n" : "=lo" (result)
  165. : "%d" (tmp), "d" (denum) , "d" (num)
  166. : "hi" ) ;
  167. return result<<16 ;
  168. }
  169. /*###########################################################################################*/
  170. #elif defined(__mips__) && (__GNUC__==3)
  171. #define FUNCTION_schur_div
  172. inline FIXP_DBL schur_div(FIXP_DBL num,FIXP_DBL denum, INT count)
  173. {
  174. INT result, tmp;
  175. __asm__ ("srl %[tmp], %[denum], 15\n"
  176. "div %[result], %[num], %[tmp]\n"
  177. : [tmp] "+r" (tmp), [result]"=r"(result)
  178. : [denum]"r"(denum), [num]"r"(num)
  179. : "hi", "lo");
  180. return result << (DFRACT_BITS-16);
  181. }
  182. /*###########################################################################################*/
  183. #elif defined(SIMULATE_MIPS_DIV)
  184. #define FUNCTION_schur_div
  185. inline FIXP_DBL schur_div(FIXP_DBL num, FIXP_DBL denum, INT count)
  186. {
  187. FDK_ASSERT (count<=DFRACT_BITS-1);
  188. FDK_ASSERT (num>=(FIXP_DBL)0);
  189. FDK_ASSERT (denum>(FIXP_DBL)0);
  190. FDK_ASSERT (num <= denum);
  191. INT tmp = denum >> (count-1);
  192. INT result = 0;
  193. while (num > tmp)
  194. {
  195. num -= tmp;
  196. result++;
  197. }
  198. return result << (DFRACT_BITS-count);
  199. }
  200. /*###########################################################################################*/
  201. #endif /* target architecture selector */
  202. #if !defined(FUNCTION_schur_div)
  203. /**
  204. * \brief Divide two FIXP_DBL values with given precision.
  205. * \param num dividend
  206. * \param denum divisor
  207. * \param count amount of significant bits of the result (starting to the MSB)
  208. * \return num/divisor
  209. */
  210. FIXP_DBL schur_div(FIXP_DBL num,FIXP_DBL denum, INT count);
  211. #endif
  212. FIXP_DBL mul_dbl_sgl_rnd (const FIXP_DBL op1,
  213. const FIXP_SGL op2);
  214. /**
  215. * \brief multiply two values with normalization, thus max precision.
  216. * Author: Robert Weidner
  217. *
  218. * \param f1 first factor
  219. * \param f2 secod factor
  220. * \param result_e pointer to an INT where the exponent of the result is stored into
  221. * \return mantissa of the product f1*f2
  222. */
  223. FIXP_DBL fMultNorm(
  224. FIXP_DBL f1,
  225. FIXP_DBL f2,
  226. INT *result_e
  227. );
  228. inline FIXP_DBL fMultNorm(FIXP_DBL f1, FIXP_DBL f2)
  229. {
  230. FIXP_DBL m;
  231. INT e;
  232. m = fMultNorm(f1, f2, &e);
  233. m = scaleValueSaturate(m, e);
  234. return m;
  235. }
  236. /**
  237. * \brief Divide 2 FIXP_DBL values with normalization of input values.
  238. * \param num numerator
  239. * \param denum denomintator
  240. * \return num/denum with exponent = 0
  241. */
  242. FIXP_DBL fDivNorm(FIXP_DBL num, FIXP_DBL denom, INT *result_e);
  243. /**
  244. * \brief Divide 2 FIXP_DBL values with normalization of input values.
  245. * \param num numerator
  246. * \param denum denomintator
  247. * \param result_e pointer to an INT where the exponent of the result is stored into
  248. * \return num/denum with exponent = *result_e
  249. */
  250. FIXP_DBL fDivNorm(FIXP_DBL num, FIXP_DBL denom);
  251. /**
  252. * \brief Divide 2 FIXP_DBL values with normalization of input values.
  253. * \param num numerator
  254. * \param denum denomintator
  255. * \return num/denum with exponent = 0
  256. */
  257. FIXP_DBL fDivNormHighPrec(FIXP_DBL L_num, FIXP_DBL L_denum, INT *result_e);
  258. /**
  259. * \brief Calculate log(argument)/log(2) (logarithm with base 2). deprecated. Use fLog2() instead.
  260. * \param arg mantissa of the argument
  261. * \param arg_e exponent of the argument
  262. * \param result_e pointer to an INT to store the exponent of the result
  263. * \return the mantissa of the result.
  264. * \param
  265. */
  266. FIXP_DBL CalcLog2(FIXP_DBL arg, INT arg_e, INT *result_e);
  267. /**
  268. * \brief return 2 ^ (exp * 2^exp_e)
  269. * \param exp_m mantissa of the exponent to 2.0f
  270. * \param exp_e exponent of the exponent to 2.0f
  271. * \param result_e pointer to a INT where the exponent of the result will be stored into
  272. * \return mantissa of the result
  273. */
  274. FIXP_DBL f2Pow(const FIXP_DBL exp_m, const INT exp_e, INT *result_e);
  275. /**
  276. * \brief return 2 ^ (exp_m * 2^exp_e). This version returns only the mantissa with implicit exponent of zero.
  277. * \param exp_m mantissa of the exponent to 2.0f
  278. * \param exp_e exponent of the exponent to 2.0f
  279. * \return mantissa of the result
  280. */
  281. FIXP_DBL f2Pow(const FIXP_DBL exp_m, const INT exp_e);
  282. /**
  283. * \brief return x ^ (exp * 2^exp_e), where log2(x) = baseLd_m * 2^(baseLd_e). This saves
  284. * the need to compute log2() of constant values (when x is a constant).
  285. * \param ldx_m mantissa of log2() of x.
  286. * \param ldx_e exponent of log2() of x.
  287. * \param exp_m mantissa of the exponent to 2.0f
  288. * \param exp_e exponent of the exponent to 2.0f
  289. * \param result_e pointer to a INT where the exponent of the result will be stored into
  290. * \return mantissa of the result
  291. */
  292. FIXP_DBL fLdPow(
  293. FIXP_DBL baseLd_m,
  294. INT baseLd_e,
  295. FIXP_DBL exp_m, INT exp_e,
  296. INT *result_e
  297. );
  298. /**
  299. * \brief return x ^ (exp * 2^exp_e), where log2(x) = baseLd_m * 2^(baseLd_e). This saves
  300. * the need to compute log2() of constant values (when x is a constant). This version
  301. * does not return an exponent, which is implicitly 0.
  302. * \param ldx_m mantissa of log2() of x.
  303. * \param ldx_e exponent of log2() of x.
  304. * \param exp_m mantissa of the exponent to 2.0f
  305. * \param exp_e exponent of the exponent to 2.0f
  306. * \return mantissa of the result
  307. */
  308. FIXP_DBL fLdPow(
  309. FIXP_DBL baseLd_m, INT baseLd_e,
  310. FIXP_DBL exp_m, INT exp_e
  311. );
  312. /**
  313. * \brief return (base * 2^base_e) ^ (exp * 2^exp_e). Use fLdPow() instead whenever possible.
  314. * \param base_m mantissa of the base.
  315. * \param base_e exponent of the base.
  316. * \param exp_m mantissa of power to be calculated of the base.
  317. * \param exp_e exponent of power to be calculated of the base.
  318. * \param result_e pointer to a INT where the exponent of the result will be stored into.
  319. * \return mantissa of the result.
  320. */
  321. FIXP_DBL fPow(FIXP_DBL base_m, INT base_e, FIXP_DBL exp_m, INT exp_e, INT *result_e);
  322. /**
  323. * \brief return (base * 2^base_e) ^ N
  324. * \param base mantissa of the base
  325. * \param base_e exponent of the base
  326. * \param power to be calculated of the base
  327. * \param result_e pointer to a INT where the exponent of the result will be stored into
  328. * \return mantissa of the result
  329. */
  330. FIXP_DBL fPowInt(FIXP_DBL base_m, INT base_e, INT N, INT *result_e);
  331. /**
  332. * \brief calculate logarithm of base 2 of x_m * 2^(x_e)
  333. * \param x_m mantissa of the input value.
  334. * \param x_e exponent of the input value.
  335. * \param pointer to an INT where the exponent of the result is returned into.
  336. * \return mantissa of the result.
  337. */
  338. FIXP_DBL fLog2(FIXP_DBL x_m, INT x_e, INT *result_e);
  339. /**
  340. * \brief calculate logarithm of base 2 of x_m * 2^(x_e)
  341. * \param x_m mantissa of the input value.
  342. * \param x_e exponent of the input value.
  343. * \return mantissa of the result with implicit exponent of LD_DATA_SHIFT.
  344. */
  345. FIXP_DBL fLog2(FIXP_DBL x_m, INT x_e);
  346. /**
  347. * \brief Add with saturation of the result.
  348. * \param a first summand
  349. * \param b second summand
  350. * \return saturated sum of a and b.
  351. */
  352. inline FIXP_SGL fAddSaturate(const FIXP_SGL a, const FIXP_SGL b)
  353. {
  354. LONG sum;
  355. sum = (LONG)(SHORT)a + (LONG)(SHORT)b;
  356. sum = fMax(fMin((INT)sum, (INT)MAXVAL_SGL), (INT)MINVAL_SGL);
  357. return (FIXP_SGL)(SHORT)sum;
  358. }
  359. /**
  360. * \brief Add with saturation of the result.
  361. * \param a first summand
  362. * \param b second summand
  363. * \return saturated sum of a and b.
  364. */
  365. inline FIXP_DBL fAddSaturate(const FIXP_DBL a, const FIXP_DBL b)
  366. {
  367. LONG sum;
  368. sum = (LONG)(a>>1) + (LONG)(b>>1);
  369. sum = fMax(fMin((INT)sum, (INT)(MAXVAL_DBL>>1)), (INT)(MINVAL_DBL>>1));
  370. return (FIXP_DBL)(LONG)(sum<<1);
  371. }
  372. //#define TEST_ROUNDING
  373. /*****************************************************************************
  374. array for 1/n, n=1..80
  375. ****************************************************************************/
  376. extern const FIXP_DBL invCount[80];
  377. LNK_SECTION_INITCODE
  378. inline void InitInvInt(void) {}
  379. /**
  380. * \brief Calculate the value of 1/i where i is a integer value. It supports
  381. * input values from 1 upto 80.
  382. * \param intValue Integer input value.
  383. * \param FIXP_DBL representation of 1/intValue
  384. */
  385. inline FIXP_DBL GetInvInt(int intValue)
  386. {
  387. FDK_ASSERT((intValue > 0) && (intValue < 80));
  388. FDK_ASSERT(intValue<80);
  389. return invCount[intValue];
  390. }
  391. #endif