include/vector.h
branchpyrit
changeset 84 6f7fe14782c2
parent 83 e3a2a5b26abb
child 86 ce6abe0aeeae
--- a/include/vector.h	Thu Apr 24 18:12:32 2008 +0200
+++ b/include/vector.h	Sun Apr 27 09:44:49 2008 +0200
@@ -191,4 +191,91 @@
 
 typedef Vector3 Colour;
 
+
+static const __m128 zeros = _mm_set_ps1(0.);
+static const __m128 ones = _mm_set_ps1(1.);
+static const __m128 mEps = _mm_set_ps1(Eps);
+
+class VectorPacket
+{
+public:
+	union {
+		__m128 ma[3];
+		struct {
+			__m128 mx;
+			__m128 my;
+			__m128 mz;
+		};
+		struct {
+			float x[4];
+			float y[4];
+			float z[4];
+		};
+	};
+
+	VectorPacket() {};
+	VectorPacket(__m128 ax, __m128 ay, __m128 az):
+		mx(ax), my(ay), mz(az) {};
+
+	Vector3 getVector(int i) const
+	{
+		return Vector3(x[i], y[i], z[i]);
+	};
+
+	void normalize()
+	{
+		__m128 m,x,y,z;
+		x = _mm_mul_ps(mx, mx); // x*x
+		y = _mm_mul_ps(my, my); // y*y
+		z = _mm_mul_ps(mz, mz); // z*z
+		m = _mm_add_ps(x, y);
+		m = _mm_add_ps(m, z);     // x*x + y*y + z*z
+		m = _mm_sqrt_ps(m);
+		m = _mm_div_ps(ones, m);   // m = 1/sqrt(m)
+		mx = _mm_mul_ps(mx, m);
+		my = _mm_mul_ps(my, m);
+		mz = _mm_mul_ps(mz, m);
+	};
+
+	friend VectorPacket operator+(const VectorPacket &a, const VectorPacket &b)
+	{
+		return VectorPacket(
+			_mm_add_ps(a.mx, b.mx),
+			_mm_add_ps(a.my, b.my),
+			_mm_add_ps(a.mz, b.mz));
+	};
+
+	friend VectorPacket operator-(const VectorPacket &a, const VectorPacket &b)
+	{
+		return VectorPacket(
+			_mm_sub_ps(a.mx, b.mx),
+			_mm_sub_ps(a.my, b.my),
+			_mm_sub_ps(a.mz, b.mz));
+	};
+
+	friend VectorPacket operator*(const VectorPacket &v,  const __m128 &m)
+	{
+		return VectorPacket(
+			_mm_mul_ps(v.mx, m),
+			_mm_mul_ps(v.my, m),
+			_mm_mul_ps(v.mz, m));
+	};
+
+	friend VectorPacket operator/(const __m128 &m, const VectorPacket &v)
+	{
+		return VectorPacket(
+			_mm_div_ps(m, v.mx),
+			_mm_div_ps(m, v.my),
+			_mm_div_ps(m, v.mz));
+	};
+
+	// write to character stream
+	friend ostream & operator<<(ostream &st, const VectorPacket &v)
+	{
+		return st << "[" << v.getVector(0) << "," << v.getVector(1) 
+			<< "," << v.getVector(2) << "," << v.getVector(3) << ")";
+	};
+
+};
+
 #endif