62,628
社区成员
发帖
与我相关
我的任务
分享
private static void destructiveMulAdd(int[] x, int y, int z) {
// Perform the multiplication word by word
//将y与z转换为long类型
long ylong = y & LONG_MASK;
long zlong = z & LONG_MASK;
int len = x.length;
long product = 0;
long carry = 0;
//从低位到高位分别与y相乘,每次都加上之前的进位,和传统乘法一模一样.
for (int i = len-1; i >= 0; i--) {
//每次相乘时将x[i]转换为long,这样其32位数就可转变为其真正代表的数
product = ylong * (x[i] & LONG_MASK) + carry;
//x[i]取乘积的低32位.
x[i] = (int)product;
//高32位为进位数,留到下次循环相加
carry = product >>> 32;
}
// Perform the addition
//执行加z
//mag最低位转换为long后与z相加
long sum = (x[len-1] & LONG_MASK) + zlong;
//mag最低位保留相加结果的低32位.
x[len-1] = (int)sum;
//高32位当成进位数
carry = sum >>> 32;
//和传统加法一样进位数不断向高位加
for (int i = len-2; i >= 0; i--) {
sum = (x[i] & LONG_MASK) + carry;
x[i] = (int)sum;
carry = sum >>> 32;
}
}