norm_s.cpp revision 2d0ac425564ff9882ebaac5267d1a04d4af67d00
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/norm_s.c
32
33------------------------------------------------------------------------------
34 REVISION HISTORY
35
36 Description: Created separate file for the norm_s function. Sync'ed up
37          with the current template and fixed tabs.
38
39 Description: Updated input/output definition and module description to
40          be the same as the equivalent assembly file (norm_s.asm).
41
42 Description: Updated definition of var1 to be the same as that in the
43          assembly file (norm_s.asm).
44
45 Description: Removed conditional code that updates WMOPS counter
46
47 Who:                       Date:
48 Description:
49
50------------------------------------------------------------------------------
51 INPUT AND OUTPUT DEFINITIONS
52
53 Inputs:
54    var1 = 16 bit signed integer of type Word16, whose value falls
55           in the range: 0x8000 <= var1 <= 0x7fff
56
57 Local Stores/Buffers/Pointers Needed:
58    None
59
60 Global Stores/Buffers/Pointers Needed:
61    None
62
63 Outputs:
64    var_out = number of left shifts need to normalize var1 (Word16)
65
66 Pointers and Buffers Modified:
67    None
68
69 Local Stores Modified:
70    None
71
72 Global Stores Modified:
73    None
74
75------------------------------------------------------------------------------
76 FUNCTION DESCRIPTION
77
78 This function produces the number of left shifts needed to normalize the 16
79 bit variable var1 for positive values on the interval with minimum of 0x4000
80 and maximum of 0x7fff, and for negative values on the interval with minimum
81 of 0x8000 and maximum of 0xc000. Note that when var1 is zero, the resulting
82 output var_out is set to zero.
83
84------------------------------------------------------------------------------
85 REQUIREMENTS
86
87 None
88
89------------------------------------------------------------------------------
90 REFERENCES
91
92 [1] basicop2.c, ETS Version 2.0.0, February 8, 1999
93
94------------------------------------------------------------------------------
95 PSEUDO-CODE
96
97Word16 norm_s (Word16 var1)
98{
99    Word16 var_out;
100
101    if (var1 == 0)
102    {
103        var_out = 0;
104    }
105    else
106    {
107        if (var1 == (Word16) 0xffff)
108        {
109            var_out = 15;
110        }
111        else
112        {
113            if (var1 < 0)
114            {
115                var1 = ~var1;
116            }
117            for (var_out = 0; var1 < 0x4000; var_out++)
118            {
119                var1 <<= 1;
120            }
121        }
122    }
123
124#if (WMOPS)
125    multiCounter[currCounter].norm_s++;
126#endif
127    return (var_out);
128}
129
130------------------------------------------------------------------------------
131 RESOURCES USED
132   When the code is written for a specific target processor the
133     the resources used should be documented below.
134
135 STACK USAGE: [stack count for this module] + [variable to represent
136          stack usage for each subroutine called]
137
138     where: [stack usage variable] = stack usage for [subroutine
139         name] (see [filename].ext)
140
141 DATA MEMORY USED: x words
142
143 PROGRAM MEMORY USED: x words
144
145 CLOCK CYCLES: [cycle count equation for this module] + [variable
146           used to represent cycle count for each subroutine
147           called]
148
149     where: [cycle count variable] = cycle count for [subroutine
150        name] (see [filename].ext)
151
152------------------------------------------------------------------------------
153*/
154
155
156/*----------------------------------------------------------------------------
157; INCLUDES
158----------------------------------------------------------------------------*/
159#include    "basic_op.h"
160
161/*----------------------------------------------------------------------------
162; MACROS
163; Define module specific macros here
164----------------------------------------------------------------------------*/
165
166/*----------------------------------------------------------------------------
167; DEFINES
168; Include all pre-processor statements here. Include conditional
169; compile variables also.
170----------------------------------------------------------------------------*/
171
172/*----------------------------------------------------------------------------
173; LOCAL FUNCTION DEFINITIONS
174; Function Prototype declaration
175----------------------------------------------------------------------------*/
176
177/*----------------------------------------------------------------------------
178; LOCAL STORE/BUFFER/POINTER DEFINITIONS
179; Variable declaration - defined here and used outside this module
180----------------------------------------------------------------------------*/
181
182/*----------------------------------------------------------------------------
183; EXTERNAL FUNCTION REFERENCES
184; Declare functions defined elsewhere and referenced in this module
185----------------------------------------------------------------------------*/
186
187/*----------------------------------------------------------------------------
188; EXTERNAL GLOBAL STORE/BUFFER/POINTER REFERENCES
189; Declare variables used in this module but defined elsewhere
190----------------------------------------------------------------------------*/
191
192/*----------------------------------------------------------------------------
193; FUNCTION CODE
194----------------------------------------------------------------------------*/
195#if !( defined(PV_ARM_V5) || defined(PV_ARM_GCC_V5) )
196
197Word16 norm_s(Word16 var1)
198{
199    /*----------------------------------------------------------------------------
200    ; Define all local variables
201    ----------------------------------------------------------------------------*/
202
203    Word16 var_out = 0;
204
205    /*----------------------------------------------------------------------------
206    ; Function body here
207    ----------------------------------------------------------------------------*/
208
209    if (var1)
210    {
211        Word16 y = var1 - (var1 < 0);
212        var1 = y ^(y >> 15);
213
214        while (!(0x4000 & var1))
215        {
216            var_out++;
217            if ((0x2000 & var1))
218            {
219                break;
220            }
221            var_out++;
222            if ((0x1000 & var1))
223            {
224                break;
225            }
226            var_out++;
227            if ((0x0800 & var1))
228            {
229                break;
230            }
231            var_out++;
232            var1 <<= 4;
233        }
234    }
235
236    /*----------------------------------------------------------------------------
237    ; Return nothing or data or data pointer
238    ----------------------------------------------------------------------------*/
239    return (var_out);
240}
241
242#endif
243