留下备忘,呵呵。
随便实现了一下,不知道是否还有更好的方法,随着学习进度修改本实现
(该思想通过拆分高低位实现,原则上可以实现所有运算中的越界)
#include
#include
void split (unsigned int src,unsigned int &high,unsigned int &low){
high=src>>16;
low=src&((1<<16) -1);
}
void myintxint(unsigned int e1,unsigned int e2,unsigned int &first,unsigned int &second ){
unsigned int e11;//e1 的高16位
unsigned int e12;//e1 的低16位
split(e1,e11,e12);
unsigned int e21;//e2 的高16位
unsigned int e22;//e2 的低16位
split(e2,e21,e22);
//计算乘积
//低*低 +低*高 +高*低 + 高*高
first=e11*e21;
second=e12*e22;
unsigned int tmp1;
unsigned int tmp2;
unsigned int tmp3;
unsigned int tmp4;
unsigned int tmp5;
unsigned int tmp6;
split(e12*e21,tmp1,tmp2);
first+=tmp1;
split(second,tmp3,tmp4);
split(tmp2+tmp3,tmp5,tmp6);
first+=tmp5;
second+=tmp2<<16;
split(e21*e12,tmp1,tmp2);
first+=tmp1;
split(second,tmp3,tmp4);
split(tmp2+tmp3,tmp5,tmp6);
first+=tmp5;
second+=tmp2<<16;
}
void multiply(unsigned int first,unsigned int second,unsigned int& high,unsigned int& low)
{
unsigned int h1 = first>>16;
unsigned int l1 = first&((1<<16)-1);
unsigned int h2 = second>>16;
unsigned int l2 = second&((1<<16)-1);
unsigned int a1h = (h1*l2)>>16;
unsigned int a1l = (h1*l2)&((1<<16)-1);
unsigned int a2h = (h2*l1)>>16;
unsigned int a2l = (h2*l1)&((1<<16)-1);
unsigned int lh = (l1*l2)>>16;
unsigned int ll = (l1*l2)&((1<<16)-1);
unsigned int ah =(a2l+a1l+lh)>>16;
unsigned int al =(a2l+a1l+lh)&((1<<16)-1);
high = h1*h2+a1h+a2h+ah;
low = (al<<16)+ll;
}
void main(int args,char** argv){
unsigned int first;
unsigned int second;
unsigned int e1=1000000000;
unsigned int e2=1000000000;
unsigned __int64 e3=e1;
unsigned __int64 e4=e2;
myintxint(e1,e2,first,second);
printf(" %08x*%08x = 0x%08x%08x /n",e1,e2,first,second);
multiply(e1,e2,first,second);
printf(" %08x*%08x = 0x%08x%08x /n",e1,e2,first,second);
printf(" %08I64x*%08I64x = 0x%016I64x /n",e3,e4,e3*e4);
}