Guest User

Untitled

a guest
Jun 24th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.01 KB | None | 0 0
  1. #include <pmmintrin.h>
  2.  
  3. typedef union vec4
  4. {
  5. public:
  6. // Empty constructor
  7. inline vec4() {
  8. m = _mm_setzero_ps();
  9. }
  10.  
  11. // 4 var init constructor
  12. inline vec4(float _x, float _y, float _z, float _w) {
  13. m = _mm_set_ps(_w, _z, _y, _x);
  14. }
  15.  
  16. // Float array constructor
  17. inline vec4(const float* fv)
  18. {
  19. m = _mm_loadu_ps(fv);
  20. }
  21.  
  22. // Copy constructor
  23. inline vec4(const vec4 &v) {
  24. m = v.m;
  25. }
  26.  
  27. // ----------------------------------------------------------------- //
  28.  
  29. // Cast operator, for []
  30. inline operator float* ()
  31. {
  32. return (float*)this;
  33. }
  34.  
  35. // Const cast operator, for const []
  36. inline operator const float* () const
  37. {
  38. return (const float*)this;
  39. }
  40.  
  41. // ----------------------------------------------------------------- //
  42.  
  43. inline vec4 operator += (float f)
  44. {
  45. m = _mm_add_ps(m, _mm_set1_ps(f));
  46.  
  47. return *this;
  48. }
  49.  
  50. inline vec4 operator += (const vec4 &v)
  51. {
  52. m = _mm_add_ps(m, v.m);
  53.  
  54. return *this;
  55. }
  56.  
  57. inline vec4 operator -= (float f)
  58. {
  59. m = _mm_sub_ps(m, _mm_set1_ps(f));
  60.  
  61. return *this;
  62. }
  63.  
  64. inline vec4 operator -= (const vec4 &v)
  65. {
  66. m = _mm_sub_ps(m, v.m);
  67.  
  68. return *this;
  69. }
  70.  
  71. inline vec4 operator *= (float f)
  72. {
  73. m = _mm_mul_ps(m, _mm_set1_ps(f));
  74.  
  75. return *this;
  76. }
  77.  
  78. inline vec4 operator *= (const vec4 &v)
  79. {
  80. m = _mm_mul_ps(m, v.m);
  81.  
  82. return *this;
  83. }
  84.  
  85. inline vec4 operator /= (float f)
  86. {
  87. m = _mm_div_ps(m, _mm_set1_ps(f));
  88.  
  89. return *this;
  90. }
  91.  
  92. inline vec4 operator /= (const vec4 &v)
  93. {
  94. m = _mm_div_ps(m, v.m);
  95.  
  96. return *this;
  97. }
  98.  
  99. // ----------------------------------------------------------------- //
  100.  
  101. inline vec4 operator - () const
  102. {
  103. return vec4(_mm_sub_ps(_mm_setzero_ps(), m));
  104. }
  105.  
  106. inline vec4 operator + (float f) const
  107. {
  108. return (vec4(*this) += f);
  109. }
  110.  
  111. inline vec4 operator + (const vec4 &v) const
  112. {
  113. return (vec4(*this) += v);
  114. }
  115.  
  116. inline vec4 operator - (float f) const
  117. {
  118. return (vec4(*this) -= f);
  119. }
  120.  
  121. inline vec4 operator - (const vec4 &v) const
  122. {
  123. return (vec4(*this) -= v);
  124. }
  125.  
  126. inline vec4 operator * (float f) const
  127. {
  128. return (vec4(*this) *= f);
  129. }
  130.  
  131. inline vec4 operator * (const vec4 &v) const
  132. {
  133. return (vec4(*this) *= v);
  134. }
  135.  
  136. inline vec4 operator / (float f) const
  137. {
  138. return (vec4(*this) /= f);
  139. }
  140.  
  141. inline vec4 operator / (const vec4 &v) const
  142. {
  143. return (vec4(*this) /= v);
  144. }
  145.  
  146. // ----------------------------------------------------------------- //
  147.  
  148. // Dot product
  149. inline float dot(const vec4 &v) const {
  150. __m128 res = _mm_mul_ps(m, v.m);
  151. res = _mm_hadd_ps(res, res);
  152. res = _mm_hadd_ps(res, res);
  153.  
  154. return *(float*)&res;
  155. }
  156.  
  157. // Length / Megnitude of a vector
  158. inline float length() const {
  159. return sqrtf(dot(*this));
  160. }
  161.  
  162. // Normalize of vector (to unit length)
  163. inline vec4 normalize() {
  164. __m128 len = _mm_mul_ps(m, m);
  165. len = _mm_hadd_ps(len, len);
  166. len = _mm_hadd_ps(len, len);
  167. len = _mm_sqrt_ps(len);
  168. m = _mm_div_ps(m, len);
  169.  
  170. return *this;
  171. }
  172.  
  173. // Unit vector copy
  174. inline vec4 unit() const {
  175. return vec4(*this).normalize();
  176. }
  177.  
  178. // Linear Interpolation
  179. inline vec4 lerp(const vec4 &v, float f) {
  180. *this -= v;
  181. *this *= f;
  182. *this += v;
  183.  
  184. return *this;
  185. }
  186.  
  187. // Linear Interpolation
  188. inline vec4 lerp(const vec4 &v0, const vec4 &v1, float f) {
  189. return *this += ((v1 - v0) * f);
  190. }
  191.  
  192. // ----------------------------------------------------------------- //
  193.  
  194. // Vertex / Vector
  195. struct {
  196. float x, y, z, w;
  197. };
  198.  
  199. // Color
  200. struct {
  201. float r, g, b, a;
  202. };
  203. // Texture coordinates
  204. struct {
  205. float s, t, p, q;
  206. };
  207.  
  208. private:
  209. // SSE register
  210. __m128 m;
  211.  
  212. // SSE compatible constructor
  213. inline vec4(const __m128 &_m) {
  214. m = _m;
  215. }
  216. } vec4;
Add Comment
Please, Sign In to add comment