Home | History | Annotate | Download | only in src
      1 /* ------------------------------------------------------------------
      2  * Copyright (C) 1998-2009 PacketVideo
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
     13  * express or implied.
     14  * See the License for the specific language governing permissions
     15  * and limitations under the License.
     16  * -------------------------------------------------------------------
     17  */
     18 /****************************************************************************************
     19 Portions of this file are derived from the following 3GPP standard:
     20 
     21     3GPP TS 26.073
     22     ANSI-C code for the Adaptive Multi-Rate (AMR) speech codec
     23     Available from http://www.3gpp.org
     24 
     25 (C) 2004, 3GPP Organizational Partners (ARIB, ATIS, CCSA, ETSI, TTA, TTC)
     26 Permission to distribute, modify and use this file under the standard license
     27 terms listed above has been obtained from the copyright holder.
     28 ****************************************************************************************/
     29 /*
     30 
     31  Filename: /audio/gsm_amr/c/src/add.c
     32 
     33 ------------------------------------------------------------------------------
     34  REVISION HISTORY
     35 
     36  Description: Created separate file for add function. Sync'ed up with the
     37           current template and fixed tabs.
     38 
     39  Description: Changed all occurrences of L_sum with sum.
     40 
     41  Description: Changed function protype to pass in pointer to Overflow flag
     42                 as a parameter.
     43 
     44  Description: Removed code that updates MOPS counter
     45 
     46  Who:                       Date:
     47  Description:
     48 
     49 ------------------------------------------------------------------------------
     50  MODULE DESCRIPTION
     51 
     52  Summation function with overflow control
     53 
     54 ------------------------------------------------------------------------------
     55 */
     56 
     57 
     58 /*----------------------------------------------------------------------------
     59 ; INCLUDES
     60 ----------------------------------------------------------------------------*/
     61 #include    "basic_op.h"
     62 
     63 /*----------------------------------------------------------------------------
     64 ; MACROS
     65 ; [Define module specific macros here]
     66 ----------------------------------------------------------------------------*/
     67 
     68 /*----------------------------------------------------------------------------
     69 ; DEFINES
     70 ; [Include all pre-processor statements here. Include conditional
     71 ; compile variables also.]
     72 ----------------------------------------------------------------------------*/
     73 
     74 /*----------------------------------------------------------------------------
     75 ; LOCAL FUNCTION DEFINITIONS
     76 ; [List function prototypes here]
     77 ----------------------------------------------------------------------------*/
     78 
     79 /*----------------------------------------------------------------------------
     80 ; LOCAL VARIABLE DEFINITIONS
     81 ; [Variable declaration - defined here and used outside this module]
     82 ----------------------------------------------------------------------------*/
     83 
     84 
     85 /*
     86 ------------------------------------------------------------------------------
     87  FUNCTION NAME: add
     88 ------------------------------------------------------------------------------
     89  INPUT AND OUTPUT DEFINITIONS
     90 
     91  Inputs:
     92     var1 = 16 bit short signed integer (Word16) whose value falls in
     93            the range : 0xffff 8000 <= var1 <= 0x0000 7fff.
     94 
     95     var2 = 16 bit short signed integer (Word16) whose value falls in
     96            the range : 0xffff 8000 <= var2 <= 0x0000 7fff.
     97 
     98     pOverflow = pointer to overflow (Flag)
     99 
    100  Outputs:
    101     pOverflow -> 1 if the add operation resulted in overflow
    102 
    103  Returns:
    104     sum = 16-bit limited sum of var1 and var2 (Word16)
    105 
    106  Global Variables Used:
    107     None
    108 
    109  Local Variables Needed:
    110     None
    111 
    112 ------------------------------------------------------------------------------
    113  FUNCTION DESCRIPTION
    114 
    115  This function performs the addition (var1+var2) with overflow control and
    116  saturation; the 16 bit result is set at +32767 when overflow occurs or at
    117  -32768 when underflow occurs.
    118 
    119 ------------------------------------------------------------------------------
    120  REQUIREMENTS
    121 
    122  None
    123 
    124 ------------------------------------------------------------------------------
    125  REFERENCES
    126 
    127  [1] add.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
    128 
    129 ------------------------------------------------------------------------------
    130  PSEUDO-CODE
    131 
    132  Word16 add (Word16 var1, Word16 var2)
    133 {
    134     Word16 var_out;
    135     Word32 sum;
    136 
    137     sum = (Word32) var1 + var2;
    138 
    139 * The reference ETSI code uses a global flag for Overflow inside the function
    140 * saturate(). In the actual implementation a pointer to Overflow flag is passed in
    141 * as a parameter to the function
    142 
    143     var_out = saturate (sum);
    144 #if (WMOPS)
    145     multiCounter[currCounter].add++;
    146 #endif
    147     return (var_out);
    148 }
    149 
    150 ------------------------------------------------------------------------------
    151  RESOURCES USED [optional]
    152 
    153  When the code is written for a specific target processor the
    154  the resources used should be documented below.
    155 
    156  HEAP MEMORY USED: x bytes
    157 
    158  STACK MEMORY USED: x bytes
    159 
    160  CLOCK CYCLES: (cycle count equation for this function) + (variable
    161                 used to represent cycle count for each subroutine
    162                 called)
    163      where: (cycle count variable) = cycle count for [subroutine
    164                                      name]
    165 
    166 ------------------------------------------------------------------------------
    167  CAUTION [optional]
    168  [State any special notes, constraints or cautions for users of this function]
    169 
    170 ------------------------------------------------------------------------------
    171 */
    172 
    173 /*----------------------------------------------------------------------------
    174 ; FUNCTION CODE
    175 ----------------------------------------------------------------------------*/
    176 Word16 add(Word16 var1, Word16 var2, Flag *pOverflow)
    177 {
    178     /*----------------------------------------------------------------------------
    179     ; Define all local variables
    180     ----------------------------------------------------------------------------*/
    181     Word32 sum;
    182     sum = (Word32) var1 + var2;
    183 
    184     /* Saturate result (if necessary). */
    185     /* Replaced function call with in-line code             */
    186     /* to conserve MIPS, i.e., var_out = saturate (sum)  */
    187 
    188     if (sum > 0X00007fffL)
    189     {
    190         *pOverflow = 1;
    191         sum = MAX_16;
    192     }
    193     else if (sum < (Word32) 0xffff8000L)
    194     {
    195         *pOverflow = 1;
    196         sum = MIN_16;
    197     }
    198 
    199     /* Return the sum as a 16 bit value by type casting Word32 to Word16 */
    200 
    201     return ((Word16) sum);
    202 }
    203 
    204