Assembler in C# code?

  • Thread starter Thread starter Daniel Gackle
  • Start date Start date
D

Daniel Gackle

Will Whidbey will include the ability to put inline assembler into C#
code?

I found a previous thread in which it was implied that a future
version of .NET would support this.
(http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&selm=eDQxAcx3BHA.2224@tkmsftngp02)

My team is working on a program for defining and executing complex
economic calculations. We love C# but there are a number of
performance-critical sections that we would write in assembler if we
could.

Best,
Daniel Gackle
 
As even the assembler would be converted to IL then converted to native exe,
not sure how much you could save over a fast c++ or c# routine. Maybe you
could do it in IL (never done this, but I think its possible.)
 
I found a previous thread in which it was implied that a future
version of .NET would support this.
(http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&selm=eDQxAcx3BH
A.2224%40tkmsftngp02)

My guess is that he was thinking of inlined 'IL' assembly rather than
inlined native assembly (though, of course, that's just my opinion).
One of the reasons for IL is so that the JIT and CLR can validate the
operation of the code, native assembly goes against this (or, at least,
makes it much more difficult).

Not only that, but by inlining native assembly, you will circumvent future
optimisations a JITter may make. As the JIT can be updated to support new
instructions within future processors you may end up with slower code on
some machines. I won't go further into these reason in case you didn't
actually mean inlined *native* assembly.

I see no reason why inlined IL assembly would be impossible, but then I
don't see the need to do that :)

n!
 
It's actually fairly trivial to handcode IL, if that's your intention, and
assemble and link it into your app using al.exe.

If you really want the speed of native code, then you need to do unmanaged
code. Wrapping it in a COM object makes it easy to use from C#.

Pete
 
Daniel said:
Will Whidbey will include the ability to put inline assembler into C#
code?

I found a previous thread in which it was implied that a future
version of .NET would support this.
(http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&selm=eDQxAcx3BHA.2224@tkmsftngp02)

My team is working on a program for defining and executing complex
economic calculations. We love C# but there are a number of
performance-critical sections that we would write in assembler if we
could.

I haven't heard any rumors of inline asm being added to C#, and I'd be
surprised if it ever did get added. I'm not sure I'd read into Eric
Gunnerson's response the idea that it's planned (and the other response
in that thread was from someone who has no ties to Microsoft as far as I
can tell).

However, you can use the C++ compiler to build a managed assembly that
also contains unmanaged native code. In general, the managed code can
call the unmanaged code and vice-versa (you may see this called
"It-Just-Works" or IJW). So, you could use Managed C++ to compile a
module or assembly that contained a function that had inline asm, and
call it from your C# code.

Of course, this will mean that your application needs to be run in a
security context that allows unsafe code, so you need to understand what
that will do to your installation/deployment scenario.
 
One straight path, if you really do need to write some FPU assembler code,
is to put it in a regular DLL, and do a DLL import in your C# frontend :

[DllImport("myfasterfunctions.dll")]
public extern double[] MyCalculation(double[] myNumbers);

Btw, if you don't plan / can't use the SSE/SSE2/3DNow instructions in your
assembly code, a C++ compiler will probably do a better job than a manual
assembler implementation (the Intel compiler does wonders). It's getting
hard today, with all the prefetching/segmenting/whatever mecanisms of the
CPUs to beat the compilers on sequential coding. Of course, if your code can
be parallelized, some manual coding with the help of the SSE/SSE2/3DNow
instructions might almost double/quadruple the speed.
 
Back
Top