2012-02-06 84 views
3

這是對this one的後續問題。現在我想在相反方向float - >unsigned int。 以下標量運算的最佳和精確向量序列是什麼?將float向量轉換爲uint32向量的最有效方法?

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

http://stackoverflow.com/questions/ 78619 /什麼是最快的方式來轉換浮點運算在x86上 – Anycorn 2012-02-06 08:34:46

+0

不一樣的問題!我想轉換爲** unsigned ** int – 2012-02-06 08:36:26

+0

你想用負號做什麼? – Anycorn 2012-02-06 08:38:42

回答

2

這是根據從舊但有用蘋果的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; 
} 
相關問題