c - 汇编中 float 的求和数组

标签 c assembly x86 sse simd

我在汇编 x86 中实现一个函数,从 C 程序调用,以添加一个 float 数组。该函数的第一个参数是指向数组的指针,第二个参数是元素的数量。当我在 linux 中运行代码时,出现段错误。我做错了什么?

.text
.globl floatsum

floatsum:
push %ebp
movl %esp, %ebp

movl  8(%ebp), %eax
movl 12(%ebp), %edx
shrl $2, %edx

xorps %xmm0, %xmm0
loop:
testl %edx, %edx
je end  
movaps (%eax), %xmm1
addps %xmm1, %xmm0
addl $16, %eax
decl %edx
jmp loop 

end:
                            #         3       2      1       0
movaps %xmm0, %xmm1         # xmm0:   w       z      y       x 
                            # xmm1:   z       w      x       y
shufps $0xb1, %xmm1, %xmm1  #        10      11      00      01  = 0xb1
addps  %xmm1, %xmm0         # xmm0:  w+z     z+w     y+x     x+y
movaps %xmm0, %xmm1         # xmm1:  w+z     z+w     y+x     x+y
                            # xmm1:  x+y     y+x     z+w     w+z
shufps $0x1b, %xmm1, %xmm1  #        00      01      10      11  = 0x1b
addps  %xmm1, %xmm0         # xmm0:  w+z+x+y z+w+y+x y+x+z+w x+y+w+z
                            #
#movd %xmm0, %eax
#pushl %eax

finst:

flds (%esp)
popl %eax

movl %ebp, %esp
popl %ebp
ret

//C代码

#include <stdio.h>
#include <stdlib.h>


float
floatsum(float *array, size_t number_of_items);

float
floatsum_c(float *array, size_t number_of_items){
float sum;
size_t i;

sum=0.0;
for(i=0; i<number_of_items;i++){
    sum+=array[i];
}
return sum;
}

float *
create_array(size_t number_of_items){
float *array;
size_t i;

array=calloc(number_of_items, sizeof(float));
if(array){
    for(i=0; i<number_of_items; i++){
        array[i]=1.0+(float)i;
    }
   }
   return array;
 }

int
main(int argc, char **argv){
float *a;
float result;
size_t number_of_items, i;

number_of_items=8;
a=create_array(number_of_items);
if(a){
    result=floatsum_c(a, number_of_items);
    printf("Sum (c version): %f\n", result);    
    result=floatsum(a, number_of_items);
    printf("Sum (asm version): %f\n", result);  
    free(a);
}

return 0;
}

最佳答案

正如 Paul 所提到的,这可能是一个对齐问题。从您的 C 代码中可以清楚地看出,您的 float 组不能保证在 16 字节边界上对齐。失败是这一行:

movaps (%eax), %xmm1

原因是MOVAPS有这个要求:

When the source or destination operand is a memory operand, the operand must be aligned on a 16-byte (128-bit version) or 32-byte (VEX.256 encoded version) boundary or a general-protection exception (#GP) will be generated.

由于您使用的是 128 位 vector 寄存器,因此您需要 16 字节对齐。您有两个选择:

  • MOVAPS 更改为 MOVUPS这样就可以完成未对齐的内存访问
  • 修改您的 C 代码以创建在 16 字节边界上对齐的 float 组

第一个解决方案需要:

movaps (%eax), %xmm1

改成;

movups (%eax), %xmm1

第二种解决方案是避免使用 calloc并利用允许您创建具有 16 字节对齐的对象的功能。如果使用 C11 那么你可以使用函数 aligned_allocmemset将数组归零。您的 create_array 可能如下所示:

float *
create_array(size_t number_of_items)
{
    float *array = NULL;
    size_t i;

    array=(float *)aligned_alloc(16, number_of_items * sizeof(*array));
    if(array){
        memset (array, 0x00, number_of_items * sizeof(*array));
        for(i=0; i<number_of_items; i++){
            array[i]=1.0+(float)i;
        }
    }
    return array;
}

如果您不使用 C11,则可以使用 POSIX 函数 posix_memalign和 Linux 上的 memset。代码可能类似于:

float *
create_array(size_t number_of_items)
{
    float *array = NULL;
    size_t i;

    if (!posix_memalign((void **)&array, 16, number_of_items * sizeof(*array))){
        memset (array, 0x00, number_of_items * sizeof(*array));
        for(i=0; i<number_of_items; i++){
            array[i]=1.0+(float)i;
        }
    }
    return array;
}

您还必须取消注释这些行:

#movd %xmm0, %eax
#pushl %eax

这样他们就这样出现了:

movd %xmm0, %eax
pushl %eax

注意:虽然我使用 memset 将 float 组置零,就像 calloc 那样,但您的代码实际上并不需要它因为您之后将所有元素初始化为特定值。在您的情况下,可以删除对 memset 的调用。

关于c - 汇编中 float 的求和数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43372750/

相关文章:

c - 非空终止 char* 数组的长度

c - 有没有成熟的二元决策图工具可用?

c - MIPS 指令的简单 C 函数

assembly - 在Knights Landing上清除单个或几个ZMM寄存器的最有效方法是什么?

c - 助记符中的无效字符 '\'

assembly - 主要发现 - 一些数组值被覆盖?

objective-c - printf/appendFormat 中的 float 不精确

c - IOHID : add trackpad to matching dictionary

linux - 汇编 Linux 系统调用与汇编 OS x 系统调用

x86 - DC PMM(内存模式)缓存​​一致性如何表现?