ベクトルクラスでSSE機能を取得しようとしています(これまでに3回書き直しました。:\)。次のようにしています。
#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を使用する際の主な問題の1つは、SSEを機能させるためにメモリ内で整列させる必要があることです。つまり、new演算子とdelete演算子がオーバーロードされ、次のようなコードになります。
BadVector* test1 = new BadVector(1, 2, 3);
BadVector* test2 = new BadVector(4, 5, 6);
*test1 *= test2;
デフォルトのコンストラクターを使用できなくなりnew
、疫病のように回避する必要があります。
私の新しいアプローチは、基本的に、クラスの外部にデータを配置して、クラスを整列させる必要がないようにすることです。
私の質問は、構造体の(メモリに整列された)インスタンスへのポインタを取得するためのより良い方法がありますか、それとも私のアプローチは本当にばかげていて、はるかにクリーンな方法がありますか?