English 中文(简体)
CPU检查 C#
原标题:Checking CPU Popcount from C#

是否有任何人知道如何从C#中检查CPU是否支持人口计算?

I m试图从C++到C#携带一些ches。

问题回答

我尚未找到一种容易发现和使用C#中CPU特别指示的途径。 有几种选择,其中没有一个是冰;

  • asmjit a function that does popcount
  • x86/x64 CPUID in C#
  • mono has a simd library with datatype support (not popcount I guess)
  • Use a C++ DLL (probably way slower because of overhead)
  • ..

我从未走过这一路,并执行了C#人口计。

    /// <summary>
    /// Count the number of bits set to 1 in a ulong
    /// </summary>
    public static byte BitCount(this ulong value)
    {
        ulong result = value - ((value >> 1) & 0x5555555555555555UL);
        result = (result & 0x3333333333333333UL) + ((result >> 2) & 0x3333333333333333UL);
        return (byte)(unchecked(((result + (result >> 4)) & 0xF0F0F0F0F0F0F0FUL) * 0x101010101010101UL) >> 56);
    }

从www.NET Core 3.0开始,您可使用Popcnt.IsSupported测试基本硬件支持。

或者,如果你需要结果,则使用。 <代码>BitOperations类别中的“在基本平台上可得到的硬件内在;否则,它们使用优化的软件反馈”。

自2006年以来 C#编成IL,而不是机器编码,你确实没有CPU级优化。 共同语言操作时间的JIT编辑在守则实际操作时能够做一些优化工作,但无法直接从语言本身进入这一过程。

然而,你可以把C++与管理代码混为一谈,并在当地进行低水平优化,但这种优化打败了转向C#的目的。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签