📄 mathhalf.c
字号:
* Put the 16 bit input into the 16 MSB's of the output Longword. The
* LS 16 bits are zeroed.
*
* INPUTS:
*
* var1
* 16 bit short signed integer (Shortword) whose value
* falls in the range 0xffff 8000 <= var1 <= 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 0000.
*
*
* KEYWORDS: deposit, assign, fractional assign
*
*************************************************************************/
Longword L_deposit_h(Shortword var1)
{
Longword L_var2;
L_var2 = (Longword) var1 << 16;
return (L_var2);
}
/***************************************************************************
*
* FUNCTION NAME: L_deposit_l
*
* PURPOSE:
*
* Put the 16 bit input into the 16 LSB's of the output Longword with
* sign extension i.e. the top 16 bits are set to either 0 or 0xffff.
*
* INPUTS:
*
* var1
* 16 bit short signed integer (Shortword) whose value
* falls in the range 0xffff 8000 <= var1 <= 0x0000 7fff.
*
* OUTPUTS:
*
* none
*
* RETURN VALUE:
*
* L_Out
* 32 bit long signed integer (Longword) whose value
* falls in the range
* 0xffff 8000 <= L_var1 <= 0x0000 7fff.
*
* KEYWORDS: deposit, assign
*
*************************************************************************/
Longword L_deposit_l(Shortword var1)
{
Longword L_Out;
L_Out = var1;
return (L_Out);
}
/***************************************************************************
*
* FUNCTION NAME: L_mac
*
* PURPOSE:
*
* Multiply accumulate. Fractionally multiply two 16 bit
* numbers together with saturation. Add 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 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 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)
{
return (L_add(L_var3, L_mult(var1, var2)));
}
/***************************************************************************
*
* FUNCTION NAME: L_msu
*
* PURPOSE:
*
* Multiply and subtract. Fractionally multiply two 16 bit
* numbers together with saturation. Subtract that result from
* 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 that result from 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 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)
{
return (L_sub(L_var3, L_mult(var1, var2)));
}
/***************************************************************************
*
* 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)
L_product = LW_MAX; /* overflow */
else
{
L_product = (Longword) var1 *var2; /* integer multiply */
L_product = L_product << 1;
}
return (L_product);
}
/***************************************************************************
*
* FUNCTION NAME: L_negate
*
* PURPOSE:
*
* Negate the 32 bit input. 0x8000 0000's negated value is
* 0x7fff ffff.
*
* 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:
*
* L_Out
* 32 bit long signed integer (Longword) whose value
* falls in the range
* 0x8000 0001 <= L_var1 <= 0x7fff ffff.
*
* KEYWORDS: negate, negative
*
*************************************************************************/
Longword L_negate(Longword L_var1)
{
Longword L_Out;
if (L_var1 == LW_MIN)
L_Out = LW_MAX;
else
L_Out = -L_var1;
return (L_Out);
}
/***************************************************************************
*
* 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, var2 + 1) & 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: 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;
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, -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)
{
/* saturate */
if (L_var1 > 0)
L_Out = LW_MAX;
else
L_Out = LW_MIN;
}
}
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 (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
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -