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

📄 mathhalf.c

📁 MELPe 1200 bps, fixed point
💻 C
📖 第 1 页 / 共 5 页
字号:
 *	   If Arithmetically shift the input left by var2.	If var2 is
 *	   negative then an arithmetic shift right (shr) of var1 by
 *	   -var2 is performed.	See description of shr for details.
 *	   When an arithmetic shift left is performed the var2 LS bits
 *	   are zero filled.
 *
 *	   The only exception is if the left shift causes an overflow
 *	   or underflow.  In this case the LS bits are not modified.
 *	   The number returned is 0x8000 in the case of an underflow or
 *	   0x7fff in the case of an overflow.
 *
 *	   The shl is equivalent to the Full-Rate GSM "<< n" operation.
 *	   Note that ANSI-C does not guarantee operation of the C ">>"
 *	   or "<<" operator for negative numbers - it is not specified
 *	   whether this shift is an arithmetic or logical shift.
 *
 *	 KEYWORDS: asl, arithmetic shift left, shift
 *
 *************************************************************************/

Shortword shl(Shortword var1, Shortword var2)
{
	Shortword	swOut;
	Longword	L_Out;

	if (var2 == 0 || var1 == 0){
		swOut = var1;
	} else if (var2 < 0){                            /* perform a right shift */
		if (var2 <= -15)
			swOut = (Shortword) ((var1 < 0) ? -1 : 0);
		else
			swOut = shr(var1, negate(var2));

	} else {                                                      /* var2 > 0 */
		if (var2 >= 15){
			swOut = (Shortword) ((var1 > 0) ? SW_MAX : SW_MIN);
                                                                  /* saturate */
			inc_saturation();
		} else {
			L_Out = (Longword) var1 * (1 << var2);
			swOut = (Shortword) L_Out;           /* copy low portion to swOut */
                                              /* overflow could have happened */
			if (swOut != L_Out){                                /* overflowed */
				swOut = (Shortword) ((var1 > 0) ? SW_MAX : SW_MIN);
                                                                  /* saturate */
				inc_saturation();
			}
		}
	}
	return (swOut);
}


/***************************************************************************
 *
 *	 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 (Shortword) whose value
 *					   falls in the range 0xffff 8000 <= var2 <= 0x0000 7fff.
 *	   L_var1
 *					   32 bit long signed integer (Longword) whose value
 *					   falls in the range
 *					   0x8000 0000 <= L_var1 <= 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:
 *
 *	   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,
 *
 *************************************************************************/

Longword L_shr(Longword L_var1, Shortword var2)
{
	Longword	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){
			L_Out = (L_var1 > 0) ? LW_MAX : LW_MIN;               /* saturate */
			inc_saturation();
		} else
			L_Out = L_shl(L_var1, (Shortword) -var2);
	} else {
		if (var2 >= 31)
			L_Out = (L_var1 > 0) ? 0 : 0xffffffffL;
		else {
			L_Mask = 0;
			if (L_var1 < 0)
				L_Mask = ~L_Mask << (32 - var2);

			L_var1 >>= var2;
			L_Out = L_Mask | L_var1;
		}
	}
	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 (Shortword) whose value
 *					   falls in the range 0xffff 8000 <= var2 <= 0x0000 7fff.
 *	   L_var1
 *					   32 bit long signed integer (Longword) whose value
 *					   falls in the range
 *					   0x8000 0000 <= L_var1 <= 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:
 *
 *	   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,
 *
 *************************************************************************/

Longword L_shl(Longword L_var1, Shortword var2)
{
	Longword	L_Mask, L_Out = 0;
	int		i, iOverflow = 0;

	if (var2 == 0 || L_var1 == 0)
		L_Out = L_var1;
	else if (var2 < 0) {
		if (var2 <= -31)
			L_Out = (L_var1 > 0) ? 0 : 0xffffffffL;
		else
			L_Out = L_shr(L_var1, (Shortword) -var2);
	} 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){
			L_Out = (L_var1 > 0) ? LW_MAX : LW_MIN;               /* saturate */
			inc_saturation();
		}
	}

	return (L_Out);
}


/***************************************************************************
 *
 *	 FUNCTION NAME: shift_r
 *
 *	 PURPOSE:
 *
 *	   Shift and round.  Perform a shift right. After shifting, use
 *	   the last bit shifted out of the LSB to round the result up
 *	   or down.
 *
 *	 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:
 *
 *	   Shift and round.  Perform a shift right. After shifting, use
 *	   the last bit shifted out of the LSB to round the result up
 *	   or down.
 *
 *	   If var2 is positive perform a arithmetic left shift
 *	   with saturation (see shl() above).
 *
 *	   If var2 is zero simply return var1.
 *
 *	   If var2 is negative perform a arithmetic right shift (shr)
 *	   of var1 by (-var2)+1.  Add the LS bit of the result to var1
 *	   shifted right (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 shl function is used.
 *
 *
 *	 KEYWORDS:
 *
 *************************************************************************/

Shortword shift_r(Shortword var1, Shortword var2)
{
	Shortword	swOut, swRnd;

	if (var2 >= 0)
		swOut = shl(var1, var2);
	else {                                                     /* right shift */
		if (var2 < -15)
			swOut = 0;
		else {
			swRnd = (Shortword) (shl(var1, (Shortword) (var2 + 1)) &
								 (Shortword) 0x1);
			swOut = add(shl(var1, var2), swRnd);
		}
	}
	return(swOut);
}


/***************************************************************************
 *
 *	 FUNCTION NAME: L_shift_r
 *
 *	 PURPOSE:
 *
 *	   Shift and round.  Perform a shift right. After shifting, use
 *	   the last bit shifted out of the LSB to round the result up
 *	   or down.
 *
 *	 INPUTS:
 *
 *	   L_var1
 *					   32 bit long signed integer (Longword) whose value
 *					   falls in the range
 *					   0x8000 0000 <= L_var1 <= 0x7fff ffff.
 *	   var2
 *					   16 bit short signed integer (Shortword) whose value
 *					   falls in the range 0xffff 8000 <= var2 <= 0x0000 7fff.
 *
 *	 OUTPUTS:
 *
 *	   none
 *
 *	 RETURN VALUE:
 *
 *	   L_var1
 *					   32 bit long signed integer (Longword) whose value
 *					   falls in the range
 *					   0x8000 0000 <= L_var1 <= 0x7fff ffff.
 *
 *
 *	 IMPLEMENTATION:
 *
 *	   Shift and round.  Perform a shift right. After shifting, use
 *	   the last bit shifted out of the LSB to round the result up
 *	   or down.  This is just like shift_r above except that the
 *	   input/output is 32 bits as opposed to 16.
 *
 *	   if var2 is positve perform a arithmetic left shift
 *	   with saturation (see L_shl() above).
 *
 *	   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:
 *
 *************************************************************************/

Longword L_shift_r(Longword L_var1, Shortword var2)
{
	Longword	L_Out, L_rnd;

	if (var2 < -31)
		L_Out = 0;
	else if (var2 < 0){                                        /* right shift */
		L_rnd = L_shl(L_var1, (Shortword) (var2 + 1)) & (Longword) 0x1;
		L_Out = L_add(L_shl(L_var1, var2), L_rnd);
	} else
		L_Out = L_shl(L_var1, var2);

	return(L_Out);
}


/***************************************************************************
 *
 *	 FUNCTION NAME: norm_l
 *
 *	 PURPOSE:
 *
 *	   Get normalize shift count:
 *
 *	   A 32 bit number is input (possiblly unnormalized).  Output
 *	   the positive (or zero) shift count required to normalize the
 *	   input.
 *
 *	 INPUTS:
 *
 *	   L_var1
 *					   32 bit long signed integer (Longword) whose value
 *					   falls in the range
 *					   0x8000 0000 <= L_var1 <= 0x7fff ffff.
 *
 *	 OUTPUTS:
 *
 *	   none
 *
 *	 RETURN VALUE:
 *
 *	   swOut
 *					   16 bit short signed integer (Shortword) whose value
 *					   falls in the range
 *					   0 <= swOut <= 31
 *
 *
 *
 *	 IMPLEMENTATION:
 *
 *	   Get normalize shift count:
 *
 *	   A 32 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 0000 >= normlzd number >= 0x7fff ffff (positive number)
 *	   or
 *	   0x8000 0000 <= normlzd number < 0xc000 0000 (negative number)
 *
 *	   Return the number of shifts.
 *
 *	   This instruction corresponds exactly to the Full-Rate "norm"
 *	   instruction.
 *
 *	 KEYWORDS: norm, normalization
 *
 *************************************************************************/

Shortword norm_l(Longword L_var1)
{
	Shortword	swShiftCnt;

	if (L_var1 != 0){

⌨️ 快捷键说明

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