On 05/27/2012 05:55 PM, Steven M. Schweda wrote: > it's not immediately obvious (to me) that the > code for two shifts and an OR will necessarily be more efficient (in > space or time) than the code for an assignment
Any decent compiler will generate exactly the same
code for both. No shifts or OR will be done at
runtime -- they'll all be done at compile time.
For example:
#include <limits.h>
unsigned long int x;
unsigned long int y;
void f (void) {
unsigned long int a = 0x87fffffe;
unsigned long int b = 0x07fffffe;
if (ULONG_MAX >> 31 >> 31 == 3)
x = a << 31 << 1 | b;
else {
x = a;
y = b;
}
}
void g (void) {
#if ULONG_MAX >> 31 >> 31 == 3
x = 0x87fffffe07fffffe;
#else
x = 0x87fffffe;
y = 0x07fffffe;
#endif
}
This generates exactly the same machine code for 'f' as for 'g'
on both x86-64 and x86 (with GCC 4.7.0 -O2).
> It's not absolutely clear that "Compaq C V6.4-005 on OpenVMS VAX
> V7.3" qualifies as "modern"
It's a little long in the tooth, as it was released over
ten years ago. But you can try it out on the above code;
by compiling with optimization enabled, and
looking at the resulting assembly language code.
