OPER_32B.C
上传用户:meifeng08
上传日期:2013-06-18
资源大小:5304k
文件大小:12k
源码类别:

语音压缩

开发平台:

C/C++

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