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/****************************************************************************************
19Portions 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)
26Permission to distribute, modify and use this file under the standard license
27terms listed above has been obtained from the copyright holder.
28****************************************************************************************/
29/*
30 Filename: /audio/gsm_amr/c/src/l_shr_r.c
31
32------------------------------------------------------------------------------
33 REVISION HISTORY
34
35 Description: Created separate file for the L_shr_r function. Sync'ed up
36          with the current template and fixed tabs.
37
38 Description: Updated template. Changed function interface to pass in a
39              pointer to overflow flag into the function instead of using a
40              global flag. Removed code that updates MOPS counter. Changed
41              function return value name from "L_var_out" to "result".
42
43 Who:                       Date:
44 Description:
45
46------------------------------------------------------------------------------
47*/
48
49
50/*----------------------------------------------------------------------------
51; INCLUDES
52----------------------------------------------------------------------------*/
53#include    "basic_op.h"
54
55/*----------------------------------------------------------------------------
56; MACROS
57; [Define module specific macros here]
58----------------------------------------------------------------------------*/
59
60/*----------------------------------------------------------------------------
61; DEFINES
62; [Include all pre-processor statements here. Include conditional
63; compile variables also.]
64----------------------------------------------------------------------------*/
65
66/*----------------------------------------------------------------------------
67; LOCAL FUNCTION DEFINITIONS
68; [List function prototypes here]
69----------------------------------------------------------------------------*/
70
71/*----------------------------------------------------------------------------
72; LOCAL VARIABLE DEFINITIONS
73; [Variable declaration - defined here and used outside this module]
74----------------------------------------------------------------------------*/
75
76
77/*
78------------------------------------------------------------------------------
79 FUNCTION NAME: L_shr_r
80------------------------------------------------------------------------------
81 INPUT AND OUTPUT DEFINITIONS
82
83 Inputs:
84    L_var1 = 32 bit long signed integer (Word32 ) whose value falls
85             in the range : 0x8000 0000 <= L_var1 <= 0x7fff ffff.
86    var2 = 16 bit short signed integer (Word16) whose value falls in
87           the range : 0xffff 8000 <= var2 <= 0x0000 7fff.
88
89    pOverflow = pointer to overflow (Flag)
90
91 Outputs:
92    pOverflow -> 1 if the 32 bit shift operation resulted in overflow
93
94 Returns:
95    result = Shifted result w/ rounding (Word32)
96
97 Global Variables Used:
98    None
99
100 Local Variables Needed:
101    None
102
103------------------------------------------------------------------------------
104 FUNCTION DESCRIPTION
105
106 This function arithmetically shifts the 32 bit input L_var1 right var2
107 positions with rounding. If var2 is negative, the function
108 arithmetically shifts L_var1 left by -var2 and zero fills the -var2 LSB of
109 the result. The result is saturated in case of underflows or overflows, i.e.,
110
111 - If var2 is greater than zero :
112    if (L_sub(L_shl(L_shr(L_var1,var2),1),L_shr(L_var1,sub(var2,1))))
113    is equal to zero
114    then
115        L_shr_r(L_var1,var2) = L_shr(L_var1,var2)
116    else
117        L_shr_r(L_var1,var2) = L_add(L_shr(L_var1,var2),1)
118 - If var2 is less than or equal to zero :
119    L_shr_r(L_var1,var2) = L_shr(L_var1,var2).
120
121------------------------------------------------------------------------------
122 REQUIREMENTS
123
124 None
125
126------------------------------------------------------------------------------
127 REFERENCES
128
129 [1] L_shr_r() function in basic_op2.c,  UMTS GSM AMR speech codec, R99 -
130 Version 3.2.0, March 2, 2001
131
132------------------------------------------------------------------------------
133 PSEUDO-CODE
134
135Word32 L_shr_r (Word32 L_var1, Word16 var2)
136{
137    Word32 L_var_out;
138
139* The reference ETSI code uses a global flag for Overflow. In the actual
140* implementation a pointer to Overflow flag is passed in as a parameter to the
141* function L_shr()
142
143    if (var2 > 31)
144    {
145        L_var_out = 0;
146    }
147    else
148    {
149        L_var_out = L_shr (L_var1, var2);
150#if (WMOPS)
151        multiCounter[currCounter].L_shr--;
152#endif
153        if (var2 > 0)
154        {
155            if ((L_var1 & ((Word32) 1 << (var2 - 1))) != 0)
156            {
157                L_var_out++;
158            }
159        }
160    }
161#if (WMOPS)
162    multiCounter[currCounter].L_shr_r++;
163#endif
164    return (L_var_out);
165}
166
167------------------------------------------------------------------------------
168 RESOURCES USED [optional]
169
170 When the code is written for a specific target processor the
171 the resources used should be documented below.
172
173 HEAP MEMORY USED: x bytes
174
175 STACK MEMORY USED: x bytes
176
177 CLOCK CYCLES: (cycle count equation for this function) + (variable
178                used to represent cycle count for each subroutine
179                called)
180     where: (cycle count variable) = cycle count for [subroutine
181                                     name]
182
183------------------------------------------------------------------------------
184 CAUTION [optional]
185 [State any special notes, constraints or cautions for users of this function]
186
187------------------------------------------------------------------------------
188*/
189
190/*----------------------------------------------------------------------------
191; FUNCTION CODE
192----------------------------------------------------------------------------*/
193Word32 L_shr_r(register Word32 L_var1, register Word16 var2, Flag *pOverflow)
194{
195    Word32 result;
196
197    if (var2 > 31)
198    {
199        result = 0;
200    }
201    else
202    {
203        result = L_shr(L_var1, var2, pOverflow);
204
205        if (var2 > 0)
206        {
207            if ((L_var1 & ((Word32) 1 << (var2 - 1))) != 0)
208            {
209                result++;
210            }
211        }
212    }
213    return (result);
214}
215