3 ms·
Does a compiler use special cases for division by two (and other integers as well) that it implements as single bitwise operations? This seems like a lot of wor
by tppiotrowski 2y ago
Does a compiler use special cases for division by two (and other integers as well) that it implements as single bitwise operations? This seems like a lot of work to scan through all mathematical operations. Is that why compiling code takes so long sometimes?
- Jtsummers 2y agoThey can, yes. I recommend playing around with code at https://godbolt.org https://godbolt.org. Here's an example using division: https://godbolt.org/z/xqebM6d11 https://godbolt.org/z/xqebM6d11 That code is simple, just divides the argument by a constant. The linked version divides by 32, note the sar eax, 5 instruction, that's the actual division computation. Play around with other constants and you'll get other results (including multiply/shift instruction combinations for non-power of two constants).