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 ****************************************************************************************/
33 ------------------------------------------------------------------------------
35 Shift right function with overflow control
36 ------------------------------------------------------------------------------
39 /*----------------------------------------------------------------------------
41 ----------------------------------------------------------------------------*/
44 /*----------------------------------------------------------------------------
46 ; [Define module specific macros here]
47 ----------------------------------------------------------------------------*/
49 /*----------------------------------------------------------------------------
51 ; [Include all pre-processor statements here. Include conditional
52 ; compile variables also.]
53 ----------------------------------------------------------------------------*/
55 /*----------------------------------------------------------------------------
56 ; LOCAL FUNCTION DEFINITIONS
57 ; [List function prototypes here]
58 ----------------------------------------------------------------------------*/
60 /*----------------------------------------------------------------------------
61 ; LOCAL VARIABLE DEFINITIONS
62 ; [Variable declaration - defined here and used outside this module]
63 ----------------------------------------------------------------------------*/
67 ------------------------------------------------------------------------------
69 ------------------------------------------------------------------------------
70 INPUT AND OUTPUT DEFINITIONS
73 var1 = 16 bit short signed integer (Word16) whose value falls in
74 the range : 0xffff 8000 <= var1 <= 0x0000 7fff.
76 var2 = 16 bit short signed integer (Word16) whose value falls in
77 the range : 0xffff 8000 <= var2 <= 0x0000 7fff.
79 pOverflow = pointer to overflow (Flag)
82 pOverflow -> 1 if the shift operation resulted in overflow
85 product = Shifted result limited to 16 bits (Word16)
87 Global Variables Used:
90 Local Variables Needed:
93 ------------------------------------------------------------------------------
96 This function arithmetically shifts the 16 bit input var1 right var2 positions
97 with sign extension. If var2 is negative, arithmetically shift var1 left by
98 -var2 with sign extension. Saturate the result in case of underflows or
101 ------------------------------------------------------------------------------
104 ------------------------------------------------------------------------------
107 [1] shr() function in basic_op2.c, UMTS GSM AMR speech codec, R99 -
108 Version 3.2.0, March 2, 2001
110 ------------------------------------------------------------------------------
113 Word16 shr_std (Word16 var1, Word16 var2)
121 var_out = shl_std (var1, -var2);
123 mult_stdiCounter[currCounter].shl_std--;
130 var_out = (var1 < 0) ? -1 : 0;
136 var_out = ~((~var1) >> var2);
140 var_out = var1 >> var2;
146 mult_stdiCounter[currCounter].shr_std++;
150 ------------------------------------------------------------------------------
152 [State any special notes, constraints or cautions for users of this function]
154 ------------------------------------------------------------------------------
157 /*----------------------------------------------------------------------------
159 ----------------------------------------------------------------------------*/
160 OSCL_EXPORT_REF Word16 shr(register Word16 var1, register Word16 var2, Flag *pOverflow)
162 register Word16 result;
173 result = var1 >> var2;
178 var2 = -var2; /* Shift right negative is equivalent */
185 result = (var1 << var2);
186 if ((result >> var2) != var1)
189 result = ((var1 > 0) ? MAX_16 : MIN_16);