Git init
[external/opencore-amr.git] / opencore / codecs_v2 / audio / gsm_amr / amr_nb / common / src / div_s.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: div_s.cpp
31
32 ------------------------------------------------------------------------------
33  INPUT AND OUTPUT DEFINITIONS
34
35  Inputs:
36     var1 = 16 bit signed integer (Word16) whose value falls in
37            the range : 0x0000 <= var1 <= 0x7fff.
38     var2 = 16 bit signed integer (Word16) whose value falls in
39            the range : 0x0000 <= var1 <= 0x7fff.
40
41  Local Stores/Buffers/Pointers Needed:
42     None
43
44  Global Stores/Buffers/Pointers Needed:
45     None
46
47  Outputs:
48     var_out = quotient of var1 divided by var2 (Word16)
49
50  Pointers and Buffers Modified:
51     None
52
53  Local Stores Modified:
54     None
55
56  Global Stores Modified:
57     None
58
59 ------------------------------------------------------------------------------
60  FUNCTION DESCRIPTION
61
62  This function produces a result which is the fractional integer division of
63  var1 by var2; var1 and var2 must be positive and var2 must be greater or equal
64  to var1; the result is positive (leading bit equal to 0) and truncated to 16
65  bits. If var1 = var2 then div(var1,var2) = 32767.
66
67 ------------------------------------------------------------------------------
68  REQUIREMENTS
69
70  None
71
72 ------------------------------------------------------------------------------
73  REFERENCES
74
75  [1] basicop2.c, ETS Version 2.0.0, February 8, 1999
76
77 ------------------------------------------------------------------------------
78  PSEUDO-CODE
79
80 Word16 div_s (Word16 var1, Word16 var2)
81 {
82     Word16 var_out = 0;
83     Word16 iteration;
84     Word32 L_num;
85     Word32 L_denom;
86     Word16 abort_flag = 0;
87
88     if ((var1 > var2) || (var1 < 0))
89     {
90         printf ("Division Error var1=%d  var2=%d\n", var1, var2);
91         abort_flag = 1;
92         exit(0);
93     }
94     if ((var1 != 0) && (abort_flag == 0))
95     {
96         if (var1 == var2)
97         {
98             var_out = MAX_16;
99         }
100         else
101         {
102             L_num = (Word32) var1;
103             L_denom = (Word32) var2;
104
105             for (iteration = 15; iteration > 0; iteration--)
106             {
107                 var_out <<= 1;
108                 L_num <<= 1;
109
110                 if (L_num >= L_denom)
111                 {
112                     L_num -= L_denom;
113                     var_out += 1;
114                 }
115             }
116         }
117     }
118
119 #if (WMOPS)
120     multiCounter[currCounter].div_s++;
121 #endif
122     return (var_out);
123 }
124
125 ------------------------------------------------------------------------------
126 */
127
128
129 /*----------------------------------------------------------------------------
130 ; INCLUDES
131 ----------------------------------------------------------------------------*/
132 #include    "basic_op.h"
133
134 /*----------------------------------------------------------------------------
135 ; MACROS
136 ; Define module specific macros here
137 ----------------------------------------------------------------------------*/
138
139 /*----------------------------------------------------------------------------
140 ; DEFINES
141 ; Include all pre-processor statements here. Include conditional
142 ; compile variables also.
143 ----------------------------------------------------------------------------*/
144
145 /*----------------------------------------------------------------------------
146 ; LOCAL FUNCTION DEFINITIONS
147 ; Function Prototype declaration
148 ----------------------------------------------------------------------------*/
149
150 /*----------------------------------------------------------------------------
151 ; LOCAL STORE/BUFFER/POINTER DEFINITIONS
152 ; Variable declaration - defined here and used outside this module
153 ----------------------------------------------------------------------------*/
154
155 /*----------------------------------------------------------------------------
156 ; EXTERNAL FUNCTION REFERENCES
157 ; Declare functions defined elsewhere and referenced in this module
158 ----------------------------------------------------------------------------*/
159
160 /*----------------------------------------------------------------------------
161 ; EXTERNAL GLOBAL STORE/BUFFER/POINTER REFERENCES
162 ; Declare variables used in this module but defined elsewhere
163 ----------------------------------------------------------------------------*/
164
165 /*----------------------------------------------------------------------------
166 ; FUNCTION CODE
167 ----------------------------------------------------------------------------*/
168 OSCL_EXPORT_REF Word16 div_s(register Word16 var1, register Word16 var2)
169 {
170     /*----------------------------------------------------------------------------
171     ; Define all local variables
172     ----------------------------------------------------------------------------*/
173     Word16 var_out = 0;
174     register Word16 iteration;
175     Word32 L_num;
176     Word32 L_denom;
177     Word32 L_denom_by_2;
178     Word32 L_denom_by_4;
179
180     /*----------------------------------------------------------------------------
181     ; Function body here
182     ----------------------------------------------------------------------------*/
183     if ((var1 > var2) || (var1 < 0))
184     {
185         return 0; // used to exit(0);
186     }
187     if (var1)
188     {
189         if (var1 != var2)
190         {
191
192             L_num = (Word32) var1;
193             L_denom = (Word32) var2;
194             L_denom_by_2 = (L_denom << 1);
195             L_denom_by_4 = (L_denom << 2);
196             for (iteration = 5; iteration > 0; iteration--)
197             {
198                 var_out <<= 3;
199                 L_num   <<= 3;
200
201                 if (L_num >= L_denom_by_4)
202                 {
203                     L_num -= L_denom_by_4;
204                     var_out |= 4;
205                 }
206
207                 if (L_num >= L_denom_by_2)
208                 {
209                     L_num -= L_denom_by_2;
210                     var_out |=  2;
211                 }
212
213                 if (L_num >= (L_denom))
214                 {
215                     L_num -= (L_denom);
216                     var_out |=  1;
217                 }
218
219             }
220         }
221         else
222         {
223             var_out = MAX_16;
224         }
225     }
226
227 #if (WMOPS)
228     multiCounter[currCounter].div_s++;
229 #endif
230
231     /*----------------------------------------------------------------------------
232     ; Return nothing or data or data pointer
233     ----------------------------------------------------------------------------*/
234     return (var_out);
235 }