On Thu, 14 Jun 2007 15:41:14 +0200, Bernd Strieder wrote: > Hello, > > Lionel B wrote: > >> Hi, >> >> If x is of (unsigned) integer type of size N then the C++ standard says >> that x << n and x >> n are undefined for n >= N. Is there by any chance >> a GNU extension that guarantees that at least x << N and x >> N are >> defined (and = 0)? > > Most compilers will create code using the assembler codes for shifting > available on the hardware, which usually do something like x << (n%N), > i.e. shifting by a modulus, which is obtained in binary by truncating > the shift value. Anything else would already require testing and > possibly branching by the compiler. I have never programmed on a CPU > where this is different. > > Maybe the branch can be optimized away by means of conditional > statements. You might have to experiment with the architecture and CPU > model options of gcc to get there. > > > #include <stdio.h> > > int main(int argc) > { > int i, j; > for(i = 0; i < 64; ++i) { > j = 1<<i; > if (i & ~31) j = 0; > printf("%d %d\n",i, j); > } > } > > Indeed, when looking at the result of this using gcc-4.1.2 there is no > branch generated for the if to maintain the zero, at least with > -march=i686 or -march=opteron, there is a branch with -march=pentium3. > So double check gcc behaves the same way on your platform, otherwise you > are hunting a phantom problem, and measure, whether on that platform a > branch is preferable.
Thanks, I'm checking that out. It seems it may depend (amongst many other things, no doubt) on the form of test. On my gcc-4.1.1 on athlon 64 (no - march flags, but -O3) I get no branch if I test as in your code, but I do get a branch if I test for i == 32. My assembler is pretty non-existent, though, so take that with a pinch of salt... -- Lionel B _______________________________________________ help-gplusplus mailing list help-gplusplus@gnu.org http://lists.gnu.org/mailman/listinfo/help-gplusplus