📄 math.h
字号:
#include "sl1defs.h"#include "dsptyp.h"#include "sl1_basic_op.h"/* 2's complement signed fractional division routine */_INLINE _STATIC void SL1_DIV_31_Int (SignedInt32 *r, Signed_1b31 *resdual,Signed_1b31 dividend, Signed_1b31 divisor){ int negative_quotient; SignedInt32 result; int ii; Signed_1b31 tmp; Signed_1b15 shiftnum; Signed_1b31 dividend_tmp; dividend_tmp=dividend; if (divisor == 0 || dividend == 0) { *r = 0; return; } negative_quotient = (dividend&0x80000000) ^ (divisor&0x80000000); /* note that if the value being negated is MIN_32, the result is * still negative if treated as Signed. So be careful to cast to * an unsigned value where it matters */ if (dividend < 0) { dividend = -dividend; } if (divisor < 0) { divisor = -divisor; } if ((UnSigned_1b31) dividend<= (UnSigned_1b31) divisor) { *r = negative_quotient ? MIN_32 : MAX_32; *resdual = dividend ; return; } shiftnum = _sl1_lead_mmode(divisor, L_CNT_ZERO) -_sl1_lead_mmode(dividend, L_CNT_ZERO); divisor = divisor<<shiftnum; result = 0; for (ii = 0; ii < shiftnum; ++ii) { if ( (UnSigned_1b31) dividend >= (UnSigned_1b31) divisor) { dividend -= divisor; ++ result; } dividend <<= 1; result <<= 1; } if ( (UnSigned_1b31) dividend >= (UnSigned_1b31) divisor) { dividend -= divisor; ++ result; } *resdual = dividend >> shiftnum; if (negative_quotient) { result = -result; } if (dividend_tmp<0) { *resdual=-(*resdual); } *r = result;} _INLINE _STATIC voidSL1_DIV_31 (Signed_1b31 *r, Signed_1b31 dividend, Signed_1b31 divisor){ int negative_quotient; Signed_1b31 result; int ii; Signed_1b31 tmp; Signed_1b15 shiftnum; if (divisor == 0 || dividend == 0) { *r = 0; return; } negative_quotient = (dividend&0x80000000) ^ (divisor&0x80000000); /* note that if the value being negated is MIN_32, the result is * still negative if treated as Signed. So be careful to cast to * an unsigned value where it matters */ if (dividend < 0) { dividend = -dividend; } if (divisor < 0) { divisor = -divisor; } if ((UnSigned_1b31) divisor <= (UnSigned_1b31) dividend) { *r = negative_quotient ? MIN_32 : MAX_32; return; } shiftnum = _sl1_lead_mmode(dividend, L_CNT_ZERO) - _sl1_lead_mmode(divisor, L_CNT_ZERO) ; dividend = dividend<<shiftnum; result = 0; for (ii = 0; ii < 31-shiftnum; ++ii) { if ( (UnSigned_1b31) dividend >= (UnSigned_1b31) divisor) { dividend -= divisor; ++ result; } dividend <<= 1; result <<= 1; } if ( (UnSigned_1b31) dividend >= (UnSigned_1b31) divisor) { ++ result; } if (negative_quotient) { result = -result; /* but inverting the sign of the result means we've rounded upwards */ /* so if result is not exact, round it back down */ if (dividend != 0) { /* subtract one from LSB to round down */ -- result; } } *r = result;} _INLINE _STATIC voidSL1_DIV (Signed_1b15 *r, Signed_1b15 a, Signed_1b15 b){ Signed_1b15 dividend = a; Signed_1b15 divisor = b; int negative_quotient = (dividend < 0) ^ (divisor < 0); Signed_1b15 result; int ii; Signed_1b31 tmp; Int16 shiftnum; if (divisor == 0 || dividend == 0) { *r = 0; return; } /* note that if the value being negated is MIN_16, the result is * still negative if treated as Signed. So be careful to cast to * an unsigned value where it matters */ if (dividend < 0) { dividend = (Signed_1b15) -dividend; } if (divisor < 0) { divisor = (Signed_1b15) -divisor; } if ((UnSigned_1b15) divisor <= (UnSigned_1b15) dividend) { *r = negative_quotient ? (Signed_1b15) MIN_16 : (Signed_1b15) MAX_16; return; } shiftnum = _sl1_lead_mmode(dividend, L_CNT_ZERO) - _sl1_lead_mmode(divisor, L_CNT_ZERO) ; dividend = dividend<<shiftnum; result = 0; for (ii = 0; ii < 15-shiftnum; ++ii) { if ( (UnSigned_1b15) dividend >= (UnSigned_1b15) divisor) { dividend -= divisor; ++ result; } dividend <<= 1; result <<= 1; } if ( (UnSigned_1b15) dividend >= (UnSigned_1b15) divisor) { ++ result; } if (negative_quotient) { result = (Signed_1b15) -result; /* but inverting the sign of the result means we've rounded upwards */ /* so if result is not exact, round it back down */ if (dividend != 0) { /* subtract one from LSB to round down */ -- result; } } *r = (Signed_1b15) result;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -