A.6.4 Allocating Bit Fields

To set members of different bit fields, the data including the bit field needs to be accessed each time. These accesses can be kept down to one access by collectively allocating the related bit fields to the same structure.

[Example]

An example in which the size is improved by allocating bit fields related to the same structure is shown below.

Source code before improvement

struct str
{
     Int flag1:1;
}b1,b2,b3;
void func()
{
     b1.flag1 = 1;
     b2.flag1 = 1;
     b3.flag1 = 1;
}

 

Assembly-language expansion code before improvement

_func:
     MOV.L #_b1,R5
     BSET #00H,[R5]
     MOV.L #_b2,R5
     BSET #00H,[R5]
     MOV.L #_b3,R5
     BSET #00H,[R5]
     RTS

 

Source code after improvement

struct str
{
     int flag1:1;
     int flag2:1;
     int flag3:1;
}a1;
void func()
{
     a1.flag1 = 1;
     a1.flag2 = 1;
     a1.flag3 = 1;
}

 

Assembly-language expansion code after improvement

_func:
     MOV.L #_a1,R4
     MOVU.B [R4],R5
     OR #07H,R5
     MOV.B R5,[R4]
     RTS