1 /* ------------------------------------------------------------------
2 * Copyright (C) 1998-2010 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 - Wideband (AMR-WB) speech codec
23 Available from http://www.3gpp.org
25 (C) 2007, 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 ****************************************************************************************/
30 ------------------------------------------------------------------------------
34 Pathname: ./src/pvamrwbdecoder_basic_op_cequivalent.h
36 ------------------------------------------------------------------------------
39 ------------------------------------------------------------------------------
41 #ifndef PVAMRWBDECODER_BASIC_OP_CEQUIVALENT_H
42 #define PVAMRWBDECODER_BASIC_OP_CEQUIVALENT_H
50 #include "normalize_amr_wb.h"
52 #if defined(C_EQUIVALENT)
55 /*----------------------------------------------------------------------------
57 Function Name : add_int16
61 Performs the addition (var1+var2) with overflow control and saturation;
62 the 16 bit result is set at +32767 when overflow occurs or at -32768
63 when underflow occurs.
67 16 bit short signed integer (int16) whose value falls in the
68 range : 0xffff 8000 <= var1 <= 0x0000 7fff.
71 16 bit short signed integer (int16) whose value falls in the
72 range : 0xffff 8000 <= var1 <= 0x0000 7fff.
78 16 bit short signed integer (int16) whose value falls in the
79 range : 0xffff 8000 <= var_out <= 0x0000 7fff.
81 ----------------------------------------------------------------------------*/
82 static inline int16 add_int16(int16 var1, int16 var2)
86 L_sum = (int32) var1 + var2;
87 if ((L_sum >> 15) != (L_sum >> 31))
89 L_sum = (L_sum >> 31) ^ MAX_16;
91 return ((int16)(L_sum));
95 /*----------------------------------------------------------------------------
97 Function Name : sub_int16
99 Performs the subtraction (var1+var2) with overflow control and satu-
100 ration; the 16 bit result is set at +32767 when overflow occurs or at
101 -32768 when underflow occurs.
106 16 bit short signed integer (int16) whose value falls in the
107 range : 0xffff 8000 <= var1 <= 0x0000 7fff.
110 16 bit short signed integer (int16) whose value falls in the
111 range : 0xffff 8000 <= var1 <= 0x0000 7fff.
117 16 bit short signed integer (int16) whose value falls in the
118 range : 0xffff 8000 <= var_out <= 0x0000 7fff.
120 ----------------------------------------------------------------------------*/
121 static inline int16 sub_int16(int16 var1, int16 var2)
125 L_diff = (int32) var1 - var2;
126 if ((L_diff >> 15) != (L_diff >> 31))
128 L_diff = (L_diff >> 31) ^ MAX_16;
130 return ((int16)(L_diff));
134 /*----------------------------------------------------------------------------
136 Function Name : mult_int16
138 Performs the multiplication of var1 by var2 and gives a 16 bit result
139 which is scaled i.e.:
140 mult_int16(var1,var2) = extract_l(L_shr((var1 times var2),15)) and
141 mult_int16(-32768,-32768) = 32767.
145 16 bit short signed integer (int16) whose value falls in the
146 range : 0xffff 8000 <= var1 <= 0x0000 7fff.
149 16 bit short signed integer (int16) whose value falls in the
150 range : 0xffff 8000 <= var1 <= 0x0000 7fff.
154 16 bit short signed integer (int16) whose value falls in the
155 range : 0xffff 8000 <= var_out <= 0x0000 7fff.
157 ----------------------------------------------------------------------------*/
159 static inline int16 mult_int16(int16 var1, int16 var2)
163 L_product = ((int32) var1 * (int32) var2) >> 15;
165 if ((L_product >> 15) != (L_product >> 31))
167 L_product = (L_product >> 31) ^ MAX_16;
170 return ((int16)L_product);
174 /*----------------------------------------------------------------------------
176 Function Name : add_int32
178 32 bits addition of the two 32 bits variables (L_var1+L_var2) with
179 overflow control and saturation; the result is set at +2147483647 when
180 overflow occurs or at -2147483648 when underflow occurs.
184 L_var1 32 bit long signed integer (int32) whose value falls in the
185 range : 0x8000 0000 <= L_var3 <= 0x7fff ffff.
187 L_var2 32 bit long signed integer (int32) whose value falls in the
188 range : 0x8000 0000 <= L_var3 <= 0x7fff ffff.
193 32 bit long signed integer (int32) whose value falls in the
194 range : 0x8000 0000 <= L_var_out <= 0x7fff ffff.
196 ----------------------------------------------------------------------------*/
199 static inline int32 add_int32(int32 L_var1, int32 L_var2)
203 L_var_out = L_var1 + L_var2;
205 if (((L_var1 ^ L_var2) & MIN_32) == 0) /* same sign ? */
207 if ((L_var_out ^ L_var1) & MIN_32) /* addition matches sign ? */
209 L_var_out = (L_var1 >> 31) ^ MAX_32;
218 /*----------------------------------------------------------------------------
220 Function Name : sub_int32
222 32 bits subtraction of the two 32 bits variables (L_var1-L_var2) with
223 overflow control and saturation; the result is set at +2147483647 when
224 overflow occurs or at -2147483648 when underflow occurs.
228 L_var1 32 bit long signed integer (int32) whose value falls in the
229 range : 0x8000 0000 <= L_var3 <= 0x7fff ffff.
231 L_var2 32 bit long signed integer (int32) whose value falls in the
232 range : 0x8000 0000 <= L_var3 <= 0x7fff ffff.
237 32 bit long signed integer (int32) whose value falls in the
238 range : 0x8000 0000 <= L_var_out <= 0x7fff ffff.
240 ----------------------------------------------------------------------------*/
243 static inline int32 sub_int32(int32 L_var1, int32 L_var2)
247 L_var_out = L_var1 - L_var2;
249 if (((L_var1 ^ L_var2) & MIN_32) != 0) /* different sign ? */
251 if ((L_var_out ^ L_var1) & MIN_32) /* difference matches sign ? */
253 L_var_out = (L_var1 >> 31) ^ MAX_32;
261 /*----------------------------------------------------------------------------
263 Function Name : mac_16by16_to_int32
265 Multiply var1 by var2 and shift the result left by 1. Add the 32 bit
266 result to L_var3 with saturation, return a 32 bit result:
267 L_mac(L_var3,var1,var2) = L_add(L_var3,L_mult(var1,var2)).
271 L_var3 32 bit long signed integer (int32) whose value falls in the
272 range : 0x8000 0000 <= L_var3 <= 0x7fff ffff.
275 16 bit short signed integer (int16) whose value falls in the
276 range : 0xffff 8000 <= var1 <= 0x0000 7fff.
279 16 bit short signed integer (int16) whose value falls in the
280 range : 0xffff 8000 <= var1 <= 0x0000 7fff.
284 32 bit long signed integer (int32) whose value falls in the
285 range : 0x8000 0000 <= L_var_out <= 0x7fff ffff.
287 ----------------------------------------------------------------------------*/
290 static inline int32 mac_16by16_to_int32(int32 L_var3, int16 var1, int16 var2)
295 L_mul = ((int32) var1 * (int32) var2);
297 if (L_mul != 0x40000000)
303 L_mul = MAX_32; /* saturation */
306 L_var_out = L_var3 + L_mul;
308 if (((L_mul ^ L_var3) & MIN_32) == 0) /* same sign ? */
310 if ((L_var_out ^ L_var3) & MIN_32) /* addition matches sign ? */
312 L_var_out = (L_var3 >> 31) ^ MAX_32;
321 /*----------------------------------------------------------------------------
323 Function Name : msu_16by16_from_int32
325 Multiply var1 by var2 and shift the result left by 1. Subtract the 32 bit
326 result to L_var3 with saturation, return a 32 bit result:
327 L_msu(L_var3,var1,var2) = L_sub(L_var3,L_mult(var1,var2)).
331 L_var3 32 bit long signed integer (int32) whose value falls in the
332 range : 0x8000 0000 <= L_var3 <= 0x7fff ffff.
335 16 bit short signed integer (int16) whose value falls in the
336 range : 0xffff 8000 <= var1 <= 0x0000 7fff.
339 16 bit short signed integer (int16) whose value falls in the
340 range : 0xffff 8000 <= var1 <= 0x0000 7fff.
344 32 bit long signed integer (int32) whose value falls in the
345 range : 0x8000 0000 <= L_var_out <= 0x7fff ffff.
347 ----------------------------------------------------------------------------*/
349 static inline int32 msu_16by16_from_int32(int32 L_var3, int16 var1, int16 var2)
354 L_mul = ((int32) var1 * (int32) var2);
356 if (L_mul != 0x40000000)
362 L_mul = MAX_32; /* saturation */
365 L_var_out = L_var3 - L_mul;
367 if (((L_mul ^ L_var3) & MIN_32) != 0) /* different sign ? */
369 if ((L_var_out ^ L_var3) & MIN_32) /* difference matches sign ? */
371 L_var_out = (L_var3 >> 31) ^ MAX_32;
379 /*----------------------------------------------------------------------------
381 Function Name : mul_16by16_to_int32
383 mul_16by16_to_int32 is the 32 bit result of the multiplication of var1
384 times var2 with one shift left i.e.:
385 L_mult(var1,var2) = L_shl((var1 times var2),1) and
386 L_mult(-32768,-32768) = 2147483647.
390 16 bit short signed integer (int16) whose value falls in the
391 range : 0xffff 8000 <= var1 <= 0x0000 7fff.
394 16 bit short signed integer (int16) whose value falls in the
395 range : 0xffff 8000 <= var1 <= 0x0000 7fff.
398 32 bit long signed integer (int32) whose value falls in the
399 range : 0x8000 0000 <= L_var_out <= 0x7fff ffff.
401 ----------------------------------------------------------------------------*/
404 static inline int32 mul_16by16_to_int32(int16 var1, int16 var2)
408 L_mul = ((int32) var1 * (int32) var2);
410 if (L_mul != 0x40000000)
416 L_mul = MAX_32; /* saturation */
423 /*----------------------------------------------------------------------------
425 Function Name : amr_wb_round
427 Round the lower 16 bits of the 32 bit input number into the MS 16 bits
428 with saturation. Shift the resulting bits right by 16 and return the 16
430 round(L_var1) = extract_h(L_add(L_var1,32768))
434 32 bit long signed integer (int32 ) whose value falls in the
435 range : 0x8000 0000 <= L_var1 <= 0x7fff ffff.
438 16 bit short signed integer (int16) whose value falls in the
439 range : 0xffff 8000 <= var_out <= 0x0000 7fff.
441 ----------------------------------------------------------------------------*/
442 static inline int16 amr_wb_round(int32 L_var1)
444 if (L_var1 != MAX_32)
446 L_var1 += 0x00008000L;
448 return ((int16)(L_var1 >> 16));
452 /*----------------------------------------------------------------------------
454 Function Name : amr_wb_shl1_round
456 Shift the 32 bit input number to the left by 1, round up the result and
458 amr_wb_shl1_round(L_var1) = round(L_shl(L_var1,1))
462 32 bit long signed integer (int32 ) whose value falls in the
463 range : 0x8000 0000 <= L_var1 <= 0x7fff ffff.
466 16 bit short signed integer (int16) whose value falls in the
467 range : 0xffff 8000 <= var_out <= 0x0000 7fff.
469 ----------------------------------------------------------------------------*/
470 static inline int16 amr_wb_shl1_round(int32 L_var1)
474 if ((L_var1 << 1) >> 1 == L_var1)
476 var_out = (int16)((L_var1 + 0x00004000) >> 15);
480 var_out = (int16)(((L_var1 >> 31) ^ MAX_32) >> 16);
486 /*----------------------------------------------------------------------------
487 Function Name : mul_32by16
489 Multiply a 16 bit integer by a 32 bit (DPF). The result is divided
492 L_32 = (hi1*lo2)<<1 + ((lo1*lo2)>>15)<<1
496 hi hi part of 32 bit number.
497 lo lo part of 32 bit number.
500 ----------------------------------------------------------------------------*/
503 static inline int32 mul_32by16(int16 hi, int16 lo, int16 n)
505 return (((((int32)hi*n)) + ((((int32)lo*n) >> 15))) << 1);
508 static inline int32 fxp_mac_16by16(int16 var1, int16 var2, int32 L_add)
511 L_add += (int32)var1 * var2;
516 static inline int32 fxp_mul_16by16(int16 var1, const int16 var2)
518 int32 L_mul = (int32)var1 * var2;
523 static inline int32 fxp_mul32_by_16b(int32 L_var1, const int32 L_var2)
525 int32 L_mul = (int32)(((int64)L_var1 * (L_var2 << 16)) >> 32);
537 #endif /* PVAMRWBDECODER_BASIC_OP_CEQUIVALENT_H */