Original Post
i have two images which im blending... im doing this with SSE and i simply loop over all the pixels and peform the calculations... it looks something like this... for each loop i do a non-temporal prefetch for the data used in the next loop... i then do calculations for all the pixels in the cache line... im wondering if this is the optimal way to use cache? is there anything i can improve? maybe prefetching several cache lines? EDIT: the reason im doing is that when i had no prefetching in my code and looked at the resulting assembly i found that the compiler had not added any prefetching... which is why i am now adding prefetch hints to the compiler
void foo(unsigned char* pDest, unsigned char* pSrc, int count)
{
__m128i* dest = reinterpret_cast<__m128i*>(pDest);
__m128i* source = reinterpret_cast<__m128i*>(pSrc);
_mm_prefetch(reinterpret_cast<char*>(&dest[0]) , _MM_HINT_NTA);
_mm_prefetch(reinterpret_cast<char*>(&source[0]) , _MM_HINT_NTA);
int length = count/sizeof(__m128i);
for(int n = 0; n < length; n+=4)
{
// prefetch a cache line
_mm_prefetch(reinterpret_cast<char*>(&dest[n+4]) , _MM_HINT_NTA);
_mm_prefetch(reinterpret_cast<char*>(&source[n+4]) , _MM_HINT_NTA);
// UNROLL 4 TIMES AND DO CALCULATIONS
d = _mm_load_si128(&dest[n+0]);
s = _mm_load_si128(&source[n+0]);
// DO CALC
_mm_store_si128(&dest[n], r);
d = _mm_load_si128(&dest[n+1]);
s = _mm_load_si128(&source[n+1]);
// DO CALC
_mm_store_si128(&dest[n+1], r);
d = _mm_load_si128(&dest[n+2]);
s = _mm_load_si128(&source[n+2]);
// DO CALC
_mm_store_si128(&dest[n+2], r);
d = _mm_load_si128(&dest[n+3]);
s = _mm_load_si128(&source[n+3]);
// DO CALC
_mm_store_si128(&dest[n+3], r);
}
}