插入一个`__asm__`块来进行非常大的加法

时间:2016-02-05 15:21:26

标签: c++ assembly cpu-registers bignum multiprecision

我正在做一个程序,此时我需要让它变得高效。 我正在使用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损失上。如果是这个问题我怎么解决呢?

1 个答案:

答案 0 :(得分: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);

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

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