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

📄 mathhalf.c

📁 MELPe 1200 bps, fixed point
💻 C
📖 第 1 页 / 共 5 页
字号:
		if (!(L_var1 & LW_SIGN)){                           /* positive input */
			for (swShiftCnt = 0; !(L_var1 <= LW_MAX && L_var1 >= 0x40000000L);
				 swShiftCnt++){
				L_var1 = L_var1 << 1;
			}
		} else {                                            /* negative input */
			for (swShiftCnt = 0;
				 !(L_var1 >= LW_MIN && L_var1 < (Longword) 0xc0000000L);
				 swShiftCnt++){
				L_var1 = L_var1 << 1;
			}
		}
	} else
		swShiftCnt = 0;
	return(swShiftCnt);
}


/***************************************************************************
 *
 *	 FUNCTION NAME: norm_s
 *
 *	 PURPOSE:
 *
 *	   Get normalize shift count:
 *
 *	   A 16 bit number is input (possiblly unnormalized).  Output
 *	   the positive (or zero) shift count required to normalize the
 *	   input.
 *
 *	 INPUTS:
 *
 *	   var1
 *					   16 bit short signed integer (Shortword) whose value
 *					   falls in the range 0xffff 8000 <= var1 <= 0x0000 7fff.
 *
 *	 OUTPUTS:
 *
 *	   none
 *
 *	 RETURN VALUE:
 *	   swOut
 *					   16 bit short signed integer (Shortword) whose value
 *					   falls in the range
 *					   0 <= swOut <= 15
 *
 *
 *
 *	 IMPLEMENTATION:
 *
 *	   Get normalize shift count:
 *
 *	   A 16 bit number is input (possiblly unnormalized).  Output
 *	   the positive (or zero) shift count required to normalize the
 *	   input.
 *
 *	   If zero in input, return 0 as the shift count.
 *
 *	   For non-zero numbers, count the number of left shift
 *	   required to get the number to fall into the range:
 *
 *	   0x4000 >= normlzd number >= 0x7fff (positive number)
 *	   or
 *	   0x8000 <= normlzd number <  0xc000 (negative number)
 *
 *	   Return the number of shifts.
 *
 *	   This instruction corresponds exactly to the Full-Rate "norm"
 *	   instruction.
 *
 *	 KEYWORDS: norm, normalization
 *
 *************************************************************************/

Shortword norm_s(Shortword var1)
{
	short	swShiftCnt;
	Longword	L_var1;

	L_var1 = L_deposit_h(var1);
	swShiftCnt = norm_l(L_var1);
	return(swShiftCnt);
}


/***************************************************************************
 *
 *	 FUNCTION NAME: L_mult
 *
 *	 PURPOSE:
 *
 *	   Perform a fractional multipy of the two 16 bit input numbers
 *	   with saturation.  Output a 32 bit number.
 *
 *	 INPUTS:
 *
 *	   var1
 *					   16 bit short signed integer (Shortword) whose value
 *					   falls in the range 0xffff 8000 <= var1 <= 0x0000 7fff.
 *	   var2
 *					   16 bit short signed integer (Shortword) whose value
 *					   falls in the range 0xffff 8000 <= var2 <= 0x0000 7fff.
 *
 *	 OUTPUTS:
 *
 *	   none
 *
 *	 RETURN VALUE:
 *
 *	   L_Out
 *					   32 bit long signed integer (Longword) whose value
 *					   falls in the range
 *					   0x8000 0000 <= L_var1 <= 0x7fff ffff.
 *
 *	 IMPLEMENTATION:
 *
 *	   Multiply the two the two 16 bit input numbers. If the
 *	   result is within this range, left shift the result by one
 *	   and output the 32 bit number.  The only possible overflow
 *	   occurs when var1==var2==-0x8000.  In this case output
 *	   0x7fff ffff.
 *
 *	 KEYWORDS: multiply, mult, mpy
 *
 *************************************************************************/

Longword L_mult(Shortword var1, Shortword var2)
{
	Longword	L_product;

	if (var1 == SW_MIN && var2 == SW_MIN){                        /* overflow */
		inc_saturation();
		L_product = LW_MAX;
	} else {
		L_product = (Longword) var1 *var2;                /* integer multiply */
		L_product <<= 1;
	}
	return(L_product);
}


