struct definition for ColdFires / Codewarrior

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

struct definition for ColdFires / Codewarrior

3,079 Views
Ricardo_RauppV
Contributor I
 
I cannot create correctly a structure with odd number of bytes, for example, a struct containing a longint(32b) and a char(8b)  should result a 5 bytes size right?Or a shortint(16b)  and a char(8b)
should result a 3 bytes size..
Codewarrior generates a 6 bytes size and 4 bytes size, respectivelly...
For only chars member it works, but if I mix int/long etc...booo..
Couls someone help me?
 
Ricardo Raupp
Labels (1)
0 Kudos
4 Replies

503 Views
Ricardo_RauppV
Contributor I
More info I received from Wildrice forum
 
Peter Barada wrote:
>>I tested it by checking its size , behavior , etc..and it is OK ...
>>Now I can create any kind of structure...it's very
>>confortable...specially if you have already applications made based on
>>structures ..
>
>Remember that you're making a trade here.  Since the "#pragma packed"
>tells the compiler to ignore the alignment of bothe the structuer, and
>its members, then any access to a member requires that it is loaded
>into registers using bytes, so if you have an int, the compiler will
>emit four byte loads as well as three shifts to load the value.  The
>same for a store.  This can dramatically increase both the size of the
>code, as well as the execution time.
>
>As an example, gcc-3.4.3 compiles the following code for -m5307:
>
>struct foo {
>  int x;
>  char y;
>} __attribute__ ((packed));
>
>int bar(struct foo *p)
>{
>  return p->x;
>}
>
>Into (removing the fuction prologue/epilogue code):
>
>move.b (%a0),%d1
>moveq #24,%d0
>lsl.l %d0,%d1
>move.b 1(%a0),%d0
>swap %d0
>clr.w %d0
>and.l #16711680,%d0
>or.l %d1,%d0
>move.b 2(%a0),%d1
>lsl.l #8,%d1
>and.l #65280,%d1
>or.l %d0,%d1
>clr.l %d0
>move.b 3(%a0),%d0
>or.l %d1,%d0
>
>So to access member 'x' from the 'foo' structure whose address is in %a0,
>leaving it in %d0 while using %d1 as a temporary takes 15 instructions
>consuming 48 bytes instead of one instruction that consumes 2 bytes,
>or 15 times as slow, and 24 times as big.
>
>Just some food for thought.
>

While that is something you don't want normally, it's very nice that gcc
will make the correct code when you *do* need it.  Occasionally you meet
a structure that is defined externally and doesn't fit the proper
alignments, making the "packed" attribute very handy.  If only there
also was a "little-endian" attribute (Diab Data's compiler has that
feature, IIRC).
 
and ...
 
 
You are right. But sometimes you have to live with it. For example, accessing SCSI structures or FAT12/FAT16/FAT32 structures from your application requires the struct fields not to be aligned.   
True, but one possiblity is to extract from the unaligned struct theinformation you need just once, and the refer to that information viaaligned variables.  This can reduce both execution time *and* codespace... 
For small pieces of code, yes. But try to write a full file system this way...



 
0 Kudos

503 Views
Arev
Contributor III
Hello,
 
If you want byte aligment, include the following pragma :
 
#pragma options align=packed
Bye
 
0 Kudos

503 Views
Ricardo_RauppV
Contributor I
Hi Folks.....specially Arev ...::smileyhappy::smileyhappy:
 
Yesterday I received a tip from a guy (Chris - the Nop Head...) from wildrice forum with a clue for it.
I spent yesterday to test it in order to check if besides the size I hoped it is used as it shoud be.
Well ..it is working !!
Now I can create any struct size/format I want.
The solution he gave me is exactly the one Arev showed...
that is .. :
#pragma options align=packed
 
In order to add info to this issue (maybe usefull for other people), follow bellow the Chris comment about it, wich culminated to Arev´s   clue.
Very Thanks everybody !!!!!::
 
Hi Ricardo,
 I am not a Codewarrior expert having only used it for a few days but according to the documentation for the verson I have to hand it only supports #pragma pack on x86 and MIPS targets but it does have:
      #pragma options align=packed
which comes with the description "Aligns every field on a 1-byte boundary. It is not available in any panel. This alignment causes your code to crash or run slowly on many platforms. Use it with caution".

You can use:
     #pragma options align=reset
to restore normal aligment.

There is also a "Struct Alignment" option on the "Target Setting Panel / Code Generation / ColdFire Processor" dialog but that only supports 68K (which will be 16 bit alignment) and 68K 4-byte. If you are using a 32 bit data bus you should set it to "68K 4-byte" to get maximum speed performance. If want smallest memory usage or have a 16 bit data bus you should set it to 68K. You can then use the pragmas around specific structure definitions to force them to be packed. You will get a speed reduction if you have 32 bit values that are not aligned on multiples of the data bus size.

Hope this helps, I have not tried it myself but I have done it with gcc and __attribute__ ((packed)).

Regards, Chris
 
 
0 Kudos

503 Views
Nouchi
Senior Contributor II
hello,

This behavior is normal for the ColdFire family, all datas access are aligned as the operand size.
So a short int have to be aligned on even addresses, and long int have to be aligned on modulo 4 addresses.
The compiler warns you, when it adds a padding byte.


Emmanuel
0 Kudos