📄 math_adv.c
字号:
if (L_num < 0 || L_denom < 0 || L_num > L_denom)
{
printf("ERROR: Invalid input into L_divide!\n");
return (0);
}
/* First approximation: 1 / L_denom = 1/extract_h(L_denom) */
approx = divide_s((Word16) 0x3fff, extract_h(L_denom));
/* 1/L_denom = approx * (2.0 - L_denom * approx) */
L_div = L_mpy_ls(L_denom, approx);
L_div = L_sub((Word32) 0x7fffffffL, L_div);
L_div = L_mpy_ls(L_div, approx);
/* L_num * (1/L_denom) */
L_div = L_mpy_ll(L_num, L_div);
L_div = L_shl(L_div, 2);
return (L_div);
}
/***************************************************************************
*
* FUNCTION NAME: sqroot
*
* PURPOSE:
*
* The purpose of this function is to perform a single precision square
* root function on a Word32
*
* INPUTS:
*
* L_SqrtIn
* input to square root function
*
* OUTPUTS:
*
* none
*
* RETURN VALUE:
*
* swSqrtOut
* output to square root function
*
* DESCRIPTION:
*
* Input assumed to be normalized
*
* The algorithm is based around a six term Taylor expansion :
*
* y^0.5 = (1+x)^0.5
* ~= 1 + (x/2) - 0.5*((x/2)^2) + 0.5*((x/2)^3)
* - 0.625*((x/2)^4) + 0.875*((x/2)^5)
*
* Max error less than 0.08 % for normalized input ( 0.5 <= x < 1 )
*
*************************************************************************/
Word16 sqroot(Word32 L_SqrtIn)
{
/*_________________________________________________________________________
| |
| Local Constants |
|_________________________________________________________________________|
*/
#define PLUS_HALF 0x40000000L /* 0.5 */
#define MINUS_ONE 0x80000000L /* -1 */
#define TERM5_MULTIPLER 0x5000 /* 0.625 */
#define TERM6_MULTIPLER 0x7000 /* 0.875 */
/*_________________________________________________________________________
| |
| Automatic Variables |
|_________________________________________________________________________|
*/
Word32 L_Temp0, L_Temp1;
Word16 swTemp, swTemp2, swTemp3, swTemp4, swSqrtOut;
/*_________________________________________________________________________
| |
| Executable Code |
|_________________________________________________________________________|
*/
/* determine 2nd term x/2 = (y-1)/2 */
/* -------------------------------- */
L_Temp1 = L_shr(L_SqrtIn, 1); /* L_Temp1 = y/2 */
L_Temp1 = L_sub(L_Temp1, PLUS_HALF); /* L_Temp1 = (y-1)/2 */
swTemp = extract_h(L_Temp1); /* swTemp = x/2 */
/* add contribution of 2nd term */
/* ---------------------------- */
L_Temp1 = L_sub(L_Temp1, MINUS_ONE); /* L_Temp1 = 1 + x/2 */
/* determine 3rd term */
/* ------------------ */
L_Temp0 = L_msu(0L, swTemp, swTemp); /* L_Temp0 = -(x/2)^2 */
swTemp2 = extract_h(L_Temp0); /* swTemp2 = -(x/2)^2 */
L_Temp0 = L_shr(L_Temp0, 1); /* L_Temp0 = -0.5*(x/2)^2 */
/* add contribution of 3rd term */
/* ---------------------------- */
L_Temp0 = L_add(L_Temp1, L_Temp0); /* L_Temp0 = 1 + x/2 - 0.5*(x/2)^2 */
/* determine 4rd term */
/* ------------------ */
L_Temp1 = L_msu(0L, swTemp, swTemp2); /* L_Temp1 = (x/2)^3 */
swTemp3 = extract_h(L_Temp1); /* swTemp3 = (x/2)^3 */
L_Temp1 = L_shr(L_Temp1, 1); /* L_Temp1 = 0.5*(x/2)^3 */
/* add contribution of 4rd term */
/* ---------------------------- */
/* L_Temp1 = 1 + x/2 - 0.5*(x/2)^2 + 0.5*(x/2)^3 */
L_Temp1 = L_add(L_Temp0, L_Temp1);
/* determine partial 5th term */
/* -------------------------- */
L_Temp0 = L_mult(swTemp, swTemp3); /* L_Temp0 = (x/2)^4 */
swTemp4 = round32(L_Temp0); /* swTemp4 = (x/2)^4 */
/* determine partial 6th term */
/* -------------------------- */
L_Temp0 = L_msu(0L, swTemp2, swTemp3); /* L_Temp0 = (x/2)^5 */
swTemp2 = round32(L_Temp0); /* swTemp2 = (x/2)^5 */
/* determine 5th term and add its contribution */
/* ------------------------------------------- */
/* L_Temp0 = -0.625*(x/2)^4 */
L_Temp0 = L_msu(0L, TERM5_MULTIPLER, swTemp4);
/* L_Temp1 = 1 + x/2 - 0.5*(x/2)^2 + 0.5*(x/2)^3 - 0.625*(x/2)^4 */
L_Temp1 = L_add(L_Temp0, L_Temp1);
/* determine 6th term and add its contribution */
/* ------------------------------------------- */
/* swSqrtOut = 1 + x/2 - 0.5*(x/2)^2 + 0.5*(x/2)^3 */
/* - 0.625*(x/2)^4 + 0.875*(x/2)^5 */
swSqrtOut = mac_r(L_Temp1, TERM6_MULTIPLER, swTemp2);
/* return output */
/* ------------- */
return (swSqrtOut);
}
/***************************************************************************
*
* FUNCTION NAME: fnLog2
*
* PURPOSE:
* The purpose of this function is to take the log base 2 of input and
* divide by 32 and return; i.e. output = log2(input)/32
*
* INPUTS:
*
* L_Input
* input
*
* OUTPUTS:
*
* none
*
* RETURN VALUE:
*
* Word32
* output
*
* DESCRIPTION:
*
* log2(x) = 4.0 * (-.3372223*x*x + .9981958*x -.6626105)
* c0 c1 c2 (includes sign)
*
*************************************************************************/
Word32 fnLog2(Word32 L_Input)
{
static Word16
swC0 = -0x2b2a, swC1 = 0x7fc5, swC2 = -0x54d0;
Word16 siShiftCnt, swInSqrd, swIn;
Word32 LwIn;
/*_________________________________________________________________________
| |
| Executable Code |
|_________________________________________________________________________|
*/
/* normalize input and store shifts required */
/* ----------------------------------------- */
siShiftCnt = norm_l(L_Input);
LwIn = L_shl(L_Input, siShiftCnt);
siShiftCnt = add(siShiftCnt, 1);
siShiftCnt = negate(siShiftCnt);
/* calculate x*x*c0 */
/* ---------------- */
swIn = extract_h(LwIn);
swInSqrd = mult_r(swIn, swIn);
LwIn = L_mult(swInSqrd, swC0);
/* add x*c1 */
/* --------- */
LwIn = L_mac(LwIn, swIn, swC1);
/* add c2 */
/* ------ */
LwIn = L_add(LwIn, L_deposit_h(swC2));
/* apply *(4/32) */
/* ------------- */
LwIn = L_shr(LwIn, 3);
LwIn = LwIn & 0x03ffffff;
siShiftCnt = shl(siShiftCnt, 10);
LwIn = L_add(LwIn, L_deposit_h(siShiftCnt));
/* return log2 */
/* ----------- */
return (LwIn);
}
/***************************************************************************
*
* FUNCTION NAME: fnLog
*
* PURPOSE:
* The purpose of this function is to take the natural log of input and
* divide by 32 and return; i.e. output = log(input)/32
*
* INPUTS:
*
* L_Input
* input
*
* OUTPUTS:
*
* none
*
* RETURN VALUE:
*
* Word32
* output
*
* DESCRIPTION:
*
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -