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 
31  Filename: /audio/gsm_amr/c/src/pow2.c
32 
33 ------------------------------------------------------------------------------
34  REVISION HISTORY
35 
36  Description: Updated template. Changed function interface to pass in a
37               pointer to overflow flag into the function instead of using a
38               global flag. Removed inclusion of "pow2.tab"
39 
40  Who:                           Date:
41  Description:
42 
43 ------------------------------------------------------------------------------
44 */
45 
46 /*----------------------------------------------------------------------------
47 ; INCLUDES
48 ----------------------------------------------------------------------------*/
49 #include    "pow2.h"
50 #include    "basic_op.h"
51 
52 /*----------------------------------------------------------------------------
53 ; MACROS
54 ; Define module specific macros here
55 ----------------------------------------------------------------------------*/
56 
57 
58 /*----------------------------------------------------------------------------
59 ; DEFINES
60 ; Include all pre-processor statements here. Include conditional
61 ; compile variables also.
62 ----------------------------------------------------------------------------*/
63 
64 /*----------------------------------------------------------------------------
65 ; LOCAL FUNCTION DEFINITIONS
66 ; Function Prototype declaration
67 ----------------------------------------------------------------------------*/
68 
69 /*----------------------------------------------------------------------------
70 ; LOCAL STORE/BUFFER/POINTER DEFINITIONS
71 ; Variable declaration - defined here and used outside this module
72 ----------------------------------------------------------------------------*/
73 
74 
75 /*
76 ------------------------------------------------------------------------------
77  FUNCTION NAME: Pow2
78 ------------------------------------------------------------------------------
79  INPUT AND OUTPUT DEFINITIONS
80 
81  Inputs:
82     exponent = Integer part whose valid range is: 0 <= value <= 30 (Word16)
83     fraction = Fractional part whose valid range is 0 <= value < 1
84 
85     pOverflow = pointer to overflow flag
86 
87  Outputs:
88     L_x = Result of the Pow2() computation (Word32)
89     pOverflow -> 1 if the Pow2() function results in saturation
90 
91  Returns:
92     None
93 
94  Global Variables Used:
95     None
96 
97  Local Variables Needed:
98     None
99 
100 ------------------------------------------------------------------------------
101  FUNCTION DESCRIPTION
102 
103  This function computes  L_x = pow(2.0, exponent.fraction)
104 
105  The function Pow2(L_x) is approximated by a table and linear interpolation.
106 
107  1- i = bit10-b15 of fraction,   0 <= i <= 31
108  2- a = bit0-b9   of fraction
109  3- L_x = table[i]<<16 - (table[i] - table[i+1]) * a * 2
110  4- L_x = L_x >> (30-exponent)     (with rounding)
111 
112 ------------------------------------------------------------------------------
113  REQUIREMENTS
114 
115  None
116 
117 ------------------------------------------------------------------------------
118  REFERENCES
119 
120  pow2.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
121 
122 ------------------------------------------------------------------------------
123  PSEUDO-CODE
124 
125 Word32 Pow2 (           // (o)  : result       (range: 0<=val<=0x7fffffff)
126     Word16 exponent,    // (i)  : Integer part.      (range: 0<=val<=30)
127     Word16 fraction     // (i)  : Fractional part.  (range: 0.0<=val<1.0)
128 )
129 {
130     Word16 exp, i, a, tmp;
131     Word32 L_x;
132 
133     L_x = L_mult (fraction, 32);        // L_x = fraction<<6
134     i = extract_h (L_x);                // Extract b10-b16 of fraction
135     L_x = L_shr (L_x, 1);
136     a = extract_l (L_x);                // Extract b0-b9   of fraction
137     a = a & (Word16) 0x7fff;
138 
139     L_x = L_deposit_h (table[i]);       // table[i] << 16
140     tmp = sub (table[i], table[i + 1]); // table[i] - table[i+1]
141     L_x = L_msu (L_x, tmp, a);          // L_x -= tmp*a*2
142 
143     exp = sub (30, exponent);
144     L_x = L_shr_r (L_x, exp);
145 
146     return (L_x);
147 }
148 
149 ------------------------------------------------------------------------------
150  RESOURCES USED [optional]
151 
152  When the code is written for a specific target processor the
153  the resources used should be documented below.
154 
155  HEAP MEMORY USED: x bytes
156 
157  STACK MEMORY USED: x bytes
158 
159  CLOCK CYCLES: (cycle count equation for this function) + (variable
160                 used to represent cycle count for each subroutine
161                 called)
162      where: (cycle count variable) = cycle count for [subroutine
163                                      name]
164 
165 ------------------------------------------------------------------------------
166  CAUTION [optional]
167  [State any special notes, constraints or cautions for users of this function]
168 
169 ------------------------------------------------------------------------------
170 */
171 
172 /*----------------------------------------------------------------------------
173 ; FUNCTION CODE
174 ----------------------------------------------------------------------------*/
175 
Pow2(Word16 exponent,Word16 fraction,Flag * pOverflow)176 Word32 Pow2(            /* (o)  : result       (range: 0<=val<=0x7fffffff) */
177     Word16 exponent,    /* (i)  : Integer part.      (range: 0<=val<=30)   */
178     Word16 fraction,    /* (i)  : Fractional part.  (range: 0.0<=val<1.0)  */
179     Flag *pOverflow
180 )
181 {
182     Word16 exp, i, a, tmp;
183     Word32 L_x;
184 
185     L_x = L_mult(fraction, 32, pOverflow);      /* L_x = fraction<<6    */
186 
187     /* Extract b0-b16 of fraction */
188 
189     i = ((Word16)(L_x >> 16)) & 31;             /* ensure index i is bounded */
190     a = (Word16)((L_x >> 1) & 0x7fff);
191 
192     L_x = L_deposit_h(pow2_tbl[i]);             /* pow2_tbl[i] << 16       */
193 
194     /* pow2_tbl[i] - pow2_tbl[i+1] */
195     tmp = sub(pow2_tbl[i], pow2_tbl[i + 1], pOverflow);
196     L_x = L_msu(L_x, tmp, a, pOverflow);        /* L_x -= tmp*a*2        */
197 
198     exp = sub(30, exponent, pOverflow);
199     L_x = L_shr_r(L_x, exp, pOverflow);
200 
201     return (L_x);
202 }
203