vec4.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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 vec3;
  21. struct matrix4;
  22. struct vec4 {
  23. union {
  24. struct {
  25. float x, y, z, w;
  26. };
  27. float ptr[4];
  28. __m128 m;
  29. };
  30. };
  31. static inline void vec4_zero(struct vec4 *v)
  32. {
  33. v->m = _mm_setzero_ps();
  34. }
  35. static inline void vec4_set(struct vec4 *dst, float x, float y, float z,
  36. float w)
  37. {
  38. dst->m = _mm_set_ps(w, z, y, x);
  39. }
  40. static inline void vec4_copy(struct vec4 *dst, const struct vec4 *v)
  41. {
  42. dst->m = v->m;
  43. }
  44. EXPORT void vec4_from_vec3(struct vec4 *dst, const struct vec3 *v);
  45. static inline void vec4_add(struct vec4 *dst, const struct vec4 *v1,
  46. const struct vec4 *v2)
  47. {
  48. dst->m = _mm_add_ps(v1->m, v2->m);
  49. }
  50. static inline void vec4_sub(struct vec4 *dst, const struct vec4 *v1,
  51. const struct vec4 *v2)
  52. {
  53. dst->m = _mm_sub_ps(v1->m, v2->m);
  54. }
  55. static inline void vec4_mul(struct vec4 *dst, const struct vec4 *v1,
  56. const struct vec4 *v2)
  57. {
  58. dst->m = _mm_mul_ps(v1->m, v2->m);
  59. }
  60. static inline void vec4_div(struct vec4 *dst, const struct vec4 *v1,
  61. const struct vec4 *v2)
  62. {
  63. dst->m = _mm_div_ps(v1->m, v2->m);
  64. }
  65. static inline void vec4_addf(struct vec4 *dst, const struct vec4 *v, float f)
  66. {
  67. dst->m = _mm_add_ps(v->m, _mm_set1_ps(f));
  68. }
  69. static inline void vec4_subf(struct vec4 *dst, const struct vec4 *v, float f)
  70. {
  71. dst->m = _mm_sub_ps(v->m, _mm_set1_ps(f));
  72. }
  73. static inline void vec4_mulf(struct vec4 *dst, const struct vec4 *v, float f)
  74. {
  75. dst->m = _mm_mul_ps(v->m, _mm_set1_ps(f));
  76. }
  77. static inline void vec4_divf(struct vec4 *dst, const struct vec4 *v, float f)
  78. {
  79. dst->m = _mm_div_ps(v->m, _mm_set1_ps(f));
  80. }
  81. static inline float vec4_dot(const struct vec4 *v1, const struct vec4 *v2)
  82. {
  83. struct vec4 add;
  84. __m128 mul = _mm_mul_ps(v1->m, v2->m);
  85. add.m = _mm_add_ps(_mm_movehl_ps(mul, mul), mul);
  86. add.m = _mm_add_ps(_mm_shuffle_ps(add.m, add.m, 0x55), add.m);
  87. return add.x;
  88. }
  89. static inline void vec4_neg(struct vec4 *dst, const struct vec4 *v)
  90. {
  91. dst->x = -v->x;
  92. dst->y = -v->y;
  93. dst->z = -v->z;
  94. dst->w = -v->w;
  95. }
  96. static inline float vec4_len(const struct vec4 *v)
  97. {
  98. float dot_val = vec4_dot(v, v);
  99. return (dot_val > 0.0f) ? sqrtf(dot_val) : 0.0f;
  100. }
  101. static inline float vec4_dist(const struct vec4 *v1, const struct vec4 *v2)
  102. {
  103. struct vec4 temp;
  104. float dot_val;
  105. vec4_sub(&temp, v1, v2);
  106. dot_val = vec4_dot(&temp, &temp);
  107. return (dot_val > 0.0f) ? sqrtf(dot_val) : 0.0f;
  108. }
  109. static inline void vec4_norm(struct vec4 *dst, const struct vec4 *v)
  110. {
  111. float dot_val = vec4_dot(v, v);
  112. dst->m = (dot_val > 0.0f)
  113. ? _mm_mul_ps(v->m, _mm_set1_ps(1.0f / sqrtf(dot_val)))
  114. : _mm_setzero_ps();
  115. }
  116. static inline int vec4_close(const struct vec4 *v1, const struct vec4 *v2,
  117. float epsilon)
  118. {
  119. struct vec4 test;
  120. vec4_sub(&test, v1, v2);
  121. return test.x < epsilon && test.y < epsilon && test.z < epsilon &&
  122. test.w < epsilon;
  123. }
  124. static inline void vec4_min(struct vec4 *dst, const struct vec4 *v1,
  125. const struct vec4 *v2)
  126. {
  127. dst->m = _mm_min_ps(v1->m, v2->m);
  128. }
  129. static inline void vec4_minf(struct vec4 *dst, const struct vec4 *v, float f)
  130. {
  131. dst->m = _mm_min_ps(v->m, _mm_set1_ps(f));
  132. }
  133. static inline void vec4_max(struct vec4 *dst, const struct vec4 *v1,
  134. const struct vec4 *v2)
  135. {
  136. dst->m = _mm_max_ps(v1->m, v2->m);
  137. }
  138. static inline void vec4_maxf(struct vec4 *dst, const struct vec4 *v, float f)
  139. {
  140. dst->m = _mm_max_ps(v->m, _mm_set1_ps(f));
  141. }
  142. static inline void vec4_abs(struct vec4 *dst, const struct vec4 *v)
  143. {
  144. dst->x = fabsf(v->x);
  145. dst->y = fabsf(v->y);
  146. dst->z = fabsf(v->z);
  147. dst->w = fabsf(v->w);
  148. }
  149. static inline void vec4_floor(struct vec4 *dst, const struct vec4 *v)
  150. {
  151. dst->x = floorf(v->x);
  152. dst->y = floorf(v->y);
  153. dst->z = floorf(v->z);
  154. dst->w = floorf(v->w);
  155. }
  156. static inline void vec4_ceil(struct vec4 *dst, const struct vec4 *v)
  157. {
  158. dst->x = ceilf(v->x);
  159. dst->y = ceilf(v->y);
  160. dst->z = ceilf(v->z);
  161. dst->w = ceilf(v->w);
  162. }
  163. static inline uint32_t vec4_to_rgba(const struct vec4 *src)
  164. {
  165. uint32_t val;
  166. val = (uint32_t)((double)src->x * 255.0);
  167. val |= (uint32_t)((double)src->y * 255.0) << 8;
  168. val |= (uint32_t)((double)src->z * 255.0) << 16;
  169. val |= (uint32_t)((double)src->w * 255.0) << 24;
  170. return val;
  171. }
  172. static inline uint32_t vec4_to_bgra(const struct vec4 *src)
  173. {
  174. uint32_t val;
  175. val = (uint32_t)((double)src->z * 255.0);
  176. val |= (uint32_t)((double)src->y * 255.0) << 8;
  177. val |= (uint32_t)((double)src->x * 255.0) << 16;
  178. val |= (uint32_t)((double)src->w * 255.0) << 24;
  179. return val;
  180. }
  181. static inline void vec4_from_rgba(struct vec4 *dst, uint32_t rgba)
  182. {
  183. dst->x = (float)((double)(rgba & 0xFF) * (1.0 / 255.0));
  184. rgba >>= 8;
  185. dst->y = (float)((double)(rgba & 0xFF) * (1.0 / 255.0));
  186. rgba >>= 8;
  187. dst->z = (float)((double)(rgba & 0xFF) * (1.0 / 255.0));
  188. rgba >>= 8;
  189. dst->w = (float)((double)(rgba & 0xFF) * (1.0 / 255.0));
  190. }
  191. static inline void vec4_from_bgra(struct vec4 *dst, uint32_t bgra)
  192. {
  193. dst->z = (float)((double)(bgra & 0xFF) * (1.0 / 255.0));
  194. bgra >>= 8;
  195. dst->y = (float)((double)(bgra & 0xFF) * (1.0 / 255.0));
  196. bgra >>= 8;
  197. dst->x = (float)((double)(bgra & 0xFF) * (1.0 / 255.0));
  198. bgra >>= 8;
  199. dst->w = (float)((double)(bgra & 0xFF) * (1.0 / 255.0));
  200. }
  201. EXPORT void vec4_transform(struct vec4 *dst, const struct vec4 *v,
  202. const struct matrix4 *m);
  203. #ifdef __cplusplus
  204. }
  205. #endif