tizen 2.3.1 release
[external/opencore-amr.git] / opencore / codecs_v2 / audio / gsm_amr / amr_nb / common / src / norm_s.cpp
1 /* ------------------------------------------------------------------
2  * Copyright (C) 1998-2010 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
31  Filename: norm_s.cpp
32
33 ------------------------------------------------------------------------------
34  INPUT AND OUTPUT DEFINITIONS
35
36  Inputs:
37     var1 = 16 bit signed integer of type Word16, whose value falls
38            in the range: 0x8000 <= var1 <= 0x7fff
39
40  Local Stores/Buffers/Pointers Needed:
41     None
42
43  Global Stores/Buffers/Pointers Needed:
44     None
45
46  Outputs:
47     var_out = number of left shifts need to normalize var1 (Word16)
48
49  Pointers and Buffers Modified:
50     None
51
52  Local Stores Modified:
53     None
54
55  Global Stores Modified:
56     None
57
58 ------------------------------------------------------------------------------
59  FUNCTION DESCRIPTION
60
61  This function produces the number of left shifts needed to normalize the 16
62  bit variable var1 for positive values on the interval with minimum of 0x4000
63  and maximum of 0x7fff, and for negative values on the interval with minimum
64  of 0x8000 and maximum of 0xc000. Note that when var1 is zero, the resulting
65  output var_out is set to zero.
66
67 ------------------------------------------------------------------------------
68  REQUIREMENTS
69
70  None
71
72 ------------------------------------------------------------------------------
73  REFERENCES
74
75  [1] basicop2.c, ETS Version 2.0.0, February 8, 1999
76
77 ------------------------------------------------------------------------------
78  PSEUDO-CODE
79
80 Word16 norm_s (Word16 var1)
81 {
82     Word16 var_out;
83
84     if (var1 == 0)
85     {
86         var_out = 0;
87     }
88     else
89     {
90         if (var1 == (Word16) 0xffff)
91         {
92             var_out = 15;
93         }
94         else
95         {
96             if (var1 < 0)
97             {
98                 var1 = ~var1;
99             }
100             for (var_out = 0; var1 < 0x4000; var_out++)
101             {
102                 var1 <<= 1;
103             }
104         }
105     }
106
107 #if (WMOPS)
108     multiCounter[currCounter].norm_s++;
109 #endif
110     return (var_out);
111 }
112
113 ------------------------------------------------------------------------------
114 */
115
116
117 /*----------------------------------------------------------------------------
118 ; INCLUDES
119 ----------------------------------------------------------------------------*/
120 #include    "basic_op.h"
121
122 /*----------------------------------------------------------------------------
123 ; MACROS
124 ; Define module specific macros here
125 ----------------------------------------------------------------------------*/
126
127 /*----------------------------------------------------------------------------
128 ; DEFINES
129 ; Include all pre-processor statements here. Include conditional
130 ; compile variables also.
131 ----------------------------------------------------------------------------*/
132
133 /*----------------------------------------------------------------------------
134 ; LOCAL FUNCTION DEFINITIONS
135 ; Function Prototype declaration
136 ----------------------------------------------------------------------------*/
137
138 /*----------------------------------------------------------------------------
139 ; LOCAL STORE/BUFFER/POINTER DEFINITIONS
140 ; Variable declaration - defined here and used outside this module
141 ----------------------------------------------------------------------------*/
142
143 /*----------------------------------------------------------------------------
144 ; EXTERNAL FUNCTION REFERENCES
145 ; Declare functions defined elsewhere and referenced in this module
146 ----------------------------------------------------------------------------*/
147
148 /*----------------------------------------------------------------------------
149 ; EXTERNAL GLOBAL STORE/BUFFER/POINTER REFERENCES
150 ; Declare variables used in this module but defined elsewhere
151 ----------------------------------------------------------------------------*/
152
153 /*----------------------------------------------------------------------------
154 ; FUNCTION CODE
155 ----------------------------------------------------------------------------*/
156 #if !((PV_CPU_ARCH_VERSION >=5) && ((PV_COMPILER == EPV_ARM_GNUC) || (PV_COMPILER == EPV_ARM_RVCT)))
157
158 OSCL_EXPORT_REF Word16 norm_s(register Word16 var1)
159 {
160     /*----------------------------------------------------------------------------
161     ; Define all local variables
162     ----------------------------------------------------------------------------*/
163
164     register Word16 var_out = 0;
165
166     /*----------------------------------------------------------------------------
167     ; Function body here
168     ----------------------------------------------------------------------------*/
169
170     if (var1)
171     {
172         Word16 y = var1 - (var1 < 0);
173         var1 = y ^(y >> 15);
174
175         while (!(0x4000 & var1))
176         {
177             var_out++;
178             if ((0x2000 & var1))
179             {
180                 break;
181             }
182             var_out++;
183             if ((0x1000 & var1))
184             {
185                 break;
186             }
187             var_out++;
188             if ((0x0800 & var1))
189             {
190                 break;
191             }
192             var_out++;
193             var1 <<= 4;
194         }
195     }
196
197     /*----------------------------------------------------------------------------
198     ; Return nothing or data or data pointer
199     ----------------------------------------------------------------------------*/
200     return (var_out);
201 }
202
203 #endif