1 /* ------------------------------------------------------------------
2 * Copyright (C) 1998-2009 PacketVideo
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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
14 * See the License for the specific language governing permissions
15 * and limitations under the License.
16 * -------------------------------------------------------------------
18 /****************************************************************************************
19 Portions of this file are derived from the following 3GPP standard:
22 ANSI-C code for the Adaptive Multi-Rate (AMR) speech codec
23 Available from http://www.3gpp.org
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 ****************************************************************************************/
33 ------------------------------------------------------------------------------
36 Subtraction function with overflow control
38 ------------------------------------------------------------------------------
41 /*----------------------------------------------------------------------------
43 ----------------------------------------------------------------------------*/
46 /*----------------------------------------------------------------------------
48 ; [Define module specific macros here]
49 ----------------------------------------------------------------------------*/
51 /*----------------------------------------------------------------------------
53 ; [Include all pre-processor statements here. Include conditional
54 ; compile variables also.]
55 ----------------------------------------------------------------------------*/
57 /*----------------------------------------------------------------------------
58 ; LOCAL FUNCTION DEFINITIONS
59 ; [List function prototypes here]
60 ----------------------------------------------------------------------------*/
62 /*----------------------------------------------------------------------------
63 ; LOCAL VARIABLE DEFINITIONS
64 ; [Variable declaration - defined here and used outside this module]
65 ----------------------------------------------------------------------------*/
69 ------------------------------------------------------------------------------
71 ------------------------------------------------------------------------------
72 INPUT AND OUTPUT DEFINITIONS
75 var1 = 16 bit short signed integer (Word16) whose value falls in
76 the range : 0xffff 8000 <= var1 <= 0x0000 7fff.
78 var2 = 16 bit short signed integer (Word16) whose value falls in
79 the range : 0xffff 8000 <= var2 <= 0x0000 7fff.
81 pOverflow = pointer to overflow (Flag)
84 pOverflow -> 1 if the subtract operation resulted in overflow
87 diff = 16-bit limited difference between var1 and var2 (Word16)
89 Global Variables Used:
92 Local Variables Needed:
95 ------------------------------------------------------------------------------
98 This function performs the subtraction (var1-var2) with overflow control and
99 saturation; the 16 bit result is set at +32767 when overflow occurs or at
100 -32768 when underflow occurs.
102 ------------------------------------------------------------------------------
107 ------------------------------------------------------------------------------
110 [1] sub() function in basicop2.c, UMTS GSM AMR speech codec, R99 -
111 Version 3.2.0, March 2, 2001
113 ------------------------------------------------------------------------------
117 Word16 sub (Word16 var1, Word16 var2)
122 diff = (Word32) var1 - var2;
124 * The reference ETSI code uses a global flag for Overflow inside the function
125 * saturate(). In the actual implementation a pointer to Overflow flag is passed
126 * in as a parameter to the function
128 var_out = saturate (diff);
131 multiCounter[currCounter].sub++;
137 ------------------------------------------------------------------------------
139 [State any special notes, constraints or cautions for users of this function]
141 ------------------------------------------------------------------------------
144 /*----------------------------------------------------------------------------
146 ----------------------------------------------------------------------------*/
148 OSCL_EXPORT_REF Word16 sub(Word16 var1, Word16 var2, Flag *pOverflow)
153 diff = (Word32) var1 - var2;
155 /* Saturate result (if necessary). */
156 /* Replaced function call with in-line code */
157 /* to conserve MIPS, i.e., var_out = saturate (diff) */
160 if ((UWord32)(diff + 32768) > 0x000FFFF)
162 if (diff > (Word32) 0x0007FFFL)
175 return ((Word16) diff);