相关疑难解决方法(0)

如何使用SSE/AVX高效执行double/int64转换?

SSE2具有在单精度浮点数和32位整数之间转换向量的指令.

  • _mm_cvtps_epi32()
  • _mm_cvtepi32_ps()

但是没有双精度和64位整数的等价物.换句话说,他们失踪了:

  • _mm_cvtpd_epi64()
  • _mm_cvtepi64_pd()

似乎AVX也没有它们.

模拟这些内在函数的最有效方法是什么?

c++ floating-point sse simd avx

19
推荐指数
2
解决办法
2850
查看次数

奇怪的uint32_t浮点数组转换

我有以下代码片段:

#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)

c++ sse vectorization visual-studio

9
推荐指数
3
解决办法
554
查看次数

将“__m256 with random-bits”转换为 [0, 1] 范围的浮点值

我有一个__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 在不同幅度下具有不同的精度

c++ random floating-point simd avx

5
推荐指数
1
解决办法
154
查看次数

标签 统计

c++ ×3

avx ×2

floating-point ×2

simd ×2

sse ×2

random ×1

vectorization ×1

visual-studio ×1