Git init
[external/opencore-amr.git] / opencore / codecs_v2 / audio / gsm_amr / amr_nb / common / src / 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
31  Filename: shr_r.cpp
32
33 ------------------------------------------------------------------------------
34  INPUT AND OUTPUT DEFINITIONS
35
36  Inputs:
37     var1 = 16 bit short signed integer (Word16) whose value falls in
38            the range : 0xffff 8000 <= var1 <= 0x0000 7fff.
39     var2 = 16 bit short signed integer (Word16) whose value falls in
40            the range : 0xffff 8000 <= var2 <= 0x0000 7fff.
41
42  Local Stores/Buffers/Pointers Needed:
43     None
44
45  Global Stores/Buffers/Pointers Needed:
46     None
47
48  Outputs:
49     var_out = shifted input w/ rounding (Word16)
50
51  Pointers and Buffers Modified:
52     None
53
54  Local Stores Modified:
55     None
56
57  Global Stores Modified:
58     None
59
60 ------------------------------------------------------------------------------
61  FUNCTION DESCRIPTION
62
63  This function arithmetically shifts the 16 bit input var1 right var2 positions
64  with rounding. If var2 is negative, arithmetically shift var1 left by
65  -var2 with rounding. Saturate the result in case of underflows or
66  overflows.
67
68     - If var2 is greater than zero :
69         if (sub(shl(shr(var1,var2),1),shr(var1,sub(var2,1))))
70         is equal to zero
71         then
72             shr_r(var1,var2) = shr(var1,var2)
73         else
74             shr_r(var1,var2) = add(shr(var1,var2),1)
75     - If var2 is less than or equal to zero :
76         shr_r(var1,var2) = shr(var1,var2).
77
78 ------------------------------------------------------------------------------
79  REQUIREMENTS
80
81  None
82
83 ------------------------------------------------------------------------------
84  REFERENCES
85
86  [1] basicop2.c, ETS Version 2.0.0, February 8, 1999
87
88 ------------------------------------------------------------------------------
89  PSEUDO-CODE
90
91 Word16 shr_r (Word16 var1, Word16 var2)
92 {
93     Word16 var_out;
94
95     if (var2 > 15)
96     {
97         var_out = 0;
98     }
99     else
100     {
101         var_out = shr (var1, var2);
102 #if (WMOPS)
103         multiCounter[currCounter].shr--;
104 #endif
105
106         if (var2 > 0)
107         {
108             if ((var1 & ((Word16) 1 << (var2 - 1))) != 0)
109             {
110                 var_out++;
111             }
112         }
113     }
114 #if (WMOPS)
115     multiCounter[currCounter].shr_r++;
116 #endif
117     return (var_out);
118 }
119
120 ------------------------------------------------------------------------------
121 */
122
123
124 /*----------------------------------------------------------------------------
125 ; INCLUDES
126 ----------------------------------------------------------------------------*/
127 #include    "basic_op.h"
128
129 /*----------------------------------------------------------------------------
130 ; MACROS
131 ; Define module specific macros here
132 ----------------------------------------------------------------------------*/
133
134 /*----------------------------------------------------------------------------
135 ; DEFINES
136 ; Include all pre-processor statements here. Include conditional
137 ; compile variables also.
138 ----------------------------------------------------------------------------*/
139
140 /*----------------------------------------------------------------------------
141 ; LOCAL FUNCTION DEFINITIONS
142 ; Function Prototype declaration
143 ----------------------------------------------------------------------------*/
144
145 /*----------------------------------------------------------------------------
146 ; LOCAL STORE/BUFFER/POINTER DEFINITIONS
147 ; Variable declaration - defined here and used outside this module
148 ----------------------------------------------------------------------------*/
149
150 /*----------------------------------------------------------------------------
151 ; EXTERNAL FUNCTION REFERENCES
152 ; Declare functions defined elsewhere and referenced in this module
153 ----------------------------------------------------------------------------*/
154
155 /*----------------------------------------------------------------------------
156 ; EXTERNAL GLOBAL STORE/BUFFER/POINTER REFERENCES
157 ; Declare variables used in this module but defined elsewhere
158 ----------------------------------------------------------------------------*/
159
160 /*----------------------------------------------------------------------------
161 ; FUNCTION CODE
162 ----------------------------------------------------------------------------*/
163 OSCL_EXPORT_REF Word16 shr_r(register Word16 var1, register Word16 var2, Flag *pOverflow)
164 {
165     /*----------------------------------------------------------------------------
166     ; Define all local variables
167     ----------------------------------------------------------------------------*/
168     Word16 var_out;
169
170     /*----------------------------------------------------------------------------
171     ; Function body here
172     ----------------------------------------------------------------------------*/
173     if (var2 > 15)
174     {
175         var_out = 0;
176     }
177     else
178     {
179         var_out = shr(var1, var2, pOverflow);
180         if (var2 > 0)
181         {
182             if ((var1 & ((Word16) 1 << (var2 - 1))) != 0)
183             {
184                 var_out++;
185             }
186         }
187     }
188
189     /*----------------------------------------------------------------------------
190     ; Return nothing or data or data pointer
191     ----------------------------------------------------------------------------*/
192     return (var_out);
193 }