2020-03-31在自己博客园搬运过来…
2020-03-31 3.71 73 74 75未解决
Homework Problems
3.58
long decode2(long x, long y, long z)
{
return ((y - z) * x) ^ (((y - z) << 63) >> 63);
}
3.59
/*
x = 2 ^ 64 * xh + xl;
y = 2 ^ 64 * yh + yl;
p = x * y = (2 ^64 * xh + xl) * (2 ^ 64 * yh + yl)
= 2 ^ 128 * xh * yh + 2 ^ 64 * (xh * yl + x1 * yh) + xl * yl
because "2 ^ 128 * xh * yh" is more than 128 bits, so
p = 2 ^ 64 * (xh * yl + x1 * yh) + xl * yl
*/
x in %rsi, y in %rdx
store_prod:
movq %rdx, %rax //y-->%rax
cqto //将%rax符号扩展为8字节,高4位保存在%rdx,值为yh;低四位在%rax,值为yl
movq %rsi, %rcx //x-->%rcx
sarq $63, %rcx //符号扩展x的位数为64位,xh保存在%rcx, xl保存在%rsi
imulq %rax, %rcx //m1 = (yl * xh)
imulq %rsi, %rdx //m2 = (xl * yh)
addq %rdx, %rcx //m1 + m2 = (xl * yh) + (yl * xh)
mulq %rsi //128位无符号乘法, 计算%rsi * %rax, 结果的高64位存储在%rdx,低64位在%rax
addq %rcx, %rdx //m1 + m2 + %rdx
movq %rax, (%rdi) //保存最终结果的低64位
movq %rdx, 8(%rdi) //保存最终结果的高64位
ret
3.60
//A
x in %rdi, n in %esi, result in %rax, mask in %rdx
//B
The initial values of result and mask are 0 and 1
//C
mask != 0
//D
mask <<= 4;
//E
result |= (x & mask)
//F
long loop(long x, int n)
{
long result = 0