Assembler in C# code?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Daniel Gackle

    #1

    Assembler in C# code?

    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=e...%40tkmsftngp02)

    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
  • William Stacey

    #2
    Re: Assembler in C# code?

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

    --
    William Stacey, MVP

    "Daniel Gackle" <dont_spam_dani el@yahoo.com> wrote in message
    news:12b9f57a.0 312180944.2c9a1 0f@posting.goog le.com...[color=blue]
    > 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.
    >[/color]
    (http://groups.google.com/groups?hl=e...elm=eDQxAcx3BH
    A.2224%40tkmsft ngp02)[color=blue]
    >
    > 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[/color]


    Comment

    • n!

      #3
      Re: Assembler in C# code?

      > I found a previous thread in which it was implied that a future[color=blue]
      > version of .NET would support this.
      >[/color]
      (http://groups.google.com/groups?hl=e...elm=eDQxAcx3BH
      A.2224%40tkmsft ngp02)

      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!


      Comment

      • mikeb

        #4
        Re: Assembler in C# code?

        Daniel Gackle wrote:
        [color=blue]
        > 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=e...%40tkmsftngp02)
        >
        > 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.
        >[/color]

        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.

        --
        mikeb

        Comment

        • Pete Davis

          #5
          Re: Assembler in C# code?

          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 Gackle" <dont_spam_dani el@yahoo.com> wrote in message
          news:12b9f57a.0 312180944.2c9a1 0f@posting.goog le.com...[color=blue]
          > 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.
          >[/color]
          (http://groups.google.com/groups?hl=e...elm=eDQxAcx3BH
          A.2224%40tkmsft ngp02)[color=blue]
          >
          > 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[/color]


          Comment

          • Bruno Zarokian

            #6
            Re: Assembler in C# code?

            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("myfa sterfunctions.d ll")]
            public extern double[] MyCalculation(d ouble[] 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.


            Comment

            Working...