C# and my indispensable inline assembly code

T

Teis Draiby

I am about to move my C++ application to C#. Parts of my previous
application consisted of inline SSE2 assembly code optimized for the P4
processor. What options do I have to integrate that part into my C#
application?
There are no such things as intrinsics support in C#, right?

Thank you very much!
regards, Teis
 
B

Bob Powell [MVP]

T

Teis Draiby

-Thank you for your reply!
Any way I reach my beloved assembly is perfect for me. I'm still a C#
novice; is your proposal using a DLL and p/invoke the best and only solution
or are there other options that I could take into consideration?
My SSE2 code handles reatime video so both bandwidth and latency are issues
while communicating with the assembly section.

thank you very much, Teis
 
M

Mark Broadbent

is this really true Bob? Seems odd that Anders would have left that ability
out (cos I'm sure I remember this ability in Delphi -which of course is part
of Anders' background).
Maybe he's done this for code safety/ seperation/ readability?


--

--

Br,
Mark Broadbent
mcdba , mcse+i
=============
 
W

Willy Denoyette [MVP]

But Delphi is unmanaged code, while C# results in managed code (IL) running
in the CLR, where it's translated to machine code by the JITter. The IL
produced by the C# compiler is secure/verifiable code and this is essential
for the language, allowing to embed assembly (unverifiable) would render the
language unverifiable (you don't wan't your assembly code to be translated
int IL do you?).

Willy.
 
T

Teis Draiby

I just miss the possibility in C# (.NET) to specifically target modern
processors supporting SIMD instructions like SSE2 and 3D Now. Nevertheless
inner loops making use of SIMD can boost the process by several times.
Is that completely incompatible with MSIL?

regards, Teis
 
S

Sam Gentile [MCP - C#/.NET]

Yes, the embedding of assembly language would render the assembly (and the
language as a feature) unverifiable. The CLR has absolutely no visibility
into unmanaged code and thus cannot control it's type safety or lifetime.

You could use MC++ which using IJW allows you to mix and match unmanaged and
managed C++ even in the same module but as long as there is assembly
language, the assembly will be non-verifiable.
 
W

Willy Denoyette [MVP]

Teis,

Yes I know, but this is not the level you are programming at when using a
managed language, if you really need this you should stick to (VS C++7.x)
C/C++ where you can mix managed code and unmanaged code (and asm).
As for your SSE2 support, this is up to the CLR (JITter) to decide whether
these are available and use them when appropriate, but again if you want
play with your own toys ;-) just ignore C# for this and use C++.

Willy.
 
G

Guest

----- Teis Draiby wrote: ----
I just miss the possibility in C# (.NET) to specifically target moder
processors supporting SIMD instructions like SSE2 and 3D Now. Nevertheles
inner loops making use of SIMD can boost the process by several times
Is that completely incompatible with MSIL

yes, because that defeats the whole purpose of having a "virtual machine". remember, when you right in .NET, you are writing code for something imaginary, there's no SSE2 or 3D Now in our imaginary world.
 
M

Mark Broadbent

yeah your right, I personally thought it might be nice to be able to embed
code like assembly that would be unmanaged and bypass the CLR (obviously in
the unsafe context) but I guess it is just as easy to import it via a dll
like Bob suggested.

--

--

Br,
Mark Broadbent
mcdba , mcse+i
=============
 
W

William Stacey [MVP]

They could probably hide the steps with sugar, but the steps would be the
same. The unmanaged code would need to be compiled and linked and the
managed code would need a ref to it. They could package that all up and
include the unmanaged dll into the exe like ilmerge does or something to do
everything automatic. Not sure much call for that. I would like to see
simple include of managed dlls into the exe or master dll be really easy and
automatic however.
 
T

Teis Draiby

Thank you for the reply's, they have been very helpfull to get me going. I
am considering whether to use P/Invoke or IJW, both proposed in this thread.
I have yet only found a few comparisons out there (which are contradicting).
What is the best choose due to the requirements of my function, if there are
any big difference at all? I've read that IJW is a bit faster than P/Invoke
and easier to code as well (And it even works).

Function requirements:
The function will be called about 30-60 times per second (The video
framerate)
The function returns a frame for each call (1 to 4 Mb)
The function requres direct access to 0.5 - 1 Gb 'unmanaged' RAM (for
instant access to every frame)
Since it operates on realtime video input latency is an issue.


Thank you very much, Teis
 
S

Sam Gentile [MVP - C#/.NET]

Both IJW and P/Invoke (and COM Interop) undergo what is known as "Transition
Thunks" between managed and unmanaged code. This is a set of assembly
language instructions that switch the stack and such. Don and I documented
in 2000 or 2001 in some DM list and it's in Don's book to some extent.
Marshaling is also done here and the base cost of the transition thunk goes
up with marshalling non-isomorphic types. IJW's transition is more efficient
as it is able to optimize for the C++ to C++ case. So there is not a
universal answer but in your situation using IJW will be faster. As long as
you are unmanaged C++ to managed C++, IJW will always be faster. I verified
this with the C++ team.
--
----------------------------------
Sam Gentile
MVP - C#/.NET
INETA Speaker http://www.ineta.org/DesktopDefault.aspx
Read my blog at http://samgentile.com/blog/
 
T

Teis Draiby

Thanks for the detailed reply and book recommandations. Now I got enough
stuff to keep myself occupied for a while, I guess.

regards, Teis
 
G

Guest

Hi

Another option besides using PInvoke with a standard DLL, is to create a VC++ managed assembly.

Check out managed extensions for VC+

Hope this helps

Eddi

----- Teis Draiby wrote: ----

I am about to move my C++ application to C#. Parts of my previou
application consisted of inline SSE2 assembly code optimized for the P
processor. What options do I have to integrate that part into my C
application
There are no such things as intrinsics support in C#, right

Thank you very much
regards, Tei
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top