1/*
2 ** Copyright 2003-2010, VisualOn, Inc.
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 express or implied.
13 ** See the License for the specific language governing permissions and
14 ** limitations under the License.
15 */
16
17/***********************************************************************
18*                                                                      *
19*      File             : log2.c                                       *
20*      Purpose          : Computes log2(L_x)                           *
21*                                                                      *
22************************************************************************/
23
24#include "log2.h"
25/********************************************************************************
26*                         INCLUDE FILES
27*********************************************************************************/
28#include "typedef.h"
29#include "basic_op.h"
30
31/*********************************************************************************
32*                         LOCAL VARIABLES AND TABLES
33**********************************************************************************/
34#include "log2_tab.h"     /* Table for Log2() */
35
36/*************************************************************************
37*
38*   FUNCTION:   Log2_norm()
39*
40*   PURPOSE:   Computes log2(L_x, exp),  where   L_x is positive and
41*              normalized, and exp is the normalisation exponent
42*              If L_x is negative or zero, the result is 0.
43*
44*   DESCRIPTION:
45*        The function Log2(L_x) is approximated by a table and linear
46*        interpolation. The following steps are used to compute Log2(L_x)
47*
48*           1- exponent = 30-norm_exponent
49*           2- i = bit25-b31 of L_x;  32<=i<=63  (because of normalization).
50*           3- a = bit10-b24
51*           4- i -=32
52*           5- fraction = table[i]<<16 - (table[i] - table[i+1]) * a * 2
53*
54*************************************************************************/
55
56void Log2_norm (
57        Word32 L_x,         /* (i) : input value (normalized)                    */
58        Word16 exp,         /* (i) : norm_l (L_x)                                */
59        Word16 *exponent,   /* (o) : Integer part of Log2.   (range: 0<=val<=30) */
60        Word16 *fraction    /* (o) : Fractional part of Log2. (range: 0<=val<1)  */
61           )
62{
63    Word16 i, a, tmp;
64    Word32 L_y;
65    if (L_x <= (Word32) 0)
66    {
67        *exponent = 0;
68        *fraction = 0;
69        return;
70    }
71    *exponent = (30 - exp);
72    L_x = (L_x >> 9);
73    i = extract_h (L_x);                /* Extract b25-b31 */
74    L_x = (L_x >> 1);
75    a = (Word16)(L_x);                /* Extract b10-b24 of fraction */
76    a = (Word16)(a & (Word16)0x7fff);
77    i -= 32;
78    L_y = L_deposit_h (table[i]);       /* table[i] << 16        */
79    tmp = vo_sub(table[i], table[i + 1]); /* table[i] - table[i+1] */
80    L_y = vo_L_msu (L_y, tmp, a);          /* L_y -= tmp*a*2        */
81    *fraction = extract_h (L_y);
82
83    return;
84}
85
86/*************************************************************************
87*
88*   FUNCTION:   Log2()
89*
90*   PURPOSE:   Computes log2(L_x),  where   L_x is positive.
91*              If L_x is negative or zero, the result is 0.
92*
93*   DESCRIPTION:
94*        normalizes L_x and then calls Log2_norm().
95*
96*************************************************************************/
97
98void Log2 (
99        Word32 L_x,         /* (i) : input value                                 */
100        Word16 *exponent,   /* (o) : Integer part of Log2.   (range: 0<=val<=30) */
101        Word16 *fraction    /* (o) : Fractional part of Log2. (range: 0<=val<1) */
102      )
103{
104    Word16 exp;
105
106    exp = norm_l(L_x);
107    Log2_norm ((L_x << exp), exp, exponent, fraction);
108}
109
110
111
112