【问题标题】:Most efficient way to convert vector of float to vector of uint32?将浮点向量转换为 uint32 向量的最有效方法?
【发布时间】:2012-02-06 08:27:56
【问题描述】:

这是this one 的后续问题。现在我想在相反的方向转换 float --> unsigned int。 下面标量运算的最优准确向量序列是多少?

float x = ...
unsigned int res = (unsigned int)x;

【问题讨论】:

  • 不是同一个问题!我想转换为 unsigned int
  • 你想用负号做什么?
  • “向量序列”是什么意思? sse 在 x86 上?内在的?
  • sse 程序集或 sse 内在函数都可以

标签: assembly floating-point sse


【解决方案1】:

这是基于旧但有用的 Apple AltiVec-SSE 迁移文档中的一个示例,不幸的是,该文档现已不再提供 http://developer.apple.com

inline __m128i _mm_ctu_ps(const __m128 f)
{
    const __m128 two31 = _mm_set1_ps(0x1.0p31f);
    const __m128 two32 = _mm_add_ps(two31, two31);
    const __m128 zero = _mm_xor_ps(f,f);

    // check for overflow before conversion to int
    const __m128 overflow = _mm_cmpge_ps(f, two31);
    const __m128 overflow2 = _mm_cmpge_ps(f, two32);
    const __m128 subval = _mm_and_ps(overflow, two31);
    const __m128i addval = _mm_slli_epi32((__m128i)overflow, 31);
    __m128i result;

    // bias the value to signed space if it is >= 2**31
    f = _mm_sub_ps(f, subval);

    // clip at zero
    f = _mm_max_ps(f, zero);

    // convert to int with saturation
    result = _mm_cvtps_epi32(f); // rounding mode should be round to nearest

    // unbias
    result = _mm_add_epi32(result, addval);

    // patch up the overflow case
    result = _mm_or_si128(result, (__m128i)overflow2);

    return result;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-09
    • 1970-01-01
    • 2023-03-17
    相关资源
    最近更新 更多