BASIC_OP.C
Upload User: meifeng08
Upload Date: 2013-06-18
Package Size: 5304k
Code Size: 101k
Category:

Voice Compress

Development Platform:

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