Inline ASM in D

Frits van Bommel fvbommel at REMwOVExCAPSs.nl
Sun Apr 27 07:41:21 PDT 2008


Sascha Katzner wrote:
> Walter wrote:
>> Inline assembler can only appear within functions.
> 
> Is there any chance that you re-(think/work) this in the future?
> 
> I've just written:
> 
> uint reverseUint(void* source) {
>     asm {
>         naked;
>         mov    EAX, [EAX];
>         bswap    EAX;
>         ret;
>     }
> }
> 
> ...and it strike me, that it would be *really* nice if the compiler 
> would inline functions like that, either manually or automatically.

That would be why both Phobos and Tango have "uint bswap(uint)" (Phobos 
in std.intrinsic, Tango in tango.core.BitManip). It's a compiler 
intrinsic, meaning it should compile down to the 'bswap' asm instruction 
on x86.
And it does, at least on DMD. Unfortunately GDC appears to only provide 
a portable implementation with bitwise operators, not using the raw 
instruction.
Though on GDC it *should* be possible to get the same effect by using 
the "Extended Assembler" syntax, which according to the documentation[1] 
doesn't prevent inlining.


[1]: See <http://dgcc.sourceforge.net/gdc/manual.html>, about halfway 
through.



More information about the Digitalmars-d mailing list