使用 SSE 内联函数的寄存器短缺

Register's shortage using SSE intrinsics

本文关键字:寄存器 函数 SSE 使用      更新时间:2023-10-16

在这个SSE加载/存储后的内存事务中,我询问了显式寄存器内存事务和中间指针之间的差异。在实践中,中间指针显示出略高的性能,然而,就硬件而言,什么是中间指针还不清楚?如果创建了指针,是否意味着某些寄存器也被占用,或者寄存器的调用发生在某些SSE操作期间(例如_mm_mul)?

让我们考虑一下这个例子:

struct sse_simple
{
    sse_simple(unsigned int InputLength):
        Len(InputLength/4),
        input1((float*)_mm_malloc((float *)_mm_malloc(cast_sz*sizeof(float), 16))),
        input2((float*)_mm_malloc((float *)_mm_malloc(cast_sz*sizeof(float), 16))),
        output((float*)_mm_malloc((float *)_mm_malloc(cast_sz*sizeof(float), 16))),
        inp1_sse(reinterpret_cast<__m128*>(input1)),
        inp1_sse(reinterpret_cast<__m128*>(input2)),
        output_sse(reinterpret_cast<__m128*>(output))
    {}
    ~sse_simple()
    {
        _mm_free(input1);
        _mm_free(input2);
        _mm_free(output);
    }
    void func()
    {
        for(auto i=0; i<Len; ++i)
            output_sse[i] = _mm_mul(inp1_sse[i], inp2_sse[i]);
    }
    float *input1;
    float *input2;
    float *output; 
    __m128 *inp1_sse;
    __m128 *inp2_sse;
    __m128 *output_sse;
    unsigned int Len;
};

在上面的例子中,中间指针inp1_sse、inp2_sse和output_sse在构造函数中创建一次。如果我复制大量的sse_simple对象(例如50000或更多),这会导致寄存器短缺吗?

首先,寄存器是靠近计算单元的小型存储器(意味着访问速度非常快)。编译器尽可能多地使用它们来加快计算速度,但如果不能,它就会使用内存。由于寄存器中存储的内存量很小,所以在计算过程中,寄存器通常只用作临时寄存器。大多数时候,除了循环索引等临时变量外,所有内容都会存储在内存中。。。所以寄存器的不足只会减慢计算速度。

在计算过程中,指针存储在通用寄存器(GPR)中,无论它们指向浮点、向量还是其他什么,而向量__m128存储在特定寄存器中。

因此,在您的示例中,树数组将存储在内存和行中

output_sse[i] = _mm_mul(inp1_sse[i], inp2_sse[i]);

编译为:

movaps -0x30(%rbp),%xmm0    # load inp1_sse[i] in register %xmm0
movaps -0x20(%rbp),%xmm1    # load inp2_sse[i] in register %xmm1
mulps  %xmm1,%xmm0          # perform the multiplication the result is stored in %xmm0
movaps %xmm0,(%rdx)         # store the result in memory

正如您所看到的,指针是使用寄存器%rbp%rdx存储的。