⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 basic_op.c

📁 EVRC是用于高通公司的语音编码
💻 C
📖 第 1 页 / 共 5 页
字号:
 *
 *     If var2 is zero simply return L_var1.
 *
 *     If var2 is negative perform a arithmetic right shift (L_shr)
 *     of L_var1 by (-var2)+1.  Add the LS bit of the result to
 *     L_var1 shifted right (L_shr) by -var2.
 *
 *     Note that there is no constraint on var2, so if var2 is
 *     -0xffff 8000 then -var2 is 0x0000 8000, not 0x0000 7fff.
 *     This is the reason the L_shl function is used.
 *
 *
 *   KEYWORDS:
 *
 *************************************************************************/

Word32 L_shift_r(Word32 L_var1, Word16 var2)
        {
	  Word32 L_Out, L_rnd;
	  
	  
	  if (var2 < -31)
	    {
	      L_Out = 0;
	    }
	  else if (var2 < 0)
	    {
	      /* right shift */
	      L_rnd = L_shl(L_var1, (Word16)(var2 + 1)) & 0x1; 
	      L_Out = L_add(L_shl(L_var1, var2), L_rnd);  
#ifdef WMOPS_FX
	      counter_fx.L_shl-=2;
	      counter_fx.L_add--;
#endif
	    }
	  else
	    {
	      L_Out = L_shl(L_var1, var2);         
#ifdef WMOPS_FX
	      counter_fx.L_shl--;
#endif
	    }
	  
#ifdef WMOPS_FX
	  counter_fx.L_shift_r++;
#endif
	  
	  return (L_Out);
	}

/***************************************************************************
 *
 *   FUNCTION NAME: L_shl
 *
 *   PURPOSE:
 *
 *     Arithmetic shift left (or right).
 *     Arithmetically shift the input left by var2.   If var2 is
 *     negative then an arithmetic shift right (L_shr) of L_var1 by
 *     -var2 is performed.
 *
 *   INPUTS:
 *
 *     var2
 *                     16 bit short signed integer (Word16) whose value
 *                     falls in the range 0xffff 8000 <= var2 <= 0x0000 7fff.
 *     L_var1
 *                     32 bit long signed integer (Word32) whose value
 *                     falls in the range
 *                     0x8000 0000 <= L_var1 <= 0x7fff ffff.
 *   OUTPUTS:
 *
 *     none
 *
 *   RETURN VALUE:
 *
 *     L_Out
 *                     32 bit long signed integer (Word32) whose value
 *                     falls in the range
 *                     0x8000 0000 <= L_var1 <= 0x7fff ffff.
 *
 *
 *   IMPLEMENTATION:
 *
 *     Arithmetically shift the 32 bit input left by var2.  This
 *     operation maintains the sign of the input number. If var2 is
 *     negative then an arithmetic shift right (L_shr) of L_var1 by
 *     -var2 is performed.  See description of L_shr for details.
 *
 *     Equivalent to the Full-Rate GSM ">> n" operation.  Note that
 *     ANSI-C does not guarantee operation of the C ">>" or "<<"
 *     operator for negative numbers.
 *
 *   KEYWORDS: shift, arithmetic shift left,
 *
 *************************************************************************/

Word32 L_shl(Word32 L_var1, Word16 var2)
        {

	  Word32 L_Mask, L_Out;
	  int i, iOverflow = 0;
	  
	  
	  if (var2 == 0 || L_var1 == 0)
	    {
	      L_Out = L_var1;
	    }
	  else if (var2 < 0)
	    {
	      if (var2 <= -31)
		{
		  if (L_var1 > 0)
		    L_Out = 0;
		  else
		    L_Out = 0xffffffffL;
		}
	      else
		{
		  L_Out = L_shr(L_var1, (Word16)(-var2));  
#ifdef WMOPS_FX
		  counter_fx.L_shr--;
#endif
		}
	    }
	  else
	    {
	      if (var2 >= 31)
		iOverflow = 1;
	      else
		{
		  if (L_var1 < 0)
		    L_Mask = LW_SIGN;	/* sign bit mask */
		  else
		    L_Mask = 0x0;
		  L_Out = L_var1;
		  for (i = 0; i < var2 && !iOverflow; i++)
		    {
		      /* check the sign bit */
		      L_Out = (L_Out & 0x7fffffffL) << 1;
		      if ((L_Mask ^ L_Out) & LW_SIGN)
			iOverflow = 1;
		    }
		}
	      
	      if (iOverflow)
		{
		  /* saturate */
		  if (L_var1 > 0)
		    L_Out = LW_MAX;
		  else
		    L_Out = LW_MIN;
		  
		  giOverflow = 1;
		}
	    }
	  
#ifdef WMOPS_FX
	  counter_fx.L_shl++;
#endif
	  
	  return (L_Out);
	}

