Initialize Tizen 2.3
[external/opencore-amr.git] / opencore / codecs_v2 / audio / gsm_amr / amr_nb / common / src / l_shr_r.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  Filename: l_shr_r.cpp
31
32 */
33
34
35 /*----------------------------------------------------------------------------
36 ; INCLUDES
37 ----------------------------------------------------------------------------*/
38 #include    "basic_op.h"
39
40 /*----------------------------------------------------------------------------
41 ; MACROS
42 ; [Define module specific macros here]
43 ----------------------------------------------------------------------------*/
44
45 /*----------------------------------------------------------------------------
46 ; DEFINES
47 ; [Include all pre-processor statements here. Include conditional
48 ; compile variables also.]
49 ----------------------------------------------------------------------------*/
50
51 /*----------------------------------------------------------------------------
52 ; LOCAL FUNCTION DEFINITIONS
53 ; [List function prototypes here]
54 ----------------------------------------------------------------------------*/
55
56 /*----------------------------------------------------------------------------
57 ; LOCAL VARIABLE DEFINITIONS
58 ; [Variable declaration - defined here and used outside this module]
59 ----------------------------------------------------------------------------*/
60
61
62 /*
63 ------------------------------------------------------------------------------
64  FUNCTION NAME: L_shr_r
65 ------------------------------------------------------------------------------
66  INPUT AND OUTPUT DEFINITIONS
67
68  Inputs:
69     L_var1 = 32 bit long signed integer (Word32 ) whose value falls
70              in the range : 0x8000 0000 <= L_var1 <= 0x7fff ffff.
71     var2 = 16 bit short signed integer (Word16) whose value falls in
72            the range : 0xffff 8000 <= var2 <= 0x0000 7fff.
73
74     pOverflow = pointer to overflow (Flag)
75
76  Outputs:
77     pOverflow -> 1 if the 32 bit shift operation resulted in overflow
78
79  Returns:
80     result = Shifted result w/ rounding (Word32)
81
82  Global Variables Used:
83     None
84
85  Local Variables Needed:
86     None
87
88 ------------------------------------------------------------------------------
89  FUNCTION DESCRIPTION
90
91  This function arithmetically shifts the 32 bit input L_var1 right var2
92  positions with rounding. If var2 is negative, the function
93  arithmetically shifts L_var1 left by -var2 and zero fills the -var2 LSB of
94  the result. The result is saturated in case of underflows or overflows, i.e.,
95
96  - If var2 is greater than zero :
97     if (L_sub(L_shl(L_shr(L_var1,var2),1),L_shr(L_var1,sub(var2,1))))
98     is equal to zero
99     then
100         L_shr_r(L_var1,var2) = L_shr(L_var1,var2)
101     else
102         L_shr_r(L_var1,var2) = L_add(L_shr(L_var1,var2),1)
103  - If var2 is less than or equal to zero :
104     L_shr_r(L_var1,var2) = L_shr(L_var1,var2).
105
106 ------------------------------------------------------------------------------
107  REQUIREMENTS
108
109  None
110
111 ------------------------------------------------------------------------------
112  REFERENCES
113
114  [1] L_shr_r() function in basic_op2.c,  UMTS GSM AMR speech codec, R99 -
115  Version 3.2.0, March 2, 2001
116
117 ------------------------------------------------------------------------------
118  PSEUDO-CODE
119
120 Word32 L_shr_r (Word32 L_var1, Word16 var2)
121 {
122     Word32 L_var_out;
123
124 * The reference ETSI code uses a global flag for Overflow. In the actual
125 * implementation a pointer to Overflow flag is passed in as a parameter to the
126 * function L_shr()
127
128     if (var2 > 31)
129     {
130         L_var_out = 0;
131     }
132     else
133     {
134         L_var_out = L_shr (L_var1, var2);
135 #if (WMOPS)
136         multiCounter[currCounter].L_shr--;
137 #endif
138         if (var2 > 0)
139         {
140             if ((L_var1 & ((Word32) 1 << (var2 - 1))) != 0)
141             {
142                 L_var_out++;
143             }
144         }
145     }
146 #if (WMOPS)
147     multiCounter[currCounter].L_shr_r++;
148 #endif
149     return (L_var_out);
150 }
151
152 ------------------------------------------------------------------------------
153  CAUTION [optional]
154  [State any special notes, constraints or cautions for users of this function]
155
156 ------------------------------------------------------------------------------
157 */
158
159 /*----------------------------------------------------------------------------
160 ; FUNCTION CODE
161 ----------------------------------------------------------------------------*/
162 OSCL_EXPORT_REF Word32 L_shr_r(register Word32 L_var1, register Word16 var2, Flag *pOverflow)
163 {
164     Word32 result;
165
166     if (var2 > 31)
167     {
168         result = 0;
169     }
170     else
171     {
172         result = L_shr(L_var1, var2, pOverflow);
173
174         if (var2 > 0)
175         {
176             if ((L_var1 & ((Word32) 1 << (var2 - 1))) != 0)
177             {
178                 result++;
179             }
180         }
181     }
182     return (result);
183 }