Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array Error - Access violation reading location 0xffffffff

Tags:

c++

simd

sse

I have previously used SIMD operators to improve the efficiency of my code, however I am now facing a new error which I cannot resolve. For this task, speed is paramount.

The size of the array will not be known until the data is imported, and may be very small (100 values) or enormous (10 million values). For the latter case, the code works fine, however I am encountering an error when I use fewer than 130036 array values.

Does anyone know what is causing this issue and how to resolve it?

I have attached the (tested) code involved, which will be used later in a more complicated function. The error occurs at "arg1List[i] = ..."

#include <iostream>
#include <xmmintrin.h>
#include <emmintrin.h>

void main()
{
    int j;
    const int loop = 130036;
    const int SIMDloop = (int)(loop/4);
    __m128 *arg1List = new __m128[SIMDloop];

    printf("sizeof(arg1List)= %d, alignof(Arg1List)= %d, pointer= %p", sizeof(arg1List), __alignof(arg1List), arg1List);
    std::cout << std::endl;

    for (int i = 0; i < SIMDloop; i++)
    {
        j = 4*i;
        arg1List[i] = _mm_set_ps((j+1)/100.0f, (j+2)/100.0f, (j+3)/100.0f, (j+4)/100.0f);
    }
}
like image 579
user1765603 Avatar asked Dec 26 '22 15:12

user1765603


1 Answers

Alignment is the reason.

MOVAPS--Move Aligned Packed Single-Precision Floating-Point Values

[...] The operand must be aligned on a 16-byte boundary or a general-protection exception (#GP) will be generated.

You can see the issue is gone as soon as you align your pointer:

__m128 *arg1List = new __m128[SIMDloop + 1];
arg1List = (__m128*) (((int) arg1List + 15) & ~15);
like image 193
Roman R. Avatar answered Jan 27 '23 06:01

Roman R.