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