1
0

vec3-ext.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /*
  2. * Copyright (c), Recep Aslantas.
  3. *
  4. * MIT License (MIT), http://opensource.org/licenses/MIT
  5. * Full license can be found in the LICENSE file
  6. */
  7. /*!
  8. * @brief SIMD like functions
  9. */
  10. /*
  11. Functions:
  12. CGLM_INLINE vec3s glms_vec3_broadcast(float val);
  13. CGLM_INLINE vec3s glms_vec3_fill(float val);
  14. CGLM_INLINE bool glms_vec3_eq(vec3s v, float val);
  15. CGLM_INLINE bool glms_vec3_eq_eps(vec3s v, float val);
  16. CGLM_INLINE bool glms_vec3_eq_all(vec3s v);
  17. CGLM_INLINE bool glms_vec3_eqv(vec3s a, vec3s b);
  18. CGLM_INLINE bool glms_vec3_eqv_eps(vec3s a, vec3s b);
  19. CGLM_INLINE float glms_vec3_max(vec3s v);
  20. CGLM_INLINE float glms_vec3_min(vec3s v);
  21. CGLM_INLINE bool glms_vec3_isnan(vec3s v);
  22. CGLM_INLINE bool glms_vec3_isinf(vec3s v);
  23. CGLM_INLINE bool glms_vec3_isvalid(vec3s v);
  24. CGLM_INLINE vec3s glms_vec3_sign(vec3s v);
  25. CGLM_INLINE vec3s glms_vec3_abs(vec3s v);
  26. CGLM_INLINE vec3s glms_vec3_fract(vec3s v);
  27. CGLM_INLINE vec3s glms_vec3_floor(vec3s v);
  28. CGLM_INLINE vec3s glms_vec3_mods(vec3s v, float s);
  29. CGLM_INLINE vec3s glms_vec3_steps(float edge, vec3s v);
  30. CGLM_INLINE vec3s glms_vec3_stepr(vec3s edge, float v);
  31. CGLM_INLINE float glms_vec3_hadd(vec3s v);
  32. CGLM_INLINE vec3s glms_vec3_sqrt(vec3s v);
  33. */
  34. #ifndef cglms_vec3s_ext_h
  35. #define cglms_vec3s_ext_h
  36. #include "../common.h"
  37. #include "../types-struct.h"
  38. #include "../util.h"
  39. #include "../vec3-ext.h"
  40. /* api definition */
  41. #define glms_vec3_(NAME) CGLM_STRUCTAPI(vec3, NAME)
  42. /*!
  43. * @brief fill a vector with specified value
  44. *
  45. * @param[in] val value
  46. * @returns dest
  47. */
  48. CGLM_INLINE
  49. vec3s
  50. glms_vec3_(broadcast)(float val) {
  51. vec3s r;
  52. glm_vec3_broadcast(val, r.raw);
  53. return r;
  54. }
  55. /*!
  56. * @brief fill a vector with specified value
  57. *
  58. * @param[in] val value
  59. * @returns dest
  60. */
  61. CGLM_INLINE
  62. vec3s
  63. glms_vec3_(fill)(float val) {
  64. vec3s r;
  65. glm_vec3_fill(r.raw, val);
  66. return r;
  67. }
  68. /*!
  69. * @brief check if vector is equal to value (without epsilon)
  70. *
  71. * @param[in] v vector
  72. * @param[in] val value
  73. */
  74. CGLM_INLINE
  75. bool
  76. glms_vec3_(eq)(vec3s v, float val) {
  77. return glm_vec3_eq(v.raw, val);
  78. }
  79. /*!
  80. * @brief check if vector is equal to value (with epsilon)
  81. *
  82. * @param[in] v vector
  83. * @param[in] val value
  84. */
  85. CGLM_INLINE
  86. bool
  87. glms_vec3_(eq_eps)(vec3s v, float val) {
  88. return glm_vec3_eq_eps(v.raw, val);
  89. }
  90. /*!
  91. * @brief check if vector members are equal (without epsilon)
  92. *
  93. * @param[in] v vector
  94. */
  95. CGLM_INLINE
  96. bool
  97. glms_vec3_(eq_all)(vec3s v) {
  98. return glm_vec3_eq_all(v.raw);
  99. }
  100. /*!
  101. * @brief check if vector is equal to another (without epsilon)
  102. *
  103. * @param[in] a vector
  104. * @param[in] b vector
  105. */
  106. CGLM_INLINE
  107. bool
  108. glms_vec3_(eqv)(vec3s a, vec3s b) {
  109. return glm_vec3_eqv(a.raw, b.raw);
  110. }
  111. /*!
  112. * @brief check if vector is equal to another (with epsilon)
  113. *
  114. * @param[in] a vector
  115. * @param[in] b vector
  116. */
  117. CGLM_INLINE
  118. bool
  119. glms_vec3_(eqv_eps)(vec3s a, vec3s b) {
  120. return glm_vec3_eqv_eps(a.raw, b.raw);
  121. }
  122. /*!
  123. * @brief max value of vector
  124. *
  125. * @param[in] v vector
  126. */
  127. CGLM_INLINE
  128. float
  129. glms_vec3_(max)(vec3s v) {
  130. return glm_vec3_max(v.raw);
  131. }
  132. /*!
  133. * @brief min value of vector
  134. *
  135. * @param[in] v vector
  136. */
  137. CGLM_INLINE
  138. float
  139. glms_vec3_(min)(vec3s v) {
  140. return glm_vec3_min(v.raw);
  141. }
  142. /*!
  143. * @brief check if one of items is NaN (not a number)
  144. * you should only use this in DEBUG mode or very critical asserts
  145. *
  146. * @param[in] v vector
  147. */
  148. CGLM_INLINE
  149. bool
  150. glms_vec3_(isnan)(vec3s v) {
  151. return glm_vec3_isnan(v.raw);
  152. }
  153. /*!
  154. * @brief check if one of items is INFINITY
  155. * you should only use this in DEBUG mode or very critical asserts
  156. *
  157. * @param[in] v vector
  158. */
  159. CGLM_INLINE
  160. bool
  161. glms_vec3_(isinf)(vec3s v) {
  162. return glm_vec3_isinf(v.raw);
  163. }
  164. /*!
  165. * @brief check if all items are valid number
  166. * you should only use this in DEBUG mode or very critical asserts
  167. *
  168. * @param[in] v vector
  169. */
  170. CGLM_INLINE
  171. bool
  172. glms_vec3_(isvalid)(vec3s v) {
  173. return glm_vec3_isvalid(v.raw);
  174. }
  175. /*!
  176. * @brief get sign of 32 bit float as +1, -1, 0
  177. *
  178. * Important: It returns 0 for zero/NaN input
  179. *
  180. * @param v vector
  181. * @returns sign vector
  182. */
  183. CGLM_INLINE
  184. vec3s
  185. glms_vec3_(sign)(vec3s v) {
  186. vec3s r;
  187. glm_vec3_sign(v.raw, r.raw);
  188. return r;
  189. }
  190. /*!
  191. * @brief absolute value of each vector item
  192. *
  193. * @param[in] v vector
  194. * @return destination vector
  195. */
  196. CGLM_INLINE
  197. vec3s
  198. glms_vec3_(abs)(vec3s v) {
  199. vec3s r;
  200. glm_vec3_abs(v.raw, r.raw);
  201. return r;
  202. }
  203. /*!
  204. * @brief fractional part of each vector item
  205. *
  206. * @param[in] v vector
  207. * @return dest destination vector
  208. */
  209. CGLM_INLINE
  210. vec3s
  211. glms_vec3_(fract)(vec3s v) {
  212. vec3s r;
  213. glm_vec3_fract(v.raw, r.raw);
  214. return r;
  215. }
  216. /*!
  217. * @brief floor of each vector item
  218. *
  219. * @param[in] v vector
  220. * @return dest destination vector
  221. */
  222. CGLM_INLINE
  223. vec3s
  224. glms_vec3_(floor)(vec3s v) {
  225. vec3s r;
  226. glm_vec3_floor(v.raw, r.raw);
  227. return r;
  228. }
  229. /*!
  230. * @brief mod of each vector item by scalar
  231. *
  232. * @param[in] v vector
  233. * @param[in] s scalar
  234. * @returns destination vector
  235. */
  236. CGLM_INLINE
  237. vec3s
  238. glms_vec3_(mods)(vec3s v, float s) {
  239. vec3s r;
  240. glm_vec3_mods(v.raw, s, r.raw);
  241. return r;
  242. }
  243. /*!
  244. * @brief threshold each vector item with scalar
  245. * condition is: (x[i] < edge) ? 0.0 : 1.0
  246. *
  247. * @param[in] edge threshold
  248. * @param[in] x vector to test against threshold
  249. * @returns destination
  250. */
  251. CGLM_INLINE
  252. vec3s
  253. glms_vec3_(steps)(float edge, vec3s x) {
  254. vec3s r;
  255. glm_vec3_steps(edge, x.raw, r.raw);
  256. return r;
  257. }
  258. /*!
  259. * @brief threshold a value with *vector* as the threshold
  260. * condition is: (x < edge[i]) ? 0.0 : 1.0
  261. *
  262. * @param[in] edge threshold vector
  263. * @param[in] x value to test against threshold
  264. * @returns destination
  265. */
  266. CGLM_INLINE
  267. vec3s
  268. glms_vec3_(stepr)(vec3s edge, float x) {
  269. vec3s r;
  270. glm_vec3_stepr(edge.raw, x, r.raw);
  271. return r;
  272. }
  273. /*!
  274. * @brief vector reduction by summation
  275. * @warning could overflow
  276. *
  277. * @param[in] v vector
  278. * @return sum of all vector's elements
  279. */
  280. CGLM_INLINE
  281. float
  282. glms_vec3_(hadd)(vec3s v) {
  283. return glm_vec3_hadd(v.raw);
  284. }
  285. /*!
  286. * @brief square root of each vector item
  287. *
  288. * @param[in] v vector
  289. * @returns destination vector
  290. */
  291. CGLM_INLINE
  292. vec3s
  293. glms_vec3_(sqrt)(vec3s v) {
  294. vec3s r;
  295. glm_vec3_sqrt(v.raw, r.raw);
  296. return r;
  297. }
  298. #endif /* cglms_vec3s_ext_h */