Robmaister's picture

Implementing Mono.SIMD

Project:The Open Toolkit library
Category:feature request

I just noticed the forum post regarding Mono.SIMD, and I would like to help bridge the current math structs with the SIMD structs where applicable (which appears to only be with Vector4 and Vector2d, unless we use Mono.SIMD.Vector4f for everything but with different components forced to be 0).

There are many ways to go about this, here are a few I can think of right now:

-Use a union struct in Vector4 where both X and the underlying Mono.SIMD.Vector4f starts at position 0. Methods/operator overloads would chain off to the corresponding SIMD methods/operators of the contained Vector4f. This would get messy fast.
-Introduce a new type of Vector4 using SIMD, have implicit casts on both types, let the user decide which one to use.
-Accept Mono.SIMD.Vector4f in all the GL methods that you accept Vector4 in. Let the user deal with everything else, maybe add a new constructor and explicit cast in Vector4 to help ease.
-Add methods like AddSIMD() for all the SIMD-supported operations and cast the vectors to the SIMD ones internally.

Any ideas? I'd really like to see this happen, OpenTK is an excellent library and having fast math operations can work as a selling point to use C#/OpenTK over other languages and OpenGL bindings/math libraries.


Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
tbk's picture


Second idea looks best for me :-)

Robmaister's picture


I think second or third would be the simplest to implement, but it would really be nice to see the functionality built into the current vectors and automatically used if the system is capable.

This weekend I'll mess around with casting for supported operations, possibly making use of SimdRuntime ( to automatically check for support.

tbk's picture


Just found that Mono already has SIMD-optimized game math:

Robmaister's picture


I actually never got a chance to work on this over the weekend, I'll try something out once I'm done with finals. And it looks like Mono.GameMath can compile to either SIMD optimized Vector4 math or regular Vector4 math by defining SIMD.

Assuming that C# compilers keep both blocks of code , a project using OpenTK only needs to add a "#define SIMD" to their project to enable the optimizations. In fact, some sort of system like this will probably have to be used if we want to still be able to compile OpenTK on anything besides Mono. I'll start with a system like this working and get some feedback when I have something that works.