Access Violation _mm_store_si128 SSE Intrinsics

I want to create a histogram of vertical gradients in an 8-bit gray image. You can specify the vertical distance to calculate the gradient. I have already managed to speed up the other part of my code using Intrinsics, but this does not work here. Code runs without exception if _mm_store_si128 is commented out. When it is not commented, I get an access violation.

What's going on here?

#define _mm_absdiff_epu8(a,b) _mm_adds_epu8(_mm_subs_epu8(a, b), _mm_subs_epu8(b, a)) //from opencv
void CreateAbsDiffHistogramUnmanaged(void* source, unsigned int sourcestride, unsigned int height, unsigned int verticalDistance, unsigned int histogram[])
{
unsigned int xcount = sourcestride / 16;
__m128i absdiffData;
unsigned char* bytes = (unsigned char*) _aligned_malloc(16, 16);
__m128i* absdiffresult = (__m128i*) bytes;
__m128i* sourceM = (__m128i*) source;
__m128i* sourceVOffset = (__m128i*)source + verticalDistance * sourcestride;

for (unsigned int y = 0; y < (height - verticalDistance); y++)
{
    for (unsigned int x = 0; x < xcount; x++, ++sourceM, ++sourceVOffset)
    {
        absdiffData = _mm_absdiff_epu8(*sourceM, *sourceVOffset);
        _mm_store_si128(absdiffresult, absdiffData);
        //unroll loop
        histogram[bytes[0]]++;
        histogram[bytes[1]]++;
        histogram[bytes[2]]++;
        histogram[bytes[3]]++;
        histogram[bytes[4]]++;
        histogram[bytes[5]]++;
        histogram[bytes[6]]++;
        histogram[bytes[7]]++;
        histogram[bytes[8]]++;
        histogram[bytes[9]]++;
        histogram[bytes[10]]++;
        histogram[bytes[11]]++;
        histogram[bytes[12]]++;
        histogram[bytes[13]]++;
        histogram[bytes[14]]++;
        histogram[bytes[15]]++;
    }
}
_aligned_free(bytes);
}
+4
source share
1 answer

Your function crashes on load because the input is not aligned. To solve the problem, you need to change the code:

from

absdiffData = _mm_absdiff_epu8(*sourceM, *sourceVOffset);

at

absdiffData = _mm_absdiff_epu8(_mm_loadu_si128(sourceM), _mm_loadu_si128(sourceVOffset));

Here I used an unaligned download.

P.S. (SimdAbsSecondDerivativeHistogram) Simd Library. SSE2, AVX2, NEON Altivec. , .

P.P.S. :

__m128i* sourceVOffset = (__m128i*)source + verticalDistance * sourcestride);

( ). , :

__m128i* sourceVOffset = (__m128i*)((char*)source + verticalDistance * sourcestride);
+3

Source: https://habr.com/ru/post/1669809/


All Articles