util_math_float3.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. /*
  2. * Copyright 2011-2017 Blender Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef __UTIL_MATH_FLOAT3_H__
  17. #define __UTIL_MATH_FLOAT3_H__
  18. #ifndef __UTIL_MATH_H__
  19. # error "Do not include this file directly, include util_types.h instead."
  20. #endif
  21. CCL_NAMESPACE_BEGIN
  22. /*******************************************************************************
  23. * Declaration.
  24. */
  25. #ifndef __KERNEL_OPENCL__
  26. ccl_device_inline float3 operator-(const float3 &a);
  27. ccl_device_inline float3 operator*(const float3 &a, const float3 &b);
  28. ccl_device_inline float3 operator*(const float3 &a, const float f);
  29. ccl_device_inline float3 operator*(const float f, const float3 &a);
  30. ccl_device_inline float3 operator/(const float f, const float3 &a);
  31. ccl_device_inline float3 operator/(const float3 &a, const float f);
  32. ccl_device_inline float3 operator/(const float3 &a, const float3 &b);
  33. ccl_device_inline float3 operator+(const float3 &a, const float3 &b);
  34. ccl_device_inline float3 operator-(const float3 &a, const float3 &b);
  35. ccl_device_inline float3 operator+=(float3 &a, const float3 &b);
  36. ccl_device_inline float3 operator-=(float3 &a, const float3 &b);
  37. ccl_device_inline float3 operator*=(float3 &a, const float3 &b);
  38. ccl_device_inline float3 operator*=(float3 &a, float f);
  39. ccl_device_inline float3 operator/=(float3 &a, const float3 &b);
  40. ccl_device_inline float3 operator/=(float3 &a, float f);
  41. ccl_device_inline bool operator==(const float3 &a, const float3 &b);
  42. ccl_device_inline bool operator!=(const float3 &a, const float3 &b);
  43. ccl_device_inline float dot(const float3 &a, const float3 &b);
  44. ccl_device_inline float dot_xy(const float3 &a, const float3 &b);
  45. ccl_device_inline float3 cross(const float3 &a, const float3 &b);
  46. ccl_device_inline float3 normalize(const float3 &a);
  47. ccl_device_inline float3 min(const float3 &a, const float3 &b);
  48. ccl_device_inline float3 max(const float3 &a, const float3 &b);
  49. ccl_device_inline float3 clamp(const float3 &a, const float3 &mn, const float3 &mx);
  50. ccl_device_inline float3 fabs(const float3 &a);
  51. ccl_device_inline float3 mix(const float3 &a, const float3 &b, float t);
  52. ccl_device_inline float3 rcp(const float3 &a);
  53. ccl_device_inline float3 sqrt(const float3 &a);
  54. #endif /* !__KERNEL_OPENCL__ */
  55. ccl_device_inline float min3(float3 a);
  56. ccl_device_inline float max3(float3 a);
  57. ccl_device_inline float len(const float3 a);
  58. ccl_device_inline float len_squared(const float3 a);
  59. ccl_device_inline float3 saturate3(float3 a);
  60. ccl_device_inline float3 safe_normalize(const float3 a);
  61. ccl_device_inline float3 normalize_len(const float3 a, float *t);
  62. ccl_device_inline float3 safe_normalize_len(const float3 a, float *t);
  63. ccl_device_inline float3 interp(float3 a, float3 b, float t);
  64. ccl_device_inline float3 sqr3(float3 a);
  65. ccl_device_inline bool is_zero(const float3 a);
  66. ccl_device_inline float reduce_add(const float3 a);
  67. ccl_device_inline float average(const float3 a);
  68. ccl_device_inline bool isequal_float3(const float3 a, const float3 b);
  69. /*******************************************************************************
  70. * Definition.
  71. */
  72. #ifndef __KERNEL_OPENCL__
  73. ccl_device_inline float3 operator-(const float3 &a)
  74. {
  75. # ifdef __KERNEL_SSE__
  76. return float3(_mm_xor_ps(a.m128, _mm_castsi128_ps(_mm_set1_epi32(0x80000000))));
  77. # else
  78. return make_float3(-a.x, -a.y, -a.z);
  79. # endif
  80. }
  81. ccl_device_inline float3 operator*(const float3 &a, const float3 &b)
  82. {
  83. # ifdef __KERNEL_SSE__
  84. return float3(_mm_mul_ps(a.m128, b.m128));
  85. # else
  86. return make_float3(a.x * b.x, a.y * b.y, a.z * b.z);
  87. # endif
  88. }
  89. ccl_device_inline float3 operator*(const float3 &a, const float f)
  90. {
  91. # ifdef __KERNEL_SSE__
  92. return float3(_mm_mul_ps(a.m128, _mm_set1_ps(f)));
  93. # else
  94. return make_float3(a.x * f, a.y * f, a.z * f);
  95. # endif
  96. }
  97. ccl_device_inline float3 operator*(const float f, const float3 &a)
  98. {
  99. # if defined(__KERNEL_SSE__)
  100. return float3(_mm_mul_ps(_mm_set1_ps(f), a.m128));
  101. # else
  102. return make_float3(a.x * f, a.y * f, a.z * f);
  103. # endif
  104. }
  105. ccl_device_inline float3 operator/(const float f, const float3 &a)
  106. {
  107. # if defined(__KERNEL_SSE__)
  108. return float3(_mm_div_ps(_mm_set1_ps(f), a.m128));
  109. # else
  110. return make_float3(f / a.x, f / a.y, f / a.z);
  111. # endif
  112. }
  113. ccl_device_inline float3 operator/(const float3 &a, const float f)
  114. {
  115. float invf = 1.0f / f;
  116. return a * invf;
  117. }
  118. ccl_device_inline float3 operator/(const float3 &a, const float3 &b)
  119. {
  120. # if defined(__KERNEL_SSE__)
  121. return float3(_mm_div_ps(a.m128, b.m128));
  122. # else
  123. return make_float3(a.x / b.x, a.y / b.y, a.z / b.z);
  124. # endif
  125. }
  126. ccl_device_inline float3 operator+(const float3 &a, const float3 &b)
  127. {
  128. # ifdef __KERNEL_SSE__
  129. return float3(_mm_add_ps(a.m128, b.m128));
  130. # else
  131. return make_float3(a.x + b.x, a.y + b.y, a.z + b.z);
  132. # endif
  133. }
  134. ccl_device_inline float3 operator-(const float3 &a, const float3 &b)
  135. {
  136. # ifdef __KERNEL_SSE__
  137. return float3(_mm_sub_ps(a.m128, b.m128));
  138. # else
  139. return make_float3(a.x - b.x, a.y - b.y, a.z - b.z);
  140. # endif
  141. }
  142. ccl_device_inline float3 operator+=(float3 &a, const float3 &b)
  143. {
  144. return a = a + b;
  145. }
  146. ccl_device_inline float3 operator-=(float3 &a, const float3 &b)
  147. {
  148. return a = a - b;
  149. }
  150. ccl_device_inline float3 operator*=(float3 &a, const float3 &b)
  151. {
  152. return a = a * b;
  153. }
  154. ccl_device_inline float3 operator*=(float3 &a, float f)
  155. {
  156. return a = a * f;
  157. }
  158. ccl_device_inline float3 operator/=(float3 &a, const float3 &b)
  159. {
  160. return a = a / b;
  161. }
  162. ccl_device_inline float3 operator/=(float3 &a, float f)
  163. {
  164. float invf = 1.0f / f;
  165. return a = a * invf;
  166. }
  167. ccl_device_inline bool operator==(const float3 &a, const float3 &b)
  168. {
  169. # ifdef __KERNEL_SSE__
  170. return (_mm_movemask_ps(_mm_cmpeq_ps(a.m128, b.m128)) & 7) == 7;
  171. # else
  172. return (a.x == b.x && a.y == b.y && a.z == b.z);
  173. # endif
  174. }
  175. ccl_device_inline bool operator!=(const float3 &a, const float3 &b)
  176. {
  177. return !(a == b);
  178. }
  179. ccl_device_inline float dot(const float3 &a, const float3 &b)
  180. {
  181. # if defined(__KERNEL_SSE41__) && defined(__KERNEL_SSE__)
  182. return _mm_cvtss_f32(_mm_dp_ps(a, b, 0x7F));
  183. # else
  184. return a.x * b.x + a.y * b.y + a.z * b.z;
  185. # endif
  186. }
  187. ccl_device_inline float dot_xy(const float3 &a, const float3 &b)
  188. {
  189. # if defined(__KERNEL_SSE41__) && defined(__KERNEL_SSE__)
  190. return _mm_cvtss_f32(_mm_hadd_ps(_mm_mul_ps(a, b), b));
  191. # else
  192. return a.x * b.x + a.y * b.y;
  193. # endif
  194. }
  195. ccl_device_inline float3 cross(const float3 &a, const float3 &b)
  196. {
  197. float3 r = make_float3(a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x);
  198. return r;
  199. }
  200. ccl_device_inline float3 normalize(const float3 &a)
  201. {
  202. # if defined(__KERNEL_SSE41__) && defined(__KERNEL_SSE__)
  203. __m128 norm = _mm_sqrt_ps(_mm_dp_ps(a.m128, a.m128, 0x7F));
  204. return float3(_mm_div_ps(a.m128, norm));
  205. # else
  206. return a / len(a);
  207. # endif
  208. }
  209. ccl_device_inline float3 min(const float3 &a, const float3 &b)
  210. {
  211. # ifdef __KERNEL_SSE__
  212. return float3(_mm_min_ps(a.m128, b.m128));
  213. # else
  214. return make_float3(min(a.x, b.x), min(a.y, b.y), min(a.z, b.z));
  215. # endif
  216. }
  217. ccl_device_inline float3 max(const float3 &a, const float3 &b)
  218. {
  219. # ifdef __KERNEL_SSE__
  220. return float3(_mm_max_ps(a.m128, b.m128));
  221. # else
  222. return make_float3(max(a.x, b.x), max(a.y, b.y), max(a.z, b.z));
  223. # endif
  224. }
  225. ccl_device_inline float3 clamp(const float3 &a, const float3 &mn, const float3 &mx)
  226. {
  227. return min(max(a, mn), mx);
  228. }
  229. ccl_device_inline float3 fabs(const float3 &a)
  230. {
  231. # ifdef __KERNEL_SSE__
  232. __m128 mask = _mm_castsi128_ps(_mm_set1_epi32(0x7fffffff));
  233. return float3(_mm_and_ps(a.m128, mask));
  234. # else
  235. return make_float3(fabsf(a.x), fabsf(a.y), fabsf(a.z));
  236. # endif
  237. }
  238. ccl_device_inline float3 sqrt(const float3 &a)
  239. {
  240. # ifdef __KERNEL_SSE__
  241. return float3(_mm_sqrt_ps(a));
  242. # else
  243. return make_float3(sqrtf(a.x), sqrtf(a.y), sqrtf(a.z));
  244. # endif
  245. }
  246. ccl_device_inline float3 mix(const float3 &a, const float3 &b, float t)
  247. {
  248. return a + t * (b - a);
  249. }
  250. ccl_device_inline float3 rcp(const float3 &a)
  251. {
  252. # ifdef __KERNEL_SSE__
  253. /* Don't use _mm_rcp_ps due to poor precision. */
  254. return float3(_mm_div_ps(_mm_set_ps1(1.0f), a.m128));
  255. # else
  256. return make_float3(1.0f / a.x, 1.0f / a.y, 1.0f / a.z);
  257. # endif
  258. }
  259. #endif /* !__KERNEL_OPENCL__ */
  260. ccl_device_inline float min3(float3 a)
  261. {
  262. return min(min(a.x, a.y), a.z);
  263. }
  264. ccl_device_inline float max3(float3 a)
  265. {
  266. return max(max(a.x, a.y), a.z);
  267. }
  268. ccl_device_inline float len(const float3 a)
  269. {
  270. #if defined(__KERNEL_SSE41__) && defined(__KERNEL_SSE__)
  271. return _mm_cvtss_f32(_mm_sqrt_ss(_mm_dp_ps(a.m128, a.m128, 0x7F)));
  272. #else
  273. return sqrtf(dot(a, a));
  274. #endif
  275. }
  276. ccl_device_inline float len_squared(const float3 a)
  277. {
  278. return dot(a, a);
  279. }
  280. ccl_device_inline float3 saturate3(float3 a)
  281. {
  282. return make_float3(saturate(a.x), saturate(a.y), saturate(a.z));
  283. }
  284. ccl_device_inline float3 normalize_len(const float3 a, float *t)
  285. {
  286. *t = len(a);
  287. float x = 1.0f / *t;
  288. return a * x;
  289. }
  290. ccl_device_inline float3 safe_normalize(const float3 a)
  291. {
  292. float t = len(a);
  293. return (t != 0.0f) ? a * (1.0f / t) : a;
  294. }
  295. ccl_device_inline float3 safe_normalize_len(const float3 a, float *t)
  296. {
  297. *t = len(a);
  298. return (*t != 0.0f) ? a / (*t) : a;
  299. }
  300. ccl_device_inline float3 interp(float3 a, float3 b, float t)
  301. {
  302. return a + t * (b - a);
  303. }
  304. ccl_device_inline float3 sqr3(float3 a)
  305. {
  306. return a * a;
  307. }
  308. ccl_device_inline bool is_zero(const float3 a)
  309. {
  310. #ifdef __KERNEL_SSE__
  311. return a == make_float3(0.0f);
  312. #else
  313. return (a.x == 0.0f && a.y == 0.0f && a.z == 0.0f);
  314. #endif
  315. }
  316. ccl_device_inline float reduce_add(const float3 a)
  317. {
  318. return (a.x + a.y + a.z);
  319. }
  320. ccl_device_inline float average(const float3 a)
  321. {
  322. return reduce_add(a) * (1.0f / 3.0f);
  323. }
  324. ccl_device_inline bool isequal_float3(const float3 a, const float3 b)
  325. {
  326. #ifdef __KERNEL_OPENCL__
  327. return all(a == b);
  328. #else
  329. return a == b;
  330. #endif
  331. }
  332. ccl_device_inline float3 pow3(float3 v, float e)
  333. {
  334. return make_float3(powf(v.x, e), powf(v.y, e), powf(v.z, e));
  335. }
  336. ccl_device_inline float3 exp3(float3 v)
  337. {
  338. return make_float3(expf(v.x), expf(v.y), expf(v.z));
  339. }
  340. ccl_device_inline float3 log3(float3 v)
  341. {
  342. return make_float3(logf(v.x), logf(v.y), logf(v.z));
  343. }
  344. ccl_device_inline int3 quick_floor_to_int3(const float3 a)
  345. {
  346. #ifdef __KERNEL_SSE__
  347. int3 b = int3(_mm_cvttps_epi32(a.m128));
  348. int3 isneg = int3(_mm_castps_si128(_mm_cmplt_ps(a.m128, _mm_set_ps1(0.0f))));
  349. /* Unsaturated add 0xffffffff is the same as subtract -1. */
  350. return b + isneg;
  351. #else
  352. return make_int3(quick_floor_to_int(a.x), quick_floor_to_int(a.y), quick_floor_to_int(a.z));
  353. #endif
  354. }
  355. ccl_device_inline bool isfinite3_safe(float3 v)
  356. {
  357. return isfinite_safe(v.x) && isfinite_safe(v.y) && isfinite_safe(v.z);
  358. }
  359. ccl_device_inline float3 ensure_finite3(float3 v)
  360. {
  361. if (!isfinite_safe(v.x))
  362. v.x = 0.0f;
  363. if (!isfinite_safe(v.y))
  364. v.y = 0.0f;
  365. if (!isfinite_safe(v.z))
  366. v.z = 0.0f;
  367. return v;
  368. }
  369. CCL_NAMESPACE_END
  370. #endif /* __UTIL_MATH_FLOAT3_H__ */