为类的实例提供指向结构的指针

发布于 2024-08-05 07:18:58 字数 1573 浏览 4 评论 0原文

我正在尝试在我的向量类中获取 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一梦等七年七年为一梦 2024-08-12 07:18:58

怎么样:

__declspec( align( 16 ) ) VectorData vd;

您还可以创建自己的运算符 new 版本,如下所示

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

,然后可以按如下方式进行分配

AlignedData* pData = new( 16 ) AlignedData;

以在 16 字节边界对齐。

如果那没有帮助那么我可能会误解你的要求......

How about:

__declspec( align( 16 ) ) VectorData vd;

?

You can also create your own version of operator new as follows

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.

If thats no help then i may be misunderstanding what you are asking for ...

栩栩如生 2024-08-12 07:18:58

您可能不应该期望一次性载体的性能得到改善。当您可以将并行处理与一定量结合起来时,即按顺序处理许多向量时,并行处理会发挥最大的作用。

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.

过期以后 2024-08-12 07:18:58

我修好了。 :O

这真的很容易。我所要做的就是变成

VectorData* m_Point;

VectorData m_Point;

我的问题就消失了,不需要 malloc 或对齐。

但我很感谢大家的帮助! :D

I fixed it. :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.

But I appreciate everyone's help! :D

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文