include/vector.h
branchpyrit
changeset 84 6f7fe14782c2
parent 83 e3a2a5b26abb
child 86 ce6abe0aeeae
equal deleted inserted replaced
83:e3a2a5b26abb 84:6f7fe14782c2
   189 	};
   189 	};
   190 };
   190 };
   191 
   191 
   192 typedef Vector3 Colour;
   192 typedef Vector3 Colour;
   193 
   193 
       
   194 
       
   195 static const __m128 zeros = _mm_set_ps1(0.);
       
   196 static const __m128 ones = _mm_set_ps1(1.);
       
   197 static const __m128 mEps = _mm_set_ps1(Eps);
       
   198 
       
   199 class VectorPacket
       
   200 {
       
   201 public:
       
   202 	union {
       
   203 		__m128 ma[3];
       
   204 		struct {
       
   205 			__m128 mx;
       
   206 			__m128 my;
       
   207 			__m128 mz;
       
   208 		};
       
   209 		struct {
       
   210 			float x[4];
       
   211 			float y[4];
       
   212 			float z[4];
       
   213 		};
       
   214 	};
       
   215 
       
   216 	VectorPacket() {};
       
   217 	VectorPacket(__m128 ax, __m128 ay, __m128 az):
       
   218 		mx(ax), my(ay), mz(az) {};
       
   219 
       
   220 	Vector3 getVector(int i) const
       
   221 	{
       
   222 		return Vector3(x[i], y[i], z[i]);
       
   223 	};
       
   224 
       
   225 	void normalize()
       
   226 	{
       
   227 		__m128 m,x,y,z;
       
   228 		x = _mm_mul_ps(mx, mx); // x*x
       
   229 		y = _mm_mul_ps(my, my); // y*y
       
   230 		z = _mm_mul_ps(mz, mz); // z*z
       
   231 		m = _mm_add_ps(x, y);
       
   232 		m = _mm_add_ps(m, z);     // x*x + y*y + z*z
       
   233 		m = _mm_sqrt_ps(m);
       
   234 		m = _mm_div_ps(ones, m);   // m = 1/sqrt(m)
       
   235 		mx = _mm_mul_ps(mx, m);
       
   236 		my = _mm_mul_ps(my, m);
       
   237 		mz = _mm_mul_ps(mz, m);
       
   238 	};
       
   239 
       
   240 	friend VectorPacket operator+(const VectorPacket &a, const VectorPacket &b)
       
   241 	{
       
   242 		return VectorPacket(
       
   243 			_mm_add_ps(a.mx, b.mx),
       
   244 			_mm_add_ps(a.my, b.my),
       
   245 			_mm_add_ps(a.mz, b.mz));
       
   246 	};
       
   247 
       
   248 	friend VectorPacket operator-(const VectorPacket &a, const VectorPacket &b)
       
   249 	{
       
   250 		return VectorPacket(
       
   251 			_mm_sub_ps(a.mx, b.mx),
       
   252 			_mm_sub_ps(a.my, b.my),
       
   253 			_mm_sub_ps(a.mz, b.mz));
       
   254 	};
       
   255 
       
   256 	friend VectorPacket operator*(const VectorPacket &v,  const __m128 &m)
       
   257 	{
       
   258 		return VectorPacket(
       
   259 			_mm_mul_ps(v.mx, m),
       
   260 			_mm_mul_ps(v.my, m),
       
   261 			_mm_mul_ps(v.mz, m));
       
   262 	};
       
   263 
       
   264 	friend VectorPacket operator/(const __m128 &m, const VectorPacket &v)
       
   265 	{
       
   266 		return VectorPacket(
       
   267 			_mm_div_ps(m, v.mx),
       
   268 			_mm_div_ps(m, v.my),
       
   269 			_mm_div_ps(m, v.mz));
       
   270 	};
       
   271 
       
   272 	// write to character stream
       
   273 	friend ostream & operator<<(ostream &st, const VectorPacket &v)
       
   274 	{
       
   275 		return st << "[" << v.getVector(0) << "," << v.getVector(1) 
       
   276 			<< "," << v.getVector(2) << "," << v.getVector(3) << ")";
       
   277 	};
       
   278 
       
   279 };
       
   280 
   194 #endif
   281 #endif