vec3.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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 2 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. #pragma once
  15. #include "math-defs.h"
  16. #include <xmmintrin.h>
  17. #ifdef __cplusplus
  18. extern "C" {
  19. #endif
  20. struct plane;
  21. struct matrix3;
  22. struct quat;
  23. struct vec3 {
  24. union {
  25. struct {
  26. float x, y, z, w;
  27. };
  28. float ptr[4];
  29. __m128 m;
  30. };
  31. };
  32. static inline void vec3_zero(struct vec3 *v)
  33. {
  34. v->m = _mm_setzero_ps();
  35. }
  36. static inline void vec3_set(struct vec3 *dst, float x, float y, float z)
  37. {
  38. dst->m = _mm_set_ps(0.0f, z, y, x);
  39. }
  40. static inline void vec3_copy(struct vec3 *dst, const struct vec3 *v)
  41. {
  42. dst->m = v->m;
  43. }
  44. static inline void vec3_add(struct vec3 *dst, const struct vec3 *v1,
  45. const struct vec3 *v2)
  46. {
  47. dst->m = _mm_add_ps(v1->m, v2->m);
  48. dst->w = 0.0f;
  49. }
  50. static inline void vec3_sub(struct vec3 *dst, const struct vec3 *v1,
  51. const struct vec3 *v2)
  52. {
  53. dst->m = _mm_sub_ps(v1->m, v2->m);
  54. dst->w = 0.0f;
  55. }
  56. static inline void vec3_mul(struct vec3 *dst, const struct vec3 *v1,
  57. const struct vec3 *v2)
  58. {
  59. dst->m = _mm_mul_ps(v1->m, v2->m);
  60. }
  61. static inline void vec3_div(struct vec3 *dst, const struct vec3 *v1,
  62. const struct vec3 *v2)
  63. {
  64. dst->m = _mm_div_ps(v1->m, v2->m);
  65. dst->w = 0.0f;
  66. }
  67. static inline void vec3_addf(struct vec3 *dst, const struct vec3 *v,
  68. float f)
  69. {
  70. dst->m = _mm_add_ps(v->m, _mm_set1_ps(f));
  71. dst->w = 0.0f;
  72. }
  73. static inline void vec3_subf(struct vec3 *dst, const struct vec3 *v,
  74. float f)
  75. {
  76. dst->m = _mm_sub_ps(v->m, _mm_set1_ps(f));
  77. dst->w = 0.0f;
  78. }
  79. static inline void vec3_mulf(struct vec3 *dst, const struct vec3 *v,
  80. float f)
  81. {
  82. dst->m = _mm_mul_ps(v->m, _mm_set1_ps(f));
  83. }
  84. static inline void vec3_divf(struct vec3 *dst, const struct vec3 *v,
  85. float f)
  86. {
  87. dst->m = _mm_div_ps(v->m, _mm_set1_ps(f));
  88. dst->w = 0.0f;
  89. }
  90. static inline float vec3_dot(const struct vec3 *v1, const struct vec3 *v2)
  91. {
  92. struct vec3 add;
  93. __m128 mul = _mm_mul_ps(v1->m, v2->m);
  94. add.m = _mm_add_ps(_mm_movehl_ps(mul, mul), mul);
  95. add.m = _mm_add_ps(_mm_shuffle_ps(add.m, add.m, 0x55), add.m);
  96. return add.x;
  97. }
  98. static inline void vec3_cross(struct vec3 *dst, const struct vec3 *v1,
  99. const struct vec3 *v2)
  100. {
  101. __m128 s1v1 = _mm_shuffle_ps(v1->m, v1->m, _MM_SHUFFLE(3, 0, 2, 1));
  102. __m128 s1v2 = _mm_shuffle_ps(v2->m, v2->m, _MM_SHUFFLE(3, 1, 0, 2));
  103. __m128 s2v1 = _mm_shuffle_ps(v1->m, v1->m, _MM_SHUFFLE(3, 1, 0, 2));
  104. __m128 s2v2 = _mm_shuffle_ps(v2->m, v2->m, _MM_SHUFFLE(3, 0, 2, 1));
  105. dst->m = _mm_sub_ps(_mm_mul_ps(s1v1, s1v2), _mm_mul_ps(s2v1, s2v2));
  106. }
  107. static inline void vec3_neg(struct vec3 *dst, const struct vec3 *v)
  108. {
  109. dst->x = -v->x;
  110. dst->y = -v->y;
  111. dst->z = -v->z;
  112. }
  113. static inline float vec3_len(const struct vec3 *v)
  114. {
  115. float dot_val = vec3_dot(v, v);
  116. return (dot_val > 0.0f) ? sqrtf(dot_val) : 0.0f;
  117. }
  118. static inline float vec3_dist(const struct vec3 *v1, const struct vec3 *v2)
  119. {
  120. struct vec3 temp;
  121. float dot_val;
  122. vec3_sub(&temp, v1, v2);
  123. dot_val = vec3_dot(&temp, &temp);
  124. return (dot_val > 0.0f) ? sqrtf(dot_val) : 0.0f;
  125. }
  126. static inline void vec3_norm(struct vec3 *dst, const struct vec3 *v)
  127. {
  128. float dot_val = vec3_dot(v, v);
  129. dst->m = (dot_val > 0.0f) ?
  130. _mm_mul_ps(v->m, _mm_set1_ps(1.0f/sqrtf(dot_val))) :
  131. _mm_setzero_ps();
  132. }
  133. static inline bool vec3_close(const struct vec3 *v1, const struct vec3 *v2,
  134. float epsilon)
  135. {
  136. struct vec3 test;
  137. vec3_sub(&test, v1, v2);
  138. return test.x < epsilon && test.y < epsilon && test.z < epsilon;
  139. }
  140. static inline void vec3_min(struct vec3 *dst, const struct vec3 *v1,
  141. const struct vec3 *v2)
  142. {
  143. dst->m = _mm_min_ps(v1->m, v2->m);
  144. dst->w = 0.0f;
  145. }
  146. static inline void vec3_minf(struct vec3 *dst, const struct vec3 *v,
  147. float f)
  148. {
  149. dst->m = _mm_min_ps(v->m, _mm_set1_ps(f));
  150. dst->w = 0.0f;
  151. }
  152. static inline void vec3_max(struct vec3 *dst, const struct vec3 *v1,
  153. const struct vec3 *v2)
  154. {
  155. dst->m = _mm_max_ps(v1->m, v2->m);
  156. dst->w = 0.0f;
  157. }
  158. static inline void vec3_maxf(struct vec3 *dst, const struct vec3 *v,
  159. float f)
  160. {
  161. dst->m = _mm_max_ps(v->m, _mm_set1_ps(f));
  162. dst->w = 0.0f;
  163. }
  164. static inline void vec3_abs(struct vec3 *dst, const struct vec3 *v)
  165. {
  166. dst->x = fabsf(v->x);
  167. dst->y = fabsf(v->y);
  168. dst->z = fabsf(v->z);
  169. }
  170. static inline void vec3_floor(struct vec3 *dst, const struct vec3 *v)
  171. {
  172. dst->x = floorf(v->x);
  173. dst->y = floorf(v->y);
  174. dst->z = floorf(v->z);
  175. }
  176. static inline void vec3_ceil(struct vec3 *dst, const struct vec3 *v)
  177. {
  178. dst->x = ceilf(v->x);
  179. dst->y = ceilf(v->y);
  180. dst->z = ceilf(v->z);
  181. }
  182. EXPORT float vec3_plane_dist(const struct vec3 *v, const struct plane *p);
  183. EXPORT void vec3_rotate(struct vec3 *dst, const struct vec3 *v,
  184. const struct matrix3 *m);
  185. EXPORT void vec3_transform(struct vec3 *dst, const struct vec3 *v,
  186. const struct matrix3 *m);
  187. EXPORT void vec3_mirror(struct vec3 *dst, const struct vec3 *v,
  188. const struct plane *p);
  189. EXPORT void vec3_mirrorv(struct vec3 *dst, const struct vec3 *v,
  190. const struct vec3 *vec);
  191. EXPORT void vec3_rand(struct vec3 *dst, int positive_only);
  192. #ifdef __cplusplus
  193. }
  194. #endif