AVX转换64位整数到64位浮点数

AVX convert 64 bit integer to 64 bit float

本文关键字:64位 浮点数 整数 AVX 转换      更新时间:2023-10-16

我想使用AVX将4个打包的64位整数转换为4个打包的64位浮点数。我试过这样做:

int_64t *ls = (int64_t *) _mm_malloc(256, 32);
ls[0] = a;
//...
ls[3] = d;
__mm256i packed = _mm256_load_si256((__m256i const *)ls);

将在调试器中显示:

(gdb) print packed
$4 = {1234, 5678, 9012, 3456}

好的,到目前为止,但是我能找到的唯一的强制转换/转换操作是_mm256i_castsi256_pd,这并没有得到我想要的:

__m256d pd = _mm256_castsi256_pd(packed);
(gdb) print pd
$5 = {6.0967700696809824e-321, 2.8053047370865979e-320, 4.4525196003213139e-320, 1.7074908720273481e-320}

我真正想看的是:

(gdb) print pd
$5 = {1234.0, 5678.0, 9012.0, 3456.0}

所有的强制转换内部函数执行位强制转换,这就是为什么你没有看到有意义的结果。

不存在64位整型和64位浮点型之间的向量转换(cvt内在函数)

为了它的价值,我查看了Agner Fog的vectorclass,看看他是如何做到的。他只是将64位整数存储到一个数组中,并将每个数组值强制转换为双精度类型。这是低效的,但它是有效的。

From file "vectorf256.h":

// function to_double: convert integer vector elements to double vector (inefficient)
static inline Vec4d to_double(Vec4q const & a) {
    int64_t aa[4];
    a.store(aa);
    return Vec4d(double(aa[0]), double(aa[1]), double(aa[2]), double(aa[3]));
}
// function to_double: convert integer vector to double vector
static inline Vec4d to_double(Vec4i const & a) {
    return _mm256_cvtepi32_pd(a);
}