Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot access memory as SSE type on x86 but works fine on x64

Tags:

c++

x86

sse

I've got some code written using the MSVC SSE intrinsics.

            __m128 zero = _mm_setzero_ps();
            __m128 center = _mm_load_ps(&sphere.origin.x);
            __m128 boxmin = _mm_load_ps(&rhs.BottomLeftClosest.x);
            __m128 boxmax = _mm_load_ps(&rhs.TopRightFurthest.x);

            __m128 e = _mm_add_ps(_mm_max_ps(_mm_sub_ps(boxmin, center), zero), _mm_max_ps(_mm_sub_ps(center, boxmax), zero));
            e = _mm_mul_ps(e, e);

            __declspec(align(16)) float arr[4];
            _mm_store_ps(arr, e);
            float r = sphere.radius;
            return (arr[0] + arr[1] + arr[2] <= r * r);

The Math::Vector type (which is the type of sphere.origin, rhs.BottomLeftClosest, and rhs.TopRightFurthest) is effectively an array of 3 floats. I aligned them to 16 bytes and this code executes fine on x64. But on x86 I get access violation reading a null pointer. Any advice on where this comes from?

like image 507
Puppy Avatar asked May 07 '12 15:05

Puppy


1 Answers

        __m128 center = _mm_load_ps(&sphere.origin.x);

_mm_load_ps() requires that the passed pointer is 16-byte aligned. There's no evidence that you ensured that sphere.origin.x is aligned properly. You'll need to use _mm_loadu_ps() instead if you can't provide that guarantee.

like image 57
Hans Passant Avatar answered Oct 22 '22 14:10

Hans Passant