vec3.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /******************************************************************************
  2. Copyright (C) 2013 by Hugh Bailey <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. #ifndef VECT_H
  15. #define VECT_H
  16. #include "math-defs.h"
  17. #include <xmmintrin.h>
  18. #ifdef __cplusplus
  19. extern "C" {
  20. #endif
  21. struct plane;
  22. struct matrix3;
  23. struct quat;
  24. struct vec3 {
  25. union {
  26. struct {
  27. float x, y, z, w;
  28. };
  29. float ptr[4];
  30. __m128 m;
  31. };
  32. };
  33. static inline void vec3_zero(struct vec3 *v)
  34. {
  35. v->m = _mm_setzero_ps();
  36. }
  37. static inline void vec3_set(struct vec3 *dst, float x, float y, float z)
  38. {
  39. dst->m = _mm_set_ps(0.0f, z, y, x);
  40. }
  41. static inline void vec3_copy(struct vec3 *dst, const struct vec3 *v)
  42. {
  43. dst->m = v->m;
  44. }
  45. static inline void vec3_add(struct vec3 *dst, const struct vec3 *v1,
  46. const struct vec3 *v2)
  47. {
  48. dst->m = _mm_add_ps(v1->m, v2->m);
  49. dst->w = 0.0f;
  50. }
  51. static inline void vec3_sub(struct vec3 *dst, const struct vec3 *v1,
  52. const struct vec3 *v2)
  53. {
  54. dst->m = _mm_sub_ps(v1->m, v2->m);
  55. dst->w = 0.0f;
  56. }
  57. static inline void vec3_mul(struct vec3 *dst, const struct vec3 *v1,
  58. const struct vec3 *v2)
  59. {
  60. dst->m = _mm_mul_ps(v1->m, v2->m);
  61. }
  62. static inline void vec3_div(struct vec3 *dst, const struct vec3 *v1,
  63. const struct vec3 *v2)
  64. {
  65. dst->m = _mm_div_ps(v1->m, v2->m);
  66. dst->w = 0.0f;
  67. }
  68. static inline void vec3_addf(struct vec3 *dst, const struct vec3 *v,
  69. float f)
  70. {
  71. dst->m = _mm_add_ps(v->m, _mm_set1_ps(f));
  72. dst->w = 0.0f;
  73. }
  74. static inline void vec3_subf(struct vec3 *dst, const struct vec3 *v,
  75. float f)
  76. {
  77. dst->m = _mm_sub_ps(v->m, _mm_set1_ps(f));
  78. dst->w = 0.0f;
  79. }
  80. static inline void vec3_mulf(struct vec3 *dst, const struct vec3 *v,
  81. float f)
  82. {
  83. dst->m = _mm_mul_ps(v->m, _mm_set1_ps(f));
  84. }
  85. static inline void vec3_divf(struct vec3 *dst, const struct vec3 *v,
  86. float f)
  87. {
  88. dst->m = _mm_div_ps(v->m, _mm_set1_ps(f));
  89. dst->w = 0.0f;
  90. }
  91. static inline float vec3_dot(const struct vec3 *v1, const struct vec3 *v2)
  92. {
  93. struct vec3 add;
  94. __m128 mul = _mm_mul_ps(v1->m, v2->m);
  95. add.m = _mm_add_ps(_mm_movehl_ps(mul, mul), mul);
  96. add.m = _mm_add_ps(_mm_shuffle_ps(add.m, add.m, 0x55), add.m);
  97. return add.x;
  98. }
  99. static inline void vec3_cross(struct vec3 *dst, const struct vec3 *v1,
  100. const struct vec3 *v2)
  101. {
  102. __m128 s1v1 = _mm_shuffle_ps(v1->m, v1->m, _MM_SHUFFLE(3, 0, 2, 1));
  103. __m128 s1v2 = _mm_shuffle_ps(v2->m, v2->m, _MM_SHUFFLE(3, 1, 0, 2));
  104. __m128 s2v1 = _mm_shuffle_ps(v1->m, v1->m, _MM_SHUFFLE(3, 1, 0, 2));
  105. __m128 s2v2 = _mm_shuffle_ps(v2->m, v2->m, _MM_SHUFFLE(3, 0, 2, 1));
  106. dst->m = _mm_sub_ps(_mm_mul_ps(s1v1, s1v2), _mm_mul_ps(s2v1, s2v2));
  107. }
  108. static inline void vec3_neg(struct vec3 *dst, const struct vec3 *v)
  109. {
  110. dst->x = -dst->x;
  111. dst->y = -dst->y;
  112. dst->z = -dst->z;
  113. }
  114. static inline float vec3_len(const struct vec3 *v)
  115. {
  116. float dot_val = vec3_dot(v, v);
  117. return (dot_val > 0.0f) ? sqrtf(dot_val) : 0.0f;
  118. }
  119. static inline float vec3_dist(const struct vec3 *v1, const struct vec3 *v2)
  120. {
  121. struct vec3 temp;
  122. float dot_val;
  123. vec3_sub(&temp, v1, v2);
  124. dot_val = vec3_dot(&temp, &temp);
  125. return (dot_val > 0.0f) ? sqrtf(dot_val) : 0.0f;
  126. }
  127. static inline void vec3_norm(struct vec3 *dst, const struct vec3 *v)
  128. {
  129. float dot_val = vec3_dot(v, v);
  130. dst->m = (dot_val > 0.0f) ?
  131. _mm_mul_ps(v->m, _mm_set1_ps(1.0f/sqrtf(dot_val))) :
  132. _mm_setzero_ps();
  133. }
  134. static inline bool vec3_close(const struct vec3 *v1, const struct vec3 *v2,
  135. float epsilon)
  136. {
  137. struct vec3 test;
  138. vec3_sub(&test, v1, v2);
  139. return test.x < epsilon && test.y < epsilon && test.z < epsilon;
  140. }
  141. static inline void vec3_min(struct vec3 *dst, const struct vec3 *v1,
  142. const struct vec3 *v2)
  143. {
  144. dst->m = _mm_min_ps(v1->m, v2->m);
  145. dst->w = 0.0f;
  146. }
  147. static inline void vec3_minf(struct vec3 *dst, const struct vec3 *v,
  148. float f)
  149. {
  150. dst->m = _mm_min_ps(v->m, _mm_set1_ps(f));
  151. dst->w = 0.0f;
  152. }
  153. static inline void vec3_max(struct vec3 *dst, const struct vec3 *v1,
  154. const struct vec3 *v2)
  155. {
  156. dst->m = _mm_max_ps(v1->m, v2->m);
  157. dst->w = 0.0f;
  158. }
  159. static inline void vec3_maxf(struct vec3 *dst, const struct vec3 *v,
  160. float f)
  161. {
  162. dst->m = _mm_max_ps(v->m, _mm_set1_ps(f));
  163. dst->w = 0.0f;
  164. }
  165. static inline void vec3_abs(struct vec3 *dst, const struct vec3 *v)
  166. {
  167. dst->x = fabsf(v->x);
  168. dst->y = fabsf(v->y);
  169. dst->z = fabsf(v->z);
  170. }
  171. static inline void vec3_floor(struct vec3 *dst, const struct vec3 *v)
  172. {
  173. dst->x = floorf(v->x);
  174. dst->y = floorf(v->y);
  175. dst->z = floorf(v->z);
  176. }
  177. static inline void vec3_ceil(struct vec3 *dst, const struct vec3 *v)
  178. {
  179. dst->x = ceilf(v->x);
  180. dst->y = ceilf(v->y);
  181. dst->z = ceilf(v->z);
  182. }
  183. EXPORT float vec3_plane_dist(const struct vec3 *v, const struct plane *p);
  184. EXPORT void vec3_rotate(struct vec3 *dst, const struct vec3 *v,
  185. const struct matrix3 *m);
  186. EXPORT void vec3_transform(struct vec3 *dst, const struct vec3 *v,
  187. const struct matrix3 *m);
  188. EXPORT void vec3_mirror(struct vec3 *dst, const struct vec3 *v,
  189. const struct plane *p);
  190. EXPORT void vec3_mirrorv(struct vec3 *dst, const struct vec3 *v,
  191. const struct vec3 *vec);
  192. EXPORT void vec3_rand(struct vec3 *dst, int positive_only);
  193. #ifdef __cplusplus
  194. }
  195. #endif
  196. #endif