view tests/mini/bitops.d @ 1499:df11cdec45a2

Another shot at fixing the issues with (constant) struct literals and their addresses. See DMD2682, #218, #324. The idea is to separate the notion of const from 'this variable can always be replaced with its initializer' in the frontend. To do that, I introduced Declaration::isSameAsInitializer, which is overridden in VarDeclaration to return false for constants that have a struct literal initializer. So {{{ const S s = S(5); void foo() { auto ps = &s; } // is no longer replaced by void foo() { auto ps = &(S(5)); } }}} To make taking the address of a struct constant with a struct-initializer outside of function scope possible, I made sure that AddrExp::optimize doesn't try to run the argument's optimization with WANTinterpret - that'd again replace the constant with a struct literal temporary.
author Christian Kamm <kamm incasoftware de>
date Sun, 14 Jun 2009 19:49:58 +0200
parents 1bb99290e03a
children
line wrap: on
line source


extern(C) int printf(char*, ...);

void main()
{
    printf("Bitwise operations test\n");
    {   ushort a = 0xFFF0;
        ushort b = 0x0FFF;
        assert((a&b) == 0x0FF0);
        assert((a|b) == 0xFFFF);
        assert((a^b) == 0xF00F);
    }
    {   ubyte a = 0xFF;
        ulong b = 0xFFFF_FFFF_FFFF_FFF0;
        assert((a&b) == 0xF0);
    }
    {   ushort s = 0xFF;
        assert((s<<1) == s*2);
        assert((s>>>1) == s/2);
    }
    {   int s = -10;
        assert((s>>1) == -5);
        assert((s>>>1) != -5);
    }

    {   ushort a = 0xFFF0;
        ushort b = 0x0FFF;
        auto t = a;
        t &= b;
        assert(t == 0x0FF0);
        t = a;
        t |= b;
        assert(t == 0xFFFF);
        t = a;
        t ^= b;
        assert(t == 0xF00F);
    }
    {   ubyte a = 0xFF;
        ulong b = 0xFFFF_FFFF_FFFF_FFF0;
        a &= b;
        assert(a == 0xF0);
    }
    {   ushort s = 0xFF;
        auto t = s;
        t <<= 1;
        assert(t == (s*2));
        t = s;
        t >>>= 1;
        assert(t == s/2);
    }
    {   int s = -10;
        auto t = s;
        t >>= 1;
        assert(t == -5);
        t = s;
        t >>>= 1;
        assert(t != -5);
    }
    {   struct S
        {
            uint i;
            ulong l;
        };
        S s = S(1,4);
        auto a = s.i | s.l;
        assert(a == 5);
        s.i = 0xFFFF_00FF;
        s.l = 0xFFFF_FFFF_0000_FF00;
        s.l ^= s.i;
        assert(s.l == ulong.max);
        s.i = 0x__FFFF_FF00;
        s.l = 0xFF00FF_FF00;
        s.i &= s.l;
        assert(s.i == 0x00FF_FF00);
    }

    printf("  SUCCESS\n");
}