/***************************************************************************
 *
 *   FUNCTION NAME: L_shr
 *
 *   PURPOSE:
 *
 *     Arithmetic shift right (or left).
 *     Arithmetically shift the input right by var2.   If var2 is
 *     negative then an arithmetic shift left (shl) of var1 by
 *     -var2 is performed.
 *
 *   INPUTS:
 *
 *     var2
 *                     16 bit short signed integer (Word16) whose value
 *                     falls in the range 0xffff 8000 <= var2 <= 0x0000 7fff.
 *     L_var1
 *                     32 bit long signed integer (Word32) whose value
 *                     falls in the range
 *                     0x8000 0000 <= L_var1 <= 0x7fff ffff.
 *   OUTPUTS:
 *
 *     none
 *
 *   RETURN VALUE:
 *
 *     L_Out
 *                     32 bit long signed integer (Word32) whose value
 *                     falls in the range
 *                     0x8000 0000 <= L_var1 <= 0x7fff ffff.
 *
 *
 *   IMPLEMENTATION:
 *
 *     Arithmetically shift the input right by var2.  This
 *     operation maintains the sign of the input number. If var2 is
 *     negative then an arithmetic shift left (shl) of L_var1 by
 *     -var2 is performed.  See description of L_shl for details.
 *
 *     The input is a 32 bit number, as is the output.
 *
 *     Equivalent to the Full-Rate GSM ">> n" operation.  Note that
 *     ANSI-C does not guarantee operation of the C ">>" or "<<"
 *     operator for negative numbers.
 *
 *   KEYWORDS: shift, arithmetic shift right,
 *
 *************************************************************************/

Word32 L_shr(Word32 L_var1, Word16 var2)
        {

	  Word32 L_Mask, L_Out;
	  
	  if (var2 == 0 || L_var1 == 0)
	    {
	      L_Out = L_var1;
	    }
	  else if (var2 < 0)
	    {
	      /* perform a left shift */
	      /*----------------------*/
	      if (var2 <= -31)
		{
		  /* saturate */
		  if (L_var1 > 0)
		    {
		      L_Out = LW_MAX;
		      giOverflow = 1;
		    }
		  else
		    {
		      L_Out = LW_MIN;
		      giOverflow = 1;
		    }
		}
	      else
		{
		  L_Out = L_shl(L_var1, (Word16)(-var2));  //  OP_COUNT(-2);   
#ifdef WMOPS_FX
		  counter_fx.L_shl--;
#endif
		}
	    }
	  else
	    {
	      
	      if (var2 >= 31)
		{
		  if (L_var1 > 0)
		    L_Out = 0;
		  else
		    L_Out = 0xffffffffL;
		}
	      else
		{
		  L_Mask = 0;
		  if (L_var1 < 0)
		    {
		      L_Mask = ~L_Mask << (32 - var2);
		    }
		  L_var1 >>= var2;
		  L_Out = L_Mask | L_var1;
		}
	    }
	  
#ifdef WMOPS_FX
	  counter_fx.L_shr++;
#endif
	  
	  return (L_Out);
	}