/***************************************************************************
 *
 *	 FUNCTION NAME: mult
 *
 *	 PURPOSE:
 *
 *	   Perform a fractional multipy of the two 16 bit input numbers
 *	   with saturation and truncation.
 *
 *	 INPUTS:
 *
 *	   var1
 *					   16 bit short signed integer (Shortword) whose value
 *					   falls in the range 0xffff 8000 <= var1 <= 0x0000 7fff.
 *	   var2
 *					   16 bit short signed integer (Shortword) whose value
 *					   falls in the range 0xffff 8000 <= var2 <= 0x0000 7fff.
 *
 *	 OUTPUTS:
 *
 *	   none
 *
 *	 RETURN VALUE:
 *
 *	   swOut
 *					   16 bit short signed integer (Shortword) whose value
 *					   falls in the range
 *					   0xffff 8000 <= swOut <= 0x0000 7fff.
 *
 *	 IMPLEMENTATION:
 *
 *	   Perform a fractional multipy of the two 16 bit input
 *	   numbers.  If var1 == var2 == -0x8000, output 0x7fff.
 *	   Otherwise output var1*var2 >> 15.  The output is a
 *	   16 bit number.
 *
 *	 KEYWORDS: mult, mulitply, mpy
 *
 *************************************************************************/

Shortword mult(Shortword var1, Shortword var2)
{
	Longword	L_product;
	Shortword	swOut;

	L_product = L_mult(var1, var2);
	swOut = extract_h(L_product);
	return(swOut);
}


/***************************************************************************
 *
 *	 FUNCTION NAME: L_mac
 *
 *	 PURPOSE:
 *
 *	   Multiply accumulate.  Fractionally multiply two 16 bit
 *	   numbers together with saturation.  Add to that result to the
 *	   32 bit input with saturation.  Return the 32 bit result.
 *
 *	 INPUTS:
 *
 *	   var1
 *					   16 bit short signed integer (Shortword) whose value
 *					   falls in the range 0xffff 8000 <= var1 <= 0x0000 7fff.
 *	   var2
 *					   16 bit short signed integer (Shortword) whose value
 *					   falls in the range 0xffff 8000 <= var2 <= 0x0000 7fff.
 *	   L_var3
 *					   32 bit long signed integer (Longword) whose value
 *					   falls in the range
 *					   0x8000 0000 <= L_var2 <= 0x7fff ffff.
 *
 *	 OUTPUTS:
 *
 *	   none
 *
 *	 RETURN VALUE:
 *
 *	   L_Out
 *					   32 bit long signed integer (Longword) whose value
 *					   falls in the range
 *					   0x8000 0000 <= L_var1 <= 0x7fff ffff.
 *
 *	 IMPLEMENTATION:
 *
 *	   Fractionally multiply two 16 bit numbers together with
 *	   saturation.	The only numbers which will cause saturation on
 *	   the multiply are 0x8000 * 0x8000.
 *
 *	   Add to that result to the 32 bit input with saturation.
 *	   Return the 32 bit result.
 *
 *	   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 an most
 *	   processors this would cause an overflow only if the 32 bit
 *	   input added to it were positive or zero.
 *
 *	 KEYWORDS: mac, multiply accumulate
 *
 *************************************************************************/

Longword L_mac(Longword L_var3, Shortword var1, Shortword var2)
{
	Longword	L_Out;

	L_Out = L_add(L_var3, L_mult(var1, var2));
	return(L_Out);
}


/***************************************************************************
 *
 *	 FUNCTION NAME: L_msu
 *
 *	 PURPOSE:
 *
 *	   Multiply and subtract.  Fractionally multiply two 16 bit
 *	   numbers together with saturation.  Subtract from that result the
 *	   32 bit input with saturation.  Return the 32 bit result.
 *
 *	 INPUTS:
 *
 *	   var1
 *					   16 bit short signed integer (Shortword) whose value
 *					   falls in the range 0xffff 8000 <= var1 <= 0x0000 7fff.
 *	   var2
 *					   16 bit short signed integer (Shortword) whose value
 *					   falls in the range 0xffff 8000 <= var2 <= 0x0000 7fff.
 *	   L_var3
 *					   32 bit long signed integer (Longword) whose value
 *					   falls in the range
 *					   0x8000 0000 <= L_var2 <= 0x7fff ffff.
 *
 *	 OUTPUTS:
 *
 *	   none
 *
 *	 RETURN VALUE:
 *
 *	   L_Out
 *					   32 bit long signed integer (Longword) whose value
 *					   falls in the range
 *					   0x8000 0000 <= L_var1 <= 0x7fff ffff.
 *
 *	 IMPLEMENTATION:
 *
 *	   Fractionally multiply two 16 bit numbers together with
 *	   saturation.	The only numbers which will cause saturation on
 *	   the multiply are 0x8000 * 0x8000.
 *
 *	   Subtract from that result to the 32 bit input with saturation.
 *	   Return the 32 bit result.
 *
 *	   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 an most
 *	   processors this would cause an overflow only if the 32 bit
 *	   input added to it were negative or zero.
 *
 *	 KEYWORDS: mac, multiply accumulate, msu
 *
 *************************************************************************/

Longword L_msu(Longword L_var3, Shortword var1, Shortword var2)
{
	Longword	L_Out;

	L_Out = L_sub(L_var3, L_mult(var1, var2));
	return(L_Out);
}


/***************************************************************************
 *
 *	 FUNCTION NAME:  msu_r
 *
 *	 PURPOSE:
 *
 *	   Multiply subtract and round.  Fractionally multiply two 16
 *	   bit numbers together with saturation.  Subtract from that result
 *	   the 32 bit input with saturation.  Finally round the result
 *	   into a 16 bit number.
 *
 *
 *	 INPUTS:
 *
 *	   var1
 *					   16 bit short signed integer (Shortword) whose value
 *					   falls in the range 0xffff 8000 <= var1 <= 0x0000 7fff.
 *	   var2
 *					   16 bit short signed integer (Shortword) whose value
 *					   falls in the range 0xffff 8000 <= var2 <= 0x0000 7fff.
 *	   L_var3
 *					   32 bit long signed integer (Longword) whose value
 *					   falls in the range
 *					   0x8000 0000 <= L_var2 <= 0x7fff ffff.
 *
 *	 OUTPUTS:
 *
 *	   none
 *
 *	 RETURN VALUE:
 *
 *	   swOut
 *					   16 bit short signed integer (Shortword) 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.
 *
 *	   Subtract from 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 Shortword.
 *
 *	   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 an 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
 *
 *************************************************************************/

Shortword msu_r(Longword L_var3, Shortword var1, Shortword var2)
{
	Shortword	swOut;

	swOut = round(L_sub(L_var3, L_mult(var1, var2)));
	return (swOut);
}


/***************************************************************************
 *
 *	 FUNCTION NAME: abs_s
 *
 *	 PURPOSE:
 *
 *	   Take the absolute value of the 16 bit input.  An input of
 *	   -0x8000 results in a return value of 0x7fff.
 *
 *	 INPUTS:
 *
 *	   var1
 *					   16 bit short signed integer (Shortword) whose value
 *					   falls in the range 0xffff 8000 <= var1 <= 0x0000 7fff.
 *
 *	 OUTPUTS:
 *
 *	   none
 *
 *	 RETURN VALUE:
 *
 *	   swOut
 *					   16 bit short signed integer (Shortword) whose value
 *					   falls in the range
 *					   0x0000 0000 <= swOut <= 0x0000 7fff.
 *
 *	 IMPLEMENTATION:
 *
 *	   Take the absolute value of the 16 bit input.  An input of
 *	   -0x8000 results in a return value of 0x7fff.
 *
 *	 KEYWORDS: absolute value, abs
 *
 *************************************************************************/

Shortword abs_s(Shortword var1)
{
	Shortword	swOut;

	if (var1 == SW_MIN)
		swOut = SW_MAX;
	else {

⌨️ 快捷键说明

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