Tech Off Thread

24 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

Compiler Intrinsics with C#

Back to Forum: Tech Off
  • User profile image
    Shining Arcanine

    Earlier today, I was reading about compiler intrinsics and thought that they would be great to use in a method I wrote in C# (as that is the language I'm working on learning). I was very happy as I was expecting a large performance increase from using these so I adapted my C# method to use them:

    Shining Arcanine wrote:
            private static bool trialFactor(UInt64 exponent, UInt64 maxFactor)
            {

                Int16 start;
                _BitScanReverse(start, exponent);

                for (UInt32 x = 3; x < maxFactor; x += 2)
                {

                    UInt64 num = 1;

                    for (Int16 i = start; i > 0; i--)
                    {
                        num *= num;

                        if (_bittest[i] == '1')
                        {
                            num *= 2;
                        }

                        num %= x;
                    }

                    if (num == 1)
                    {
                        return true;
                    }
                }

                return false;
            }


    Then I discovered that they don't work in C# and after a little digging I find that they're exclusive to C++. So I'm left with mainly one question... Why???

    I thought the .NET framework was supposed to make choosing between C#, C++ and Visual Basic irrelevant and unimportant. How can it do that when the C++ guys get the special low level features?

    I'm using Visual C# 2005 Express Edition. Is it possible for me to use inline assembly to do this? I've been reading up on assembly language so if there is a way to do this I should be able to figure it out eventually. I just need to know how to put the assembly into the C# code. Or is this another C++ exclusive feature? If so is there any way I could adapt my method to C++ to get a higher level of performance from the intrinsics and then use it in my C# application?

  • User profile image
    W3bbo

    Shining Arcanine wrote:
    I'm using Visual C# 2005 Express Edition. Is it possible for me to use inline assembly to do this? I've been reading up on assembly language so if there is a way to do this I should be able to figure it out eventually. I just need to know how to put the assembly into the C# code. Or is this another C++ exclusive feature? If so is there any way I could adapt my method to C++ to get a higher level of performance from the intrinsics and then use it in my C# application?


    No.

    Because .NET is cross-platform and not native to any processor architecture nor instruction set, allowing inline ASM in MSIL would break several key points of .NET:

    • Managed Code (you can't really run a garbage checker against pure processor instructions)
    • Cross-platform
    • Code security (buffer-overruns here we come!)

  • User profile image
    W3bbo

    Shining Arcanine wrote:
    Isn't there something known as MASM that you can use? I know of one guy that uses it:

    http://www.virtualdub.org/blog/pivot/entry.php?id=9


    The Microsoft Macro Assembly (MASM) is used to create processor-specific executables and libraries. It is 100% incompatible with .NET.

    Remember, for all intents and purposes, .NET is Microsoft's Java.

  • User profile image
    geekling

    Everything goes down to IL.

    MASM is for C++.

  • User profile image
    Shining Arcanine

    Isn't there something known as MASM that you can use? I know of one guy that uses it:

    http://www.virtualdub.org/blog/pivot/entry.php?id=9

    <rant>What is surprising is that Microsoft goes nuts about breaking webpages to fix incompatibilities with the HTML and CSS specifications when they break program code unrelentlessly to solve the very same problem</endrant>

    I guess I might be expecting too much from the Express Edition in the way of features. I understand that the .NET in some way supports assembly through.

  • User profile image
    Sven Groot

    These are features of the C++ compiler that have nothing to do with .Net.

    Also, I sincerely doubt that you would see any real performance benefit in this case. If you're willing to go that far for performance, why aren't you using << 1 instead of multiplying by 2. Have you actually profiled the code to see where the performance bottlenecks are?

  • User profile image
    Sven Groot

    Also, even in C++/CLI not all intrinsics are supported in managed code. _BitScanReverse is one of them. Even if you use C++/CLI using _BitScanReverse will cause any function that uses it to be compiled to native code instead of IL. Attempting to use these intrinsics with /clr:pure would cause an error.

    And inline assembly isn't even available in C++ if you use the x64 or ia64 compilers.

  • User profile image
    Shining Arcanine

    Wow I didn't expect such quick and prompt replies. I hit the post button before I had finished typing and editing my post.

    Sven Groot wrote:
    These are features of the C++ compiler that have nothing to do with .Net.

    Also, I sincerely doubt that you would see any real performance benefit in this case. If you're willing to go that far for performance, why aren't you using << 1 instead of multiplying by 2. Have you actually profiled the code to see where the performance bottlenecks are?


    Everything I know on this subject is pretty much from PHP (which doesn't translate into much) but when writing PHP scripts I did do some form of profiling where I timed certain stretches of code to figure out what made my scripts run slowly even through I went through every single line looking for optimizations I could make. In short, no I haven't done profiling. I don't quite know how to unless I use the High Performance Timer library I found on codeproject.org but there has to be a better way...

    When I've finished this program it will run in the background for weeks and weeks doing quintillions of calculations (if not more) so I need every ounce of speed I can squeeze out of it. I hadn't thought of using << 1 instead of *= 2. Thanks for the tip.

    Looking over everyone else's replies, I must ask, exactly how do the C++ and the other .NET languages differ? I thought they were the same thing in  that all code went to an intermediate language upon being compiled, then is turned into assembly by the CLR and therefore all of the languages could implement the same exact feature set minus syntax differences. Are things done differently for C++ differently and if so why?

  • User profile image
    Sven Groot

    Shining Arcanine wrote:
    Looking over everyone else's replies, I must ask, exactly how do the C++ and the other .NET languages differ? I thought they were the same thing in  that all code went to an intermediate language upon being compiled, then is turned into assembly by the CLR and therefore all of the languages could implement the same exact feature set minus syntax differences. Are things done differently for C++ differently and if so why?

    There are still differences. Not all .Net languages allow access to all .Net features. VB.NET for instance is the only language that supports conditional exceptions (the When clause on the Try/Catch keywords).

    C++/CLI differs greatly because it's the only .Net language that can mix both IL and native code in the same assembly. You can use the /clr:pure switch to force only IL, but not all C++ constructs can be compiled to IL (those will cause an error if you use them with /clr:pure).

  • User profile image
    Shining Arcanine

    Sven Groot wrote:
    Also, even in C++/CLI not all intrinsics are supported in managed code. _BitScanReverse is one of them. Even if you use C++/CLI using _BitScanReverse will cause any function that uses it to be compiled to native code instead of IL. Attempting to use these intrinsics with /clr:pure would cause an error.

    And inline assembly isn't even available in C++ if you use the x64 or ia64 compilers.


    I realize there is a difference to inline assembly and external assembly (what everyone seems to use) but I'm new to application programming so I didn't think of that when I had mentioned inline. I just wanted to make that part optimal which (is impossible in C# without some kind of native getBit() method and therefore) meant using some form of assembly.

  • User profile image
    W3bbo

    Shining Arcanine wrote:
    I realize there is a difference to inline assembly and external assembly (what everyone seems to use) but I'm new to application programming so I didn't think of that when I had mentioned inline. I just wanted to make that part optimal which (is impossible in C# without some kind of native getBit() method and therefore) meant using some form of assembly.


    For the benefit of any newbies reading this, it also helps not to confuse Assembly "code" with Assembly "file", they're totally different things.

  • User profile image
    Shining Arcanine

    Anyway, is an external .asm file possible by way of a C++ file in my C# application? If I knew a way to do it I could probably write an .asm file to do what I want with all of the stuff I've been reading on assembly.

  • User profile image
    amotif

    Shining Arcanine wrote:

    Then I discovered that they don't work in C# and after a little digging I find that they're exclusive to C++. So I'm left with mainly one question... Why???


    So these things are called "compiler intrinsics," you say, not "CLR intrinsics..." Smiley

    Shining Arcanine wrote:

    I thought the .NET framework was supposed to make choosing between C#, C++ and Visual Basic irrelevant and unimportant. How can it do that when the C++ guys get the special low level features?


    Who said choosing language was irrelevant? If the languages were all the same there would be no need to have three of them. As I recall the C++/CLI compiler guys claim theirs has the best optimizations, including global optimizations.

    Choose your tools according to your needs.

  • User profile image
    Sven Groot

    Shining Arcanine wrote:
    Anyway, is an external .asm file possible by way of a C++ file in my C# application? If I knew a way to do it I could probably write an .asm file to do what I want with all of the stuff I've been reading on assembly.

    You could use a C++/CLI assembly referenced from your C# project which calls into an unmanaged C++ function which uses either inline assembly, the mentioned intrinsics or a call to a function defined in an external library written in assembly. But unless you do the whole thing in C++ the call overhead and crossing the managed/unmanaged boundary is likely to be more expensive than just doing the whole thing in C#.

  • User profile image
    teis

    Sven Groot wrote:
    why aren't you using << 1 instead of multiplying by 2.


    Any decent compiler will make this optimization for you. In fact, as far as I can recall MSVC++ makes this optimization even on debug builds. So you don't get any speed boost from that, only less readable code.

  • User profile image
    Shining Arcanine

    amotif wrote:

    Shining Arcanine wrote:
    I thought the .NET framework was supposed to make choosing between C#, C++ and Visual Basic irrelevant and unimportant. How can it do that when the C++ guys get the special low level features?


    Who said choosing language was irrelevant? If the languages were all the same there would be no need to have three of them. As I recall the C++/CLI compiler guys claim theirs has the best optimizations, including global optimizations.

    Choose your tools according to your needs.


    http://msdn.microsoft.com/msdnmag/issues/04/05/VisualC2005/

    MSDN wrote:
    As for new applications, you might ask why developers uninitiated in the world of .NET choose one .NET-based language over another. There is no straightforward answer as each language has its own strengths, but for pure .NET-based applications, the experience in C#, Visual Basic, and C++ is basically the same. If, as a developer, you are already comfortable in a particular language, there is no significant reason to switch to another one.


    From reading stuff like that I had assumed them to be the same and C# to have the most PHP like syntax and therefore the lowest learning curve. There is also this:

    http://msdn.microsoft.com/vcsharp/learning/default.aspx

    MSDN wrote:
    C# (pronounced C sharp) is a new programming language designed for building a wide range of enterprise applications that run on the .NET Framework. An evolution of Microsoft C and Microsoft C++, C# is simple, modern, type safe, and object oriented.


    You'd think that an evolution of a language wouldn't remove important features.

    I wonder what the chances are that the C# .NET developers will implement C++ like intrinsics into the C# compiler. They would be nice.

  • User profile image
    Shining Arcanine

    Sven Groot wrote:
    Shining Arcanine wrote:Anyway, is an external .asm file possible by way of a C++ file in my C# application? If I knew a way to do it I could probably write an .asm file to do what I want with all of the stuff I've been reading on assembly.

    You could use a C++/CLI assembly referenced from your C# project which calls into an unmanaged C++ function which uses either inline assembly, the mentioned intrinsics or a call to a function defined in an external library written in assembly. But unless you do the whole thing in C++ the call overhead and crossing the managed/unmanaged boundary is likely to be more expensive than just doing the whole thing in C#.


    I guess I'll want to rewrite the entire thing in C++. Hopefully I'll be able to stand the strange alien syntax. Where should I get started?

    teis wrote:
    Sven Groot wrote:why aren't you using << 1 instead of multiplying by 2.


    Any decent compiler will make this optimization for you. In fact, as far as I can recall MSVC++ makes this optimization even on debug builds. So you don't get any speed boost from that, only less readable code.


    At least my code now looks like it will run faster. ^_^;;

  • User profile image
    Sven Groot

    Shining Arcanine wrote:
    You'd think that an evolution of a language wouldn't remove important features.

    C# is not an evolution of C++ in that sense. There are many, many things that C++ can do that C# cannot. The removed parts serve to make C# easier and safer than C++.

    Learning regular C++ can be quite daunting. You'll be getting into the dangerous world of manual memory management. Forget online tutorials, they're mostly crap. Pick up a good book instead (unfortunately I don't have any recommendations).

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.