oper_32b.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*****************************************************************************
  2. * $Id $
  3. *
  4. * This file contains operations in double precision. *
  5. * These operations are not standard double precision operations. *
  6. * They are used where single precision is not enough but the full 32 bits *
  7. * precision is not necessary. For example, the function Div_32() has a *
  8. * 24 bits precision which is enough for our purposes. *
  9. * *
  10. * The double precision numbers use a special representation: *
  11. * *
  12. * L_32 = hi<<16 + lo<<1 *
  13. * *
  14. * L_32 is a 32 bit integer. *
  15. * hi and lo are 16 bit signed integers. *
  16. * As the low part also contains the sign, this allows fast multiplication. *
  17. * *
  18. * 0x8000 0000 <= L_32 <= 0x7fff fffe. *
  19. * *
  20. * We will use DPF (Double Precision Format )in this file to specify *
  21. * this special format. *
  22. *****************************************************************************
  23. */
  24. #include "typedef.h"
  25. #include "basic_op.h"
  26. #include "oper_32b.h"
  27. #include "count.h"
  28. /*****************************************************************************
  29. * *
  30. * Function L_Extract() *
  31. * *
  32. * Extract from a 32 bit integer two 16 bit DPF. *
  33. * *
  34. * Arguments: *
  35. * *
  36. * L_32 : 32 bit integer. *
  37. * 0x8000 0000 <= L_32 <= 0x7fff ffff. *
  38. * hi : b16 to b31 of L_32 *
  39. * lo : (L_32 - hi<<16)>>1 *
  40. *****************************************************************************
  41. */
  42. void L_Extract (Word32 L_32, Word16 *hi, Word16 *lo)
  43. {
  44. *hi = extract_h_ex (L_32);
  45. *lo = extract_l_ex (L_msu_ex (L_shr_ex (L_32, 1), *hi, 16384));
  46. return;
  47. }
  48. /*****************************************************************************
  49. * *
  50. * Function L_Comp() *
  51. * *
  52. * Compose from two 16 bit DPF a 32 bit integer. *
  53. * *
  54. * L_32 = hi<<16 + lo<<1 *
  55. * *
  56. * Arguments: *
  57. * *
  58. * hi msb *
  59. * lo lsf (with sign) *
  60. * *
  61. * Return Value : *
  62. * *
  63. * 32 bit long signed integer (Word32) whose value falls in the *
  64. * range : 0x8000 0000 <= L_32 <= 0x7fff fff0. *
  65. * *
  66. *****************************************************************************
  67. */
  68. Word32 L_Comp (Word16 hi, Word16 lo)
  69. {
  70. Word32 L_32;
  71. L_32 = L_deposit_h_ex (hi);
  72. return (L_mac_ex (L_32, lo, 1)); /* = hi<<16 + lo<<1 */
  73. }
  74. /*****************************************************************************
  75. * Function Mpy_32() *
  76. * *
  77. * Multiply two 32 bit integers (DPF). The result is divided by 2**31 *
  78. * *
  79. * L_32 = (hi1*hi2)<<1 + ( (hi1*lo2)>>15 + (lo1*hi2)>>15 )<<1 *
  80. * *
  81. * This operation can also be viewed as the multiplication of two Q31 *
  82. * number and the result is also in Q31. *
  83. * *
  84. * Arguments: *
  85. * *
  86. * hi1 hi part of first number *
  87. * lo1 lo part of first number *
  88. * hi2 hi part of second number *
  89. * lo2 lo part of second number *
  90. * *
  91. *****************************************************************************
  92. */
  93. Word32 Mpy_32 (Word16 hi1, Word16 lo1, Word16 hi2, Word16 lo2)
  94. {
  95. Word32 L_32;
  96. L_32 = L_mult_ex (hi1, hi2);
  97. L_32 = L_mac_ex (L_32, mult_ex (hi1, lo2), 1);
  98. L_32 = L_mac_ex (L_32, mult_ex (lo1, hi2), 1);
  99. return (L_32);
  100. }
  101. /*****************************************************************************
  102. * Function Mpy_32_16() *
  103. * *
  104. * Multiply a 16 bit integer by a 32 bit (DPF). The result is divided *
  105. * by 2**15 *
  106. * *
  107. * *
  108. * L_32 = (hi1*lo2)<<1 + ((lo1*lo2)>>15)<<1 *
  109. * *
  110. * Arguments: *
  111. * *
  112. * hi hi part of 32 bit number. *
  113. * lo lo part of 32 bit number. *
  114. * n 16 bit number. *
  115. * *
  116. *****************************************************************************
  117. */
  118. Word32 Mpy_32_16 (Word16 hi, Word16 lo, Word16 n)
  119. {
  120. Word32 L_32;
  121. L_32 = L_mult_ex (hi, n);
  122. L_32 = L_mac_ex (L_32, mult_ex (lo, n), 1);
  123. return (L_32);
  124. }
  125. /*****************************************************************************
  126. * *
  127. * Function Name : Div_32 *
  128. * *
  129. * Purpose : *
  130. * Fractional integer division of two 32 bit numbers. *
  131. * L_num / L_denom. *
  132. * L_num and L_denom must be positive and L_num < L_denom. *
  133. * L_denom = denom_hi<<16 + denom_lo<<1 *
  134. * denom_hi is a normalize number. *
  135. * *
  136. * Inputs : *
  137. * *
  138. * L_num *
  139. * 32 bit long signed integer (Word32) whose value falls in the *
  140. * range : 0x0000 0000 < L_num < L_denom *
  141. * *
  142. * L_denom = denom_hi<<16 + denom_lo<<1 (DPF) *
  143. * *
  144. * denom_hi *
  145. * 16 bit positive normalized integer whose value falls in the *
  146. * range : 0x4000 < hi < 0x7fff *
  147. * denom_lo *
  148. * 16 bit positive integer whose value falls in the *
  149. * range : 0 < lo < 0x7fff *
  150. * *
  151. * Return Value : *
  152. * *
  153. * L_div *
  154. * 32 bit long signed integer (Word32) whose value falls in the *
  155. * range : 0x0000 0000 <= L_div <= 0x7fff ffff. *
  156. * *
  157. * Algorithm: *
  158. * *
  159. * - find = 1/L_denom. *
  160. * First approximation: approx = 1 / denom_hi *
  161. * 1/L_denom = approx * (2.0 - L_denom * approx ) *
  162. * *
  163. * - result = L_num * (1/L_denom) *
  164. *****************************************************************************
  165. */
  166. Word32 Div_32 (Word32 L_num, Word16 denom_hi, Word16 denom_lo)
  167. {
  168. Word16 approx, hi, lo, n_hi, n_lo;
  169. Word32 L_32;
  170. /* First approximation: 1 / L_denom = 1/denom_hi */
  171. approx = div_s ((Word16) 0x3fff, denom_hi);
  172. /* 1/L_denom = approx * (2.0 - L_denom * approx) */
  173. L_32 = Mpy_32_16 (denom_hi, denom_lo, approx);
  174. L_32 = L_sub_ex ((Word32) 0x7fffffffL, L_32);
  175. L_Extract (L_32, &hi, &lo);
  176. L_32 = Mpy_32_16 (hi, lo, approx);
  177. /* L_num * (1/L_denom) */
  178. L_Extract (L_32, &hi, &lo);
  179. L_Extract (L_num, &n_hi, &n_lo);
  180. L_32 = Mpy_32 (n_hi, n_lo, hi, lo);
  181. L_32 = L_shl_ex (L_32, 2);
  182. return (L_32);
  183. }