Converting a uint8_t to single bit increases code size?

This forum is dedicated to software development related to MultiWii.
It is not the right place to submit a setup problem.
Software download
Post Reply
User avatar
Hamburger
Posts: 2578
Joined: Tue Mar 01, 2011 2:14 pm
Location: air
Contact:

Converting a uint8_t to single bit increases code size?

Post by Hamburger »

just curious, when I convert the variable uint8_t line_is_valid to single bit in flags_struct_t f and references turn into f.LINE_IS_VALID, then the size of binary increases. Same effect when applied to uint8_t refresLCD, so probably there is some logic behind this.

Both variables are really only used as boolean toggles, so I thought converting to single bits in the f struct was a memory saver. But on the contrary, with both unit8_t converted to single bits, code size increases by about 60 bytes.

I had expected the effect to be the opposite?

Alexinparis
Posts: 1630
Joined: Wed Jan 19, 2011 9:07 pm

Re: Converting a uint8_t to single bit increases code size?

Post by Alexinparis »

Hi,

When you use a single uint8_t as a boolean, it takes 8 bits as a global var.
When you use the flags_struct f for a new boolean, it takes only 1 bit as global var (you can try to suppress unused boolean inside like those related to GPS if you don't use it)
The arduino IDE can tell you now how many ram is used permanently by global vars.

The counterpart is: checking the state of a single bit in ram for a conditional test can be more complicated than a whole 8 bit var, thus resulting in a flash code increase.
The computation time is also probably a little bit higher.
Another effect: depending on the bit position in the struct, the flash code size varies.
To illustrate this, just move uint8_t CALIBRATE_MAG :1 ; at the beginning of the struct. you will see a code decrease of 10 bytes !
gcc is not smart enough to look at the best optimization.

But, is it worth changing something ? if it's only for 50 flash bytes, I'm not sure because ram size consumption is also important.
First step is probably to find a better boolean order in the struct to save some flash.

Post Reply