Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.

Channels ▼


64-Bit SIMD Code from C#

After much pleading from C# developers, Microsoft has finally introduced a new .NET JIT compiler, codenamed RyuJIT, that adds support for Single Instruction Multiple Data (SIMD) operations with vector types when compiling for x64. In this article, the first in a two-part series, I provide all the necessary steps to install and configure the new JIT compiler and additional packages to generate SIMD instructions from C# code.

The New JIT Compiler for .NET

A few years ago, I wrote a brief post explaining why high-level programming languages such as C# needed to allow developers to take advantage of SIMD instructions. Before RyuJIT, if you wanted to use SIMD instructions in C# without using libraries written in other programming languages such as C++, you had to work with Mono, which provided the Mono.Simd namespace since its 2.2 release.

RyuJIT is the codename for a new 64-bit JIT compiler for .NET. The codename is the result of joining the words Ryu (Ryū Japanese for dragon) and JIT. If you want to know why a dragon is closely related to a compiler, consult the Dragon Book. RyuJIT provides many optimizations in the generated x64 code (compared with the existing JIT compilation) that makes .NET applications start and run faster. One of these features is the ability to emit SIMD instructions.

At the time of writing, the latest version of RyuJIT is Community Technology Preview 4, also known as RyuJIT CTP4. The initial RyuJIT CTP versions worked only with 64-bit editions of Windows 8.1 or Windows Server 2012 R2. Luckily, RyuJIT CTP4 added support for Windows Vista, 7, 8, and Windows Server 2008/2008R2. However, if you want to work with operating systems supported by the newest version, you need to install Microsoft .NET Framework 4.5.2.

RyuJIT is capable of emitting SIMD instructions included in the Streaming SIMD Extensions 2 (SSE2) instruction set. To support the generation of more powerful SIMD instruction sets, such as Advanced Vector Extensions (AVX), there are additional changes that should be included in a future .NET Framework release. According to the information provided by the RyuJIT team, the final release will be able to generate AVX instructions.

If you install and activate RyuJIT, the Microsoft SIMD-enabled vector types will permit you to generate SIMD instructions by using the vector types included in the System.Numerics namespace. Unfortunately, these steps are more complex than you might expect, so I'm going to provide detailed instructions.

Installing and Activating RyuJIT

You can install RyuJIT without generating undesired side effects in your existing Visual Studio 2013 and .NET Framework installations. There is a mechanism to activate and deactivate the RyuJIT,so you can easily go back to the original JIT, if you want to.

First, if you aren't working with either Windows 8.1 or Windows Server 2012 R2, don't forget that you must install Microsoft .NET Framework 4.5.2. Then, download the RyuJIT installer (RyuJIT-CPT4.msi) from http://aka.ms/RyuJIT and execute it. Once you finish installing RyuJIT, the managed 64-bit applications will still continue using the previous JIT because you have to activate RyuJIT.

You can run a few commands to activate RyuJIT and enable SIMD instructions per user, or set temporary environment variables to activate these features in the command line before you run an executable. If you decide to activate RyuJIT per user, all managed 64-bit applications will use RyuJIT while it's activated.

You need to execute the following commands to add the necessary registry entries that activate RyuJIT and enable SIMD instructions in .NET Framework for the current user:

reg add HKCU\SOFTWARE\Microsoft\.NETFramework /v AltJit /t REG_SZ /d "*" /f /reg:64 > NUL
reg add HKCU\SOFTWARE\Microsoft\.NETFramework /v FeatureSIMD /t REG_DWORD /d 1 /f /reg:64 > NUL 

After you execute the two commands, the following two keys and values will be added to the registry:

  • HKEY_CURRENT_USER\Software\Microsoft\.NETFramework\AltJit: A string key (REG_SZ) with the value set to *
  • HKEY_CURRENT_USER\Software\Microsoft\.NETFramework\FeatureSIMD: A 32-bit (REG_DWORD) with the value set to 1

