简化 OPER_32B.C
Upload User: meifeng08
Upload Date: 2013-06-18
Package Size: 5304k
Code Size: 12k
Category:

Voice Compress

Development Platform:

C/C++

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