Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <pmmintrin.h>
- typedef union vec4
- {
- public:
- // Empty constructor
- inline vec4() {
- m = _mm_setzero_ps();
- }
- // 4 var init constructor
- inline vec4(float _x, float _y, float _z, float _w) {
- m = _mm_set_ps(_w, _z, _y, _x);
- }
- // Float array constructor
- inline vec4(const float* fv)
- {
- m = _mm_loadu_ps(fv);
- }
- // Copy constructor
- inline vec4(const vec4 &v) {
- m = v.m;
- }
- // ----------------------------------------------------------------- //
- // Cast operator, for []
- inline operator float* ()
- {
- return (float*)this;
- }
- // Const cast operator, for const []
- inline operator const float* () const
- {
- return (const float*)this;
- }
- // ----------------------------------------------------------------- //
- inline vec4 operator += (float f)
- {
- m = _mm_add_ps(m, _mm_set1_ps(f));
- return *this;
- }
- inline vec4 operator += (const vec4 &v)
- {
- m = _mm_add_ps(m, v.m);
- return *this;
- }
- inline vec4 operator -= (float f)
- {
- m = _mm_sub_ps(m, _mm_set1_ps(f));
- return *this;
- }
- inline vec4 operator -= (const vec4 &v)
- {
- m = _mm_sub_ps(m, v.m);
- return *this;
- }
- inline vec4 operator *= (float f)
- {
- m = _mm_mul_ps(m, _mm_set1_ps(f));
- return *this;
- }
- inline vec4 operator *= (const vec4 &v)
- {
- m = _mm_mul_ps(m, v.m);
- return *this;
- }
- inline vec4 operator /= (float f)
- {
- m = _mm_div_ps(m, _mm_set1_ps(f));
- return *this;
- }
- inline vec4 operator /= (const vec4 &v)
- {
- m = _mm_div_ps(m, v.m);
- return *this;
- }
- // ----------------------------------------------------------------- //
- inline vec4 operator - () const
- {
- return vec4(_mm_sub_ps(_mm_setzero_ps(), m));
- }
- inline vec4 operator + (float f) const
- {
- return (vec4(*this) += f);
- }
- inline vec4 operator + (const vec4 &v) const
- {
- return (vec4(*this) += v);
- }
- inline vec4 operator - (float f) const
- {
- return (vec4(*this) -= f);
- }
- inline vec4 operator - (const vec4 &v) const
- {
- return (vec4(*this) -= v);
- }
- inline vec4 operator * (float f) const
- {
- return (vec4(*this) *= f);
- }
- inline vec4 operator * (const vec4 &v) const
- {
- return (vec4(*this) *= v);
- }
- inline vec4 operator / (float f) const
- {
- return (vec4(*this) /= f);
- }
- inline vec4 operator / (const vec4 &v) const
- {
- return (vec4(*this) /= v);
- }
- // ----------------------------------------------------------------- //
- // Dot product
- inline float dot(const vec4 &v) const {
- __m128 res = _mm_mul_ps(m, v.m);
- res = _mm_hadd_ps(res, res);
- res = _mm_hadd_ps(res, res);
- return *(float*)&res;
- }
- // Length / Megnitude of a vector
- inline float length() const {
- return sqrtf(dot(*this));
- }
- // Normalize of vector (to unit length)
- inline vec4 normalize() {
- __m128 len = _mm_mul_ps(m, m);
- len = _mm_hadd_ps(len, len);
- len = _mm_hadd_ps(len, len);
- len = _mm_sqrt_ps(len);
- m = _mm_div_ps(m, len);
- return *this;
- }
- // Unit vector copy
- inline vec4 unit() const {
- return vec4(*this).normalize();
- }
- // Linear Interpolation
- inline vec4 lerp(const vec4 &v, float f) {
- *this -= v;
- *this *= f;
- *this += v;
- return *this;
- }
- // Linear Interpolation
- inline vec4 lerp(const vec4 &v0, const vec4 &v1, float f) {
- return *this += ((v1 - v0) * f);
- }
- // ----------------------------------------------------------------- //
- // Vertex / Vector
- struct {
- float x, y, z, w;
- };
- // Color
- struct {
- float r, g, b, a;
- };
- // Texture coordinates
- struct {
- float s, t, p, q;
- };
- private:
- // SSE register
- __m128 m;
- // SSE compatible constructor
- inline vec4(const __m128 &_m) {
- m = _m;
- }
- } vec4;
Add Comment
Please, Sign In to add comment