1 /* ------------------------------------------------------------------
2 * Copyright (C) 1998-2009 PacketVideo
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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
14 * See the License for the specific language governing permissions
15 * and limitations under the License.
16 * -------------------------------------------------------------------
18 /****************************************************************************************
19 Portions of this file are derived from the following 3GPP standard:
22 ANSI-C code for the Adaptive Multi-Rate (AMR) speech codec
23 Available from http://www.3gpp.org
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 ****************************************************************************************/
31 Filename: log2_norm.cpp
33 ------------------------------------------------------------------------------
36 /*----------------------------------------------------------------------------
38 ----------------------------------------------------------------------------*/
39 #include "log2_norm.h"
41 /*----------------------------------------------------------------------------
43 ; Define module specific macros here
44 ----------------------------------------------------------------------------*/
47 /*----------------------------------------------------------------------------
49 ; Include all pre-processor statements here. Include conditional
50 ; compile variables also.
51 ----------------------------------------------------------------------------*/
53 /*----------------------------------------------------------------------------
54 ; LOCAL FUNCTION DEFINITIONS
55 ; Function Prototype declaration
56 ----------------------------------------------------------------------------*/
58 /*----------------------------------------------------------------------------
59 ; LOCAL STORE/BUFFER/POINTER DEFINITIONS
60 ; Variable declaration - defined here and used outside this module
61 ----------------------------------------------------------------------------*/
65 ------------------------------------------------------------------------------
66 FUNCTION NAME: Log2_norm
67 ------------------------------------------------------------------------------
68 INPUT AND OUTPUT DEFINITIONS
71 L_x = normalized input value of type Word32
72 exp = number of shifts required to normalize L_x; it is of type Word16
73 exponent = pointer to the integer part of Log2 (of type Word16)
74 whose valid range is: 0 <= value <= 30
75 fraction = pointer to the fractional part of Log2 (of type Word16)
76 whose valid range is: 0 <= value < 1
79 exponent points to the newly calculated integer part of Log2
80 fraction points to the newly calculated fractional part of Log2
85 Global Variables Used:
88 Local Variables Needed:
89 table = Log2 table of constants of type Word16
91 ------------------------------------------------------------------------------
94 The function Log2(L_x) calculates the logarithm of the normalized input
95 buffer L_x. The logarithm is approximated by a table and linear
96 interpolation. The following steps are used to compute Log2(L_x):
98 1. exponent = 30 - norm_exponent
99 2. i = bit25-b31 of L_x; 32<=i<=63 (because of normalization).
102 5. fraction = table[i]<<16 - (table[i] - table[i+1]) * a * 2
104 ------------------------------------------------------------------------------
109 ------------------------------------------------------------------------------
112 log2.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
114 ------------------------------------------------------------------------------
118 Word32 L_x, // (i) : input value (normalized)
119 Word16 exp, // (i) : norm_l (L_x)
120 Word16 *exponent, // (o) : Integer part of Log2. (range: 0<=val<=30)
121 Word16 *fraction // (o) : Fractional part of Log2. (range: 0<=val<1)
127 if (L_x <= (Word32) 0)
134 *exponent = sub (30, exp);
136 L_x = L_shr (L_x, 9);
137 i = extract_h (L_x); // Extract b25-b31
138 L_x = L_shr (L_x, 1);
139 a = extract_l (L_x); // Extract b10-b24 of fraction
140 a = a & (Word16) 0x7fff;
144 L_y = L_deposit_h (table[i]); // table[i] << 16
145 tmp = sub (table[i], table[i + 1]); // table[i] - table[i+1]
146 L_y = L_msu (L_y, tmp, a); // L_y -= tmp*a*2
148 *fraction = extract_h (L_y);
153 ------------------------------------------------------------------------------
155 [State any special notes, constraints or cautions for users of this function]
157 ------------------------------------------------------------------------------
161 Word32 L_x, /* (i) : input value (normalized) */
162 Word16 exp, /* (i) : norm_l (L_x) */
163 Word16 *exponent, /* (o) : Integer part of Log2. (range: 0<=val<=30)*/
164 Word16 *fraction /* (o) : Fractional part of Log2. (range: 0<=val<1) */
170 if (L_x <= (Word32) 0)
177 /* Calculate exponent portion of Log2 */
178 *exponent = 30 - exp;
180 /* At this point, L_x > 0 */
181 /* Shift L_x to the right by 10 to extract bits 10-31, */
182 /* which is needed to calculate fractional part of Log2 */
184 i = (Word16)(L_x >> 15); /* Extract b25-b31 */
185 a = L_x & 0x7fff; /* Extract b10-b24 of fraction */
187 /* Calculate table index -> subtract by 32 is done for */
188 /* proper table indexing, since 32<=i<=63 (due to normalization) */
191 /* Fraction part of Log2 is approximated by using table[] */
192 /* and linear interpolation, i.e., */
193 /* fraction = table[i]<<16 - (table[i] - table[i+1]) * a * 2 */
194 L_y = (Word32) log2_tbl[i] << 16; /* table[i] << 16 */
195 tmp = log2_tbl[i] - log2_tbl[i + 1]; /* table[i] - table[i+1] */
196 L_y -= (((Word32) tmp) * a) << 1; /* L_y -= tmp*a*2 */
198 *fraction = (Word16)(L_y >> 16);