Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
让GCC在使用内联asm的整个函数中保留SSE寄存器_C_Gcc_Assembly_Sse_Inline Assembly - Fatal编程技术网

让GCC在使用内联asm的整个函数中保留SSE寄存器

让GCC在使用内联asm的整个函数中保留SSE寄存器,c,gcc,assembly,sse,inline-assembly,C,Gcc,Assembly,Sse,Inline Assembly,我正在用C写一个程序,需要做一些快速的数学计算。我使用内联SSE汇编指令获得一些SIMD操作(使用压缩双精度浮点数)。我在Linux上使用GCC进行编译 我需要循环一些数据,在计算中使用常数因子。我希望在循环期间将该因子隐藏在一个安全的寄存器中,这样我就不必每次都重新加载它 用一些代码来澄清: struct vect2 { fltpt x; fltpt y; }__attribute__((aligned(16))); /* Align on 16B boundary for S

我正在用C写一个程序,需要做一些快速的数学计算。我使用内联SSE汇编指令获得一些SIMD操作(使用压缩双精度浮点数)。我在Linux上使用GCC进行编译

我需要循环一些数据,在计算中使用常数因子。我希望在循环期间将该因子隐藏在一个安全的寄存器中,这样我就不必每次都重新加载它

用一些代码来澄清:

struct vect2 {
    fltpt x;
    fltpt y;
}__attribute__((aligned(16))); /* Align on 16B boundary for SSE2 instructions */
typedef struct vect2 vect2_t;


void function()
{
    /* get a specific value set up in xmm1, and keep it there for the 
     * rest of the loop. */
    for( int i = 0, i<N; i++ ){
        asm(
            "Some calculations;"
            "on an element of;"
            "a data set.;"
            "The value in xmm1;"
            "is needed;"
        );
    }
}

我不只是想假设GCC不会更改xmm1寄存器,它太像是一种“恶魔从鼻子里飞出来”的东西:-)。所以我希望有一个合适的方法来做到这一点。

我认为这里的解决方案是让gcc意识到你的
vec2\u t
类型实际上是一个向量;然后,您可以只计算循环不变值并将其视为普通变量(除非编译器知道它是向量类型):

typedef双向量2_t___属性(向量大小(16));
空函数()
{
/*设置一个特定的值,例如*/
向量2_t不变量;
asm(“某些计算、排序导致不变量。”
:“=x”(不变量);

对于(inti=0;i我认为最好将寄存器分配留给编译器。它可能比您更好地跟踪它。 GCC已经使用了SSE扩展,但是如果您确信您知道的更多,请使用GCC内置函数。 老实说,我有点怀疑你那样做会更快。

祝你好运!

这些网站可能很有趣


我习惯于使用汇编和C语言,我在这里要做的是在汇编中编写整个函数。 如果您有一个灵活的make系统,我建议单独组装ASM函数并将其链接到应用程序中。唯一的问题是编译器无法内联该函数

void函数(void);/C

外部“C”函数(void);//C++

register vect2_t hVect asm("xmm1") = {h, h};
/* Gives error: data type of 'hVect' isn't suitable for a register */

register vect2_t *hVect2 asm("rax");
*hVect2 = (vect2_t){h,h};
/* Seems to work, but not what I'm looking for */
typedef double vec2_t __attribute__ ((vector_size (16)));

void function()
{
  /* get a specific value set up, e.g. */
  vec2_t invariant;
  asm( "some calculations, soring result in invariant."
       : "=x" (invariant) );

  for( int i = 0; i<N; i++ ){
    asm(
            "Some calculations;"
            "on an element of;"
            "a data set.;"
            "The value in xmm1;"
            "is needed;"
            : "x" (invariant) // and other SSE arguments
       );
   }
}