1 /* ------------------------------------------------------------------
2 * Copyright (C) 1998-2009 PacketVideo
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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
14 * See the License for the specific language governing permissions
15 * and limitations under the License.
16 * -------------------------------------------------------------------
18 /****************************************************************************************
19 Portions of this file are derived from the following 3GPP standard:
22 ANSI-C code for the Adaptive Multi-Rate (AMR) speech codec
23 Available from http://www.3gpp.org
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 ****************************************************************************************/
31 Filename: basic_op_c_equivalent.h
33 ------------------------------------------------------------------------------
36 This file includes all the C-Equivalent basicop.c functions.
38 ------------------------------------------------------------------------------
41 /*----------------------------------------------------------------------------
42 ; CONTINUE ONLY IF NOT ALREADY DEFINED
43 ----------------------------------------------------------------------------*/
44 #ifndef BASIC_OP_C_EQUIVALENT_H
45 #define BASIC_OP_C_EQUIVALENT_H
47 /*----------------------------------------------------------------------------
49 ----------------------------------------------------------------------------*/
50 #include "basicop_malloc.h"
52 /*--------------------------------------------------------------------------*/
58 /*----------------------------------------------------------------------------
60 ; Define module specific macros here
61 ----------------------------------------------------------------------------*/
63 /*----------------------------------------------------------------------------
65 ; Include all pre-processor statements here.
66 ----------------------------------------------------------------------------*/
68 /*----------------------------------------------------------------------------
69 ; EXTERNAL VARIABLES REFERENCES
70 ; Declare variables used in this module but defined elsewhere
71 ----------------------------------------------------------------------------*/
73 /*----------------------------------------------------------------------------
75 ----------------------------------------------------------------------------*/
77 /*----------------------------------------------------------------------------
78 ; ENUMERATED TYPEDEF'S
79 ----------------------------------------------------------------------------*/
81 /*----------------------------------------------------------------------------
82 ; STRUCTURES TYPEDEF'S
83 ----------------------------------------------------------------------------*/
85 /*----------------------------------------------------------------------------
86 ; GLOBAL FUNCTION DEFINITIONS
87 ; Function Prototype declaration
88 ----------------------------------------------------------------------------*/
92 ------------------------------------------------------------------------------
94 ------------------------------------------------------------------------------
95 INPUT AND OUTPUT DEFINITIONS
98 L_var1 = 32 bit long signed integer (Word32) whose value falls
99 in the range : 0x8000 0000 <= L_var1 <= 0x7fff ffff.
101 L_var2 = 32 bit long signed integer (Word32) whose value falls
102 in the range : 0x8000 0000 <= L_var1 <= 0x7fff ffff.
104 pOverflow = pointer to overflow (Flag)
107 pOverflow -> 1 if the 32 bit add operation resulted in overflow
110 L_sum = 32-bit sum of L_var1 and L_var2 (Word32)
112 static inline Word32 L_add(register Word32 L_var1, register Word32 L_var2, Flag *pOverflow)
116 L_sum = L_var1 + L_var2;
118 if ((L_var1 ^ L_var2) >= 0)
120 if ((L_sum ^ L_var1) >> 31)
122 L_sum = (L_var1 >> 31) ? MIN_32 : MAX_32;
131 ------------------------------------------------------------------------------
133 ------------------------------------------------------------------------------
134 INPUT AND OUTPUT DEFINITIONS
137 L_var1 = 32 bit long signed integer (Word32) whose value falls
138 in the range : 0x8000 0000 <= L_var1 <= 0x7fff ffff.
140 L_var2 = 32 bit long signed integer (Word32) whose value falls
141 in the range : 0x8000 0000 <= L_var1 <= 0x7fff ffff.
143 pOverflow = pointer to overflow (Flag)
146 pOverflow -> 1 if the 32 bit add operation resulted in overflow
149 L_diff = 32-bit difference of L_var1 and L_var2 (Word32)
151 static inline Word32 L_sub(register Word32 L_var1, register Word32 L_var2,
152 register Flag *pOverflow)
156 L_diff = L_var1 - L_var2;
158 if ((L_var1 ^ L_var2) >> 31)
160 if ((L_diff ^ L_var1) & MIN_32)
162 L_diff = (L_var1 >> 31) ? MIN_32 : MAX_32;
172 ------------------------------------------------------------------------------
174 ------------------------------------------------------------------------------
175 INPUT AND OUTPUT DEFINITIONS
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.
185 pOverflow = pointer to overflow (Flag)
188 pOverflow -> 1 if the 32 bit add operation resulted in overflow
191 result = 32-bit result of L_var3 + (var1 * var2)(Word32)
193 static inline Word32 L_mac(Word32 L_var3, Word16 var1, Word16 var2, Flag *pOverflow)
197 result = (Word32) var1 * var2;
198 if (result != (Word32) 0x40000000L)
200 L_sum = (result << 1) + L_var3;
202 /* Check if L_sum and L_var_3 share the same sign */
203 if ((L_var3 ^ result) > 0)
205 if ((L_sum ^ L_var3) >> 31)
207 L_sum = (L_var3 >> 31) ? MIN_32 : MAX_32;
221 ------------------------------------------------------------------------------
222 FUNCTION NAME: L_mult
223 ------------------------------------------------------------------------------
224 INPUT AND OUTPUT DEFINITIONS
227 L_var1 = 16 bit short signed integer (Word16) whose value falls in
228 the range : 0xffff 8000 <= var1 <= 0x0000 7fff.
230 L_var2 = 16 bit short signed integer (Word16) whose value falls in
231 the range : 0xffff 8000 <= var1 <= 0x0000 7fff.
233 pOverflow = pointer to overflow (Flag)
236 pOverflow -> 1 if the 32 bit add operation resulted in overflow
239 L_product = 32-bit product of L_var1 and L_var2 (Word32)
241 static inline Word32 L_mult(Word16 var1, Word16 var2, Flag *pOverflow)
243 register Word32 L_product;
245 L_product = (Word32) var1 * var2;
247 if (L_product != (Word32) 0x40000000L)
249 L_product <<= 1; /* Multiply by 2 */
262 ------------------------------------------------------------------------------
264 ------------------------------------------------------------------------------
265 INPUT AND OUTPUT DEFINITIONS
268 L_var3 = 32 bit long signed integer (Word32) whose value falls
269 in the range : 0x8000 0000 <= L_var3 <= 0x7fff ffff.
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.
276 pOverflow = pointer to overflow (Flag)
279 pOverflow -> 1 if the 32 bit operation resulted in overflow
282 result = 32-bit result of L_var3 - (var1 * var2)
285 static inline Word32 L_msu(Word32 L_var3, Word16 var1, Word16 var2, Flag *pOverflow)
289 result = L_mult(var1, var2, pOverflow);
290 result = L_sub(L_var3, result, pOverflow);
296 ------------------------------------------------------------------------------
297 FUNCTION NAME: Mpy_32
298 ------------------------------------------------------------------------------
299 INPUT AND OUTPUT DEFINITIONS
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).
307 pOverflow = pointer to overflow (Flag)
310 pOverflow -> 1 if the 32 bit multiply operation resulted in overflow
313 L_product = 32-bit product of L_var1 and L_var2 (Word32)
315 static inline Word32 Mpy_32(Word16 L_var1_hi,
325 OSCL_UNUSED_ARG(pOverflow);
326 L_product = (Word32) L_var1_hi * L_var2_hi;
328 if (L_product != (Word32) 0x40000000L)
337 /* result = mult (L_var1_hi, L_var2_lo, pOverflow); */
338 product32 = ((Word32) L_var1_hi * L_var2_lo) >> 15;
340 /* L_product = L_mac (L_product, result, 1, pOverflow); */
341 L_sum = L_product + (product32 << 1);
343 if ((L_product ^ product32) > 0)
345 if ((L_sum ^ L_product) >> 31)
347 L_sum = (L_product >> 31) ? MIN_32 : MAX_32;
353 /* result = mult (L_var1_lo, L_var2_hi, pOverflow); */
354 product32 = ((Word32) L_var1_lo * L_var2_hi) >> 15;
356 /* L_product = L_mac (L_product, result, 1, pOverflow); */
357 L_sum = L_product + (product32 << 1);
359 if ((L_product ^ product32) > 0)
361 if ((L_sum ^ L_product) >> 31)
363 L_sum = (L_product >> 31) ? MIN_32 : MAX_32;
370 ------------------------------------------------------------------------------
371 FUNCTION NAME: Mpy_32_16
372 ------------------------------------------------------------------------------
373 INPUT AND OUTPUT DEFINITIONS
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).
380 pOverflow = pointer to overflow (Flag)
383 pOverflow -> 1 if the 32 bit product operation resulted in overflow
386 product = 32-bit product of the 32-bit L_var1 and 16-bit var1 (Word32)
389 static inline Word32 Mpy_32_16(Word16 L_var1_hi,
398 L_product = (Word32) L_var1_hi * var2;
400 if (L_product != (Word32) 0x40000000L)
410 result = ((Word32)L_var1_lo * var2) >> 15;
412 L_sum = L_product + (result << 1);
414 if ((L_product ^ result) > 0)
416 if ((L_sum ^ L_product) >> 31)
418 L_sum = (L_product >> 31) ? MIN_32 : MAX_32;
427 ------------------------------------------------------------------------------
429 ------------------------------------------------------------------------------
430 INPUT AND OUTPUT DEFINITIONS
433 var1 = 16 bit short signed integer (Word16) whose value falls in
434 the range : 0xffff 8000 <= var1 <= 0x0000 7fff.
436 var2 = 16 bit short signed integer (Word16) whose value falls in
437 the range : 0xffff 8000 <= var2 <= 0x0000 7fff.
439 pOverflow = pointer to overflow (Flag)
442 pOverflow -> 1 if the add operation resulted in overflow
445 product = 16-bit limited product of var1 and var2 (Word16)
447 static inline Word16 mult(Word16 var1, Word16 var2, Flag *pOverflow)
449 register Word32 product;
451 product = ((Word32) var1 * var2) >> 15;
453 /* Saturate result (if necessary). */
454 /* var1 * var2 >0x00007fff is the only case */
455 /* that saturation occurs. */
457 if (product > 0x00007fffL)
460 product = (Word32) MAX_16;
464 /* Return the product as a 16 bit value by type casting Word32 to Word16 */
466 return ((Word16) product);
470 static inline Word32 amrnb_fxp_mac_16_by_16bb(Word32 L_var1, Word32 L_var2, Word32 L_var3)
474 result = L_var3 + L_var1 * L_var2;
479 static inline Word32 amrnb_fxp_msu_16_by_16bb(Word32 L_var1, Word32 L_var2, Word32 L_var3)
483 result = L_var3 - L_var1 * L_var2;
489 /*----------------------------------------------------------------------------
491 ----------------------------------------------------------------------------*/
496 #endif /* BASIC_OP_C_EQUIVALENT_H */