LCOV - code coverage report
Current view: top level - lib_com - basop_util.h (source / functions) Hit Total Coverage
Test: Coverage on main enc/dec/rend @ 3b2f07138c61dcf997bbf4165d0882f794b2995f Lines: 12 12 100.0 %
Date: 2025-05-03 01:55:50 Functions: 2 2 100.0 %

          Line data    Source code
       1             : /******************************************************************************************************
       2             : 
       3             :    (C) 2022-2025 IVAS codec Public Collaboration with portions copyright Dolby International AB, Ericsson AB,
       4             :    Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
       5             :    Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
       6             :    Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
       7             :    contributors to this repository. All Rights Reserved.
       8             : 
       9             :    This software is protected by copyright law and by international treaties.
      10             :    The IVAS codec Public Collaboration consisting of Dolby International AB, Ericsson AB,
      11             :    Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
      12             :    Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
      13             :    Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
      14             :    contributors to this repository retain full ownership rights in their respective contributions in
      15             :    the software. This notice grants no license of any kind, including but not limited to patent
      16             :    license, nor is any license granted by implication, estoppel or otherwise.
      17             : 
      18             :    Contributors are required to enter into the IVAS codec Public Collaboration agreement before making
      19             :    contributions.
      20             : 
      21             :    This software is provided "AS IS", without any express or implied warranties. The software is in the
      22             :    development stage. It is intended exclusively for experts who have experience with such software and
      23             :    solely for the purpose of inspection. All implied warranties of non-infringement, merchantability
      24             :    and fitness for a particular purpose are hereby disclaimed and excluded.
      25             : 
      26             :    Any dispute, controversy or claim arising under or in relation to providing this software shall be
      27             :    submitted to and settled by the final, binding jurisdiction of the courts of Munich, Germany in
      28             :    accordance with the laws of the Federal Republic of Germany excluding its conflict of law rules and
      29             :    the United Nations Convention on Contracts on the International Sales of Goods.
      30             : 
      31             : *******************************************************************************************************/
      32             : 
      33             : /*====================================================================================
      34             :     EVS Codec 3GPP TS26.443 Nov 04, 2021. Version 12.14.0 / 13.10.0 / 14.6.0 / 15.4.0 / 16.3.0
      35             :   ====================================================================================*/
      36             : 
      37             : #ifndef __BASOP_UTIL_H__
      38             : #define __BASOP_UTIL_H__
      39             : 
      40             : #include <stdint.h>
      41             : #include "options.h"
      42             : #include "basop_settings.h"
      43             : #include "typedef.h"
      44             : #include "basop32.h"
      45             : #include "math_32.h"
      46             : #include "log2.h"
      47             : 
      48             : #define LD_DATA_SCALE ( 6 )
      49             : 
      50             : #define LD_DATA_SHIFT_I5 ( 7 )
      51             : 
      52             : #define modDiv2( x ) sub( x, shl( shr( x, 1 ), 1 ) )
      53             : #define modDiv8( x ) L_sub( x, L_shl( L_shr( x, 3 ), 3 ) )
      54             : 
      55             : // basop_mpy.h
      56             : #define MUL_F( A, B ) Mpy_32_16_1( ( A ), ( B ) )
      57             : 
      58             : #ifndef CHEAP_NORM_SIZE
      59             : #define CHEAP_NORM_SIZE 161
      60             : #endif
      61             : 
      62        9300 : static __inline Word16 limitScale16( Word16 s )
      63             : {
      64             :     /* It is assumed, that s is calculated just before, therefore we can switch upon sign */
      65        9300 :     if ( s >= 0 )
      66        8808 :         s = s_min( s, WORD16_BITS - 1 );
      67        9300 :     if ( s < 0 )
      68         492 :         s = s_max( s, 1 - WORD16_BITS );
      69        9300 :     return ( s );
      70             : }
      71             : 
      72      675733 : static __inline Word16 limitScale32( Word16 s )
      73             : {
      74             :     /* It is assumed, that s is calculated just before, therefore we can switch upon sign */
      75      675733 :     if ( s >= 0 )
      76      672097 :         s = s_min( s, WORD32_BITS - 1 );
      77      675733 :     if ( s < 0 )
      78        3636 :         s = s_max( s, 1 - WORD32_BITS );
      79      675733 :     return ( s );
      80             : }
      81             : 
      82             : 
      83             : /*!**********************************************************************
      84             :    \brief   Add two values given by mantissa and exponent.
      85             : 
      86             :    Mantissas are in 16-bit-fractional format with values between 0 and 1. <br>
      87             :    The base for exponents is 2.  Example:  \f$  a = a\_m * 2^{a\_e}  \f$<br>
      88             : 
      89             : ************************************************************************/
      90             : Word16 BASOP_Util_Add_MantExp /*!< Exponent of result        */
      91             :     ( Word16 a_m,             /*!< Mantissa of 1st operand a */
      92             :       Word16 a_e,             /*!< Exponent of 1st operand a */
      93             :       Word16 b_m,             /*!< Mantissa of 2nd operand b */
      94             :       Word16 b_e,             /*!< Exponent of 2nd operand b */
      95             :       Word16 *ptrSum_m );     /*!< Mantissa of result */
      96             : 
      97             : /************************************************************************/
      98             : /*!
      99             :    \brief   Divide two values given by mantissa and exponent.
     100             : 
     101             :    Mantissas are in 16-bit-fractional format with values between 0 and 1. <br>
     102             :    The base for exponents is 2.  Example:  \f$  a = a\_m * 2^{a\_e}  \f$<br>
     103             : 
     104             :    For performance reasons, the division is based on a table lookup
     105             :    which limits accuracy.
     106             : */
     107             : void BASOP_Util_Divide_MantExp( Word16 a_m,          /*!< Mantissa of dividend a */
     108             :                                 Word16 a_e,          /*!< Exponent of dividend a */
     109             :                                 Word16 b_m,          /*!< Mantissa of divisor b */
     110             :                                 Word16 b_e,          /*!< Exponent of divisor b */
     111             :                                 Word16 *ptrResult_m, /*!< Mantissa of quotient a/b */
     112             :                                 Word16 *ptrResult_e  /*!< Exponent of quotient a/b */
     113             : );
     114             : 
     115             : /************************************************************************/
     116             : /*!
     117             :   \brief   Calculate the squareroot of a number given by mantissa and exponent
     118             : 
     119             :   Mantissa is in 16/32-bit-fractional format with values between 0 and 1. <br>
     120             :   For *norm versions mantissa has to be between 0.5 and 1. <br>
     121             :   The base for the exponent is 2.  Example:  \f$  a = a\_m * 2^{a\_e}  \f$<br>
     122             :   The exponent is addressed via pointers and will be overwritten with the result.
     123             : */
     124             : Word16 Sqrt16(                  /*!< output mantissa */
     125             :                Word16 mantissa, /*!< input mantissa */
     126             :                Word16 *exponent /*!< pointer to exponent */
     127             : );
     128             : 
     129             : Word16 Sqrt16norm(                  /*!< output mantissa */
     130             :                    Word16 mantissa, /*!< normalized input mantissa */
     131             :                    Word16 *exponent /*!< pointer to exponent */
     132             : );
     133             : 
     134             : Word32 Sqrt32(                  /*!< output mantissa */
     135             :                Word32 mantissa, /*!< input mantissa */
     136             :                Word16 *exponent /*!< pointer to exponent */
     137             : );
     138             : 
     139             : 
     140             : /* deprecated, use Sqrt16! */
     141             : void BASOP_Util_Sqrt_MantExp( Word16 *mantissa, /*!< Pointer to mantissa */
     142             :                               Word16 *exponent  /*!< Pointer to exponent */
     143             : );
     144             : 
     145             : /* deprecated, use Sqrt16norm! */
     146             : void BASOP_Util_Sqrt_MantExpNorm( Word16 *mantissa, /*!< Pointer to normalized mantissa */
     147             :                                   Word16 *exponent  /*!< Pointer to exponent */
     148             : );
     149             : 
     150             : /****************************************************************************/
     151             : /*!
     152             :   \brief   Calculate the inverse of the squareroot of a number given by mantissa and exponent
     153             : 
     154             :   Mantissa is in 16/32-bit-fractional format with values between 0 and 1. <br>
     155             :   For *norm versions mantissa has to be between 0.5 and 1. <br>
     156             :   The base for the exponent is 2.  Example:  \f$  a = a\_m * 2^{a\_e}  \f$<br>
     157             :   The exponent is addressed via pointers and will be overwritten with the result.
     158             : */
     159             : Word16 ISqrt16(                  /*!< output mantissa */
     160             :                 Word16 mantissa, /*!< input mantissa */
     161             :                 Word16 *exponent /*!< pointer to exponent */
     162             : );
     163             : 
     164             : Word32 ISqrt32(                  /*!< output mantissa */
     165             :                 Word32 mantissa, /*!< input mantissa */
     166             :                 Word16 *exponent /*!< pointer to exponent */
     167             : );
     168             : 
     169             : Word32 ISqrt32norm(                  /*!< output mantissa */
     170             :                     Word32 mantissa, /*!< normalized input mantissa */
     171             :                     Word16 *exponent /*!< pointer to exponent */
     172             : );
     173             : 
     174             : /* deprecated, use ISqrt16! */
     175             : void BASOP_Util_InvSqrt_MantExp( Word16 *mantissa, /*!< Pointer to mantissa */
     176             :                                  Word16 *exponent  /*!< Pointer to exponent */
     177             : );
     178             : 
     179             : /*****************************************************************************/
     180             : /*!
     181             :   \brief   Calculate the inverse of a number given by mantissa and exponent
     182             : 
     183             :   Mantissa is in 16-bit-fractional format with values between 0 and 1. <br>
     184             :   The base for the exponent is 2.  Example:  \f$  a = a\_m * 2^{a\_e}  \f$<br>
     185             :   The operand is addressed via pointers and will be overwritten with the result.
     186             : 
     187             :   The function uses a table lookup and a newton iteration.
     188             : */
     189             : Word16 Inv16(                  /*!< output mantissa */
     190             :               Word16 mantissa, /*!< input mantissa */
     191             :               Word16 *exponent /*!< pointer to exponent */
     192             : );
     193             : 
     194             : /******************************************************************************/
     195             : /*!
     196             :   \brief   Calculate the squareroot and inverse of squareroot of a number given by mantissa and exponent
     197             : 
     198             :   Mantissa is in 16-bit-fractional format with values between 0 and 1. <br>
     199             :   The base for the exponent is 2.  Example:  \f$  a = a\_m * 2^{a\_e}  \f$<br>
     200             : */
     201             : void BASOP_Util_Sqrt_InvSqrt_MantExp( Word16 mantissa,    /*!< mantissa */
     202             :                                       Word16 exponent,    /*!< expoinent */
     203             :                                       Word16 *sqrt_mant,  /*!< Pointer to sqrt mantissa */
     204             :                                       Word16 *sqrt_exp,   /*!< Pointer to sqrt exponent */
     205             :                                       Word16 *isqrt_mant, /*!< Pointer to 1/sqrt mantissa */
     206             :                                       Word16 *isqrt_exp   /*!< Pointer to 1/sqrt exponent */
     207             : );
     208             : 
     209             : /********************************************************************/
     210             : /*!
     211             :   \brief   Calculates the scalefactor needed to normalize input array
     212             : 
     213             :     The scalefactor needed to normalize the Word16 input array is returned <br>
     214             :     If the input array contains only '0', a scalefactor 0 is returned <br>
     215             :     Scaling factor is determined wrt a normalized target x: 16384 <= x <= 32767 for positive x <br>
     216             :     and   -32768 <= x <= -16384 for negative x
     217             : */
     218             : 
     219             : Word16 getScaleFactor16(                       /* o: measured headroom in range [0..15], 0 if all x[i] == 0 */
     220             :                          const Word16 *x,      /* i: array containing 16-bit data */
     221             :                          const Word16 len_x ); /* i: length of the array to scan  */
     222             : 
     223             : /********************************************************************/
     224             : /*!
     225             :   \brief   Calculates the scalefactor needed to normalize input array
     226             : 
     227             :     The scalefactor needed to normalize the Word32 input array is returned <br>
     228             :     If the input array contains only '0', a scalefactor 0 is returned <br>
     229             :     Scaling factor is determined wrt a normalized target x: 1073741824 <= x <= 2147483647 for positive x <br>
     230             :     and   -2147483648 <= x <= -1073741824 for negative x
     231             : */
     232             : 
     233             : /*! r: measured headroom in range [0..31], 0 if all x[i] == 0 */
     234             : Word16 getScaleFactor32(
     235             :     const Word32 *x,      /* i  : array containing 32-bit data */
     236             :     const Word16 len_x ); /* i  : length of the array to scan  */
     237             : 
     238             : Word16 getScaleFactor32_copy(                       /* o: measured headroom in range [0..31], 0 if all x[i] == 0 */
     239             :                               const Word32 *x,      /* i: array containing 32-bit data */
     240             :                               const Word32 len_x ); /* i: length of the array to scan  */
     241             : 
     242             : /**
     243             :  * \brief normalize mantissa and update the exponent accordingly.
     244             :  * \param mantissa the mantissa to be normalized
     245             :  * \param pexponent pointer to the exponent.
     246             :  * \return the normalized mantissa.
     247             :  */
     248             : Word16 normalize16( Word16 mantissa, Word16 *pexponent );
     249             : /****************************************************************************/
     250             : /*!
     251             :   \brief   Does fractional integer division of Word32 arg1 by Word16 arg2
     252             : 
     253             :   both input arguments may be positive or negative <br>
     254             :   the result is truncated to Word16
     255             : 
     256             :   \return fractional integer Word16 result of arg1/arg2
     257             : */
     258             : Word16 divide3216( Word32 x,   /*!< Numerator*/
     259             :                    Word16 y ); /*!< Denominator*/
     260             : 
     261             : 
     262             : /****************************************************************************/
     263             : /*!
     264             :   \brief   Does fractional integer division of Word16 arg1 by Word16 arg2
     265             : 
     266             :   both input arguments may be positive or negative <br>
     267             :   the result is truncated to Word16
     268             : 
     269             :   \return fractional integer Word16 result of arg1/arg2
     270             : */
     271             : Word16 divide1616( Word16 x,   /*!< Numerator*/
     272             :                    Word16 y ); /*!< Denominator*/
     273             : 
     274             : 
     275             : /****************************************************************************/
     276             : /*!
     277             :   \brief   Does fractional integer division of Word32 arg1 by Word32 arg2
     278             : 
     279             :    this function makes both the numerator and the denominator positive integers,
     280             :    and scales up both values to avoid losing the accuracy of the outcome
     281             :    too much
     282             : 
     283             :    WARNING: it should be arg1 < arg2 because of the maximum degree of scaling for the mantissa!
     284             : 
     285             :   \return fractional Word16 integer z = arg1(32bits)/arg2(32bits)
     286             : */
     287             : Word16 divide3232( Word32 x,   /*!< Numerator*/
     288             :                    Word32 y ); /*!< Denominator*/
     289             : 
     290             : 
     291             : /****************************************************************************/
     292             : /*!
     293             :   \brief   Does fractional integer division of UWord32 arg1 by UWord32 arg2
     294             : 
     295             :    This function ensures both the numerator  and the denominator are positive integers,
     296             :    and scales up both values to avoid losing the accuracy of the outcome
     297             :    too much.<br>
     298             : 
     299             :    CAUTION: Arg 3 is a Word16 pointer which will point to the scalefactor difference
     300             :    s_diff = sub(s2,s1), where s1 and s2 are the scalefactors of the arguments, which
     301             :    were shifted in order to e.g. preserve accuracy.
     302             :    I.e. the result has to be scaled due to shifting it
     303             :    s_diff to the right to obtain the real result of the division.
     304             : 
     305             :   \return fractional Word16 integer z = arg1(32bits)/arg2(32bits)
     306             : */
     307             : Word16 BASOP_Util_Divide3232_uu_1616_Scale( Word32 x,    /*!< i  : Numerator*/
     308             :                                             Word32 y,    /*!< i  : Denominator*/
     309             :                                             Word16 *s ); /*!< o  : Additional scalefactor difference*/
     310             : 
     311             : 
     312             : /****************************************************************************/
     313             : /*!
     314             :   \brief   Does fractional integer division of Word32 arg1 by Word32 arg2
     315             : 
     316             :    This function scales up both values to avoid losing the accuracy of the outcome
     317             :    too much.<br>
     318             : 
     319             :    CAUTION: Arg 3 is a Word16 pointer which will point to the scalefactor difference
     320             :    s_diff = sub(s2,s1), where s1 and s2 are the scalefactors of the arguments, which
     321             :    were shifted in order to e.g. preserve accuracy.
     322             :    I.e. the result has to be scaled due to shifting it
     323             :    s_diff to the right to obtain the real result of the division.
     324             : 
     325             :   \return fractional Word16 integer z = arg1(32bits)/arg2(32bits)
     326             : */
     327             : Word16 BASOP_Util_Divide3232_Scale( Word32 x,    /*!< i  : Numerator*/
     328             :                                     Word32 y,    /*!< i  : Denominator*/
     329             :                                     Word16 *s ); /*!< o  : Additional scalefactor difference*/
     330             : 
     331             : Word32 BASOP_Util_Divide3232_Scale_cadence( Word32 x,    /*!< i  : Numerator*/
     332             :                                             Word32 y,    /*!< i  : Denominator*/
     333             :                                             Word16 *s ); /*!< o  : Additional scalefactor difference*/
     334             : 
     335             : 
     336             : #ifdef DIV32_OPT_NEWTON
     337             : Word32 BASOP_Util_Divide3232_Scale_newton( Word32 x,    /*!< i  : Numerator*/
     338             :                                            Word32 y,    /*!< i  : Denominator*/
     339             :                                            Word16 *s ); /*!< o  : Additional scalefactor difference*/
     340             : #endif
     341             : 
     342             : 
     343             : /************************************************************************/
     344             : /*!
     345             :   \brief     Binary logarithm with 7 iterations
     346             : 
     347             :   \param     x
     348             : 
     349             :   \return log2(x)/64
     350             :  */
     351             : /************************************************************************/
     352             : Word32 BASOP_Util_Log2( Word32 x );
     353             : Word32 BASOP_Util_Log10( Word32 x, Word16 e );
     354             : Word32 BASOP_Util_Loge( Word32 x, Word16 e );
     355             : 
     356             : 
     357             : /****************************************************************************/
     358             : /*!
     359             :   \brief   Does fractional division of Word16 arg1 by Word16 arg2
     360             : 
     361             : 
     362             :   \return fractional Q15 Word16 z = arg1(Q15)/arg2(Q15)  with scaling s
     363             : */
     364             : Word16 BASOP_Util_Divide1616_Scale( Word16 x,    /* i  : Numerator*/
     365             :                                     Word16 y,    /* i  : Denominator*/
     366             :                                     Word16 *s ); /* o  : Additional scalefactor difference*/
     367             : 
     368             : /****************************************************************************/
     369             : /*!
     370             :   \brief   Does fractional integer division of Word32 arg1 by Word16 arg2
     371             : 
     372             : 
     373             :   \return fractional Word16 integer z = arg1(32bits)/arg2(16bits), z not normalized
     374             : */
     375             : Word16 BASOP_Util_Divide3216_Scale( Word32 x,    /*!< i  : Numerator  */
     376             :                                     Word16 y,    /*!< i  : Denominator*/
     377             :                                     Word16 *s ); /*!< o  : Additional scalefactor difference*/
     378             : 
     379             : 
     380             : /************************************************************************/
     381             : /*!
     382             :  * \brief    Binary logarithm with 5 iterations
     383             :  *
     384             :  * \param[i] val
     385             :  *
     386             :  * \return basop_log2(val)/128
     387             :  */
     388             : /************************************************************************/
     389             : Word32 BASOP_Util_log2_i5( Word32 val );
     390             : 
     391             : /************************************************************************/
     392             : /*!
     393             :   \brief     Binary power
     394             : 
     395             :   Date: 06-JULY-2012 Arthur Tritthart, IIS Fraunhofer Erlangen
     396             : 
     397             :   Version with 3 table lookup and 1 linear interpolations
     398             : 
     399             :   Algorithm: compute power of 2, argument x is in Q7.25 format
     400             :              result = 2^(x/64)
     401             :              We split exponent (x/64) into 5 components:
     402             :              integer part:      represented by b31..b25  (exp)
     403             :              fractional part 1: represented by b24..b20  (lookup1)
     404             :              fractional part 2: represented by b19..b15  (lookup2)
     405             :              fractional part 3: represented by b14..b10  (lookup3)
     406             :              fractional part 4: represented by b09..b00  (frac)
     407             :              => result = (lookup1*lookup2*(lookup3+C1*frac)<<3)>>exp
     408             : 
     409             :   Due to the fact, that all lookup values contain a factor 0.5
     410             :   the result has to be shifted by 3 to the right also.
     411             :   Table exp2_tab_long contains the log2 for 0 to 1.0 in steps
     412             :   of 1/32, table exp2w_tab_long the log2 for 0 to 1/32 in steps
     413             :   of 1/1024, table exp2x_tab_long the log2 for 0 to 1/1024 in
     414             :   steps of 1/32768. Since the 2-logarithm of very very small
     415             :   negative value is rather linear, we can use interpolation.
     416             : 
     417             :   Limitations:
     418             : 
     419             :   For x <= 0, the result is fractional positive
     420             :   For x > 0, the result is integer in range 1...7FFF.FFFF
     421             :   For x < -31/64, we have to clear the result
     422             :   For x = 0, the result is ~1.0 (0x7FFF.FFFF)
     423             :   For x >= 31/64, the result is 0x7FFF.FFFF
     424             : 
     425             :   \param  x
     426             : 
     427             :   \return pow(2,(x/64))
     428             :  */
     429             : /************************************************************************/
     430             : Word32 BASOP_Util_InvLog2( Word32 x );
     431             : 
     432             : Word16 BASOP_util_norm_s_bands2shift( Word16 x );
     433             : 
     434             : 
     435             : /***********************************************************************/
     436             : /*!
     437             :   \brief        Calculate the headroom of the complex data in a 2 dimensional array
     438             : 
     439             :   \return number of headroom bits
     440             :  */
     441             : Word16 BASOP_util_norm_l_dim2_cplx( const Word32 *const *re, /*!< Real part of 32 Bit input */
     442             :                                     const Word32 *const *im, /*!< Imag part if 32 Bit input */
     443             :                                     Word16 startBand,        /*!< start band of cplx data   */
     444             :                                     Word16 stopBand,         /*!< stop band of cplx data    */
     445             :                                     Word16 startSlot,        /*!< start slot of cplx data   */
     446             :                                     Word16 stopSlot          /*!< stop slot of cplx data    */
     447             : );
     448             : /****************************************************************************/
     449             : /*!
     450             :   \brief   Does a data copy of Word8 *arg1 to Word8 *arg2 with Word16 arg3 number of moves
     451             : */
     452             : void copyWord8( const Word8 *src, /*!< i  : Source address             */
     453             :                 Word8 *dst,       /*!< i  : Destination address        */
     454             :                 const Word32 n ); /*!< i  : Number of elements to copy */
     455             : 
     456             : 
     457             : /****************************************************************************/
     458             : /*!
     459             :   \brief   Sets Word8 array arg1[] to zero for a length of Word16 arg2 elements
     460             : */
     461             : void set_zero_Word8( Word8 X[],  /*!< i  : Address of array                   */
     462             :                      Word32 n ); /*!< i  : Number of elements to set to zero  */
     463             : 
     464             : /****************************************************************************/
     465             : /*!
     466             :   \brief    Does a multiplication of Word32 * Word16 input values
     467             : 
     468             :   \return   z32 = x32 * y16
     469             : */
     470             : Word32 L_mult0_3216( Word32 x,   /*!<   : Multiplier     */
     471             :                      Word16 y ); /*!<   : Multiplicand   */
     472             : 
     473             : /* Calculate sin/cos. Angle in 2Q13 format, result has exponent = 1 */
     474             : Word16 getCosWord16( Word16 theta );
     475             : Word16 getSinWord16( Word16 theta );
     476             : Word32 getCosWord32( Word32 theta );
     477             : /**
     478             :  * \brief calculate cosine of angle. Tuned for ISF domain.
     479             :  * \param theta Angle normalized to radix 2, theta = (angle in radians)*2.0/pi
     480             :  * \return result with exponent 0.
     481             :  */
     482             : Word16 getCosWord16R2( Word16 theta );
     483             : Word16 getSineWord16R2( Word16 theta );
     484             : /****************************************************************************/
     485             : /*!
     486             :   \brief Sets Array Word16 arg1 to value Word16 arg2 for Word16 arg3 elements
     487             : */
     488             : void set_val_Word16( Word16 X[],       /*!< Address of array               */
     489             :                      const Word16 val, /*!< Value to copy into array       */
     490             :                      Word16 n );       /*!< Number of elements to process  */
     491             : 
     492             : 
     493             : /****************************************************************************/
     494             : /*!
     495             :   \brief Sets Array Word32 arg1 to value Word32 arg2 for Word16 arg3 elements
     496             : */
     497             : void set_val_Word32( Word32 X[],       /*!< Address of array               */
     498             :                      const Word32 val, /*!< Value to copy into array       */
     499             :                      Word16 n );       /*!< Number of elements to process  */
     500             : 
     501             : /****************************************************************************/
     502             : /*!
     503             :   \brief    Does a multiplication of Word16 input values
     504             : 
     505             :   \return   z16 = x16 * y16
     506             : */
     507             : Word16 mult0( Word16 x,   /* i  : Multiplier    */
     508             :               Word16 y ); /* i  : Multiplicand  */
     509             : 
     510             : /**
     511             :  * \brief calculate cosine of angle. Tuned for ISF domain.
     512             :  * \param theta Angle normalized to radix 2, theta = (angle in radians)*2.0/pi
     513             :  * \return result with exponent 0.
     514             :  */
     515             : Word16 getCosWord16R2( Word16 theta );
     516             : 
     517             : 
     518             : /****************************************************************************/
     519             : /*!
     520             :   \brief    square root abacus algorithm
     521             : 
     522             :   \return integer sqrt(x)
     523             :  */
     524             : Word16 getSqrtWord32( Word32 x );
     525             : 
     526             : /****************************************************************************/
     527             : /*!
     528             :   \brief    finds index of min Word16 in array
     529             : 
     530             :   \return   index of min Word16
     531             :  */
     532             : Word16 findIndexOfMinWord16( Word16 *x, const Word16 len );
     533             : 
     534             : /****************************************************************************/
     535             : /*!
     536             :   \brief    finds index of min Word32 in array
     537             : 
     538             :   \return   index of min Word32
     539             :  */
     540             : Word16 findIndexOfMinWord32( Word32 *x, const Word16 len );
     541             : 
     542             : /****************************************************************************/
     543             : /*!
     544             :   \brief    finds index of max Word16 in array
     545             : 
     546             :   \return   index of max Word16
     547             :  */
     548             : Word16 findIndexOfMaxWord16( Word16 *x, const Word16 len );
     549             : 
     550             : /****************************************************************************/
     551             : /*!
     552             :   \brief    finds index of max Word32 in array
     553             : 
     554             :   \return   index of max Word32
     555             :  */
     556             : Word16 findIndexOfMaxWord32( Word32 *x, const Word16 len );
     557             : 
     558             : /****************************************************************************/
     559             : /*!
     560             :   \brief    16x16->16 integer multiplication without overflow control
     561             : 
     562             :   \return 16x16->16 integer
     563             :  */
     564             : Word16 imult1616( Word16 x, Word16 y );
     565             : 
     566             : /****************************************************************************/
     567             : /*!
     568             :   \brief    32x16->32 integer multiplication with overflow control
     569             : 
     570             :   \return 32x16->32 integer
     571             :  */
     572             : Word32 imult3216( Word32 x, Word16 y );
     573             : 
     574             : /****************************************************************************/
     575             : /*!
     576             :   \brief    16/16->16 unsigned integer division
     577             : 
     578             :   x and y have to be positive, x has to be < 16384
     579             : 
     580             :   \return 16/16->16 integer
     581             :  */
     582             : 
     583             : Word16 idiv1616U_IVAS( Word16 x, Word16 y );
     584             : Word16 idiv1616U( Word16 x, Word16 y );
     585             : 
     586             : /****************************************************************************/
     587             : /*!
     588             :   \brief    16/16->16 signed integer division
     589             : 
     590             :   x and y have to be positive, x has to be < 16384
     591             : 
     592             :   \return 16/16->16 integer
     593             :  */
     594             : 
     595             : Word16 idiv1616( Word16 x, Word16 y );
     596             : 
     597             : Word16 idiv1616_1( Word16 x, Word16 y );
     598             : 
     599             : /*------------------------------------------------------------------*
     600             :  * Dot_product16HQ:
     601             :  *
     602             :  * \brief Compute scalar product of <x[],y[]> using 64-bit accumulator.
     603             :  *
     604             :  * Performs normalization of the result, returns the exponent
     605             :  * Note: In contrast to dotWord32, no headroom is required for data
     606             :  *       in x[] and y[], means, they may have any format Qn
     607             :  *------------------------------------------------------------------*/
     608             : Word32 Dot_product16HQ(                     /*<! o : normalized result              Q31 */
     609             :                         const Word32 L_off, /*<! i : initial sum value               Qn */
     610             :                         const Word16 x[],   /*<! i : x vector                        Qn */
     611             :                         const Word16 y[],   /*<! i : y vector                        Qn */
     612             :                         const Word16 lg,    /*<! i : vector length, range [0..7FFF]  Q0 */
     613             :                         Word16 *exp         /*<! o : exponent of result in [-32,31]  Q0 */
     614             : );
     615             : 
     616             : /*------------------------------------------------------------------*
     617             :  * norm_llQ31:
     618             :  *
     619             :  * \brief Compute normalized Q31 Values out of overflowed Q31 value
     620             :  *
     621             :  * Performs the calculation of a normalized Q31 Value with its
     622             :  * scalingfactor, taking into account the overflowed Q31 input value
     623             :  * and the number of Carrys, collected.
     624             :  *------------------------------------------------------------------*/
     625             : Word32 norm_llQ31(               /* o : normalized result              Q31 */
     626             :                    Word32 L_c,   /* i : upper bits of accu             Q-1 */
     627             :                    Word32 L_sum, /* i : lower bits of accu, unsigned   Q31 */
     628             :                    Word16 *exp   /* o : exponent of result in [-32,31]  Q0 */
     629             : );
     630             : 
     631             : /**
     632             :  * \brief Compute dot product of 1 32 bit vectors with itself
     633             :  * \param x input vector 1
     634             :  * \param headroom amount of headroom bits the input vector
     635             :  * \param length the length of the input vector
     636             :  * \param result_e pointer to where the exponent of the result will be stored into
     637             :  * \return the dot product of x and x.
     638             :  */
     639             : Word32 Norm32Norm( const Word32 *x, const Word16 headroom, const Word16 length, Word16 *result_e );
     640             : 
     641             : 
     642             : /*------------------------------------------------------------------*
     643             :  * Dot_productSq16HQ:
     644             :  *
     645             :  * \brief Compute scalar product of <x[],x[]> using 64-bit accumulator.
     646             :  *
     647             :  * Performs normalization of the result, returns the exponent
     648             :  * Note: In contrast to dotWord32, no headroom is required for data
     649             :  *       in x[], means, they may have any format Qn
     650             :  *------------------------------------------------------------------*/
     651             : Word32 Dot_productSq16HQ(                     /*<! o : normalized result              Q31 */
     652             :                           const Word32 L_off, /*<! i : initial sum value               Qn */
     653             :                           const Word16 x[],   /*<! i : x vector                        Qn */
     654             :                           const Word16 lg,    /*<! i : vector length, range [0..7FFF]  Q0 */
     655             :                           Word16 *exp         /*<! o : exponent of result in [-32,31]  Q0 */
     656             : );
     657             : /*------------------------------------------------------------------*
     658             :  * dotp_s_fx:
     659             :  *
     660             :  * \brief Compute scalar product of <x[],y[]> using 64-bit accumulator.
     661             :  *
     662             :  * Performs no normalization of the result
     663             :  *------------------------------------------------------------------*/
     664             : Word32 dotp_s_fx(                  /*<! o  : dot product of vector x and y 16Q15 */
     665             :                   const Word16 *x, /*<! i  : vector x                       6Q9  */
     666             :                   const Word16 *y, /*<! i  : vector y                       6Q9  */
     667             :                   const Word16 n,  /*<! i  : vector length                   Q0  */
     668             :                   Word16 s         /*<! i  : headroom                        Q0  */
     669             : );
     670             : 
     671             : /**
     672             :  * \brief return 2 ^ (exp * 2^exp_e)
     673             :  * \param exp_m mantissa of the exponent to 2.0f
     674             :  * \param exp_e exponent of the exponent to 2.0f
     675             :  * \param result_e pointer to a INT where the exponent of the result will be stored into
     676             :  * \return mantissa of the result
     677             :  */
     678             : Word32 BASOP_util_Pow2(
     679             :     const Word32 exp_m,
     680             :     const Word16 exp_e,
     681             :     Word16 *result_e );
     682             : 
     683             : 
     684             : Word32 Isqrt_lc1(
     685             :     Word32 frac, /* i  : Q31: normalized value (1.0 < frac <= 0.5) */
     686             :     Word16 *exp  /* i/o: exponent (value = frac x 2^exponent)      */
     687             : );
     688             : 
     689             : /**
     690             :  * \brief return 1/x
     691             :  * \param x index of lookup table
     692             :  * \return Word16 value of 1/x
     693             :  */
     694             : Word16 getNormReciprocalWord16( Word16 x );
     695             : 
     696             : /**
     697             :  * \brief return (1/x) << s
     698             :  * \param x index of lookup table
     699             :  * \param s shift factor
     700             :  * \return Word16 value of (1/x) << s
     701             :  */
     702             : Word16 getNormReciprocalWord16Scale( Word16 x, Word16 s );
     703             : 
     704             : 
     705             : /*************************************************************************
     706             :  *
     707             :  *   FUNCTION:   BASOP_Util_fPow()
     708             :  */
     709             : /**
     710             :  * \brief BASOP_Util_fPow
     711             :  *
     712             :  *   PURPOSE:   Computes pow(base_m, base_e, exp_m, exp_e), where base_m and base_e
     713             :  *              specify the base, and exp_m and exp_e specify the exponent.
     714             :  *              The result is returned in a mantissa and exponent representation.
     715             :  *
     716             :  *   DESCRIPTION:
     717             :  *        The function BASOP_Util_fPow(L_x) calculates the power function by
     718             :  *        calculating 2 ^ (log2(base)*exp)
     719             :  *
     720             :  * \param base_m mantissa of base
     721             :  * \param base_e exponent of base
     722             :  * \param exp_m mantissa of exponent
     723             :  * \param exp_e exponent of exponent
     724             :  * \param result_e pointer to exponent of result
     725             :  * \return Word32 mantissa of result
     726             :  *
     727             :  *************************************************************************/
     728             : 
     729             : Word32 BASOP_Util_fPow( /* o  : mantissa of result                                              */
     730             :                         Word32 base_m,
     731             :                         Word16 base_e, /* i  : input value for base (mantissa and exponent)                    */
     732             :                         Word32 exp_m,
     733             :                         Word16 exp_e,    /* i  : input value for exponent (mantissa and exponent)                */
     734             :                         Word16 *result_e /* o  : output pointer to exponent of result                            */
     735             : );
     736             : 
     737             : /*___________________________________________________________________________
     738             :  |                                                                           |
     739             :  |   Function Name : Dot_product12_offs()                                    |
     740             :  |                                                                           |
     741             :  |       Compute scalar product of <x[],y[]> using accumulator.              |
     742             :  |       The parameter 'L_off' is added to the accumulation result.          |
     743             :  |       The result is normalized (in Q31) with exponent (0..30).            |
     744             :  |   Notes:                                                                  |
     745             :  |       o  data in x[],y[] must provide enough headroom for accumulation    |
     746             :  |       o  L_off must correspond in format with product of x,y              |
     747             :  |          Example: 0.01f for Q9 x Q9: 0x0000147B in Q19                    |
     748             :  |                   means: L_off = FL2WORD32_SCALE(0.01f,31-19)             |
     749             :  |---------------------------------------------------------------------------|
     750             :  |  Algorithm:                                                               |
     751             :  |                                                                           |
     752             :  |       dot_product = L_off + sum(x[i]*y[i])     i=0..N-1                   |
     753             :  |___________________________________________________________________________|
     754             : */
     755             : 
     756             : Word32 Dot_product12_offs(                   /* (o) Q31: normalized result (1 < val <= -1) */
     757             :                            const Word16 x[], /* (i) 12bits: x vector                       */
     758             :                            const Word16 y[], /* (i) 12bits: y vector                       */
     759             :                            const Word16 lg,  /* (i)    : vector length in range [1..256]   */
     760             :                            Word16 *exp,      /* (o)    : exponent of result (0..+30)       */
     761             :                            Word32 L_off      /* (i) initial summation offset /2              */
     762             : );
     763             : 
     764             : Word32 Dot_product15_offs(                   /* (o) Q31: normalized result (1 < val <= -1) */
     765             :                            const Word16 x[], /* (i) 15bits: x vector                       */
     766             :                            const Word16 y[], /* (i) 15bits: y vector                       */
     767             :                            const Word16 lg,  /* (i)    : vector length in range [1..256]   */
     768             :                            Word16 *exp,      /* (o)    : exponent of result (0..+30)       */
     769             :                            Word32 L_off      /* (i) initial summation offset               */
     770             : );
     771             : 
     772             : /*!**********************************************************************
     773             :    \brief   Add two values given by mantissa and exponent.
     774             : 
     775             :    Mantissas are in 32-bit-fractional format with values between 0 and 1. <br>
     776             :    The base for exponents is 2.  Example:  \f$  a = a\_m * 2^{a\_e}  \f$<br>
     777             : 
     778             : ************************************************************************/
     779             : Word32 BASOP_Util_Add_Mant32Exp /* o  : normalized result mantissa */
     780             :     ( Word32 a_m,               /* i  : Mantissa of 1st operand a  */
     781             :       Word16 a_e,               /* i  : Exponent of 1st operand a  */
     782             :       Word32 b_m,               /* i  : Mantissa of 2nd operand b  */
     783             :       Word16 b_e,               /* i  : Exponent of 2nd operand b  */
     784             :       Word16 *ptr_e );          /* o  : exponent of result         */
     785             : 
     786             : /*!**********************************************************************
     787             :    \brief   Returns the comparison result of two normalized values given by mantissa and exponent.
     788             :             return value: -1: a < b, 0: a == b, 1; a > b
     789             : 
     790             :    Mantissas are in 32-bit-fractional format with values between 0 and 1. <br>
     791             :    The base for exponents is 2.  Example:  \f$  a = a\_m * 2^{a\_e}  \f$<br>
     792             : 
     793             : ************************************************************************/
     794             : Word16 BASOP_Util_Cmp_Mant32Exp /*!< o: flag: result of comparison */
     795             :     ( Word32 a_m,               /*!< i: Mantissa of 1st operand a  */
     796             :       Word16 a_e,               /*!< i: Exponent of 1st operand a  */
     797             :       Word32 b_m,               /*!< i: Mantissa of 2nd operand b  */
     798             :       Word16 b_e );             /*!< i: Exponent of 2nd operand b  */
     799             : 
     800             : /********************************************************************
     801             :  * bufferCopyFx
     802             :  *
     803             :  * \brief copies buffer while preserving Format of destination buffer
     804             :  *********************************************************************
     805             :  */
     806             : void bufferCopyFx(
     807             :     Word16 *src,    /*<! Qx  pointer to input buffer                */
     808             :     Word16 *dest,   /*<! Qx  pointer to output buffer               */
     809             :     Word16 length,  /*<! Q0  length of buffer to copy               */
     810             :     Word16 Qf_src,  /*<! Q0  Q format (frac-bits) of source buffer  */
     811             :     Word16 Qf_dest, /*<! Q0  Q format (frac-bits )of dest buffer    */
     812             :     Word16 Q_src,   /*<! Q0  exponent of source buffer              */
     813             :     Word16 Q_dest   /*<! Q0  exponent of destination buffer         */
     814             : );
     815             : 
     816             : /****************************************************************************/
     817             : /*!
     818             :   \brief   Accumulates multiplications
     819             : 
     820             :   Accumulates the elementwise multiplications of Word32 Array bufX32 with Word16 Array bufY16
     821             :   pointed to by arg1 to arg4 including the corresponding exponents. Length of to be multiplied arrays is arg5,
     822             : 
     823             :   \return Word32 result of accumulated multiplications over Word32 array arg1 and Word16 array arg3 and Word16 pointer
     824             :           to exponent of the result
     825             : */
     826             : Word32 dotWord32_16_Mant32Exp( const Word32 *bufX32, /* i: 32-bit buffer with unknown headroom */
     827             :                                Word16 bufX32_exp,    /* i: exponent of buffer bufX32           */
     828             :                                const Word16 *bufY16, /* i: 16-bit buffer quite right-aligned   */
     829             :                                Word16 bufY16_exp,    /* i: exponent of buffer bufY16           */
     830             :                                Word16 len,           /* i: buffer len to process               */
     831             :                                Word16 *exp );        /* o: result exponent                     */
     832             : 
     833             : /*!**********************************************************************
     834             :    \brief   Converts linear factor or energy to Decibel
     835             :             return value: fEnergy=0: 20 * log10(x * 2^{x\_e}),
     836             :                           fEnergy=1: 10 * log10(x * 2^{x\_e})
     837             : 
     838             :    Mantissa x is in 32-bit-fractional format with values between 0 and 1. <br>
     839             :    The base for exponent x_e is 2. <br>
     840             : 
     841             : ************************************************************************/
     842             : Word16 BASOP_Util_lin2dB(                   /*!< o: dB value (7Q8) */
     843             :                           Word32 x,         /*!< i: mantissa */
     844             :                           Word16 x_e,       /*!< i: exponent */
     845             :                           Word16 fEnergy ); /*!< i: flag indicating if x is energy */
     846             : 
     847             : /*!**********************************************************************
     848             :    \brief   Calculates atan(x).
     849             : ************************************************************************/
     850             : Word16 BASOP_util_atan(          /*!< o:  atan(x)           [-pi/2;pi/2]   1Q14  */
     851             :                         Word32 x /*!< i:  input data        (-64;64)       6Q25  */
     852             : );
     853             : 
     854             : /*!**********************************************************************
     855             :    \brief   Calculates atan2(y,x).
     856             : ************************************************************************/
     857             : Word16 BASOP_util_atan2(           /*!< o: atan2(y,x)    [-pi,pi]        Q13   */
     858             :                          Word32 y, /*!< i:                                     */
     859             :                          Word32 x, /*!< i:                                     */
     860             :                          Word16 e  /*!< i: exponent difference (exp_y - exp_x) */
     861             : );
     862             : 
     863             : /****************************************************************************/
     864             : /*!
     865             :   \brief   Accumulates multiplications
     866             : 
     867             :     Accumulates the elementwise multiplications of Word32 Array X with Word16 Array Y
     868             :     pointed to by arg1 and arg2 with specified headroom. Length of to be multiplied arrays is arg3,
     869             :     headroom with has to be taken into account is specified in arg4
     870             : 
     871             :   \return Word32 result of accumulated multiplications over Word32 array arg1 and Word16 array arg2 and Word16 pointer
     872             :           to exponent correction factor which needs to be added to the exponent of the result vector
     873             : */
     874             : Word32 dotWord32_16_guards( const Word32 *X, const Word16 *Y, Word16 n, Word16 hr, Word16 *shift );
     875             : 
     876             : // function defined in basop_util commented from basop_tcx_utils.c
     877             : Word16 compMantExp16Unorm( Word16 m1, Word16 e1, Word16 m2, Word16 e2 );
     878             : 
     879             : cmplx CL_scale_t( cmplx x, Word16 y );
     880             : cmplx CL_dscale_t( cmplx x, Word16 y1, Word16 y2 );
     881             : cmplx CL_mult_32x16( cmplx input, cmplx_s coeff );
     882             : 
     883             : #endif /* __BASOP_UTIL_H__ */

Generated by: LCOV version 1.14