本文介绍了计算高64位的64×64 INT产品的C语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我的C函数有效地计算高64位的两个64位有符号整数的产品。我知道如何做到这一点的x86-64装配,与imulq拉,结果出来%RDX的。但我在为如何用C写这在所有的亏损,更别说哄编译器能够有效地做到这一点。

有没有人有在C写这个有什么建议?这是性能敏感的,所以手动方法(像俄罗斯的农民,或BIGNUM库)都出来了。

这学究气内联汇编函数,我写的作品,是大致codeGEN我以后是:

 静态长mull_hi(长INP1,长INP2){
    长期输出= -1;
    __asm​​ __(MOVQ%[INP1] %% RAX;
            imulq%[INP2]。
            MOVQ %% RDX,%[输出];
            :【输出】= R(输出)
            :[INP1]R(INP1),[INP2]R(INP2)
            :%RAX,%RDX);
    返回输出;
}
 

解决方案

如果您使用的是相对较新的海湾合作​​委员会在x86_64:

 的int64_t mulHi(的int64_t X,ÿ的int64_t){
    返程(的int64_t)((__ int128_t)X * Y>> 64);
}
 

目前-O1或更高,这​​编译成你想要的:

  _mulHi:
0000000000000000 MOVQ%RSI,%RAX
0000000000000003 imulq%RDI
0000000000000006 MOVQ%的RDX,%RAX
0000000000000009 RET
 

我相信铛和VC ++也有对__int128_t类型的支持,所以这也应该在这些平台上,用你自己尝试它平时的告诫。

I would like my C function to efficiently compute the high 64 bits of the product of two 64 bit signed ints. I know how to do this in x86-64 assembly, with imulq and pulling the result out of %rdx. But I'm at a loss for how to write this in C at all, let alone coax the compiler to do it efficiently.

Does anyone have any suggestions for writing this in C? This is performance sensitive, so "manual methods" (like Russian Peasant, or bignum libraries) are out.

This dorky inline assembly function I wrote works and is roughly the codegen I'm after:

static long mull_hi(long inp1, long inp2) {
    long output = -1;
    __asm__("movq %[inp1], %%rax;"
            "imulq %[inp2];"
            "movq %%rdx, %[output];"
            : [output] "=r" (output)
            : [inp1] "r" (inp1), [inp2] "r" (inp2)
            :"%rax", "%rdx");
    return output;
}
解决方案

If you're using a relatively recent GCC on x86_64:

int64_t mulHi(int64_t x, int64_t y) {
    return (int64_t)((__int128_t)x*y >> 64);
}

At -O1 and higher, this compiles to what you want:

_mulHi:
0000000000000000    movq	%rsi,%rax
0000000000000003    imulq	%rdi
0000000000000006    movq	%rdx,%rax
0000000000000009    ret

I believe that clang and VC++ also have support for the __int128_t type, so this should also work on those platforms, with the usual caveats about trying it yourself.

这篇关于计算高64位的64×64 INT产品的C语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 19:24