[Issue 6829] Unsigned rotate standard function in Phobos

d-bugmail at puremagic.com d-bugmail at puremagic.com
Fri Jul 12 02:00:17 PDT 2013


http://d.puremagic.com/issues/show_bug.cgi?id=6829



--- Comment #24 from bearophile_hugs at eml.cc 2013-07-12 01:59:52 PDT ---
(In reply to comment #23)
> and 5. Make sure that you use bearophiles last implementation example. ;)

Updated code, lacks unittests:


import std.traits: isIntegral, isUnsigned;

/// Left-shift x by n bits.
T rol(T)(in T x, in uint nBits) @safe pure nothrow
if (isIntegral!T && isUnsigned!T)
in {
    assert(nBits < (T.sizeof * 8));
} body {
    return cast(T)((x << nBits) | (x >> ((T.sizeof * 8) - nBits)));
}

/// Right-shift x by n bits.
T ror(T)(in T x, in uint nBits) @safe pure nothrow
if (isIntegral!T && isUnsigned!T)
in {
    assert(nBits < (T.sizeof * 8));
} body {
    return cast(T)((x >> nBits) | (x << ((T.sizeof * 8) - nBits)));
}

void main() {
    // Tests to check for assembly output.
    {
        __gshared static ubyte xb;
        __gshared static ushort xs;
        __gshared static uint xi;
        __gshared static ulong xl;
        __gshared static uint yi;

        rol(xb, yi);   // rolb
        ror(xb, yi);   // rorb

        rol(xs, yi);   // rolw
        ror(xs, yi);   // rorw

        rol(xi, yi);   // roll
        ror(xi, yi);   // rorl

        rol(xl, yi);   // version(X86_64) rolq
        ror(xl, yi);   // version(X86_64) rorq
    }
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list