简体   繁体   English

给一个类的实例一个指向结构的指针

[英]Giving an instance of a class a pointer to a struct

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: 我正在尝试在我的向量类中获得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

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. 我确定我的瓶颈是我用来获取指向结构的指针的malloc。

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: 在类中使用SSE的主要问题之一是它必须在内存中对齐才能工作,这意味着重载new和delete运算符,结果是这样的代码:

 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. 您不能再使用默认构造函数,而必须避免new的灾难。

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? 我的问题是:有没有更好的方法来获取指向(对齐到内存的)结构实例的指针,或者我的方法真的很愚蠢,并且有一种更清洁的方法?

How about: 怎么样:

__declspec( align( 16 ) ) VectorData vd;

?

You can also create your own version of operator new as follows 您还可以创建自己的新版operator,如下所示

void* operator new( size_t size, size_t alignment )
{
     return __aligned_malloc( size, alignment );
}

which can then make allocationas follows 然后可以进行如下分配

AlignedData* pData = new( 16 ) AlignedData;

to align at a 16 byte boundary. 以16字节边界对齐。

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, ie when processing many vectors in sequence. 当您可以将并行处理与一定的体积结合使用时,即在按顺序处理多个矢量时,并行处理最亮。

I fixed it. 我修好了它。 :O :o

It was really rather easy. 这真的很容易。 All I had to do was turn 我要做的就是转身

VectorData* m_Point;

into

VectorData m_Point;

and my problems are gone, with no need for malloc or aligning. 并且我的问题消失了,不需要malloc或aligning。

But I appreciate everyone's help! 但我感谢大家的帮助! :D :d

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM