OSDN Git Service

original
[gb-231r1-is01/Gingerbread_2.3.3_r1_IS01.git] / frameworks / base / media / libstagefright / codecs / aacdec / pv_pow2.cpp
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  Filename: pv_pow2.c
21
22
23 ------------------------------------------------------------------------------
24  REVISION HISTORY
25
26
27  Who:                                   Date: MM/DD/YYYY
28  Description:
29
30 ------------------------------------------------------------------------------
31  INPUT AND OUTPUT DEFINITIONS
32
33 Input
34     Int32 x             32-bit integer input  Q27
35
36 Output
37     Int32               32-bit integer in Q25
38
39
40 ------------------------------------------------------------------------------
41  FUNCTION DESCRIPTION
42
43     Implement the power base 2 for positive numbers lesser than 5.999999
44 ------------------------------------------------------------------------------
45  REQUIREMENTS
46
47
48 ------------------------------------------------------------------------------
49  REFERENCES
50
51 ------------------------------------------------------------------------------
52  PSEUDO-CODE
53
54 ------------------------------------------------------------------------------
55 */
56 #ifdef AAC_PLUS
57
58 /*----------------------------------------------------------------------------
59 ; INCLUDES
60 ----------------------------------------------------------------------------*/
61 #include "pv_pow2.h"
62 #include "fxp_mul32.h"
63
64 /*----------------------------------------------------------------------------
65 ; MACROS
66 ; Define module specific macros here
67 ----------------------------------------------------------------------------*/
68
69
70 /*----------------------------------------------------------------------------
71 ; DEFINES
72 ; Include all pre-processor statements here. Include conditional
73 ; compile variables also.
74 ----------------------------------------------------------------------------*/
75
76 /*----------------------------------------------------------------------------
77 ; LOCAL FUNCTION DEFINITIONS
78 ; Function Prototype declaration
79 ----------------------------------------------------------------------------*/
80 #define POW_2_TABLE_LENGTH          6
81 #define POW_2_TABLE_LENGTH_m_2      (POW_2_TABLE_LENGTH - 2)
82
83 /*----------------------------------------------------------------------------
84 ; LOCAL STORE/BUFFER/POINTER DEFINITIONS
85 ; Variable declaration - defined here and used outside this module
86 ----------------------------------------------------------------------------*/
87
88 #define R_SHIFT     29
89 #define Q_fmt(x)   (Int32)(x*((Int32)1<<R_SHIFT) + (x>=0?0.5F:-0.5F))
90
91 #define Q27fmt(x)   (Int32)(x*((Int32)1<<27) + (x>=0?0.5F:-0.5F))
92
93 const Int32 pow2_table[6] =
94 {
95     Q_fmt(0.00224510927441F),   Q_fmt(0.00777943379416F),
96     Q_fmt(0.05737929218747F),   Q_fmt(0.23918017179889F),
97     Q_fmt(0.69345251849351F),   Q_fmt(0.99996347120248F)
98 };
99
100
101 /*----------------------------------------------------------------------------
102 ; EXTERNAL FUNCTION REFERENCES
103 ; Declare functions defined elsewhere and referenced in this module
104 ----------------------------------------------------------------------------*/
105
106 /*----------------------------------------------------------------------------
107 ; EXTERNAL GLOBAL STORE/BUFFER/POINTER REFERENCES
108 ; Declare variables used in this module but defined elsewhere
109 ----------------------------------------------------------------------------*/
110
111 /*----------------------------------------------------------------------------
112 ; FUNCTION CODE
113 ----------------------------------------------------------------------------*/
114
115
116 /*
117  *      z in Q27 format
118  */
119
120 Int32 pv_pow2(Int32 z)
121 {
122     const Int32 *pt_table = pow2_table;
123     Int32 multiplier = 0;
124     Int32 shift_factor;
125     Int32 i;
126     Int32 v_q;
127     Int32 y;
128
129
130     if (z > Q27fmt(1.0f))
131     {
132         v_q = z - (z & 0xF8000000);
133         shift_factor =   z >> 27;
134     }
135     else
136     {
137         v_q = z;
138         shift_factor = 0;
139     }
140
141     if (v_q < Q27fmt(0.5f))
142     {
143         v_q += Q27fmt(0.5f);
144         multiplier = Q_fmt(0.70710678118655F);
145     }
146
147     v_q = v_q << 2;
148
149     y  = fxp_mul32_Q29(*(pt_table++), v_q);
150
151     for (i = POW_2_TABLE_LENGTH_m_2; i != 0; i--)
152     {
153         y += *(pt_table++);
154         y  = fxp_mul32_Q29(y, v_q);
155     }
156     y += *(pt_table++);
157
158     if (multiplier)
159     {
160         y = fxp_mul32_Q29(y, multiplier);
161     }
162
163     /*
164      *  returns number on Q25
165      */
166     return (y >> (4 - shift_factor));
167
168 }
169
170 #endif