In the msgpcc (GCC for MSP430 microcontrollers) manual authors wrote:
Use int instead of char or unsigned char if you want a small integer within a function. The code produced will be more efficient, and in most cases storage isn't actually wasted.
Why int
is more efficient?
UPD. And why (u)int_fast8_t
in the mspgcc defined to (unsigned) char
, not (unsigned) int
. As I understand, (u)int_fast*_t
should be defined to the most efficient type with a suffient size.
In general, not necessarily specific to this processor, it has to do with sign extension and masking, requiring additional instructions to faithfully implement the C source code. A signed 8 bit value in a 16 or 32 or 64 bit processor MAY involve additional instructions to sign extend. An 8 bit add on a 32 bit processor might involve extra instructions to and with 0xFF, etc.
You should do some simple experiments, it took a few iterations but I quickly hit something that showed a difference.
produces
The msp430 has word and byte versions of the instructions so a simple add or subtract doesnt have to do the clipping or sign extension that you would expect when using smaller than register sized variables. As a programmer we might know that we were only going to feed sbfun some very small numbers, but the compiler doesnt and has to faithfully implement our code as written, generating more code between sfun and sbfun. It is not hard to do these experiements with different compilers and processors to see this in action, the only trick is to create code that the processor doesnt have simple instructions to solve.
another example
produces