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

Generated by: LCOV version 1.14