Git init
[external/opencore-amr.git] / opencore / codecs_v2 / audio / gsm_amr / amr_nb / common / src / add.cpp
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: add.cpp
32
33 ------------------------------------------------------------------------------
34  MODULE DESCRIPTION
35
36  Summation function with overflow control
37
38 ------------------------------------------------------------------------------
39 */
40
41
42 /*----------------------------------------------------------------------------
43 ; INCLUDES
44 ----------------------------------------------------------------------------*/
45 #include    "basic_op.h"
46
47 /*----------------------------------------------------------------------------
48 ; MACROS
49 ; [Define module specific macros here]
50 ----------------------------------------------------------------------------*/
51
52 /*----------------------------------------------------------------------------
53 ; DEFINES
54 ; [Include all pre-processor statements here. Include conditional
55 ; compile variables also.]
56 ----------------------------------------------------------------------------*/
57
58 /*----------------------------------------------------------------------------
59 ; LOCAL FUNCTION DEFINITIONS
60 ; [List function prototypes here]
61 ----------------------------------------------------------------------------*/
62
63 /*----------------------------------------------------------------------------
64 ; LOCAL VARIABLE DEFINITIONS
65 ; [Variable declaration - defined here and used outside this module]
66 ----------------------------------------------------------------------------*/
67
68
69 /*
70 ------------------------------------------------------------------------------
71  FUNCTION NAME: add
72 ------------------------------------------------------------------------------
73  INPUT AND OUTPUT DEFINITIONS
74
75  Inputs:
76     var1 = 16 bit short signed integer (Word16) whose value falls in
77            the range : 0xffff 8000 <= var1 <= 0x0000 7fff.
78
79     var2 = 16 bit short signed integer (Word16) whose value falls in
80            the range : 0xffff 8000 <= var2 <= 0x0000 7fff.
81
82     pOverflow = pointer to overflow (Flag)
83
84  Outputs:
85     pOverflow -> 1 if the add operation resulted in overflow
86
87  Returns:
88     sum = 16-bit limited sum of var1 and var2 (Word16)
89
90  Global Variables Used:
91     None
92
93  Local Variables Needed:
94     None
95
96 ------------------------------------------------------------------------------
97  FUNCTION DESCRIPTION
98
99  This function performs the addition (var1+var2) with overflow control and
100  saturation; the 16 bit result is set at +32767 when overflow occurs or at
101  -32768 when underflow occurs.
102
103 ------------------------------------------------------------------------------
104  REQUIREMENTS
105
106  None
107
108 ------------------------------------------------------------------------------
109  REFERENCES
110
111  [1] add.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
112
113 ------------------------------------------------------------------------------
114  PSEUDO-CODE
115
116  Word16 add (Word16 var1, Word16 var2)
117 {
118     Word16 var_out;
119     Word32 sum;
120
121     sum = (Word32) var1 + var2;
122
123 * The reference ETSI code uses a global flag for Overflow inside the function
124 * saturate(). In the actual implementation a pointer to Overflow flag is passed in
125 * as a parameter to the function
126
127     var_out = saturate (sum);
128 #if (WMOPS)
129     multiCounter[currCounter].add++;
130 #endif
131     return (var_out);
132 }
133
134 ------------------------------------------------------------------------------
135  CAUTION [optional]
136  [State any special notes, constraints or cautions for users of this function]
137
138 ------------------------------------------------------------------------------
139 */
140
141 /*----------------------------------------------------------------------------
142 ; FUNCTION CODE
143 ----------------------------------------------------------------------------*/
144 OSCL_EXPORT_REF Word16 add_16(Word16 var1, Word16 var2, Flag *pOverflow)
145 {
146     /*----------------------------------------------------------------------------
147     ; Define all local variables
148     ----------------------------------------------------------------------------*/
149     Word32 sum;
150     sum = (Word32) var1 + var2;
151
152     /* Saturate result (if necessary). */
153     /* Replaced function call with in-line code             */
154     /* to conserve MIPS, i.e., var_out = saturate (sum)  */
155
156     if (sum > 0X00007fffL)
157     {
158         *pOverflow = 1;
159         sum = MAX_16;
160     }
161     else if (sum < (Word32) 0xffff8000L)
162     {
163         *pOverflow = 1;
164         sum = MIN_16;
165     }
166
167     /* Return the sum as a 16 bit value by type casting Word32 to Word16 */
168
169     return ((Word16) sum);
170 }
171