On Fri, Oct 25, 2002 at 12:26:21PM -0700, Dave Parker wrote:
> So, for example, if you have a structure made up of 4 bytes, the C compiler
> will assign it with a single MOVE.L, but the C++ compiler will assign it
> with 4 MOVE.B instructions.
[...]
> typedef struct
> {
> UInt8 red;
> UInt8 blue;
> UInt8 green;
> UInt8 unused;
> } RGB;
>
> static void CopyRGBs(RGB *pRGBSrc, RGB *pRGBDst, int count)
> {
> int i;
> for (i = 0; i < count; i++)
> *pRGBDst++ = *pRGBSrc++;
> }
Your example brings up another interesting consideration.
Pop quiz: What alignment do you think this RGB struct requires? Under
what circumstances is the desired optimization even valid?
GCC notes that all the fields of the struct need not be aligned at
all, and gives the struct the same flexibility: arbitrary alignment.
Therefore it must generate the 4*MOVE.B code for CopyRGBs, because it
cannot assume that pRGBDst and pRGBSrc are even.
If you want your RGB type to have greater alignment, you can tell GCC
so either by making RGB a union that includes a field with the desired
alignment (e.g. a UInt16 or UInt32 on m68k, or a UInt32 on ARM) alongside
the existing struct, or by using GCC's "aligned" attribute (e.g. with 2
or 4) on the struct in your typedef. Then GCC will have grounds for
assuming evenness in CopyRGBs, and will generate the desired MOVE.L
code -- in both C and C++.
OTOH CodeWarrior, given the code as written above, ascribes an *even
alignment* requirement to RGB, even though it has no fields with such a
requirement. (Perhaps the idea is that it reckons that RGB wants that
alignment simply by virtue of its "aggregateness"?) Consequently CW can
always assume evenness and generate the desired MOVE.L code for CopyRGBs
-- modulo the C++ compiler's inability to take advantage of the as-if
rule (C++ 1.9 [intro.execution]) here :-).
I've been unable to find an explanation or even a description of this
alignment inflation in the CodeWarrior manuals. (Ben? :-))
John
--
For information on using the Palm Developer Forums, or to unsubscribe, please see
http://www.palmos.com/dev/support/forums/