Initialize Tizen 2.3
[external/opencore-amr.git] / opencore / codecs_v2 / audio / gsm_amr / amr_nb / common / include / basic_op_c_equivalent.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_c_equivalent.h
32
33 ------------------------------------------------------------------------------
34  INCLUDE DESCRIPTION
35
36  This file includes all the C-Equivalent basicop.c functions.
37
38 ------------------------------------------------------------------------------
39 */
40
41 /*----------------------------------------------------------------------------
42 ; CONTINUE ONLY IF NOT ALREADY DEFINED
43 ----------------------------------------------------------------------------*/
44 #ifndef BASIC_OP_C_EQUIVALENT_H
45 #define BASIC_OP_C_EQUIVALENT_H
46
47 /*----------------------------------------------------------------------------
48 ; INCLUDES
49 ----------------------------------------------------------------------------*/
50 #include    "basicop_malloc.h"
51
52 /*--------------------------------------------------------------------------*/
53 #ifdef __cplusplus
54 extern "C"
55 {
56 #endif
57
58     /*----------------------------------------------------------------------------
59     ; MACROS
60     ; Define module specific macros here
61     ----------------------------------------------------------------------------*/
62
63     /*----------------------------------------------------------------------------
64     ; DEFINES
65     ; Include all pre-processor statements here.
66     ----------------------------------------------------------------------------*/
67
68     /*----------------------------------------------------------------------------
69     ; EXTERNAL VARIABLES REFERENCES
70     ; Declare variables used in this module but defined elsewhere
71     ----------------------------------------------------------------------------*/
72
73     /*----------------------------------------------------------------------------
74     ; SIMPLE TYPEDEF'S
75     ----------------------------------------------------------------------------*/
76
77     /*----------------------------------------------------------------------------
78     ; ENUMERATED TYPEDEF'S
79     ----------------------------------------------------------------------------*/
80
81     /*----------------------------------------------------------------------------
82     ; STRUCTURES TYPEDEF'S
83     ----------------------------------------------------------------------------*/
84
85     /*----------------------------------------------------------------------------
86     ; GLOBAL FUNCTION DEFINITIONS
87     ; Function Prototype declaration
88     ----------------------------------------------------------------------------*/
89
90
91     /*
92     ------------------------------------------------------------------------------
93      FUNCTION NAME: L_add
94     ------------------------------------------------------------------------------
95      INPUT AND OUTPUT DEFINITIONS
96
97      Inputs:
98         L_var1 = 32 bit long signed integer (Word32) whose value falls
99                  in the range : 0x8000 0000 <= L_var1 <= 0x7fff ffff.
100
101         L_var2 = 32 bit long signed integer (Word32) whose value falls
102                  in the range : 0x8000 0000 <= L_var1 <= 0x7fff ffff.
103
104         pOverflow = pointer to overflow (Flag)
105
106      Outputs:
107         pOverflow -> 1 if the 32 bit add operation resulted in overflow
108
109      Returns:
110         L_sum = 32-bit sum of L_var1 and L_var2 (Word32)
111     */
112     static inline Word32 L_add(register Word32 L_var1, register Word32 L_var2, Flag *pOverflow)
113     {
114         Word32 L_sum;
115
116         L_sum = L_var1 + L_var2;
117
118         if ((L_var1 ^ L_var2) >= 0)
119         {
120             if ((L_sum ^ L_var1) >> 31)
121             {
122                 L_sum = (L_var1 >> 31) ? MIN_32 : MAX_32;
123                 *pOverflow = 1;
124             }
125         }
126
127         return (L_sum);
128     }
129
130     /*
131     ------------------------------------------------------------------------------
132      FUNCTION NAME: L_sub
133     ------------------------------------------------------------------------------
134      INPUT AND OUTPUT DEFINITIONS
135
136      Inputs:
137         L_var1 = 32 bit long signed integer (Word32) whose value falls
138                  in the range : 0x8000 0000 <= L_var1 <= 0x7fff ffff.
139
140         L_var2 = 32 bit long signed integer (Word32) whose value falls
141                  in the range : 0x8000 0000 <= L_var1 <= 0x7fff ffff.
142
143         pOverflow = pointer to overflow (Flag)
144
145      Outputs:
146         pOverflow -> 1 if the 32 bit add operation resulted in overflow
147
148      Returns:
149         L_diff = 32-bit difference of L_var1 and L_var2 (Word32)
150     */
151     static inline Word32 L_sub(register Word32 L_var1, register Word32 L_var2,
152                                register Flag *pOverflow)
153     {
154         Word32 L_diff;
155
156         L_diff = L_var1 - L_var2;
157
158         if ((L_var1 ^ L_var2) >> 31)
159         {
160             if ((L_diff ^ L_var1) & MIN_32)
161             {
162                 L_diff = (L_var1 >> 31) ? MIN_32 : MAX_32;
163                 *pOverflow = 1;
164             }
165         }
166
167         return (L_diff);
168     }
169
170
171     /*
172     ------------------------------------------------------------------------------
173      FUNCTION NAME: L_mac
174     ------------------------------------------------------------------------------
175      INPUT AND OUTPUT DEFINITIONS
176
177      Inputs:
178         L_var3 = 32 bit long signed integer (Word32) whose value falls
179                  in the range : 0x8000 0000 <= L_var3 <= 0x7fff ffff.
180         var1 = 16 bit short signed integer (Word16) whose value falls in
181                the range : 0xffff 8000 <= var1 <= 0x0000 7fff.
182         var2 = 16 bit short signed integer (Word16) whose value falls in
183                the range : 0xffff 8000 <= var2 <= 0x0000 7fff.
184
185         pOverflow = pointer to overflow (Flag)
186
187      Outputs:
188         pOverflow -> 1 if the 32 bit add operation resulted in overflow
189
190      Returns:
191         result = 32-bit result of L_var3 + (var1 * var2)(Word32)
192     */
193     static inline Word32 L_mac(Word32 L_var3, Word16 var1, Word16 var2, Flag *pOverflow)
194     {
195         Word32 result;
196         Word32 L_sum;
197         result = (Word32) var1 * var2;
198         if (result != (Word32) 0x40000000L)
199         {
200             L_sum = (result << 1) + L_var3;
201
202             /* Check if L_sum and L_var_3 share the same sign */
203             if ((L_var3 ^ result) > 0)
204             {
205                 if ((L_sum ^ L_var3) >> 31)
206                 {
207                     L_sum = (L_var3 >> 31) ? MIN_32 : MAX_32;
208                     *pOverflow = 1;
209                 }
210             }
211         }
212         else
213         {
214             *pOverflow = 1;
215             L_sum = MAX_32;
216         }
217         return (L_sum);
218     }
219
220     /*
221     ------------------------------------------------------------------------------
222      FUNCTION NAME: L_mult
223     ------------------------------------------------------------------------------
224      INPUT AND OUTPUT DEFINITIONS
225
226      Inputs:
227         L_var1 = 16 bit short signed integer (Word16) whose value falls in
228                the range : 0xffff 8000 <= var1 <= 0x0000 7fff.
229
230         L_var2 = 16 bit short signed integer (Word16) whose value falls in
231                the range : 0xffff 8000 <= var1 <= 0x0000 7fff.
232
233         pOverflow = pointer to overflow (Flag)
234
235      Outputs:
236         pOverflow -> 1 if the 32 bit add operation resulted in overflow
237
238      Returns:
239         L_product = 32-bit product of L_var1 and L_var2 (Word32)
240     */
241     static inline Word32 L_mult(Word16 var1, Word16 var2, Flag *pOverflow)
242     {
243         register Word32 L_product;
244
245         L_product = (Word32) var1 * var2;
246
247         if (L_product != (Word32) 0x40000000L)
248         {
249             L_product <<= 1;          /* Multiply by 2 */
250         }
251         else
252         {
253             *pOverflow = 1;
254             L_product = MAX_32;
255         }
256
257         return (L_product);
258     }
259
260
261     /*
262     ------------------------------------------------------------------------------
263      FUNCTION NAME: L_msu
264     ------------------------------------------------------------------------------
265      INPUT AND OUTPUT DEFINITIONS
266
267      Inputs:
268         L_var3 = 32 bit long signed integer (Word32) whose value falls
269                  in the range : 0x8000 0000 <= L_var3 <= 0x7fff ffff.
270
271         var1 = 16 bit short signed integer (Word16) whose value falls in
272                the range : 0xffff 8000 <= var1 <= 0x0000 7fff.
273         var2 = 16 bit short signed integer (Word16) whose value falls in
274                the range : 0xffff 8000 <= var2 <= 0x0000 7fff.
275
276         pOverflow = pointer to overflow (Flag)
277
278      Outputs:
279         pOverflow -> 1 if the 32 bit operation resulted in overflow
280
281      Returns:
282         result = 32-bit result of L_var3 - (var1 * var2)
283     */
284
285     static inline Word32 L_msu(Word32 L_var3, Word16 var1, Word16 var2, Flag *pOverflow)
286     {
287         Word32 result;
288
289         result = L_mult(var1, var2, pOverflow);
290         result = L_sub(L_var3, result, pOverflow);
291
292         return (result);
293     }
294
295     /*
296     ------------------------------------------------------------------------------
297      FUNCTION NAME: Mpy_32
298     ------------------------------------------------------------------------------
299      INPUT AND OUTPUT DEFINITIONS
300
301      Inputs:
302         L_var1_hi = most significant word of first input (Word16).
303         L_var1_lo = least significant word of first input (Word16).
304         L_var2_hi = most significant word of second input (Word16).
305         L_var2_lo = least significant word of second input (Word16).
306
307         pOverflow = pointer to overflow (Flag)
308
309      Outputs:
310         pOverflow -> 1 if the 32 bit multiply operation resulted in overflow
311
312      Returns:
313         L_product = 32-bit product of L_var1 and L_var2 (Word32)
314     */
315     static inline Word32 Mpy_32(Word16 L_var1_hi,
316                                 Word16 L_var1_lo,
317                                 Word16 L_var2_hi,
318                                 Word16 L_var2_lo,
319                                 Flag   *pOverflow)
320     {
321         Word32 L_product;
322         Word32 L_sum;
323         Word32 product32;
324
325         OSCL_UNUSED_ARG(pOverflow);
326         L_product = (Word32) L_var1_hi * L_var2_hi;
327
328         if (L_product != (Word32) 0x40000000L)
329         {
330             L_product <<= 1;
331         }
332         else
333         {
334             L_product = MAX_32;
335         }
336
337         /* result = mult (L_var1_hi, L_var2_lo, pOverflow); */
338         product32 = ((Word32) L_var1_hi * L_var2_lo) >> 15;
339
340         /* L_product = L_mac (L_product, result, 1, pOverflow); */
341         L_sum = L_product + (product32 << 1);
342
343         if ((L_product ^ product32) > 0)
344         {
345             if ((L_sum ^ L_product) >> 31)
346             {
347                 L_sum = (L_product >> 31) ? MIN_32 : MAX_32;
348             }
349         }
350
351         L_product = L_sum;
352
353         /* result = mult (L_var1_lo, L_var2_hi, pOverflow); */
354         product32 = ((Word32) L_var1_lo * L_var2_hi) >> 15;
355
356         /* L_product = L_mac (L_product, result, 1, pOverflow); */
357         L_sum = L_product + (product32 << 1);
358
359         if ((L_product ^ product32) > 0)
360         {
361             if ((L_sum ^ L_product) >> 31)
362             {
363                 L_sum = (L_product >> 31) ? MIN_32 : MAX_32;
364             }
365         }
366         return (L_sum);
367     }
368
369     /*
370     ------------------------------------------------------------------------------
371      FUNCTION NAME: Mpy_32_16
372     ------------------------------------------------------------------------------
373      INPUT AND OUTPUT DEFINITIONS
374
375      Inputs:
376         L_var1_hi = most significant 16 bits of 32-bit input (Word16).
377         L_var1_lo = least significant 16 bits of 32-bit input (Word16).
378         var2  = 16-bit signed integer (Word16).
379
380         pOverflow = pointer to overflow (Flag)
381
382      Outputs:
383         pOverflow -> 1 if the 32 bit product operation resulted in overflow
384
385      Returns:
386         product = 32-bit product of the 32-bit L_var1 and 16-bit var1 (Word32)
387     */
388
389     static inline Word32 Mpy_32_16(Word16 L_var1_hi,
390                                    Word16 L_var1_lo,
391                                    Word16 var2,
392                                    Flag *pOverflow)
393     {
394
395         Word32 L_product;
396         Word32 L_sum;
397         Word32 result;
398         L_product = (Word32) L_var1_hi * var2;
399
400         if (L_product != (Word32) 0x40000000L)
401         {
402             L_product <<= 1;
403         }
404         else
405         {
406             *pOverflow = 1;
407             L_product = MAX_32;
408         }
409
410         result = ((Word32)L_var1_lo * var2) >> 15;
411
412         L_sum  =  L_product + (result << 1);
413
414         if ((L_product ^ result) > 0)
415         {
416             if ((L_sum ^ L_product) >> 31)
417             {
418                 L_sum = (L_product >> 31) ? MIN_32 : MAX_32;
419                 *pOverflow = 1;
420             }
421         }
422         return (L_sum);
423
424     }
425
426     /*
427     ------------------------------------------------------------------------------
428      FUNCTION NAME: mult
429     ------------------------------------------------------------------------------
430      INPUT AND OUTPUT DEFINITIONS
431
432      Inputs:
433         var1 = 16 bit short signed integer (Word16) whose value falls in
434                the range : 0xffff 8000 <= var1 <= 0x0000 7fff.
435
436         var2 = 16 bit short signed integer (Word16) whose value falls in
437                the range : 0xffff 8000 <= var2 <= 0x0000 7fff.
438
439         pOverflow = pointer to overflow (Flag)
440
441      Outputs:
442         pOverflow -> 1 if the add operation resulted in overflow
443
444      Returns:
445         product = 16-bit limited product of var1 and var2 (Word16)
446     */
447     static inline Word16 mult(Word16 var1, Word16 var2, Flag *pOverflow)
448     {
449         register Word32 product;
450
451         product = ((Word32) var1 * var2) >> 15;
452
453         /* Saturate result (if necessary). */
454         /* var1 * var2 >0x00007fff is the only case */
455         /* that saturation occurs. */
456
457         if (product > 0x00007fffL)
458         {
459             *pOverflow = 1;
460             product = (Word32) MAX_16;
461         }
462
463
464         /* Return the product as a 16 bit value by type casting Word32 to Word16 */
465
466         return ((Word16) product);
467     }
468
469
470     static inline Word32 amrnb_fxp_mac_16_by_16bb(Word32 L_var1, Word32 L_var2, Word32 L_var3)
471     {
472         Word32 result;
473
474         result = L_var3 + L_var1 * L_var2;
475
476         return result;
477     }
478
479     static inline Word32 amrnb_fxp_msu_16_by_16bb(Word32 L_var1, Word32 L_var2, Word32 L_var3)
480     {
481         Word32 result;
482
483         result = L_var3 - L_var1 * L_var2;
484
485         return result;
486     }
487
488
489     /*----------------------------------------------------------------------------
490     ; END
491     ----------------------------------------------------------------------------*/
492 #ifdef __cplusplus
493 }
494 #endif
495
496 #endif /* BASIC_OP_C_EQUIVALENT_H */
497
498
499