PageRenderTime 54ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/media/libstagefright/codecs/amrwbenc/src/oper_32b.c

https://bitbucket.org/patrikryd/base
C | 223 lines | 48 code | 25 blank | 150 comment | 0 complexity | 1337f8555f0b18eada3602044f904954 MD5 | raw file
Possible License(s): CC0-1.0
  1. /*
  2. ** Copyright 2003-2010, VisualOn, Inc.
  3. **
  4. ** Licensed under the Apache License, Version 2.0 (the "License");
  5. ** you may not use this file except in compliance with the License.
  6. ** You may obtain a copy of the License at
  7. **
  8. ** http://www.apache.org/licenses/LICENSE-2.0
  9. **
  10. ** Unless required by applicable law or agreed to in writing, software
  11. ** distributed under the License is distributed on an "AS IS" BASIS,
  12. ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. ** See the License for the specific language governing permissions and
  14. ** limitations under the License.
  15. */
  16. /*****************************************************************************
  17. * This file contains operations in double precision. *
  18. * These operations are not standard double precision operations. *
  19. * They are used where single precision is not enough but the full 32 bits *
  20. * precision is not necessary. For example, the function Div_32() has a *
  21. * 24 bits precision which is enough for our purposes. *
  22. * *
  23. * The double precision numbers use a special representation: *
  24. * *
  25. * L_32 = hi<<16 + lo<<1 *
  26. * *
  27. * L_32 is a 32 bit integer. *
  28. * hi and lo are 16 bit signed integers. *
  29. * As the low part also contains the sign, this allows fast multiplication. *
  30. * *
  31. * 0x8000 0000 <= L_32 <= 0x7fff fffe. *
  32. * *
  33. * We will use DPF (Double Precision Format )in this file to specify *
  34. * this special format. *
  35. *****************************************************************************
  36. */
  37. #include "typedef.h"
  38. #include "basic_op.h"
  39. #include "oper_32b.h"
  40. /*****************************************************************************
  41. * *
  42. * Function L_Extract() *
  43. * *
  44. * Extract from a 32 bit integer two 16 bit DPF. *
  45. * *
  46. * Arguments: *
  47. * *
  48. * L_32 : 32 bit integer. *
  49. * 0x8000 0000 <= L_32 <= 0x7fff ffff. *
  50. * hi : b16 to b31 of L_32 *
  51. * lo : (L_32 - hi<<16)>>1 *
  52. *****************************************************************************
  53. */
  54. __inline void VO_L_Extract (Word32 L_32, Word16 *hi, Word16 *lo)
  55. {
  56. *hi = (Word16)(L_32 >> 16);
  57. *lo = (Word16)((L_32 & 0xffff) >> 1);
  58. return;
  59. }
  60. /*****************************************************************************
  61. * *
  62. * Function L_Comp() *
  63. * *
  64. * Compose from two 16 bit DPF a 32 bit integer. *
  65. * *
  66. * L_32 = hi<<16 + lo<<1 *
  67. * *
  68. * Arguments: *
  69. * *
  70. * hi msb *
  71. * lo lsf (with sign) *
  72. * *
  73. * Return Value : *
  74. * *
  75. * 32 bit long signed integer (Word32) whose value falls in the *
  76. * range : 0x8000 0000 <= L_32 <= 0x7fff fff0. *
  77. * *
  78. *****************************************************************************
  79. */
  80. Word32 L_Comp (Word16 hi, Word16 lo)
  81. {
  82. Word32 L_32;
  83. L_32 = L_deposit_h (hi);
  84. return (L_mac (L_32, lo, 1)); /* = hi<<16 + lo<<1 */
  85. }
  86. /*****************************************************************************
  87. * Function Mpy_32() *
  88. * *
  89. * Multiply two 32 bit integers (DPF). The result is divided by 2**31 *
  90. * *
  91. * L_32 = (hi1*hi2)<<1 + ( (hi1*lo2)>>15 + (lo1*hi2)>>15 )<<1 *
  92. * *
  93. * This operation can also be viewed as the multiplication of two Q31 *
  94. * number and the result is also in Q31. *
  95. * *
  96. * Arguments: *
  97. * *
  98. * hi1 hi part of first number *
  99. * lo1 lo part of first number *
  100. * hi2 hi part of second number *
  101. * lo2 lo part of second number *
  102. * *
  103. *****************************************************************************
  104. */
  105. __inline Word32 Mpy_32 (Word16 hi1, Word16 lo1, Word16 hi2, Word16 lo2)
  106. {
  107. Word32 L_32;
  108. L_32 = (hi1 * hi2);
  109. L_32 += (hi1 * lo2) >> 15;
  110. L_32 += (lo1 * hi2) >> 15;
  111. L_32 <<= 1;
  112. return (L_32);
  113. }
  114. /*****************************************************************************
  115. * Function Mpy_32_16() *
  116. * *
  117. * Multiply a 16 bit integer by a 32 bit (DPF). The result is divided *
  118. * by 2**15 *
  119. * *
  120. * *
  121. * L_32 = (hi1*lo2)<<1 + ((lo1*lo2)>>15)<<1 *
  122. * *
  123. * Arguments: *
  124. * *
  125. * hi hi part of 32 bit number. *
  126. * lo lo part of 32 bit number. *
  127. * n 16 bit number. *
  128. * *
  129. *****************************************************************************
  130. */
  131. __inline Word32 Mpy_32_16 (Word16 hi, Word16 lo, Word16 n)
  132. {
  133. Word32 L_32;
  134. L_32 = (hi * n)<<1;
  135. L_32 += (((lo * n)>>15)<<1);
  136. return (L_32);
  137. }
  138. /*****************************************************************************
  139. * *
  140. * Function Name : Div_32 *
  141. * *
  142. * Purpose : *
  143. * Fractional integer division of two 32 bit numbers. *
  144. * L_num / L_denom. *
  145. * L_num and L_denom must be positive and L_num < L_denom. *
  146. * L_denom = denom_hi<<16 + denom_lo<<1 *
  147. * denom_hi is a normalize number. *
  148. * *
  149. * Inputs : *
  150. * *
  151. * L_num *
  152. * 32 bit long signed integer (Word32) whose value falls in the *
  153. * range : 0x0000 0000 < L_num < L_denom *
  154. * *
  155. * L_denom = denom_hi<<16 + denom_lo<<1 (DPF) *
  156. * *
  157. * denom_hi *
  158. * 16 bit positive normalized integer whose value falls in the *
  159. * range : 0x4000 < hi < 0x7fff *
  160. * denom_lo *
  161. * 16 bit positive integer whose value falls in the *
  162. * range : 0 < lo < 0x7fff *
  163. * *
  164. * Return Value : *
  165. * *
  166. * L_div *
  167. * 32 bit long signed integer (Word32) whose value falls in the *
  168. * range : 0x0000 0000 <= L_div <= 0x7fff ffff. *
  169. * *
  170. * Algorithm: *
  171. * *
  172. * - find = 1/L_denom. *
  173. * First approximation: approx = 1 / denom_hi *
  174. * 1/L_denom = approx * (2.0 - L_denom * approx ) *
  175. * *
  176. * - result = L_num * (1/L_denom) *
  177. *****************************************************************************
  178. */
  179. Word32 Div_32 (Word32 L_num, Word16 denom_hi, Word16 denom_lo)
  180. {
  181. Word16 approx, hi, lo, n_hi, n_lo;
  182. Word32 L_32;
  183. /* First approximation: 1 / L_denom = 1/denom_hi */
  184. approx = div_s ((Word16) 0x3fff, denom_hi);
  185. /* 1/L_denom = approx * (2.0 - L_denom * approx) */
  186. L_32 = Mpy_32_16 (denom_hi, denom_lo, approx);
  187. L_32 = L_sub ((Word32) 0x7fffffffL, L_32);
  188. hi = L_32 >> 16;
  189. lo = (L_32 & 0xffff) >> 1;
  190. L_32 = Mpy_32_16 (hi, lo, approx);
  191. /* L_num * (1/L_denom) */
  192. hi = L_32 >> 16;
  193. lo = (L_32 & 0xffff) >> 1;
  194. VO_L_Extract (L_num, &n_hi, &n_lo);
  195. L_32 = Mpy_32 (n_hi, n_lo, hi, lo);
  196. L_32 = L_shl2(L_32, 2);
  197. return (L_32);
  198. }