vec4.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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,
  66. float f)
  67. {
  68. dst->m = _mm_add_ps(v->m, _mm_set1_ps(f));
  69. }
  70. static inline void vec4_subf(struct vec4 *dst, const struct vec4 *v,
  71. float f)
  72. {
  73. dst->m = _mm_sub_ps(v->m, _mm_set1_ps(f));
  74. }
  75. static inline void vec4_mulf(struct vec4 *dst, const struct vec4 *v,
  76. float f)
  77. {
  78. dst->m = _mm_mul_ps(v->m, _mm_set1_ps(f));
  79. }
  80. static inline void vec4_divf(struct vec4 *dst, const struct vec4 *v,
  81. float f)
  82. {
  83. dst->m = _mm_div_ps(v->m, _mm_set1_ps(f));
  84. }
  85. static inline float vec4_dot(const struct vec4 *v1, const struct vec4 *v2)
  86. {
  87. struct vec4 add;
  88. __m128 mul = _mm_mul_ps(v1->m, v2->m);
  89. add.m = _mm_add_ps(_mm_movehl_ps(mul, mul), mul);
  90. add.m = _mm_add_ps(_mm_shuffle_ps(add.m, add.m, 0x55), add.m);
  91. return add.x;
  92. }
  93. static inline void vec4_neg(struct vec4 *dst, const struct vec4 *v)
  94. {
  95. dst->x = -v->x;
  96. dst->y = -v->y;
  97. dst->z = -v->z;
  98. dst->w = -v->w;
  99. }
  100. static inline float vec4_len(const struct vec4 *v)
  101. {
  102. float dot_val = vec4_dot(v, v);
  103. return (dot_val > 0.0f) ? sqrtf(dot_val) : 0.0f;
  104. }
  105. static inline float vec4_dist(const struct vec4 *v1, const struct vec4 *v2)
  106. {
  107. struct vec4 temp;
  108. float dot_val;
  109. vec4_sub(&temp, v1, v2);
  110. dot_val = vec4_dot(&temp, &temp);
  111. return (dot_val > 0.0f) ? sqrtf(dot_val) : 0.0f;
  112. }
  113. static inline void vec4_norm(struct vec4 *dst, const struct vec4 *v)
  114. {
  115. float dot_val = vec4_dot(v, v);
  116. dst->m = (dot_val > 0.0f) ?
  117. _mm_mul_ps(v->m, _mm_set1_ps(1.0f/sqrtf(dot_val))) :
  118. _mm_setzero_ps();
  119. }
  120. static inline int vec4_close(const struct vec4 *v1, const struct vec4 *v2,
  121. float epsilon)
  122. {
  123. struct vec4 test;
  124. vec4_sub(&test, v1, v2);
  125. return test.x < epsilon &&
  126. test.y < epsilon &&
  127. test.z < epsilon &&
  128. test.w < epsilon;
  129. }
  130. static inline void vec4_min(struct vec4 *dst, const struct vec4 *v1,
  131. const struct vec4 *v2)
  132. {
  133. dst->m = _mm_min_ps(v1->m, v2->m);
  134. }
  135. static inline void vec4_minf(struct vec4 *dst, const struct vec4 *v,
  136. float f)
  137. {
  138. dst->m = _mm_min_ps(v->m, _mm_set1_ps(f));
  139. }
  140. static inline void vec4_max(struct vec4 *dst, const struct vec4 *v1,
  141. const struct vec4 *v2)
  142. {
  143. dst->m = _mm_max_ps(v1->m, v2->m);
  144. }
  145. static inline void vec4_maxf(struct vec4 *dst, const struct vec4 *v,
  146. float f)
  147. {
  148. dst->m = _mm_max_ps(v->m, _mm_set1_ps(f));
  149. }
  150. static inline void vec4_abs(struct vec4 *dst, const struct vec4 *v)
  151. {
  152. dst->x = fabsf(v->x);
  153. dst->y = fabsf(v->y);
  154. dst->z = fabsf(v->z);
  155. dst->w = fabsf(v->w);
  156. }
  157. static inline void vec4_floor(struct vec4 *dst, const struct vec4 *v)
  158. {
  159. dst->x = floorf(v->x);
  160. dst->y = floorf(v->y);
  161. dst->z = floorf(v->z);
  162. dst->w = floorf(v->w);
  163. }
  164. static inline void vec4_ceil(struct vec4 *dst, const struct vec4 *v)
  165. {
  166. dst->x = ceilf(v->x);
  167. dst->y = ceilf(v->y);
  168. dst->z = ceilf(v->z);
  169. dst->w = ceilf(v->w);
  170. }
  171. static inline uint32_t vec4_to_rgba(const struct vec4 *src)
  172. {
  173. uint32_t val;
  174. val = (uint32_t)((double)src->x * 255.0);
  175. val |= (uint32_t)((double)src->y * 255.0) << 8;
  176. val |= (uint32_t)((double)src->z * 255.0) << 16;
  177. val |= (uint32_t)((double)src->w * 255.0) << 24;
  178. return val;
  179. }
  180. static inline uint32_t vec4_to_bgra(const struct vec4 *src)
  181. {
  182. uint32_t val;
  183. val = (uint32_t)((double)src->z * 255.0);
  184. val |= (uint32_t)((double)src->y * 255.0) << 8;
  185. val |= (uint32_t)((double)src->x * 255.0) << 16;
  186. val |= (uint32_t)((double)src->w * 255.0) << 24;
  187. return val;
  188. }
  189. static inline void vec4_from_rgba(struct vec4 *dst, uint32_t rgba)
  190. {
  191. dst->x = (float)((double)(rgba&0xFF) * (1.0/255.0));
  192. rgba >>= 8;
  193. dst->y = (float)((double)(rgba&0xFF) * (1.0/255.0));
  194. rgba >>= 8;
  195. dst->z = (float)((double)(rgba&0xFF) * (1.0/255.0));
  196. rgba >>= 8;
  197. dst->w = (float)((double)(rgba&0xFF) * (1.0/255.0));
  198. }
  199. static inline void vec4_from_bgra(struct vec4 *dst, uint32_t bgra)
  200. {
  201. dst->z = (float)((double)(bgra&0xFF) * (1.0/255.0));
  202. bgra >>= 8;
  203. dst->y = (float)((double)(bgra&0xFF) * (1.0/255.0));
  204. bgra >>= 8;
  205. dst->x = (float)((double)(bgra&0xFF) * (1.0/255.0));
  206. bgra >>= 8;
  207. dst->w = (float)((double)(bgra&0xFF) * (1.0/255.0));
  208. }
  209. EXPORT void vec4_transform(struct vec4 *dst, const struct vec4 *v,
  210. const struct matrix4 *m);
  211. #ifdef __cplusplus
  212. }
  213. #endif