Indeed, it is. But maybe some additional information about why this is the solution and why this isn't a bug but caused by design should be provided:
Per default, mspgcc padds structs to even size (or padds elements inside the structs so any short or long int inside the struct is word-aligned) and also places structs onto even addresses. So the compiler always assumes structs as well as ints inside a struct to be word-aligned. The reason is that the MSP does not support word access to an odd address. If you try (you can do so, e.g. by accessing a memory location indirectly through a register with an odd value, which the compiler cannot know at compiletime), the processor will simply ignore/clear the LSB of the address. Which of course does the wrong thing. Anyway, the usage of word access is necessary to reach optimum performance. You cannot generally use byte access jsut to circumvent the case that the programmer for some reason interferes manually with the alignment and presents misaligned data. It would cut down handling of this data by 50% of the optimum speed. So the key is to tell the compiler that a certain struct is NOT aligned. This is done by declaring the struct as packed. Then the compiler always uses byte access to the struct members and never uses word access. With all the impact on the speed. This is done by adding the packed attribute to the struct definition. This will, however, not allow misaligned access to int members inside the struct. I had a similar problem when passing data packets of different sizes (and not necessarily even) through a RF stream. Deliberate padding wasn't an option as the data was crossing to different hardware and bandwidth was limited. My solution was to implement all non-char members as char arrays and extracting the data through some macros. Not the fastest solution, but safe for optimisation and the headers/definitions are compatible across platforms. JMGross ----- Ursprüngliche Nachricht ----- Von: Sergey A. Borshch An: GCC for MSP430 - http://mspgcc.sf.net Gesendet am: 17 Nov 2010 20:47:43 Betreff: Re: [Mspgcc-users] optimizations and (mis)alignment of instruction operands > void *packetbuf_dataptr(void); > __attribute__((packed)) > struct cxmac_hdr { > uint8_t dispatch; > uint8_t type; > }; That's all you need. ------------------------------------------------------------------------------ Increase Visibility of Your 3D Game App & Earn a Chance To Win $500! Tap into the largest installed PC base & get more eyes on your game by optimizing for Intel(R) Graphics Technology. Get started today with the Intel(R) Software Partner Program. Five $500 cash prizes are up for grabs. http://p.sf.net/sfu/intelisp-dev2dev _______________________________________________ Mspgcc-users mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/mspgcc-users
