Initialize Tizen 2.3
[external/opencore-amr.git] / opencore / codecs_v2 / audio / gsm_amr / amr_nb / common / src / shr.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: shr.cpp
32
33 ------------------------------------------------------------------------------
34  MODULE DESCRIPTION
35  Shift right function with overflow control
36 ------------------------------------------------------------------------------
37 */
38
39 /*----------------------------------------------------------------------------
40 ; INCLUDES
41 ----------------------------------------------------------------------------*/
42 #include    "basic_op.h"
43
44 /*----------------------------------------------------------------------------
45 ; MACROS
46 ; [Define module specific macros here]
47 ----------------------------------------------------------------------------*/
48
49 /*----------------------------------------------------------------------------
50 ; DEFINES
51 ; [Include all pre-processor statements here. Include conditional
52 ; compile variables also.]
53 ----------------------------------------------------------------------------*/
54
55 /*----------------------------------------------------------------------------
56 ; LOCAL FUNCTION DEFINITIONS
57 ; [List function prototypes here]
58 ----------------------------------------------------------------------------*/
59
60 /*----------------------------------------------------------------------------
61 ; LOCAL VARIABLE DEFINITIONS
62 ; [Variable declaration - defined here and used outside this module]
63 ----------------------------------------------------------------------------*/
64
65
66 /*
67 ------------------------------------------------------------------------------
68  FUNCTION NAME: shr
69 ------------------------------------------------------------------------------
70  INPUT AND OUTPUT DEFINITIONS
71
72  Inputs:
73     var1 = 16 bit short signed integer (Word16) whose value falls in
74            the range : 0xffff 8000 <= var1 <= 0x0000 7fff.
75
76     var2 = 16 bit short signed integer (Word16) whose value falls in
77            the range : 0xffff 8000 <= var2 <= 0x0000 7fff.
78
79     pOverflow = pointer to overflow (Flag)
80
81  Outputs:
82     pOverflow -> 1 if the shift operation resulted in overflow
83
84  Returns:
85     product = Shifted result limited to 16 bits (Word16)
86
87  Global Variables Used:
88     None
89
90  Local Variables Needed:
91     None
92
93 ------------------------------------------------------------------------------
94  FUNCTION DESCRIPTION
95
96  This function arithmetically shifts the 16 bit input var1 right var2 positions
97  with sign extension. If var2 is negative, arithmetically shift var1 left by
98  -var2 with sign extension. Saturate the result in case of underflows or
99  overflows.
100
101 ------------------------------------------------------------------------------
102  REQUIREMENTS
103  None
104 ------------------------------------------------------------------------------
105  REFERENCES
106
107  [1] shr() function in basic_op2.c,  UMTS GSM AMR speech codec, R99 -
108  Version 3.2.0, March 2, 2001
109
110 ------------------------------------------------------------------------------
111  PSEUDO-CODE
112
113 Word16 shr_std (Word16 var1, Word16 var2)
114 {
115     Word16 var_out;
116
117     if (var2 < 0)
118     {
119         if (var2 < -16)
120             var2 = -16;
121         var_out = shl_std (var1, -var2);
122 #if (WMOPS)
123         mult_stdiCounter[currCounter].shl_std--;
124 #endif
125     }
126     else
127     {
128         if (var2 >= 15)
129         {
130             var_out = (var1 < 0) ? -1 : 0;
131         }
132         else
133         {
134             if (var1 < 0)
135             {
136                 var_out = ~((~var1) >> var2);
137             }
138             else
139             {
140                 var_out = var1 >> var2;
141             }
142         }
143     }
144
145 #if (WMOPS)
146     mult_stdiCounter[currCounter].shr_std++;
147 #endif
148     return (var_out);
149 }
150 ------------------------------------------------------------------------------
151  CAUTION [optional]
152  [State any special notes, constraints or cautions for users of this function]
153
154 ------------------------------------------------------------------------------
155 */
156
157 /*----------------------------------------------------------------------------
158 ; FUNCTION CODE
159 ----------------------------------------------------------------------------*/
160 OSCL_EXPORT_REF Word16 shr(register Word16 var1, register Word16 var2, Flag *pOverflow)
161 {
162     register Word16 result;
163
164     if (var2 != 0)
165     {
166         if (var2 > 0)
167         {
168             if (var2 > 15)
169             {
170                 var2 = 15;
171             }
172
173             result = var1 >> var2;
174
175         }
176         else
177         {
178             var2 = -var2;   /* Shift right negative is equivalent */
179
180             if (var2 > 15)
181             {
182                 var2 = 15;
183             }
184
185             result = (var1 << var2);
186             if ((result >> var2) != var1)
187             {
188                 *pOverflow = 1;
189                 result = ((var1 > 0) ? MAX_16 : MIN_16);
190             }
191         }
192
193     }
194     else
195     {
196         result = var1;
197     }
198
199     return (result);
200 }
201