Note: To remove the previously added keys and values from the registry after you finished working with RyuJIT:

reg delete HKCU\SOFTWARE\Microsoft\.NETFramework /v AltJit /f /reg:64 > NUL
reg delete HKCU\SOFTWARE\Microsoft\.NETFramework /v FeatureSIMD /f /reg:64 > NUL

If you just want to activate RyuJIT and enable SIMD instructions in .NET Framework before running an executable at the command line, you can run the following commands to set the temporary environment variables and avoid changes to the registry.


Adding SIMD-enabled Vector Types to a Project

It is necessary to add some magic code to make SIMD-enabled vector types work as expected. I'll provide an example with a WPF application.

Create a new Visual C# Windows WPF application project in Visual Studio 2013. Then, run the following command within the Package Manager Console. The -Pre option indicates that you want to install a pre-release package. At the time of writing, the latest version for Microsoft.Bcl.Simd is 1.0.2-beta. You can check the latest version and the versions history for this NuGet package here.

Install-Package Microsoft.Bcl.Simd -Pre

If you check the References for the project within Solution Explorer, you will notice the installation added the System.Numerics.Vectors assembly. If you work with Windows 8.x, the path for this assembly will be packages\Microsoft.Bcl.Simd.1.0.2-beta\lib\portable-net45+win8\System.Numerics.Vectors.dll within your solution's base folder. The installation also adds a new packages.config file to the project with the following lines:

<?xml version="1.0" encoding="utf-8"?>
  <package id="Microsoft.Bcl.Simd" version="1.0.2-beta" targetFramework="net45" />

By default, when a debugger is attached, there is a setting in Visual Studio 2013 that makes the JIT compiler suppress all the optimizations. Thus, if you write code that is supposed to generate SIMD instructions with RyuJIT and compile it in release mode with the default debugging settings, the JIT compiler won't generate SIMD instructions. To debug a binary with SIMD intrinsics select Tools | Options… | Debugging | General and uncheck the Suppress JIT optimization on module load (Managed only) checkbox.

The RyuJIT CTP4 version requires some magic code to get SIMD generation working. You need to use the System.Numerics.Vector type in the class constructor. This magic shouldn't be necessary in the final release. The following lines show the new code for the App class (App.xaml.cs) that declares a static System.Numerics.Vector<float> named dummy, and then initializes it in the class constructor with System.Numerics.Vector<float>.One. If you peek at the assembly code generated for this initialization, you should take into account that, in this case, the generated code won't be optimized to take full advantage of SIMD instructions. I know, you might be wondering why such a thing is necessary, but it won't work correctly if you don't add the code.

using System;
using System.Windows;
// Added for SIMD
using System.Numerics;

namespace SIMDWpfApplication1
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
        // Code required to get SIMD working with RyuJIT CTP4
        static Vector<float> dummy;

        // Code required to get SIMD working with RyuJIT CTP4
        static App() { dummy = Vector<float>.One; }

        protected override void OnStartup(StartupEventArgs e)

            // Check whether optimized SIMD intrinsics will be generated
            if (!VectorMath.IsHardwareAccelerated)
                MessageBox.Show("I'm sorry. The JIT won't generate SIMD instructions.", "Error");

The System.Numerics.VectorMath class includes math functions to operate on generic and static type vectors. Each math operation generates SIMD intrinsics with RyuJIT. However, the math functions will also work OK when the JIT compiler isn't generating the optimized SIMD intrinsics. Thus, its best to check the boolean value of the System.Numerics.VectorMath.IsHardwareAccelerated property. A true value indicates that all the necessary configurations and magic code are in place to generate the optimized SIMD intrinsics. The previous lines check the value for this property and exit if the value is false. If the value is false, make sure you followed all the earlier steps to activate RyuJIT and enable SIMD instructions.

Related Reading

More Insights

Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.