ldc -O leads to wrong results

Dan Olson via digitalmars-d-ldc digitalmars-d-ldc at puremagic.com
Mon Mar 9 22:35:58 PDT 2015


"salsa" <salsa at salsa.com> writes:

> Recently I got stuck with this:
> compiling following code with ldc -O generates code that doesn't do
> what I expect it to do. Compiling it without optimizations works
> fine. Does somebody have any idea what the problem might be? To
> reproduce the bug just compile and run once with 'ldc *.d' and once
> with 'ldc -O *.d'...
>
> 			x[ 4] ^= rotl((x[ 0]+x[12]), 7);
...
> 	final static T rotl(T)(T x, T y) pure nothrow @nogc
> 	{
> 		return (x << y) | (x >>> -y);
> 	}

Hi salsa.

y is 7 so (x >>> -7) yields an unspecified value because shift is
negative (see TDPL 2.3.10 Shift Expression).  I think results are only
defined when shifting 0 to nbits-1.

Here is one way to get it to work with -O:

 	final static T rotl(T)(T x, uint y) pure nothrow @nogc
 	{
            enum nbits = T.sizeof*8;
            return (x << y) | (x >>> nbits-y);
 	}

-- 
Dan


More information about the digitalmars-d-ldc mailing list