/***************************************************************************
 *
 *   FUNCTION NAME: L_sub
 *
 *   PURPOSE:
 *
 *     Perform the subtraction of the two 32 bit input variables with
 *     saturation.
 *
 *   INPUTS:
 *
 *     L_var1
 *                     32 bit long signed integer (Word32) whose value
 *                     falls in the range
 *                     0x8000 0000 <= L_var1 <= 0x7fff ffff.
 *     L_var2
 *                     32 bit long signed integer (Word32) whose value
 *                     falls in the range
 *                     0x8000 0000 <= L_var2 <= 0x7fff ffff.
 *
 *   OUTPUTS:
 *
 *     none
 *
 *   RETURN VALUE:
 *
 *     L_Out
 *                     32 bit long signed integer (Word32) whose value
 *                     falls in the range
 *                     0x8000 0000 <= L_var1 <= 0x7fff ffff.
 *
 *   IMPLEMENTATION:
 *
 *     Perform the subtraction of the two 32 bit input variables with
 *     saturation.
 *
 *     L_Out = L_var1 - L_var2
 *
 *     L_Out is set to 0x7fff ffff if the operation results in an
 *     overflow.  L_Out is set to 0x8000 0000 if the operation
 *     results in an underflow.
 *
 *   KEYWORDS: sub, subtraction
 *
 *************************************************************************/

Word32 L_sub(Word32 L_var1, Word32 L_var2)
        {
	  Word32 L_Sum;
	  double dSum;
	  
	  dSum = (double) L_var1 - (double) L_var2;
	  L_Sum = L_var1 - L_var2;
	  
	  if (dSum != L_Sum)
	    {
	      /* overflow occurred */
	      L_Sum = L_saturate(dSum);
#ifdef WMOPS_FX
	      counter_fx.L_saturate--;
#endif
	    }
	  
#ifdef WMOPS_FX
	  counter_fx.L_sub++;
#endif
	  
	  return (L_Sum);
	}

/***************************************************************************
 *
 *   FUNCTION NAME:mac_r
 *
 *   PURPOSE:
 *
 *     Multiply accumulate and round.  Fractionally multiply two 16
 *     bit numbers together with saturation.  Add that result to
 *     the 32 bit input with saturation.  Finally round the result
 *     into a 16 bit number.
 *
 *
 *   INPUTS:
 *
 *     var1
 *                     16 bit short signed integer (Word16) whose value
 *                     falls in the range 0xffff 8000 <= var1 <= 0x0000 7fff.
 *     var2
 *                     16 bit short signed integer (Word16) whose value
 *                     falls in the range 0xffff 8000 <= var2 <= 0x0000 7fff.
 *     L_var3
 *                     32 bit long signed integer (Word32) whose value
 *                     falls in the range
 *                     0x8000 0000 <= L_var2 <= 0x7fff ffff.
 *
 *   OUTPUTS:
 *
 *     none
 *
 *   RETURN VALUE:
 *
 *     swOut
 *                     16 bit short signed integer (Word16) whose value
 *                     falls in the range
 *                     0xffff 8000 <= swOut <= 0x0000 7fff.
 *
 *   IMPLEMENTATION:
 *
 *     Fractionally multiply two 16 bit numbers together with
 *     saturation.  The only numbers which will cause saturation on
 *     the multiply are 0x8000 * 0x8000.
 *
 *     Add that result to the 32 bit input with saturation.
 *     Round the 32 bit result by adding 0x0000 8000 to the input.
 *     The result may overflow due to the add.  If so, the result
 *     is saturated.  The 32 bit rounded number is then shifted
 *     down 16 bits and returned as a Word16.
 *
 *     Please note that this is not a true multiply accumulate as
 *     most processors would implement it.  The 0x8000*0x8000
 *     causes and overflow for this instruction.  On most
 *     processors this would cause an overflow only if the 32 bit
 *     input added to it were positive or zero.
 *
 *   KEYWORDS: mac, multiply accumulate, macr
 *
 *************************************************************************/

Word16 mac_r(Word32 L_var3, Word16 var1, Word16 var2)
        {
	  return (round32(L_mac(L_var3, var1, var2)));
	}

/***************************************************************************
 *
 *   FUNCTION NAME:  msu_r
 *
 *   PURPOSE:
 *
 *     Multiply subtract and round.  Fractionally multiply two 16
 *     bit numbers together with saturation.  Subtract that result from
 *     the 32 bit input with saturation.  Finally round the result
 *     into a 16 bit number.
 *
 *
 *   INPUTS:
 *
 *     var1
 *                     16 bit short signed integer (Word16) whose value
 *                     falls in the range 0xffff 8000 <= var1 <= 0x0000 7fff.
 *     var2
 *                     16 bit short signed integer (Word16) whose value
 *                     falls in the range 0xffff 8000 <= var2 <= 0x0000 7fff.
 *     L_var3
 *                     32 bit long signed integer (Word32) whose value

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -