Initialize Tizen 2.3
[external/opencore-amr.git] / opencore / codecs_v2 / audio / gsm_amr / amr_nb / common / include / basic_op.h
1 /* ------------------------------------------------------------------
2  * Copyright (C) 1998-2010 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: basic_op.h
32
33 ------------------------------------------------------------------------------
34  INCLUDE DESCRIPTION
35
36  This file includes all the basicop2.c functions' header files.
37
38 ------------------------------------------------------------------------------
39 */
40
41 /*----------------------------------------------------------------------------
42 ; CONTINUE ONLY IF NOT ALREADY DEFINED
43 ----------------------------------------------------------------------------*/
44 #ifndef BASIC_OP_H
45 #define BASIC_OP_H
46
47 /*----------------------------------------------------------------------------
48 ; INCLUDES
49 ----------------------------------------------------------------------------*/
50 #include    "basicop_malloc.h"
51
52 #if   ((PV_CPU_ARCH_VERSION >=5) && (PV_COMPILER == EPV_ARM_GNUC))
53 #include "basic_op_arm_gcc_v5.h"
54
55 #else
56 #include "basic_op_c_equivalent.h"
57
58 #endif
59
60
61
62 #include    "add.h"
63 #include    "div_s.h"
64 #include    "l_shr_r.h"
65 #include    "mult_r.h"
66 #include    "norm_l.h"
67 #include    "norm_s.h"
68 #include    "round.h"
69 #include    "shr_r.h"
70 #include    "sub.h"
71 #include    "shr.h"
72 #include    "l_negate.h"
73 #include    "l_extract.h"
74 /*--------------------------------------------------------------------------*/
75 #ifdef __cplusplus
76 extern "C"
77 {
78 #endif
79
80     /*----------------------------------------------------------------------------
81     ; MACROS
82     ; Define module specific macros here
83     ----------------------------------------------------------------------------*/
84
85     /*----------------------------------------------------------------------------
86     ; DEFINES
87     ; Include all pre-processor statements here.
88     ----------------------------------------------------------------------------*/
89
90     /*----------------------------------------------------------------------------
91     ; EXTERNAL VARIABLES REFERENCES
92     ; Declare variables used in this module but defined elsewhere
93     ----------------------------------------------------------------------------*/
94
95     /*----------------------------------------------------------------------------
96     ; SIMPLE TYPEDEF'S
97     ----------------------------------------------------------------------------*/
98
99     /*----------------------------------------------------------------------------
100     ; ENUMERATED TYPEDEF'S
101     ----------------------------------------------------------------------------*/
102
103     /*----------------------------------------------------------------------------
104     ; STRUCTURES TYPEDEF'S
105     ----------------------------------------------------------------------------*/
106
107     /*----------------------------------------------------------------------------
108     ; GLOBAL FUNCTION DEFINITIONS
109     ; Function Prototype declaration
110     ----------------------------------------------------------------------------*/
111     /*
112     ------------------------------------------------------------------------------
113      FUNCTION NAME: mac_32
114     ------------------------------------------------------------------------------
115      INPUT AND OUTPUT DEFINITIONS
116
117      Inputs:
118         L_var3 = 32 bit long signed integer (Word32) whose value falls
119                  in the range : 0x8000 0000 <= L_var3 <= 0x7fff ffff.
120         L_var1_hi = 16 bit short signed integer (Word16) whose value falls in
121                the range : 0xffff 8000 <= var1 <= 0x0000 7fff.
122         L_var1_lo = 16 bit short signed integer (Word16) whose value falls in
123                the range : 0xffff 8000 <= var2 <= 0x0000 7fff.
124         L_var2_hi = 16 bit short signed integer (Word16) whose value falls in
125                the range : 0xffff 8000 <= var1 <= 0x0000 7fff.
126         L_var2_lo = 16 bit short signed integer (Word16) whose value falls in
127                the range : 0xffff 8000 <= var2 <= 0x0000 7fff.
128
129         pOverflow = pointer to overflow (Flag)
130
131      Outputs:
132         pOverflow -> 1 if the 32 bit mac operation resulted in overflow
133
134      Returns:
135         L_var3 = 32-bit result of L_var3 + (L_var1 * L_var2)(Word32)
136
137     */
138     static inline Word32 Mac_32(Word32 L_var3,
139     Word16 L_var1_hi,
140     Word16 L_var1_lo,
141     Word16 L_var2_hi,
142     Word16 L_var2_lo,
143     Flag *pOverflow)
144     {
145         Word16  product;
146
147         L_var3 = L_mac(L_var3, L_var1_hi, L_var2_hi, pOverflow);
148
149         product = mult(L_var1_hi, L_var2_lo, pOverflow);
150         L_var3 = L_mac(L_var3, product, 1, pOverflow);
151
152         product = mult(L_var1_lo, L_var2_hi, pOverflow);
153         L_var3 = L_mac(L_var3, product, 1, pOverflow);
154
155         return (L_var3);
156     }
157
158     /*
159     ------------------------------------------------------------------------------
160      FUNCTION NAME: mac_32_16
161     ------------------------------------------------------------------------------
162      INPUT AND OUTPUT DEFINITIONS
163
164      Inputs:
165         L_var3 = 32 bit long signed integer (Word32) whose value falls
166                  in the range : 0x8000 0000 <= L_var3 <= 0x7fff ffff.
167         L_var1_hi = 16 bit short signed integer (Word16) whose value falls in
168                the range : 0xffff 8000 <= var1 <= 0x0000 7fff.
169         L_var1_lo = 16 bit short signed integer (Word16) whose value falls in
170                the range : 0xffff 8000 <= var2 <= 0x0000 7fff.
171         var2= 16 bit short signed integer (Word16) whose value falls in
172                the range : 0xffff 8000 <= var1 <= 0x0000 7fff.
173         pOverflow = pointer to overflow (Flag)
174
175      Outputs:
176         pOverflow -> 1 if the 32 bit mac operation resulted in overflow
177
178      Returns:
179         L_var3 = 32-bit result of L_var3 + (L_var1 * var2)(Word32)
180     */
181
182     static inline Word32 Mac_32_16(Word32 L_var3,
183                                    Word16 L_var1_hi,
184                                    Word16 L_var1_lo,
185                                    Word16 var2,
186                                    Flag  *pOverflow)
187     {
188         Word16  product;
189
190         L_var3 = L_mac(L_var3, L_var1_hi, var2, pOverflow);
191
192         product = mult(L_var1_lo, var2, pOverflow);
193         L_var3 = L_mac(L_var3, product, 1, pOverflow);
194
195         return (L_var3);
196     }
197
198
199     /*----------------------------------------------------------------------------
200          Function Name : negate
201
202          Negate var1 with saturation, saturate in the case where input is -32768:
203                       negate(var1) = sub(0,var1).
204
205          Inputs :
206           var1
207                    16 bit short signed integer (Word16) whose value falls in the
208                    range : 0x8000 <= var1 <= 0x7fff.
209
210          Outputs :
211           none
212
213          Return Value :
214                    16 bit short signed integer (Word16) whose value falls in the
215                    range : 0x8000 <= var_out <= 0x7fff.
216      ----------------------------------------------------------------------------*/
217
218     static inline Word16 negate(Word16 var1)
219     {
220         return (((var1 == MIN_16) ? MAX_16 : -var1));
221     }
222
223     /*----------------------------------------------------------------------------
224
225          Function Name : shl
226
227          Arithmetically shift the 16 bit input var1 left var2 positions.Zero fill
228          the var2 LSB of the result. If var2 is negative, arithmetically shift
229          var1 right by -var2 with sign extension. Saturate the result in case of
230          underflows or overflows.
231
232          Inputs :
233           var1
234                    16 bit short signed integer (Word16) whose value falls in the
235                    range : 0x8000 <= var1 <= 0x7fff.
236
237           var2
238                    16 bit short signed integer (Word16) whose value falls in the
239                    range : 0x8000 <= var1 <= 0x7fff.
240
241           pOverflow : pointer to overflow (Flag)
242
243          Return Value :
244           var_out
245                    16 bit short signed integer (Word16) whose value falls in the
246                    range : 0x8000 <= var_out <= 0x7fff.
247      ----------------------------------------------------------------------------*/
248
249     static inline Word16 shl(Word16 var1, Word16 var2, Flag *pOverflow)
250     {
251         Word16 var_out = 0;
252
253         OSCL_UNUSED_ARG(pOverflow);
254
255         if (var2 < 0)
256         {
257             var2 = -var2;
258             if (var2 < 15)
259             {
260                 var_out = var1 >> var2;
261             }
262
263         }
264         else
265         {
266             var_out = var1 << var2;
267             if (var_out >> var2 != var1)
268             {
269                 var_out = (var1 >> 15) ^ MAX_16;
270             }
271         }
272         return (var_out);
273     }
274
275
276     /*----------------------------------------------------------------------------
277
278          Function Name : L_shl
279
280          Arithmetically shift the 32 bit input L_var1 left var2 positions. Zero
281          fill the var2 LSB of the result. If var2 is negative, arithmetically
282          shift L_var1 right by -var2 with sign extension. Saturate the result in
283          case of underflows or overflows.
284
285          Inputs :
286           L_var1   32 bit long signed integer (Word32) whose value falls in the
287                    range : 0x8000 0000 <= L_var1 <= 0x7fff ffff.
288
289           var2
290                    16 bit short signed integer (Word16) whose value falls in the
291                    range :  8000 <= var2 <= 7fff.
292
293           pOverflow : pointer to overflow (Flag)
294
295          Return Value :
296                    32 bit long signed integer (Word32) whose value falls in the
297                    range : 0x8000 0000 <= L_var_out <= 0x7fff ffff.
298
299      ----------------------------------------------------------------------------*/
300
301     static inline Word32 L_shl(Word32 L_var1, Word16 var2, Flag *pOverflow)
302     {
303         Word32 L_var_out = 0;
304
305         OSCL_UNUSED_ARG(pOverflow);
306
307         if (var2 > 0)
308         {
309             L_var_out = L_var1 << var2;
310             if (L_var_out >> var2 != L_var1)
311             {
312                 L_var_out = (L_var1 >> 31) ^ MAX_32;
313             }
314         }
315         else
316         {
317             var2 = -var2;
318             if (var2 < 31)
319             {
320                 L_var_out = L_var1 >> var2;
321             }
322
323         }
324
325         return (L_var_out);
326     }
327
328
329     /*----------------------------------------------------------------------------
330
331          Function Name : L_shr
332
333          Arithmetically shift the 32 bit input L_var1 right var2 positions with
334          sign extension. If var2 is negative, arithmetically shift L_var1 left
335          by -var2 and zero fill the -var2 LSB of the result. Saturate the result
336          in case of underflows or overflows.
337
338          Inputs :
339           L_var1   32 bit long signed integer (Word32) whose value falls in the
340                    range : 0x8000 0000 <= L_var1 <= 0x7fff ffff.
341
342           var2
343                    16 bit short signed integer (Word16) whose value falls in the
344                    range :  8000 <= var2 <= 7fff.
345
346           pOverflow : pointer to overflow (Flag)
347
348          Return Value :
349                    32 bit long signed integer (Word32) whose value falls in the
350                    range : 0x8000 0000 <= L_var_out <= 0x7fff ffff.
351
352      ----------------------------------------------------------------------------*/
353
354     static inline Word32 L_shr(Word32 L_var1, Word16 var2, Flag *pOverflow)
355     {
356         Word32 L_var_out = 0;
357
358         OSCL_UNUSED_ARG(pOverflow);
359
360         if (var2 > 0)
361         {
362             if (var2 < 31)
363             {
364                 L_var_out = L_var1 >> var2;
365             }
366         }
367         else
368         {
369             var2 = -var2;
370
371             L_var_out = L_var1 << (var2) ;
372             if ((L_var_out >> (var2)) != L_var1)
373             {
374                 L_var_out = (L_var1 >> 31) ^ MAX_32;
375             }
376
377         }
378
379         return (L_var_out);
380     }
381
382     /*----------------------------------------------------------------------------
383
384          Function Name : abs_s
385
386           Absolute value of var1; abs_s(-32768) = 32767.
387
388          Inputs :
389           var1
390                    16 bit short signed integer (Word16) whose value falls in the
391                    range : 0x8000 <= var1 <= 0x7fff.
392
393           pOverflow : pointer to overflow (Flag)
394
395          Outputs :
396           none
397
398          Return Value :
399                    16 bit short signed integer (Word16) whose value falls in the
400                    range : 0x0000 <= var_out <= 0x7fff.
401
402      ----------------------------------------------------------------------------*/
403
404     static inline Word16 abs_s(Word16 var1)
405     {
406
407         Word16 y = var1 - (var1 < 0);
408         y = y ^(y >> 15);
409         return (y);
410
411     }
412     /*----------------------------------------------------------------------------
413     ; END
414     ----------------------------------------------------------------------------*/
415 #ifdef __cplusplus
416 }
417 #endif
418
419
420 #endif /* BASIC_OP_H */
421
422