Home | History | Annotate | Download | only in aacdec
      1 /* ------------------------------------------------------------------
      2  * Copyright (C) 1998-2009 PacketVideo
      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
     13  * express or implied.
     14  * See the License for the specific language governing permissions
     15  * and limitations under the License.
     16  * -------------------------------------------------------------------
     17  */
     18 /*
     19 
     20  Filename: pv_sqrt.c
     21 
     22 ------------------------------------------------------------------------------
     23  REVISION HISTORY
     24 
     25 
     26  Who:                                   Date: MM/DD/YYYY
     27  Description:
     28 
     29 ------------------------------------------------------------------------------
     30  INPUT AND OUTPUT DEFINITIONS
     31 
     32     Int32 x             32-bit integer
     33 
     34     Int32 y             32-bit integer
     35 
     36 
     37 ------------------------------------------------------------------------------
     38  FUNCTION DESCRIPTION
     39 
     40     Implement root squared of a number
     41 
     42 ------------------------------------------------------------------------------
     43  REQUIREMENTS
     44 
     45 
     46 ------------------------------------------------------------------------------
     47  REFERENCES
     48 
     49 ------------------------------------------------------------------------------
     50  PSEUDO-CODE
     51 
     52 ------------------------------------------------------------------------------
     53 */
     54 
     55 
     56 /*----------------------------------------------------------------------------
     57 ; INCLUDES
     58 ----------------------------------------------------------------------------*/
     59 
     60 #ifdef AAC_PLUS
     61 
     62 
     63 #include "pv_audio_type_defs.h"
     64 
     65 #include "fxp_mul32.h"
     66 #include "pv_sqrt.h"
     67 
     68 
     69 /*----------------------------------------------------------------------------
     70 ; MACROS
     71 ; Define module specific macros here
     72 ----------------------------------------------------------------------------*/
     73 
     74 
     75 /*----------------------------------------------------------------------------
     76 ; DEFINES
     77 ; Include all pre-processor statements here. Include conditional
     78 ; compile variables also.
     79 ----------------------------------------------------------------------------*/
     80 
     81 /*----------------------------------------------------------------------------
     82 ; LOCAL FUNCTION DEFINITIONS
     83 ; Function Prototype declaration
     84 ----------------------------------------------------------------------------*/
     85 #define R_SHIFT     28
     86 #define Q_fmt(x)   (Int32)(x*((Int32)1<<R_SHIFT) + (x>=0?0.5F:-0.5F))
     87 
     88 
     89 const Int32 sqrt_table[9] =
     90 {
     91     Q_fmt(-0.13829740941110F),  Q_fmt(0.95383399963991F),
     92     Q_fmt(-2.92784603873353F),  Q_fmt(5.27429191920042F),
     93     Q_fmt(-6.20272445821478F),  Q_fmt(5.04717433019620F),
     94     Q_fmt(-3.03362807640415F),  Q_fmt(1.86178814410910F),
     95     Q_fmt(0.16540758699193F)
     96 };
     97 
     98 /*----------------------------------------------------------------------------
     99 ; LOCAL STORE/BUFFER/POINTER DEFINITIONS
    100 ; Variable declaration - defined here and used outside this module
    101 ----------------------------------------------------------------------------*/
    102 
    103 /*----------------------------------------------------------------------------
    104 ; EXTERNAL FUNCTION REFERENCES
    105 ; Declare functions defined elsewhere and referenced in this module
    106 ----------------------------------------------------------------------------*/
    107 
    108 /*----------------------------------------------------------------------------
    109 ; EXTERNAL GLOBAL STORE/BUFFER/POINTER REFERENCES
    110 ; Declare variables used in this module but defined elsewhere
    111 ----------------------------------------------------------------------------*/
    112 
    113 /*----------------------------------------------------------------------------
    114 ; FUNCTION CODE
    115 ----------------------------------------------------------------------------*/
    116 
    117 
    118 void pv_sqrt(Int32 man, Int32 exp, Root_sq *result, Int32 *sqrt_cache)
    119 {
    120 
    121     Int32   y;
    122     Int32   xx;
    123     Int32   nn;
    124     Int32   i;
    125     const Int32 *pt_table = sqrt_table;
    126 
    127 
    128     if (sqrt_cache[0] == man && sqrt_cache[1] == exp)
    129     {
    130         result->root         =        sqrt_cache[2];
    131         result->shift_factor = (Int16)sqrt_cache[3];
    132     }
    133     else
    134     {
    135 
    136         sqrt_cache[0] = man;
    137         sqrt_cache[1] = exp;
    138 
    139 
    140         if (man > 0)
    141         {
    142             xx =  man;
    143             if (man >= Q_fmt(1.0f))
    144             {
    145                 nn = exp + 1;
    146                 while ((xx >>= 1) > Q_fmt(1.0f))
    147                 {
    148                     nn++;
    149                 }
    150             }
    151             else if (man < Q_fmt(0.5f))
    152             {
    153                 nn = exp - 1;
    154                 while ((xx <<= 1) < Q_fmt(0.5f))
    155                 {
    156                     nn--;
    157                 }
    158             }
    159             else
    160             {
    161                 nn = exp;
    162             }
    163 
    164 
    165             y  = fxp_mul32_Q28(*(pt_table++), xx);
    166 
    167             for (i = 3; i != 0; i--)
    168             {
    169                 y += *(pt_table++);
    170                 y  = fxp_mul32_Q28(y, xx);
    171                 y += *(pt_table++);
    172                 y  = fxp_mul32_Q28(y, xx);
    173             }
    174             y += *(pt_table++);
    175             y  = fxp_mul32_Q28(y, xx) + *(pt_table++);
    176 
    177             if (nn >= 0)
    178             {
    179                 if (nn&1)
    180                 {
    181                     y = fxp_mul32_Q29(y, Q_fmt(1.41421356237310F));
    182                     result->shift_factor = (nn >> 1) - 28;
    183                 }
    184                 else
    185                 {
    186                     result->shift_factor = (nn >> 1) - 29;
    187                 }
    188             }
    189             else
    190             {
    191                 if (nn&1)
    192                 {
    193                     y = fxp_mul32_Q28(y, Q_fmt(0.70710678118655F));
    194                 }
    195 
    196                 result->shift_factor = -((-nn) >> 1) - 29;
    197             }
    198 
    199             result->root = y;
    200 
    201         }
    202         else
    203         {
    204             result->root = 0;
    205             result->shift_factor = 0;
    206         }
    207 
    208     }
    209 
    210     sqrt_cache[2] = result->root;
    211     sqrt_cache[3] = result->shift_factor;
    212 
    213 }
    214 
    215 
    216 #endif
    217 
    218 
    219