I'm not very experienced in the world of C programming, I just worked with arduino before, and I decided to get my feet more wet by doing a project on an ATtiny13 without using the Arduino IDE, only avr-gcc
via cli. Since Flash storage is at a premium, I am exploring various techniques to reduce code size. In my journey I came across multiplications and divisions, and I just became aware of the existance of libgcc
with its various arithmetic routines.
So I have this small test program:
unsigned char x = 200;
void main() {
x = x*2/3;
}
If I look at the map file that is generated, I see that the compiler is adding both __udivmodhi4
and __divmodhi4
to do this calculation, of which the signed division routine __divmodhi4
seems unnecessary. However, if use a short
instead of a char
, as such:
unsigned short x = 200;
void main() {
x = x*2/3;
}
This time I see only __udivmodhi4
being added in the map file, which is what I would expect. I also tried with uint8_t
from inttypes.h
and in that case it is adding back the signed division routine.
Could anyone please explain what is going on here? In all the above-mentioned cases, I would have expected the compiler to only add the unsigned division routine. Thanks!