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------------------------------------------------------------------------------
20   PacketVideo Corp.
21   MP3 Decoder Library
22
23   Filename: pvmp3_normalize.cpp
24
25     Date: 10/02/2007
26
27------------------------------------------------------------------------------
28 REVISION HISTORY
29
30
31 Description:
32
33------------------------------------------------------------------------------
34 INPUT AND OUTPUT DEFINITIONS
35
36Input
37    Int32 x             32-bit integer non-zero input
38Returns
39    Int32 i             number of leading zeros on x
40
41
42------------------------------------------------------------------------------
43 FUNCTION DESCRIPTION
44
45    Returns number of leading zeros on the non-zero input
46
47------------------------------------------------------------------------------
48 REQUIREMENTS
49
50
51------------------------------------------------------------------------------
52 REFERENCES
53
54------------------------------------------------------------------------------
55 PSEUDO-CODE
56
57------------------------------------------------------------------------------
58*/
59
60
61/*----------------------------------------------------------------------------
62; INCLUDES
63----------------------------------------------------------------------------*/
64
65#include "pvmp3_audio_type_defs.h"
66#include "pvmp3_normalize.h"
67
68/*----------------------------------------------------------------------------
69; MACROS
70; Define module specific macros here
71----------------------------------------------------------------------------*/
72
73
74/*----------------------------------------------------------------------------
75; DEFINES
76; Include all pre-processor statements here. Include conditional
77; compile variables also.
78----------------------------------------------------------------------------*/
79
80/*----------------------------------------------------------------------------
81; LOCAL FUNCTION DEFINITIONS
82; Function Prototype declaration
83----------------------------------------------------------------------------*/
84
85/*----------------------------------------------------------------------------
86; LOCAL STORE/BUFFER/POINTER DEFINITIONS
87; Variable declaration - defined here and used outside this module
88----------------------------------------------------------------------------*/
89
90/*----------------------------------------------------------------------------
91; EXTERNAL FUNCTION REFERENCES
92; Declare functions defined elsewhere and referenced in this module
93----------------------------------------------------------------------------*/
94
95/*----------------------------------------------------------------------------
96; EXTERNAL GLOBAL STORE/BUFFER/POINTER REFERENCES
97; Declare variables used in this module but defined elsewhere
98----------------------------------------------------------------------------*/
99
100/*----------------------------------------------------------------------------
101; FUNCTION CODE
102----------------------------------------------------------------------------*/
103
104#if (defined(PV_ARM_V5)||defined(PV_ARM_V4))
105#elif (defined(PV_ARM_GCC_V5)||defined(PV_ARM_GCC_V4))
106
107
108/* function is inlined in header file */
109
110
111#else
112
113int32 pvmp3_normalize(int32 x)
114{
115    /*----------------------------------------------------------------------------
116    ; Define all local variables
117    ----------------------------------------------------------------------------*/
118    int32 i;
119
120
121    if (x > 0x0FFFFFFF)
122    {
123        i = 0;  /* most likely case */
124    }
125    else if (x > 0x00FFFFFF)
126    {
127        i = 3;  /* second most likely case */
128    }
129    else if (x > 0x0000FFFF)
130    {
131        i  = x > 0x000FFFFF ?  7 :  11;
132    }
133    else
134    {
135        if (x > 0x000000FF)
136        {
137            i  = x > 0x00000FFF ?  15 :  19;
138        }
139        else
140        {
141            i  = x > 0x0000000F ?  23 :  27;
142        }
143    }
144
145
146    x <<= i;
147
148    switch (x & 0x78000000)
149    {
150        case 0x08000000:
151            i += 3;
152            break;
153
154        case 0x18000000:
155        case 0x10000000:
156            i += 2;
157            break;
158        case 0x28000000:
159        case 0x20000000:
160        case 0x38000000:
161        case 0x30000000:
162            i++;
163
164        default:
165            ;
166    }
167
168    return i;
169
170}
171
172#endif
173
174