This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

non-alignment issue

Hi,

As I understand c64x+ core allows non aligned access.

But when I try to load/store 4 byte data at non aligned 32 bit location, I face the problem.

Data gets written/read from aligned location which I do not want.

How can I use unaligned accesses?

I do not want to use intrinsics to load/store unaligned data.

pseudo code:

int val;

/* a[] is aligned address */

char a[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

char * ptr;

ptr = a + 1;

val = *(int *)ptr;   

expected result is 4321

but I am getting val as 3210?

Is there any option to be set to tell to compiler to use unaligned acess?

  • Mike,

    The 64x+ core does allow non word-aligned accesses.  Just to be clear, let me explain what's going on in your example from the compiler point of view.  I expect that you probably already understand this, but I'll put it here for anyone else that comes upon this thread.

    Assume that the address of a[0] is at 0x4000 for simplicity.  

    In the instruction ptr = a + 1; the pointer gets computed as 0x4001.  It uses the size of a char to calculate the increment in the pointer address.  (If a[] was actually an array of ints, the result of this instruction would actually be 0x4004) You then cast that value to an int pointer (32-bits).  This converts the value back to 0x4000, because that's the 32-bit pointer to that location.  While you can do non-aligned accesses, you can't do so through a 32-bit pointer, because they have to be 32-bit aligned.  So, in summary, char pointers can make byte aligned accesses, short pointers can make 16-bit aligned accesses, and int pointers can only make word aligned accesses. When you do the cast, think of it as a conversion from the 8-bit address that contains a[1] (0x4001) to the 32-bit address that contains a[1] (0x4000).  

    Intrinsics are provided in order to implement functionality in C code that can't be expressed in C.  In effect, the intrinsics are the constructs that tell the compiler to make unaligned access.  I'm curious, why do you say you don't want to use intrinsics?  They are effectively exactly what you are asking for, a way to tell the compiler to make unaligned accesses.

    If you replace your instruction val = *(int *)ptr;   with  val = _mem4(ptr);, you will get the result that you want.

    Regards,
    Dan

     


  • Hello DanRinkes,

    I got it. I really appreciate your help.

    Thank you very much.

    Regards,

    Mike