|
From: <axl...@us...> - 2010-04-25 19:41:25
|
Revision: 697
http://hgengine.svn.sourceforge.net/hgengine/?rev=697&view=rev
Author: axlecrusher
Date: 2010-04-25 19:41:19 +0000 (Sun, 25 Apr 2010)
Log Message:
-----------
Make FloatRow a class to abstract the differences between __m128 and float[4].
Clean up how numbers are accessed in MercuryVertex.
Modified Paths:
--------------
Mercury2/src/MercuryMath.cpp
Mercury2/src/MercuryMath.h
Mercury2/src/MercuryMatrix.cpp
Mercury2/src/MercuryVertex.cpp
Mercury2/src/MercuryVertex.h
Modified: Mercury2/src/MercuryMath.cpp
===================================================================
--- Mercury2/src/MercuryMath.cpp 2010-04-25 18:21:24 UTC (rev 696)
+++ Mercury2/src/MercuryMath.cpp 2010-04-25 19:41:19 UTC (rev 697)
@@ -144,6 +144,13 @@
f[i] = r[i];
}
+void MMCrossProduct( const FloatRow& r1, const FloatRow& r2, FloatRow& result)
+{
+ result[0] = r1[1]*r2[2] - r1[2]*r2[1];
+ result[1] = r1[2]*r2[0] - r1[0]*r2[2];
+ result[2] = r1[0]*r2[1] - r1[1]*r2[0];
+}
+
#else
//inline __m128 Hadd4(__m128 x);
@@ -258,7 +265,7 @@
void ZeroFloatRow(FloatRow& r)
{
- r = (FloatRow)_mm_setzero_ps();
+ r = _mm_setzero_ps();
}
void Float2FloatRow(const float* f, FloatRow& r)
@@ -271,6 +278,20 @@
_mm_store_ps( f, r );
}
+void MMCrossProduct( const FloatRow& r1, const FloatRow& r2, FloatRow& result)
+{
+ __m128 a,b,c,d,r;//using more registers is faster
+
+ a = _mm_shuffle_ps(r1, r1, 0xc9);
+ b = _mm_shuffle_ps(r2, r2, 0xd2);
+ r = _mm_mul_ps( a, b );
+
+ c = _mm_shuffle_ps(r2, r2, 0xc9);
+ d = _mm_shuffle_ps(r1, r1, 0xd2);
+ r -= _mm_mul_ps( c, d );
+ result = r;
+}
+
#endif
/*
Modified: Mercury2/src/MercuryMath.h
===================================================================
--- Mercury2/src/MercuryMath.h 2010-04-25 18:21:24 UTC (rev 696)
+++ Mercury2/src/MercuryMath.h 2010-04-25 19:41:19 UTC (rev 697)
@@ -10,10 +10,26 @@
#ifdef USE_SSE
#include <xmmintrin.h>
-typedef __m128 FloatRow __attribute__((aligned(16)));
+#endif
+class FloatRow
+{
+ public:
+ inline float& operator[](unsigned int i) { return ((float*)&m_floats)[i]; }
+ inline const float& operator[](unsigned int i) const { return ((const float*)&m_floats)[i]; }
+
+ inline operator float*() { return (float*)&m_floats; }
+ inline operator const float*() const { return (const float*)&m_floats; }
+
+#ifndef USE_SSE
+ float m_floats[4];
#else
-typedef float FloatRow[4];
+ inline FloatRow& operator=(const __m128& f) { m_floats=f; return *this; }
+
+ inline operator __m128&() { return m_floats; }
+ inline operator const __m128&() const { return m_floats; }
+ __m128 m_floats __attribute__((aligned(16)));
#endif
+};
#ifdef WIN32
#include <limits>
@@ -79,37 +95,15 @@
void MatrixMultiply4f ( const FloatRow* in1, const FloatRow* in2, FloatRow* out );
void VectorMultiply4f(const FloatRow* matrix, const FloatRow& p, FloatRow& out );
void TransposeMatrix( FloatRow* m );
+void MMCrossProduct( const FloatRow& r1, const FloatRow& r2, FloatRow& result);
//void Float2FloatRow(const float* f, FloatRow& r);
//void FloatRow2Float(const FloatRow& fr, float* f);
-#ifdef USE_SSE
-inline void MMCrossProduct( const FloatRow& r1, const FloatRow& r2, FloatRow& result)
-{
- __m128 a,b,c,d,r;//using more registers is faster
+const FloatRow gfrZero = { { 0.f, 0.f, 0.f, 0.f } };
- a = _mm_shuffle_ps(r1, r1, 0xc9);
- b = _mm_shuffle_ps(r2, r2, 0xd2);
- r = _mm_mul_ps( a, b );
-
- c = _mm_shuffle_ps(r2, r2, 0xc9);
- d = _mm_shuffle_ps(r1, r1, 0xd2);
- r -= _mm_mul_ps( c, d );
- result = r;
-}
-#else
-inline void MMCrossProduct( const FloatRow& r1, const FloatRow& r2, FloatRow& result)
-{
- result[0] = r1[1]*r2[2] - r1[2]*r2[1];
- result[1] = r1[2]*r2[0] - r1[0]*r2[2];
- result[2] = r1[0]*r2[1] - r1[1]*r2[0];
-}
#endif
-const FloatRow gfrZero = { 0.f, 0.f, 0.f, 0.f };
-
-#endif
-
/*
* (c) 2006 Joshua Allen
* All rights reserved.
Modified: Mercury2/src/MercuryMatrix.cpp
===================================================================
--- Mercury2/src/MercuryMatrix.cpp 2010-04-25 18:21:24 UTC (rev 696)
+++ Mercury2/src/MercuryMatrix.cpp 2010-04-25 19:41:19 UTC (rev 697)
@@ -8,7 +8,7 @@
0.0f, 0.0f, 0.0f, 1.0f };
MercuryMatrix::MercuryMatrix()
-{
+{/*
#ifdef USE_SSE
m_matrix[0] = _mm_load1_ps( &base_matrix_identity[0] );
m_matrix[1] = _mm_load1_ps( &base_matrix_identity[4] );
@@ -17,6 +17,8 @@
#else
Copy16f(m_matrix[0], base_matrix_identity );
#endif
+*/
+ *this = Identity();
}
const MercuryMatrix& MercuryMatrix::operator=(const MercuryMatrix& m)
@@ -46,7 +48,7 @@
if (!bSetIdentity)
{
bSetIdentity = true;
-#ifdef USE_SSE
+#ifdef USE_SSEXXX //XXX broken _mm_load1_ps routines
MercuryMatrix::IdentityMatrix.m_matrix[0] = _mm_load1_ps( &base_matrix_identity[0] );
MercuryMatrix::IdentityMatrix.m_matrix[1] = _mm_load1_ps( &base_matrix_identity[4] );
MercuryMatrix::IdentityMatrix.m_matrix[2] = _mm_load1_ps( &base_matrix_identity[8] );
Modified: Mercury2/src/MercuryVertex.cpp
===================================================================
--- Mercury2/src/MercuryVertex.cpp 2010-04-25 18:21:24 UTC (rev 696)
+++ Mercury2/src/MercuryVertex.cpp 2010-04-25 19:41:19 UTC (rev 697)
@@ -7,22 +7,22 @@
MercuryVertex::MercuryVertex()
{
- (*this)[0] = (*this)[1] = (*this)[2] = (*this)[3] = 0;
+ m_xyzw[0] = m_xyzw[1] = m_xyzw[2] = m_xyzw[3] = 0;
}
MercuryVertex::MercuryVertex( float ix, float iy, float iz, float iw )
{
- (*this)[0] = ix;
- (*this)[1] = iy;
- (*this)[2] = iz;
- (*this)[3] = iw;
+ m_xyzw[0] = ix;
+ m_xyzw[1] = iy;
+ m_xyzw[2] = iz;
+ m_xyzw[3] = iw;
}
MercuryVertex::MercuryVertex( const float* in3f, float f )
{
for (unsigned int i = 0; i < 3; ++i)
(*this)[i] = in3f[i];
- (*this)[3] = f;
+ m_xyzw[3] = f;
}
MercuryVertex::MercuryVertex( const float* in4f )
@@ -41,7 +41,7 @@
{
for (unsigned int i = 0; i < 3; ++i)
(*this)[i] = v[i];
- (*this)[3] = w;
+ m_xyzw[3] = w;
}
void MercuryVertex::NormalizeSelf()
@@ -60,19 +60,19 @@
float MercuryVertex::Length() const
{
- float length = (*this)[0]*(*this)[0];
- length += (*this)[1]*(*this)[1];
- length += (*this)[2]*(*this)[2];
- length += (*this)[3]*(*this)[3];
+ float length = m_xyzw[0]*m_xyzw[0];
+ length += m_xyzw[1]*m_xyzw[1];
+ length += m_xyzw[2]*m_xyzw[2];
+ length += m_xyzw[3]*m_xyzw[3];
return SQRT(length);
}
float MercuryVertex::GetBiggestElement() const
{
- float tmp = (*this)[0];
- tmp = MAX<float>(tmp, (*this)[1]);
- tmp = MAX<float>(tmp, (*this)[2]);
- return MAX<float>(tmp, (*this)[3]);
+ float tmp = m_xyzw[0];
+ tmp = MAX<float>(tmp, m_xyzw[1]);
+ tmp = MAX<float>(tmp, m_xyzw[2]);
+ return MAX<float>(tmp, m_xyzw[3]);
}
const MercuryVertex& MercuryVertex::operator *= (const MercuryVertex& p)
@@ -114,7 +114,7 @@
float MercuryVertex::DotProduct(const MercuryVertex& rhs) const
{
//XXX should this use all 4 components?
- return ((*this)[0]*rhs[0]+(*this)[1]*rhs[1]+(*this)[2]*rhs[2]);
+ return (m_xyzw[0]*rhs[0]+m_xyzw[1]*rhs[1]+m_xyzw[2]*rhs[2]);
}
MercuryVertex MercuryVertex::DotProduct3(const MercuryVertex& rhs1, const MercuryVertex& rhs2, const MercuryVertex& rhs3) const
@@ -150,7 +150,7 @@
MString MercuryVertex::Stringify(const MString& s) const
{
- return ssprintf("%s: %f %f %f %f", s.c_str(), (*this)[0], (*this)[1], (*this)[2], (*this)[3]);
+ return ssprintf("%s: %f %f %f %f", s.c_str(), m_xyzw[0], m_xyzw[1], m_xyzw[2], m_xyzw[3]);
}
MercuryVertex MercuryVertex::Rotate(const MQuaternion& q) const
Modified: Mercury2/src/MercuryVertex.h
===================================================================
--- Mercury2/src/MercuryVertex.h 2010-04-25 18:21:24 UTC (rev 696)
+++ Mercury2/src/MercuryVertex.h 2010-04-25 19:41:19 UTC (rev 697)
@@ -24,23 +24,23 @@
MercuryVertex( const MercuryVertex& v, float w);
///Direct conversion to float*
- __inline__ operator float* () { return (float*)&m_xyzw; }
+ __inline__ operator float* () { return m_xyzw; }
///Direct conversion to const float*
- __inline__ operator const float* () const { return (float*)&m_xyzw; }
+ __inline__ operator const float* () const { return m_xyzw; }
inline FloatRow& ToFloatRow() { return m_xyzw; }
inline const FloatRow& ToFloatRow() const { return m_xyzw; }
- inline float GetX() const { return (*this)[0]; }
- inline float GetY() const { return (*this)[1]; }
- inline float GetZ() const { return (*this)[2]; }
- inline float GetW() const { return (*this)[3]; }
- inline void SetX(const float ix) { (*this)[0] = ix; }
- inline void SetY(const float iy) { (*this)[1] = iy; }
- inline void SetZ(const float iz) { (*this)[2] = iz; }
- inline void SetW(const float iw) { (*this)[3] = iw; }
+ inline float GetX() const { return m_xyzw[0]; }
+ inline float GetY() const { return m_xyzw[1]; }
+ inline float GetZ() const { return m_xyzw[2]; }
+ inline float GetW() const { return m_xyzw[3]; }
+ inline void SetX(const float ix) { m_xyzw[0] = ix; }
+ inline void SetY(const float iy) { m_xyzw[1] = iy; }
+ inline void SetZ(const float iz) { m_xyzw[2] = iz; }
+ inline void SetW(const float iw) { m_xyzw[3] = iw; }
- inline void Zero() { (*this)[0] = 0; (*this)[1] = 0; (*this)[2] = 0; }
+ inline void Zero() { m_xyzw[0] = 0; m_xyzw[1] = 0; m_xyzw[2] = 0; }
///Normalize (make |point| = 1)
void NormalizeSelf();
@@ -52,11 +52,11 @@
float GetBiggestElement() const;
///Write out to be = to this point
- inline void ConvertToVector3( float* out ) const { out[0] = (*this)[0]; out[1] = (*this)[1]; out[2] = (*this)[2]; }
+ inline void ConvertToVector3( float* out ) const { out[0] = m_xyzw[0]; out[1] = m_xyzw[1]; out[2] = m_xyzw[2]; }
///Write out to be = to this point, however the 4th element will be 0
- inline void ConvertToVector4( float* out, float w = 0 ) const { out[0] = (*this)[0]; out[1] = (*this)[1]; out[2] = (*this)[2]; out[3] = w; }
+ inline void ConvertToVector4( float* out, float w = 0 ) const { out[0] = m_xyzw[0]; out[1] = m_xyzw[1]; out[2] = m_xyzw[2]; out[3] = w; }
///Write out to be = - to this point, however the 4th element will be 0
- inline void ConvertToIVector4( float* out, float w = 0 ) const { out[0] = -(*this)[0]; out[1] = -(*this)[1]; out[2] = -(*this)[2]; out[3] = w; }
+ inline void ConvertToIVector4( float* out, float w = 0 ) const { out[0] = -m_xyzw[0]; out[1] = -m_xyzw[1]; out[2] = -m_xyzw[2]; out[3] = w; }
const MercuryVertex& operator *= (const MercuryVertex& p);
const MercuryVertex& operator /= (const MercuryVertex& p);
@@ -66,15 +66,15 @@
MercuryVertex operator*(const MercuryMatrix& m) const;
- inline MercuryVertex& operator += ( const MercuryVertex& other ) { (*this)[0]+=other[0]; (*this)[1]+=other[1]; (*this)[2]+=other[2]; return *this; }
- inline MercuryVertex& operator -= ( const MercuryVertex& other ) { (*this)[0]-=other[0]; (*this)[1]-=other[1]; (*this)[2]-=other[2]; return *this; }
- inline MercuryVertex& operator *= ( float f ) { (*this)[0]*=f; (*this)[1]*=f; (*this)[2]*=f; return *this; }
- inline MercuryVertex& operator /= ( float f ) { (*this)[0]/=f; (*this)[1]/=f; (*this)[2]/=f; return *this; }
+ inline MercuryVertex& operator += ( const MercuryVertex& other ) { m_xyzw[0]+=other[0]; m_xyzw[1]+=other[1]; m_xyzw[2]+=other[2]; return *this; }
+ inline MercuryVertex& operator -= ( const MercuryVertex& other ) { m_xyzw[0]-=other[0]; m_xyzw[1]-=other[1]; m_xyzw[2]-=other[2]; return *this; }
+ inline MercuryVertex& operator *= ( float f ) { m_xyzw[0]*=f; m_xyzw[1]*=f; m_xyzw[2]*=f; return *this; }
+ inline MercuryVertex& operator /= ( float f ) { m_xyzw[0]/=f; m_xyzw[1]/=f; m_xyzw[2]/=f; return *this; }
- inline MercuryVertex operator + ( const MercuryVertex& other ) const { return MercuryVertex( (*this)[0]+other[0], (*this)[1]+other[1], (*this)[2]+other[2] ); }
- inline MercuryVertex operator - ( const MercuryVertex& other ) const { return MercuryVertex( (*this)[0]-other[0], (*this)[1]-other[1], (*this)[2]-other[2] ); }
- inline MercuryVertex operator * ( float f ) const { return MercuryVertex( (*this)[0]*f, (*this)[1]*f, (*this)[2]*f ); }
- inline MercuryVertex operator / ( float f ) const { return MercuryVertex( (*this)[0]/f, (*this)[1]/f, (*this)[2]/f ); }
+ inline MercuryVertex operator + ( const MercuryVertex& other ) const { return MercuryVertex( m_xyzw[0]+other[0], m_xyzw[1]+other[1], m_xyzw[2]+other[2] ); }
+ inline MercuryVertex operator - ( const MercuryVertex& other ) const { return MercuryVertex( m_xyzw[0]-other[0], m_xyzw[1]-other[1], m_xyzw[2]-other[2] ); }
+ inline MercuryVertex operator * ( float f ) const { return MercuryVertex( m_xyzw[0]*f, m_xyzw[1]*f, m_xyzw[2]*f ); }
+ inline MercuryVertex operator / ( float f ) const { return MercuryVertex( m_xyzw[0]/f, m_xyzw[1]/f, m_xyzw[2]/f ); }
bool operator==(const MercuryVertex& p) const;
inline bool operator!=(const MercuryVertex& p) const { return !(*this == p); }
@@ -96,7 +96,7 @@
static MercuryVertex CreateFromString(const MString& s);
-// float (*this)[3];
+// float m_xyzw[3];
FloatRow m_xyzw;
};
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|