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