2009-09-11 59 views
1

我想在我的矢量類來獲得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

我的新方法基本上是從類的外部數據,所以類不必對齊。

我的問題是:有沒有更好的方法來獲得指向結構(對齊內存)實例的指針,還是我的方法真的很笨,還有更簡潔的方法?

回答

2

如何:

__declspec(align(16)) VectorData vd; 

您也可以創建自己的運營商新的版本如下

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

那麼它可以使allocationas如下

AlignedData* pData = new(16) AlignedData; 

在16字節邊界對齊。

如果那是沒有任何幫助的話,我可能會誤會你問什麼...

+1

你的意思是_declspec,我想? – cedrou 2009-09-11 14:50:13

+0

LOL真的沒有注意到這個錯字! – Goz 2009-09-11 15:30:18

1

你可能不應該期望獲得的一次性使用的載體提高了性能。當您可以將並行處理與某些音量相結合時,即按順序處理多個向量時,並行處理將發光最亮。

0

我修好了。 :O

這真的很容易。我所要做的就是把

VectorData* m_Point; 

VectorData m_Point; 

,我的問題都沒有了,沒有必要的malloc或對齊。

但我很感謝大家的幫助! :D

+0

對不起,但我懷疑。是的,x86-64上的MS編譯器在16字節邊界上對齊(不適用於32位平臺)。我懷疑ICC是否會在堆棧中對齊16個字節,如果沒有明確地這樣說,也正是因爲它試圖產生真正快速的代碼。 declspec將是必要的,resp。相應的gcc選項。 – gimpf 2009-09-11 15:41:47

+0

而且,是的,malloc()首先是個壞主意...... – gimpf 2009-09-11 15:42:27

相關問題