且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

有效计算32位整数乘法的高阶位

更新时间:2023-11-26 12:21:52

gcc 4.3.2(具有-O1优化或更高版本),完全按照如下所示将其功能转换为IA32程序集:

gcc 4.3.2, with -O1 optimisation or higher, translated your function exactly as you showed it to IA32 assembly like this:

umulhi32:
        pushl   %ebp
        movl    %esp, %ebp
        movl    12(%ebp), %eax
        mull    8(%ebp)
        movl    %edx, %eax
        popl    %ebp
        ret

仅执行一个32位mull,并将结果的高32位(来自%edx)放入返回值.

Which is just doing a single 32 bit mull and putting the high 32 bits of the result (from %edx) into the return value.

这就是您想要的,对吧?听起来您只需要对编译器进行优化即可;)您有可能可以通过消除中间变量来向正确的方向推动编译器:

That's what you wanted, right? Sounds like you just need to turn up the optimisation on your compiler ;) It's possible you could push the compiler in the right direction by eliminating the intermediate variable:

unsigned int umulhi32(unsigned int x, unsigned int y)
{
  return (unsigned int)(((unsigned long long)x * y)>>32);
}