我想在我的矢量類來獲得SSE功能(我到目前爲止已經重寫了三遍:\),然後我做了以下內容:給人一種類的一個實例的指針,結構
#ifndef _POINT_FINAL_H_
#define _POINT_FINAL_H_
#include "math.h"
namespace Vector3D
{
#define SSE_VERSION 3
#if SSE_VERSION >= 2
#include <emmintrin.h> // SSE2
#if SSE_VERSION >= 3
#include <pmmintrin.h> // SSE3
#endif
#else
#include <stdlib.h>
#endif
#if SSE_VERSION >= 2
typedef union { __m128 vector; float numbers[4]; } VectorData;
//typedef union { __m128 vector; struct { float x, y, z, w; }; } VectorData;
#else
typedef struct { float x, y, z, w; } VectorData;
#endif
class Point3D
{
public:
Point3D();
Point3D(float a_X, float a_Y, float a_Z);
Point3D(VectorData* a_Data);
~Point3D();
// a lot of not-so-interesting functions
private:
VectorData* _NewData();
}; // class Point3D
}; // namespace Vector3D
#endif
它的工作原理!歡呼!但它比我以前的嘗試慢。噓。
我已經確定我的瓶頸是我用來獲取指向結構的指針的malloc。
VectorData* Point3D::_NewData()
{
#if SSE_VERSION >= 2
return ((VectorData*) _aligned_malloc(sizeof(VectorData), 16));
#else
return ((VectorData*) malloc(sizeof(VectorData)));
#endif
}
之一與一類使用SSE的主要問題之一是,它在存儲器中對準爲它工作,這意味着過載的新和刪除操作符,從而導致這樣的代碼:
BadVector* test1 = new BadVector(1, 2, 3);
BadVector* test2 = new BadVector(4, 5, 6);
*test1 *= test2;
你不能再使用默認的構造函數,你必須避免像瘟疫那樣的new
。
我的新方法基本上是從類的外部數據,所以類不必對齊。
我的問題是:有沒有更好的方法來獲得指向結構(對齊內存)實例的指針,還是我的方法真的很笨,還有更簡潔的方法?
你的意思是_declspec,我想? – cedrou 2009-09-11 14:50:13
LOL真的沒有注意到這個錯字! – Goz 2009-09-11 15:30:18