为类的实例提供指向结构的指针
我正在尝试在我的向量类中获取 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 的主要问题之一是它必须在内存中对齐才能工作,这意味着重载 new 和 delete 运算符,导致如下代码:
BadVector* test1 = new BadVector(1, 2, 3);
BadVector* test2 = new BadVector(4, 5, 6);
*test1 *= test2;
您不能再使用默认构造函数,并且您必须像躲避瘟疫一样躲避新
。
我的新方法基本上是将数据从类外部获取,这样类就不必对齐。
我的问题是:是否有更好的方法来获取指向结构体(在内存上对齐)实例的指针,或者我的方法是否真的很愚蠢并且有更干净的方法?
I am trying to get SSE functionality in my vector class (I've rewritten it three times so far. :\) and I'm doing the following:
#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
It works! Hurray! But it's slower than my previous attempt. Boo.
I've determined that my bottle neck is the malloc I'm using to get a pointer to a struct.
VectorData* Point3D::_NewData()
{
#if SSE_VERSION >= 2
return ((VectorData*) _aligned_malloc(sizeof(VectorData), 16));
#else
return ((VectorData*) malloc(sizeof(VectorData)));
#endif
}
One of the main problems with using SSE in a class is that it has to be aligned in memory for it to work, which means overloading the new and delete operators, resulting in code like this:
BadVector* test1 = new BadVector(1, 2, 3);
BadVector* test2 = new BadVector(4, 5, 6);
*test1 *= test2;
You can no longer use the default constructor and you have to avoid new
like the plague.
My new approach is basically to have the data external from the class so the class doesn't have to be aligned.
My question is: is there a better way to get a pointer to an (aligned on memory) instance of a struct or is my approach really dumb and there's a much cleaner way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
怎么样:
?
您还可以创建自己的运算符 new 版本,如下所示
,然后可以按如下方式进行分配
以在 16 字节边界对齐。
如果那没有帮助那么我可能会误解你的要求......
How about:
?
You can also create your own version of operator new as follows
which can then make allocationas follows
to align at a 16 byte boundary.
If thats no help then i may be misunderstanding what you are asking for ...
您可能不应该期望一次性载体的性能得到改善。当您可以将并行处理与一定量结合起来时,即按顺序处理许多向量时,并行处理会发挥最大的作用。
You should probably not expect to get improved performance for single-use vectors. Parallel processing shines brightest when you can combine the parallel processing with some volume, i.e. when processing many vectors in sequence.
我修好了。 :O
这真的很容易。我所要做的就是变成
,
我的问题就消失了,不需要 malloc 或对齐。
但我很感谢大家的帮助! :D
I fixed it. :O
It was really rather easy. All I had to do was turn
into
and my problems are gone, with no need for malloc or aligning.
But I appreciate everyone's help! :D