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

语音压缩

开发平台:

C/C++

  1. /*___________________________________________________________________________
  2.  |                                                                           |
  3.  | Basics operators.                                                         |
  4.  |___________________________________________________________________________|
  5. */
  6. /*___________________________________________________________________________
  7.  |                                                                           |
  8.  |   Include-Files                                                           |
  9.  |___________________________________________________________________________|
  10. */
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include "typedef.h"
  14. #include "basop.h"
  15. /*___________________________________________________________________________
  16.  |                                                                           |
  17.  |   Local Functions                                                         |
  18.  |___________________________________________________________________________|
  19. */
  20. Word16 sature(Word32 L_var1);
  21. /*___________________________________________________________________________
  22.  |                                                                           |
  23.  |   Constants and Globals                                                   |
  24.  |___________________________________________________________________________|
  25. */
  26. Flag Overflow =0;
  27. Flag Carry =0;
  28. /*___________________________________________________________________________
  29.  |                                                                           |
  30.  |   Functions                                                               |
  31.  |___________________________________________________________________________|
  32. */
  33. /*___________________________________________________________________________
  34.  |                                                                           |
  35.  |   Function Name : sature                                                  |
  36.  |                                                                           |
  37.  |   Purpose :                                                               |
  38.  |                                                                           |
  39.  |    Limit the 32 bit input to the range of a 16 bit word.                  |
  40.  |                                                                           |
  41.  |   Inputs :                                                                |
  42.  |                                                                           |
  43.  |    L_var1                                                                 |
  44.  |             32 bit long signed integer (Word32) whose value falls in the  |
  45.  |             range : 0x8000 0000 <= L_var1 <= 0x7fff ffff.                 |
  46.  |                                                                           |
  47.  |   Outputs :                                                               |
  48.  |                                                                           |
  49.  |    none                                                                   |
  50.  |                                                                           |
  51.  |   Return Value :                                                          |
  52.  |                                                                           |
  53.  |    var_out                                                                |
  54.  |             16 bit short signed integer (Word16) whose value falls in the |
  55.  |             range : 0xffff 8000 <= var_out <= 0x0000 7fff.                |
  56.  |___________________________________________________________________________|
  57. */
  58. Word16 sature(Word32 L_var1)
  59. {
  60.     Word16 var_out;
  61.     if (L_var1 > 0X00007fffL) {
  62.         Overflow = 1;
  63.         var_out = MAX_16;
  64.     }
  65.     else {
  66.         if (L_var1 < (Word32)0xffff8000L) {
  67.             Overflow = 1;
  68.             var_out = MIN_16;
  69.         }
  70.         else {
  71.             Overflow = 0;
  72.             var_out = extract_l(L_var1);
  73.         }
  74.     }
  75.     return(var_out);
  76. }
  77. /*___________________________________________________________________________
  78.  |                                                                           |
  79.  |   Function Name : add                                                     |
  80.  |                                                                           |
  81.  |   Purpose :                                                               |
  82.  |                                                                           |
  83.  |    Performs the addition (var1+var2) with overflow control and saturation;|
  84.  |    the 16 bit result is set at +32767 when overflow occurs or at -32768   |
  85.  |    when underflow occurs.                                                 |
  86.  |                                                                           |
  87.  |   Complexity weight : 1                                                   |
  88.  |                                                                           |
  89.  |   Inputs :                                                                |
  90.  |                                                                           |
  91.  |    var1                                                                   |
  92.  |             16 bit short signed integer (Word16) whose value falls in the |
  93.  |             range : 0xffff 8000 <= var1 <= 0x0000 7fff.                   |
  94.  |                                                                           |
  95.  |    var2                                                                   |
  96.  |             16 bit short signed integer (Word16) whose value falls in the |
  97.  |             range : 0xffff 8000 <= var1 <= 0x0000 7fff.                   |
  98.  |                                                                           |
  99.  |   Outputs :                                                               |
  100.  |                                                                           |
  101.  |    none                                                                   |
  102.  |                                                                           |
  103.  |   Return Value :                                                          |
  104.  |                                                                           |
  105.  |    var_out                                                                |
  106.  |             16 bit short signed integer (Word16) whose value falls in the |
  107.  |             range : 0xffff 8000 <= var_out <= 0x0000 7fff.                |
  108.  |___________________________________________________________________________|
  109. */
  110. Word16 add(Word16 var1,Word16 var2)
  111. {
  112.     Word16 var_out;
  113.     Word32 L_somme;
  114.     L_somme = (Word32) var1 + (Word32) var2;
  115.     var_out = sature(L_somme);
  116.     return(var_out);
  117. }
  118. /*___________________________________________________________________________
  119.  |                                                                           |
  120.  |   Function Name : sub                                                     |
  121.  |                                                                           |
  122.  |   Purpose :                                                               |
  123.  |                                                                           |
  124.  |    Performs the subtraction (var1+var2) with overflow control and satu-   |
  125.  |    ration; the 16 bit result is set at +32767 when overflow occurs or at  |
  126.  |    -32768 when underflow occurs.                                          |
  127.  |                                                                           |
  128.  |   Complexity weight : 1                                                   |
  129.  |                                                                           |
  130.  |   Inputs :                                                                |
  131.  |                                                                           |
  132.  |    var1                                                                   |
  133.  |             16 bit short signed integer (Word16) whose value falls in the |
  134.  |             range : 0xffff 8000 <= var1 <= 0x0000 7fff.                   |
  135.  |                                                                           |
  136.  |    var2                                                                   |
  137.  |             16 bit short signed integer (Word16) whose value falls in the |
  138.  |             range : 0xffff 8000 <= var1 <= 0x0000 7fff.                   |
  139.  |                                                                           |
  140.  |   Outputs :                                                               |
  141.  |                                                                           |
  142.  |    none                                                                   |
  143.  |                                                                           |
  144.  |   Return Value :                                                          |
  145.  |                                                                           |
  146.  |    var_out                                                                |
  147.  |             16 bit short signed integer (Word16) whose value falls in the |
  148.  |             range : 0xffff 8000 <= var_out <= 0x0000 7fff.                |
  149.  |___________________________________________________________________________|
  150. */
  151. Word16 sub(Word16 var1,Word16 var2)
  152. {
  153.     Word16 var_out;
  154.     Word32 L_diff;
  155.     L_diff = (Word32) var1 -  (Word32) var2;
  156.     var_out = sature(L_diff);
  157.     return(var_out);
  158. }
  159. /*___________________________________________________________________________
  160.  |                                                                           |
  161.  |   Function Name : abs_s                                                   |
  162.  |                                                                           |
  163.  |   Purpose :                                                               |
  164.  |                                                                           |
  165.  |    Absolute value of var1; abs_s(-32768) = 32767.                         |
  166.  |                                                                           |
  167.  |   Complexity weight : 1                                                   |
  168.  |                                                                           |
  169.  |   Inputs :                                                                |
  170.  |                                                                           |
  171.  |    var1                                                                   |
  172.  |             16 bit short signed integer (Word16) whose value falls in the |
  173.  |             range : 0xffff 8000 <= var1 <= 0x0000 7fff.                   |
  174.  |                                                                           |
  175.  |   Outputs :                                                               |
  176.  |                                                                           |
  177.  |    none                                                                   |
  178.  |                                                                           |
  179.  |   Return Value :                                                          |
  180.  |                                                                           |
  181.  |    var_out                                                                |
  182.  |             16 bit short signed integer (Word16) whose value falls in the |
  183.  |             range : 0x0000 0000 <= var_out <= 0x0000 7fff.                |
  184.  |___________________________________________________________________________|
  185. */
  186. Word16 abs_s(Word16 var1)
  187. {
  188.     Word16 var_out;
  189.     if (var1 == (Word16)0X8000 ) {
  190.         var_out = MAX_16;
  191.     }
  192.     else {
  193.         if (var1 < 0) {
  194.             var_out = -var1;
  195.         }
  196.         else {
  197.             var_out = var1;
  198.         }
  199.     }
  200.     return(var_out);
  201. }
  202. /*___________________________________________________________________________
  203.  |                                                                           |
  204.  |   Function Name : shl                                                     |
  205.  |                                                                           |
  206.  |   Purpose :                                                               |
  207.  |                                                                           |
  208.  |   Arithmetically shift the 16 bit input var1 left var2 positions.Zero fill|
  209.  |   the var2 LSB of the result. If var2 is negative, arithmetically shift   |
  210.  |   var1 right by -var2 with sign extension. Saturate the result in case of |
  211.  |   underflows or overflows.                                                |
  212.  |                                                                           |
  213.  |   Complexity weight : 1                                                   |
  214.  |                                                                           |
  215.  |   Inputs :                                                                |
  216.  |                                                                           |
  217.  |    var1                                                                   |
  218.  |             16 bit short signed integer (Word16) whose value falls in the |
  219.  |             range : 0xffff 8000 <= var1 <= 0x0000 7fff.                   |
  220.  |                                                                           |
  221.  |    var2                                                                   |
  222.  |             16 bit short signed integer (Word16) whose value falls in the |
  223.  |             range : 0xffff 8000 <= var1 <= 0x0000 7fff.                   |
  224.  |                                                                           |
  225.  |   Outputs :                                                               |
  226.  |                                                                           |
  227.  |    none                                                                   |
  228.  |                                                                           |
  229.  |   Return Value :                                                          |
  230.  |                                                                           |
  231.  |    var_out                                                                |
  232.  |             16 bit short signed integer (Word16) whose value falls in the |
  233.  |             range : 0xffff 8000 <= var_out <= 0x0000 7fff.                |
  234.  |___________________________________________________________________________|
  235. */
  236. Word16 shl(Word16 var1,Word16 var2)
  237. {
  238.     Word16 var_out;
  239.     Word32 L_result;
  240.     if (var2 < 0) {
  241.         var_out = shr(var1, (Word16)-var2);
  242.     }
  243.     else {
  244.         L_result = (Word32) var1 * ((Word32) 1 << var2);
  245.         if ( ((var2 > 15) && (var1 != 0)) ||
  246.              (L_result != (Word32)((Word16) L_result)) ) {
  247.             Overflow = 1;
  248.             var_out = (var1 > 0) ? MAX_16 : MIN_16;
  249.         }
  250.         else {
  251.             var_out = extract_l(L_result);
  252.         }
  253.     }
  254.     return(var_out);
  255. }
  256. /*___________________________________________________________________________
  257.  |                                                                           |
  258.  |   Function Name : shr                                                     |
  259.  |                                                                           |
  260.  |   Purpose :                                                               |
  261.  |                                                                           |
  262.  |   Arithmetically shift the 16 bit input var1 right var2 positions with    |
  263.  |   sign extension. If var2 is negative, arithmetically shift var1 left by  |
  264.  |   -var2 with sign extension. Saturate the result in case of underflows or |
  265.  |   overflows.                                                              |
  266.  |                                                                           |
  267.  |   Complexity weight : 1                                                   |
  268.  |                                                                           |
  269.  |   Inputs :                                                                |
  270.  |                                                                           |
  271.  |    var1                                                                   |
  272.  |             16 bit short signed integer (Word16) whose value falls in the |
  273.  |             range : 0xffff 8000 <= var1 <= 0x0000 7fff.                   |
  274.  |                                                                           |
  275.  |    var2                                                                   |
  276.  |             16 bit short signed integer (Word16) whose value falls in the |
  277.  |             range : 0xffff 8000 <= var1 <= 0x0000 7fff.                   |
  278.  |                                                                           |
  279.  |   Outputs :                                                               |
  280.  |                                                                           |
  281.  |    none                                                                   |
  282.  |                                                                           |
  283.  |   Return Value :                                                          |
  284.  |                                                                           |
  285.  |    var_out                                                                |
  286.  |             16 bit short signed integer (Word16) whose value falls in the |
  287.  |             range : 0xffff 8000 <= var_out <= 0x0000 7fff.                |
  288.  |___________________________________________________________________________|
  289. */
  290. Word16 shr(Word16 var1,Word16 var2)
  291. {
  292.     Word16 var_out;
  293.     if (var2 < 0) {
  294.         var_out = shl(var1, (Word16)-var2);
  295.     }
  296.     else {
  297.         if (var2 >= 15) {
  298.             var_out = (var1 < (Word16)0) ? (Word16)-1 : (Word16)0;
  299.         }
  300.         else {
  301.             if (var1 < 0) {
  302.                 var_out = ~(( ~var1) >> var2 );
  303.             }
  304.             else {
  305.                 var_out = var1 >> var2;
  306.             }
  307.         }
  308.     }
  309.     return(var_out);
  310. }
  311. /*___________________________________________________________________________
  312.  |                                                                           |
  313.  |   Function Name : mult                                                    |
  314.  |                                                                           |
  315.  |   Purpose :                                                               |
  316.  |                                                                           |
  317.  |    Performs the multiplication of var1 by var2 and gives a 16 bit result  |
  318.  |    which is scaled i.e.:                                                  |
  319.  |             mult(var1,var2) = shr((var1 times var2),15) and               |
  320.  |             mult(-32768,-32768) = 32767.                                  |
  321.  |                                                                           |
  322.  |   Complexity weight : 1                                                   |
  323.  |                                                                           |
  324.  |   Inputs :                                                                |
  325.  |                                                                           |
  326.  |    var1                                                                   |
  327.  |             16 bit short signed integer (Word16) whose value falls in the |
  328.  |             range : 0xffff 8000 <= var1 <= 0x0000 7fff.                   |
  329.  |                                                                           |
  330.  |    var2                                                                   |
  331.  |             16 bit short signed integer (Word16) whose value falls in the |
  332.  |             range : 0xffff 8000 <= var1 <= 0x0000 7fff.                   |
  333.  |                                                                           |
  334.  |   Outputs :                                                               |
  335.  |                                                                           |
  336.  |    none                                                                   |
  337.  |                                                                           |
  338.  |   Return Value :                                                          |
  339.  |                                                                           |
  340.  |    var_out                                                                |
  341.  |             16 bit short signed integer (Word16) whose value falls in the |
  342.  |             range : 0xffff 8000 <= var_out <= 0x0000 7fff.                |
  343.  |___________________________________________________________________________|
  344. */
  345. Word16 mult(Word16 var1, Word16 var2)
  346. {
  347.     Word16 var_out;
  348.     Word32 L_produit;
  349.     L_produit = (Word32)var1 * (Word32)var2;
  350.     L_produit = (L_produit & (Word32) 0xffff8000L) >> 15;
  351.     if (L_produit & (Word32) 0x00010000L) {
  352.         L_produit |= (Word32) 0xffff0000L;
  353.     }
  354.     var_out = sature(L_produit);
  355.     return(var_out);
  356. }
  357. /*___________________________________________________________________________
  358.  |                                                                           |
  359.  |   Function Name : L_mult                                                  |
  360.  |                                                                           |
  361.  |   Purpose :                                                               |
  362.  |                                                                           |
  363.  |   L_mult is the 32 bit result of the multiplication of var1 times var2    |
  364.  |   with one shift left i.e.:                                               |
  365.  |        L_mult(var1,var2) = shl((var1 times var2),1) and                   |
  366.  |        L_mult(-32768,-32768) = 2147483647.                                |
  367.  |                                                                           |
  368.  |   Complexity weight : 1                                                   |
  369.  |                                                                           |
  370.  |   Inputs :                                                                |
  371.  |                                                                           |
  372.  |    var1                                                                   |
  373.  |             16 bit short signed integer (Word16) whose value falls in the |
  374.  |             range : 0xffff 8000 <= var1 <= 0x0000 7fff.                   |
  375.  |                                                                           |
  376.  |    var2                                                                   |
  377.  |             16 bit short signed integer (Word16) whose value falls in the |
  378.  |             range : 0xffff 8000 <= var1 <= 0x0000 7fff.                   |
  379.  |                                                                           |
  380.  |   Outputs :                                                               |
  381.  |                                                                           |
  382.  |    none                                                                   |
  383.  |                                                                           |
  384.  |   Return Value :                                                          |
  385.  |                                                                           |
  386.  |    L_var_out                                                              |
  387.  |             32 bit long signed integer (Word32) whose value falls in the  |
  388.  |             range : 0x8000 0000 <= L_var_out <= 0x7fff ffff.              |
  389.  |___________________________________________________________________________|
  390. */
  391. Word32 L_mult(Word16 var1,Word16 var2)
  392. {
  393.     Word32 L_var_out;
  394.     L_var_out = (Word32)var1 * (Word32)var2;
  395.     if (L_var_out != (Word32)0x40000000L) {
  396.         L_var_out *= 2L;
  397.     }
  398.     else {
  399.         Overflow = 1;
  400.         L_var_out = MAX_32;
  401.     }
  402.     return(L_var_out);
  403. }
  404. /*___________________________________________________________________________
  405.  |                                                                           |
  406.  |   Function Name : negate                                                  |
  407.  |                                                                           |
  408.  |   Purpose :                                                               |
  409.  |                                                                           |
  410.  |   Negate var1 with saturation, saturate in the case where input is -32768:|
  411.  |                negate(var1) = sub(0,var1).                                |
  412.  |                                                                           |
  413.  |   Complexity weight : 1                                                   |
  414.  |                                                                           |
  415.  |   Inputs :                                                                |
  416.  |                                                                           |
  417.  |    var1                                                                   |
  418.  |             16 bit short signed integer (Word16) whose value falls in the |
  419.  |             range : 0xffff 8000 <= var1 <= 0x0000 7fff.                   |
  420.  |                                                                           |
  421.  |   Outputs :                                                               |
  422.  |                                                                           |
  423.  |    none                                                                   |
  424.  |                                                                           |
  425.  |   Return Value :                                                          |
  426.  |                                                                           |
  427.  |    var_out                                                                |
  428.  |             16 bit short signed integer (Word16) whose value falls in the |
  429.  |             range : 0xffff 8000 <= var_out <= 0x0000 7fff.                |
  430.  |___________________________________________________________________________|
  431. */
  432. Word16 negate(Word16 var1)
  433. {
  434.     Word16 var_out;
  435.     var_out = (var1 == MIN_16) ? MAX_16 : -var1;
  436.     return(var_out);
  437. }
  438. /*___________________________________________________________________________
  439.  |                                                                           |
  440.  |   Function Name : extract_h                                               |
  441.  |                                                                           |
  442.  |   Purpose :                                                               |
  443.  |                                                                           |
  444.  |   Return the 16 MSB of L_var1.                                            |
  445.  |                                                                           |
  446.  |   Complexity weight : 1                                                   |
  447.  |                                                                           |
  448.  |   Inputs :                                                                |
  449.  |                                                                           |
  450.  |    L_var1                                                                 |
  451.  |             32 bit long signed integer (Word32 ) whose value falls in the |
  452.  |             range : 0x8000 0000 <= L_var1 <= 0x7fff ffff.                 |
  453.  |                                                                           |
  454.  |   Outputs :                                                               |
  455.  |                                                                           |
  456.  |    none                                                                   |
  457.  |                                                                           |
  458.  |   Return Value :                                                          |
  459.  |                                                                           |
  460.  |    var_out                                                                |
  461.  |             16 bit short signed integer (Word16) whose value falls in the |
  462.  |             range : 0xffff 8000 <= var_out <= 0x0000 7fff.                |
  463.  |___________________________________________________________________________|
  464. */
  465. Word16 extract_h(Word32 L_var1)
  466. {
  467.     Word16 var_out;
  468.     var_out = (Word16) (L_var1 >> 16);
  469.     return(var_out);
  470. }
  471. /*___________________________________________________________________________
  472.  |                                                                           |
  473.  |   Function Name : extract_l                                               |
  474.  |                                                                           |
  475.  |   Purpose :                                                               |
  476.  |                                                                           |
  477.  |   Return the 16 LSB of L_var1.                                            |
  478.  |                                                                           |
  479.  |   Complexity weight : 1                                                   |
  480.  |                                                                           |
  481.  |   Inputs :                                                                |
  482.  |                                                                           |
  483.  |    L_var1                                                                 |
  484.  |             32 bit long signed integer (Word32 ) whose value falls in the |
  485.  |             range : 0x8000 0000 <= L_var1 <= 0x7fff ffff.                 |
  486.  |                                                                           |
  487.  |   Outputs :                                                               |
  488.  |                                                                           |
  489.  |    none                                                                   |
  490.  |                                                                           |
  491.  |   Return Value :                                                          |
  492.  |                                                                           |
  493.  |    var_out                                                                |
  494.  |             16 bit short signed integer (Word16) whose value falls in the |
  495.  |             range : 0xffff 8000 <= var_out <= 0x0000 7fff.                |
  496.  |___________________________________________________________________________|
  497. */
  498. Word16 extract_l(Word32 L_var1)
  499. {
  500.     Word16 var_out;
  501.     var_out = (Word16) L_var1;
  502.     return(var_out);
  503. }
  504. /*___________________________________________________________________________
  505.  |                                                                           |
  506.  |   Function Name : round                                                   |
  507.  |                                                                           |
  508.  |   Purpose :                                                               |
  509.  |                                                                           |
  510.  |   Round the lower 16 bits of the 32 bit input number into its MS 16 bits  |
  511.  |   with saturation. Shift the resulting bits right by 16 and return the 16 |
  512.  |   bit number:                                                             |
  513.  |               round(L_var1) = extract_h(L_add(L_var1,32768))              |
  514.  |                                                                           |
  515.  |   Complexity weight : 1                                                   |
  516.  |                                                                           |
  517.  |   Inputs :                                                                |
  518.  |                                                                           |
  519.  |    L_var1                                                                 |
  520.  |             32 bit long signed integer (Word32 ) whose value falls in the |
  521.  |             range : 0x8000 0000 <= L_var1 <= 0x7fff ffff.                 |
  522.  |                                                                           |
  523.  |   Outputs :                                                               |
  524.  |                                                                           |
  525.  |    none                                                                   |
  526.  |                                                                           |
  527.  |   Return Value :                                                          |
  528.  |                                                                           |
  529.  |    var_out                                                                |
  530.  |             16 bit short signed integer (Word16) whose value falls in the |
  531.  |             range : 0xffff 8000 <= var_out <= 0x0000 7fff.                |
  532.  |___________________________________________________________________________|
  533. */
  534. Word16 round(Word32 L_var1)
  535. {
  536.     Word16 var_out;
  537.     Word32 L_arrondi;
  538.     L_arrondi = L_add(L_var1, (Word32)0x00008000L);
  539.     var_out = extract_h(L_arrondi);
  540.     return(var_out);
  541. }
  542. /*___________________________________________________________________________
  543.  |                                                                           |
  544.  |   Function Name : L_mac                                                   |
  545.  |                                                                           |
  546.  |   Purpose :                                                               |
  547.  |                                                                           |
  548.  |   Multiply var1 by var2 and shift the result left by 1. Add the 32 bit    |
  549.  |   result to L_var3 with saturation, return a 32 bit result:               |
  550.  |        L_mac(L_var3,var1,var2) = L_add(L_var3,(L_mult(var1,var2)).        |
  551.  |                                                                           |
  552.  |   Complexity weight : 1                                                   |
  553.  |                                                                           |
  554.  |   Inputs :                                                                |
  555.  |                                                                           |
  556.  |    L_var3   32 bit long signed integer (Word32) whose value falls in the  |
  557.  |             range : 0x8000 0000 <= L_var3 <= 0x7fff ffff.                 |
  558.  |                                                                           |
  559.  |    var1                                                                   |
  560.  |             16 bit short signed integer (Word16) whose value falls in the |
  561.  |             range : 0xffff 8000 <= var1 <= 0x0000 7fff.                   |
  562.  |                                                                           |
  563.  |    var2                                                                   |
  564.  |             16 bit short signed integer (Word16) whose value falls in the |
  565.  |             range : 0xffff 8000 <= var1 <= 0x0000 7fff.                   |
  566.  |                                                                           |
  567.  |   Outputs :                                                               |
  568.  |                                                                           |
  569.  |    none                                                                   |
  570.  |                                                                           |
  571.  |   Return Value :                                                          |
  572.  |                                                                           |
  573.  |    L_var_out                                                              |
  574.  |             32 bit long signed integer (Word32) whose value falls in the  |
  575.  |             range : 0x8000 0000 <= L_var_out <= 0x7fff ffff.              |
  576.  |___________________________________________________________________________|
  577. */
  578. Word32 L_mac(Word32 L_var3, Word16 var1, Word16 var2)
  579. {
  580.     Word32 L_var_out;
  581.     Word32 L_produit;
  582.     L_produit = L_mult(var1, var2);
  583.     L_var_out = L_add(L_var3, L_produit);
  584.     return(L_var_out);
  585. }
  586. /*___________________________________________________________________________
  587.  |                                                                           |
  588.  |   Function Name : L_msu                                                   |
  589.  |                                                                           |
  590.  |   Purpose :                                                               |
  591.  |                                                                           |
  592.  |   Multiply var1 by var2 and shift the result left by 1. Subtract the 32   |
  593.  |   bit result to L_var3 with saturation, return a 32 bit result:           |
  594.  |        L_msu(L_var3,var1,var2) = L_sub(L_var3,(L_mult(var1,var2)).        |
  595.  |                                                                           |
  596.  |   Complexity weight : 1                                                   |
  597.  |                                                                           |
  598.  |   Inputs :                                                                |
  599.  |                                                                           |
  600.  |    L_var3   32 bit long signed integer (Word32) whose value falls in the  |
  601.  |             range : 0x8000 0000 <= L_var3 <= 0x7fff ffff.                 |
  602.  |                                                                           |
  603.  |    var1                                                                   |
  604.  |             16 bit short signed integer (Word16) whose value falls in the |
  605.  |             range : 0xffff 8000 <= var1 <= 0x0000 7fff.                   |
  606.  |                                                                           |
  607.  |    var2                                                                   |
  608.  |             16 bit short signed integer (Word16) whose value falls in the |
  609.  |             range : 0xffff 8000 <= var1 <= 0x0000 7fff.                   |
  610.  |                                                                           |
  611.  |   Outputs :                                                               |
  612.  |                                                                           |
  613.  |    none                                                                   |
  614.  |                                                                           |
  615.  |   Return Value :                                                          |
  616.  |                                                                           |
  617.  |    L_var_out                                                              |
  618.  |             32 bit long signed integer (Word32) whose value falls in the  |
  619.  |             range : 0x8000 0000 <= L_var_out <= 0x7fff ffff.              |
  620.  |___________________________________________________________________________|
  621. */
  622. Word32 L_msu(Word32 L_var3, Word16 var1, Word16 var2)
  623. {
  624.     Word32 L_var_out;
  625.     Word32 L_produit;
  626.     L_produit = L_mult(var1, var2);
  627.     L_var_out = L_sub(L_var3, L_produit);
  628.     return(L_var_out);
  629. }
  630. /*___________________________________________________________________________
  631.  |                                                                           |
  632.  |   Function Name : L_macNs                                                 |
  633.  |                                                                           |
  634.  |   Purpose :                                                               |
  635.  |                                                                           |
  636.  |   Multiply var1 by var2 and shift the result left by 1. Add the 32 bit    |
  637.  |   result to L_var3 without saturation, return a 32 bit result. Generate   |
  638.  |   carry and overflow values :                                             |
  639.  |        L_macNs(L_var3,var1,var2) = L_add_c(L_var3,(L_mult(var1,var2)).    |
  640.  |                                                                           |
  641.  |   Complexity weight : 1                                                   |
  642.  |                                                                           |
  643.  |   Inputs :                                                                |
  644.  |                                                                           |
  645.  |    L_var3   32 bit long signed integer (Word32) whose value falls in the  |
  646.  |             range : 0x8000 0000 <= L_var3 <= 0x7fff ffff.                 |
  647.  |                                                                           |
  648.  |    var1                                                                   |
  649.  |             16 bit short signed integer (Word16) whose value falls in the |
  650.  |             range : 0xffff 8000 <= var1 <= 0x0000 7fff.                   |
  651.  |                                                                           |
  652.  |    var2                                                                   |
  653.  |             16 bit short signed integer (Word16) whose value falls in the |
  654.  |             range : 0xffff 8000 <= var1 <= 0x0000 7fff.                   |
  655.  |                                                                           |
  656.  |   Outputs :                                                               |
  657.  |                                                                           |
  658.  |    none                                                                   |
  659.  |                                                                           |
  660.  |   Return Value :                                                          |
  661.  |                                                                           |
  662.  |    L_var_out                                                              |
  663.  |             32 bit long signed integer (Word32) whose value falls in the  |
  664.  |             range : 0x8000 0000 <= L_var_out <= 0x7fff ffff.              |
  665.  |                                                                           |
  666.  |   Caution :                                                               |
  667.  |                                                                           |
  668.  |    In some cases the Carry flag has to be cleared or set before using op- |
  669.  |    rators which take into account its value.                              |
  670.  |___________________________________________________________________________|
  671. */
  672. Word32 L_macNs(Word32 L_var3, Word16 var1, Word16 var2)
  673. {
  674.     Word32 L_var_out;
  675.     L_var_out = L_mult(var1, var2);
  676.     L_var_out = L_add_c(L_var3, L_var_out);
  677.     return(L_var_out);
  678. }
  679. /*___________________________________________________________________________
  680.  |                                                                           |
  681.  |   Function Name : L_msuNs                                                 |
  682.  |                                                                           |
  683.  |   Purpose :                                                               |
  684.  |                                                                           |
  685.  |   Multiply var1 by var2 and shift the result left by 1. Subtract the 32   |
  686.  |   bit result from L_var3 without saturation, return a 32 bit result. Ge-  |
  687.  |   nerate carry and overflow values :                                      |
  688.  |        L_msuNs(L_var3,var1,var2) = L_sub_c(L_var3,(L_mult(var1,var2)).    |
  689.  |                                                                           |
  690.  |   Complexity weight : 1                                                   |
  691.  |                                                                           |
  692.  |   Inputs :                                                                |
  693.  |                                                                           |
  694.  |    L_var3   32 bit long signed integer (Word32) whose value falls in the  |
  695.  |             range : 0x8000 0000 <= L_var3 <= 0x7fff ffff.                 |
  696.  |                                                                           |
  697.  |    var1                                                                   |
  698.  |             16 bit short signed integer (Word16) whose value falls in the |
  699.  |             range : 0xffff 8000 <= var1 <= 0x0000 7fff.                   |
  700.  |                                                                           |
  701.  |    var2                                                                   |
  702.  |             16 bit short signed integer (Word16) whose value falls in the |
  703.  |             range : 0xffff 8000 <= var1 <= 0x0000 7fff.                   |
  704.  |                                                                           |
  705.  |   Outputs :                                                               |
  706.  |                                                                           |
  707.  |    none                                                                   |
  708.  |                                                                           |
  709.  |   Return Value :                                                          |
  710.  |                                                                           |
  711.  |    L_var_out                                                              |
  712.  |             32 bit long signed integer (Word32) whose value falls in the  |
  713.  |             range : 0x8000 0000 <= L_var_out <= 0x7fff ffff.              |
  714.  |                                                                           |
  715.  |   Caution :                                                               |
  716.  |                                                                           |
  717.  |    In some cases the Carry flag has to be cleared or set before using op- |
  718.  |    rators which take into account its value.                              |
  719.  |___________________________________________________________________________|
  720. */
  721. Word32 L_msuNs(Word32 L_var3, Word16 var1, Word16 var2)
  722. {
  723.     Word32 L_var_out;
  724.     L_var_out = L_mult(var1,var2);
  725.     L_var_out = L_sub_c(L_var3,L_var_out);
  726.     return(L_var_out);
  727. }
  728. /*___________________________________________________________________________
  729.  |                                                                           |
  730.  |   Function Name : L_add                                                   |
  731.  |                                                                           |
  732.  |   Purpose :                                                               |
  733.  |                                                                           |
  734.  |   32 bits addition of the two 32 bits variables (L_var1+L_var2) with      |
  735.  |   overflow control and saturation; the result is set at +214783647 when   |
  736.  |   overflow occurs or at -214783648 when underflow occurs.                 |
  737.  |                                                                           |
  738.  |   Complexity weight : 2                                                   |
  739.  |                                                                           |
  740.  |   Inputs :                                                                |
  741.  |                                                                           |
  742.  |    L_var1   32 bit long signed integer (Word32) whose value falls in the  |
  743.  |             range : 0x8000 0000 <= L_var3 <= 0x7fff ffff.                 |
  744.  |                                                                           |
  745.  |    L_var2   32 bit long signed integer (Word32) whose value falls in the  |
  746.  |             range : 0x8000 0000 <= L_var3 <= 0x7fff ffff.                 |
  747.  |                                                                           |
  748.  |   Outputs :                                                               |
  749.  |                                                                           |
  750.  |    none                                                                   |
  751.  |                                                                           |
  752.  |   Return Value :                                                          |
  753.  |                                                                           |
  754.  |    L_var_out                                                              |
  755.  |             32 bit long signed integer (Word32) whose value falls in the  |
  756.  |             range : 0x8000 0000 <= L_var_out <= 0x7fff ffff.              |
  757.  |___________________________________________________________________________|
  758. */
  759. Word32 L_add(Word32 L_var1, Word32 L_var2)
  760. {
  761.     Word32 L_var_out;
  762.     L_var_out = L_var1 + L_var2;
  763.     if (((L_var1 ^ L_var2) & MIN_32) == 0L) {
  764.         if ((L_var_out ^ L_var1) & MIN_32) {
  765.             L_var_out = (L_var1 < 0L) ? MIN_32 : MAX_32;
  766.             Overflow = 1;
  767.         }
  768.     }
  769.     return(L_var_out);
  770. }
  771. /*___________________________________________________________________________
  772.  |                                                                           |
  773.  |   Function Name : L_sub                                                   |
  774.  |                                                                           |
  775.  |   Purpose :                                                               |
  776.  |                                                                           |
  777.  |   32 bits subtraction of the two 32 bits variables (L_var1-L_var2) with   |
  778.  |   overflow control and saturation; the result is set at +214783647 when   |
  779.  |   overflow occurs or at -214783648 when underflow occurs.                 |
  780.  |                                                                           |
  781.  |   Complexity weight : 2                                                   |
  782.  |                                                                           |
  783.  |   Inputs :                                                                |
  784.  |                                                                           |
  785.  |    L_var1   32 bit long signed integer (Word32) whose value falls in the  |
  786.  |             range : 0x8000 0000 <= L_var3 <= 0x7fff ffff.                 |
  787.  |                                                                           |
  788.  |    L_var2   32 bit long signed integer (Word32) whose value falls in the  |
  789.  |             range : 0x8000 0000 <= L_var3 <= 0x7fff ffff.                 |
  790.  |                                                                           |
  791.  |   Outputs :                                                               |
  792.  |                                                                           |
  793.  |    none                                                                   |
  794.  |                                                                           |
  795.  |   Return Value :                                                          |
  796.  |                                                                           |
  797.  |    L_var_out                                                              |
  798.  |             32 bit long signed integer (Word32) whose value falls in the  |
  799.  |             range : 0x8000 0000 <= L_var_out <= 0x7fff ffff.              |
  800.  |___________________________________________________________________________|
  801. */
  802. Word32 L_sub(Word32 L_var1, Word32 L_var2)
  803. {
  804.     Word32 L_var_out;
  805.     L_var_out = L_var1 - L_var2;
  806.     if (((L_var1 ^ L_var2) & MIN_32) != 0L) {
  807.         if ((L_var_out ^ L_var1) & MIN_32) {
  808.             L_var_out = (L_var1 < 0L) ? MIN_32 : MAX_32;
  809.             Overflow = 1;
  810.         }
  811.     }
  812.     return(L_var_out);
  813. }
  814. /*___________________________________________________________________________
  815.  |                                                                           |
  816.  |   Function Name : L_add_c                                                 |
  817.  |                                                                           |
  818.  |   Purpose :                                                               |
  819.  |                                                                           |
  820.  |   Performs 32 bits addition of the two 32 bits variables (L_var1+L_var2+C)|
  821.  |   with carry. No saturation. Generate carry and Overflow values. The car- |
  822.  |   ry and overflow values are binary variables which can be tested and as- |
  823.  |   signed values.                                                          |
  824.  |                                                                           |
  825.  |   Complexity weight : 2                                                   |
  826.  |                                                                           |
  827.  |   Inputs :                                                                |
  828.  |                                                                           |
  829.  |    L_var1   32 bit long signed integer (Word32) whose value falls in the  |
  830.  |             range : 0x8000 0000 <= L_var3 <= 0x7fff ffff.                 |
  831.  |                                                                           |
  832.  |    L_var2   32 bit long signed integer (Word32) whose value falls in the  |
  833.  |             range : 0x8000 0000 <= L_var3 <= 0x7fff ffff.                 |
  834.  |                                                                           |
  835.  |   Outputs :                                                               |
  836.  |                                                                           |
  837.  |    none                                                                   |
  838.  |                                                                           |
  839.  |   Return Value :                                                          |
  840.  |                                                                           |
  841.  |    L_var_out                                                              |
  842.  |             32 bit long signed integer (Word32) whose value falls in the  |
  843.  |             range : 0x8000 0000 <= L_var_out <= 0x7fff ffff.              |
  844.  |                                                                           |
  845.  |   Caution :                                                               |
  846.  |                                                                           |
  847.  |    In some cases the Carry flag has to be cleared or set before using op- |
  848.  |    rators which take into account its value.                              |
  849.  |___________________________________________________________________________|
  850. */
  851. Word32 L_add_c(Word32 L_var1, Word32 L_var2)
  852. {
  853.     Word32 L_var_out;
  854.     Word32 L_test;
  855.     Flag carry_int = 0;
  856.     L_var_out = L_var1 + L_var2 + (Word32)Carry;
  857.     L_test = L_var1 + L_var2;
  858.     if ((L_var1 > 0L) && (L_var2 > 0L) && (L_test < 0L)) {
  859.         Overflow = 1;
  860.         carry_int = 0;
  861.     }
  862.     else {
  863.         if ((L_var1 < 0L) && (L_var2 < 0L) && (L_test > 0L)) {
  864.             Overflow = 1;
  865.             carry_int = 1;
  866.         }
  867.         else {
  868.             if (((L_var1 ^ L_var2) < 0L) && (L_test > 0L)) {
  869.                 Overflow = 0;
  870.                 carry_int = 1;
  871.             }
  872.             else {
  873.                 Overflow = 0;
  874.                 carry_int = 0;
  875.             }
  876.         }
  877.     }
  878.     if (Carry) {
  879.         if (L_test == MAX_32) {
  880.             Overflow = 1;
  881.             Carry = carry_int;
  882.         }
  883.         else {
  884.             if (L_test == (Word32) 0xFFFFFFFFL) {
  885.                 Carry = 1;
  886.             }
  887.             else {
  888.                 Carry = carry_int;
  889.             }
  890.         }
  891.     }
  892.     else {
  893.         Carry = carry_int;
  894.     }
  895.     return(L_var_out);
  896. }
  897. /*___________________________________________________________________________
  898.  |                                                                           |
  899.  |   Function Name : L_sub_c                                                 |
  900.  |                                                                           |
  901.  |   Purpose :                                                               |
  902.  |                                                                           |
  903.  |   Performs 32 bits subtraction of the two 32 bits variables with carry    |
  904.  |   (borrow) : L_var1-L_var2-C. No saturation. Generate carry and Overflow  |
  905.  |   values. The carry and overflow values are binary variables which can    |
  906.  |   be tested and assigned values.                                          |
  907.  |                                                                           |
  908.  |   Complexity weight : 2                                                   |
  909.  |                                                                           |
  910.  |   Inputs :                                                                |
  911.  |                                                                           |
  912.  |    L_var1   32 bit long signed integer (Word32) whose value falls in the  |
  913.  |             range : 0x8000 0000 <= L_var3 <= 0x7fff ffff.                 |
  914.  |                                                                           |
  915.  |    L_var2   32 bit long signed integer (Word32) whose value falls in the  |
  916.  |             range : 0x8000 0000 <= L_var3 <= 0x7fff ffff.                 |
  917.  |                                                                           |
  918.  |   Outputs :                                                               |
  919.  |                                                                           |
  920.  |    none                                                                   |
  921.  |                                                                           |
  922.  |   Return Value :                                                          |
  923.  |                                                                           |
  924.  |    L_var_out                                                              |
  925.  |             32 bit long signed integer (Word32) whose value falls in the  |
  926.  |             range : 0x8000 0000 <= L_var_out <= 0x7fff ffff.              |
  927.  |                                                                           |
  928.  |   Caution :                                                               |
  929.  |                                                                           |
  930.  |    In some cases the Carry flag has to be cleared or set before using op- |
  931.  |    rators which take into account its value.                              |
  932.  |___________________________________________________________________________|
  933. */
  934. Word32 L_sub_c(Word32 L_var1, Word32 L_var2)
  935. {
  936.     Word32 L_var_out;
  937.     Word32 L_test;
  938.     Flag carry_int = 0;
  939.     if (Carry) {
  940.         Carry = 0;
  941.         if (L_var2 != MIN_32) {
  942.             L_var_out = L_add_c(L_var1,-L_var2);
  943.         }
  944.         else {
  945.             L_var_out = L_var1 - L_var2;
  946.             if (L_var1 > 0L) {
  947.                 Overflow = 1;
  948.                 Carry = 0;
  949.             }
  950.         }
  951.     }
  952.     else {
  953.         L_var_out = L_var1 - L_var2 - (Word32)0X00000001L;
  954.         L_test = L_var1 - L_var2;
  955.         if ((L_test < 0L) && (L_var1 > 0L) && (L_var2 < 0L)) {
  956.             Overflow = 1;
  957.             carry_int = 0;
  958.         }
  959.         else if ((L_test > 0L) && (L_var1 < 0L) && (L_var2 > 0L)) {
  960.             Overflow = 1;
  961.             carry_int = 1;
  962.         }
  963.         else if ((L_test > 0L) && ((L_var1 ^ L_var2) > 0L)) {
  964.             Overflow = 0;
  965.             carry_int = 1;
  966.         }
  967.         if (L_test == MIN_32) {
  968.             Overflow = 1;
  969.             Carry = carry_int;
  970.         }
  971.         else {
  972.             Carry = carry_int;
  973.         }
  974.     }
  975.     return(L_var_out);
  976. }
  977. /*___________________________________________________________________________
  978.  |                                                                           |
  979.  |   Function Name : L_negate                                                |
  980.  |                                                                           |
  981.  |   Purpose :                                                               |
  982.  |                                                                           |
  983.  |   Negate the 32 bit variable L_var1 with saturation; saturate in the case |
  984.  |   where input is -2147483648 (0x8000 0000).                               |
  985.  |                                                                           |
  986.  |   Complexity weight : 2                                                   |
  987.  |                                                                           |
  988.  |   Inputs :                                                                |
  989.  |                                                                           |
  990.  |    L_var1   32 bit long signed integer (Word32) whose value falls in the  |
  991.  |             range : 0x8000 0000 <= L_var3 <= 0x7fff ffff.                 |
  992.  |                                                                           |
  993.  |   Outputs :                                                               |
  994.  |                                                                           |
  995.  |    none                                                                   |
  996.  |                                                                           |
  997.  |   Return Value :                                                          |
  998.  |                                                                           |
  999.  |    L_var_out                                                              |
  1000.  |             32 bit long signed integer (Word32) whose value falls in the  |
  1001.  |             range : 0x8000 0000 <= L_var_out <= 0x7fff ffff.              |
  1002.  |___________________________________________________________________________|
  1003. */
  1004. Word32 L_negate(Word32 L_var1)
  1005. {
  1006.     Word32 L_var_out;
  1007.     L_var_out = (L_var1 == MIN_32) ? MAX_32 : -L_var1;
  1008.     return(L_var_out);
  1009. }
  1010. /*___________________________________________________________________________
  1011.  |                                                                           |
  1012.  |   Function Name : mult_r                                                  |
  1013.  |                                                                           |
  1014.  |   Purpose :                                                               |
  1015.  |                                                                           |
  1016.  |   Same as mult with rounding, i.e.:                                       |
  1017.  |     mult_r(var1,var2) = shr(((var1*var2) + 16384),15) and                 |
  1018.  |     mult_r(-32768,-32768) = 32767.                                        |
  1019.  |                                                                           |
  1020.  |   Complexity weight : 2                                                   |
  1021.  |                                                                           |
  1022.  |   Inputs :                                                                |
  1023.  |                                                                           |
  1024.  |    var1                                                                   |
  1025.  |             16 bit short signed integer (Word16) whose value falls in the |
  1026.  |             range : 0xffff 8000 <= var1 <= 0x0000 7fff.                   |
  1027.  |                                                                           |
  1028.  |    var2                                                                   |
  1029.  |             16 bit short signed integer (Word16) whose value falls in the |
  1030.  |             range : 0xffff 8000 <= var1 <= 0x0000 7fff.                   |
  1031.  |                                                                           |
  1032.  |   Outputs :                                                               |
  1033.  |                                                                           |
  1034.  |    none                                                                   |
  1035.  |                                                                           |
  1036.  |   Return Value :                                                          |
  1037.  |                                                                           |
  1038.  |    var_out                                                                |
  1039.  |             16 bit short signed integer (Word16) whose value falls in the |
  1040.  |             range : 0xffff 8000 <= var_out <= 0x0000 7fff.                |
  1041.  |___________________________________________________________________________|
  1042. */
  1043. Word16 mult_r(Word16 var1, Word16 var2)
  1044. {
  1045.     Word16 var_out;
  1046.     Word32 L_produit_arr;
  1047.     L_produit_arr = (Word32)var1 * (Word32)var2; /* product */
  1048.     L_produit_arr += (Word32) 0x00004000L;        /* round */
  1049.     L_produit_arr &= (Word32) 0xffff8000L;
  1050.     L_produit_arr >>= 15;                        /* shift */
  1051.     if (L_produit_arr & (Word32) 0x00010000L)  {
  1052.                                      /* sign extend when necessary */
  1053.         L_produit_arr |= (Word32) 0xffff0000L;
  1054.     }
  1055.     var_out = sature(L_produit_arr);
  1056.     return(var_out);
  1057. }
  1058. /*___________________________________________________________________________
  1059.  |                                                                           |
  1060.  |   Function Name : L_shl                                                   |
  1061.  |                                                                           |
  1062.  |   Purpose :                                                               |
  1063.  |                                                                           |
  1064.  |   Arithmetically shift the 32 bit input L_var1 left var2 positions. Zero  |
  1065.  |   fill the var2 LSB of the result. If var2 is negative, L_var1 right by   |
  1066.  |   -var2 arithmetically shift with sign extension. Saturate the result in  |
  1067.  |   case of underflows or overflows.                                        |
  1068.  |                                                                           |
  1069.  |   Complexity weight : 2                                                   |
  1070.  |                                                                           |
  1071.  |   Inputs :                                                                |
  1072.  |                                                                           |
  1073.  |    L_var1   32 bit long signed integer (Word32) whose value falls in the  |
  1074.  |             range : 0x8000 0000 <= L_var3 <= 0x7fff ffff.                 |
  1075.  |                                                                           |
  1076.  |    var2                                                                   |
  1077.  |             16 bit short signed integer (Word16) whose value falls in the |
  1078.  |             range : 0xffff 8000 <= var1 <= 0x0000 7fff.                   |
  1079.  |                                                                           |
  1080.  |   Outputs :                                                               |
  1081.  |                                                                           |
  1082.  |    none                                                                   |
  1083.  |                                                                           |
  1084.  |   Return Value :                                                          |
  1085.  |                                                                           |
  1086.  |    L_var_out                                                              |
  1087.  |             32 bit long signed integer (Word32) whose value falls in the  |
  1088.  |             range : 0x8000 0000 <= L_var_out <= 0x7fff ffff.              |
  1089.  |___________________________________________________________________________|
  1090. */
  1091. Word32 L_shl(Word32 L_var1, Word16 var2)
  1092. {
  1093.     Word32 L_var_out = 0L;
  1094.     if (var2 <= (Word16)0) {
  1095.         L_var_out = L_shr(L_var1, (Word16)-var2);
  1096.     }
  1097.     else {
  1098.         for(; var2 > (Word16)0; var2--) {
  1099.             if (L_var1 > (Word32) 0X3fffffffL) {
  1100.                 Overflow = 1;
  1101.                 L_var_out = MAX_32;
  1102.                 break;
  1103.             }
  1104.             else {
  1105.                 if (L_var1 < (Word32) 0xc0000000L) {
  1106.                     Overflow = 1;
  1107.                     L_var_out = MIN_32;
  1108.                     break;
  1109.                 }
  1110.             }
  1111.             L_var1 *= 2L;
  1112.             L_var_out = L_var1;
  1113.         }
  1114.     }
  1115.     return(L_var_out);
  1116. }
  1117. /*___________________________________________________________________________
  1118.  |                                                                           |
  1119.  |   Function Name : L_shr                                                   |
  1120.  |                                                                           |
  1121.  |   Purpose :                                                               |
  1122.  |                                                                           |
  1123.  |   Arithmetically shift the 32 bit input L_var1 right var2 positions with  |
  1124.  |   sign extension. If var2 is negative, arithmetically shift L_var1 left   |
  1125.  |   by -var2 and zero fill the var2 LSB of the result. Saturate the result  |
  1126.  |   in case of underflows or overflows.                                     |
  1127.  |                                                                           |
  1128.  |   Complexity weight : 2                                                   |
  1129.  |                                                                           |
  1130.  |   Inputs :                                                                |
  1131.  |                                                                           |
  1132.  |    L_var1   32 bit long signed integer (Word32) whose value falls in the  |
  1133.  |             range : 0x8000 0000 <= L_var3 <= 0x7fff ffff.                 |
  1134.  |                                                                           |
  1135.  |    var2                                                                   |
  1136.  |             16 bit short signed integer (Word16) whose value falls in the |
  1137.  |             range : 0xffff 8000 <= var1 <= 0x0000 7fff.                   |
  1138.  |                                                                           |
  1139.  |   Outputs :                                                               |
  1140.  |                                                                           |
  1141.  |    none                                                                   |
  1142.  |                                                                           |
  1143.  |   Return Value :                                                          |
  1144.  |                                                                           |
  1145.  |    L_var_out                                                              |
  1146.  |             32 bit long signed integer (Word32) whose value falls in the  |
  1147.  |             range : 0x8000 0000 <= L_var_out <= 0x7fff ffff.              |
  1148.  |___________________________________________________________________________|
  1149. */
  1150. Word32 L_shr(Word32 L_var1, Word16 var2)
  1151. {
  1152.     Word32 L_var_out;
  1153.     if (var2 < (Word16)0) {
  1154.         L_var_out = L_shl(L_var1, (Word16)-var2);
  1155.     }
  1156.     else {
  1157.         if (var2 >= (Word16)31) {
  1158.             L_var_out = (L_var1 < 0L) ? -1L : 0L;
  1159.         }
  1160.         else {
  1161.             if (L_var1< 0L)  {
  1162.                 L_var_out = ~((~L_var1) >> var2);
  1163.             }
  1164.             else {
  1165.                 L_var_out = L_var1 >> var2;
  1166.             }
  1167.         }
  1168.     }
  1169.     return(L_var_out);
  1170. }
  1171. /*___________________________________________________________________________
  1172.  |                                                                           |
  1173.  |   Function Name : shr_r                                                   |
  1174.  |                                                                           |
  1175.  |   Purpose :                                                               |
  1176.  |                                                                           |
  1177.  |   Same as shr(var1,var2) but with rounding. Saturate the result in case of|
  1178.  |   underflows or overflows :                                               |
  1179.  |    If var2 is greater than zero :                                         |
  1180.  |       shr_r(var1,var2) = shr(add(var1,2**(var2-1)),var2)                  |
  1181.  |    If var2 is less than zero :                                            |
  1182.  |       shr_r(var1,var2) = shr(var1,var2).                                  |
  1183.  |                                                                           |
  1184.  |   Complexity weight : 2                                                   |
  1185.  |                                                                           |
  1186.  |   Inputs :                                                                |
  1187.  |                                                                           |
  1188.  |    var1                                                                   |
  1189.  |             16 bit short signed integer (Word16) whose value falls in the |
  1190.  |             range : 0xffff 8000 <= var1 <= 0x0000 7fff.                   |
  1191.  |                                                                           |
  1192.  |    var2                                                                   |
  1193.  |             16 bit short signed integer (Word16) whose value falls in the |
  1194.  |             range : 0xffff 8000 <= var1 <= 0x0000 7fff.                   |
  1195.  |                                                                           |
  1196.  |   Outputs :                                                               |
  1197.  |                                                                           |
  1198.  |    none                                                                   |
  1199.  |                                                                           |
  1200.  |   Return Value :                                                          |
  1201.  |                                                                           |
  1202.  |    var_out                                                                |
  1203.  |             16 bit short signed integer (Word16) whose value falls in the |
  1204.  |             range : 0xffff 8000 <= var_out <= 0x0000 7fff.                |
  1205.  |___________________________________________________________________________|
  1206. */
  1207. Word16 shr_r(Word16 var1, Word16 var2)
  1208. {
  1209.     Word16 var_out;
  1210.     if (var2 > (Word16)15) {
  1211.         var_out = (Word16)0;
  1212.     }
  1213.     else {
  1214.         var_out = shr(var1,var2);
  1215.         if (var2 > (Word16)0) {
  1216.             if ((var1 & ((Word16)1 << (var2-(Word16)1))) != (Word16)0) {
  1217.                 var_out++;
  1218.             }
  1219.         }
  1220.     }
  1221.     return(var_out);
  1222. }
  1223. /*___________________________________________________________________________
  1224.  |                                                                           |
  1225.  |   Function Name : mac_r                                                   |
  1226.  |                                                                           |
  1227.  |   Purpose :                                                               |
  1228.  |                                                                           |
  1229.  |   Multiply var1 by var2 and shift the result left by 1. Add the 32 bit    |
  1230.  |   result to L_var3 with saturation. Round the LS 16 bits of the result    |
  1231.  |   into the MS 16 bits with saturation and shift the result right by 16.   |
  1232.  |   Return a 16 bit result.                                                 |
  1233.  |            mac_r(L_var3,var1,var2) = round(L_mac(Lvar3,var1,var2))        |
  1234.  |                                                                           |
  1235.  |   Complexity weight : 2                                                   |
  1236.  |                                                                           |
  1237.  |   Inputs :                                                                |
  1238.  |                                                                           |
  1239.  |    L_var3   32 bit long signed integer (Word32) whose value falls in the  |
  1240.  |             range : 0x8000 0000 <= L_var3 <= 0x7fff ffff.                 |
  1241.  |                                                                           |
  1242.  |    var1                                                                   |
  1243.  |             16 bit short signed integer (Word16) whose value falls in the |
  1244.  |             range : 0xffff 8000 <= var1 <= 0x0000 7fff.                   |
  1245.  |                                                                           |
  1246.  |    var2                                                                   |
  1247.  |             16 bit short signed integer (Word16) whose value falls in the |
  1248.  |             range : 0xffff 8000 <= var1 <= 0x0000 7fff.                   |
  1249.  |                                                                           |
  1250.  |   Outputs :                                                               |
  1251.  |                                                                           |
  1252.  |    none                                                                   |
  1253.  |                                                                           |
  1254.  |   Return Value :                                                          |
  1255.  |                                                                           |
  1256.  |    var_out                                                                |
  1257.  |             16 bit short signed integer (Word16) whose value falls in the |
  1258.  |             range : 0x0000 8000 <= L_var_out <= 0x0000 7fff.              |
  1259.  |___________________________________________________________________________|
  1260. */
  1261. Word16 mac_r(Word32 L_var3, Word16 var1, Word16 var2)
  1262. {
  1263.     Word16 var_out;
  1264.     L_var3 = L_mac(L_var3, var1, var2);
  1265.     L_var3 = L_add(L_var3, (Word32) 0x00008000L);
  1266.     var_out = extract_h(L_var3);
  1267.     return(var_out);
  1268. }
  1269. /*___________________________________________________________________________
  1270.  |                                                                           |
  1271.  |   Function Name : msu_r                                                   |
  1272.  |                                                                           |
  1273.  |   Purpose :                                                               |
  1274.  |                                                                           |
  1275.  |   Multiply var1 by var2 and shift the result left by 1. Subtract the 32   |
  1276.  |   bit result to L_var3 with saturation. Round the LS 16 bits of the res-  |
  1277.  |   ult into the MS 16 bits with saturation and shift the result right by   |
  1278.  |   16. Return a 16 bit result.                                             |
  1279.  |            msu_r(L_var3,var1,var2) = round(L_msu(Lvar3,var1,var2))        |
  1280.  |                                                                           |
  1281.  |   Complexity weight : 2                                                   |
  1282.  |                                                                           |
  1283.  |   Inputs :                                                                |
  1284.  |                                                                           |
  1285.  |    L_var3   32 bit long signed integer (Word32) whose value falls in the  |
  1286.  |             range : 0x8000 0000 <= L_var3 <= 0x7fff ffff.                 |
  1287.  |                                                                           |
  1288.  |    var1                                                                   |
  1289.  |             16 bit short signed integer (Word16) whose value falls in the |
  1290.  |             range : 0xffff 8000 <= var1 <= 0x0000 7fff.                   |
  1291.  |                                                                           |
  1292.  |    var2                                                                   |
  1293.  |             16 bit short signed integer (Word16) whose value falls in the |
  1294.  |             range : 0xffff 8000 <= var1 <= 0x0000 7fff.                   |
  1295.  |                                                                           |
  1296.  |   Outputs :                                                               |
  1297.  |                                                                           |
  1298.  |    none                                                                   |
  1299.  |                                                                           |
  1300.  |   Return Value :                                                          |
  1301.  |                                                                           |
  1302.  |    var_out                                                                |
  1303.  |             16 bit short signed integer (Word16) whose value falls in the |
  1304.  |             range : 0x0000 8000 <= L_var_out <= 0x0000 7fff.              |
  1305.  |___________________________________________________________________________|
  1306. */
  1307. Word16 msu_r(Word32 L_var3, Word16 var1, Word16 var2)
  1308. {
  1309.     Word16 var_out;
  1310.     L_var3 = L_msu(L_var3,var1,var2);
  1311.     L_var3 = L_add(L_var3, (Word32) 0x00008000L);
  1312.     var_out = extract_h(L_var3);
  1313.     return(var_out);
  1314. }
  1315. /*___________________________________________________________________________
  1316.  |                                                                           |
  1317.  |   Function Name : L_deposit_h                                             |
  1318.  |                                                                           |
  1319.  |   Purpose :                                                               |
  1320.  |                                                                           |
  1321.  |   Deposit the 16 bit var1 into the 16 MS bits of the 32 bit output. The   |
  1322.  |   16 LS bits of the output are zeroed.                                    |
  1323.  |                                                                           |
  1324.  |   Complexity weight : 2                                                   |
  1325.  |                                                                           |
  1326.  |   Inputs :                                                                |
  1327.  |                                                                           |
  1328.  |    var1                                                                   |
  1329.  |             16 bit short signed integer (Word16) whose value falls in the |
  1330.  |             range : 0xffff 8000 <= var1 <= 0x0000 7fff.                   |
  1331.  |                                                                           |
  1332.  |   Outputs :                                                               |
  1333.  |                                                                           |
  1334.  |    none                                                                   |
  1335.  |                                                                           |
  1336.  |   Return Value :                                                          |
  1337.  |                                                                           |
  1338.  |    L_var_out                                                              |
  1339.  |             32 bit long signed integer (Word32) whose value falls in the  |
  1340.  |             range : 0x8000 0000 <= var_out <= 0x7fff 0000.                |
  1341.  |___________________________________________________________________________|
  1342. */
  1343. Word32 L_deposit_h(Word16 var1)
  1344. {
  1345.     Word32 L_var_out;
  1346.     L_var_out = (Word32) var1 << 16;
  1347.     return(L_var_out);
  1348. }
  1349. /*___________________________________________________________________________
  1350.  |                                                                           |
  1351.  |   Function Name : L_deposit_l                                             |
  1352.  |                                                                           |
  1353.  |   Purpose :                                                               |
  1354.  |                                                                           |
  1355.  |   Deposit the 16 bit var1 into the 16 LS bits of the 32 bit output. The   |
  1356.  |   16 MS bits of the output are sign extended.                             |
  1357.  |                                                                           |
  1358.  |   Complexity weight : 2                                                   |
  1359.  |                                                                           |
  1360.  |   Inputs :                                                                |
  1361.  |                                                                           |
  1362.  |    var1                                                                   |
  1363.  |             16 bit short signed integer (Word16) whose value falls in the |
  1364.  |             range : 0xffff 8000 <= var1 <= 0x0000 7fff.                   |
  1365.  |                                                                           |
  1366.  |   Outputs :                                                               |
  1367.  |                                                                           |
  1368.  |    none                                                                   |
  1369.  |                                                                           |
  1370.  |   Return Value :                                                          |
  1371.  |                                                                           |
  1372.  |    L_var_out                                                              |
  1373.  |             32 bit long signed integer (Word32) whose value falls in the  |
  1374.  |             range : 0xFFFF 8000 <= var_out <= 0x0000 7fff.                |
  1375.  |___________________________________________________________________________|
  1376. */
  1377. Word32 L_deposit_l(Word16 var1)
  1378. {
  1379.     Word32 L_var_out;
  1380.     L_var_out = (Word32) var1;
  1381.     return(L_var_out);
  1382. }
  1383. /*___________________________________________________________________________
  1384.  |                                                                           |
  1385.  |   Function Name : L_shr_r                                                 |
  1386.  |                                                                           |
  1387.  |   Purpose :                                                               |
  1388.  |                                                                           |
  1389.  |   Same as L_shr(L_var1,var2)but with rounding. Saturate the result in case|
  1390.  |   of underflows or overflows :                                            |
  1391.  |    If var2 is greater than zero :                                         |
  1392.  |       L_shr_r(var1,var2) = L_shr(L_add(L_var1,2**(var2-1)),var2)          |
  1393.  |    If var2 is less than zero :                                            |
  1394.  |       L_shr_r(var1,var2) = L_shr(L_var1,var2).                            |
  1395.  |                                                                           |
  1396.  |   Complexity weight : 3                                                   |
  1397.  |                                                                           |
  1398.  |   Inputs :                                                                |
  1399.  |                                                                           |
  1400.  |    L_var1                                                                 |
  1401.  |             32 bit long signed integer (Word32) whose value falls in the  |
  1402.  |             range : 0x8000 0000 <= var1 <= 0x7fff ffff.                   |
  1403.  |                                                                           |
  1404.  |    var2                                                                   |
  1405.  |             16 bit short signed integer (Word16) whose value falls in the |
  1406.  |             range : 0xffff 8000 <= var1 <= 0x0000 7fff.                   |
  1407.  |                                                                           |
  1408.  |   Outputs :                                                               |
  1409.  |                                                                           |
  1410.  |    none                                                                   |
  1411.  |                                                                           |
  1412.  |   Return Value :                                                          |
  1413.  |                                                                           |
  1414.  |    L_var_out                                                              |
  1415.  |             32 bit long signed integer (Word32) whose value falls in the  |
  1416.  |             range : 0x8000 0000 <= var_out <= 0x7fff ffff.                |
  1417.  |___________________________________________________________________________|
  1418. */
  1419. Word32 L_shr_r(Word32 L_var1,Word16 var2)
  1420. {
  1421.     Word32 L_var_out;
  1422.     if (var2 > (Word16)31) {
  1423.         L_var_out = 0L;
  1424.     }
  1425.     else {
  1426.         L_var_out = L_shr(L_var1,var2);
  1427.         if (var2 > (Word16)0) {
  1428.             if ( (L_var1 & ( 1L << (Word16)(var2-(Word16)1) )) != 0L)  {
  1429.                 L_var_out++;
  1430.             }
  1431.         }
  1432.     }
  1433.     return(L_var_out);
  1434. }
  1435. /*___________________________________________________________________________
  1436.  |                                                                           |
  1437.  |   Function Name : L_abs                                                   |
  1438.  |                                                                           |
  1439.  |   Purpose :                                                               |
  1440.  |                                                                           |
  1441.  |    Absolute value of L_var1; Saturate in case where the input is          |
  1442.  |                                                               -214783648  |
  1443.  |                                                                           |
  1444.  |   Complexity weight : 3                                                   |
  1445.  |                                                                           |
  1446.  |   Inputs :                                                                |
  1447.  |                                                                           |
  1448.  |    L_var1                                                                 |
  1449.  |             32 bit long signed integer (Word32) whose value falls in the  |
  1450.  |             range : 0x8000 0000 <= var1 <= 0x7fff ffff.                   |
  1451.  |                                                                           |
  1452.  |   Outputs :                                                               |
  1453.  |                                                                           |
  1454.  |    none                                                                   |
  1455.  |                                                                           |
  1456.  |   Return Value :                                                          |
  1457.  |                                                                           |
  1458.  |    L_var_out                                                              |
  1459.  |             32 bit long signed integer (Word32) whose value falls in the  |
  1460.  |             range : 0x0000 0000 <= var_out <= 0x7fff ffff.                |
  1461.  |___________________________________________________________________________|
  1462. */
  1463. Word32 L_abs(Word32 L_var1)
  1464. {
  1465.     Word32 L_var_out;
  1466.     if (L_var1 == MIN_32) {
  1467.         L_var_out = MAX_32;
  1468.     }
  1469.     else {
  1470.         if (L_var1 < 0L) {
  1471.             L_var_out = -L_var1;
  1472.         }
  1473.         else {
  1474.             L_var_out = L_var1;
  1475.         }
  1476.     }
  1477.     return(L_var_out);
  1478. }
  1479. /*___________________________________________________________________________
  1480.  |                                                                           |
  1481.  |   Function Name : L_sat                                                   |
  1482.  |                                                                           |
  1483.  |   Purpose :                                                               |
  1484.  |                                                                           |
  1485.  |    32 bit L_var1 is set to 2147833647 if an overflow occured or to        |
  1486.  |    -214783648 if an underflow occured on the most recent L_add_c, L_sub_c,|
  1487.  |    L_macNs or LmsuNs operations. The carry and overflow values are binary |
  1488.  |    values which can be tested and assigned values.                        |
  1489.  |                                                                           |
  1490.  |   Complexity weight : 4                                                   |
  1491.  |                                                                           |
  1492.  |   Inputs :                                                                |
  1493.  |                                                                           |
  1494.  |    L_var1                                                                 |
  1495.  |             32 bit long signed integer (Word32) whose value falls in the  |
  1496.  |             range : 0x8000 0000 <= var1 <= 0x7fff ffff.                   |
  1497.  |                                                                           |
  1498.  |   Outputs :                                                               |
  1499.  |                                                                           |
  1500.  |    none                                                                   |
  1501.  |                                                                           |
  1502.  |   Return Value :                                                          |
  1503.  |                                                                           |
  1504.  |    L_var_out                                                              |
  1505.  |             32 bit long signed integer (Word32) whose value falls in the  |
  1506.  |             range : 0x8000 0000 <= var_out <= 0x7fff ffff.                |
  1507.  |___________________________________________________________________________|
  1508. */
  1509. Word32 L_sat (Word32 L_var1)
  1510. {
  1511.     Word32 L_var_out;
  1512.     L_var_out = L_var1;
  1513.     if (Overflow) {
  1514.         if (Carry) {
  1515.             L_var_out = MIN_32;
  1516.         }
  1517.         else {
  1518.             L_var_out = MAX_32;
  1519.         }
  1520.         Carry = 0;
  1521.         Overflow = 0;
  1522.     }
  1523.     return(L_var_out);
  1524. }
  1525. /*___________________________________________________________________________
  1526.  |                                                                           |
  1527.  |   Function Name : norm_s                                                  |
  1528.  |                                                                           |
  1529.  |   Purpose :                                                               |
  1530.  |                                                                           |
  1531.  |   Produces the number of left shift needed to normalize the 16 bit varia- |
  1532.  |   ble var1 for positive values on the interval with minimum of 16384 and  |
  1533.  |   maximum of 32767, and for negative values on the interval with minimum  |
  1534.  |   of -32768 and maximum of -16384; in order to normalize the result, the  |
  1535.  |   following operation must be done :                                      |
  1536.  |                    norm_var1 = shl(var1,norm_s(var1)).                    |
  1537.  |                                                                           |
  1538.  |   Complexity weight : 15                                                  |
  1539.  |                                                                           |
  1540.  |   Inputs :                                                                |
  1541.  |                                                                           |
  1542.  |    var1                                                                   |
  1543.  |             16 bit short signed integer (Word16) whose value falls in the |
  1544.  |             range : 0xffff 8000 <= var1 <= 0x0000 7fff.                   |
  1545.  |                                                                           |
  1546.  |   Outputs :                                                               |
  1547.  |                                                                           |
  1548.  |    none                                                                   |
  1549.  |                                                                           |
  1550.  |   Return Value :                                                          |
  1551.  |                                                                           |
  1552.  |    var_out                                                                |
  1553.  |             16 bit short signed integer (Word16) whose value falls in the |
  1554.  |             range : 0x0000 0000 <= var_out <= 0x0000 000f.                |
  1555.  |___________________________________________________________________________|
  1556. */
  1557. Word16 norm_s(Word16 var1)
  1558. {
  1559.     Word16 var_out;
  1560.     if (var1 == (Word16)0) {
  1561.         var_out = (Word16)0;
  1562.     }
  1563.     else {
  1564.         if (var1 == (Word16) 0xffff) {
  1565.             var_out = (Word16)15;
  1566.         }
  1567.         else {
  1568.             if (var1 < (Word16)0) {
  1569.                 var1 = ~var1;
  1570.             }
  1571.             for(var_out = (Word16)0; var1 < (Word16)0x4000; var_out++) {
  1572.                 var1 <<= 1;
  1573.             }
  1574.         }
  1575.     }
  1576.     return(var_out);
  1577. }
  1578. /*___________________________________________________________________________
  1579.  |                                                                           |
  1580.  |   Function Name : div_s                                                   |
  1581.  |                                                                           |
  1582.  |   Purpose :                                                               |
  1583.  |                                                                           |
  1584.  |   Produces a result which is the fractional integer division of var1 by  |
  1585.  |   var2; var1 and var2 must be positive and var2 must be greater or equal  |
  1586.  |   to var1; the result is positive (leading bit equal to 0) and truncated  |
  1587.  |   to 16 bits.                                                             |
  1588.  |   If var1 = var2 then div(var1,var2) = 32767.                             |
  1589.  |                                                                           |
  1590.  |   Complexity weight : 18                                                  |
  1591.  |                                                                           |
  1592.  |   Inputs :                                                                |
  1593.  |                                                                           |
  1594.  |    var1                                                                   |
  1595.  |             16 bit short signed integer (Word16) whose value falls in the |
  1596.  |             range : 0x0000 0000 <= var1 <= var2 and var2 != 0.            |
  1597.  |                                                                           |
  1598.  |    var2                                                                   |
  1599.  |             16 bit short signed integer (Word16) whose value falls in the |
  1600.  |             range : var1 <= var2 <= 0x0000 7fff and var2 != 0.            |
  1601.  |                                                                           |
  1602.  |   Outputs :                                                               |
  1603.  |                                                                           |
  1604.  |    none                                                                   |
  1605.  |                                                                           |
  1606.  |   Return Value :                                                          |
  1607.  |                                                                           |
  1608.  |    var_out                                                                |
  1609.  |             16 bit short signed integer (Word16) whose value falls in the |
  1610.  |             range : 0x0000 0000 <= var_out <= 0x0000 7fff.                |
  1611.  |             It's a Q15 value (point between b15 and b14).                 |
  1612.  |___________________________________________________________________________|
  1613. */
  1614. Word16 div_s(Word16 var1, Word16 var2)
  1615. {
  1616.     Word16 var_out = (Word16)0;
  1617.     Word16 iteration;
  1618.     Word32 L_num;
  1619.     Word32 L_denom;
  1620.     if ((var1 > var2) || (var1 < (Word16)0) || (var2 < (Word16)0)) {
  1621.         printf("Division Errorn");
  1622.         exit(0);
  1623.     }
  1624.     if (var2 == (Word16)0) {
  1625.         printf("Division by 0, Fatal error n");
  1626.         exit(0);
  1627.     }
  1628.     if (var1 == (Word16)0) {
  1629.         var_out = (Word16)0;
  1630.     }
  1631.     else {
  1632.         if (var1 == var2) {
  1633.             var_out = MAX_16;
  1634.         }
  1635.         else {
  1636.             L_num = L_deposit_l(var1);
  1637.             L_denom = L_deposit_l(var2);
  1638.             for(iteration=(Word16)0; iteration< (Word16)15;iteration++) {
  1639.                 var_out <<=1;
  1640.                 L_num <<= 1;
  1641.                 if (L_num >= L_denom) {
  1642.                     L_num = L_sub(L_num,L_denom);
  1643.                     var_out = add(var_out, (Word16)1);
  1644.                 }
  1645.             }
  1646.         }
  1647.     }
  1648.     return(var_out);
  1649. }
  1650. /*___________________________________________________________________________
  1651.  |                                                                           |
  1652.  |   Function Name : norm_l                                                  |
  1653.  |                                                                           |
  1654.  |   Purpose :                                                               |
  1655.  |                                                                           |
  1656.  |   Produces the number of left shift needed to normalize the 32 bit varia- |
  1657.  |   ble l_var1 for positive values on the interval with minimum of          |
  1658.  |   1073741824 and maximum of 2147483647, and for negative values on the in-|
  1659.  |   terval with minimum of -2147483648 and maximum of -1073741824; in order |
  1660.  |   to normalize the result, the following operation must be done :         |
  1661.  |                   norm_L_var1 = L_shl(L_var1,norm_l(L_var1)).             |
  1662.  |                                                                           |
  1663.  |   Complexity weight : 30                                                  |
  1664.  |                                                                           |
  1665.  |   Inputs :                                                                |
  1666.  |                                                                           |
  1667.  |    L_var1                                                                 |
  1668.  |             32 bit long signed integer (Word32) whose value falls in the  |
  1669.  |             range : 0x8000 0000 <= var1 <= 0x7fff ffff.                   |
  1670.  |                                                                           |
  1671.  |   Outputs :                                                               |
  1672.  |                                                                           |
  1673.  |    none                                                                   |
  1674.  |                                                                           |
  1675.  |   Return Value :                                                          |
  1676.  |                                                                           |
  1677.  |    var_out                                                                |
  1678.  |             16 bit short signed integer (Word16) whose value falls in the |
  1679.  |             range : 0x0000 0000 <= var_out <= 0x0000 001f.                |
  1680.  |___________________________________________________________________________|
  1681. */
  1682. Word16 norm_l(Word32 L_var1)
  1683. {
  1684.     Word16 var_out;
  1685.     if (L_var1 == 0L) {
  1686.         var_out = (Word16)0;
  1687.     }
  1688.     else {
  1689.         if (L_var1 == (Word32)0xffffffffL) {
  1690.             var_out = (Word16)31;
  1691.         }
  1692.         else {
  1693.             if (L_var1 < 0L) {
  1694.                 L_var1 = ~L_var1;
  1695.             }
  1696.             for(var_out = (Word16)0;L_var1 < (Word32)0x40000000L;var_out++) {
  1697.                 L_var1 <<= 1L;
  1698.             }
  1699.         }
  1700.     }
  1701.     return(var_out);
  1702. }
  1703. /*
  1704.    Additional operators
  1705. */
  1706. Word32   L_mls( Word32 Lv, Word16 v )
  1707. {
  1708.    Word32   Temp  ;
  1709.    Temp = Lv & (Word32) 0x0000ffff ;
  1710.    Temp = Temp * (Word32) v ;
  1711.    Temp = L_shr( Temp, (Word16) 15 ) ;
  1712.    Temp = L_mac( Temp, v, extract_h(Lv) ) ;
  1713.    return Temp ;
  1714. }
  1715. /*__________________________________________________________________________
  1716. |                                                                           |
  1717. |   Function Name : div_l                                                   |
  1718. |                                                                           |
  1719. |   Purpose :                                                               |
  1720. |                                                                           |
  1721. |   Produces a result which is the fractional integer division of L_var1 by|
  1722. |   var2; L_var1 and var2 must be positive and var2 << 16 must be greater or|
  1723. |   equal to L_var1; the result is positive (leading bit equal to 0) and    |
  1724. |   truncated to 16 bits.                                                   |
  1725. |   If L_var1 == var2 << 16 then div_l(L_var1,var2) = 32767.                |
  1726. |                                                                           |
  1727. |   Complexity weight : 20                                                  |
  1728. |                                                                           |
  1729. |   Inputs :                                                                |
  1730. |                                                                           |
  1731. |    L_var1                                                                 |
  1732. |             32 bit long signed integer (Word32) whose value falls in the  |
  1733. |             range : 0x0000 0000 <= var1 <= (var2 << 16)  and var2 != 0.   |
  1734. |             L_var1 must be considered as a Q.31 value                     |
  1735. |                                                                           |
  1736. |    var2                                                                   |
  1737. |             16 bit short signed integer (Word16) whose value falls in the |
  1738. |             range : var1 <= (var2<< 16) <= 0x7fff0000 and var2 != 0.      |
  1739. |             var2 must be considered as a Q.15 value                       |
  1740. |                                                                           |
  1741. |   Outputs :                                                               |
  1742. |                                                                           |
  1743. |    none                                                                   |
  1744. |                                                                           |
  1745. |   Return Value :                                                          |
  1746. |                                                                           |
  1747. |    var_out                                                                |
  1748. |             16 bit short signed integer (Word16) whose value falls in the |
  1749. |             range : 0x0000 0000 <= var_out <= 0x0000 7fff.                |
  1750. |             It's a Q15 value (point between b15 and b14).                 |
  1751. |___________________________________________________________________________|
  1752. */
  1753. Word16 div_l( Word32  L_num, Word16 den ){
  1754.     Word16   var_out = (Word16)0;
  1755.     Word32   L_den;
  1756.     Word16   iteration;
  1757.     if ( den == (Word16) 0 ) {
  1758.         printf("Division by 0 in div_l, Fatal error n");
  1759.         exit(0);
  1760.     }
  1761.     if ( (L_num < (Word32) 0) || (den < (Word16) 0) ) {
  1762.         printf("Division Error in div_l, Fatal error n");
  1763.         exit(0);
  1764.     }
  1765.     L_den = L_deposit_h( den ) ;
  1766.     if ( L_num >= L_den ){
  1767.         return MAX_16 ;
  1768.     }
  1769.     else {
  1770.         L_num = L_shr(L_num, (Word16)1) ;
  1771.         L_den = L_shr(L_den, (Word16)1);
  1772.         for(iteration=(Word16)0; iteration< (Word16)15;iteration++) {
  1773.             var_out = shl( var_out, (Word16)1);
  1774.             L_num   = L_shl( L_num, (Word16)1);
  1775.             if (L_num >= L_den) {
  1776.                 L_num = L_sub(L_num,L_den);
  1777.                 var_out = add(var_out, (Word16)1);
  1778.             }
  1779.         }
  1780.         return var_out;
  1781.     }
  1782. }
  1783. /*---------------------------------------------------------------------------*
  1784.  *  Function  i_mult()                                                        *
  1785.  *  ~~~~~~~~~~~~~~~~~                                                        *
  1786.  * Integer multiplication.                                                   *
  1787.  *--------------------------------------------------------------------------*/
  1788. Word16 i_mult(Word16 a, Word16 b)
  1789. {
  1790.    return  a*b;
  1791. }