typecast arm problem

Note: This was originally posted on 29th July 2009 at http://forums.arm.com

Hi


I use eclipse+yagarto+the exemple getting-started-project-1.3-at91sam7x-ek or RTOS "ARM7_AT91SAM7X256_Eclipse". I have a problem when I do this
char var1[2];
int short var2 = 0x1234;

*(int short*)var1 = var2

When I do the typecast the arm stop working if the address is odd. Can you tell me why the arm his not able to do that and how I can solve my problem, because with the Rabbit I was able to do that. I know I can use memcpy, but with this cast I save some CPU time.
Parents
  • Note: This was originally posted on 29th July 2009 at http://forums.arm.com

    This isn't valid C code - which is why it breaks.

    The C compiler will (correctly) assume natural alignment of types and generates load and store instructions accordingly. In your case the compiler will generate LDRH and STRH (load and store halfword) instructions when accessing the buffer through the "short" pointer - but these instructions are not valid if the memory is only byte aligned. You will get a data abort in this case.

    You have a few choices:

    (1) You can use a compiler-specific extension to mark the memory buffer as packed (which will cause an ARM7 to generate byte loads and assemble the result).

    (2) Memcpy - which is probably as fast as the above anyway because compilers are pretty good at optimizing short copies. And it's portable across multiple compilers because it is compliant C code.

    (3) Ensure data is properly aligned in the first place - which is typically the highest performance solution.

    Later ARM cores, such as ARM11 and Cortex-A include unaligned load /store support, so your example will work. However, you still have to be aware that the code is not really valid C and as such is prone to portability issues.
Reply
  • Note: This was originally posted on 29th July 2009 at http://forums.arm.com

    This isn't valid C code - which is why it breaks.

    The C compiler will (correctly) assume natural alignment of types and generates load and store instructions accordingly. In your case the compiler will generate LDRH and STRH (load and store halfword) instructions when accessing the buffer through the "short" pointer - but these instructions are not valid if the memory is only byte aligned. You will get a data abort in this case.

    You have a few choices:

    (1) You can use a compiler-specific extension to mark the memory buffer as packed (which will cause an ARM7 to generate byte loads and assemble the result).

    (2) Memcpy - which is probably as fast as the above anyway because compilers are pretty good at optimizing short copies. And it's portable across multiple compilers because it is compliant C code.

    (3) Ensure data is properly aligned in the first place - which is typically the highest performance solution.

    Later ARM cores, such as ARM11 and Cortex-A include unaligned load /store support, so your example will work. However, you still have to be aware that the code is not really valid C and as such is prone to portability issues.
Children
No data
More questions in this forum