shr_r.cpp revision 4f1efc098cb5791c3e9f483f2af84aef70d2d0a0
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
31 Pathname: ./gsm-amr/c/src/shr_r.c
32
33------------------------------------------------------------------------------
34 REVISION HISTORY
35
36 Description: Created separate file for the shr_r function. Sync'ed up
37          with the current template and fixed tabs.
38
39 Description: Passing around pOverflow as per EPOC changes.
40
41 Who:                           Date:
42 Description:
43
44------------------------------------------------------------------------------
45 INPUT AND OUTPUT DEFINITIONS
46
47 Inputs:
48    var1 = 16 bit short signed integer (Word16) whose value falls in
49           the range : 0xffff 8000 <= var1 <= 0x0000 7fff.
50    var2 = 16 bit short signed integer (Word16) whose value falls in
51           the range : 0xffff 8000 <= var2 <= 0x0000 7fff.
52
53 Local Stores/Buffers/Pointers Needed:
54    None
55
56 Global Stores/Buffers/Pointers Needed:
57    None
58
59 Outputs:
60    var_out = shifted input w/ rounding (Word16)
61
62 Pointers and Buffers Modified:
63    None
64
65 Local Stores Modified:
66    None
67
68 Global Stores Modified:
69    None
70
71------------------------------------------------------------------------------
72 FUNCTION DESCRIPTION
73
74 This function arithmetically shifts the 16 bit input var1 right var2 positions
75 with rounding. If var2 is negative, arithmetically shift var1 left by
76 -var2 with rounding. Saturate the result in case of underflows or
77 overflows.
78
79    - If var2 is greater than zero :
80        if (sub(shl(shr(var1,var2),1),shr(var1,sub(var2,1))))
81        is equal to zero
82        then
83            shr_r(var1,var2) = shr(var1,var2)
84        else
85            shr_r(var1,var2) = add(shr(var1,var2),1)
86    - If var2 is less than or equal to zero :
87        shr_r(var1,var2) = shr(var1,var2).
88
89------------------------------------------------------------------------------
90 REQUIREMENTS
91
92 None
93
94------------------------------------------------------------------------------
95 REFERENCES
96
97 [1] basicop2.c, ETS Version 2.0.0, February 8, 1999
98
99------------------------------------------------------------------------------
100 PSEUDO-CODE
101
102Word16 shr_r (Word16 var1, Word16 var2)
103{
104    Word16 var_out;
105
106    if (var2 > 15)
107    {
108        var_out = 0;
109    }
110    else
111    {
112        var_out = shr (var1, var2);
113#if (WMOPS)
114        multiCounter[currCounter].shr--;
115#endif
116
117        if (var2 > 0)
118        {
119            if ((var1 & ((Word16) 1 << (var2 - 1))) != 0)
120            {
121                var_out++;
122            }
123        }
124    }
125#if (WMOPS)
126    multiCounter[currCounter].shr_r++;
127#endif
128    return (var_out);
129}
130
131------------------------------------------------------------------------------
132 RESOURCES USED
133   When the code is written for a specific target processor the
134     the resources used should be documented below.
135
136 STACK USAGE: [stack count for this module] + [variable to represent
137          stack usage for each subroutine called]
138
139     where: [stack usage variable] = stack usage for [subroutine
140         name] (see [filename].ext)
141
142 DATA MEMORY USED: x words
143
144 PROGRAM MEMORY USED: x words
145
146 CLOCK CYCLES: [cycle count equation for this module] + [variable
147           used to represent cycle count for each subroutine
148           called]
149
150     where: [cycle count variable] = cycle count for [subroutine
151        name] (see [filename].ext)
152
153------------------------------------------------------------------------------
154*/
155
156
157/*----------------------------------------------------------------------------
158; INCLUDES
159----------------------------------------------------------------------------*/
160#include    "basic_op.h"
161
162/*----------------------------------------------------------------------------
163; MACROS
164; Define module specific macros here
165----------------------------------------------------------------------------*/
166
167/*----------------------------------------------------------------------------
168; DEFINES
169; Include all pre-processor statements here. Include conditional
170; compile variables also.
171----------------------------------------------------------------------------*/
172
173/*----------------------------------------------------------------------------
174; LOCAL FUNCTION DEFINITIONS
175; Function Prototype declaration
176----------------------------------------------------------------------------*/
177
178/*----------------------------------------------------------------------------
179; LOCAL STORE/BUFFER/POINTER DEFINITIONS
180; Variable declaration - defined here and used outside this module
181----------------------------------------------------------------------------*/
182
183/*----------------------------------------------------------------------------
184; EXTERNAL FUNCTION REFERENCES
185; Declare functions defined elsewhere and referenced in this module
186----------------------------------------------------------------------------*/
187
188/*----------------------------------------------------------------------------
189; EXTERNAL GLOBAL STORE/BUFFER/POINTER REFERENCES
190; Declare variables used in this module but defined elsewhere
191----------------------------------------------------------------------------*/
192
193/*----------------------------------------------------------------------------
194; FUNCTION CODE
195----------------------------------------------------------------------------*/
196Word16 shr_r(register Word16 var1, register Word16 var2, Flag *pOverflow)
197{
198    /*----------------------------------------------------------------------------
199    ; Define all local variables
200    ----------------------------------------------------------------------------*/
201    Word16 var_out;
202
203    /*----------------------------------------------------------------------------
204    ; Function body here
205    ----------------------------------------------------------------------------*/
206    if (var2 > 15)
207    {
208        var_out = 0;
209    }
210    else
211    {
212        var_out = shr(var1, var2, pOverflow);
213        if (var2 > 0)
214        {
215            if ((var1 & ((Word16) 1 << (var2 - 1))) != 0)
216            {
217                var_out++;
218            }
219        }
220    }
221
222    /*----------------------------------------------------------------------------
223    ; Return nothing or data or data pointer
224    ----------------------------------------------------------------------------*/
225    return (var_out);
226}
227