插入一个“__asm__”块以执行非常大的加法



我正在做一个程序,在这一点上我需要提高它的效率。我正在使用Haswell微架构(64位)和"g++"。目标是使用ADC指令,直到循环结束。

//I removed every carry handlers from this preview, yo be more simple
size_t anum = ap[i], bnum = bp[i];
unsigned carry;
// The Carry flag is set here with an common addtion  
anum += bnum;
cnum[0]= anum;
carry = check_Carry(anum, bnum);
for (int i=1; i<n; i++){
    anum  = ap[i];
    bnum = bp[i];
    //I want to remove this line and insert the __asm__ block
    anum += (bnum + carry);
    carry = check_Carry(anum, bnum);
    //This block is not working
    __asm__(
            "movq   -64(%rbp), %rcx;"
            "adcq   %rdx, %rcx;"
            "movq   %rsi, -88(%rbp);"
    );
    cnum[i] = anum;
}

CF是否仅在第一次添加中设置?还是每次我做ADC指令时?

我认为问题出在每次循环完成时CF损失。如果是这个问题,我该如何解决?

你在 gcc 编译器家族中使用这样的 asm:

 int src = 1;
 int dst;
 asm ("mov %1, %0nt"
     "add $1, %0"
     : "=r" (dst)
     : "r" (src));
 printf("%dn", dst);

也就是说,您可以引用变量,而不是猜测它们在内存/寄存器中的位置。

[编辑] 关于携带的主题:目前还不完全清楚你想要什么,但是:ADCCF作为输入并将其作为输出生成。但是,许多其他指令都带有标志(例如编译器可能用来构造for循环的指令),因此您可能需要使用一些指令来保存/恢复CF(可能是LAHF/SAHF)。

最新更新