如何解决_mm_stream_si128 比 _mm_store_si128 慢 2000%
我正在编写一些 C 代码来制作随机数生成器,并使用了以下代码:
// header
typedef struct {
uint64_t values[2];
} fy5z_state_t;
fy5z_state_t fy5z_seed(uint64_t seed_value);
uint64_t fy5z_generate(fy5z_state_t* state);
// source
fy5z_state_t fy5z_seed(uint64_t seed_value)
{
fy5z_state_t state;
state.values[0] = (seed_value & 0xFFFFFFFF);
state.values[1] = (seed_value & (0xFFFFFFFF << 31)) + seed_value;
return state;
}
uint64_t fy5z_generate(fy5z_state_t* state)
{
__m128i got_data = _mm_load_si128((__m128i const*)(state->values));
__m128i shuffled = _mm_shuffle_epi32(got_data,0x8d);
__m128i final_add = _mm_add_epi8 (shuffled,got_data);
_mm_store_si128((__m128i*)(state->values),final_add);
return state->values[0];
}
此外,以下代码用于对性能进行计时:
#ifndef TRIAL_COUNT
#define TRIAL_COUNT 1024 * 1024 * 10
#endif
static void print_time_us(const char* name,void(*fn)(void))
{
struct timespec start,end;
clock_gettime(CLOCK_MONOTONIC_RAW,&start);
fn();
clock_gettime(CLOCK_MONOTONIC_RAW,&end);
uint64_t delta_us = (end.tv_sec - start.tv_sec) * 1000000 + (end.tv_nsec - start.tv_nsec) / 1000;
printf("Running: '%s' took %llu u/s\n",name,delta_us);
}
static void test_fy5z(void) {
fy5z_state_t fseed = fy5z_seed(0xfab5381);
unsigned long total = 0;
for (int i = 0; i < TRIAL_COUNT; ++i)
{
total += fy5z_generate(&fseed);
}
}
我发现,在生成器函数中,如果使用 _mm_store_si128
,我得到: Running: 'fy5z' took 113328 u/s
,但如果我用 _mm_stream_si128
交换它,我得到 Running: 'fy5z' took 1956792 u/s
.
这是在 MacOS 2.7 GHz 四核 Intel Core i7 上
为什么在这个用例中 store
比 stream
快这么多?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。