SSE2具有在单精度浮点数和32位整数之间转换向量的指令.
_mm_cvtps_epi32()_mm_cvtepi32_ps()但是没有双精度和64位整数的等价物.换句话说,他们失踪了:
_mm_cvtpd_epi64()_mm_cvtepi64_pd()似乎AVX也没有它们.
模拟这些内在函数的最有效方法是什么?
我有以下代码片段:
#include <cstdio>
#include <cstdint>
static const size_t ARR_SIZE = 129;
int main()
{
uint32_t value = 2570980487;
uint32_t arr[ARR_SIZE];
for (int x = 0; x < ARR_SIZE; ++x)
arr[x] = value;
float arr_dst[ARR_SIZE];
for (int x = 0; x < ARR_SIZE; ++x)
{
arr_dst[x] = static_cast<float>(arr[x]);
}
printf("%s\n", arr_dst[ARR_SIZE - 1] == arr_dst[ARR_SIZE - 2] ? "OK" : "WTF??!!");
printf("magic = %0.10f\n", arr_dst[ARR_SIZE - 2]);
printf("magic = %0.10f\n", arr_dst[ARR_SIZE - 1]);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如果我在MS Visual Studio 2015下编译它,我可以看到输出是:
WTF??!! …Run Code Online (Sandbox Code Playgroud) 我有一个__m256包含随机位的值。
我想,以“解释”,就得到另一个__m256保存float
在值均匀 [0.0f, 1.0f]范围。
计划使用:
__m256 randomBits = /* generated random bits, uniformly distribution */;
__m256 invFloatRange = _mm256_set1_ps( numeric_limits<float>::min() ); //min is a smallest increment of float precision
__m256 float01 = _mm256_mul(randomBits, invFloatRange);
//float01 is now ready to be used
Run Code Online (Sandbox Code Playgroud)
问题 1:
但是,这会在非常罕见的情况下导致问题,其中randomBits所有位都为 1,因此是 NAN?
我能做些什么来保护自己免受这种伤害?
我希望float01永远是一个可用的数字
问题2:
使用上述方法获得后,[0 到 1] 范围会保持一致吗?我知道 float 在不同幅度下具有不同的精度