Eran Kampf
Eran Kampf
2 min read

Moving on to Sharp3D.Math 2.0 - testing the use of generic types

I’ve started doing tests on the use of generics (introduced in the new .NET framework 2.0) for the next versio of Sharp3D.Math.

Doing numerics on generic types is a bit of a problem because unconstrained type parameters are assumed to be of type System.Object which does not define arithmetic operations (like +, – etc…).
Using a constrained type parameter, the interfaces the type parameter has to implement could be defined but it is not possible to define operator constraints.

You can read a detailed discussion about the problem here and at this codeproject article.

The two suggested workarounds for this problems are:

  1. Define an IArithmetic interface that defines the basic operations needed for math calculations (addition etc…) and use it to wrap around numeric types (float, double etc…)
  2. Use a separated type for the operators.

The suggested workaround of using a separate type for the operators could be a good idea for Sharp3D.Math as it can also be used for platform specific performance tuning.
Say an IKernelMatrix4 implements all arithmetics for a 4×4 matrix structure, besides a native .NET implementation of IKernelMatrix4 there could also implementations using platform specific libraries such as the DirectX math library (which uses SSE optimizations for matrix arithmetics), Intel’s Math Kernel library and other unmanaged optimized libraries.

Currently I am implementing a 4×4 matrix structure using the method specified above for the purpose of performance testing:
Generic matrix4x4 (.NET implementation) vs. Generic matrix4x4 (DirectX implementation) vs. Sharp3D.Math.Core.Matrix4F

I’ll post the results soon…