我想在我的矢量类来获得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