Search code examples
c#.netsimdcil

Are there SIMD instructions in CIL?


When we write in C (or C++) we can inline assembly code with _asm. This allows us to optimize a small highly used portion of our program to take advantage of assembly SIMD instructions. Most of times we won't need it but, for example, video processing, encryption/decryption and mathematical algorithm will take a great advantage of them. In certain situations some compilers (I'm sure about gcc) can even use SIMD instruction for plain code so we'll use them even when we're not aware of their existance.

From my naive point of view to support them in CIL should be pretty easy (falling back to a software implementation in the rare cases they're not supported). Different story is to expose them in C# (for example) but for such specialized feature even a msil block (similar to _asm) would work great.

So my question is: In ECMA 335 I didn't find anything but is there any (documented or undocumented) support for SIMD instructions in MSIL?

I don't consider their use in what JIT compiler produces but a mechanism to explictly use them in my .NET applications. Of course I can write a C++/CLI assembly with all these algorithms and simply use them in my .NET application but I'd avoid a mixed mode assembly to keep application completely verifiable (and to do not ship different assemblies for different architectures).

Even most most dirty trick is welcome because it has to be used in very few specialized functions.

I'm aware of existance of Microsoft SIMD-enabled Vector Types, they're a great thing but (1) I'm not looking for an external library and I absolutely don't want to deploy a beta JIT, (2) they target Windows 8 64 bit only, (3) so far SIMD support is pretty small (from my POV if I need this then I want it all, dirty and fast).


Solution

  • I'm afraid there's nothing at the CIL level.

    As you noted, Microsoft is moving forward with SIMD support for a future version of .NET.

    If you can use Mono, it has had some SIMD support for a while (currently incompatible with the Microsoft.Bcl.Simd API.

    Both .NET and Mono share a common approach here, with JIT intrisics with a standard IL implementation.