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