128乘法和除法的本征 [英] Intrinsics for 128 multiplication and division
问题描述
在x86_64中,我知道mul和div opp代码通过将低64位放在rax中并将高64位放在rdx寄存器中来支持128个整数.我在intel内在函数指南中正在寻找某种内在函数来执行此操作,但找不到.我正在写一个大数字库,其字长为64位.现在,我正在按这样的一个词进行除法.
In x86_64 I know that the mul and div opp codes support 128 integers by putting the lower 64 bits in the rax and the upper in the rdx registers. I was looking for some sort of intrinsic to do this in the intel intrinsics guide and I could not find one. I am writing a big number library where the word size is 64 bits. Right now I am doing division by a single word like this.
int ubi_div_i64(ubigint_t* a, ubi_i64_t b, ubi_i64_t* rem)
{
if(b == 0)
return UBI_MATH_ERR;
ubi_i64_t r = 0;
for(size_t i = a->used; i-- > 0;)
{
ubi_i64_t out;
__asm__("\t"
"div %[d] \n\t"
: "=a"(out), "=d"(r)
: "a"(a->data[i]), "d"(r), [d]"r"(b)
: "cc");
a->data[i] = out;
//ubi_i128_t top = (r << 64) + a->data[i];
//r = top % b;
//a->data[i] = top / b;
}
if(rem)
*rem = r;
return ubi_strip_leading_zeros(a);
}
如果我可以在x86intrinsics.h标头中使用某些内容而不是嵌入式asm,那就太好了.
It would be nice if I could use something in the x86intrinsics.h header instead of inline asm.
推荐答案
gcc具有__int128
和__uint128
类型.
与它们的算法应该在存在时使用正确的汇编指令;我过去曾使用它们来获得产品的高64位,尽管我从未将其用于除法.如果使用的不是正确的,请相应地提交错误报告/功能请求.
Arithmetic with them should be using the right assembly instructions when they exist; I've used them in the past to get the upper 64 bits of a product, although I've never used it for division. If it's not using the right ones, submit a bug report / feature request as appropriate.
这篇关于128乘法和除法的本征的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!