微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

我有一个保存随机位的 __m256 值。

我想“解释”它,以获得另一个持有 __m256float 统一 [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

问题 1:

但是,在极少数情况下,randomBits 的所有位都为 1,因此是 NAN,这会导致问题吗?

我能做些什么来保护自己免受这种伤害?

我希望 float01 始终是一个可用的数字

问题 2:

使用上述方法获取后,[0 到 1] 范围会保持一致吗?我知道 float 在不同的幅度下具有不同的精度

解决方法

将 int32_t 重新解释为浮点数,可以

 auto const one = _mm256_set1_epi32(0x7f800000);
 a = _mm256_and_si256(a,_mm256_set1_epi32(0x007fffff));
 a = _mm256_or_si256(a,one);
 return _mm256_sub_ps(_mm256_castsi256_ps(a),_mm256_castsi256_ps(one));

和/或序列将重用输入序列的 23 个 LSB,以在 1.0f

,

正如@Soonts 所指出的,可以在 [0,1] 范围内均匀地创建浮点数:

https://stackoverflow.com/a/54873925/9007125

我最终使用了以下答案:

https://stackoverflow.com/a/54893167/9007125

//converts __m256i values into __m256 values,that contains floats in [0,1] range.
//https://stackoverflow.com/a/54893167/9007125
inline void int_rand_int_toFloat01( const __m256i* m256i_vals,__m256* m256f_vals){ //<-- stores here.
    const static __m256 c =  _mm256_set1_ps(0x1.0p-24f); // or (1.0f / (uint32_t(1) << 24));

    __m256i* rnd =   ((__m256i*)m256i_vals);
    __m256* output =  ((__m256*)m256f_vals);

    // remember that '_mm256_cvtepi32_ps' will convert 32-bit ints into a 32-bit floats
    __m256 converted =  _mm256_cvtepi32_ps(_mm256_srli_epi32(*rnd,8));
             *output =  _mm256_mul_ps( converted,c);
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。