summaryrefslogtreecommitdiffstats
path: root/arm-wt-22k/lib_src/eas_flog.c
blob: 16539f54a4710890a71daf4d31662c26b76ff2ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*----------------------------------------------------------------------------
 *
 * File:
 * eas_flog2.c
 *
 * Contents and purpose:
 * Fixed point square root
 *
 *
 * Copyright (c) 2006 Sonic Network Inc.

 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 *----------------------------------------------------------------------------
 * Revision Control:
 *   $Revision$
 *   $Date$
 *----------------------------------------------------------------------------
*/

#include "eas_types.h"
#include "eas_math.h"

#define MANTISSA_SHIFT          27
#define MANTISSA_MASK           0x0000000f
#define MANTISSA_LSB_SHIFT      7
#define MANTISSA_LSB_MASK       0x000fffff
#define LOG_EXPONENT_SHIFT      10
#define INTERPOLATION_SHIFT     20
#define MAX_NEGATIVE            (-2147483647-1)

/* log lookup table */
static const EAS_U16 eas_log2_table[] =
{
    0, 90, 174, 254, 330, 402, 470, 536,
    599, 659, 717, 773, 827, 879, 929, 977,
    1024
};

/*----------------------------------------------------------------------------
 * EAS_flog2()
 *----------------------------------------------------------------------------
 * Purpose:
 * Calculates the log2 of a 32-bit fixed point value
 *
 * Inputs:
 * n = value of interest
 *
 * Outputs:
 * returns the log2 of n
 *
 *----------------------------------------------------------------------------
*/
EAS_I32 EAS_flog2 (EAS_U32 n)
{
    EAS_U32 exp;
    EAS_U32 interp;

    /* check for error condition */
    if (n == 0)
        return MAX_NEGATIVE;

    /* find exponent */
    for (exp = 31; exp > 0; exp--)
    {
        /* shift until we get a 1 bit in bit 31 */
        if ((n & (EAS_U32) MAX_NEGATIVE) != 0)
            break;
        n <<= 1;
    }
    /*lint -e{701} use shift for performance */
    exp <<= LOG_EXPONENT_SHIFT;

    /* get the least significant bits for interpolation */
    interp = (n >> MANTISSA_LSB_SHIFT) & MANTISSA_LSB_MASK;

    /* get the most significant bits for mantissa lookup */
    n = (n >> MANTISSA_SHIFT) & MANTISSA_MASK;

    /* interpolate mantissa */
    interp = ((eas_log2_table[n+1] - eas_log2_table[n]) * interp) >> INTERPOLATION_SHIFT;
    exp += eas_log2_table[n] + interp;

    return (EAS_I32) exp;
}