/*
***********************************************************************
*
* Copyright (C) 1999 Ericsson Radio Systems AB
* 
* The Association of Radio Industries and Businesses (ARIB) has the
* licence to reproduce, distribute and publish the Bit Exact C Code
* programs in this CD-ROM together with the other parts of the RCR
* STD-27 (Personal Digital Cellular Telecommunication System)
* specification.
* 
* Ericsson has declared Option 1 (royalty-free licence) to ARIB for
* its Essential Industrial Property Rights related to the ACELP
* specification and the Bit Exact C Code programs. The royalty-free
* licence is limited to the development and manufacturing of
* products complying with the RCR STD-27H specification (or
* subsequent versions) for sale and use in Japan. Copying of the
* program(s) for any other purpose requires the prior written
* permission from Ericsson.
* 
* The user of the Bit Exact C Code hereby acknowledges the
* following:
* 
* 1.  Ericsson does not provide the user with any warranties,
*     whether express or implied, including but not limited to
*     warranties of merchantability and fitness for a particular
*     purpose relating to the Bit Exact C Code.
* 
* 2.  Ericsson shall have no responsibility whatsoever to indemnify
*     the user against actions or claims for infringement of any
*     intellectual property rights, e.g. patents, copyrights,
*     registered designs or any other rights, by reason of the
*     user's use of the Bit Exact C Code.
* 
***********************************************************************
*/

/*************************************************************************
 *
 *   FUNCTION:  Pow2()
 *
 *   PURPOSE: computes  L_x = pow(2.0, exponent.fraction)
 *
 *   DESCRIPTION:
 *       The function Pow2(L_x) is approximated by a table and linear
 *       interpolation.
 *   1- i = bit10-b15 of fraction,   0 <= i <= 31
 *   2- a = bit0-b9   of fraction
 *   3- L_x = table[i]<<16 - (table[i] - table[i+1]) * a * 2
 *   4- L_x = L_x >> (30-exponent)     (with rounding)
 *
 *************************************************************************/

#include "typedef.h"
#include "basic_op.h"

#include "pow2.tab"     /* Table for Pow2() */


Word32 Pow2(          /* (o)   : result       (range: 0<=val<=0x7fffffff) */
    Word16 exponent,  /* (i)   : Integer part.      (range: 0<=val<=30)   */
    Word16 fraction   /* (i)   : Fractionnal part.  (range: 0.0<=val<1.0) */
)
{
    Word16 exp, i, a, tmp;
    Word32 L_x;

    L_x = L_mult(fraction, 32); /* L_x = fraction<<6           */
    i = extract_h(L_x);         /* Extract b10-b16 of fraction */
    L_x = L_shr(L_x, 1);
    a = extract_l(L_x);         /* Extract b0-b9   of fraction */
    a = a & (Word16) 0x7fff;

    L_x = L_deposit_h(table[i]);/* table[i] << 16        */
    tmp = sub(table[i], table[i + 1]);  /* table[i] - table[i+1] */
    L_x = L_msu(L_x, tmp, a);   /* L_x -= tmp*a*2        */

    exp = sub(30, exponent);
    L_x = L_shr_r(L_x, exp);

    return (L_x);
}
