0

我正在做一个程序,此时我需要让它高效。我正在使用 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

4

1 回答 1

0

在 gcc 系列编译器中,您可以像这样使用asm :

 int src = 1;
 int dst;

 asm ("mov %1, %0\n\t"
     "add $1, %0"
     : "=r" (dst)
     : "r" (src));

 printf("%d\n", dst);

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

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

于 2016-02-05T15:28:51.470 回答