Problem with structure bit fields

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

Problem with structure bit fields

2,333 Views
tara
Contributor I
Greetings,
 
The down piece of code builds under wind river diab compiler, the same does not happen in Code Warrior as it does not support the "maxalign" pragma. Please find the attachment which details the maxalign pragma functionality for reference.
I am pointing this structure to a series of registers and checking for the bits through these bit fields registers (mystruct).
With Diab build everything works fine and when I port the same code in Code Warrior, It does not build. When I remove the maxalign pragma to make it build the code does not work as required.
 
Is there any exact/proper replacement for the #pragma maxalign in Code Warrior?
or Can you please send me a sample code which uses bit fields to access bits in a register.
 
 
 
/****************************************/
#pragma pack(1)
#pragma maxalign(1)
 typedef struct
 {
   ungined char aa   : 1;
   ungined char bb   : 1;
   ungined char cc   : 6;
   ungined char dd   : 8;
   ungined char ee   : 1;
   ungined char ff    : 2;
   ungined char gg  : 1;
   ungined char hh  : 4;
 } mystruct;
#pragma maxalign()
#pragma pack()
 
/****************************************/
 
Best regards
Tara
 
 
Labels (1)
0 Kudos
1 Reply

548 Views
RichTestardi
Senior Contributor II
The problem may be structure alignment (i.e., the padding between multi-byte values within a structure), or it may be that the order that bit fields are allocated is not consistent across compilers, nor is the endianness of multi-byte values (if you are switching processors as well as compilers).
 
Since you have no structure members that are larger than a byte below, I'd suspect it is bit field ordering that might be the real issue -- some compilers allocate bits starting at the right and some at the left; some pad and some do not.  Usually you have to look up the details of each compiler and adjust your structure definition (with #ifdef's) to get the behavior you want.
 
CW supports the (standard) #pragma pack which might do what you want.
 
See: CodeWarriorTM Development Studio ColdFire® Architectures Edition Build Tools Reference v7.x
 

pack

Stores data to reduce data size instead of improving execution performance.

Syntax

#pragma pack()

#pragma pack(0 | n | push | pop)

n

One of these integer values: 1, 2, 4, 8, or 16.

Remarks

Use this pragma to align data to use less storage even if the alignment might affect

program performance or does not conform to the target platform’s application

binary interface (ABI).

If this pragma’s argument is a power of 2 from 1 to 16, the compiler will store

subsequent data structures to this byte alignment.

0 Kudos