LCOV - code coverage report
Current view: top level - lib_com - swb_bwe_com_fx.c (source / functions) Hit Total Coverage
Test: Coverage on main @ e70d960493906e094628337ac73d3c408863a5e6 Lines: 1736 1913 90.7 %
Date: 2025-09-17 02:22:58 Functions: 15 16 93.8 %

          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             : #include <stdint.h>
      34             : #include "options.h"
      35             : #include "prot_fx.h"
      36             : #include "basop_util.h"
      37             : #include "rom_com.h"
      38             : 
      39             : 
      40             : /*==========================================================================*/
      41             : /* FUNCTION      : Word16 WB_BWE_gain_pred_fx ()             */
      42             : /*--------------------------------------------------------------------------*/
      43             : /* PURPOSE       : predict WB frequency envelopes for 0b WB BWE        */
      44             : /*--------------------------------------------------------------------------*/
      45             : /* INPUT ARGUMENTS  :                            */
      46             : /* _Word16 *core_dec_freq        i  : Frequency domain core decoded signal  */
      47             : /* _Word16 coder_type            i  : coding type                           */
      48             : /* _Word16 prev_coder_type       i  : coding type of last frame             */
      49             : /* _Word16 prev_WB_fenv          i  : envelope for last frame               */
      50             : /* _Word16 *voice_factors        i  : voicing factors      //Q15            */
      51             : /* _Word16 pitch_buf[]           i  : pitch buffer         //Q6             */
      52             : /* _Word16  last_core_brate      i  : previous frame core bitrate           */
      53             : /* _Word16 last_wb_bwe_ener      i  : previous frame wb bwe signal energy   */
      54             : /*--------------------------------------------------------------------------*/
      55             : /* OUTPUT ARGUMENTS :                                                       */
      56             : /* _Word16 *WB_fenv,             o  : WB frequency envelopes   Q3           */
      57             : /*--------------------------------------------------------------------------*/
      58             : /* INPUT/OUTPUT ARGUMENTS :                                                 */
      59             : /*--------------------------------------------------------------------------*/
      60             : /* RETURN ARGUMENTS :                                                       */
      61             : /*          _Word16 mode                                                    */
      62             : /*--------------------------------------------------------------------------*/
      63             : /* CALLED FROM :                                                            */
      64             : /*==========================================================================*/
      65             : 
      66        4767 : Word16 WB_BWE_gain_pred_fx(
      67             :     Word16 *WB_fenv,             /* o  : WB frequency envelopes               Q3            */
      68             :     const Word16 *core_dec_freq, /* i  : Frequency domain core decoded signal Q_syn         */
      69             :     const Word16 coder_type,     /* i  : coding type                                        */
      70             :     Word16 prev_coder_type,      /* i  : coding type of last frame                          */
      71             :     Word16 prev_WB_fenv,         /* i  : envelope for last frame              Q3            */
      72             :     Word16 *voice_factors,       /* i  : voicing factors                      Q15           */
      73             :     const Word16 pitch_buf[],    /* i  : pitch buffer                         Q6            */
      74             :     Word32 last_core_brate,      /* i  : previous frame core bitrate                        */
      75             :     Word16 last_wb_bwe_ener,     /* i  : previous frame wb bwe signal energy  Q3            */
      76             :     Word16 Q_syn,                /* i  : synthesis scaling                                  */
      77             :     Word16 last_extl_fx,         /* i  : extl. layer for last frame                         */
      78             :     Word16 tilt_wb_fx            /*                                           Q4            */
      79             : )
      80             : {
      81             :     Word32 enerL;
      82             :     Word16 n_freq, mode, pitch;
      83        4767 :     Word16 ener_var_flag = 0;
      84        4767 :     move16();
      85             :     Word16 voice_factor, enerL_16, enerL_40, enerL_64;
      86        4767 :     Word16 env_var_flag = 0;
      87        4767 :     move16();
      88             :     Word16 exp;
      89             :     Word16 tmp, tmp1;
      90             :     Word32 L_tmp;
      91             :     Word32 L_WB_fenv0, L_WB_fenv1;
      92             :     Word16 pitch_buf_tmp[4];
      93        4767 :     Word16 alfa = MAX_16;
      94        4767 :     move16();
      95             : 
      96        4767 :     mode = NORMAL;
      97        4767 :     move16();
      98             : 
      99        4767 :     enerL = L_deposit_l( 0 );
     100      309855 :     FOR( n_freq = 128; n_freq < 192; n_freq++ )
     101             :     {
     102      305088 :         enerL = L_mac0_sat( enerL, core_dec_freq[n_freq], core_dec_freq[n_freq] ); /*2(Q_syn)  */
     103             :     }
     104             : 
     105        4767 :     L_WB_fenv0 = L_deposit_l( 0 );
     106      157311 :     FOR( n_freq = 192; n_freq < 224; n_freq++ )
     107             :     {
     108      152544 :         L_WB_fenv0 = L_mac0_sat( L_WB_fenv0, core_dec_freq[n_freq], core_dec_freq[n_freq] ); /*2*Q_syn */
     109             :     }
     110             : 
     111        4767 :     L_WB_fenv1 = L_deposit_l( 0 );
     112      157311 :     FOR( n_freq = 224; n_freq < 256; n_freq++ )
     113             :     {
     114      152544 :         L_WB_fenv1 = L_mac0_sat( L_WB_fenv1, core_dec_freq[n_freq], core_dec_freq[n_freq] ); /*2*Q_syn */
     115             :     }
     116             : 
     117        4767 :     L_tmp = sum16_32_fx( voice_factors, 4 );
     118        4767 :     voice_factor = extract_l( L_shr( L_tmp, 2 ) ); /*Q13 */
     119        4767 :     Copy_Scale_sig( pitch_buf, pitch_buf_tmp, 4, -1 );
     120        4767 :     pitch = sum16_fx( pitch_buf_tmp, 4 );
     121             :     /*Q5 */
     122             : 
     123        4767 :     L_tmp = L_shr( enerL, 4 );
     124        4767 :     test();
     125        4767 :     if ( GT_32( L_max( L_WB_fenv1, L_WB_fenv0 ), L_tmp ) && GT_16( 9856 /* 308 in Q5 */, pitch ) )
     126             :     {
     127        4381 :         ener_var_flag = 1;
     128        4381 :         move16();
     129             :     }
     130             : 
     131        4767 :     test();
     132        4767 :     IF( GT_32( L_WB_fenv0, L_shl_sat( L_WB_fenv1, 1 ) ) )
     133             :     {
     134        1168 :         exp = norm_l( L_WB_fenv0 );
     135        1168 :         tmp = extract_h( L_shl( L_WB_fenv0, exp ) );
     136        1168 :         tmp = div_s( 16384 /* 1 in Q14 */, tmp );                                   /*Q(15+14-2*Q_syn-exp) */
     137        1168 :         L_tmp = L_shr( Mult_32_16( L_shl( L_WB_fenv1, 1 ), tmp ), sub( 15, exp ) ); /*2*Q_syn+15+exp-15->2*Q_syn+exp */
     138             :         /*L_tmp Q15   */
     139        1168 :         tmp = extract_l( L_tmp );
     140        1168 :         alfa = s_max( tmp, 3277 /* 0.1 in Q15 */ );  /*Q15 */
     141        1168 :         L_WB_fenv0 = Mult_32_16( L_WB_fenv0, alfa ); /*2*Q_syn+15-15->2*Q_syn */
     142             :     }
     143        3599 :     ELSE IF( GT_32( L_WB_fenv1, L_shl_sat( L_WB_fenv0, 1 ) ) && NE_16( coder_type, UNVOICED ) )
     144             :     {
     145         183 :         exp = norm_l( L_WB_fenv1 );
     146         183 :         tmp = extract_h( L_shl( L_WB_fenv1, exp ) );
     147         183 :         tmp = div_s( 16384 /* 1 in Q14 */, tmp );                                   /*Q(15+14-2*Q_syn-exp) */
     148         183 :         L_tmp = L_shr( Mult_32_16( L_shl( L_WB_fenv0, 1 ), tmp ), sub( 15, exp ) ); /*2*Q_syn+15+exp-15->2*Q_syn+exp */
     149             :         /*L_tmp Q15 */
     150         183 :         tmp = extract_l( L_tmp );
     151         183 :         alfa = s_max( tmp, 3277 /* 0.1 in Q15 */ );  /*Q15 */
     152         183 :         L_WB_fenv1 = Mult_32_16( L_WB_fenv1, alfa ); /*2*Q_syn+15-15->2*Q_syn                      */
     153             :     }
     154             : 
     155        4767 :     if ( L_WB_fenv0 == 0 )
     156             :     {
     157           0 :         L_WB_fenv0 = L_deposit_l( 1 );
     158             :     }
     159             : 
     160        4767 :     if ( L_WB_fenv1 == 0 )
     161             :     {
     162           0 :         L_WB_fenv1 = L_deposit_l( 1 );
     163             :     }
     164             : 
     165        4767 :     if ( enerL == 0 )
     166             :     {
     167           0 :         enerL = L_deposit_l( 1 );
     168             :     }
     169             : 
     170        4767 :     L_tmp = L_add_sat( L_WB_fenv0, L_WB_fenv1 ); /* In 2*Q_syn */
     171        4767 :     exp = norm_l( L_tmp );
     172        4767 :     tmp = extract_h( L_shl( L_tmp, exp ) );
     173             :     /*exp = sub(exp, 30-(2*Q_syn+6)); //+6(/64) */
     174        4767 :     exp = sub( exp, sub( 30, add( shl( Q_syn, 1 ), 6 ) ) ); /*+6(/64) */
     175             : 
     176        4767 :     tmp = div_s( 16384, tmp );
     177        4767 :     L_tmp = L_deposit_h( tmp );
     178        4767 :     L_tmp = Isqrt_lc( L_tmp, &exp );
     179             : 
     180        4767 :     WB_fenv[0] = round_fx_sat( L_shl_sat( L_tmp, sub( exp, 12 ) ) ); /* Q3 */
     181        4767 :     move16();
     182        4767 :     test();
     183        4767 :     test();
     184        4767 :     IF( NE_16( coder_type, AUDIO ) && NE_16( coder_type, UNVOICED ) && ener_var_flag == 0 )
     185             :     {
     186         384 :         WB_fenv[0] = add_sat( WB_fenv[0], mult_r_sat( WB_fenv[0], 16384 /* 0.5 in Q15 */ ) );
     187         384 :         move16();
     188             :     }
     189             : 
     190        4767 :     exp = norm_l( enerL );
     191        4767 :     tmp = extract_h( L_shl( enerL, exp ) );
     192        4767 :     exp = sub( exp, sub( 30, shl( Q_syn, 1 ) ) );
     193             : 
     194        4767 :     tmp = div_s( 16384, tmp );
     195        4767 :     L_tmp = L_deposit_h( tmp );
     196        4767 :     L_tmp = Isqrt_lc( L_tmp, &exp );
     197        4767 :     enerL_16 = round_fx_sat( L_shl_sat( L_tmp, sub( exp, 15 ) ) ); /* Q0 */
     198        4767 :     enerL_40 = mult_r( 6554 /* 0.2 in Q15 */, enerL_16 );          /*1/40 in Q18  ->Q3    */
     199             : 
     200        4767 :     test();
     201        4767 :     test();
     202        4767 :     test();
     203        4767 :     test();
     204        4767 :     test();
     205        4767 :     test();
     206        4767 :     IF( NE_16( coder_type, TRANSITION ) && NE_16( coder_type, AUDIO ) && NE_16( coder_type, UNVOICED ) &&
     207             :         GT_16( enerL_40, WB_fenv[0] ) && GT_16( alfa, 29491 /*0.9 in Q15*/ ) && !( EQ_16( coder_type, prev_coder_type ) && GT_16( WB_fenv[0], prev_WB_fenv ) ) )
     208             :     {
     209           0 :         IF( WB_fenv[0] != 0 )
     210             :         {
     211           0 :             exp = norm_s( WB_fenv[0] );
     212           0 :             tmp = div_s( shl( 1, sub( 14, exp ) ), WB_fenv[0] );     /*Q(29-exp-3) */
     213           0 :             L_tmp = L_mult( enerL_40, tmp );                         /*Q(30-exp) */
     214           0 :             tmp = round_fx_sat( L_shl_sat( L_tmp, sub( exp, 2 ) ) ); /*Q12 */
     215           0 :             tmp = s_min( tmp, 16384 /*4 in Q12*/ );
     216           0 :             L_tmp = L_shr( L_mult0( tmp, WB_fenv[0] ), 12 ); /*Q15 */
     217           0 :             WB_fenv[0] = extract_l( L_tmp );                 /*Q3 */
     218           0 :             move16();
     219             :         }
     220             : 
     221           0 :         IF( GT_16( WB_fenv[0], prev_WB_fenv ) )
     222             :         {
     223             :             /*WB_fenv[0]= add(mult_r(9830, WB_fenv[0]), mult_r(22938, prev_WB_fenv));    move16();//Q3 */
     224           0 :             WB_fenv[0] = round_fx( L_mac( L_mult( 9830 /* 0.5 in Q15 */, WB_fenv[0] ), 22938 /* 0.7 in Q15 */, prev_WB_fenv ) ); /*Q3 */
     225           0 :             move16();
     226             :         }
     227             :     }
     228             : 
     229        4767 :     L_tmp = L_mult0( voice_factor, 77 );
     230        4767 :     tmp1 = extract_l( L_shr( L_tmp, 13 ) );
     231             : 
     232        4767 :     exp = norm_s( pitch );
     233        4767 :     tmp = div_s( shl( 1, sub( 14, exp ) ), pitch );          /*Q(29-exp-5) */
     234        4767 :     L_tmp = L_mult0( tmp1, tmp );                            /*29-exp-5->24-exp */
     235        4767 :     tmp = round_fx_sat( L_shl_sat( L_tmp, add( exp, 6 ) ) ); /*14          */
     236        4767 :     tmp1 = s_max( tmp, 8192 /* 0.5 in Q14 */ );
     237        4767 :     alfa = s_min( 24576 /* 1.5 in Q14 */, tmp1 ); /*Q14 */
     238             : 
     239        4767 :     L_tmp = L_mult0( alfa, WB_fenv[0] ); /*Q14+Q3->Q17 */
     240        4767 :     L_tmp = L_shr( L_tmp, 14 );          /*Q3 */
     241        4767 :     tmp = extract_l( L_tmp );
     242             : 
     243        4767 :     enerL = L_deposit_l( enerL_16 );
     244        4767 :     enerL = L_shl( enerL, 6 ); /*Q6 */
     245             :     /* Here, we do not multiply L_tmp by 3 to avoid overflow */
     246        4767 :     L_tmp = L_mult0( WB_fenv[0], WB_fenv[0] ); /* Q6 */
     247             : 
     248        4767 :     test();
     249        4767 :     test();
     250        4767 :     test();
     251             : #define ONE_DIV_3 ( (Word32) 0x2AAAAAAA )
     252             :     /* Here, L_tmp is not pre-multiplied with 3, we multiply enerL with 1/3 */
     253        4767 :     IF( GT_16( shr( enerL_16, 3 ), tmp ) && GT_32( Mpy_32_32( ONE_DIV_3, enerL ), L_tmp ) && NE_16( prev_coder_type, UNVOICED ) && WB_fenv[0] != 0 )
     254             :     {
     255          34 :         env_var_flag = 1;
     256          34 :         move16();
     257          34 :         enerL_64 = mult_r( 4096, enerL_16 ); /* 1/64 in Q18  ->Q3 */
     258             : 
     259          34 :         exp = norm_s( WB_fenv[0] );
     260          34 :         tmp = div_s( shl( 1, sub( 14, exp ) ), WB_fenv[0] );     /*Q(29-exp-3) */
     261          34 :         L_tmp = L_mult( enerL_64, tmp );                         /*Q(30-exp) */
     262          34 :         tmp = round_fx_sat( L_shl_sat( L_tmp, sub( exp, 2 ) ) ); /*Q12 */
     263          34 :         tmp = s_min( tmp, 16384 /*4 in Q12*/ );
     264          34 :         L_tmp = L_shr( L_mult0( tmp, WB_fenv[0] ), 12 ); /*Q3 */
     265          34 :         WB_fenv[0] = extract_l( L_tmp );                 /*Q3 */
     266          34 :         move16();
     267          34 :         IF( GT_16( WB_fenv[0], prev_WB_fenv ) )
     268             :         {
     269             :             /*WB_fenv[0] = add(mult_r(9830, WB_fenv[0]), mult_r(22938, prev_WB_fenv));//Q3 */
     270          32 :             WB_fenv[0] = round_fx( L_mac( L_mult( 9830 /* 0.3 in Q15 */, WB_fenv[0] ), 22938 /* 0.7 in Q15 */, prev_WB_fenv ) ); /*Q3 */
     271          32 :             move16();
     272             :         }
     273             :     }
     274             : 
     275        4767 :     test();
     276        4767 :     IF( EQ_16( coder_type, UNVOICED ) || EQ_16( prev_coder_type, UNVOICED ) )
     277             :     {
     278          35 :         WB_fenv[0] = shr( WB_fenv[0], 1 );
     279          35 :         move16(); /*Q3 */
     280             :     }
     281             : 
     282        4767 :     IF( NE_16( coder_type, AUDIO ) )
     283             :     {
     284        4767 :         tmp = mult_r( voice_factor, 19661 /* 1.2 in Q14 */ ); /*Q12 */
     285        4767 :         tmp = s_max( tmp, 4096 /* 1 in Q12 */ );
     286        4767 :         exp = norm_s( tmp );
     287        4767 :         tmp = div_s( shl( 1, sub( 14, exp ) ), tmp ); /*Q(29-exp-12) */
     288        4767 :         L_tmp = L_mult( WB_fenv[0], tmp );            /*Q(21-exp) */
     289             : 
     290        4767 :         WB_fenv[0] = round_fx( L_shl( L_tmp, sub( exp, 2 ) ) ); /*Q3 */
     291        4767 :         move16();
     292        4767 :         tmp1 = mult_r( 328 /* (1/400) in Q17 */, pitch );                      /*Q7 */
     293        4767 :         tmp = s_min( s_max( tmp1, 16 /* 0.125 in Q7 */ ), 256 /* 1 in Q7 */ ); /*Q7 */
     294        4767 :         L_tmp = L_shr( L_mult0( WB_fenv[0], tmp ), 7 );                        /*Q3 */
     295             :                                                                                /*WB_fenv[0] = saturate(L_tmp); //Q3 */
     296        4767 :         WB_fenv[0] = round_fx_sat( L_shl_sat( L_tmp, 16 ) );                   /*Q3 */
     297        4767 :         move16();
     298             :     }
     299        4767 :     test();
     300        4767 :     IF( GT_32( last_core_brate, ACELP_8k00 ) && GT_16( WB_fenv[0], last_wb_bwe_ener ) )
     301             :     {
     302             :         /*WB_fenv[0]= add(mult_r(29491, last_wb_bwe_ener), mult_r(3277, WB_fenv[0]));//Q3 */
     303         731 :         WB_fenv[0] = round_fx( L_mac( L_mult( 29491 /* 0.9 in Q15 */, last_wb_bwe_ener ), 3277, WB_fenv[0] ) ); /*Q3 */
     304         731 :         move16();
     305             :     }
     306             : 
     307        4767 :     IF( NE_16( last_extl_fx, WB_BWE ) && LT_16( tilt_wb_fx, 128 /* 8 in Q4 */ ) )
     308             :     {
     309           9 :         WB_fenv[0] = mult_r( s_min( 16384 /* 0.5 in Q15 */, shl( tilt_wb_fx, 8 ) ), WB_fenv[0] );
     310           9 :         move16();
     311             :     }
     312             : 
     313        4767 :     IF( EQ_16( env_var_flag, 1 ) )
     314             :     {
     315          34 :         WB_fenv[1] = add( WB_fenv[0], mult_r( WB_fenv[0], 16384 /* 0.5 in Q15 */ ) );
     316          34 :         move16();
     317          34 :         WB_fenv[0] = mult_r( 24576 /* 0.75 in Q15 */, WB_fenv[0] );
     318          34 :         move16(); /*Q3                 */
     319             :     }
     320             :     ELSE
     321             :     {
     322        4733 :         WB_fenv[1] = WB_fenv[0];
     323        4733 :         move16();
     324             :     }
     325             : 
     326        4767 :     test();
     327        4767 :     IF( EQ_16( coder_type, UNVOICED ) || EQ_16( prev_coder_type, UNVOICED ) )
     328             :     {
     329          35 :         WB_fenv[1] = shr( WB_fenv[1], 1 );
     330          35 :         move16(); /*Q3 */
     331             :     }
     332             : 
     333        4767 :     return ( mode );
     334             : }
     335             : 
     336             : /*-------------------------------------------------------------------*
     337             :  * calc_norm_envelop_lf_fx()
     338             :  *
     339             :  * calc_envelope of low frequency spectrum
     340             :  *-------------------------------------------------------------------*/
     341        2650 : static void calc_norm_envelop_lf_fx(
     342             :     const Word32 SWB_signal[],
     343             :     /* i  : SWB spectrum                                    */ /* Q12 */
     344             :     Word32 *envelope,
     345             :     /* o  : normalized envelope                             */ /* Q12 */
     346             :     Word16 *L_swb_norm,                                        /* i/o : length of envelope                              */
     347             :     const Word16 HQ_mode,
     348             :     /* i  : HQ mode                                         */ /* Q0 */
     349             :     const Word16 hq_generic_offset,
     350             :     /* i  : frequency offset for representing hq generic    */ /* Q0 */
     351             :     Word16 *sfreq,                                             /* i  : starting frequency index                        */
     352             :     Word16 *efreq                                              /* i  : ending frequency index                          */
     353             : )
     354             : {
     355             :     Word16 lookback, env_index, n_freq, n_lag_now, n_lag;
     356             : 
     357        2650 :     *sfreq = 2;
     358        2650 :     move16();
     359        2650 :     IF( EQ_16( hq_generic_offset, HQ_GENERIC_FOFFSET_24K4 ) )
     360             :     {
     361        2650 :         *efreq = 146;
     362        2650 :         move16();
     363        2650 :         if ( EQ_16( HQ_mode, HQ_GEN_FB ) )
     364             :         {
     365        2020 :             *efreq = 306;
     366        2020 :             move16();
     367             :         }
     368        2650 :         IF( LT_16( add( shl( sub( 328, *efreq ), 1 ), 1 ), *L_swb_norm ) )
     369             :         {
     370          50 :             *L_swb_norm = add( shl( sub( 328, *efreq ), 1 ), 1 );
     371          50 :             move16();
     372             :         }
     373             :     }
     374             :     ELSE
     375             :     {
     376           0 :         *efreq = 130;
     377           0 :         move16();
     378           0 :         if ( EQ_16( HQ_mode, HQ_GEN_FB ) )
     379             :         {
     380           0 :             *efreq = 290;
     381           0 :             move16();
     382             :         }
     383           0 :         IF( LT_16( add( shl( sub( 400, *efreq ), 1 ), 1 ), *L_swb_norm ) )
     384             :         {
     385           0 :             *L_swb_norm = add( shl( sub( 400, *efreq ), 1 ), 1 );
     386             :         }
     387             :     }
     388        2650 :     lookback = shr( *L_swb_norm, 1 );
     389        2650 :     env_index = 0;
     390        2650 :     move16();
     391        2650 :     n_lag_now = *L_swb_norm;
     392        2650 :     move16();
     393             : 
     394       19030 :     FOR( n_freq = 0; n_freq < lookback; n_freq++ )
     395             :     {
     396             :         Word16 tmp;
     397             :         Word32 L_tmp;
     398             : 
     399       16380 :         L_tmp = L_deposit_l( 1 );
     400       16380 :         tmp = add( lookback, n_freq );
     401      289926 :         FOR( n_lag = 0; n_lag < tmp; n_lag++ )
     402             :         {
     403      273546 :             L_tmp = L_add_sat( L_tmp, L_abs( SWB_signal[n_lag] ) );
     404             :         }
     405       16380 :         envelope[env_index] = L_tmp;
     406       16380 :         move32();
     407       16380 :         env_index = add( env_index, 1 );
     408             :     }
     409             : 
     410      696370 :     FOR( ; n_freq < *efreq; n_freq++ )
     411             :     {
     412             :         /* Apply MA filter */
     413      693720 :         Word32 L_tmp = L_deposit_l( 1 );
     414     9007453 :         FOR( n_lag = 0; n_lag < n_lag_now; n_lag++ )
     415             :         {
     416     8313733 :             L_tmp = L_add_sat( L_tmp, L_abs( SWB_signal[n_freq - lookback + n_lag] ) );
     417             :         }
     418             : 
     419      693720 :         envelope[env_index] = L_tmp;
     420      693720 :         move32();
     421      693720 :         env_index = add( env_index, 1 );
     422             :     }
     423             : 
     424        2650 :     return;
     425             : }
     426             : /*-------------------------------------------------------------------*
     427             :  * calc_normal_length()
     428             :  *
     429             :  *-------------------------------------------------------------------*/
     430             : 
     431       37094 : void calc_normal_length_fx(
     432             :     const Word16 core,       /* i  : core                                 */
     433             :     const Word16 *sp,        /* i  : input signal                Q_syn    */
     434             :     const Word16 mode,       /* i  : input mode                           */
     435             :     const Word16 extl,       /* i  : extension layer                      */
     436             :     Word16 *L_swb_norm,      /* o  : normalize length                     */
     437             :     Word16 *prev_L_swb_norm, /*i/o : last normalize length                */
     438             :     Word16 Q_syn )
     439             : {
     440             :     Word16 i, n_freq, n_band, THRES;
     441             :     const Word16 *pit;
     442             :     Word16 peak, mag;
     443             :     Word16 L_swb_norm_trans, L_swb_norm_norm, L_swb_norm_harm, L_swb_norm_cur;
     444             :     Word16 N;
     445             :     Word32 L_mean, L_tmp, L_tmp1;
     446             : 
     447       37094 :     THRES = 4;
     448       37094 :     move16();
     449       37094 :     test();
     450       37094 :     test();
     451       37094 :     if ( EQ_16( core, HQ_CORE ) || EQ_16( extl, SWB_BWE ) || EQ_16( extl, FB_BWE ) )
     452             :     {
     453       21224 :         THRES = 8;
     454       21224 :         move16();
     455             :     }
     456             : 
     457       37094 :     N = 16;
     458       37094 :     move16();
     459       37094 :     test();
     460       37094 :     test();
     461       37094 :     if ( EQ_16( core, HQ_CORE ) && ( EQ_16( mode, HQ_HARMONIC ) || EQ_16( mode, HQ_HVQ ) ) )
     462             :     {
     463           0 :         N = 13;
     464           0 :         move16();
     465             :     }
     466             : 
     467       37094 :     n_band = 0;
     468       37094 :     move16();
     469       37094 :     pit = sp;
     470      630598 :     FOR( i = 0; i < N; i++ )
     471             :     {
     472      593504 :         peak = 0;
     473      593504 :         move16();
     474      593504 :         L_mean = L_deposit_l( 0 );
     475             : 
     476    10089568 :         FOR( n_freq = 0; n_freq < 16; n_freq++ )
     477             :         {
     478     9496064 :             mag = abs_s( *pit );
     479             : 
     480     9496064 :             peak = s_max( peak, mag );
     481     9496064 :             L_mean = L_add( L_mean, mag ); /*Q_syn */
     482     9496064 :             pit++;
     483             :         }
     484             : 
     485      593504 :         L_tmp = L_mult0( peak, 15 + THRES ); /*Q_syn */
     486      593504 :         IF( EQ_16( THRES, 8 ) )
     487             :         {
     488      339584 :             L_tmp1 = L_shl( Mpy_32_16_1( L_mean, 32767 ), 3 );
     489             :         }
     490             :         ELSE
     491             :         {
     492      253920 :             L_tmp1 = L_shl( Mpy_32_16_1( L_mean, 32767 ), 2 );
     493             :         }
     494             : 
     495      593504 :         test();
     496      593504 :         if ( GT_32( L_tmp, L_tmp1 ) && ( GT_16( peak, shl( 10, Q_syn ) ) ) )
     497             :         {
     498       81316 :             n_band = add( 1, n_band );
     499             :         }
     500             :     }
     501             : 
     502       37094 :     IF( EQ_16( core, ACELP_CORE ) )
     503             :     {
     504       37094 :         L_swb_norm_trans = add( 4, mult( n_band, 8192 /* 0.25 in Q15 */ ) );
     505       37094 :         L_swb_norm_norm = add( 8, mult( n_band, 16384 /* 0.5 in Q15 */ ) );
     506             : 
     507       37094 :         L_tmp = L_add( 65536, L_mult0( n_band, 4096 ) );              /*Q16 */
     508       37094 :         L_swb_norm_harm = s_max( round_fx( L_shl( L_tmp, 5 ) ), 24 ); /* Q0 */
     509             : 
     510       37094 :         IF( EQ_16( mode, HARMONIC ) )
     511             :         {
     512        3432 :             L_swb_norm_cur = L_swb_norm_harm;
     513        3432 :             move16();
     514             :         }
     515       33662 :         ELSE IF( EQ_16( mode, NORMAL ) )
     516             :         {
     517       32186 :             L_swb_norm_cur = L_swb_norm_norm;
     518       32186 :             move16();
     519             :         }
     520             :         ELSE
     521             :         {
     522        1476 :             L_swb_norm_cur = L_swb_norm_trans;
     523        1476 :             move16();
     524             :         }
     525       37094 :         *L_swb_norm = shr( add( L_swb_norm_cur, *prev_L_swb_norm ), 1 );
     526       37094 :         move16();
     527       37094 :         *prev_L_swb_norm = L_swb_norm_cur;
     528       37094 :         move16();
     529             :     }
     530             :     ELSE
     531             :     {
     532           0 :         test();
     533           0 :         IF( EQ_16( mode, HQ_HARMONIC ) || EQ_16( mode, HQ_HVQ ) )
     534             :         {
     535           0 :             L_tmp = L_add( 65536, L_mult( n_band, 2560 /* 2.5 in Q10*/ ) );
     536           0 :             L_swb_norm_cur = round_fx( L_shl( L_tmp, 5 ) ); /*Q0 */
     537             :         }
     538             :         ELSE
     539             :         {
     540           0 :             L_tmp = L_add( 65536, L_mult( n_band, 2048 ) ); /*Q16 */
     541           0 :             L_swb_norm_cur = round_fx( L_shl( L_tmp, 3 ) ); /*Q0 */
     542             :         }
     543             : 
     544             :         /**L_swb_norm = add(mult_r(L_swb_norm_cur, 3277), mult_r(*prev_L_swb_norm, 29491)); */
     545           0 :         *L_swb_norm = round_fx( L_mac( L_mult( L_swb_norm_cur, 3277 /* 0.1 in Q15 */ ), *prev_L_swb_norm, 29491 /* 0.9 in Q15 */ ) );
     546           0 :         move16();
     547           0 :         *prev_L_swb_norm = L_swb_norm_cur;
     548           0 :         move16();
     549             :     }
     550             : 
     551       37094 :     return;
     552             : }
     553             : /*-------------------------------------------------------------------*
     554             :  * calc_tilt_bwe()
     555             :  *
     556             :  * calculate tilt parameter
     557             :  *-------------------------------------------------------------------*/
     558             : 
     559      183231 : Word32 calc_tilt_bwe_fx(                      /* o  : Tilt in Q24                      */
     560             :                          const Word16 *sp,    /* i  : input signal     Q(15 - exp_sp)  */
     561             :                          const Word16 exp_sp, /* i  : Exp of inp signal                */
     562             :                          const Word16 N       /* i  : signal length                    */
     563             : )
     564             : {
     565             :     Word16 i, j;
     566             :     Word32 L_ener, L_ener_tot, L_temp;
     567             :     Word16 tmp1, tmp2;
     568             :     const Word16 *ptr;
     569             :     Word16 exp2;
     570             : #ifdef BASOP_NOGLOB_DECLARE_LOCAL
     571      183231 :     Flag Overflow = 0;
     572      183231 :     move32();
     573             : #endif
     574             : 
     575             :     BASOP_SATURATE_WARNING_OFF_EVS
     576             : 
     577             :     /* this is required for adaptative precision energy summation loop, do not remove */
     578      183231 :     Overflow = 0;
     579      183231 :     move16();
     580      183231 :     exp2 = 0;
     581      183231 :     move16();
     582             : 
     583      183231 :     ptr = sp;
     584      183231 :     move16();
     585      183231 :     L_ener_tot = L_deposit_l( 1 );
     586             : 
     587             :     /* Divide Frame Length by 32 */
     588     1960947 :     FOR( j = shr( N, 5 ); j > 0; j-- )
     589             :     {
     590     1777716 :         tmp1 = mult_ro( *ptr++, 8192 /*0.25 in Q15 */, &Overflow ); /* Divide by 4 */
     591     1777716 :         L_ener = L_mult0( tmp1, tmp1 );
     592             :         /* With the shift by 4 and the L_mult0, no overflow possible for 32 samples */
     593    56886912 :         FOR( i = 1; i < 32; i++ )
     594             :         {
     595    55109196 :             tmp1 = mult_ro( *ptr++, 8192 /*0.25 in Q15 */, &Overflow ); /* Divide by 4 */
     596    55109196 :             L_ener = L_mac0_o( L_ener, tmp1, tmp1, &Overflow );
     597             :         }
     598             :         /*Overflow will never happen because exp2 is always positive*/
     599     1777716 :         L_ener = L_shr( L_ener, exp2 );
     600     1777716 :         L_temp = L_add_o( L_ener_tot, L_ener, &Overflow );
     601     1777716 :         IF( Overflow != 0 )
     602             :         {
     603          35 :             L_ener_tot = L_shr( L_ener_tot, 1 );
     604          35 :             L_ener = L_shr( L_ener, 1 );
     605          35 :             exp2 = add( exp2, 1 );
     606             :             /* this is required, do not remove */
     607          35 :             Overflow = 0;
     608          35 :             move16();
     609             :         }
     610     1777716 :         L_ener_tot = L_add_o( L_ener_tot, L_ener, &Overflow );
     611             :     }
     612      183231 :     L_ener = L_deposit_l( abs_s( sub_o( sp[1], sp[0], &Overflow ) ) );
     613    56703681 :     FOR( i = 2; i < N; i++ )
     614             :     {
     615             :         /* Eq to (sp[i] - sp[i-1]) * (sp[i-1] - sp[i-2]) < 0 */
     616    56520450 :         tmp1 = sub_o( sp[i], sp[i - 1], &Overflow );
     617    56520450 :         tmp2 = sub_o( sp[i - 1], sp[i - 2], &Overflow );
     618    56520450 :         tmp2 = mult( tmp1, tmp2 );
     619    56520450 :         tmp1 = abs_s( tmp1 );
     620             :         /* to Get either 0 or -1 in 'tmp2' */
     621    56520450 :         tmp2 = shr( tmp2, 15 );
     622             :         /* this allows this code */
     623    56520450 :         L_ener = L_msu0( L_ener, tmp2, tmp1 );
     624             :         /* instead of this one */
     625             :         /* test(); */
     626             :         /* if (tmp2 < 0) */
     627             :         /* { */
     628             :         /*     L_ener = L_mac0(L_ener, 1, tmp1); */
     629             :         /* } */
     630             :         /* it saves one op */
     631             :     }
     632             : 
     633      183231 :     tmp1 = norm_l( L_ener_tot );
     634      183231 :     L_temp = L_shl( L_ener_tot, tmp1 );
     635      183231 :     tmp1 = sub( add( 31 + 4, exp2 ), add( tmp1, shl( exp_sp, 1 ) ) );
     636      183231 :     L_temp = Isqrt_lc( L_temp, &tmp1 );
     637             : 
     638             :     /* *tilt_flt = (float)(r1/sqrt(r0)); */
     639      183231 :     exp2 = norm_l( L_ener );
     640      183231 :     L_temp = Mult_32_16( L_temp, round_fx_sat( L_shl_sat( L_ener, exp2 ) ) );
     641      183231 :     exp2 = sub( exp2, tmp1 );
     642      183231 :     exp2 = add( exp2, exp_sp );
     643             : 
     644             :     /* Put in Q24 */
     645      183231 :     L_temp = L_shr_sat( L_temp, sub( exp2, 24 ) );
     646             :     BASOP_SATURATE_WARNING_ON_EVS
     647             : 
     648      183231 :     return L_temp;
     649             : }
     650      175117 : Word16 ivas_calc_tilt_bwe_fx(                      /* o  : Tilt in Q24                        */
     651             :                               const Word32 *sp,    /* i  : input signal                       */
     652             :                               const Word16 exp_sp, /* i  : Exp of inp signal   Q(15 - exp_sp) */
     653             :                               const Word16 N       /* i  : signal length                      */
     654             : )
     655             : {
     656             :     Word16 i, j;
     657             :     Word32 L_ener, L_ener_tot, L_temp;
     658             :     Word32 tmp1, tmp2;
     659      175117 :     Word16 sign = 0;
     660      175117 :     move16();
     661             :     const Word32 *ptr;
     662             :     Word16 exp2, tmp_exp;
     663             : 
     664             :     BASOP_SATURATE_WARNING_OFF_EVS
     665             : 
     666             :     /* this is required for adaptative precision energy summation loop, do not remove */
     667      175117 :     exp2 = 0;
     668      175117 :     move16();
     669      175117 :     ptr = sp;
     670             : 
     671      175117 :     L_ener_tot = L_deposit_l( 1 );
     672             :     /* Divide Frame Length by 32 */
     673             :     Word64 W_ener;
     674      175117 :     W_ener = Mpy_32_32( *ptr, *ptr );
     675      175117 :     ptr++;
     676   112074880 :     FOR( j = 1; j < N; j++ )
     677             :     {
     678             :         /* With the shift by 4 and the L_mult0, no overflow possible for 32 samples */
     679   111899763 :         W_ener = W_mac_32_32( W_ener, *ptr, *ptr );
     680   111899763 :         ptr++;
     681             :     }
     682      175117 :     L_ener_tot = W_round64_L( W_ener );
     683      175117 :     L_ener_tot = L_max( L_ener_tot, 1 ); /* L_ener_tot is energy, it is always positive, but have to be > 0 for the following division */
     684      175117 :     L_ener = L_abs( L_sub( sp[1], sp[0] ) );
     685             : 
     686   111899763 :     FOR( i = 2; i < N; i++ )
     687             :     {
     688             :         /* Eq to (sp[i] - sp[i-1]) * (sp[i-1] - sp[i-2]) < 0 */
     689   111724646 :         tmp1 = L_sub( sp[i], sp[i - 1] );
     690   111724646 :         tmp2 = L_sub( sp[i - 1], sp[i - 2] );
     691             : 
     692   111724646 :         tmp2 = Mpy_32_32( tmp1, tmp2 );
     693   111724646 :         tmp1 = L_abs( tmp1 );
     694             :         /* to Get either 0 or -1 in 'tmp2' */
     695   111724646 :         tmp2 = L_shr( tmp2, 31 );
     696   111724646 :         sign = extract_l( L_shl( tmp2, Q15 ) );
     697             :         /* this allows this code */
     698   111724646 :         L_ener = Msub_32_16( L_ener, tmp1, sign );
     699             :         /* instead of this one */
     700             :         /* test(); */
     701             :         /* if (tmp2 < 0) */
     702             :         /* { */
     703             :         /*     L_ener = L_mac0(L_ener, 1, tmp1); */
     704             :         /* } */
     705             :         /* it saves one op */
     706             :     }
     707             : 
     708      175117 :     L_temp = L_ener_tot;
     709      175117 :     move32();
     710      175117 :     tmp_exp = sub( Q31, sub( shl( exp_sp, 1 ), Q31 ) );
     711      175117 :     L_temp = Sqrt32( L_temp, &tmp_exp );
     712      175117 :     L_temp = L_shl( L_temp, sub( exp_sp, sub( Q31, tmp_exp ) ) );
     713             : 
     714             :     /* *tilt_flt = (float)(r1/sqrt(r0)); */
     715      175117 :     L_temp = L_deposit_l( BASOP_Util_Divide3232_Scale( L_ener, L_temp, &exp2 ) );
     716      175117 :     IF( GT_16( sub( exp2, 4 ), norm_l( L_temp ) ) )
     717             :     {
     718       13635 :         L_temp = MAX_16;
     719       13635 :         move32();
     720             :     }
     721             :     ELSE
     722             :     {
     723      161482 :         L_temp = L_shl( L_temp, sub( exp2, 4 ) ); // Output is expected in Q11
     724             :     }
     725             :     BASOP_SATURATE_WARNING_ON_EVS
     726      175117 :     return extract_h( L_shl_sat( L_temp, 16 ) );
     727             : }
     728             : 
     729             : /*-------------------------------------------------------------------*
     730             :  * calc_norm_envelop()
     731             :  *
     732             :  * calculate normalized parameter
     733             :  *-------------------------------------------------------------------*/
     734             : 
     735       36312 : void calc_norm_envelop_fx(
     736             :     const Word16 SWB_signal[], /* i  : SWB spectrum            Q_syn*/
     737             :     Word32 *envelope,          /* o  : normalized envelope     Q_syn*/
     738             :     const Word16 L_swb_norm,   /* i  : length of envelope      Q0   */
     739             :     const Word16 SWB_flength,  /* i  : Length of input/output       */
     740             :     const Word16 st_offset     /* i  : offset                       */
     741             : )
     742             : {
     743             :     Word16 i, lookback, env_index, n_freq, n_lag_now, n_lag, tmp;
     744             : 
     745       36312 :     lookback = shr( L_swb_norm, 1 );
     746       36312 :     env_index = add( swb_bwe_subband[0], st_offset );
     747       36312 :     n_lag_now = L_swb_norm;
     748       36312 :     move16();
     749       36312 :     tmp = sub( add( SWB_flength, st_offset ), L_swb_norm );
     750     8288619 :     FOR( n_freq = sub( add( swb_bwe_trans_subband[0], st_offset ), lookback ); n_freq < tmp; n_freq++ )
     751             :     {
     752             :         /* Apply MA filter */
     753     8252307 :         Word32 L_tmp = L_deposit_l( 0 );
     754    85964694 :         FOR( n_lag = 0; n_lag < n_lag_now; n_lag++ )
     755             :         {
     756    77712387 :             L_tmp = L_add( L_tmp, abs_s( SWB_signal[n_freq + n_lag] ) );
     757             :         }
     758     8252307 :         envelope[env_index] = L_tmp;
     759     8252307 :         move32();
     760     8252307 :         env_index++;
     761             :     }
     762             : 
     763       36312 :     i = 0;
     764       36312 :     move16();
     765       36312 :     tmp = sub( add( SWB_flength, st_offset ), lookback );
     766      261045 :     FOR( n_freq = sub( add( SWB_flength, st_offset ), L_swb_norm ); n_freq < tmp; n_freq++ )
     767             :     {
     768             :         Word32 L_tmp;
     769             : 
     770      224733 :         n_lag_now = sub( L_swb_norm, i );
     771             :         /* Apply MA filter */
     772      224733 :         L_tmp = L_deposit_l( 0 );
     773     4353210 :         FOR( n_lag = 0; n_lag < n_lag_now; n_lag++ )
     774             :         {
     775     4128477 :             L_tmp = L_add( L_tmp, abs_s( SWB_signal[n_freq + n_lag] ) );
     776             :         }
     777      224733 :         envelope[env_index] = L_tmp;
     778      224733 :         move32();
     779      224733 :         env_index++;
     780      224733 :         i = add( i, 1 );
     781             :     }
     782             : 
     783       36312 :     return;
     784             : }
     785             : 
     786             : /*==========================================================================*/
     787             : /* FUNCTION      : void WB_BWE_decoding_fx ()                               */
     788             : /*--------------------------------------------------------------------------*/
     789             : /* PURPOSE       : WB BWE decoder                                           */
     790             : /*--------------------------------------------------------------------------*/
     791             : /* INPUT ARGUMENTS  :                                                       */
     792             : /* _Word16 *core_dec_freq     i  : Frequency domain core decoded signal Q_syn*/
     793             : /* _Word16 *WB_fenv       i  : WB frequency envelopes   Q3                  */
     794             : /* _Word16 WB_flength         i  : Length of input/output                   */
     795             : /* _Word16 mode               i  : classification for WB signal             */
     796             : /* _Word16 prev_mode          i  : classification for last frame            */
     797             : /* _Word16 last_extl          i  : extl. layer for last frame               */
     798             : /* _Word16 extl               i  : extension layer                          */
     799             : /* _Word16 coder_type         i  : coding type                              */
     800             : /* _Word16 total_brate        i  : core layer bitrate                       */
     801             : /* _Word16 prev_coder_type    i  : coding type of last frame                */
     802             : /* _Word16 Q_syn        i  : Q format                                       */
     803             : /*--------------------------------------------------------------------------*/
     804             : /* OUTPUT ARGUMENTS :                                                       */
     805             : /* _Word16 *WB_signal,         o  : WB signal in MDCT domain          Q_syn */
     806             : /*--------------------------------------------------------------------------*/
     807             : /* INPUT/OUTPUT ARGUMENTS :                         */
     808             : /* _Word32 *prev_Energy,       i/o: energy for last frame           Q_syn   */
     809             : /* _Word16 *prev_WB_fenv,      i/o: envelope for last frame         Q3      */
     810             : /* _Word16 *prev_L_wb_norm,    i/o: length for last frame wb norm   Q0      */
     811             : /* _Word16 *Seed,              i/o: random generator seed           Q15     */
     812             : /* _Word16 *prev_flag,         i/o: attenu flag of last frame        Q0     */
     813             : /*--------------------------------------------------------------------------*/
     814             : /* RETURN ARGUMENTS :                           */
     815             : /*           _ None                         */
     816             : /*--------------------------------------------------------------------------*/
     817             : /* CALLED FROM :                              */
     818             : /*==========================================================================*/
     819        8937 : void WB_BWE_decoding_fx(
     820             :     const Word16 *core_dec_freq, /* i  : Frequency domain core decoded signal */
     821             :     Word16 *WB_fenv,             /* i  : WB frequency envelopes               */
     822             :     Word32 *WB_signal_32,        /* o  : WB signal in MDCT domain             */
     823             :     const Word16 WB_flength,     /* i  : Length of input/output               */
     824             :     const Word16 mode,           /* i  : classification for WB signal         */
     825             :     const Word16 last_extl,      /* i  : extl. layer for last frame           */
     826             :     Word32 *prev_Energy,         /* i/o: energy for last frame                */
     827             :     Word16 *prev_WB_fenv,        /* i/o: envelope for last frame              */
     828             :     Word16 *prev_L_wb_norm,      /* i/o: length for last frame wb norm        */
     829             :     const Word16 extl,           /* i  : extension layer                      */
     830             :     const Word16 coder_type,     /* i  : coding type                          */
     831             :     const Word32 total_brate,    /* i  : core layer bitrate                   */
     832             :     Word16 *Seed,                /* i/o: random generator seed                */
     833             :     Word16 *prev_flag,           /* i/o: attenu flag of last frame            */
     834             :     Word16 prev_coder_type,      /* i  : coding type of last frame            */
     835             :     Word16 Q_syn,
     836             :     Word16 *Q_syn_hb /*o   : Q value of WB_signal_32              */
     837             : )
     838             : {
     839             :     Word16 n_freq, n_band;
     840             :     Word16 i, L;
     841             :     Word32 envelope[L_FRAME16k];
     842             :     Word32 energy, EnergyL;
     843             :     Word32 *pit1;
     844             :     Word16 WB_signal[L_FRAME16k];
     845             :     Word16 L_wb_norm, wfenv[2];
     846             :     Word16 alfa, beta;
     847        8937 :     Word16 flag = 0;
     848        8937 :     Word16 core_type = 1;
     849        8937 :     move16();
     850        8937 :     move16();
     851             :     Word16 tmp, tmp1, exp, tmp2;
     852             :     Word32 L_tmp, L_tmp1, L_tmp2;
     853             :     Word32 prev_ener_alpha, prev_ener_beta;
     854             :     Word16 signum[L_FRAME16k];
     855             :     Word16 inv_L_wb_norm, weight;
     856             : 
     857        8937 :     calc_normal_length_fx( ACELP_CORE, core_dec_freq, mode, extl, &L_wb_norm, prev_L_wb_norm, Q_syn );
     858        8937 :     set32_fx( WB_signal_32, 0, L_FRAME16k );
     859        8937 :     set16_fx( WB_signal, 0, L_FRAME16k );
     860             : 
     861             :     /* copy excitation */
     862        8937 :     test();
     863        8937 :     if ( NE_16( coder_type, AUDIO ) && LE_32( total_brate, ACELP_8k00 ) )
     864             :     {
     865        4057 :         core_type = 0;
     866        4057 :         move16();
     867             :     }
     868             : 
     869        8937 :     IF( core_type == 0 )
     870             :     {
     871        4057 :         Copy( &core_dec_freq[160], &WB_signal[240], 80 );
     872             :     }
     873             :     ELSE
     874             :     {
     875        4880 :         Copy( &core_dec_freq[80], &WB_signal[240], 80 );
     876             :     }
     877             : 
     878             :     /* calculate envelope */
     879        8937 :     calc_norm_envelop_fx( WB_signal, envelope, L_wb_norm, WB_flength, 0 );
     880        8937 :     test();
     881        8937 :     IF( coder_type != UNVOICED && total_brate <= ACELP_8k00 )
     882             :     {
     883        7704 :         exp = norm_s( L_wb_norm );
     884        7704 :         inv_L_wb_norm = shl( div_s( shl( 1, sub( 14, exp ) ), L_wb_norm ), sub( exp, 14 ) ); /* Q(15) */
     885        7704 :         IF( NE_16( mode, HARMONIC ) )
     886             :         {
     887        6238 :             tmp = add( shl( inv_L_wb_norm, 1 ), inv_L_wb_norm );
     888        6238 :             weight = s_max( s_min( tmp, 16384 /* 0.5 in Q15 */ ), 8192 /*0.25 in Q15 */ );
     889             :         }
     890             :         ELSE
     891             :         {
     892        1466 :             weight = 8192; /*0.25 in Q15 */
     893        1466 :             move16();
     894             :         }
     895      624024 :         FOR( n_freq = swb_bwe_subband[0]; n_freq < swb_bwe_subband[4]; n_freq++ )
     896             :         {
     897      616320 :             signum[n_freq] = 1;
     898      616320 :             move16();
     899      616320 :             IF( WB_signal[n_freq] < 0 )
     900             :             {
     901      308589 :                 signum[n_freq] = -1;
     902      308589 :                 move16();
     903      308589 :                 WB_signal[n_freq] = negate( WB_signal[n_freq] );
     904      308589 :                 move16();
     905             :             }
     906      616320 :             L_tmp = Mult_32_16( envelope[n_freq], inv_L_wb_norm ); /* Q_syn */
     907      616320 :             L_tmp = Mult_32_16( L_tmp, 14746 );                    /* Q_syn */
     908      616320 :             L_tmp1 = L_deposit_l( WB_signal[n_freq] );             /* Q_syn */
     909      616320 :             L_tmp = L_sub( L_tmp1, L_tmp );                        /* Q_syn */
     910      616320 :             WB_signal[n_freq] = extract_l( L_tmp );                /* Q_syn */
     911      616320 :             move16();
     912      616320 :             IF( L_tmp > 0 )
     913             :             {
     914      452559 :                 tmp = sub( 18022, weight );                                /* Q15 */
     915      452559 :                 WB_signal[n_freq] = extract_l( Mult_32_16( L_tmp, tmp ) ); /* Q_syn */
     916      452559 :                 move16();
     917             :             }
     918             : 
     919      616320 :             IF( NE_16( signum[n_freq], 1 ) )
     920             :             {
     921      308589 :                 WB_signal[n_freq] = negate( WB_signal[n_freq] );
     922      308589 :                 move16();
     923             :             }
     924             :         }
     925             :     }
     926             : 
     927             :     /* Normalize with envelope */
     928      723897 :     FOR( n_freq = swb_bwe_subband[0]; n_freq < swb_bwe_subband[4]; n_freq++ )
     929             :     {
     930      714960 :         IF( envelope[n_freq] != 0 )
     931             :         {
     932      714960 :             exp = norm_l( envelope[n_freq] );
     933      714960 :             tmp = extract_h( L_shl( envelope[n_freq], exp ) );
     934      714960 :             exp = sub( sub( 30, exp ), Q_syn );
     935      714960 :             tmp = div_s( 16384, tmp );
     936      714960 :             L_tmp = L_shr( L_mult0( WB_signal[n_freq], tmp ), add( exp, Q_syn ) ); /*Q15 */
     937      714960 :             WB_signal[n_freq] = extract_l( L_tmp );                                /*Q15 */
     938      714960 :             move16();
     939             :         }
     940             :         ELSE
     941             :         {
     942           0 :             WB_signal[n_freq] = shl( WB_signal[n_freq], sub( 15, Q_syn ) ); /*Q15 */
     943           0 :             move16();
     944             :         }
     945             :     }
     946             : 
     947        8937 :     L = 1;
     948        8937 :     move16();
     949        8937 :     if ( EQ_16( mode, HARMONIC ) )
     950             :     {
     951        1468 :         L = 4;
     952        1468 :         move16();
     953             :     }
     954             : 
     955        8937 :     IF( EQ_16( coder_type, UNVOICED ) )
     956             :     {
     957        2430 :         FOR( n_freq = swb_bwe_subband[0]; n_freq < swb_bwe_subband[4]; n_freq++ )
     958             :         {
     959        2400 :             *Seed = extract_l( L_mac0( 20101L, *Seed, 12345 ) );
     960        2400 :             L_tmp = L_shl( *Seed, add( Q_syn, 1 ) );
     961        2400 :             WB_signal_32[n_freq] = L_tmp;
     962        2400 :             move32();
     963             :         }
     964             :     }
     965             :     ELSE
     966             :     {
     967       40131 :         FOR( n_band = 0; n_band < 4; n_band += L )
     968             :         {
     969       31224 :             energy = L_deposit_l( 0 );
     970      743784 :             FOR( n_freq = swb_bwe_subband[n_band]; n_freq < swb_bwe_subband[n_band + L]; n_freq++ )
     971             :             {
     972      712560 :                 L_tmp = L_mult0( WB_signal[n_freq], WB_signal[n_freq] ); /*Q30 */
     973      712560 :                 energy = L_add( energy, L_shr( L_tmp, 5 ) );             /*Q25 */
     974             :             }
     975             : 
     976       31224 :             tmp = sub( swb_bwe_subband[n_band + L], swb_bwe_subband[n_band] );
     977       31224 :             tmp = div_s( 1, tmp );              /*Q15 */
     978       31224 :             energy = Mult_32_16( energy, tmp ); /*Q(15+25-15)->Q(25) */
     979             : 
     980       31224 :             exp = norm_l( energy );
     981       31224 :             L_tmp1 = L_shl( energy, exp );
     982       31224 :             exp = 31 - exp - ( 25 );
     983       31224 :             move16();
     984       31224 :             L_tmp1 = Isqrt_lc( L_tmp1, &exp ); /*Q(31-exp) */
     985             : 
     986      743784 :             FOR( n_freq = swb_bwe_subband[n_band]; n_freq < swb_bwe_subband[n_band + L]; n_freq++ )
     987             :             {
     988      712560 :                 L_tmp2 = Mult_32_16( L_tmp1, WB_signal[n_freq] );
     989      712560 :                 WB_signal_32[n_freq] = L_shl( L_tmp2, sub( add( exp, Q_syn ), 15 ) );
     990      712560 :                 move32();
     991             :             }
     992             :         }
     993             :     }
     994             : 
     995        8937 :     EnergyL = L_deposit_l( 0 );
     996        8937 :     IF( EQ_16( core_type, 1 ) )
     997             :     {
     998        4880 :         test();
     999        4880 :         IF( NE_16( prev_coder_type, AUDIO ) && LE_32( total_brate, ACELP_8k00 ) )
    1000             :         {
    1001        5346 :             FOR( i = 160; i < 240; i++ )
    1002             :             {
    1003        5280 :                 EnergyL = L_add( abs_s( core_dec_freq[i] ), EnergyL );
    1004             :             }
    1005             :         }
    1006             :         ELSE
    1007             :         {
    1008      775054 :             FOR( i = 80; i < 240; i++ )
    1009             :             {
    1010      770240 :                 EnergyL = L_add( abs_s( core_dec_freq[i] ), EnergyL );
    1011             :             }
    1012             :         }
    1013             : 
    1014        4880 :         IF( LE_32( total_brate, ACELP_8k00 ) )
    1015             :         {
    1016        3659 :             alfa = 26214;
    1017        3659 :             move16(); /*0.8f in Q15; */
    1018        3659 :             beta = 10240;
    1019        3659 :             move16(); /*1.25f in Q13; */
    1020             :         }
    1021             :         ELSE
    1022             :         {
    1023        1221 :             alfa = 16384;
    1024        1221 :             move16(); /* 0.5f in Q15; */
    1025        1221 :             beta = 16384;
    1026        1221 :             move16(); /*2.0f in Q13 */
    1027             :         }
    1028             :     }
    1029             :     ELSE
    1030             :     {
    1031        4057 :         IF( EQ_16( prev_coder_type, AUDIO ) )
    1032             :         {
    1033         644 :             FOR( i = 80; i < 240; i++ )
    1034             :             {
    1035         640 :                 EnergyL = L_add( abs_s( core_dec_freq[i] ), EnergyL ); /*Q_syn */
    1036             :             }
    1037             :         }
    1038             :         ELSE
    1039             :         {
    1040      328293 :             FOR( i = 160; i < 240; i++ )
    1041             :             {
    1042      324240 :                 EnergyL = L_add( abs_s( core_dec_freq[i] ), EnergyL );
    1043             :             }
    1044             :         }
    1045             : 
    1046        4057 :         test();
    1047        4057 :         IF( EQ_16( prev_coder_type, coder_type ) && GT_16( WB_fenv[0], prev_WB_fenv[0] ) )
    1048             :         {
    1049         325 :             alfa = 13107;
    1050         325 :             move16(); /*.4 in Q15 */
    1051         325 :             beta = 20480;
    1052         325 :             move16(); /*2.5 in Q13 */
    1053             :         }
    1054             :         ELSE
    1055             :         {
    1056        3732 :             alfa = 19661;
    1057        3732 :             move16(); /*.6 in Q15 */
    1058        3732 :             beta = 13681;
    1059        3732 :             move16(); /*1.67 in Q13 */
    1060             :         }
    1061             : 
    1062        4057 :         test();
    1063        4057 :         test();
    1064        4057 :         test();
    1065        4057 :         IF( EQ_16( coder_type, GENERIC ) || ( GT_32( EnergyL, L_shr( *prev_Energy, 1 ) ) && GT_32( *prev_Energy, L_shr( EnergyL, 1 ) ) && EQ_16( *prev_flag, 1 ) ) )
    1066             :         {
    1067          59 :             WB_fenv[0] = shr( WB_fenv[0], 1 );
    1068          59 :             move16();
    1069          59 :             WB_fenv[1] = shr( WB_fenv[1], 1 );
    1070          59 :             move16();
    1071          59 :             flag = 1;
    1072          59 :             move16();
    1073             :         }
    1074             :     }
    1075        8937 :     L_tmp1 = Mult_32_16( EnergyL, prev_WB_fenv[0] );               /*Qsyn+3-15 */
    1076        8937 :     L_tmp2 = Mult_32_16( *prev_Energy, WB_fenv[0] );               /*Q_syn+3-15 */
    1077        8937 :     prev_ener_alpha = Mult_32_16( *prev_Energy, alfa );            /*Q_syn+15-15->Q_syn */
    1078        8937 :     prev_ener_beta = L_shl( Mult_32_16( *prev_Energy, beta ), 2 ); /*Q_syn+13-15+2 ->Q_syn */
    1079             : 
    1080        8937 :     test();
    1081        8937 :     test();
    1082        8937 :     IF( ( EQ_16( mode, HARMONIC ) && GT_16( shr( WB_fenv[0], 2 ), WB_fenv[1] ) ) || EQ_16( mode, NORMAL ) )
    1083             :     {
    1084        7491 :         test();
    1085        7491 :         test();
    1086        7491 :         test();
    1087        7491 :         test();
    1088        7491 :         test();
    1089        7491 :         test();
    1090        7491 :         test();
    1091        7491 :         test();
    1092        7491 :         test();
    1093        7491 :         test();
    1094        7491 :         test();
    1095        7491 :         test();
    1096        7491 :         test();
    1097        7491 :         IF( EQ_16( last_extl, WB_BWE ) &&
    1098             :             ( ( EQ_16( prev_coder_type, AUDIO ) && NE_16( coder_type, AUDIO ) ) ||
    1099             :               ( NE_16( prev_coder_type, AUDIO ) && EQ_16( coder_type, AUDIO ) ) ) &&
    1100             :             LE_32( total_brate, ACELP_8k00 ) )
    1101             :         {
    1102           8 :             IF( GT_16( WB_fenv[0], prev_WB_fenv[0] ) )
    1103             :             {
    1104             :                 /*wfenv[0]= add(mult_r(9830, WB_fenv[0]), mult_r(22938, prev_WB_fenv[0]));//Q3 */
    1105           4 :                 wfenv[0] = round_fx( L_mac( L_mult( 9830, WB_fenv[0] ), 22938, prev_WB_fenv[0] ) ); /*Q3 */
    1106           4 :                 move16();
    1107             :                 /*wfenv[1]= add(mult_r(9830, WB_fenv[1]), mult_r(22938, prev_WB_fenv[1]));//Q3 */
    1108           4 :                 wfenv[1] = round_fx( L_mac( L_mult( 9830, WB_fenv[1] ), 22938, prev_WB_fenv[1] ) ); /*Q3 */
    1109           4 :                 move16();
    1110             :             }
    1111             :             ELSE
    1112             :             {
    1113             :                 /*wfenv[0]= add(mult_r(16384,WB_fenv[0]),mult_r(16384,prev_WB_fenv[0]));//Q3 */
    1114           4 :                 wfenv[0] = round_fx( L_mac( L_mult( 16384, WB_fenv[0] ), 16384, prev_WB_fenv[0] ) ); /*Q3 */
    1115           4 :                 move16();
    1116             :                 /*wfenv[1]= add(mult_r(13108,WB_fenv[1]),mult_r(13108,prev_WB_fenv[1]));//Q3 */
    1117           4 :                 wfenv[1] = round_fx( L_mac( L_mult( 13108, WB_fenv[1] ), 13108, prev_WB_fenv[1] ) ); /*Q3 */
    1118           4 :                 move16();
    1119             :             }
    1120             :         }
    1121        7483 :         ELSE IF( EQ_16( last_extl, WB_BWE ) && LT_32( L_tmp1, L_tmp2 ) && GT_16( WB_fenv[0], prev_WB_fenv[0] ) &&
    1122             :                  NE_16( coder_type, AUDIO ) && NE_16( coder_type, UNVOICED ) && LE_32( total_brate, ACELP_8k00 ) )
    1123             :         {
    1124             :             /*wfenv[0]= add(mult_r(9830,WB_fenv[0]),mult_r(22938,prev_WB_fenv[0]));//Q3 */
    1125          73 :             wfenv[0] = round_fx( L_mac( L_mult( 9830, WB_fenv[0] ), 22938, prev_WB_fenv[0] ) ); /*Q3 */
    1126          73 :             move16();
    1127             :             /*wfenv[1]= add(mult_r(9830,WB_fenv[1]),mult_r(22938,prev_WB_fenv[1]));//Q3 */
    1128          73 :             wfenv[1] = round_fx( L_mac( L_mult( 9830, WB_fenv[1] ), 22938, prev_WB_fenv[1] ) ); /*Q3 */
    1129          73 :             move16();
    1130             :         }
    1131        7410 :         ELSE IF( EQ_16( last_extl, WB_BWE ) && GT_32( EnergyL, prev_ener_alpha ) && GT_32( prev_ener_beta, EnergyL ) &&
    1132             :                  NE_16( prev_coder_type, UNVOICED ) )
    1133             :         {
    1134             :             /*wfenv[0] = add(shr(WB_fenv[0],1), shr(prev_WB_fenv[0],1));//Q3 */
    1135        5968 :             wfenv[0] = round_fx( L_mac( L_mult( WB_fenv[0], 16384 ), prev_WB_fenv[0], 16384 ) ); /*Q3 */
    1136        5968 :             move16();
    1137             :             /*wfenv[1] = add(shr(WB_fenv[1],1), shr(prev_WB_fenv[1],1));//Q3 */
    1138        5968 :             wfenv[1] = round_fx( L_mac( L_mult( WB_fenv[1], 16384 ), prev_WB_fenv[1], 16384 ) ); /*Q3 */
    1139        5968 :             move16();
    1140             :         }
    1141             :         ELSE
    1142             :         {
    1143        1442 :             wfenv[0] = WB_fenv[0];
    1144        1442 :             move16();
    1145        1442 :             wfenv[1] = WB_fenv[1];
    1146        1442 :             move16();
    1147             :         }
    1148      307131 :         FOR( n_freq = swb_bwe_subband[0]; n_freq < swb_bwe_subband[2]; n_freq++ )
    1149             :         {
    1150      299640 :             WB_signal_32[n_freq] = Mult_32_16( WB_signal_32[n_freq], wfenv[0] );
    1151      299640 :             move32(); /* Q_syn+3+1 */
    1152             :         }
    1153             : 
    1154      307131 :         FOR( n_freq = swb_bwe_subband[2]; n_freq < swb_bwe_subband[4]; n_freq++ )
    1155             :         {
    1156      299640 :             WB_signal_32[n_freq] = Mult_32_16( WB_signal_32[n_freq], wfenv[1] );
    1157      299640 :             move32(); /* Q_syn+3+1 */
    1158             :         }
    1159             : 
    1160        7491 :         prev_WB_fenv[0] = wfenv[0];
    1161        7491 :         move16();
    1162        7491 :         prev_WB_fenv[1] = wfenv[1];
    1163        7491 :         move16();
    1164             :     }
    1165             :     ELSE
    1166             :     {
    1167        1446 :         wfenv[0] = add( shr( WB_fenv[0], 1 ), shr( WB_fenv[1], 1 ) ); /*Q3 */
    1168             : 
    1169        1446 :         test();
    1170        1446 :         test();
    1171        1446 :         IF( EQ_16( last_extl, WB_BWE ) && GT_32( EnergyL, L_shr( *prev_Energy, 1 ) ) && GT_32( *prev_Energy, L_shr( EnergyL, 1 ) ) )
    1172             :         {
    1173        1400 :             L_tmp1 = L_mac( L_mult( 8192 /* 0.25 in Q15 */, wfenv[0] ), 12288 /* 0.375 in Q15 */, prev_WB_fenv[0] ); /* Q3 */
    1174        1400 :             wfenv[0] = round_fx( L_mac( L_tmp1, 12288 /* 0.375 in Q15 */, prev_WB_fenv[1] ) );                       /* Q3 */
    1175             :         }
    1176      117126 :         FOR( n_freq = swb_bwe_subband[0]; n_freq < swb_bwe_subband[4]; n_freq++ )
    1177             :         {
    1178      115680 :             WB_signal_32[n_freq] = Mult_32_16( WB_signal_32[n_freq], wfenv[0] );
    1179      115680 :             move32(); /* Q_syn+3+1 */
    1180             :         }
    1181        1446 :         prev_WB_fenv[0] = wfenv[0]; /* Q3 */
    1182        1446 :         move16();
    1183        1446 :         prev_WB_fenv[1] = wfenv[0]; /* Q3 */
    1184        1446 :         move16();
    1185             :     }
    1186             : 
    1187        8937 :     *prev_flag = flag;
    1188        8937 :     move16();
    1189        8937 :     *prev_Energy = EnergyL;
    1190        8937 :     move32();
    1191        8937 :     pit1 = &WB_signal_32[240];
    1192             : 
    1193      151929 :     FOR( n_freq = 0; n_freq < 16; n_freq++ )
    1194             :     {
    1195      142992 :         tmp1 = extract_l( L_mult0( n_freq, 1638 /* 0.05 in Q15 */ ) ); /*Q15 */
    1196      142992 :         tmp2 = add( 6554 /* 0.2 in Q15 */, tmp1 );                     /*Q15 */
    1197      142992 :         L_tmp1 = Mult_32_16( *pit1, tmp2 );                            /*Q_syn+3+1 */
    1198      142992 :         *( pit1++ ) = L_tmp1;
    1199      142992 :         move32();
    1200             :     }
    1201             : 
    1202        8937 :     IF( EQ_16( core_type, 1 ) )
    1203             :     {
    1204        4880 :         pit1 = &WB_signal_32[280];
    1205      200080 :         FOR( n_freq = 0; n_freq < 40; n_freq++ )
    1206             :         {
    1207      195200 :             tmp1 = extract_l( L_mult0( n_freq, 655 /* 0.02 in Q15 */ ) ); /*Q15 */
    1208      195200 :             tmp2 = sub( 32767, tmp1 );
    1209      195200 :             L_tmp1 = Mult_32_16( *pit1, tmp2 ); /*Q_syn+3+1 */
    1210      195200 :             *( pit1++ ) = L_tmp1;
    1211      195200 :             move32();
    1212             :         }
    1213             :     }
    1214             :     ELSE
    1215             :     {
    1216        4057 :         pit1 = &WB_signal_32[300];
    1217       85197 :         FOR( n_freq = 0; n_freq < 20; n_freq++ )
    1218             :         {
    1219       81140 :             tmp1 = extract_l( L_mult0( n_freq, 1311 /* 0.04 in Q15 */ ) ); /*Q15 */
    1220       81140 :             tmp2 = sub( 32767, tmp1 );
    1221       81140 :             L_tmp1 = Mult_32_16( *pit1, tmp2 ); /*Q_syn+3+1 */
    1222       81140 :             *( pit1++ ) = L_tmp1;
    1223       81140 :             move32();
    1224             :         }
    1225             :     }
    1226        8937 :     pit1 = &WB_signal_32[240];
    1227        8937 :     tmp = Find_Max_Norm32( pit1, 80 );
    1228      723897 :     FOR( i = 0; i < 80; i++ )
    1229             :     {
    1230      714960 :         L_tmp = *pit1;
    1231      714960 :         *( pit1++ ) = L_shl( L_tmp, sub( tmp, 1 ) );
    1232      714960 :         move32();
    1233             :     }
    1234        8937 :     *Q_syn_hb = add( Q_syn, add( tmp, 3 ) );
    1235        8937 :     move16();
    1236        8937 :     return;
    1237             : }
    1238             : 
    1239             : 
    1240             : /*==========================================================================*/
    1241             : /* FUNCTION      : void SWB_BWE_decoding_fx()                               */
    1242             : /*--------------------------------------------------------------------------*/
    1243             : /* PURPOSE       :    SWB BWE decoder                                       */
    1244             : /*--------------------------------------------------------------------------*/
    1245             : /* INPUT ARGUMENTS                                                          */
    1246             : /* _(Word16*) core_dec_freq     :Frequency domain core decoded signal Q_syn */
    1247             : /* _(Word16) SWB_flength    :Length of input/output         Q0              */
    1248             : /* _(Word16) mode         :classification for SWB signal                    */
    1249             : /* _(Word16) tilt_nb      :tilt of synthesis wb signal      Q11             */
    1250             : /* _(Word16) st_offset      :offset value due to different core             */
    1251             : /* _(Word16) Q_syn          :Q format                                       */
    1252             : /*--------------------------------------------------------------------------*/
    1253             : /* OUTPUT ARGUMENTS :                                                       */
    1254             : /*  _(Word16*)SWB_fenv        : SWB frequency envelopes         Q1          */
    1255             : /*  _(Word16*)prev_Energy     : energy for last frame           Q3          */
    1256             : /*  _(Word16*)prev_SWB_fenv   : envelope for last frame         Q1          */
    1257             : /*  _(Word16*)prev_L_swb_norm : length for last frame wb norm   Q0          */
    1258             : /*  _(Word16*)Seed        : random generator seed           Q0              */
    1259             : /*--------------------------------------------------------------------------*/
    1260             : /* INPUT/OUTPUT ARGUMENTS :                                                 */
    1261             : /*  _(Word16*)SWB_signal    : SWB signal in MDCT domain       Q0            */
    1262             : /*  _(Word16*)frica_flag    : fricative signal flag           Q0            */
    1263             : /*--------------------------------------------------------------------------*/
    1264             : /* RETURN ARGUMENTS :                                                       */
    1265             : /*   _ None                                                                 */
    1266             : /*--------------------------------------------------------------------------*/
    1267             : 
    1268       12392 : void SWB_BWE_decoding_fx(
    1269             :     const Word16 *core_dec_freq, /* i  : Frequency domain core decoded signal  */
    1270             :     Word16 *SWB_fenv,            /* i/o: SWB frequency envelopes               */
    1271             :     Word32 *SWB_signal_32,       /* o  : SWB signal in MDCT domain             */
    1272             :     const Word16 SWB_flength,    /* i  : Length of input/output                */
    1273             :     const Word16 mode,           /* i  : classification for SWB signal         */
    1274             :     Word16 *frica_flag,          /* o  : fricative signal flag                 */
    1275             :     Word16 *prev_Energy,         /* i/o: energy for last frame                 */
    1276             :     Word16 *prev_SWB_fenv,       /* i/o: envelope for last frame               */
    1277             :     Word16 *prev_L_swb_norm,     /* i/o: length for last frame wb norm         */
    1278             :     const Word16 tilt_nb,        /* i  : tilt of synthesis wb signal           */
    1279             :     Word16 *Seed,                /* i/o: random generator seed                 */
    1280             :     const Word16 st_offset,      /* i  : offset value due to different core    */
    1281             :     Word16 *prev_weight,         /* i/o: excitation weight value of last frame */
    1282             :     const Word16 extl,           /* i  : extension layer                       */
    1283             :     Word16 Q_syn,
    1284             :     const Word16 last_extl /* i  : extension layer of last frame         */
    1285             : )
    1286             : {
    1287             :     Word16 n_freq, n_band, L, L_swb_norm;
    1288             :     Word32 *pit1_32;
    1289             :     Word16 *pit1;
    1290             :     Word32 envelope[L_FRAME32k];
    1291             :     Word32 fenvL, EnergyL, Energy, energy, L_mean;
    1292             :     Word16 fenvL_16, EnergyL_16, Energy_16, tmp, exp, exp1;
    1293             :     Word16 SWB_signal[L_FRAME32k];
    1294             :     Word16 mean, factor, factor1, tmp1, tmp2, tmp3, tmp_exp, tmp_ener, weight, wfenv;
    1295             :     Word32 L_tmp, L_tmp3, L_tmp4, Ltmp_ener, L_tmp1;
    1296             :     Word32 L_energy;
    1297             :     Word16 signum[L_FRAME32k];
    1298             :     Word16 inv_L_swb_norm;
    1299             : 
    1300       12392 :     fenvL = L_deposit_l( 0 );
    1301       12392 :     EnergyL = L_deposit_l( 0 );
    1302      210664 :     FOR( n_freq = 224 + st_offset; n_freq < swb_bwe_trans_subband[0] + st_offset; n_freq++ )
    1303             :     {
    1304      198272 :         fenvL = L_mac0_sat( fenvL, core_dec_freq[n_freq], core_dec_freq[n_freq] ); /*2*Q_syn */
    1305             :     }
    1306             : 
    1307     2986472 :     FOR( n_freq = 16; n_freq < L_FRAME; n_freq++ )
    1308             :     {
    1309     2974080 :         EnergyL = L_mac0_sat( EnergyL, core_dec_freq[n_freq], core_dec_freq[n_freq] ); /*2*Q_syn */
    1310             :     }
    1311             : 
    1312       12392 :     fenvL_16 = 0;
    1313       12392 :     move16();
    1314       12392 :     IF( fenvL != 0 )
    1315             :     {
    1316       12384 :         exp = norm_l( fenvL ); /* In 2*Q_syn */
    1317       12384 :         tmp = extract_h( L_shl( fenvL, exp ) );
    1318       12384 :         exp = sub( exp, sub( 30, add( shl( Q_syn, 1 ), 4 ) ) ); /*+4(/16) */
    1319             : 
    1320       12384 :         tmp = div_s( 16384, tmp );
    1321       12384 :         L_tmp = L_deposit_h( tmp );
    1322       12384 :         L_tmp = Isqrt_lc( L_tmp, &exp );
    1323             : 
    1324       12384 :         fenvL_16 = round_fx_sat( L_shl_sat( L_tmp, sub( exp, 12 ) ) ); /* Q3 */
    1325             :     }
    1326       12392 :     IF( GT_16( fenvL_16, shl_sat( SWB_fenv[0], 5 ) ) )
    1327             :     {
    1328        2119 :         fenvL_16 = shl( SWB_fenv[0], 2 );
    1329        2119 :         move16();
    1330             :     }
    1331       12392 :     EnergyL = Mult_32_16( EnergyL, 17476 ); /*2*Q_syn+3; 17476=(1/15) in Q18 */
    1332       12392 :     EnergyL_16 = 0;
    1333       12392 :     move16();
    1334       12392 :     IF( EnergyL != 0 )
    1335             :     {
    1336       12392 :         exp = norm_l( EnergyL ); /* In 2*Q_syn+3 */
    1337       12392 :         tmp = extract_h( L_shl( EnergyL, exp ) );
    1338       12392 :         exp = sub( exp, sub( 30, add( shl( Q_syn, 1 ), 3 + 4 ) ) ); /*+4(/16) */
    1339             : 
    1340       12392 :         tmp = div_s( 16384, tmp );
    1341       12392 :         L_tmp = L_deposit_h( tmp );
    1342       12392 :         L_tmp = Isqrt_lc( L_tmp, &exp );
    1343             : 
    1344       12392 :         EnergyL_16 = round_fx_sat( L_shl_sat( L_tmp, sub( exp, 12 ) ) ); /* Q3 */
    1345             :     }
    1346       12392 :     calc_normal_length_fx( ACELP_CORE, core_dec_freq, mode, extl, &L_swb_norm, prev_L_swb_norm, Q_syn );
    1347             : 
    1348       12392 :     set16_fx( SWB_signal, 0, L_FRAME32k );
    1349       12392 :     IF( EQ_16( mode, TRANSIENT ) )
    1350             :     {
    1351          75 :         Energy = L_deposit_l( 0 );
    1352         375 :         FOR( n_band = 0; n_band < SWB_FENV_TRANS; n_band++ )
    1353             :         {
    1354         300 :             Energy = L_mac( Energy, SWB_fenv[n_band], SWB_fenv[n_band] ); /*Q(2*1+1)->Q3 */
    1355             :         }
    1356          75 :         exp = norm_s( SWB_FENV_TRANS );
    1357          75 :         tmp = div_s( shl( 1, sub( 14, exp ) ), SWB_FENV_TRANS ); /*Q(29-exp) */
    1358          75 :         L_tmp = Mult_32_16( Energy, tmp );                       /*Q(3+29-exp+1-16)->Q(17-exp) */
    1359          75 :         Energy_16 = round_fx( L_shl( L_tmp, add( exp, 2 ) ) );   /*Q3 */
    1360             : 
    1361             :         /* Reconstruct excitation from LF signal */
    1362          75 :         Copy( &core_dec_freq[112], &SWB_signal[240 + st_offset], 128 );
    1363          75 :         Copy( &core_dec_freq[112], &SWB_signal[368 + st_offset], 128 );
    1364          75 :         Copy( &core_dec_freq[176], &SWB_signal[496 + st_offset], 64 );
    1365             : 
    1366             :         /* calculate envelope */
    1367          75 :         calc_norm_envelop_fx( SWB_signal, envelope, L_swb_norm, SWB_flength, st_offset );
    1368             : 
    1369             :         /* Normalize with envelope */
    1370          75 :         tmp_exp = sub( 15, Q_syn );
    1371       24075 :         FOR( n_freq = swb_bwe_trans_subband[0] + st_offset; n_freq < swb_bwe_trans_subband[SWB_FENV_TRANS] + st_offset; n_freq++ )
    1372             :         {
    1373       24000 :             IF( envelope[n_freq] != 0 )
    1374             :             {
    1375       23989 :                 exp = norm_l( envelope[n_freq] );
    1376       23989 :                 tmp = extract_h( L_shl( envelope[n_freq], exp ) );
    1377       23989 :                 exp = sub( sub( 30, exp ), Q_syn );
    1378       23989 :                 tmp = div_s( 16384 /* 0.5 in Q15 */, tmp );                             /*Q(15+exp) */
    1379       23989 :                 L_tmp = L_shr( L_mult0( SWB_signal[n_freq], tmp ), add( exp, Q_syn ) ); /*Q15 */
    1380       23989 :                 SWB_signal[n_freq] = extract_l( L_tmp );                                /*Q15 */
    1381       23989 :                 move16();
    1382             :             }
    1383             :             ELSE
    1384             :             {
    1385          11 :                 SWB_signal[n_freq] = shl( SWB_signal[n_freq], tmp_exp );
    1386          11 :                 move16(); /*Q15 */
    1387             :             }
    1388             :         }
    1389             : 
    1390         375 :         FOR( n_band = 0; n_band < SWB_FENV_TRANS; n_band++ )
    1391             :         {
    1392         300 :             energy = L_deposit_l( 0 );
    1393         300 :             tmp = add( swb_bwe_trans_subband[n_band + 1], st_offset );
    1394       24300 :             FOR( n_freq = add( swb_bwe_trans_subband[n_band], st_offset ); n_freq < tmp; n_freq++ )
    1395             :             {
    1396       24000 :                 L_tmp = L_mult_sat( SWB_signal[n_freq], SWB_signal[n_freq] ); /*Q31 */
    1397       24000 :                 energy = L_add_sat( energy, L_shr( L_tmp, 6 ) );              /*Q25 */
    1398             :             }
    1399             : 
    1400         300 :             IF( energy == 0 )
    1401             :             {
    1402           0 :                 Ltmp_ener = L_mult( sqrt_swb_bwe_trans_subband_width_fx[n_band], SWB_fenv[n_band] ); /*Q13 */
    1403           0 :                 tmp = add( swb_bwe_trans_subband[n_band + 1], st_offset );
    1404           0 :                 FOR( n_freq = add( swb_bwe_trans_subband[n_band], st_offset ); n_freq < tmp; n_freq++ )
    1405             :                 {
    1406           0 :                     SWB_signal_32[n_freq] = L_shl( Mult_32_16( Ltmp_ener, SWB_signal[n_freq] ), 2 + Q_syn );
    1407           0 :                     move32(); /*15+Qsyn */
    1408             :                 }
    1409             :             }
    1410             :             ELSE
    1411             :             {
    1412         300 :                 exp1 = norm_s( swb_bwe_trans_subband_width[n_band] );
    1413         300 :                 tmp = div_s( shl( 1, sub( 14, exp1 ) ), swb_bwe_trans_subband_width[n_band] ); /*Q(29-exp1) */
    1414         300 :                 energy = Mult_32_16( energy, tmp );                                            /*Q(29-exp1+25-15)->Q(-exp1+39) */
    1415             : 
    1416         300 :                 exp = norm_l( energy );
    1417         300 :                 L_tmp = L_shl( energy, exp );
    1418             :                 /*exp = 31-exp-(-exp1+39); */
    1419         300 :                 exp = sub( sub( exp1, exp ), 8 );
    1420         300 :                 L_tmp = Isqrt_lc( L_tmp, &exp ); /*Q(31-exp) */
    1421             : 
    1422         300 :                 Ltmp_ener = Mult_32_16( L_tmp, SWB_fenv[n_band] ); /*Q(31-exp+1+1-16)->Q(17-exp) */
    1423         300 :                 tmp = add( swb_bwe_trans_subband[n_band + 1], st_offset );
    1424         300 :                 tmp_exp = add( Q_syn, sub( exp, 2 ) );
    1425       24300 :                 FOR( n_freq = add( swb_bwe_trans_subband[n_band], st_offset ); n_freq < tmp; n_freq++ )
    1426             :                 {
    1427       24000 :                     SWB_signal_32[n_freq] = L_shl( Mult_32_16( Ltmp_ener, SWB_signal[n_freq] ), tmp_exp );
    1428       24000 :                     move32(); /*15+Qsyn */
    1429             :                 }
    1430             :             }
    1431             :         }
    1432             : 
    1433         675 :         FOR( n_band = 0; n_band < 8; n_band++ )
    1434             :         {
    1435         600 :             L_tmp = L_mult( SWB_fenv[n_band / 4], SWB_fenv[n_band / 4] ); /*Q3 */
    1436         600 :             prev_SWB_fenv[n_band] = round_fx( L_shl( L_tmp, 14 ) );       /*Q1 */
    1437         600 :             move16();
    1438             :         }
    1439             : 
    1440         525 :         FOR( n_band = 0; n_band < 6; n_band++ )
    1441             :         {
    1442         450 :             L_tmp = L_mult( SWB_fenv[2 + n_band / 3], SWB_fenv[2 + n_band / 3] ); /*Q3 */
    1443         450 :             prev_SWB_fenv[8 + n_band] = round_fx( L_shl( L_tmp, 14 ) );           /*Q1 */
    1444         450 :             move16();
    1445             :         }
    1446             : 
    1447          75 :         *prev_weight = 16384 /* 0.5 in Q15 */;
    1448          75 :         move16();
    1449             :     }
    1450             :     ELSE
    1451             :     {
    1452       12317 :         Energy_16 = 0;
    1453       12317 :         move16();
    1454       12317 :         L_energy = L_deposit_l( 0 );
    1455      184755 :         FOR( n_band = 0; n_band < SWB_FENV; n_band++ )
    1456             :         {
    1457      172438 :             L_energy = L_add( L_energy, SWB_fenv[n_band] ); /*Q1 */
    1458             :         }
    1459       12317 :         exp = norm_s( SWB_FENV );
    1460       12317 :         tmp = div_s( shl( 1, sub( 14, exp ) ), SWB_FENV ); /*Q(29-exp) */
    1461       12317 :         L_tmp = Mult_32_16( L_energy, tmp );               /*Q(1+29-exp+1)->Q(15-exp) */
    1462             : 
    1463       12317 :         Energy_16 = round_fx_sat( L_shl_sat( L_tmp, add( exp, 4 ) ) ); /* Q3 */
    1464             : 
    1465       12317 :         test();
    1466       12317 :         IF( NE_16( last_extl, SWB_BWE ) && NE_16( last_extl, FB_BWE ) )
    1467             :         {
    1468        4375 :             test();
    1469        4375 :             IF( LT_16( Energy_16, shr( EnergyL_16, 4 ) ) && EQ_16( extl, FB_BWE ) )
    1470             :             {
    1471           0 :                 FOR( n_band = 0; n_band < SWB_FENV; n_band++ )
    1472             :                 {
    1473           0 :                     SWB_fenv[n_band] = mult_r( SWB_fenv[n_band], 6554 /* 0.2 in Q15 */ );
    1474           0 :                     move16();
    1475             :                 }
    1476           0 :                 fenvL_16 = mult_r( fenvL_16, 6554 /* 0.2 in Q15 */ );
    1477             :             }
    1478        4375 :             Copy( SWB_fenv, prev_SWB_fenv, SWB_FENV );
    1479             :         }
    1480             : 
    1481       12317 :         IF( EQ_16( mode, HARMONIC ) )
    1482             :         {
    1483         195 :             Copy( core_dec_freq, &SWB_signal[240 + st_offset], 240 );
    1484         195 :             Copy( &core_dec_freq[128], &SWB_signal[480 + st_offset], 80 );
    1485             :             /* calculate envelope */
    1486         195 :             calc_norm_envelop_fx( SWB_signal, envelope, L_swb_norm, SWB_flength, st_offset );
    1487             :         }
    1488             :         ELSE
    1489             :         {
    1490       12122 :             test();
    1491       12122 :             test();
    1492       12122 :             test();
    1493       12122 :             test();
    1494       12122 :             test();
    1495       12122 :             test();
    1496       12122 :             IF( EQ_16( mode, NOISE ) || ( ( GT_16( Energy_16, EnergyL_16 ) || ( GT_16( tilt_nb, 14336 /*7 in Q11*/ ) && GT_16( Energy_16, shr( EnergyL_16, 1 ) ) ) ||
    1497             :                                             GT_16( tilt_nb, 24576 /*12 in Q11*/ ) ) &&
    1498             :                                           GT_16( Energy_16, 600 /* 75 in Q3*/ ) && GT_16( fenvL_16, 200 /* 25 in Q3*/ ) ) )
    1499             :             {
    1500         782 :                 tmp = add( swb_bwe_subband[SWB_FENV], st_offset );
    1501      251022 :                 FOR( n_freq = add( swb_bwe_subband[0], st_offset ); n_freq < tmp; n_freq++ )
    1502             :                 {
    1503      250240 :                     *Seed = extract_l( L_mac0( 20101L, 12345, *Seed ) );
    1504      250240 :                     move16();
    1505      250240 :                     SWB_signal[n_freq] = mult_r( *Seed, 32767 );
    1506      250240 :                     move16(); /*Q15 */
    1507             :                 }
    1508         782 :                 if ( NE_16( mode, NOISE ) )
    1509             :                 {
    1510         134 :                     *frica_flag = 1;
    1511         134 :                     move16();
    1512             :                 }
    1513             :             }
    1514             :             ELSE
    1515             :             {
    1516             :                 /* modify SHB frequency envelopes when SHB spectrum is unflat */
    1517      158760 :                 FOR( n_band = 0; n_band < 13; n_band++ )
    1518             :                 {
    1519      147420 :                     IF( GT_16( mult_r( SWB_fenv[n_band], 29491 /* 0.9 in Q15 */ ), SWB_fenv[n_band + 1] ) )
    1520             :                     {
    1521       43782 :                         tmp = extract_l( L_mac0( 26214 /* 0.8 in Q15 */, n_band, 492 ) ); /*Q15; 0.015 in Q15 = 492  */
    1522       43782 :                         SWB_fenv[n_band + 1] = mult_r( SWB_fenv[n_band + 1], tmp );
    1523       43782 :                         move16(); /*Q1 */
    1524             :                     }
    1525             : 
    1526      147420 :                     IF( GT_16( mult_r( SWB_fenv[n_band + 1], 29491 /* 0.9 in Q15 */ ), SWB_fenv[n_band] ) )
    1527             :                     {
    1528       39416 :                         tmp = extract_l( L_mac0( 26214 /* 0.8 in Q15 */, n_band, 492 ) ); /*Q15; 0.015 in Q15 = 492  */
    1529       39416 :                         SWB_fenv[n_band] = mult_r( SWB_fenv[n_band], tmp );
    1530       39416 :                         move16(); /*Q1 */
    1531             :                     }
    1532             :                 }
    1533       11340 :                 Copy( &core_dec_freq[112], &SWB_signal[240 + st_offset], 128 );
    1534       11340 :                 Copy( &core_dec_freq[112], &SWB_signal[368 + st_offset], 128 );
    1535       11340 :                 Copy( &core_dec_freq[176], &SWB_signal[496 + st_offset], 64 );
    1536             : 
    1537       11340 :                 tmp1 = add_sat( abs_s( SWB_signal[368 + st_offset] ), abs_s( SWB_signal[369 + st_offset] ) ); /*Q_syn */
    1538       11340 :                 tmp2 = add_sat( abs_s( SWB_signal[365 + st_offset] ), abs_s( SWB_signal[366 + st_offset] ) ); /*Q_syn */
    1539       11340 :                 pit1 = &SWB_signal[368 + st_offset];
    1540       11340 :                 move16();
    1541             : 
    1542       11340 :                 test();
    1543       11340 :                 IF( ( tmp2 == 0 ) || ( LT_16( tmp2, mult_r( tmp1, 9830 /* 0.3 in Q15*/ ) ) ) )
    1544             :                 {
    1545        3308 :                     tmp3 = 9830;
    1546        3308 :                     move16(); /*0.3 in Q15 */
    1547       26464 :                     WHILE( LT_16( tmp3, 32767 ) )
    1548             :                     {
    1549       23156 :                         *pit1 = mult_r( *pit1, tmp3 );
    1550       23156 :                         move16(); /*Q_syn */
    1551       23156 :                         pit1++;
    1552       23156 :                         tmp3 = add_sat( tmp3, 3277 /* 0.1 in Q15*/ ); /*Q15 */
    1553             :                     }
    1554             :                 }
    1555        8032 :                 ELSE IF( LT_16( tmp2, tmp1 ) )
    1556             :                 {
    1557        5118 :                     exp = norm_s( tmp1 );
    1558        5118 :                     tmp = div_s( shl( 1, sub( 14, exp ) ), tmp1 );                          /*Q(29-exp) */
    1559        5118 :                     tmp3 = round_fx_sat( L_shl_sat( L_mult( tmp2, tmp ), add( exp, 2 ) ) ); /*Q15 */
    1560       12229 :                     WHILE( LT_16( tmp3, 32767 ) )
    1561             :                     {
    1562        7111 :                         *pit1 = mult_r( *pit1, tmp3 );
    1563        7111 :                         move16(); /*Q_syn */
    1564        7111 :                         pit1++;
    1565        7111 :                         tmp3 = add_sat( tmp3, 3277 /* 0.1 in Q15*/ ); /*Q15 */
    1566             :                     }
    1567             :                 }
    1568             : 
    1569       11340 :                 pit1 = &SWB_signal[367 + st_offset];
    1570             : 
    1571       11340 :                 IF( GT_16( mult_r( tmp1, 6554 /*0.2 in Q15*/ ), tmp2 ) )
    1572             :                 {
    1573             :                     /*20480 = 5 in Q12 */
    1574       19017 :                     FOR( tmp3 = 20480; tmp3 > 4096; tmp3 -= 2048 )
    1575             :                     {
    1576       16904 :                         *pit1 = round_fx_sat( L_shl_sat( L_mult( *pit1, tmp3 ), 3 ) ); /*Q_syn */
    1577       16904 :                         move16();
    1578       16904 :                         pit1--;
    1579             :                     }
    1580             :                 }
    1581             : 
    1582       11340 :                 tmp1 = add_sat( abs_s( SWB_signal[496 + st_offset] ), abs_s( SWB_signal[497 + st_offset] ) ); /*Q_syn */
    1583       11340 :                 tmp2 = add_sat( add_sat( abs_s( SWB_signal[492 + st_offset] ), abs_s( SWB_signal[493 + st_offset] ) ), add_sat( abs_s( SWB_signal[494 + st_offset] ), abs_s( SWB_signal[495 + st_offset] ) ) );
    1584       11340 :                 pit1 = &SWB_signal[496 + st_offset];
    1585             : 
    1586       11340 :                 test();
    1587       11340 :                 IF( ( tmp2 == 0 ) || ( LT_16( tmp2, mult_r( tmp1, 9830 /* 0.3 in Q15 */ ) ) ) )
    1588             :                 {
    1589         594 :                     tmp3 = 9830;
    1590         594 :                     move16(); /*0.3 in Q15 */
    1591        4752 :                     WHILE( LT_16( tmp3, 32767 ) )
    1592             :                     {
    1593        4158 :                         *pit1 = mult_r( *pit1, tmp3 );
    1594        4158 :                         move16(); /*Q_syn */
    1595        4158 :                         pit1++;
    1596        4158 :                         tmp3 = add_sat( tmp3, 3277 /* 0.1 in Q15 */ ); /*Q15 */
    1597             :                     }
    1598             :                 }
    1599       10746 :                 ELSE IF( LT_16( tmp2, tmp1 ) )
    1600             :                 {
    1601        3086 :                     exp = norm_s( tmp1 );
    1602        3086 :                     tmp = div_s( shl( 1, sub( 14, exp ) ), tmp1 );                          /*Q(29-exp) */
    1603        3086 :                     tmp3 = round_fx_sat( L_shl_sat( L_mult( tmp2, tmp ), add( exp, 2 ) ) ); /*Q15 */
    1604        4944 :                     WHILE( LT_16( tmp3, 32767 ) )
    1605             :                     {
    1606        1858 :                         *pit1 = mult_r( *pit1, tmp3 );
    1607        1858 :                         move16(); /*Q_syn */
    1608        1858 :                         pit1++;
    1609        1858 :                         tmp3 = add_sat( tmp3, 3277 /* 0.1 in Q15 */ ); /*Q15 */
    1610             :                     }
    1611             :                 }
    1612       11340 :                 pit1 = &SWB_signal[495 + st_offset];
    1613             : 
    1614       11340 :                 L_tmp3 = L_deposit_h( tmp1 );                         /*Q17 */
    1615       11340 :                 L_tmp4 = Mult_32_16( L_tmp3, 1638 /* 0.05 in Q15*/ ); /*Q17 */
    1616       11340 :                 exp = 14;
    1617       11340 :                 move16();
    1618       11340 :                 IF( tmp2 != 0 )
    1619             :                 {
    1620       11217 :                     exp = norm_s( tmp2 );
    1621       11217 :                     tmp = div_s( shl( 1, sub( 14, exp ) ), tmp2 );         /*Q(29-exp) */
    1622       11217 :                     L_tmp3 = L_shr( L_mult( tmp1, tmp ), 1 );              /*Q(30-exp+1)->Q(30-exp) (+1) due to *0.5 */
    1623       11217 :                     L_tmp4 = Mult_32_16( L_tmp3, 1638 /* 0.05 in Q15 */ ); /*Q(30-exp) */
    1624             :                 }
    1625             : 
    1626       11340 :                 L_tmp1 = L_shl( 1L, sub( 30, exp ) );
    1627       22047 :                 WHILE( GT_32( L_tmp3, L_tmp1 ) )
    1628             :                 {
    1629       10707 :                     L_tmp = Mult_32_16( L_tmp3, *pit1 );       /*Q(16-exp) */
    1630       10707 :                     *pit1-- = round_fx( L_shl( L_tmp, exp ) ); /*Q_syn */
    1631       10707 :                     move16();
    1632       10707 :                     L_tmp3 = L_sub( L_tmp3, L_tmp4 );
    1633             :                 }
    1634             : 
    1635             :                 /* calculate envelope */
    1636       11340 :                 calc_norm_envelop_fx( SWB_signal, envelope, L_swb_norm, SWB_flength, st_offset );
    1637             :             }
    1638             :         }
    1639             : 
    1640             :         /* Normalize with envelope */
    1641       12317 :         test();
    1642       12317 :         IF( *frica_flag == 0 && NE_16( mode, NOISE ) )
    1643             :         {
    1644       11535 :             L = add( swb_bwe_subband[0], st_offset );
    1645       11535 :             exp = norm_s( L_swb_norm );
    1646       11535 :             inv_L_swb_norm = shl( div_s( shl( 1, sub( 14, exp ) ), L_swb_norm ), sub( exp, 14 ) ); /* Q15 */
    1647             : 
    1648       11535 :             IF( NE_16( mode, HARMONIC ) )
    1649             :             {
    1650       11340 :                 tmp = add( shl( inv_L_swb_norm, 1 ), inv_L_swb_norm );
    1651       11340 :                 weight = s_max( s_min( tmp, 16384 /* 0.5 in Q15 */ ), 6554 /* 0.2 in Q15 */ );
    1652             :             }
    1653             :             ELSE
    1654             :             {
    1655         195 :                 weight = 6554; /* 0.2 in Q15 */
    1656         195 :                 move16();
    1657             :             }
    1658             : 
    1659       11535 :             weight = mac_r( L_mult( 13107 /* 0.4 in Q15 */, weight ), 19661 /* 0.6 in Q15 */, ( *prev_weight ) );
    1660             : 
    1661     3702735 :             FOR( n_freq = L; n_freq < swb_bwe_subband[SWB_FENV] + st_offset; n_freq++ )
    1662             :             {
    1663     3691200 :                 signum[n_freq] = 1;
    1664     3691200 :                 move16();
    1665     3691200 :                 IF( SWB_signal[n_freq] < 0 )
    1666             :                 {
    1667     1756309 :                     signum[n_freq] = -1;
    1668     1756309 :                     move16();
    1669     1756309 :                     SWB_signal[n_freq] = negate( SWB_signal[n_freq] );
    1670     1756309 :                     move16();
    1671             :                 }
    1672     3691200 :                 L_tmp = Mult_32_16( envelope[n_freq], inv_L_swb_norm ); /* Q_syn */
    1673     3691200 :                 L_tmp1 = L_deposit_l( SWB_signal[n_freq] );             /* Q_syn */
    1674     3691200 :                 L_tmp = L_sub( L_tmp1, L_tmp );                         /* Q_syn */
    1675     3691200 :                 IF( L_tmp > 0 )
    1676             :                 {
    1677     1651513 :                     tmp = shr( weight, 1 );                                                 /* Q14 */
    1678     1651513 :                     tmp = sub( 19661, tmp );                                                /* Q14 */
    1679     1651513 :                     SWB_signal[n_freq] = extract_l( L_shl( Mult_32_16( L_tmp, tmp ), 1 ) ); /* Q_syn */
    1680             :                 }
    1681             :                 ELSE
    1682             :                 {
    1683     2039687 :                     SWB_signal[n_freq] = extract_l( L_tmp ); /* Q_syn */
    1684             :                 }
    1685     3691200 :                 move16();
    1686     3691200 :                 IF( NE_16( signum[n_freq], 1 ) )
    1687             :                 {
    1688     1756309 :                     SWB_signal[n_freq] = negate( SWB_signal[n_freq] );
    1689     1756309 :                     move16();
    1690             :                 }
    1691             :             }
    1692             : 
    1693       11535 :             tmp_exp = sub( 15, Q_syn );
    1694     3702735 :             FOR( n_freq = L; n_freq < swb_bwe_subband[SWB_FENV] + st_offset; n_freq++ ){
    1695     3691200 :                 IF( envelope[n_freq] != 0 ){
    1696     3685509 :                     exp = norm_l( envelope[n_freq] );
    1697     3685509 :             tmp = extract_h( L_shl( envelope[n_freq], exp ) );
    1698     3685509 :             exp = sub( sub( 30, exp ), Q_syn );
    1699     3685509 :             tmp = div_s( 16384 /* 0.5 in Q15*/, tmp );                              /* Q(15+exp) */
    1700     3685509 :             L_tmp = L_shr( L_mult0( SWB_signal[n_freq], tmp ), add( exp, Q_syn ) ); /* Q15 */
    1701     3685509 :             SWB_signal[n_freq] = extract_l( L_tmp );
    1702     3685509 :             move16(); /* Q15 */
    1703             :         }
    1704             :         ELSE
    1705             :         {
    1706        5691 :             SWB_signal[n_freq] = shl( SWB_signal[n_freq], tmp_exp );
    1707        5691 :             move16(); /* Q15 */
    1708             :         }
    1709             :     }
    1710       11535 :     *prev_weight = weight;
    1711       11535 :     move16();
    1712             : }
    1713             : ELSE
    1714             : {
    1715         782 :     exp = norm_s( L_swb_norm );
    1716         782 :     tmp = shl( div_s( shl( 1, sub( 14, exp ) ), L_swb_norm ), sub( exp, 14 ) ); /* Q15 */
    1717         782 :     tmp = add( shl( tmp, 1 ), tmp );
    1718         782 :     *prev_weight = s_max( s_min( tmp, 16384 /* 0.5 in Q15 */ ), 6554 /* 0.2 in Q15 */ ); /* Q15 */
    1719         782 :     move16();
    1720             : }
    1721             : 
    1722       12317 : IF( EQ_16( mode, HARMONIC ) )
    1723             : {
    1724         195 :     pit1 = &SWB_signal[swb_bwe_subband[0] + st_offset];
    1725         195 :     move16();
    1726        3900 :     FOR( n_band = 0; n_band < 19; n_band++ )
    1727             :     {
    1728        3705 :         L_mean = L_deposit_l( 0 );
    1729       62985 :         FOR( n_freq = 0; n_freq < 16; n_freq++ )
    1730             :         {
    1731       59280 :             L_mean = L_add( L_mean, abs_s( *pit1 ) ); /*Q15 */
    1732       59280 :             pit1++;
    1733             :         }
    1734        3705 :         mean = extract_l( Mult_32_16( L_mean, 2048 /* 1/16 in Q15 */ ) ); /*Q15  */
    1735        3705 :         pit1 -= 16;
    1736       62985 :         FOR( n_freq = 0; n_freq < 16; n_freq++ )
    1737             :         {
    1738       59280 :             IF( LT_16( abs_s( *pit1 ), mean ) )
    1739             :             {
    1740       34723 :                 *pit1 = mult_r( *pit1, 6554 /* 0.2 in Q15 */ ); /*Q15*/
    1741       34723 :                 move16();
    1742             :             }
    1743       59280 :             pit1++;
    1744             :         }
    1745             :     }
    1746             : }
    1747             : 
    1748       12317 : L = 1;
    1749       12317 : move16();
    1750       12317 : if ( EQ_16( mode, HARMONIC ) )
    1751             : {
    1752         195 :     L = 2;
    1753         195 :     move16();
    1754             : }
    1755             : 
    1756      183390 : FOR( n_band = 0; n_band < SWB_FENV; n_band += L )
    1757             : {
    1758      171073 :     energy = L_deposit_l( 0 );
    1759      171073 :     tmp = add( swb_bwe_subband[n_band + L], st_offset );
    1760     4112513 :     FOR( n_freq = add( swb_bwe_subband[n_band], st_offset ); n_freq < tmp; n_freq++ )
    1761             :     {
    1762     3941440 :         L_tmp = L_mult( SWB_signal[n_freq], SWB_signal[n_freq] ); /*Q31 */
    1763     3941440 :         energy = L_add( energy, L_shr( L_tmp, 6 ) );              /*Q25 */
    1764             :     }
    1765             : 
    1766      171073 :     IF( energy == 0 )
    1767             :     {
    1768        4223 :         tmp_ener = sqrt_swb_bwe_subband_fx_L1[n_band]; /*Q12 */
    1769        4223 :         move16();
    1770        4223 :         if ( NE_16( L, 1 ) )
    1771             :         {
    1772           3 :             tmp_ener = sqrt_swb_bwe_subband_fx_L2[shr( n_band, 1 )]; /*Q12 */
    1773           3 :             move16();
    1774             :         }
    1775             : 
    1776        4223 :         tmp = add( swb_bwe_subband[n_band + L], st_offset );
    1777        4223 :         tmp_exp = sub( Q_syn, 12 );
    1778      105639 :         FOR( n_freq = add( swb_bwe_subband[n_band], st_offset ); n_freq < tmp; n_freq++ )
    1779             :         {
    1780      101416 :             SWB_signal_32[n_freq] = L_shl( L_mult( tmp_ener, SWB_signal[n_freq] ), tmp_exp ); /*Qsyn */
    1781      101416 :             move32();
    1782             :         }
    1783             :     }
    1784             :     ELSE
    1785             :     {
    1786      166850 :         tmp = sub( swb_bwe_subband[n_band + L], swb_bwe_subband[n_band] );
    1787      166850 :         exp1 = norm_s( tmp );
    1788      166850 :         tmp = div_s( shl( 1, sub( 14, exp1 ) ), tmp ); /*Q(29-exp1) */
    1789      166850 :         energy = Mult_32_16( energy, tmp );            /*Q(29-exp1+25-15)->Q(-exp1+39) */
    1790             : 
    1791      166850 :         exp = norm_l( energy );
    1792      166850 :         L_tmp = L_shl( energy, exp );
    1793             :         /*exp = 31-exp-(-exp1+39);move16(); */
    1794      166850 :         exp = sub( sub( exp1, exp ), 8 );
    1795      166850 :         Ltmp_ener = Isqrt_lc( L_tmp, &exp ); /*Q(31-exp) */
    1796             : 
    1797      166850 :         tmp = add( swb_bwe_subband[n_band + L], st_offset );
    1798      166850 :         tmp_exp = add( Q_syn, sub( exp, 15 ) );
    1799     4006874 :         FOR( n_freq = add( swb_bwe_subband[n_band], st_offset ); n_freq < tmp; n_freq++ )
    1800             :         {
    1801     3840024 :             SWB_signal_32[n_freq] = L_shl( Mult_32_16( Ltmp_ener, SWB_signal[n_freq] ), tmp_exp );
    1802     3840024 :             move32(); /*Qsyn+16 */
    1803             :         }
    1804             :     }
    1805             : }
    1806             : 
    1807       12317 : IF( GT_16( *prev_Energy, add_sat( Energy_16, shr( Energy_16, 2 ) ) ) && Energy_16 > 0 )
    1808             : {
    1809        1204 :     weight = shr( div_s( Energy_16, *prev_Energy ), 1 ); /*Q15 */
    1810             : }
    1811             : ELSE
    1812             : {
    1813       11113 :     weight = 16384;
    1814       11113 :     move16(); /*Q15 */
    1815             : }
    1816       12317 : L_tmp = L_mult( weight, prev_SWB_fenv[0] );                /*Q17 */
    1817       12317 : L_tmp = L_mac( L_tmp, sub( 32767, weight ), SWB_fenv[0] ); /*Q17 */
    1818       12317 : wfenv = round_fx( L_tmp );                                 /*Q1 */
    1819             : 
    1820       12317 : tmp = norm_s( wfenv );
    1821       12317 : IF( GT_16( tmp, 4 ) )
    1822             : {
    1823       10066 :     tmp = 12;
    1824       10066 :     move16();
    1825       10066 :     factor = fenvL_16;
    1826       10066 :     move16();                                                                      /*Q3 */
    1827       10066 :     factor1 = mult_r( sub( shl( wfenv, 2 ), fenvL_16 ), 4096 /* 0.125 in Q15 */ ); /*Q3  */
    1828             : }
    1829             : ELSE
    1830             : {
    1831        2251 :     tmp = 14;
    1832        2251 :     move16();
    1833        2251 :     factor = shr( fenvL_16, 2 );                                       /*Q1 */
    1834        2251 :     factor1 = mult_r( sub( wfenv, factor ), 4096 /* 0.125 in Q15 */ ); /*Q1 */
    1835             : }
    1836             : 
    1837       12317 : tmp2 = add( add( swb_bwe_subband[0], 8 ), st_offset );
    1838      110853 : FOR( n_freq = add( swb_bwe_subband[0], st_offset ); n_freq < tmp2; n_freq++ )
    1839             : {
    1840       98536 :     L_tmp1 = Mult_32_16( SWB_signal_32[n_freq], factor );
    1841       98536 :     SWB_signal_32[n_freq] = L_shl_sat( L_tmp1, tmp - 1 );
    1842       98536 :     move32();                        /*15+Qsyn */
    1843       98536 :     factor = add( factor, factor1 ); /*Q3 */
    1844             : }
    1845             : 
    1846      160121 : FOR( n_band = 0; n_band < 12; n_band++ )
    1847             : {
    1848      147804 :     L_tmp = L_mult( weight, prev_SWB_fenv[n_band + 1] );                /*Q17 */
    1849      147804 :     L_tmp = L_mac( L_tmp, sub( 32767, weight ), SWB_fenv[n_band + 1] ); /*Q17 */
    1850      147804 :     wfenv = round_fx( L_tmp );                                          /*Q1 */
    1851      147804 :     factor = SWB_fenv[n_band];
    1852      147804 :     move16();                                                                     /*Q1 */
    1853      147804 :     factor1 = mult_r( sub( wfenv, SWB_fenv[n_band] ), smooth_factor_fx[n_band] ); /*Q1 */
    1854      147804 :     tmp = norm_s( factor );
    1855      147804 :     IF( GT_16( tmp, 4 ) )
    1856             :     {
    1857      119371 :         tmp = 12;
    1858      119371 :         move16();
    1859      119371 :         factor = shl( factor, 2 );
    1860      119371 :         factor1 = shl( factor1, 2 );
    1861             :     }
    1862             :     ELSE
    1863             :     {
    1864       28433 :         tmp = 14;
    1865       28433 :         move16();
    1866             :     }
    1867             : 
    1868      147804 :     tmp2 = add( swb_bwe_sm_subband[n_band + 1], st_offset );
    1869     3399492 :     FOR( ; n_freq < tmp2; n_freq++ )
    1870             :     {
    1871     3251688 :         L_tmp1 = Mult_32_16( SWB_signal_32[n_freq], factor );
    1872     3251688 :         SWB_signal_32[n_freq] = L_shl_sat( L_tmp1, tmp - 1 );
    1873     3251688 :         move32();                            /*15+Qsyn */
    1874     3251688 :         factor = add_sat( factor, factor1 ); /*Q1 */
    1875             :     }
    1876             : }
    1877       12317 : L_tmp = L_mult( weight, prev_SWB_fenv[13] );                /*Q17 */
    1878       12317 : L_tmp = L_mac( L_tmp, sub( 32767, weight ), SWB_fenv[13] ); /*Q17 */
    1879       12317 : wfenv = round_fx( L_tmp );                                  /*Q1 */
    1880       12317 : factor = SWB_fenv[12];
    1881       12317 : move16();                                                             /*Q1 */
    1882       12317 : factor1 = mult_r( sub( wfenv, SWB_fenv[12] ), smooth_factor_fx[12] ); /*Q1 */
    1883       12317 : tmp2 = add( swb_bwe_sm_subband[13], st_offset );
    1884      406461 : FOR( ; n_freq < tmp2; n_freq++ )
    1885             : {
    1886      394144 :     L_tmp1 = Mult_32_16( SWB_signal_32[n_freq], factor );
    1887      394144 :     SWB_signal_32[n_freq] = L_shl_sat( L_tmp1, 13 );
    1888      394144 :     move32();                        /*15+Qsyn */
    1889      394144 :     factor = add( factor, factor1 ); /*Q1 */
    1890             : }
    1891             : 
    1892       24634 : FOR( n_band = 13; n_band < SWB_FENV; n_band++ )
    1893             : {
    1894       12317 :     L_tmp = L_mult( weight, prev_SWB_fenv[n_band] );                /*Q17 */
    1895       12317 :     L_tmp = L_mac( L_tmp, sub( 32767, weight ), SWB_fenv[n_band] ); /*Q17 */
    1896       12317 :     wfenv = round_fx( L_tmp );                                      /*Q1 */
    1897       12317 :     tmp2 = add( swb_bwe_subband[n_band + 1], st_offset );
    1898      209389 :     FOR( ; n_freq < tmp2; n_freq++ )
    1899             :     {
    1900      197072 :         L_tmp1 = Mult_32_16( SWB_signal_32[n_freq], factor );
    1901      197072 :         SWB_signal_32[n_freq] = L_shl_sat( L_tmp1, 13 );
    1902      197072 :         move32(); /*15+Qsyn */
    1903             :     }
    1904             : }
    1905      184755 : FOR( n_band = 0; n_band < SWB_FENV; n_band++ )
    1906             : {
    1907      172438 :     prev_SWB_fenv[n_band] = SWB_fenv[n_band];
    1908      172438 :     move16(); /*Q1 */
    1909             : }
    1910             : }
    1911       12392 : pit1_32 = &SWB_signal_32[240 + st_offset]; /*15+Qsyn */
    1912       61960 : FOR( n_freq = 0; n_freq < 4; n_freq++ )
    1913             : {
    1914       49568 :     L_tmp1 = Mult_32_16( *pit1_32, 16384 /* 0.5 in Q15 */ ); /*15+Qsyn */
    1915       49568 :     *( pit1_32++ ) = L_tmp1;
    1916       49568 :     move32();
    1917             : }
    1918       12392 : *prev_Energy = Energy_16;
    1919       12392 : move16();
    1920             : 
    1921       12392 : return;
    1922             : }
    1923             : 
    1924             : /*==========================================================================*/
    1925             : /* FUNCTION      : void time_envelop_shaping_fx()                           */
    1926             : /*--------------------------------------------------------------------------*/
    1927             : /* PURPOSE       :   Time shaping of SHB signal                             */
    1928             : /*--------------------------------------------------------------------------*/
    1929             : /* INPUT ARGUMENTS                                                          */
    1930             : /* _(Word16) L          :length                                             */
    1931             : /* _(Word16) Q_syn      :Q format                                           */
    1932             : /*--------------------------------------------------------------------------*/
    1933             : /* OUTPUT ARGUMENTS :                                                       */
    1934             : /*   _None                                                                  */
    1935             : /*--------------------------------------------------------------------------*/
    1936             : /* INPUT/OUTPUT ARGUMENTS :                                                 */
    1937             : /*   _(Word16[])werr      : SHB synthesis                                   */
    1938             : /*   _(Word16[])SWB_tenv    : frequency envelope                            */
    1939             : /*--------------------------------------------------------------------------*/
    1940             : /* RETURN ARGUMENTS :                                                       */
    1941             : /*   _ None                                                                 */
    1942             : /*--------------------------------------------------------------------------*/
    1943           0 : void time_envelop_shaping_fx(
    1944             :     Word16 werr[],     /* i/o: SHB synthesis           Q_synth*/
    1945             :     Word32 SWB_tenv[], /* i/o: frequency envelope          Q15*/
    1946             :     const Word16 L,    /* i  : frame length                   */
    1947             :     Word16 *Q_synth )
    1948             : {
    1949             :     Word16 *pit;
    1950             :     Word32 Energy;
    1951             :     Word16 i, j;
    1952             :     Word16 tmp_ener, Energy_16;
    1953             :     Word16 exp_L, exp, frac, tmp, inv_L;
    1954             :     Word32 L_tmp;
    1955             : 
    1956           0 :     pit = werr;
    1957           0 :     exp_L = norm_s( L );
    1958           0 :     inv_L = div_s( shl( 1, sub( 14, exp_L ) ), L ); /*Q(29-exp_L) */
    1959           0 :     FOR( i = 0; i < SWB_TENV; i++ )
    1960             :     {
    1961           0 :         Energy = L_deposit_l( 0 );
    1962           0 :         FOR( j = 0; j < L / 4; j++ )
    1963             :         {
    1964           0 :             Energy = L_mac0_sat( Energy, *pit, *pit ); /*(2*Q_synth) */
    1965           0 :             pit++;
    1966             :         }
    1967           0 :         Energy = Mult_32_16( Energy, inv_L ); /*Q(29-exp_L-15) -> Q(-exp_L+14+2*Q_synth) */
    1968           0 :         Energy_16 = 0;
    1969           0 :         move16();
    1970             :         /*exp = 31-(-exp_L+14 +(2*(*Q_synth))); */
    1971           0 :         exp = sub( 17, sub( shl( ( *Q_synth ), 1 ), exp_L ) );
    1972             : 
    1973           0 :         IF( Energy != 0 )
    1974             :         {
    1975           0 :             exp = norm_l( Energy );
    1976           0 :             frac = extract_h( L_shl( Energy, exp ) );
    1977             :             /*exp = sub(exp, 30-(-exp_L+14-2+(2*(*Q_synth))));  */
    1978           0 :             exp = sub( exp, sub( 30, add( sub( shl( ( *Q_synth ), 1 ), exp_L ), 14 - 2 ) ) );
    1979             : 
    1980           0 :             tmp = div_s( 16384, frac );
    1981           0 :             L_tmp = L_deposit_h( tmp );
    1982           0 :             Energy = Isqrt_lc( L_tmp, &exp );                        /*Q(31-exp) */
    1983           0 :             Energy_16 = round_fx( L_shl( Energy, sub( exp, 15 ) ) ); /*Q0 */
    1984             :         }
    1985             : 
    1986           0 :         test();
    1987           0 :         IF( LT_32( SWB_tenv[i], 65536 /* 2 in Q15 */ ) && LT_32( Energy, L_shl_sat( SWB_tenv[i], sub( 16, exp ) ) ) )
    1988             :         {
    1989           0 :             *Q_synth = add( *Q_synth, 3 );
    1990           0 :             move16();
    1991             :         }
    1992             :         ELSE
    1993             :         {
    1994           0 :             pit -= shr( L, 2 );
    1995           0 :             move16();
    1996           0 :             tmp_ener = 0;
    1997           0 :             move16();
    1998           0 :             exp = 0;
    1999           0 :             move16();
    2000             : 
    2001           0 :             IF( Energy_16 != 0 )
    2002             :             {
    2003           0 :                 exp = norm_s( Energy_16 );
    2004           0 :                 tmp_ener = div_s( shl( 1, sub( 14, exp ) ), Energy_16 ); /*Q(29-exp) */
    2005             :             }
    2006             : 
    2007           0 :             L_tmp = Mult_32_16( SWB_tenv[i], tmp_ener ); /*Q(29-exp) */
    2008           0 :             tmp = round_fx( L_tmp );                     /*Q(13-exp) */
    2009             : 
    2010           0 :             FOR( j = 0; j < L / 4; j++ )
    2011             :             {
    2012           0 :                 *pit = round_fx_sat( L_shl_sat( L_mult( tmp, *pit ), sub( exp, 1 ) ) ); /*Q(13-exp+1)->Q(14-exp)->Qsyn-3 */
    2013           0 :                 move16();
    2014           0 :                 pit++;
    2015             :             }
    2016             :         }
    2017             :     }
    2018             : 
    2019           0 :     return;
    2020             : }
    2021             : 
    2022          75 : void time_envelop_shaping_ivas_fx(
    2023             :     Word16 werr[],     /* i/o: SHB synthesis           Q_synth*/
    2024             :     Word32 SWB_tenv[], /* i/o: frequency envelope          Q15*/
    2025             :     const Word16 L,    /* i  : frame length                   */
    2026             :     Word16 *Q_synth )
    2027             : {
    2028             :     Word16 *pit;
    2029             :     Word32 Energy;
    2030             :     Word16 i, j;
    2031             :     Word16 tmp_ener, Energy_16;
    2032             :     Word64 Energy_64;
    2033             :     Word16 exp_L, exp, frac, tmp, inv_L, w_tmp, Energy_Q;
    2034             :     Word32 L_tmp;
    2035             : 
    2036          75 :     pit = werr;
    2037          75 :     exp_L = norm_s( L );
    2038          75 :     inv_L = div_s( shl( 1, sub( 14, exp_L ) ), L ); /*Q(29-exp_L) */
    2039         375 :     FOR( i = 0; i < SWB_TENV; i++ )
    2040             :     {
    2041         300 :         Energy_64 = L_deposit_l( 0 );
    2042       65900 :         FOR( j = 0; j < L / 4; j++ )
    2043             :         {
    2044       65600 :             Energy_64 = W_mac0_16_16( Energy_64, *pit, *pit ); /*(2*Q_synth) */
    2045       65600 :             pit++;
    2046             :         }
    2047         300 :         w_tmp = W_norm( Energy_64 );
    2048         300 :         Energy_64 = W_shl( Energy_64, w_tmp );
    2049         300 :         Energy = W_extract_h( Energy_64 ); /*2*Q_synth + w_tmp -32*/
    2050         300 :         Energy_Q = sub( add( shl( ( *Q_synth ), 1 ), w_tmp ), 32 );
    2051         300 :         Energy = Mult_32_16( Energy, inv_L ); /*Q(29-exp_L-15) -> Q(-exp_L+14+2*Q_synth+w_tmp-32) */
    2052         300 :         Energy_16 = 0;
    2053         300 :         move16();
    2054             :         /*exp = 31-(-exp_L+14 +(2*(*Q_synth)+w_tmp-32)); */
    2055         300 :         exp = sub( 17, sub( Energy_Q, exp_L ) );
    2056             : 
    2057         300 :         IF( Energy != 0 )
    2058             :         {
    2059         300 :             exp = norm_l( Energy );
    2060         300 :             frac = extract_h( L_shl( Energy, exp ) );
    2061             :             /*exp = sub(exp, 30-(-exp_L+14-2+(2*(*Q_synth)+w_tmp-32)));  */
    2062         300 :             exp = sub( exp, sub( 30, add( sub( Energy_Q, exp_L ), 14 - 2 ) ) );
    2063             : 
    2064         300 :             tmp = div_s( 16384, frac );
    2065         300 :             L_tmp = L_deposit_h( tmp );
    2066         300 :             Energy = Isqrt_lc( L_tmp, &exp );                        /*Q(31-exp) */
    2067         300 :             Energy_16 = round_fx( L_shl( Energy, sub( exp, 15 ) ) ); /*Q0 */
    2068             :         }
    2069             : 
    2070         300 :         test();
    2071         300 :         IF( LT_32( SWB_tenv[i], 65536 /* 2 in Q15 */ ) && LT_32( Energy, L_shl_sat( SWB_tenv[i], sub( 16, exp ) ) ) )
    2072             :         {
    2073           7 :             *Q_synth = add( *Q_synth, 3 );
    2074           7 :             move16();
    2075             :         }
    2076             :         ELSE
    2077             :         {
    2078         293 :             pit -= shr( L, 2 );
    2079         293 :             tmp_ener = 0;
    2080         293 :             move16();
    2081         293 :             exp = 0;
    2082         293 :             move16();
    2083             : 
    2084         293 :             IF( Energy_16 != 0 )
    2085             :             {
    2086         292 :                 exp = norm_s( Energy_16 );
    2087         292 :                 tmp_ener = div_s( shl( 1, sub( 14, exp ) ), Energy_16 ); /*Q(29-exp) */
    2088             :             }
    2089             : 
    2090         293 :             L_tmp = Mult_32_16( SWB_tenv[i], tmp_ener ); /*Q(29-exp) */
    2091         293 :             tmp = round_fx( L_tmp );                     /*Q(13-exp) */
    2092             : 
    2093       64213 :             FOR( j = 0; j < L / 4; j++ )
    2094             :             {
    2095       63920 :                 *pit = round_fx_sat( L_shl_sat( L_mult( tmp, *pit ), sub( exp, 1 ) ) ); /*Q(13-exp+1)->Q(14-exp)->Qsyn-3 */
    2096       63920 :                 move16();
    2097       63920 :                 pit++;
    2098             :             }
    2099             :         }
    2100             :     }
    2101             : 
    2102          75 :     return;
    2103             : }
    2104             : 
    2105             : /*==========================================================================*/
    2106             : /* FUNCTION      : void time_reduce_pre_echo_fx()                           */
    2107             : /*--------------------------------------------------------------------------*/
    2108             : /* PURPOSE       :   Windowing and time-domain aliasing                     */
    2109             : /*--------------------------------------------------------------------------*/
    2110             : /* INPUT ARGUMENTS                                                          */
    2111             : /* _(Word16*) synth         :ACELP core synthesis    Q_syn                  */
    2112             : /* _(Word16) L          :subframe length                                    */
    2113             : /* _(Word16) Q_syn        :Q format                                         */
    2114             : /*--------------------------------------------------------------------------*/
    2115             : /* OUTPUT ARGUMENTS :                                                       */
    2116             : /*   _(Word16*)error        : SHB BWE synthesis       Q_syn                 */
    2117             : /*   _(Word16)prev_td_energy    : last td energy          Q_syn             */
    2118             : /*--------------------------------------------------------------------------*/
    2119             : /* INPUT/OUTPUT ARGUMENTS :                                                 */
    2120             : /*   _None                                                                  */
    2121             : /*--------------------------------------------------------------------------*/
    2122             : /* RETURN ARGUMENTS :                                                       */
    2123             : /*   _ None                                                                 */
    2124             : /*--------------------------------------------------------------------------*/
    2125          97 : void time_reduce_pre_echo_fx(
    2126             :     const Word16 *synth,   /* i  : ACELP core synthesis    Q_syn*/
    2127             :     Word16 *error,         /* i/o: SHB BWE synthesis          Q0*/
    2128             :     Word16 prev_td_energy, /* o  : last td energy             Q0*/
    2129             :     const Word16 L,        /* i  : subframe length              */
    2130             :     Word16 Q_syn,
    2131             :     Word16 Q_synth )
    2132             : {
    2133          97 :     Word16 i, j, pos = 0, Len;
    2134             :     Word32 energy;
    2135             :     Word16 energy_16;
    2136             :     Word32 energyL[4];
    2137             :     Word16 tmp_ener;
    2138             :     Word16 *pit;
    2139             :     Word16 tmpi, tmp_exp;
    2140             :     Word16 exp_L, exp, frac, inv_L, exp_j, tmp;
    2141             :     Word32 L_tmp, L_tmp1, Ltmp_ener;
    2142             : 
    2143          97 :     exp_L = norm_s( L );
    2144          97 :     inv_L = div_s( shl( 1, sub( 14, exp_L ) ), L ); /*Q(29-exp_L) */
    2145         485 :     FOR( i = 0; i < 4; i++ )
    2146             :     {
    2147         388 :         Len = i_mult( L, i );
    2148             : 
    2149         388 :         L_tmp = L_deposit_l( 0 );
    2150       88388 :         FOR( j = 0; j < L; j++ )
    2151             :         {
    2152       88000 :             L_tmp = L_mac0_sat( L_tmp, synth[Len + j], synth[Len + j] ); /*2*Q_syn */
    2153             :         }
    2154         388 :         energyL[i] = Mult_32_16( L_shr( L_tmp, sub( 14, exp_L ) ), inv_L );
    2155         388 :         move32(); /*Q(2*Q_syn +29-exp_L-15 - (14-exp_L) ) -> Q(2*Q_syn) */
    2156             : 
    2157         388 :         IF( energyL[i] != 0 )
    2158             :         {
    2159         356 :             exp = norm_l( energyL[i] );
    2160         356 :             frac = extract_h( L_shl( energyL[i], exp ) );
    2161         356 :             exp = sub( exp, sub( 30, shl( Q_syn, 1 ) ) );
    2162             : 
    2163         356 :             tmp = div_s( 16384, frac );
    2164         356 :             L_tmp = L_deposit_h( tmp );
    2165         356 :             L_tmp = Isqrt_lc( L_tmp, &exp );
    2166         356 :             energyL[i] = L_shl( L_tmp, sub( exp, 16 ) );
    2167         356 :             move32(); /* Q15 */
    2168             :         }
    2169             :     }
    2170             : 
    2171         378 :     FOR( i = 0; i < 3; i++ )
    2172             :     {
    2173         289 :         L_tmp = Mult_32_16( energyL[i], 29491 /* 0.9 in Q15 */ ); /*Q14 */
    2174         289 :         IF( GT_32( L_shr( energyL[i + 1], 1 ), L_tmp ) && GT_32( energyL[i + 1], 1638400 /* 100 in Q14 */ ) )
    2175             :         {
    2176           8 :             pos = add( i, 1 );
    2177           8 :             move16();
    2178           8 :             BREAK;
    2179             :         }
    2180             :     }
    2181             : 
    2182          97 :     IF( pos > 0 )
    2183             :     {
    2184           8 :         IF( LT_16( pos, 3 ) )
    2185             :         {
    2186           1 :             pos = add( pos, 1 );
    2187             :         }
    2188           8 :         energy = L_deposit_l( 0 );
    2189           8 :         j = i_mult( L, pos );
    2190        5528 :         FOR( i = 0; i < j; i++ )
    2191             :         {
    2192        5520 :             energy = L_mac0_sat( energy, error[i], error[i] ); /*Q(2*Q_synth) */
    2193             :         }
    2194             : 
    2195           8 :         exp_j = norm_s( j );
    2196           8 :         tmp = div_s( shl( 1, sub( 14, exp_j ) ), j ); /*Q(29-exp_j) */
    2197           8 :         energy = Mult_32_16( energy, tmp );           /*Q(29-exp_j+1-16) -> Q(-exp_j+14 +2*Q_synth) */
    2198           8 :         energy_16 = 0;
    2199           8 :         move16();
    2200             : 
    2201           8 :         IF( energy != 0 )
    2202             :         {
    2203           8 :             exp = norm_l( energy );
    2204           8 :             frac = extract_h( L_shl( energy, exp ) );
    2205             :             /*exp = sub(exp, 30-(-exp_j+14 +2*Q_synth));  */
    2206           8 :             exp = sub( exp, sub( 14, sub( shl( Q_synth, 1 ), exp_j ) ) );
    2207           8 :             tmp = div_s( 16384, frac );
    2208           8 :             L_tmp = L_deposit_h( tmp );
    2209           8 :             energy = Isqrt_lc( L_tmp, &exp );                        /*Q(31-exp) */
    2210           8 :             energy_16 = round_fx( L_shl( energy, sub( exp, 15 ) ) ); /*Q0 */
    2211             :         }
    2212             : 
    2213           8 :         tmp = mult_r( energy_16, 6554 /* 0.2 in Q15 */ ); /*Q0 */
    2214           8 :         if ( LT_16( prev_td_energy, tmp ) )
    2215             :         {
    2216           8 :             prev_td_energy = tmp;
    2217           8 :             move16();
    2218             :         }
    2219             : 
    2220           8 :         tmp_ener = 0;
    2221           8 :         move16();
    2222           8 :         exp = 0;
    2223           8 :         move16();
    2224           8 :         IF( energy_16 != 0 )
    2225             :         {
    2226           8 :             exp = norm_s( energy_16 );
    2227           8 :             tmp_ener = div_s( shl( 1, sub( 14, exp ) ), energy_16 ); /*Q(29-exp) */
    2228             :         }
    2229           8 :         L_tmp = L_mult( prev_td_energy, tmp_ener ); /*Q(30-exp) */
    2230           8 :         tmp_exp = add( 1, exp );
    2231        5528 :         FOR( i = 0; i < j; i++ )
    2232             :         {
    2233        5520 :             error[i] = round_fx_sat( L_shl_sat( Mult_32_16( L_tmp, error[i] ), tmp_exp ) ); /*Q(30-exp+1-16)->Q(15-exp)->Q_synth */
    2234        5520 :             move16();
    2235             :         }
    2236             : 
    2237           8 :         energy = L_deposit_l( 0 );
    2238        1928 :         FOR( i = j; i < ( j + L ); i++ )
    2239             :         {
    2240        1920 :             energy = L_mac0_sat( energy, error[i], error[i] ); /*(2*Q_synth) */
    2241             :         }
    2242             : 
    2243           8 :         energy = Mult_32_16( energy, inv_L ); /*Q(29-exp_L+1-16) -> Q(-exp_L+14) */
    2244           8 :         energy_16 = 0;
    2245           8 :         move16();
    2246           8 :         IF( energy != 0 )
    2247             :         {
    2248           8 :             exp = norm_l( energy );
    2249           8 :             frac = extract_h( L_shl( energy, exp ) );
    2250             :             /*exp = sub(exp, 30-(-exp_L+14+2*Q_synth));  */
    2251           8 :             exp = sub( exp, sub( 14, sub( shl( Q_synth, 1 ), exp_L ) ) );
    2252             : 
    2253           8 :             tmp = div_s( 16384, frac );
    2254           8 :             L_tmp = L_deposit_h( tmp );
    2255           8 :             energy = Isqrt_lc( L_tmp, &exp );                        /*Q(31-exp) */
    2256           8 :             energy_16 = round_fx( L_shl( energy, sub( exp, 15 ) ) ); /*Q0 */
    2257             :         }
    2258             : 
    2259           8 :         tmp_ener = 0;
    2260           8 :         move16();
    2261           8 :         exp = 0;
    2262           8 :         move16();
    2263           8 :         IF( energy_16 != 0 )
    2264             :         {
    2265           8 :             exp = norm_s( energy_16 );
    2266           8 :             tmp_ener = div_s( shl( 1, sub( 14, exp ) ), energy_16 ); /*Q(29-exp) */
    2267             :         }
    2268           8 :         Ltmp_ener = L_mult( prev_td_energy, tmp_ener ); /*Q(30-exp) */
    2269           8 :         L_tmp1 = L_shl( 1, sub( 30, exp ) );
    2270             : 
    2271           8 :         pit = &error[j];
    2272           8 :         move16();
    2273        1928 :         FOR( i = 0; i < L; i++ )
    2274             :         {
    2275        1920 :             tmpi = round_fx( L_shl( L_mult( i, inv_L ), add( 1, exp_L ) ) );             /*Q15 */
    2276        1920 :             L_tmp = L_sub( L_tmp1, Ltmp_ener );                                          /*Q(30-exp) */
    2277        1920 :             L_tmp = Mult_32_16( L_tmp, tmpi );                                           /*Q(30-exp) */
    2278        1920 :             L_tmp = L_add( Ltmp_ener, L_tmp );                                           /*Q(30-exp) */
    2279        1920 :             tmp = round_fx_sat( L_shl_sat( Mult_32_16( L_tmp, *pit ), add( 1, exp ) ) ); /*Q0 */
    2280        1920 :             *pit++ = tmp;
    2281        1920 :             move16();
    2282             :         }
    2283             :     }
    2284             : 
    2285          97 :     return;
    2286             : }
    2287             : 
    2288             : /*-------------------------------------------------------------------*
    2289             :  * calc_normal_length_fx_32()
    2290             :  *
    2291             :  *-------------------------------------------------------------------*/
    2292        7633 : void calc_normal_length_fx_32(
    2293             :     const Word16 core,      /* i  : core                   : Q0  */
    2294             :     const Word32 *sp,       /* i  : input signal           : Q12 */
    2295             :     const Word16 mode,      /* i  : input mode             : Q0  */
    2296             :     const Word16 extl,      /* i  : extension layer        : Q0  */
    2297             :     Word16 *L_swb_norm,     /* o  : normalize length       : Q0  */
    2298             :     Word16 *prev_L_swb_norm /*i/o : last normalize length  : Q0  */
    2299             : )
    2300             : {
    2301             :     Word16 i, n_freq, n_band, THRES;
    2302             :     Word16 L_swb_norm_trans, L_swb_norm_norm, L_swb_norm_harm, L_swb_norm_cur;
    2303             :     Word16 N;
    2304             : 
    2305             :     const Word32 *pit;
    2306             :     Word32 peak, mean, mag;
    2307             :     Word32 L_tmp1, L_tmp2;
    2308             : 
    2309        7633 :     THRES = 4;
    2310        7633 :     move16();
    2311        7633 :     test();
    2312        7633 :     test();
    2313        7633 :     if ( EQ_16( core, HQ_CORE ) || EQ_16( extl, SWB_BWE ) || EQ_16( extl, FB_BWE ) )
    2314             :     {
    2315        7633 :         THRES = 8;
    2316        7633 :         move16();
    2317             :     }
    2318             : 
    2319        7633 :     N = 16;
    2320        7633 :     move16();
    2321        7633 :     test();
    2322        7633 :     test();
    2323        7633 :     if ( EQ_16( core, HQ_CORE ) && ( EQ_16( mode, HQ_HARMONIC ) || EQ_16( mode, HQ_HVQ ) ) )
    2324             :     {
    2325        2122 :         N = 13;
    2326        2122 :         move16();
    2327             :     }
    2328             : 
    2329        7633 :     n_band = 0;
    2330        7633 :     move16();
    2331        7633 :     pit = sp;
    2332             : 
    2333      123395 :     FOR( i = 0; i < N; i++ )
    2334             :     {
    2335      115762 :         peak = 0;
    2336      115762 :         move16();
    2337      115762 :         mean = 0;
    2338      115762 :         move16();
    2339             : 
    2340     1967954 :         FOR( n_freq = 0; n_freq < 16; n_freq++ )
    2341             :         {
    2342     1852192 :             mag = L_abs( *pit );
    2343     1852192 :             if ( GT_32( mag, peak ) )
    2344             :             {
    2345      384249 :                 peak = mag;
    2346      384249 :                 move16();
    2347             :             }
    2348     1852192 :             mean = L_add_sat( mean, mag );
    2349     1852192 :             pit++;
    2350             :         }
    2351             : 
    2352      115762 :         L_tmp1 = Mult_32_16( peak, shl( add( 15, THRES ), 10 ) );
    2353      115762 :         L_tmp2 = Mult_32_16( mean, shl( THRES, 10 ) );
    2354      115762 :         test();
    2355      115762 :         IF( GT_32( L_tmp1, L_tmp2 ) && GT_32( peak, 40960 /* 10 in Q12 */ ) )
    2356             :         {
    2357       27840 :             n_band = add( n_band, 1 );
    2358             :         }
    2359             :     }
    2360             : 
    2361        7633 :     IF( core == ACELP_CORE )
    2362             :     {
    2363           0 :         L_swb_norm_trans = add( 4, shr( n_band, 2 ) );
    2364           0 :         L_swb_norm_norm = add( 8, shr( n_band, 1 ) );
    2365           0 :         L_swb_norm_harm = s_max( add( 32, shl( n_band, 1 ) ), 24 );
    2366             : 
    2367           0 :         IF( EQ_16( mode, HARMONIC ) )
    2368             :         {
    2369           0 :             L_swb_norm_cur = L_swb_norm_harm;
    2370           0 :             move16();
    2371             :         }
    2372           0 :         ELSE IF( EQ_16( mode, NORMAL ) )
    2373             :         {
    2374           0 :             L_swb_norm_cur = L_swb_norm_norm;
    2375           0 :             move16();
    2376             :         }
    2377             :         ELSE
    2378             :         {
    2379           0 :             L_swb_norm_cur = L_swb_norm_trans;
    2380           0 :             move16();
    2381             :         }
    2382             : 
    2383           0 :         *L_swb_norm = shr( add( L_swb_norm_cur, *prev_L_swb_norm ), 1 );
    2384           0 :         move16();
    2385           0 :         *prev_L_swb_norm = L_swb_norm_cur;
    2386           0 :         move16();
    2387             :     }
    2388             :     ELSE
    2389             :     {
    2390        7633 :         test();
    2391        7633 :         IF( EQ_16( mode, HQ_HARMONIC ) || EQ_16( mode, HQ_HVQ ) )
    2392             :         {
    2393        2122 :             L_swb_norm_cur = add( 32, add( shl( n_band, 1 ), shr( n_band, 1 ) ) );
    2394             :         }
    2395             :         ELSE
    2396             :         {
    2397        5511 :             L_swb_norm_cur = add( 8, shr( n_band, 1 ) );
    2398             :         }
    2399             : 
    2400        7633 :         *L_swb_norm = extract_h( L_add( L_mac( L_mult( L_swb_norm_cur, 3276 /* 0.1 in Q15 */ ), *prev_L_swb_norm, 29491 /* 0.9 in Q15 */ ), 32768 /* 1 in Q15 */ ) );
    2401        7633 :         *prev_L_swb_norm = L_swb_norm_cur;
    2402        7633 :         move16();
    2403        7633 :         move16();
    2404             :     }
    2405             : 
    2406        7633 :     return;
    2407             : }
    2408             : 
    2409             : 
    2410             : /*-------------------------------------------------------------------*
    2411             :  * calc_norm_envelop_fx_32()
    2412             :  *
    2413             :  *-------------------------------------------------------------------*/
    2414        2861 : void calc_norm_envelop_fx_32(
    2415             :     const Word32 SWB_signal_fx[], /* i  : SWB spectrum           : Q12 */
    2416             :     Word32 *envelope_fx,          /* o  : normalized envelope    : Q16 */
    2417             :     const Word16 L_swb_norm,      /* i  : length of envelope     : Q0  */
    2418             :     const Word16 SWB_flength,     /* i  : Length of input/output : Q0  */
    2419             :     const Word16 st_offset        /* i  : offset                 : Q0  */
    2420             : )
    2421             : {
    2422             :     Word16 i, lookback, env_index, n_freq, n_lag_now, n_lag, tmp;
    2423             : 
    2424        2861 :     lookback = shr( L_swb_norm, 1 );
    2425        2861 :     move16();
    2426        2861 :     env_index = add( swb_bwe_subband[0], st_offset );
    2427        2861 :     move16();
    2428        2861 :     n_lag_now = L_swb_norm;
    2429        2861 :     move16();
    2430        2861 :     tmp = sub( add( SWB_flength, st_offset ), L_swb_norm );
    2431      903964 :     FOR( n_freq = sub( add( swb_bwe_trans_subband[0], st_offset ), lookback ); n_freq < tmp; n_freq++ )
    2432             :     {
    2433             :         /* Apply MA filter */
    2434      901103 :         envelope_fx[env_index] = 0;
    2435      901103 :         move16();
    2436     9603137 :         FOR( n_lag = 0; n_lag < n_lag_now; n_lag++ )
    2437             :         {
    2438     8702034 :             envelope_fx[env_index] = L_add_sat( envelope_fx[env_index], L_abs( SWB_signal_fx[n_freq + n_lag] ) );
    2439     8702034 :             move32();
    2440             :         }
    2441      901103 :         env_index = add( env_index, 1 );
    2442             :     }
    2443        2861 :     i = 0;
    2444        2861 :     move16();
    2445        2861 :     tmp = sub( add( SWB_flength, st_offset ), lookback );
    2446       17278 :     FOR( n_freq = sub( add( SWB_flength, st_offset ), L_swb_norm ); n_freq < tmp; n_freq++ )
    2447             :     {
    2448             :         Word32 L_tmp;
    2449             : 
    2450       14417 :         n_lag_now = sub( L_swb_norm, i );
    2451       14417 :         move16();
    2452             :         /* Apply MA filter */
    2453       14417 :         L_tmp = L_deposit_l( 0 );
    2454      127086 :         FOR( n_lag = 0; n_lag < n_lag_now; n_lag++ )
    2455             :         {
    2456      112669 :             L_tmp = L_add( L_tmp, L_abs( SWB_signal_fx[n_freq + n_lag] ) );
    2457             :         }
    2458       14417 :         envelope_fx[env_index] = L_tmp;
    2459       14417 :         move32();
    2460       14417 :         env_index = add( env_index, 1 );
    2461       14417 :         i++;
    2462             :     }
    2463             : 
    2464        2861 :     return;
    2465             : }
    2466             : 
    2467             : /*-------------------------------------------------------------------*
    2468             :  * hq_generic_decoding_fx()
    2469             :  *
    2470             :  *-------------------------------------------------------------------*/
    2471          46 : void hq_generic_decoding_fx(
    2472             :     const Word16 HQ_mode,             /* i  : HQ mode                                      : Q0  */
    2473             :     Word32 *coeff_out1_fx,            /* i/o: BWE input & temporary buffer                 : Q12 */
    2474             :     const Word16 *hq_generic_fenv_fx, /* i  : SWB frequency envelopes                      : Q1  */
    2475             :     Word32 *coeff_out_fx,             /* o  : SWB signal in MDCT domain                    : Q12 */
    2476             :     const Word16 hq_generic_offset,   /* i  : frequency offset for representing hq generci : Q0  */
    2477             :     Word16 *prev_L_swb_norm,          /* i/o: last normalize length                        : Q0  */
    2478             :     const Word16 hq_generic_exc_clas, /* i  : bwe excitation class                         " Q0  */
    2479             :     const Word16 *R )
    2480             : {
    2481             :     Word16 i, n_freq, n_band, L_swb_norm;
    2482             :     Word16 k;
    2483             :     Word16 nenv;
    2484             :     Word16 tenv;
    2485             : 
    2486             :     Word16 exp, exp1, exp2, frac, tmp, tmp2, cs;
    2487             :     Word32 L_tmp, L_tmp1, L_tmp2, max_coeff_fx;
    2488             :     Word16 fenvL_fx, wfenv_fx, factor_fx;
    2489             :     Word32 *pit1_fx;
    2490             :     Word16 tmp1_fx, tmp2_fx, tmp3_fx, tmp4_fx;
    2491             :     Word32 energy_fx;
    2492             : 
    2493             :     Word32 envelope_fx[L_FRAME16k];
    2494             :     Word32 mean_vector_fx[20];
    2495             :     Word16 rn_weight0_fx;
    2496             :     Word16 s;
    2497             :     Word16 blen, nband_lf, sfidx, efidx;
    2498             :     Word16 bwe_seed;
    2499             :     Word16 signum[L_FRAME16k];
    2500             : 
    2501          46 :     nenv = sub( SWB_FENV, 2 );
    2502          46 :     if ( LE_16( hq_generic_offset, HQ_GENERIC_FOFFSET_24K4 ) )
    2503             :     {
    2504          46 :         nenv = SWB_FENV;
    2505          46 :         move16();
    2506             :     }
    2507             : 
    2508          46 :     tenv = nenv;
    2509          46 :     move16();
    2510          46 :     if ( EQ_16( HQ_mode, HQ_GEN_FB ) )
    2511             :     {
    2512           0 :         tenv = add( nenv, DIM_FB );
    2513             :     }
    2514             : 
    2515          46 :     max_coeff_fx = 0;
    2516          46 :     move16();
    2517          46 :     tmp = add( swb_bwe_subband[0], hq_generic_offset );
    2518         782 :     FOR( n_freq = add( HQ_GENERIC_ST_FREQ, hq_generic_offset ); n_freq < tmp; n_freq++ )
    2519             :     {
    2520         736 :         max_coeff_fx = L_max( max_coeff_fx, L_abs( coeff_out1_fx[n_freq] ) );
    2521             :     }
    2522          46 :     cs = norm_l( max_coeff_fx );
    2523             : 
    2524          46 :     L_tmp = 0;
    2525          46 :     move16();
    2526          46 :     tmp2 = add( swb_bwe_subband[0], hq_generic_offset );
    2527         782 :     FOR( n_freq = add( HQ_GENERIC_ST_FREQ, hq_generic_offset ); n_freq < tmp2; n_freq++ )
    2528             :     {
    2529         736 :         tmp = extract_h( L_shl( coeff_out1_fx[n_freq], cs ) ); /*12 + cs - 16 */
    2530         736 :         L_tmp1 = L_mult0( tmp, tmp );                          /*2*(cs-2) */
    2531         736 :         L_tmp = L_add( L_tmp, L_shr( L_tmp1, 5 ) );            /*2*(cs-2) - 5 */
    2532             :     }
    2533          46 :     cs = sub( shl( cs, 1 ), 9 );
    2534          46 :     fenvL_fx = 0;
    2535          46 :     move16();
    2536          46 :     IF( L_tmp != 0 )
    2537             :     {
    2538          46 :         exp = norm_l( L_tmp );
    2539          46 :         frac = round_fx_sat( L_shl( L_tmp, exp ) ); /*cs+exp-16 */
    2540          46 :         tmp = div_s( 16384, frac );                 /*15 + 14 - (cs+exp-16) */
    2541          46 :         exp = sub( add( cs, exp ), 30 );
    2542          46 :         L_tmp = Isqrt_lc( L_deposit_h( tmp ), &exp );                  /*Q31 - exp */
    2543          46 :         fenvL_fx = round_fx_sat( L_shl_sat( L_tmp, sub( exp, 14 ) ) ); /*Q1 */
    2544             :     }
    2545             : 
    2546          46 :     calc_normal_length_fx_32( HQ_CORE, coeff_out1_fx, HQ_GEN_SWB, -1, &L_swb_norm, prev_L_swb_norm );
    2547             : 
    2548          46 :     calc_norm_envelop_lf_fx( coeff_out1_fx, envelope_fx, &L_swb_norm, HQ_mode, hq_generic_offset, &sfidx, &efidx );
    2549             : 
    2550          46 :     blen = 16;
    2551          46 :     move16();
    2552             : 
    2553          46 :     IF( EQ_16( hq_generic_exc_clas, HQ_GENERIC_EXC0 ) )
    2554             :     {
    2555          25 :         rn_weight0_fx = 819;
    2556          25 :         move16(); /* 0.8 Q10 */
    2557             :     }
    2558          21 :     ELSE IF( EQ_16( hq_generic_exc_clas, HQ_GENERIC_EXC1 ) )
    2559             :     {
    2560           0 :         rn_weight0_fx = 51;
    2561           0 :         move16(); /* 0.05 Q10*/
    2562             :     }
    2563             :     ELSE
    2564             :     {
    2565          21 :         rn_weight0_fx = 205;
    2566          21 :         move16(); /* 0.02 Q10 */
    2567             :     }
    2568             : 
    2569          46 :     tmp = sub( efidx, sfidx );
    2570          46 :     IF( tmp == 0 )
    2571             :     {
    2572           0 :         nband_lf = 0;
    2573           0 :         move16();
    2574             :     }
    2575             :     ELSE
    2576             :     {
    2577          46 :         exp = norm_s( tmp );
    2578          46 :         nband_lf = shl( tmp, sub( exp, 1 ) );
    2579          46 :         exp1 = norm_s( blen );
    2580          46 :         tmp = shl( blen, exp1 );
    2581          46 :         nband_lf = shr( div_s( nband_lf, tmp ), add( sub( 14, exp1 ), exp ) ); /* 15 + exp-1 - exp1, Q0*/
    2582             :     }
    2583             : 
    2584        6670 :     FOR( n_freq = sfidx; n_freq < efidx; n_freq++ )
    2585             :     {
    2586        6624 :         IF( coeff_out1_fx[n_freq] < 0 )
    2587             :         {
    2588        3309 :             signum[n_freq] = -1;
    2589        3309 :             move16();
    2590        3309 :             coeff_out1_fx[n_freq] = L_negate( coeff_out1_fx[n_freq] );
    2591        3309 :             move32();
    2592             :         }
    2593             :         ELSE
    2594             :         {
    2595        3315 :             signum[n_freq] = 1;
    2596        3315 :             move16();
    2597             :         }
    2598             :     }
    2599             : 
    2600             :     /* applying whitening */
    2601        6670 :     FOR( n_freq = sfidx; n_freq < efidx; n_freq++ )
    2602             :     {
    2603        6624 :         exp = norm_l( coeff_out1_fx[n_freq] ) - 1;
    2604        6624 :         exp1 = norm_l( envelope_fx[n_freq] );
    2605             : 
    2606        6624 :         L_tmp = L_shl( coeff_out1_fx[n_freq], exp );
    2607        6624 :         L_tmp1 = L_shl( envelope_fx[n_freq], exp1 );
    2608             : 
    2609        6624 :         logic16();
    2610        6624 :         coeff_out1_fx[n_freq] = Div_32( L_tmp, extract_h( L_tmp1 ), extract_l( L_shr( L_tmp1, 1 ) ) & 0x00007fff );
    2611        6624 :         move32(); /*31 + exp1 - exp2*/
    2612             : 
    2613        6624 :         exp = add( 31, sub( exp, exp1 ) );
    2614        6624 :         coeff_out1_fx[n_freq] = L_shl( coeff_out1_fx[n_freq], sub( 20, exp ) );
    2615        6624 :         move32(); /*Q12->Q20*/
    2616             :     }
    2617             : 
    2618             :     /* mean vector generation for controlling dynamic range */
    2619         460 :     FOR( k = 0; k < nband_lf; ++k )
    2620             :     {
    2621         414 :         energy_fx = 1;
    2622         414 :         move16();
    2623             : 
    2624         414 :         tmp = add( i_mult2( add( k, 1 ), blen ), sfidx );
    2625        7038 :         FOR( i = add( i_mult2( k, blen ), sfidx ); i < tmp; ++i )
    2626             :         {
    2627        6624 :             energy_fx = L_add( energy_fx, coeff_out1_fx[i] );
    2628             :         }
    2629         414 :         exp = sub( norm_l( energy_fx ), 1 );
    2630         414 :         L_tmp = L_shl( energy_fx, exp );
    2631         414 :         exp1 = norm_l( L_and( blen, 0x00007fff ) );
    2632         414 :         L_tmp1 = L_shl( blen, exp1 );
    2633         414 :         logic16();
    2634         414 :         mean_vector_fx[k] = Div_32( L_tmp, extract_h( L_tmp1 ), extract_l( L_shr( L_tmp1, 1 ) ) & 0x00007fff );
    2635         414 :         move32(); /*31 + 20 + exp1 - exp2*/
    2636         414 :         exp = add( 51, sub( exp, exp1 ) );
    2637         414 :         mean_vector_fx[k] = L_shl( mean_vector_fx[k], sub( 20, exp ) );
    2638         414 :         move32(); /*Q12->Q20*/
    2639             :     }
    2640             : 
    2641             :     /* dynamics control */
    2642         460 :     FOR( k = 0; k < nband_lf; ++k )
    2643             :     {
    2644         414 :         tmp = add( i_mult2( add( k, 1 ), blen ), sfidx );
    2645        7038 :         FOR( i = add( i_mult2( k, blen ), sfidx ); i < tmp; ++i )
    2646             :         {
    2647        6624 :             L_tmp = L_sub( coeff_out1_fx[i], mean_vector_fx[k] );
    2648        6624 :             exp = norm_l( L_tmp );
    2649        6624 :             L_tmp = L_shl( L_tmp, exp ); /* exp+12*/
    2650        6624 :             exp1 = norm_l( rn_weight0_fx );
    2651        6624 :             L_tmp1 = L_shl( rn_weight0_fx, exp1 ); /* exp1+10*/
    2652        6624 :             L_tmp = L_mult( extract_h( L_tmp ), extract_h( L_tmp1 ) );
    2653        6624 :             L_tmp = L_shr( L_tmp, add( exp, sub( exp1, 21 ) ) ); /* Q20*/
    2654        6624 :             coeff_out1_fx[i] = L_sub( coeff_out1_fx[i], L_tmp );
    2655        6624 :             move32();
    2656             :         }
    2657             :     }
    2658             : 
    2659          46 :     IF( EQ_16( hq_generic_exc_clas, HQ_GENERIC_EXC0 ) )
    2660             :     {
    2661          25 :         bwe_seed = add( add( shl( R[0], 3 ), shl( R[1], 2 ) ), add( shl( R[2], 1 ), R[3] ) );
    2662             : 
    2663        3625 :         FOR( n_freq = sfidx; n_freq < efidx; n_freq++ )
    2664             :         {
    2665        3600 :             IF( signum[n_freq] < 0 )
    2666             :             {
    2667        1810 :                 coeff_out1_fx[n_freq] = L_negate( coeff_out1_fx[n_freq] );
    2668        1810 :                 move32();
    2669             :             }
    2670             : 
    2671        3600 :             IF( Random( &bwe_seed ) < 0 )
    2672             :             {
    2673        1808 :                 coeff_out1_fx[n_freq] = L_negate( coeff_out1_fx[n_freq] );
    2674        1808 :                 move32();
    2675             :             }
    2676             :         }
    2677             :     }
    2678             :     ELSE
    2679             :     {
    2680        3045 :         FOR( n_freq = sfidx; n_freq < efidx; n_freq++ )
    2681             :         {
    2682        3024 :             IF( signum[n_freq] < 0 )
    2683             :             {
    2684        1499 :                 coeff_out1_fx[n_freq] = L_negate( coeff_out1_fx[n_freq] );
    2685        1499 :                 move32();
    2686             :             }
    2687             :         }
    2688             :     }
    2689             : 
    2690             :     /* normalizing modified low frequency spectrum */
    2691         460 :     FOR( k = 0; k < nband_lf; ++k )
    2692             :     {
    2693         414 :         energy_fx = 1;
    2694         414 :         move16();
    2695         414 :         tmp = add( i_mult2( add( k, 1 ), blen ), sfidx );
    2696        7038 :         FOR( i = add( ( i_mult2( k, blen ) ), sfidx ); i < tmp; ++i )
    2697             :         {
    2698        6624 :             exp = norm_l( coeff_out1_fx[i] );
    2699        6624 :             L_tmp1 = L_shl( coeff_out1_fx[i], exp ); /* exp + 12*/
    2700             : 
    2701        6624 :             L_tmp = Mult_32_32( L_tmp1, L_tmp1 );
    2702        6624 :             L_tmp = L_shr( L_tmp, sub( i_mult2( 2, exp ), 11 ) ); /*Q20  */
    2703        6624 :             energy_fx = L_add( energy_fx, L_tmp );
    2704             :         }
    2705             : 
    2706         414 :         exp = norm_l( energy_fx );
    2707         414 :         L_tmp = L_shl( energy_fx, sub( exp, 1 ) );
    2708         414 :         exp1 = norm_s( blen );
    2709         414 :         L_tmp1 = L_shl( (Word32) blen, add( exp1, 16 ) );
    2710             : 
    2711         414 :         L_tmp = Div_32( L_tmp, extract_h( L_tmp1 ), extract_l( L_shr( L_tmp1, 1 ) ) & 0x00007fff );
    2712         414 :         exp = sub( add( 34, exp ), exp1 );
    2713         414 :         L_tmp = L_shr( L_tmp, sub( exp, 31 ) );
    2714         414 :         exp = 31;
    2715         414 :         move16();
    2716             : 
    2717         414 :         exp = sub( 31, exp );
    2718         414 :         IF( exp & 0x1 )
    2719             :         {
    2720           0 :             L_tmp = L_shr( L_tmp, 1 );
    2721           0 :             exp = add( exp, 1 );
    2722             :         }
    2723         414 :         L_tmp = Sqrt_l( L_tmp, &exp1 );
    2724         414 :         exp = add( 31, sub( shr( exp1, 1 ), shr( exp, 1 ) ) );
    2725         414 :         energy_fx = L_shl( L_tmp, sub( 31, exp ) ); /*Q31*/
    2726             : 
    2727         414 :         tmp = add( i_mult2( add( k, 1 ), blen ), sfidx );
    2728        7038 :         FOR( i = add( ( i_mult2( k, blen ) ), sfidx ); i < tmp; ++i )
    2729             :         {
    2730        6624 :             IF( NE_32( L_abs( coeff_out1_fx[i] ), coeff_out1_fx[i] ) )
    2731             :             {
    2732        3317 :                 s = -1;
    2733        3317 :                 move16();
    2734        3317 :                 coeff_out1_fx[i] = L_abs( coeff_out1_fx[i] );
    2735        3317 :                 move32();
    2736             :             }
    2737             :             ELSE
    2738             :             {
    2739        3307 :                 s = 0;
    2740        3307 :                 move16();
    2741             :             }
    2742        6624 :             exp = norm_l( coeff_out1_fx[i] );
    2743        6624 :             L_tmp = L_shl( coeff_out1_fx[i], sub( exp, 1 ) );
    2744        6624 :             exp1 = norm_l( energy_fx );
    2745        6624 :             L_tmp1 = L_shl( (Word32) energy_fx, exp1 );
    2746        6624 :             logic16();
    2747        6624 :             L_tmp = Div_32( L_tmp, extract_h( L_tmp1 ), extract_l( L_shr( L_tmp1, 1 ) ) & 0x00007fff );
    2748        6624 :             exp = add( sub( 19, exp1 ), exp );
    2749        6624 :             coeff_out1_fx[i] = L_shl( L_tmp, add( sub( 12, exp ), 15 ) );
    2750        6624 :             move32(); /* Q12 -> Q27 */
    2751        6624 :             IF( s )
    2752             :             {
    2753        3317 :                 coeff_out1_fx[i] = L_negate( coeff_out1_fx[i] );
    2754        3317 :                 move32();
    2755             :             }
    2756             :         }
    2757             :     }
    2758             : 
    2759          46 :     Copy32( &coeff_out1_fx[HQ_GENERIC_OFFSET], &coeff_out_fx[HQ_GENERIC_HIGH0 + hq_generic_offset], HQ_GENERIC_LEN0 );
    2760          46 :     Copy32( &coeff_out1_fx[HQ_GENERIC_OFFSET], &coeff_out_fx[HQ_GENERIC_HIGH1 + hq_generic_offset], HQ_GENERIC_LEN0 );
    2761             : 
    2762          46 :     IF( LE_16( hq_generic_offset, HQ_GENERIC_FOFFSET_24K4 ) )
    2763             :     {
    2764          46 :         Copy32( &coeff_out1_fx[HQ_GENERIC_LOW0], &coeff_out_fx[HQ_GENERIC_HIGH2 + hq_generic_offset], HQ_GENERIC_END_FREQ - HQ_GENERIC_HIGH2 );
    2765             :     }
    2766             : 
    2767          46 :     IF( EQ_16( HQ_mode, HQ_GEN_FB ) )
    2768             :     {
    2769           0 :         IF( LE_16( hq_generic_offset, HQ_GENERIC_FOFFSET_24K4 ) )
    2770             :         {
    2771           0 :             Copy32( &coeff_out1_fx[HQ_GENERIC_LOW0 + HQ_GENERIC_END_FREQ - HQ_GENERIC_HIGH2], &coeff_out_fx[fb_bwe_subband[0]], 160 );
    2772             :         }
    2773             :         ELSE
    2774             :         {
    2775           0 :             Copy32( &coeff_out1_fx[HQ_GENERIC_OFFSET + HQ_GENERIC_LEN0], &coeff_out_fx[fb_bwe_subband[0]], 160 );
    2776             :         }
    2777             :     }
    2778             : 
    2779             : 
    2780          46 :     L_tmp1 = L_deposit_l( 0 );
    2781          46 :     L_tmp2 = L_deposit_l( 0 );
    2782         276 :     FOR( i = 0; i < 5; ++i )
    2783             :     {
    2784         230 :         L_tmp1 = L_add( L_tmp1, L_abs( coeff_out_fx[HQ_GENERIC_HIGH1 + hq_generic_offset + i] ) );
    2785         230 :         L_tmp2 = L_add( L_tmp2, L_abs( coeff_out_fx[HQ_GENERIC_HIGH1 - 2 + hq_generic_offset - i] ) );
    2786             :     }
    2787             : 
    2788          46 :     pit1_fx = &coeff_out_fx[HQ_GENERIC_HIGH1 + hq_generic_offset];
    2789          46 :     L_tmp1 = L_max( L_tmp1, 1 );
    2790          46 :     L_tmp2 = L_max( L_tmp2, 1 );
    2791          46 :     exp1 = norm_l( L_tmp1 );
    2792          46 :     exp2 = sub( norm_l( L_tmp2 ), 1 );
    2793          46 :     tmp1_fx = extract_h( L_shl( L_tmp1, exp1 ) );
    2794          46 :     tmp2_fx = extract_h( L_shl( L_tmp2, exp2 ) );
    2795          46 :     tmp3_fx = div_s( tmp2_fx, tmp1_fx );                   /*15 + exp2 + 15 - (exp1 + 15) */
    2796          46 :     tmp3_fx = shr( tmp3_fx, add( 5, sub( exp2, exp1 ) ) ); /*10 */
    2797             : 
    2798          46 :     if ( LT_16( tmp3_fx, 307 /*0.3 in Q10 */ ) )
    2799             :     {
    2800           0 :         tmp3_fx = 307 /*0.3 in Q10 */;
    2801           0 :         move16();
    2802             :     }
    2803          89 :     FOR( ; tmp3_fx < 1024; tmp3_fx += 102 )
    2804             :     {
    2805          43 :         *pit1_fx = Mult_32_16( *pit1_fx, shl( tmp3_fx, 5 ) );
    2806          43 :         move32(); /*15 + 5 + 10 -15 */
    2807          43 :         pit1_fx++;
    2808             :     }
    2809             : 
    2810          46 :     pit1_fx = &coeff_out_fx[HQ_GENERIC_HIGH1 - 1 + hq_generic_offset];
    2811             : 
    2812          46 :     exp1 = sub( norm_l( L_tmp1 ), 1 );
    2813          46 :     exp2 = norm_l( L_tmp2 );
    2814          46 :     tmp1_fx = extract_h( L_shl( L_tmp1, exp1 ) );
    2815          46 :     tmp2_fx = extract_h( L_shl( L_tmp2, exp2 ) );
    2816          46 :     tmp3_fx = div_s( tmp1_fx, tmp2_fx );                   /*15 + exp2 + 15 - (exp1 + 15) */
    2817          46 :     tmp3_fx = shr( tmp3_fx, add( 5, sub( exp1, exp2 ) ) ); /*10 */
    2818             : 
    2819          46 :     IF( GT_16( tmp3_fx, 5120 ) )
    2820             :     {
    2821           0 :         FOR( tmp3_fx = 5120; tmp3_fx > 1024; tmp3_fx -= 512 )
    2822             :         {
    2823             :             /* Adding saturation suggested as fix for issue #957 */
    2824           0 :             L_tmp1 = L_shl_sat( Mult_32_16( *pit1_fx, tmp3_fx ), 5 ); /*15 + 5 + 10 -15 */
    2825           0 :             *pit1_fx-- = L_tmp1;
    2826           0 :             move32();
    2827             :         }
    2828             :     }
    2829             : 
    2830          46 :     IF( LE_16( hq_generic_offset, HQ_GENERIC_FOFFSET_24K4 ) )
    2831             :     {
    2832          46 :         L_tmp1 = L_add( L_abs( coeff_out_fx[HQ_GENERIC_HIGH2 + hq_generic_offset] ), L_abs( coeff_out_fx[HQ_GENERIC_HIGH2 + 1 + hq_generic_offset] ) );
    2833          46 :         L_tmp2 = L_add( L_abs( coeff_out_fx[HQ_GENERIC_HIGH2 - 4 + hq_generic_offset] ), L_add( L_abs( coeff_out_fx[HQ_GENERIC_HIGH2 - 3 + hq_generic_offset] ),
    2834          46 :                                                                                                 L_add( L_abs( coeff_out_fx[HQ_GENERIC_HIGH2 - 2 + hq_generic_offset] ), L_abs( coeff_out_fx[HQ_GENERIC_HIGH2 - 1 + hq_generic_offset] ) ) ) );
    2835             : 
    2836          46 :         pit1_fx = &coeff_out_fx[HQ_GENERIC_HIGH2 + hq_generic_offset];
    2837             : 
    2838          46 :         L_tmp1 = L_max( L_tmp1, 1 );
    2839          46 :         L_tmp2 = L_max( L_tmp2, 1 );
    2840          46 :         exp1 = norm_l( L_tmp1 );
    2841          46 :         exp2 = sub( norm_l( L_tmp2 ), 1 );
    2842          46 :         tmp1_fx = extract_h( L_shl( L_tmp1, exp1 ) );
    2843          46 :         tmp2_fx = extract_h( L_shl( L_tmp2, exp2 ) );
    2844          46 :         tmp3_fx = div_s( tmp2_fx, tmp1_fx );                       /*15 + exp2 + 15 - (exp1 + 15) */
    2845          46 :         tmp3_fx = shr_sat( tmp3_fx, add( 5, sub( exp2, exp1 ) ) ); /*10 */
    2846          46 :         if ( LT_16( tmp3_fx, 307 /* 0.3 in Q10*/ ) )
    2847             :         {
    2848           0 :             tmp3_fx = 307; /* 0.3 in Q10*/
    2849           0 :             move16();
    2850             :         }
    2851          52 :         FOR( ; tmp3_fx < 1024; tmp3_fx += 102 )
    2852             :         {
    2853           6 :             L_tmp = L_shl( Mult_32_16( *pit1_fx, tmp3_fx ), 5 ); /*15 + 5 + 10 -15 */
    2854           6 :             *pit1_fx++ = L_tmp;
    2855           6 :             move32();
    2856             :         }
    2857             : 
    2858          46 :         pit1_fx = &coeff_out_fx[HQ_GENERIC_HIGH2 - 1 + hq_generic_offset];
    2859             : 
    2860          46 :         exp1 = sub( norm_l( L_tmp1 ), 1 );
    2861          46 :         exp2 = norm_l( L_tmp2 );
    2862          46 :         tmp1_fx = extract_h( L_shl( L_tmp1, exp1 ) );
    2863          46 :         tmp2_fx = extract_h( L_shl( L_tmp2, exp2 ) );
    2864          46 :         tmp3_fx = div_s( tmp1_fx, tmp2_fx );                   /*15 + exp2 + 15 - (exp1 + 15) */
    2865          46 :         tmp3_fx = shr( tmp3_fx, add( 5, sub( exp1, exp2 ) ) ); /*10 */
    2866          46 :         tmp3_fx = shr( tmp3_fx, 1 );
    2867          46 :         tmp4_fx = mult_r( tmp3_fx, 1638 /* 0.05 in Q15 */ );
    2868          46 :         WHILE( tmp3_fx > 1024 /* 1 in Q10*/ )
    2869             :         {
    2870           0 :             L_tmp1 = L_shl( Mult_32_16( *pit1_fx, tmp3_fx ), 5 ); /*15 + 5 + 10 -15 */
    2871           0 :             *pit1_fx-- = L_tmp1;
    2872           0 :             move32();
    2873           0 :             tmp3_fx = sub( tmp3_fx, tmp4_fx );
    2874             :         }
    2875             :     }
    2876             : 
    2877             : 
    2878          46 :     wfenv_fx = hq_generic_fenv_fx[0];
    2879          46 :     move16(); /*1 */
    2880          46 :     i = 0;
    2881          46 :     move16();
    2882          46 :     tmp2 = add( add( swb_bwe_subband[0], hq_generic_offset ), 8 );
    2883         414 :     FOR( n_freq = add( swb_bwe_subband[0], hq_generic_offset ); n_freq < tmp2; n_freq++ )
    2884             :     {
    2885         368 :         factor_fx = shl( i, 12 );                             /*15 */
    2886         368 :         L_tmp1 = L_mult( sub( 32767, factor_fx ), fenvL_fx ); /*17 */
    2887         368 :         L_tmp2 = L_mult( factor_fx, wfenv_fx );               /*17 */
    2888         368 :         L_tmp1 = L_add( L_tmp1, L_tmp2 );                     /*17 */
    2889             : 
    2890         368 :         cs = norm_l( L_tmp1 );
    2891         368 :         tmp = extract_h( L_shl( L_tmp1, cs ) );          /*17 + cs - 16 */
    2892         368 :         L_tmp = Mult_32_16( coeff_out_fx[n_freq], tmp ); /*12 + 15 + 17 + cs - 16 - 15 */
    2893         368 :         coeff_out_fx[n_freq] = L_shr( L_tmp, add( 1, cs ) );
    2894         368 :         move32(); /*12 */
    2895         368 :         i++;
    2896         368 :         move16();
    2897             :     }
    2898             : 
    2899          46 :     k = sub( nenv, 2 );
    2900         598 :     FOR( n_band = 0; n_band < k; n_band++ )
    2901             :     {
    2902         552 :         wfenv_fx = hq_generic_fenv_fx[n_band + 1];
    2903         552 :         move16(); /*1 */
    2904             : 
    2905         552 :         tmp2 = swb_bwe_sm_subband[n_band + 1] + hq_generic_offset;
    2906       12696 :         FOR( i = 0; n_freq < tmp2; i++ )
    2907             :         {
    2908       12144 :             L_tmp1 = L_mult( sub( wfenv_fx, hq_generic_fenv_fx[n_band] ), smooth_factor_fx[n_band] ); /*17 */
    2909       12144 :             L_tmp1 = Mult_32_16( L_tmp1, shl( i, 10 ) );                                              /*17 + 10 - 15 */
    2910       12144 :             L_tmp1 = L_add( L_tmp1, L_shl( hq_generic_fenv_fx[n_band], 11 ) );                        /*12 */
    2911             : 
    2912       12144 :             cs = norm_l( L_tmp1 );
    2913       12144 :             tmp = extract_h( L_shl( L_tmp1, cs ) );          /*12 + cs - 16 */
    2914       12144 :             L_tmp = Mult_32_16( coeff_out_fx[n_freq], tmp ); /*12 + 15 + 12 + cs - 16 - 15 */
    2915       12144 :             coeff_out_fx[n_freq] = L_shl( L_tmp, sub( 4, cs ) );
    2916       12144 :             move32(); /*12 */
    2917       12144 :             n_freq++;
    2918             :         }
    2919             :     }
    2920             : 
    2921          46 :     wfenv_fx = hq_generic_fenv_fx[nenv - 1];
    2922          46 :     move16(); /*1 */
    2923          46 :     tmp2 = add( swb_bwe_sm_subband[nenv - 1], hq_generic_offset );
    2924        1518 :     FOR( i = 0; n_freq < tmp2; i++ )
    2925             :     {
    2926        1472 :         L_tmp1 = L_mult( sub( wfenv_fx, hq_generic_fenv_fx[nenv - 2] ), smooth_factor_fx[nenv - 2] ); /*17 */
    2927        1472 :         L_tmp1 = Mult_32_16( L_tmp1, shl( i, 10 ) );                                                  /*17 + 10 - 15 */
    2928        1472 :         L_tmp1 = L_add( L_tmp1, L_shl( hq_generic_fenv_fx[nenv - 2], 11 ) );                          /*12 */
    2929             : 
    2930        1472 :         cs = norm_l( L_tmp1 );
    2931        1472 :         tmp = extract_h( L_shl( L_tmp1, cs ) );          /*12 + cs - 16 */
    2932        1472 :         L_tmp = Mult_32_16( coeff_out_fx[n_freq], tmp ); /*12 + 15 + 12 + cs - 16 - 15 */
    2933        1472 :         coeff_out_fx[n_freq] = L_shl( L_tmp, sub( 4, cs ) );
    2934        1472 :         move32(); /*12 */
    2935        1472 :         n_freq++;
    2936             :     }
    2937             : 
    2938          46 :     IF( EQ_16( HQ_mode, HQ_GEN_SWB ) )
    2939             :     {
    2940          92 :         FOR( n_band = sub( nenv, 1 ); n_band < nenv; ++n_band )
    2941             :         {
    2942          46 :             wfenv_fx = hq_generic_fenv_fx[n_band];
    2943          46 :             move16(); /*1 */
    2944          46 :             tmp2 = add( swb_bwe_subband[n_band + 1], hq_generic_offset );
    2945         782 :             FOR( ; n_freq < tmp2; n_freq++ )
    2946             :             {
    2947         736 :                 L_tmp = Mult_32_16( coeff_out_fx[n_freq], wfenv_fx ); /*15 + 12 + 1 - 15 */
    2948         736 :                 coeff_out_fx[n_freq] = L_shr( L_tmp, 1 );
    2949         736 :                 move32(); /*12 */
    2950             :             }
    2951             :         }
    2952             :     }
    2953             :     ELSE
    2954             :     {
    2955           0 :         test();
    2956           0 :         IF( GT_16( sub( hq_generic_fenv_fx[nenv - 1], hq_generic_fenv_fx[nenv] ), 30 ) || LT_16( hq_generic_fenv_fx[nenv], 10 ) )
    2957             :         {
    2958           0 :             wfenv_fx = hq_generic_fenv_fx[nenv - 1];
    2959           0 :             move16(); /*1 */
    2960           0 :             FOR( i = 0; n_freq < fb_bwe_subband[0]; i++ )
    2961             :             {
    2962           0 :                 L_tmp = Mult_32_16( coeff_out_fx[n_freq], wfenv_fx ); /*15 + 12 + 1 - 15 */
    2963           0 :                 coeff_out_fx[n_freq] = L_shr( L_tmp, 1 );
    2964           0 :                 move32(); /*12 */
    2965           0 :                 n_freq++;
    2966             :             }
    2967             : 
    2968           0 :             FOR( n_band = 0; n_band < DIM_FB; n_band++ )
    2969             :             {
    2970           0 :                 wfenv_fx = hq_generic_fenv_fx[n_band + nenv];
    2971           0 :                 move16(); /*1 */
    2972           0 :                 tmp2 = fb_bwe_subband[n_band + 1];
    2973           0 :                 FOR( i = 0; n_freq < tmp2; i++ )
    2974             :                 {
    2975           0 :                     L_tmp = Mult_32_16( coeff_out_fx[n_freq], wfenv_fx ); /*15 + 12 + 1 - 15 */
    2976           0 :                     coeff_out_fx[n_freq] = L_shr( L_tmp, 1 );
    2977           0 :                     move32(); /*12 */
    2978           0 :                     n_freq++;
    2979             :                 }
    2980             :             }
    2981             :         }
    2982             :         ELSE
    2983             :         {
    2984           0 :             FOR( n_band = 0; n_band < DIM_FB; n_band++ )
    2985             :             {
    2986           0 :                 wfenv_fx = hq_generic_fenv_fx[n_band + nenv - 1];
    2987           0 :                 move16(); /*1 */
    2988             : 
    2989           0 :                 FOR( i = 0; n_freq < fb_bwe_sm_subband[n_band]; i++ )
    2990             :                 {
    2991           0 :                     L_tmp1 = L_mult( sub( wfenv_fx, hq_generic_fenv_fx[n_band + nenv] ), fb_smooth_factor_fx[n_band] ); /*17 */
    2992           0 :                     L_tmp1 = Mult_32_16( L_tmp1, shl( i, 9 ) );                                                         /*17 + 9 - 15 */
    2993           0 :                     L_tmp1 = L_add( L_tmp1, L_shl( hq_generic_fenv_fx[n_band + nenv], 10 ) );                           /*11 */
    2994             : 
    2995           0 :                     cs = norm_l( L_tmp1 );
    2996           0 :                     tmp = extract_h( L_shl( L_tmp1, cs ) );          /*11 + cs - 16 */
    2997           0 :                     L_tmp = Mult_32_16( coeff_out_fx[n_freq], tmp ); /*12 + 15 + 11 + cs - 16 - 15 */
    2998           0 :                     coeff_out_fx[n_freq] = L_shl( L_tmp, sub( 5, cs ) );
    2999           0 :                     move32(); /*12 */
    3000           0 :                     n_freq = add( n_freq, 1 );
    3001             :                 }
    3002             :             }
    3003             : 
    3004           0 :             wfenv_fx = hq_generic_fenv_fx[tenv - 1];
    3005           0 :             move16(); /*1 */
    3006             : 
    3007           0 :             FOR( ; n_freq < fb_bwe_subband[DIM_FB]; n_freq++ )
    3008             :             {
    3009           0 :                 L_tmp = Mult_32_16( coeff_out_fx[n_freq], wfenv_fx ); /*15 + 12 + 1 - 15 */
    3010           0 :                 coeff_out_fx[n_freq] = L_shr( L_tmp, 1 );
    3011           0 :                 move32(); /*12 */
    3012             :             }
    3013             :         }
    3014             :     }
    3015             : 
    3016          46 :     return;
    3017             : }
    3018             : 
    3019        2604 : void hq_generic_decoding_ivas_fx(
    3020             :     const Word16 HQ_mode,             /* i  : HQ mode                                      : Q0  */
    3021             :     Word32 *coeff_out1_fx,            /* i/o: BWE input & temporary buffer                 : Q12 */
    3022             :     const Word16 *hq_generic_fenv_fx, /* i  : SWB frequency envelopes                      : Q1  */
    3023             :     Word32 *coeff_out_fx,             /* o  : SWB signal in MDCT domain                    : Q12 */
    3024             :     const Word16 hq_generic_offset,   /* i  : frequency offset for representing hq generci : Q0  */
    3025             :     Word16 *prev_L_swb_norm,          /* i/o: last normalize length                        : Q0  */
    3026             :     const Word16 hq_generic_exc_clas, /* i  : bwe excitation class                         " Q0  */
    3027             :     const Word16 *R )
    3028             : {
    3029             :     Word16 i, n_freq, n_band, L_swb_norm;
    3030             :     Word16 k;
    3031             :     Word16 nenv;
    3032             :     Word16 tenv;
    3033             : 
    3034             :     Word16 exp, exp1, exp2, frac, tmp, tmp2, cs;
    3035             :     Word32 L_tmp, L_tmp1, L_tmp2, max_coeff_fx;
    3036             :     Word16 fenvL_fx, wfenv_fx, factor_fx;
    3037             :     Word32 *pit1_fx;
    3038             :     Word16 tmp1_fx, tmp2_fx, tmp3_fx, tmp4_fx;
    3039             :     Word32 energy_fx;
    3040             : 
    3041             :     Word32 envelope_fx[L_FRAME16k];
    3042             :     Word32 mean_vector_fx[20];
    3043             :     Word16 rn_weight0_fx;
    3044             :     Word16 s;
    3045             :     Word16 blen, nband_lf, sfidx, efidx;
    3046             :     Word16 bwe_seed;
    3047             :     Word16 signum[L_FRAME16k];
    3048             : 
    3049        2604 :     nenv = sub( SWB_FENV, 2 );
    3050        2604 :     if ( LE_16( hq_generic_offset, HQ_GENERIC_FOFFSET_24K4 ) )
    3051             :     {
    3052        2604 :         nenv = SWB_FENV;
    3053        2604 :         move16();
    3054             :     }
    3055             : 
    3056        2604 :     tenv = nenv;
    3057        2604 :     move16();
    3058        2604 :     if ( EQ_16( HQ_mode, HQ_GEN_FB ) )
    3059             :     {
    3060        2020 :         tenv = add( nenv, DIM_FB );
    3061             :     }
    3062             : 
    3063        2604 :     max_coeff_fx = 0;
    3064        2604 :     move16();
    3065        2604 :     tmp = add( swb_bwe_subband[0], hq_generic_offset );
    3066       44268 :     FOR( n_freq = add( HQ_GENERIC_ST_FREQ, hq_generic_offset ); n_freq < tmp; n_freq++ )
    3067             :     {
    3068       41664 :         max_coeff_fx = L_max( max_coeff_fx, L_abs( coeff_out1_fx[n_freq] ) );
    3069             :     }
    3070        2604 :     cs = norm_l( max_coeff_fx );
    3071             : 
    3072        2604 :     L_tmp = 0;
    3073        2604 :     move16();
    3074        2604 :     tmp2 = add( swb_bwe_subband[0], hq_generic_offset );
    3075       44268 :     FOR( n_freq = add( HQ_GENERIC_ST_FREQ, hq_generic_offset ); n_freq < tmp2; n_freq++ )
    3076             :     {
    3077       41664 :         tmp = extract_h( L_shl( coeff_out1_fx[n_freq], cs ) ); /*12 + cs - 16 */
    3078       41664 :         L_tmp1 = L_mult0( tmp, tmp );                          /*2*(cs-2) */
    3079       41664 :         L_tmp = L_add( L_tmp, L_shr( L_tmp1, 5 ) );            /*2*(cs-2) - 5 */
    3080             :     }
    3081        2604 :     cs = sub( shl( cs, 1 ), 9 );
    3082        2604 :     fenvL_fx = 0;
    3083        2604 :     move16();
    3084        2604 :     IF( L_tmp != 0 )
    3085             :     {
    3086        2604 :         exp = norm_l( L_tmp );
    3087        2604 :         frac = round_fx_sat( L_shl( L_tmp, exp ) ); /*cs+exp-16 */
    3088        2604 :         tmp = div_s( 16384, frac );                 /*15 + 14 - (cs+exp-16) */
    3089        2604 :         exp = sub( add( cs, exp ), 30 );
    3090        2604 :         L_tmp = Isqrt_lc( L_deposit_h( tmp ), &exp );                  /*Q31 - exp */
    3091        2604 :         fenvL_fx = round_fx_sat( L_shl_sat( L_tmp, sub( exp, 14 ) ) ); /*Q1 */
    3092             :     }
    3093             : 
    3094        2604 :     calc_normal_length_fx_32( HQ_CORE, coeff_out1_fx, HQ_GEN_SWB, -1, &L_swb_norm, prev_L_swb_norm );
    3095             : 
    3096        2604 :     calc_norm_envelop_lf_fx( coeff_out1_fx, envelope_fx, &L_swb_norm, HQ_mode, hq_generic_offset, &sfidx, &efidx );
    3097             : 
    3098        2604 :     blen = 16;
    3099        2604 :     move16();
    3100             : 
    3101        2604 :     IF( EQ_16( hq_generic_exc_clas, HQ_GENERIC_EXC0 ) )
    3102             :     {
    3103        2501 :         rn_weight0_fx = 819;
    3104        2501 :         move16(); /* 0.8 Q10 */
    3105             :     }
    3106         103 :     ELSE IF( EQ_16( hq_generic_exc_clas, HQ_GENERIC_EXC1 ) )
    3107             :     {
    3108          16 :         rn_weight0_fx = 51;
    3109          16 :         move16(); /* 0.05 Q10*/
    3110             :     }
    3111             :     ELSE
    3112             :     {
    3113          87 :         rn_weight0_fx = 205;
    3114          87 :         move16(); /* 0.02 Q10 */
    3115             :     }
    3116             : 
    3117        2604 :     tmp = sub( efidx, sfidx );
    3118        2604 :     IF( tmp == 0 )
    3119             :     {
    3120           0 :         nband_lf = 0;
    3121           0 :         move16();
    3122             :     }
    3123             :     ELSE
    3124             :     {
    3125        2604 :         exp = norm_s( tmp );
    3126        2604 :         nband_lf = shl( tmp, sub( exp, 1 ) );
    3127        2604 :         exp1 = norm_s( blen );
    3128        2604 :         tmp = shl( blen, exp1 );
    3129        2604 :         nband_lf = shr( div_s( nband_lf, tmp ), add( sub( 14, exp1 ), exp ) ); /* 15 + exp-1 - exp1, Q0*/
    3130             :     }
    3131             : 
    3132      700780 :     FOR( n_freq = sfidx; n_freq < efidx; n_freq++ )
    3133             :     {
    3134      698176 :         IF( coeff_out1_fx[n_freq] < 0 )
    3135             :         {
    3136      347641 :             signum[n_freq] = -1;
    3137      347641 :             move16();
    3138      347641 :             coeff_out1_fx[n_freq] = L_negate( coeff_out1_fx[n_freq] );
    3139      347641 :             move32();
    3140             :         }
    3141             :         ELSE
    3142             :         {
    3143      350535 :             signum[n_freq] = 1;
    3144      350535 :             move16();
    3145             :         }
    3146             :     }
    3147             : 
    3148             :     /* applying whitening */
    3149      700780 :     FOR( n_freq = sfidx; n_freq < efidx; n_freq++ )
    3150             :     {
    3151      698176 :         exp = norm_l( coeff_out1_fx[n_freq] ) - 1;
    3152      698176 :         exp1 = norm_l( envelope_fx[n_freq] );
    3153             : 
    3154      698176 :         L_tmp = L_shl( coeff_out1_fx[n_freq], exp );
    3155      698176 :         L_tmp1 = L_shl( envelope_fx[n_freq], exp1 );
    3156             : 
    3157      698176 :         logic16();
    3158      698176 :         coeff_out1_fx[n_freq] = Div_32( L_tmp, extract_h( L_tmp1 ), extract_l( L_shr( L_tmp1, 1 ) ) & 0x00007fff );
    3159      698176 :         move32(); /*31 + exp1 - exp2*/
    3160             : 
    3161      698176 :         exp = add( 31, sub( exp, exp1 ) );
    3162      698176 :         coeff_out1_fx[n_freq] = L_shl( coeff_out1_fx[n_freq], sub( 20, exp ) );
    3163      698176 :         move32(); /*Q12->Q20*/
    3164             :     }
    3165             : 
    3166             :     /* mean vector generation for controlling dynamic range */
    3167       46240 :     FOR( k = 0; k < nband_lf; ++k )
    3168             :     {
    3169       43636 :         energy_fx = 1;
    3170       43636 :         move16();
    3171             : 
    3172       43636 :         tmp = add( i_mult2( add( k, 1 ), blen ), sfidx );
    3173      741812 :         FOR( i = add( i_mult2( k, blen ), sfidx ); i < tmp; ++i )
    3174             :         {
    3175      698176 :             energy_fx = L_add( energy_fx, coeff_out1_fx[i] );
    3176             :         }
    3177       43636 :         exp = sub( norm_l( energy_fx ), 1 );
    3178       43636 :         L_tmp = L_shl( energy_fx, exp );
    3179       43636 :         exp1 = norm_l( L_and( blen, 0x00007fff ) );
    3180       43636 :         L_tmp1 = L_shl( blen, exp1 );
    3181       43636 :         logic16();
    3182       43636 :         mean_vector_fx[k] = Div_32( L_tmp, extract_h( L_tmp1 ), extract_l( L_shr( L_tmp1, 1 ) ) & 0x00007fff );
    3183       43636 :         move32(); /*31 + 20 + exp1 - exp2*/
    3184       43636 :         exp = add( 51, sub( exp, exp1 ) );
    3185       43636 :         mean_vector_fx[k] = L_shl( mean_vector_fx[k], sub( 20, exp ) );
    3186       43636 :         move32(); /*Q12->Q20*/
    3187             :     }
    3188             : 
    3189             :     /* dynamics control */
    3190       46240 :     FOR( k = 0; k < nband_lf; ++k )
    3191             :     {
    3192       43636 :         tmp = add( i_mult2( add( k, 1 ), blen ), sfidx );
    3193      741812 :         FOR( i = add( i_mult2( k, blen ), sfidx ); i < tmp; ++i )
    3194             :         {
    3195      698176 :             L_tmp = L_sub( coeff_out1_fx[i], mean_vector_fx[k] );
    3196      698176 :             exp = norm_l( L_tmp );
    3197      698176 :             L_tmp = L_shl( L_tmp, exp ); /* exp+12*/
    3198      698176 :             exp1 = norm_l( rn_weight0_fx );
    3199      698176 :             L_tmp1 = L_shl( rn_weight0_fx, exp1 ); /* exp1+10*/
    3200      698176 :             L_tmp = L_mult( extract_h( L_tmp ), extract_h( L_tmp1 ) );
    3201      698176 :             L_tmp = L_shr( L_tmp, add( exp, sub( exp1, 21 ) ) ); /* Q20*/
    3202      698176 :             coeff_out1_fx[i] = L_sub( coeff_out1_fx[i], L_tmp );
    3203      698176 :             move32();
    3204             :         }
    3205             :     }
    3206             : 
    3207        2604 :     IF( EQ_16( hq_generic_exc_clas, HQ_GENERIC_EXC0 ) )
    3208             :     {
    3209        2501 :         bwe_seed = add( add( shl( R[0], 3 ), shl( R[1], 2 ) ), add( shl( R[2], 1 ), R[3] ) );
    3210             : 
    3211      673845 :         FOR( n_freq = sfidx; n_freq < efidx; n_freq++ )
    3212             :         {
    3213      671344 :             IF( signum[n_freq] < 0 )
    3214             :             {
    3215      334399 :                 coeff_out1_fx[n_freq] = L_negate( coeff_out1_fx[n_freq] );
    3216      334399 :                 move32();
    3217             :             }
    3218             : 
    3219      671344 :             IF( Random( &bwe_seed ) < 0 )
    3220             :             {
    3221      333128 :                 coeff_out1_fx[n_freq] = L_negate( coeff_out1_fx[n_freq] );
    3222      333128 :                 move32();
    3223             :             }
    3224             :         }
    3225             :     }
    3226             :     ELSE
    3227             :     {
    3228       26935 :         FOR( n_freq = sfidx; n_freq < efidx; n_freq++ )
    3229             :         {
    3230       26832 :             IF( signum[n_freq] < 0 )
    3231             :             {
    3232       13242 :                 coeff_out1_fx[n_freq] = L_negate( coeff_out1_fx[n_freq] );
    3233       13242 :                 move32();
    3234             :             }
    3235             :         }
    3236             :     }
    3237             : 
    3238             :     /* normalizing modified low frequency spectrum */
    3239       46240 :     FOR( k = 0; k < nband_lf; ++k )
    3240             :     {
    3241       43636 :         energy_fx = 1;
    3242       43636 :         move16();
    3243       43636 :         tmp = add( i_mult2( add( k, 1 ), blen ), sfidx );
    3244      741812 :         FOR( i = add( ( i_mult2( k, blen ) ), sfidx ); i < tmp; ++i )
    3245             :         {
    3246      698176 :             exp = norm_l( coeff_out1_fx[i] );
    3247      698176 :             L_tmp1 = L_shl( coeff_out1_fx[i], exp ); /* exp + 12*/
    3248             : 
    3249      698176 :             L_tmp = Mult_32_32( L_tmp1, L_tmp1 );
    3250      698176 :             L_tmp = L_shr( L_tmp, sub( i_mult2( 2, exp ), 11 ) ); /*Q20  */
    3251      698176 :             energy_fx = L_add( energy_fx, L_tmp );
    3252             :         }
    3253             : 
    3254       43636 :         exp = norm_l( energy_fx );
    3255       43636 :         L_tmp = L_shl( energy_fx, sub( exp, 1 ) );
    3256       43636 :         exp1 = norm_s( blen );
    3257       43636 :         L_tmp1 = L_shl( (Word32) blen, add( exp1, 16 ) );
    3258             : 
    3259       43636 :         L_tmp = Div_32( L_tmp, extract_h( L_tmp1 ), extract_l( L_shr( L_tmp1, 1 ) ) & 0x00007fff );
    3260       43636 :         exp = sub( add( 34, exp ), exp1 );
    3261       43636 :         L_tmp = L_shr( L_tmp, sub( exp, 31 ) );
    3262       43636 :         exp = 31;
    3263       43636 :         move16();
    3264             : 
    3265       43636 :         exp = sub( 31, exp );
    3266       43636 :         IF( exp & 0x1 )
    3267             :         {
    3268           0 :             L_tmp = L_shr( L_tmp, 1 );
    3269           0 :             exp = add( exp, 1 );
    3270             :         }
    3271       43636 :         L_tmp = Sqrt_l( L_tmp, &exp1 );
    3272       43636 :         exp = add( 31, sub( shr( exp1, 1 ), shr( exp, 1 ) ) );
    3273       43636 :         energy_fx = L_shl( L_tmp, sub( 31, exp ) ); /*Q31*/
    3274             : 
    3275       43636 :         tmp = add( i_mult2( add( k, 1 ), blen ), sfidx );
    3276      741812 :         FOR( i = add( ( i_mult2( k, blen ) ), sfidx ); i < tmp; ++i )
    3277             :         {
    3278      698176 :             IF( NE_32( L_abs( coeff_out1_fx[i] ), coeff_out1_fx[i] ) )
    3279             :             {
    3280      349133 :                 s = -1;
    3281      349133 :                 move16();
    3282      349133 :                 coeff_out1_fx[i] = L_abs( coeff_out1_fx[i] );
    3283      349133 :                 move32();
    3284             :             }
    3285             :             ELSE
    3286             :             {
    3287      349043 :                 s = 0;
    3288      349043 :                 move16();
    3289             :             }
    3290      698176 :             exp = norm_l( coeff_out1_fx[i] );
    3291      698176 :             L_tmp = L_shl( coeff_out1_fx[i], sub( exp, 1 ) );
    3292      698176 :             exp1 = norm_l( energy_fx );
    3293      698176 :             L_tmp1 = L_shl( (Word32) energy_fx, exp1 );
    3294      698176 :             logic16();
    3295      698176 :             L_tmp = Div_32( L_tmp, extract_h( L_tmp1 ), extract_l( L_shr( L_tmp1, 1 ) ) & 0x00007fff );
    3296      698176 :             exp = add( sub( 19, exp1 ), exp );
    3297      698176 :             coeff_out1_fx[i] = L_shl( L_tmp, add( sub( 12, exp ), 15 ) );
    3298      698176 :             move32(); /* Q12 -> Q27 */
    3299      698176 :             IF( s )
    3300             :             {
    3301      349133 :                 coeff_out1_fx[i] = L_negate( coeff_out1_fx[i] );
    3302      349133 :                 move32();
    3303             :             }
    3304             :         }
    3305             :     }
    3306             : 
    3307        2604 :     Copy32( &coeff_out1_fx[HQ_GENERIC_OFFSET], &coeff_out_fx[HQ_GENERIC_HIGH0 + hq_generic_offset], HQ_GENERIC_LEN0 );
    3308        2604 :     Copy32( &coeff_out1_fx[HQ_GENERIC_OFFSET], &coeff_out_fx[HQ_GENERIC_HIGH1 + hq_generic_offset], HQ_GENERIC_LEN0 );
    3309             : 
    3310        2604 :     IF( LE_16( hq_generic_offset, HQ_GENERIC_FOFFSET_24K4 ) )
    3311             :     {
    3312        2604 :         Copy32( &coeff_out1_fx[HQ_GENERIC_LOW0], &coeff_out_fx[HQ_GENERIC_HIGH2 + hq_generic_offset], HQ_GENERIC_END_FREQ - HQ_GENERIC_HIGH2 );
    3313             :     }
    3314             : 
    3315        2604 :     IF( EQ_16( HQ_mode, HQ_GEN_FB ) )
    3316             :     {
    3317        2020 :         IF( LE_16( hq_generic_offset, HQ_GENERIC_FOFFSET_24K4 ) )
    3318             :         {
    3319        2020 :             Copy32( &coeff_out1_fx[HQ_GENERIC_LOW0 + HQ_GENERIC_END_FREQ - HQ_GENERIC_HIGH2], &coeff_out_fx[fb_bwe_subband[0]], 160 );
    3320             :         }
    3321             :         ELSE
    3322             :         {
    3323           0 :             Copy32( &coeff_out1_fx[HQ_GENERIC_OFFSET + HQ_GENERIC_LEN0], &coeff_out_fx[fb_bwe_subband[0]], 160 );
    3324             :         }
    3325             :     }
    3326             : 
    3327             : 
    3328        2604 :     L_tmp1 = L_deposit_l( 0 );
    3329        2604 :     L_tmp2 = L_deposit_l( 0 );
    3330       15624 :     FOR( i = 0; i < 5; ++i )
    3331             :     {
    3332       13020 :         L_tmp1 = L_add( L_tmp1, L_shr( L_abs( coeff_out_fx[HQ_GENERIC_HIGH1 + hq_generic_offset + i] ), 3 ) );     // adding guard bits
    3333       13020 :         L_tmp2 = L_add( L_tmp2, L_shr( L_abs( coeff_out_fx[HQ_GENERIC_HIGH1 - 2 + hq_generic_offset - i] ), 3 ) ); // adding guard bits
    3334             :     }
    3335             : 
    3336        2604 :     pit1_fx = &coeff_out_fx[HQ_GENERIC_HIGH1 + hq_generic_offset];
    3337        2604 :     L_tmp1 = L_max( L_tmp1, 1 );
    3338        2604 :     L_tmp2 = L_max( L_tmp2, 1 );
    3339        2604 :     exp1 = norm_l( L_tmp1 );
    3340        2604 :     exp2 = sub( norm_l( L_tmp2 ), 1 );
    3341        2604 :     tmp1_fx = extract_h( L_shl( L_tmp1, exp1 ) );
    3342        2604 :     tmp2_fx = extract_h( L_shl( L_tmp2, exp2 ) );
    3343        2604 :     tmp3_fx = div_s( tmp2_fx, tmp1_fx );                   /*15 + exp2 + 15 - (exp1 + 15) */
    3344        2604 :     tmp3_fx = shr( tmp3_fx, add( 5, sub( exp2, exp1 ) ) ); /*10 */
    3345             : 
    3346        2604 :     if ( LT_16( tmp3_fx, 307 /*0.3 in Q10 */ ) )
    3347             :     {
    3348           0 :         tmp3_fx = 307 /*0.3 in Q10 */;
    3349           0 :         move16();
    3350             :     }
    3351        4862 :     FOR( ; tmp3_fx < 1024; tmp3_fx += 102 )
    3352             :     {
    3353        2258 :         *pit1_fx = Mult_32_16( *pit1_fx, shl( tmp3_fx, 5 ) );
    3354        2258 :         move32(); /*15 + 5 + 10 -15 */
    3355        2258 :         pit1_fx++;
    3356             :     }
    3357             : 
    3358        2604 :     pit1_fx = &coeff_out_fx[HQ_GENERIC_HIGH1 - 1 + hq_generic_offset];
    3359             : 
    3360        2604 :     exp1 = sub( norm_l( L_tmp1 ), 1 );
    3361        2604 :     exp2 = norm_l( L_tmp2 );
    3362        2604 :     tmp1_fx = extract_h( L_shl( L_tmp1, exp1 ) );
    3363        2604 :     tmp2_fx = extract_h( L_shl( L_tmp2, exp2 ) );
    3364        2604 :     tmp3_fx = div_s( tmp1_fx, tmp2_fx );                   /*15 + exp2 + 15 - (exp1 + 15) */
    3365        2604 :     tmp3_fx = shr( tmp3_fx, add( 5, sub( exp1, exp2 ) ) ); /*10 */
    3366             : 
    3367        2604 :     IF( GT_16( tmp3_fx, 5120 ) )
    3368             :     {
    3369           0 :         FOR( tmp3_fx = 5120; tmp3_fx > 1024; tmp3_fx -= 512 )
    3370             :         {
    3371             :             /* Adding saturation suggested as fix for issue #957 */
    3372           0 :             L_tmp1 = L_shl_sat( Mult_32_16( *pit1_fx, tmp3_fx ), 5 ); /*15 + 5 + 10 -15 */
    3373           0 :             *pit1_fx-- = L_tmp1;
    3374           0 :             move32();
    3375             :         }
    3376             :     }
    3377             : 
    3378        2604 :     IF( LE_16( hq_generic_offset, HQ_GENERIC_FOFFSET_24K4 ) )
    3379             :     {
    3380        2604 :         L_tmp1 = L_add( L_abs( coeff_out_fx[HQ_GENERIC_HIGH2 + hq_generic_offset] ), L_abs( coeff_out_fx[HQ_GENERIC_HIGH2 + 1 + hq_generic_offset] ) );
    3381        2604 :         L_tmp2 = L_add( L_abs( coeff_out_fx[HQ_GENERIC_HIGH2 - 4 + hq_generic_offset] ), L_add( L_abs( coeff_out_fx[HQ_GENERIC_HIGH2 - 3 + hq_generic_offset] ),
    3382        2604 :                                                                                                 L_add( L_abs( coeff_out_fx[HQ_GENERIC_HIGH2 - 2 + hq_generic_offset] ), L_abs( coeff_out_fx[HQ_GENERIC_HIGH2 - 1 + hq_generic_offset] ) ) ) );
    3383             : 
    3384        2604 :         pit1_fx = &coeff_out_fx[HQ_GENERIC_HIGH2 + hq_generic_offset];
    3385             : 
    3386        2604 :         L_tmp1 = L_max( L_tmp1, 1 );
    3387        2604 :         L_tmp2 = L_max( L_tmp2, 1 );
    3388        2604 :         exp1 = norm_l( L_tmp1 );
    3389        2604 :         exp2 = sub( norm_l( L_tmp2 ), 1 );
    3390        2604 :         tmp1_fx = extract_h( L_shl( L_tmp1, exp1 ) );
    3391        2604 :         tmp2_fx = extract_h( L_shl( L_tmp2, exp2 ) );
    3392        2604 :         tmp3_fx = div_s( tmp2_fx, tmp1_fx );                       /*15 + exp2 + 15 - (exp1 + 15) */
    3393        2604 :         tmp3_fx = shr_sat( tmp3_fx, add( 5, sub( exp2, exp1 ) ) ); /*10 */
    3394        2604 :         if ( LT_16( tmp3_fx, 307 /* 0.3 in Q10*/ ) )
    3395             :         {
    3396           0 :             tmp3_fx = 307; /* 0.3 in Q10*/
    3397           0 :             move16();
    3398             :         }
    3399        2608 :         FOR( ; tmp3_fx < 1024; tmp3_fx += 102 )
    3400             :         {
    3401           4 :             L_tmp = L_shl( Mult_32_16( *pit1_fx, tmp3_fx ), 5 ); /*15 + 5 + 10 -15 */
    3402           4 :             *pit1_fx++ = L_tmp;
    3403           4 :             move32();
    3404             :         }
    3405             : 
    3406        2604 :         pit1_fx = &coeff_out_fx[HQ_GENERIC_HIGH2 - 1 + hq_generic_offset];
    3407             : 
    3408        2604 :         exp1 = sub( norm_l( L_tmp1 ), 1 );
    3409        2604 :         exp2 = norm_l( L_tmp2 );
    3410        2604 :         tmp1_fx = extract_h( L_shl( L_tmp1, exp1 ) );
    3411        2604 :         tmp2_fx = extract_h( L_shl( L_tmp2, exp2 ) );
    3412        2604 :         tmp3_fx = div_s( tmp1_fx, tmp2_fx );                   /*15 + exp2 + 15 - (exp1 + 15) */
    3413        2604 :         tmp3_fx = shr( tmp3_fx, add( 5, sub( exp1, exp2 ) ) ); /*10 */
    3414        2604 :         tmp3_fx = shr( tmp3_fx, 1 );
    3415        2604 :         tmp4_fx = mult_r( tmp3_fx, 1638 /* 0.05 in Q15 */ );
    3416        2604 :         WHILE( tmp3_fx > 1024 /* 1 in Q10*/ )
    3417             :         {
    3418           0 :             L_tmp1 = L_shl( Mult_32_16( *pit1_fx, tmp3_fx ), 5 ); /*15 + 5 + 10 -15 */
    3419           0 :             *pit1_fx-- = L_tmp1;
    3420           0 :             move32();
    3421           0 :             tmp3_fx = sub( tmp3_fx, tmp4_fx );
    3422             :         }
    3423             :     }
    3424             : 
    3425             : 
    3426        2604 :     wfenv_fx = hq_generic_fenv_fx[0];
    3427        2604 :     move16(); /*1 */
    3428        2604 :     i = 0;
    3429        2604 :     move16();
    3430        2604 :     tmp2 = add( add( swb_bwe_subband[0], hq_generic_offset ), 8 );
    3431       23436 :     FOR( n_freq = add( swb_bwe_subband[0], hq_generic_offset ); n_freq < tmp2; n_freq++ )
    3432             :     {
    3433       20832 :         factor_fx = shl( i, 12 );                             /*15 */
    3434       20832 :         L_tmp1 = L_mult( sub( 32767, factor_fx ), fenvL_fx ); /*17 */
    3435       20832 :         L_tmp2 = L_mult( factor_fx, wfenv_fx );               /*17 */
    3436       20832 :         L_tmp1 = L_add( L_tmp1, L_tmp2 );                     /*17 */
    3437             : 
    3438       20832 :         cs = norm_l( L_tmp1 );
    3439       20832 :         tmp = extract_h( L_shl( L_tmp1, cs ) );          /*17 + cs - 16 */
    3440       20832 :         L_tmp = Mult_32_16( coeff_out_fx[n_freq], tmp ); /*12 + 15 + 17 + cs - 16 - 15 */
    3441       20832 :         coeff_out_fx[n_freq] = L_shr( L_tmp, add( 1, cs ) );
    3442       20832 :         move32(); /*12 */
    3443       20832 :         i++;
    3444       20832 :         move16();
    3445             :     }
    3446             : 
    3447        2604 :     k = sub( nenv, 2 );
    3448       33852 :     FOR( n_band = 0; n_band < k; n_band++ )
    3449             :     {
    3450       31248 :         wfenv_fx = hq_generic_fenv_fx[n_band + 1];
    3451       31248 :         move16(); /*1 */
    3452             : 
    3453       31248 :         tmp2 = swb_bwe_sm_subband[n_band + 1] + hq_generic_offset;
    3454      718704 :         FOR( i = 0; n_freq < tmp2; i++ )
    3455             :         {
    3456      687456 :             L_tmp1 = L_mult( sub( wfenv_fx, hq_generic_fenv_fx[n_band] ), smooth_factor_fx[n_band] ); /*17 */
    3457      687456 :             L_tmp1 = Mult_32_16( L_tmp1, shl( i, 10 ) );                                              /*17 + 10 - 15 */
    3458      687456 :             L_tmp1 = L_add( L_tmp1, L_shl( hq_generic_fenv_fx[n_band], 11 ) );                        /*12 */
    3459             : 
    3460      687456 :             cs = norm_l( L_tmp1 );
    3461      687456 :             tmp = extract_h( L_shl( L_tmp1, cs ) );          /*12 + cs - 16 */
    3462      687456 :             L_tmp = Mult_32_16( coeff_out_fx[n_freq], tmp ); /*12 + 15 + 12 + cs - 16 - 15 */
    3463      687456 :             coeff_out_fx[n_freq] = L_shl( L_tmp, sub( 4, cs ) );
    3464      687456 :             move32(); /*12 */
    3465      687456 :             n_freq++;
    3466             :         }
    3467             :     }
    3468             : 
    3469        2604 :     wfenv_fx = hq_generic_fenv_fx[nenv - 1];
    3470        2604 :     move16(); /*1 */
    3471        2604 :     tmp2 = add( swb_bwe_sm_subband[nenv - 1], hq_generic_offset );
    3472       85932 :     FOR( i = 0; n_freq < tmp2; i++ )
    3473             :     {
    3474       83328 :         L_tmp1 = L_mult( sub( wfenv_fx, hq_generic_fenv_fx[nenv - 2] ), smooth_factor_fx[nenv - 2] ); /*17 */
    3475       83328 :         L_tmp1 = Mult_32_16( L_tmp1, shl( i, 10 ) );                                                  /*17 + 10 - 15 */
    3476       83328 :         L_tmp1 = L_add( L_tmp1, L_shl( hq_generic_fenv_fx[nenv - 2], 11 ) );                          /*12 */
    3477             : 
    3478       83328 :         cs = norm_l( L_tmp1 );
    3479       83328 :         tmp = extract_h( L_shl( L_tmp1, cs ) );          /*12 + cs - 16 */
    3480       83328 :         L_tmp = Mult_32_16( coeff_out_fx[n_freq], tmp ); /*12 + 15 + 12 + cs - 16 - 15 */
    3481       83328 :         coeff_out_fx[n_freq] = L_shl( L_tmp, sub( 4, cs ) );
    3482       83328 :         move32(); /*12 */
    3483       83328 :         n_freq++;
    3484             :     }
    3485             : 
    3486        2604 :     IF( EQ_16( HQ_mode, HQ_GEN_SWB ) )
    3487             :     {
    3488        1168 :         FOR( n_band = sub( nenv, 1 ); n_band < nenv; ++n_band )
    3489             :         {
    3490         584 :             wfenv_fx = hq_generic_fenv_fx[n_band];
    3491         584 :             move16(); /*1 */
    3492         584 :             tmp2 = add( swb_bwe_subband[n_band + 1], hq_generic_offset );
    3493        9928 :             FOR( ; n_freq < tmp2; n_freq++ )
    3494             :             {
    3495        9344 :                 L_tmp = Mult_32_16( coeff_out_fx[n_freq], wfenv_fx ); /*15 + 12 + 1 - 15 */
    3496        9344 :                 coeff_out_fx[n_freq] = L_shr( L_tmp, 1 );
    3497        9344 :                 move32(); /*12 */
    3498             :             }
    3499             :         }
    3500             :     }
    3501             :     ELSE
    3502             :     {
    3503        2020 :         test();
    3504        2020 :         IF( GT_16( sub( hq_generic_fenv_fx[nenv - 1], hq_generic_fenv_fx[nenv] ), 30 ) || LT_16( hq_generic_fenv_fx[nenv], 10 ) )
    3505             :         {
    3506        1549 :             wfenv_fx = hq_generic_fenv_fx[nenv - 1];
    3507        1549 :             move16(); /*1 */
    3508       26333 :             FOR( i = 0; n_freq < fb_bwe_subband[0]; i++ )
    3509             :             {
    3510       24784 :                 L_tmp = Mult_32_16( coeff_out_fx[n_freq], wfenv_fx ); /*15 + 12 + 1 - 15 */
    3511       24784 :                 coeff_out_fx[n_freq] = L_shr( L_tmp, 1 );
    3512       24784 :                 move32(); /*12 */
    3513       24784 :                 n_freq++;
    3514             :             }
    3515             : 
    3516        6196 :             FOR( n_band = 0; n_band < DIM_FB; n_band++ )
    3517             :             {
    3518        4647 :                 wfenv_fx = hq_generic_fenv_fx[n_band + nenv];
    3519        4647 :                 move16(); /*1 */
    3520        4647 :                 tmp2 = fb_bwe_subband[n_band + 1];
    3521      252487 :                 FOR( i = 0; n_freq < tmp2; i++ )
    3522             :                 {
    3523      247840 :                     L_tmp = Mult_32_16( coeff_out_fx[n_freq], wfenv_fx ); /*15 + 12 + 1 - 15 */
    3524      247840 :                     coeff_out_fx[n_freq] = L_shr( L_tmp, 1 );
    3525      247840 :                     move32(); /*12 */
    3526      247840 :                     n_freq++;
    3527             :                 }
    3528             :             }
    3529             :         }
    3530             :         ELSE
    3531             :         {
    3532        1884 :             FOR( n_band = 0; n_band < DIM_FB; n_band++ )
    3533             :             {
    3534        1413 :                 wfenv_fx = hq_generic_fenv_fx[n_band + nenv - 1];
    3535        1413 :                 move16(); /*1 */
    3536             : 
    3537       65469 :                 FOR( i = 0; n_freq < fb_bwe_sm_subband[n_band]; i++ )
    3538             :                 {
    3539       64056 :                     L_tmp1 = L_mult( sub( wfenv_fx, hq_generic_fenv_fx[n_band + nenv] ), fb_smooth_factor_fx[n_band] ); /*17 */
    3540       64056 :                     L_tmp1 = Mult_32_16( L_tmp1, shl( i, 9 ) );                                                         /*17 + 9 - 15 */
    3541       64056 :                     L_tmp1 = L_add( L_tmp1, L_shl( hq_generic_fenv_fx[n_band + nenv], 10 ) );                           /*11 */
    3542             : 
    3543       64056 :                     cs = norm_l( L_tmp1 );
    3544       64056 :                     tmp = extract_h( L_shl( L_tmp1, cs ) );          /*11 + cs - 16 */
    3545       64056 :                     L_tmp = Mult_32_16( coeff_out_fx[n_freq], tmp ); /*12 + 15 + 11 + cs - 16 - 15 */
    3546       64056 :                     coeff_out_fx[n_freq] = L_shl( L_tmp, sub( 5, cs ) );
    3547       64056 :                     move32(); /*12 */
    3548       64056 :                     n_freq = add( n_freq, 1 );
    3549             :                 }
    3550             :             }
    3551             : 
    3552         471 :             wfenv_fx = hq_generic_fenv_fx[tenv - 1];
    3553         471 :             move16(); /*1 */
    3554             : 
    3555       19311 :             FOR( ; n_freq < fb_bwe_subband[DIM_FB]; n_freq++ )
    3556             :             {
    3557       18840 :                 L_tmp = Mult_32_16( coeff_out_fx[n_freq], wfenv_fx ); /*15 + 12 + 1 - 15 */
    3558       18840 :                 coeff_out_fx[n_freq] = L_shr( L_tmp, 1 );
    3559       18840 :                 move32(); /*12 */
    3560             :             }
    3561             :         }
    3562             :     }
    3563             : 
    3564        2604 :     return;
    3565             : }
    3566             : 
    3567             : 
    3568             : /*-------------------------------------------------------------------*
    3569             :  * save_old_syn()
    3570             :  *
    3571             :  * Save and delay the ACELP core synthesis signal by
    3572             :  * DELAY_FD_BWE_ENC_xxkx to be used by SWB BWE
    3573             :  *-------------------------------------------------------------------*/
    3574             : 
    3575      329186 : void save_old_syn_fx(
    3576             :     const Word16 L_frame,     /* i  : frame length                */
    3577             :     const Word16 syn[],       /* i  : ACELP synthesis             */
    3578             :     Word16 old_syn[],         /* o  : old synthesis buffer        */
    3579             :     Word16 old_syn_mem[],     /* i/o: old synthesis buffer memory */
    3580             :     const Word16 preemph_fac, /* i  : preemphasis factor          */
    3581             :     Word16 *mem_deemph        /* i/o: deemphasis filter memory    */
    3582             : )
    3583             : {
    3584             :     Word16 tmps;
    3585             : 
    3586      329186 :     tmps = NS2SA( 16000, DELAY_FD_BWE_ENC_16k_NS );
    3587      329186 :     move16();
    3588      329186 :     if ( EQ_16( L_frame, L_FRAME ) )
    3589             :     {
    3590      172831 :         tmps = NS2SA( 12800, DELAY_FD_BWE_ENC_12k8_NS );
    3591      172831 :         move16();
    3592             :     }
    3593      329186 :     Copy( old_syn_mem, old_syn, tmps );
    3594      329186 :     Copy( syn, old_syn + tmps, sub( L_frame, tmps ) );
    3595      329186 :     Copy( syn + L_frame - tmps, old_syn_mem, tmps );
    3596             : 
    3597      329186 :     deemph_fx( old_syn, preemph_fac, L_frame, mem_deemph );
    3598             : 
    3599      329186 :     return;
    3600             : }

Generated by: LCOV version 1.14