void mul_wide_s32(int in0, int in1, unsigned int *ptrOutBitsHi, unsigned int *ptrOutBitsLo)
{
unsigned int absIn0;//输入In0的绝对值
unsigned int absIn1;//输入In1的绝对值
unsigned int in0Lo;//输入In0绝对值的低16位
unsigned int in0Hi;//输入In0绝对值的高16位
unsigned int in1Lo;//输入In1绝对值的低16位
unsigned int in1Hi;//输入In1绝对值的高16位
unsigned int productHiLo;//In0的高16位与In的低16位的乘积
unsigned int productLoHi;//In0的低16位与In的高16位的乘积
unsigned int productLoLo;//In0的低16位与In的低16位的乘积
absIn0 = (unsigned int)(in0 < 0 ? -in0 : in0);//对In0取绝对值
absIn1 = (unsigned int)(in1 < 0 ? -in1 : in1);//对In1取绝对值
in0Hi = absIn0>>16;
in0Lo = absIn0 &65535U;
in1Hi = absIn1>>16;
in1Lo = absIn1 &65535U;
productHiLo = in0Hi*in1Lo;
productLoHi = in0Lo*in1Hi;
productLoLo = in0Lo*in1Lo;
absIn1 = (unsigned int)0;
in0Lo = (productLoHi << 16) + productLoLo;
if (in0Lo < productLoLo)
{
absIn1 = (unsigned int)1;
}
absIn0 = in0Lo;
in0Lo = (productHiLo << 16) + in0Lo;
if (in0Lo < absIn0)
{
absIn1 = absIn1+(unsigned int)1;
}
absIn0 = ((in0Hi * in1Hi + absIn1) + (productLoHi >> 16)) + (productHiLo >> 16);
if (!((in0 == 0) || (in1 == 0) || ((in0 > 0) == (in1 > 0)))) {
absIn0 = ~absIn0;
in0Lo = ~in0Lo;
in0Lo = in0Lo + (unsigned int)1;
if (in0Lo == (unsigned int)0) {
absIn0 = absIn0 + (unsigned int)1;
}
}
*ptrOutBitsHi = absIn0;
*ptrOutBitsLo = in0Lo;
}
上面这个程序是实现32位数相乘
下面的这个程序怎么理解 求指导 谢谢
int mul_s32_s32_s32_sr28(int a, int b)
{
int result;
unsigned int u32_chi;
unsigned int u32_clo;
mul_wide_s32(a, b, &u32_chi, &u32_clo);
u32_clo = (unsigned int)(u32_chi << 4 | u32_clo >> 28);
result = (unsigned int)u32_clo;
return result;
}
------解决方案--------------------------------------------------------
mul_wide_s32(a, b, &u32_chi, &u32_clo); 两个32位信号作乘法
u32_clo = (unsigned int)(u32_chi << 4 | u32_clo >> 28);取出输出的64位信号中的28到60位
具体数学上的意义不知道 应该和其他数据处理的函数一起看
------解决方案--------------------------------------------------------
- C/C++ code
int mul_s32_s32_s32_sr28(int a, int b){ int result; unsigned int u32_chi; unsigned int u32_clo; mul_wide_s32(a, b, &u32_chi, &u32_clo); u32_clo = (unsigned int)(u32_chi << 4 | u32_clo >> 28); result = (unsigned int)u32_clo; return result;}
------解决方案--------------------------------------------------------
http://www.mathworks.com/matlabcentral/fx_files/27694/1/content/nxtway_gs/models/nxtway_app_fixpt_ert_rtw/html/nxtway_app_fixpt_c
你看看这个 再想想你的程序是用来干嘛的
------解决方案--------------------------------------------------------
- C/C++ code
void mul_wide_s32(int in0, int in1, unsigned int *ptrOutBitsHi, unsigned int *ptrOutBitsLo){ unsigned int absIn0;//输入In0的绝对值 unsigned int absIn1;//输入In1的绝对值 unsigned int in0Lo;//输入In0绝对值的低16位 unsigned int in0Hi;//输入In0绝对值的高16位 unsigned int in1Lo;//输入In1绝对值的低16位 unsigned int in1Hi;//输入In1绝对值的高16位 unsigned int productHiLo;//In0的高16位与In的低16位的乘积 unsigned int productLoHi;//In0的低16位与In的高16位的乘积 unsigned int productLoLo;//In0的低16位与In的低16位的乘积 absIn0 = (unsigned int)(in0 < 0 ? -in0 : in0);//对In0取绝对值 absIn1 = (unsigned int)(in1 < 0 ? -in1 : in1);//对In1取绝对值 in0Hi = absIn0>>16; //get the high 16 bits of int0 in0Lo = absIn0 &65535U; //get the low 16 bits of int0 in1Hi = absIn1>>16; //get the high 16 bits of int1 in1Lo = absIn1 &65535U; //get the low 16 bits of int1 //as we know that the result should be a 64 bits number productHiLo = in0Hi*in1Lo; //we get a value whose low 16 bits should be the low 16 bits of final result let us denote the bits by 32-16-0 productLoHi = in0Lo*in1Hi;//let us denote the bits by 48-32-16 productLoLo = in0Lo*in1Lo;//let us denote the bits by 48-32-16 //you should know 32-16-0 means that the value composes the 32-16-0 bits of the final result absIn1 = (unsigned int)0; //set absIn1 to zero in0Lo = (productLoHi << 16) + productLoLo;// as we know that the low 16 bits of final result will come from productLoLo, we need to find is 64-17 bits of the final result, moreover the low 16 bits of productLoHi will compose the 32-17 bits of thefinal result with the high 16 bits of productLoLo, so add themif (in0Lo < productLoLo){absIn1 = (unsigned int)1; //we add the two values, what we get is a number must larger than the original number.If not the case then overflow happens, in other words,the carry must be one. So set absIn1 to unity}absIn0 = in0Lo; in0Lo = (productHiLo << 16) + in0Lo; //the low 16 bits of productHiLo compose the bits 32-17 of final result,so add itif (in0Lo < absIn0){absIn1 = absIn1+(unsigned int)1; //we add the two values, what we get is a number must larger than the original number. If not the case, overflow happens, in other words,the carry must be one, then add the carry to adsIn1}//now let us calculate the 64-33 bits of the final answerabsIn0 = ((in0Hi * in1Hi + absIn1) + (productLoHi >> 16)) + (productHiLo >> 16);//the 64-33 bits of the final answer will composed by in0Hi * in1Hi, high 16 bits of productLoHi, high 16 bits of productHiLo and the carry stored in absIn1//if int0=0, int1=0 or int0 and int1 are both positive numbers we get the final resultif (!((in0 == 0) || (in1 == 0) || ((in0 > 0) == (in1 > 0)))) { absIn0 = ~absIn0; in0Lo = ~in0Lo; in0Lo = in0Lo + (unsigned int)1; //if result is a negtive number, change result to 2's complement if (in0Lo == (unsigned int)0) { absIn0 = absIn0 + (unsigned int)1; //if low 32 bits is zero, then carry must be one we add the carry to the high 32 bits }} *ptrOutBitsHi = absIn0; //return the result we get*ptrOutBitsLo = in0Lo; }