Use unsigned types whenever you're doing bitwise arithmetic. Then it's just -(x>>31).
-
-
Replying to @RichFelker @cr88192 and
That simplifies to a single SAR instruction on any decent compiler architecture.
1 reply 0 retweets 1 like -
Replying to @RichFelker @oshepherd and
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.
1 reply 0 retweets 0 likes -
Replying to @cr88192 @oshepherd and
>> on signed is implementation-defined, not undefined, and fairly safe to rely upon. << on negative is undefined. Overflow is always undef.
1 reply 0 retweets 0 likes -
Replying to @RichFelker @cr88192 and
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.
1 reply 0 retweets 0 likes -
Replying to @RichFelker @oshepherd and
usually << on signed is functionally equivalent to the unsigned case. some targets also have expensive '*' (if not optimized away).
1 reply 0 retweets 0 likes -
Replying to @cr88192 @oshepherd and
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.
1 reply 0 retweets 1 like -
Replying to @RichFelker @oshepherd and
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.
1 reply 0 retweets 0 likes -
Replying to @cr88192 @oshepherd and
If this optimization or lack thereof happens at the target backend level rather than on high-level IR, the compiler is hopelessly broken.
1 reply 0 retweets 1 like -
Replying to @RichFelker @oshepherd and
I am thinking it is going pretty ok if the CC's work and accept modern'ish C syntax (luckily K&R only CC's are pretty much dead now...).
2 replies 0 retweets 0 likes
You're a decade or two late on these discussions...
-
-
Replying to @RichFelker @oshepherd and
old stuff lingers for a while, it seems only pretty recent that using GCC/etc became semi-universal (vs LCC variants and one-off compilers).
1 reply 0 retweets 0 likes -
Replying to @cr88192 @RichFelker and
actually, FWIW, in a class I am taking there are still some machines around using orange-on-black CRTs and floppy drives. epic new tech...
0 replies 0 retweets 0 likes
End of conversation
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.