[Issue 5728] New: "rol" in core.bitop

d-bugmail at puremagic.com d-bugmail at puremagic.com
Thu Mar 10 14:13:39 PST 2011


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

           Summary: "rol" in core.bitop
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: DMD
        AssignedTo: nobody at puremagic.com
        ReportedBy: bearophile_hugs at eml.cc


--- Comment #0 from bearophile_hugs at eml.cc 2011-03-10 14:10:33 PST ---
I'd like the "rol" X86 instrinsic added to core.bitop module.

--------------------

Rationale:

I'd like the intrinsic because rotating bits in a 32 bit number is a common
enough operation (there was recently a thread in D.learn of a person in need
for the fastest rotation of the bytes of a uint), and in C-derived languages
there isn't a bitwise operator for it, while most CPUs have four or more
instructions to rotate a register.

In C-like languages the problem is sometimes solved making the compiler a bit
smarter, but this little test shows DMD is not able to optimize two shifts plus
or into a single rot instruction:

dmd -O -release -inline test.d


int main(string[] args) {
    uint x = args.length;
    return (x << 24) | (x >> 8);
}


__Dmain comdat
    mov EAX,4[ESP]
    mov ECX,4[ESP]
    shl EAX,018h
    shr ECX,8
    or  EAX,ECX
    ret

--------------------

Using inline asm is an option:

int main(string[] args) {
    uint x = args.length;
    asm {
        rol x, 8;
    }
    return x;
}

__Dmain    comdat
    push    EBP
    mov    EBP,ESP
    push    EAX
    mov    EAX,8[EBP]
    mov    -4[EBP],EAX
    rol    -4[EBP],8
    mov    EAX,-4[EBP]
    mov    ESP,EBP
    pop    EBP
    ret

-----------------

But in some cases the inline gives problems, so I think an inlined intrinsic is
more handy, safer, shorter, simpler to use:

union Four {
    uint u;
    ubyte[4] a;
}
void main() {
    Four f;
    asm {
        rol f.u, 8;
    }
}


test.d(8): bad type/size of operands 'f.u'

-----------------

Note: an alternative to the intrinsic is to introduce in D/DMD a standard
syntax for inlined asm expressions using __asm(), as present in LDC, but they
are quite less easy to use than a rot() function-like intrinsic (but they are
much more useful than a single intrinsic):

http://www.dsource.org/projects/ldc/wiki/InlineAsmExpressions

-- 
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