2023-05-15 05:52 AM
I have a lot of different flags in project, I wonder which style definition reads faster by processor .
Definition 1
typedef struct{
__IO uint8_t flag1 ;
. //other flags
}Flag_t ;
Definition 2
typedef struct{
__IO uint32_t flag1 : 1 ;
//other flags
.
}Flag_t ;
Definition 3
typedef struct{
__IO bool flag1
. //other flags
}Flag_t ;
Flag_t Flag;
int main(){
if(Flag.flag1&(bit masking) == SET){
//something..
}
}
2023-05-15 08:39 AM
IMO the use of uint8_t and bool are equivalent and fast. The size of bool is 1 byte. The assembly instruction LDR can load the byte and extend to 32 bits in one operation.
The use of bit fields is slower if you use more than 1 definition in the same int. If you use only 1 bit in the int the compiler may optimize and revert to the uintX_t use.
Compile your code then inspect the ***.list file in Debug folder to see what is generated by the compiler. Most of the time the fewer instructions the faster the code.