目前我正尝试使用SSE将整数数组加载到SIMD寄存器中。
我有一个对齐的32位整数数组Ai
,并希望将4个连续的元素加载到SIMD寄存器Xi
中。但是,执行Xi
后存储在_mm_load_si128
中的值除了第一个之外都是垃圾。
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <immintrin.h>
// number has to be divisible by 4 without remainder
#define VECTOR_SIZE 8
int main() {
__attribute__((aligned (16))) int32_t *Ai = (int32_t*) malloc(VECTOR_SIZE * sizeof(int32_t));
for(int i = 0; i < VECTOR_SIZE; i++) {
Ai[i] = rand() % 100000;
}
__m128i Xi;
for(int i = 0; i < VECTOR_SIZE; i+=4) {
Xi = _mm_load_si128((__m128i*) &Ai[i]);
// show content of Xi and Ai
for(int j = 0; j < 4; j++) {
printf("Xi[%d] = %d\t Ai[%d] = %d\n", j, Xi[j], i+j, Ai[i+j]);
}
}
free(Ai);
}
以下是输出示例:
Xi[0] = 16807 Ai[0] = 16807
Xi[1] = 50073 Ai[1] = 75249
Xi[2] = 1489217992 Ai[2] = 50073
Xi[3] = 1346391152 Ai[3] = 43658
Xi[0] = 8930 Ai[4] = 8930
Xi[1] = 27544 Ai[5] = 11272
Xi[2] = 1489217992 Ai[6] = 27544
Xi[3] = 1346391168 Ai[7] = 50878
有什么问题?
答案 0 :(得分:1)
当你提出你的例子时,你可能意味着这个:
union {
__m128i m128;
int32_t i32[4];
} Xi;
for(int i = 0; i < VECTOR_SIZE; i+=4) {
Xi.m128 = _mm_load_si128((__m128i*) &Ai[i]);
// show content of Xi and Ai
for(int j = 0; j < 4; j++) {
printf("Xi[%d] = %d\t Ai[%d] = %d\n", j, Xi.i32[j], i+j, Ai[i+j]);
}
}
以下是示例输出:
Xi[0] = 89383 Ai[0] = 89383
Xi[1] = 30886 Ai[1] = 30886
Xi[2] = 92777 Ai[2] = 92777
Xi[3] = 36915 Ai[3] = 36915
Xi[0] = 47793 Ai[4] = 47793
Xi[1] = 38335 Ai[5] = 38335
Xi[2] = 85386 Ai[6] = 85386
Xi[3] = 60492 Ai[7] = 60492