In C and C++ std documents notes and examples are non-normative so if they contradict normative text that is a mistake or a defect
-
-
That simplifies to a single SAR instruction on any decent compiler architecture.
-
but signed >> works on most targets and compilers I have used (excepting for MSP430 and AVR and similar), and a lot of code relies on it.
-
>> on signed is implementation-defined, not undefined, and fairly safe to rely upon. << on negative is undefined. Overflow is always undef.
-
When x is signed, x<<n is equivalent to x*(1<<n) except the former is defined on a smaller domain; just use the latter if you care.
-
usually << on signed is functionally equivalent to the unsigned case. some targets also have expensive '*' (if not optimized away).
-
If a compiler can't generate "SHL n" for "*(1<<n)" it's not suitable for real-world use. You write C to the language, not to a bad compiler.
-
I have seen CC's which would fail to optimize this, for targets with neither a "SHL n" op, nor a "MUL" op, so both end up as runtime calls.
-
If this optimization or lack thereof happens at the target backend level rather than on high-level IR, the compiler is hopelessly broken.
- 4 more replies
New conversation -
Loading seems to be taking a while.
Twitter may be over capacity or experiencing a momentary hiccup. Try again or visit Twitter Status for more information.