Does a c/c++ compiler optimize constant divisions by power-of-two value into shifts?
- by porgarmingduod
Question says it all. Does anyone know if the following...
size_t div(size_t value) {
const size_t x = 64;
return value / x;
}
...is optimized into?
size_t div(size_t value) {
return value >> 6;
}
Do compilers do this? (My interest lies in GCC). Are there situations where it does and others where it doesn't?
I would really like to know, because every time I write a division that could be optimized like this I spend some mental energy wondering about whether precious nothings of a second is wasted doing a division where a shift would suffice.