vec4.h 5.9 KB

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