• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/shr.c
32 
33 ------------------------------------------------------------------------------
34  REVISION HISTORY
35 
36  Description: Created separate file for the shr function. Sync'ed up with
37           the current template and fixed tabs.
38 
39  Description: 1. Modified code by seperating var2=0 condition.
40               2. Changed Input range definitions.
41 
42  Description: Made changes based on review meeting.
43               1. Changed Overflow definition.
44               2. Removed pseudo-code.
45               3. Deleted (var2>15&&var1!=0) condition.
46               4. Moved var2>0 condition in front of var2<0 condition.
47 
48  Description: Changed the function prototype to pass in a pointer to the
49               overflow flag instead of using global data.
50 
51  Description: Made changes per formal review. Updated template.
52               Removed code that updates MOPS counter.
53               Changed parameter name from "overflow" and "pOverflow".
54               Optimized code by eliminating unnecessary typecasting.
55               Filled in the PSEUDO CODE section
56 
57  Description: Further optimized typecasting for overflow case
58 
59  Who:                       Date:
60  Description:
61 ------------------------------------------------------------------------------
62 ------------------------------------------------------------------------------
63  MODULE DESCRIPTION
64  Shift right function with overflow control
65 ------------------------------------------------------------------------------
66 */
67 
68 /*----------------------------------------------------------------------------
69 ; INCLUDES
70 ----------------------------------------------------------------------------*/
71 #include    "basic_op.h"
72 
73 /*----------------------------------------------------------------------------
74 ; MACROS
75 ; [Define module specific macros here]
76 ----------------------------------------------------------------------------*/
77 
78 /*----------------------------------------------------------------------------
79 ; DEFINES
80 ; [Include all pre-processor statements here. Include conditional
81 ; compile variables also.]
82 ----------------------------------------------------------------------------*/
83 
84 /*----------------------------------------------------------------------------
85 ; LOCAL FUNCTION DEFINITIONS
86 ; [List function prototypes here]
87 ----------------------------------------------------------------------------*/
88 
89 /*----------------------------------------------------------------------------
90 ; LOCAL VARIABLE DEFINITIONS
91 ; [Variable declaration - defined here and used outside this module]
92 ----------------------------------------------------------------------------*/
93 
94 
95 /*
96 ------------------------------------------------------------------------------
97  FUNCTION NAME: shr
98 ------------------------------------------------------------------------------
99  INPUT AND OUTPUT DEFINITIONS
100 
101  Inputs:
102     var1 = 16 bit short signed integer (Word16) whose value falls in
103            the range : 0xffff 8000 <= var1 <= 0x0000 7fff.
104 
105     var2 = 16 bit short signed integer (Word16) whose value falls in
106            the range : 0xffff 8000 <= var2 <= 0x0000 7fff.
107 
108     pOverflow = pointer to overflow (Flag)
109 
110  Outputs:
111     pOverflow -> 1 if the shift operation resulted in overflow
112 
113  Returns:
114     product = Shifted result limited to 16 bits (Word16)
115 
116  Global Variables Used:
117     None
118 
119  Local Variables Needed:
120     None
121 
122 ------------------------------------------------------------------------------
123  FUNCTION DESCRIPTION
124 
125  This function arithmetically shifts the 16 bit input var1 right var2 positions
126  with sign extension. If var2 is negative, arithmetically shift var1 left by
127  -var2 with sign extension. Saturate the result in case of underflows or
128  overflows.
129 
130 ------------------------------------------------------------------------------
131  REQUIREMENTS
132  None
133 ------------------------------------------------------------------------------
134  REFERENCES
135 
136  [1] shr() function in basic_op2.c,  UMTS GSM AMR speech codec, R99 -
137  Version 3.2.0, March 2, 2001
138 
139 ------------------------------------------------------------------------------
140  PSEUDO-CODE
141 
142 Word16 shr_std (Word16 var1, Word16 var2)
143 {
144     Word16 var_out;
145 
146     if (var2 < 0)
147     {
148         if (var2 < -16)
149             var2 = -16;
150         var_out = shl_std (var1, -var2);
151 #if (WMOPS)
152         mult_stdiCounter[currCounter].shl_std--;
153 #endif
154     }
155     else
156     {
157         if (var2 >= 15)
158         {
159             var_out = (var1 < 0) ? -1 : 0;
160         }
161         else
162         {
163             if (var1 < 0)
164             {
165                 var_out = ~((~var1) >> var2);
166             }
167             else
168             {
169                 var_out = var1 >> var2;
170             }
171         }
172     }
173 
174 #if (WMOPS)
175     mult_stdiCounter[currCounter].shr_std++;
176 #endif
177     return (var_out);
178 }
179 ------------------------------------------------------------------------------
180  RESOURCES USED [optional]
181 
182  When the code is written for a specific target processor the
183  the resources used should be documented below.
184 
185  HEAP MEMORY USED: x bytes
186 
187  STACK MEMORY USED: x bytes
188 
189  CLOCK CYCLES: (cycle count equation for this function) + (variable
190                 used to represent cycle count for each subroutine
191                 called)
192      where: (cycle count variable) = cycle count for [subroutine
193                                      name]
194 
195 ------------------------------------------------------------------------------
196  CAUTION [optional]
197  [State any special notes, constraints or cautions for users of this function]
198 
199 ------------------------------------------------------------------------------
200 */
201 
202 /*----------------------------------------------------------------------------
203 ; FUNCTION CODE
204 ----------------------------------------------------------------------------*/
shr(register Word16 var1,register Word16 var2,Flag * pOverflow)205 Word16 shr(register Word16 var1, register Word16 var2, Flag *pOverflow)
206 {
207     register Word16 result;
208     register Word32 temp_res;
209 
210     if (var2 != 0)
211     {
212         if (var2 > 0)
213         {
214             if (var2 >= 15)
215             {
216                 result = ((var1 < 0) ? -1 : 0);
217             }
218             else
219             {
220                 if (var1 < 0)
221                 {
222                     result = (~((~var1) >> var2));
223                 }
224                 else
225                 {
226                     result = (var1 >> var2);
227                 }
228             }
229         }
230         else
231         {
232             if (var2 < -16)
233             {
234                 var2 = -16;
235             }
236 
237             var2 = -var2;   /* Shift right negative is equivalent */
238             /*   to shifting left positive.       */
239 
240             temp_res = ((Word32) var1) << var2;
241             result = (Word16)(temp_res);
242 
243             if (temp_res != (Word32) result)
244             {
245                 *pOverflow = 1;
246                 result = ((var1 > 0) ? MAX_16 : MIN_16);
247             }
248         }
249 
250     }
251     else
252     {
253         result = var1;
254     }
255 
256     return (result);
257 }
258 
259