I am designing a series of Vector classes in C++ that support SSE(SIMD). The operators have been overloaded for convenience. Example of class:
class vector2 {
public:
//...code
friend const vector2 operator+ (const vector2 & lhs, const vector2 & rhs);
//...code
protected:
float x, y;
};
So far the method checks to see if the CPU has a SSE(SIMD) feature, using a class I created called PROCESSOR, which does this check when the program is executed at run-time. Example of method:
const vector2 operator+ (const vector2 & lhs, const vector2 & rhs) {
vector2 temp;
if(PROCESSOR.SSE) {
_asm { //... The "SSE WAY"
}
} else {
// The "NORMAL WAY"
}
return temp;
}
So as you can see if SSE is available it will run the “SSE” way otherwise it will run “normal” way. However, it is very in-efficient having to check if SSE is available every time this operation is called. Is there a way to implement two versions of a method and call only the appropriate method? Since my PROCESSOR class only does the SSE check once, is there a way of setting my vector class can do the same?
To help you avoid code duplication you can create two vector classes, one for SSE and one for non-SSE. Then you can template your calling algorithms.
If you’re likely to use other classes than vector (like matrix etc) in an SSE manner you might do better by tagging your types instead .. in which case the code looks like this: