First I feel I must defend myself. I know I should probably not worry about this kind of thing, premature optimization and what-not. I know. I am asking this purely because I’m curious and couldn’t (or don’t know how) find the solution myself.
Is it common practice for compilers to optimze away constant integer division? Something like this:
const int FOUR = 4;
const int TWO = 2;
int result = FOUR / TWO;
Optimized to be:
const int FOUR = 4;
const int TWO = 2;
int result = 2;
Edit: I’m very much aware that the answer varies from compiler to compiler, I’m mostly curious if its common practice.
Yes it is virtually universal practice, in fact if your compiler doesn’t do it you have a very unusual compiler indeed.