Mono Class Library: Mono.Simd Namespace

Mono.Simd.SimdRuntime Class

Methods to probe for SIMD support. [Edit]

See Also: SimdRuntime Members

public static class SimdRuntime

Remarks

Helper methods used to probe for features in the SIMD runtime.

Sometimes it is desirable to write code in more than way to take advantage or features available only on recent machines. This class provides the required introspection capabilities to have such thing detected at runtime.

[Edit]

Example

The following example shows how to detect the feature set and conditionally generate the right code.

C# Example
	using System;
	using Mono.Simd;

	public class SampleRuntimeDetection {

		static bool MinimalSimdSupport ()
		{
			return SimdRuntime.IsMethodAccelerated (typeof (Vector4f), "op_Addition") &&
				   SimdRuntime.IsMethodAccelerated (typeof (Vector4f), "op_Multiply") &&
				   SimdRuntime.IsMethodAccelerated (typeof (VectorOperations), "Shuffle", typeof (Vector4f), typeof (ShuffleSel));
		}

		static bool EnhancedSimdSupport ()
		{
			return SimdRuntime.IsMethodAccelerated (typeof (VectorOperations), "HorizontalAdd", typeof (Vector4f), typeof (Vector4f)) &&
				   SimdRuntime.IsMethodAccelerated (typeof (Vector4f), "op_Multiply");
		}

		static readonly bool basic_support = MinimalSimdSupport ();
		static readonly bool enhanced_support = EnhancedSimdSupport ();

		public static float DotProduct (ref Vector4f a, ref Vector4f b)
		{
			if (enhanced_support) {
				Vector4f prod = a * b;
				prod = prod.HorizontalAdd (prod);
				prod = prod.HorizontalAdd (prod);
				return prod.X;
			} else if (basic_support) {
				Vector4f prod = a * b;
				prod += prod.Shuffle (ShuffleSel.XFromZ | ShuffleSel.YFromW);
				prod += prod.Shuffle (ShuffleSel.XFromY);
				return prod.X;
			} else {
				return a.X * b.X + a.Y * b.Y + a.Z * b.Z  + a.W * b.W;
			}
		}

		public static void Main () {
			Vector4f x = new Vector4f (1, 2, 3, 1);
			Vector4f y = new Vector4f (5, 6, 7, 1);
			float res = DotProduct (ref x, ref y);
			Console.WriteLine ("Dot product of {0} . {1} is {2}", x, y, res);
		}
	}

	
		

Requirements

Namespace: Mono.Simd
Assembly: Mono.Simd (in Mono.Simd.dll)
Assembly Versions: 2.0.0.0