Im trying to make a few efficient maths classes that will optomise using SIMD SSE or SSE2 if the target CPU supports those SIMD variants or failing to support either use a normal CPU bound c++ version.

The issue im having is that i want to have for example a vector class

Vector3D which always looks the same to the developer but using function pointers or derived classes to change the implimentation based on a global var such as

int gloabalSSE// -1 for no sse, 1 for sse and 2 for sse2 support.

so when i create a Vector instance such as

Vector3D someVector;

the constructor will evaluate the global var (this is set by a function i have that is run upon program starting and it works how i want no problems there) and then use the correct version of each member function such as


the code looks the same to the programmer but the code implimenting the Normalise() function uses the best SIMD optomisation available on the target CPU.

Im using Win XP + Visual stuido 2008 express (have a pro install available to me as well)

So really to summarise the question im asking is, how do i make a class that always looks the same to the user but uses either function pointers or derived classes to achieve the best SIMD optomisation that the target CPU supports. IF i have to use derived classes i would aprpeciate if you could suggest how i can make all the derived classes still be called Vector3D when used in code.

Also i know there is no code here but i think this question is really idependant of the code as its more of a class usage thing than how the functions are implimented really but for completeness i attached my current function pointers attempt.

Ill admit im somewhat new to c++ and still geting used to using c++ features rather than c which i started a few years ago. so please dont think why such a n00b programmer is asking to use SIMD optomisation when he doesnt understand classes, it's cause i want to try make the class really good once then hopefully it wont need me to come back later to update it :)

Thanks again for your time.

The obvious thing do here seems to be to use a typedef.

Consider this

class SSE2Vector
   // Stuff here that makes use of SSE2 and is fast

class BasicVector
   // General version. Work on everything.

typedef BasicVector Vector;

What you have done there is selected the basicVector for the Vector and you can select the other if you wish. Then you program with just Vector.

However, this is the quick and dirty version. Problems that you have to overcome and CAN be overcome with template metaprogramming or simple #defines are that you don't want to compile the SSE2 version unless your compiler supports it.

Note: if you use templates, what you are going to do is encapsulate the typedef within a structure. However, I will expand of this after you get a bit of experienced with the basic concepts.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.18 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.