2013-03-06 14 views
7

的我有以下代碼找到我嘗試用SSE3內在矢量化它的最大值SSE3內部函數:如何找到最大的一個大陣花車

int length = 2000; 
float *data; 
// data is allocated and initialized 

float max = 0.0; 
for(int i = 0; i < length; i++) 
{ 
    if(data[i] > max) 
    { 
     max = data; 
    } 
} 

,但我有種擊中我應該怎麼做比較。

int length = 2000; 
float *data; 
// data is allocated and initialized 

float max = 0.0; 
// for time being just assume that length is always mod 4 
for(int i = 0; i < length; i+=4) 
{ 
    __m128 a = _mm_loadu_ps(data[i]); 
    __m128 b = _mm_load1_ps(max); 

    __m128 gt = _mm_cmpgt_ps(a,b); 

    // Kinda of struck on what to do next 
} 

任何人都可以提供一些想法。

+1

如果我們看代碼的實際意思是,我們看到它與http://stackoverflow.c相同om/questions/9877700 /正在獲取最大價值的m128i-vector-with-sse – 2013-03-06 04:30:40

回答

9

所以你的代碼找到了一個固定長度的浮點數組中的最大值。好。

存在_mm_max_ps,它可以從兩個向量中分別給出兩個向量的成對最大值。那麼這個怎麼樣?

int length = 2000; 
float *data; // maybe you should just use the SSE type here to avoid copying later 
// data is allocated and initialized 

// for time being just assume that length is always mod 4 
__m128 max = _mm_loadu_ps(data); // load the first 4 
for(int i = 4; i < length; i+=4) 
{ 
    __m128 cur = _mm_loadu_ps(data + i); 
    max = _mm_max_ps(max, cur); 
} 

最後,搶到了最大的四個值的max(見Getting max value in a __m128i vector with SSE?爲該)。

它應該是這樣的:

第1步:

[43, 29, 58, 94] (this is max) 
[82, 83, 10, 88] 
[19, 39, 85, 77] 

第2步:

[82, 83, 58, 94] (this is max) 
[19, 39, 85, 77] 

第2步:

[82, 83, 85, 94] (this is max) 
+0

不客氣。當你完成後,我很樂意看到一些基準。 :) – 2013-03-06 05:03:54

+0

不應該是:'for(int i = 4; i 2013-03-06 07:54:57

+0

@JohnZwinck *「我很樂意在你完成時看到一些基準」* - 可能會是一個不好的驚喜,因爲* unaligned *動作是你可以用SSE做的最糟糕的事情。 – 2013-03-06 08:14:49

相關問題