util_math_float4.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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_FLOAT4_H__
  17. #define __UTIL_MATH_FLOAT4_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 float4 operator-(const float4 &a);
  27. ccl_device_inline float4 operator*(const float4 &a, const float4 &b);
  28. ccl_device_inline float4 operator*(const float4 &a, float f);
  29. ccl_device_inline float4 operator*(float f, const float4 &a);
  30. ccl_device_inline float4 operator/(const float4 &a, float f);
  31. ccl_device_inline float4 operator/(const float4 &a, const float4 &b);
  32. ccl_device_inline float4 operator+(const float4 &a, const float4 &b);
  33. ccl_device_inline float4 operator-(const float4 &a, const float4 &b);
  34. ccl_device_inline float4 operator+=(float4 &a, const float4 &b);
  35. ccl_device_inline float4 operator*=(float4 &a, const float4 &b);
  36. ccl_device_inline float4 operator*=(float4 &a, float f);
  37. ccl_device_inline float4 operator/=(float4 &a, float f);
  38. ccl_device_inline int4 operator<(const float4 &a, const float4 &b);
  39. ccl_device_inline int4 operator>=(const float4 &a, const float4 &b);
  40. ccl_device_inline int4 operator<=(const float4 &a, const float4 &b);
  41. ccl_device_inline bool operator==(const float4 &a, const float4 &b);
  42. ccl_device_inline float dot(const float4 &a, const float4 &b);
  43. ccl_device_inline float len_squared(const float4 &a);
  44. ccl_device_inline float4 rcp(const float4 &a);
  45. ccl_device_inline float4 sqrt(const float4 &a);
  46. ccl_device_inline float4 sqr(const float4 &a);
  47. ccl_device_inline float4 cross(const float4 &a, const float4 &b);
  48. ccl_device_inline bool is_zero(const float4 &a);
  49. ccl_device_inline float average(const float4 &a);
  50. ccl_device_inline float len(const float4 &a);
  51. ccl_device_inline float4 normalize(const float4 &a);
  52. ccl_device_inline float4 safe_normalize(const float4 &a);
  53. ccl_device_inline float4 min(const float4 &a, const float4 &b);
  54. ccl_device_inline float4 max(const float4 &a, const float4 &b);
  55. ccl_device_inline float4 clamp(const float4 &a, const float4 &mn, const float4 &mx);
  56. ccl_device_inline float4 fabs(const float4 &a);
  57. #endif /* !__KERNEL_OPENCL__*/
  58. #ifdef __KERNEL_SSE__
  59. template<size_t index_0, size_t index_1, size_t index_2, size_t index_3>
  60. __forceinline const float4 shuffle(const float4 &b);
  61. template<size_t index_0, size_t index_1, size_t index_2, size_t index_3>
  62. __forceinline const float4 shuffle(const float4 &a, const float4 &b);
  63. template<> __forceinline const float4 shuffle<0, 1, 0, 1>(const float4 &b);
  64. template<> __forceinline const float4 shuffle<0, 1, 0, 1>(const float4 &a, const float4 &b);
  65. template<> __forceinline const float4 shuffle<2, 3, 2, 3>(const float4 &a, const float4 &b);
  66. # ifdef __KERNEL_SSE3__
  67. template<> __forceinline const float4 shuffle<0, 0, 2, 2>(const float4 &b);
  68. template<> __forceinline const float4 shuffle<1, 1, 3, 3>(const float4 &b);
  69. # endif
  70. #endif /* __KERNEL_SSE__ */
  71. #ifndef __KERNEL_GPU__
  72. ccl_device_inline float4 select(const int4 &mask, const float4 &a, const float4 &b);
  73. ccl_device_inline float4 reduce_min(const float4 &a);
  74. ccl_device_inline float4 reduce_max(const float4 &a);
  75. ccl_device_inline float4 reduce_add(const float4 &a);
  76. #endif /* !__KERNEL_GPU__ */
  77. /*******************************************************************************
  78. * Definition.
  79. */
  80. #ifndef __KERNEL_OPENCL__
  81. ccl_device_inline float4 operator-(const float4 &a)
  82. {
  83. # ifdef __KERNEL_SSE__
  84. __m128 mask = _mm_castsi128_ps(_mm_set1_epi32(0x80000000));
  85. return float4(_mm_xor_ps(a.m128, mask));
  86. # else
  87. return make_float4(-a.x, -a.y, -a.z, -a.w);
  88. # endif
  89. }
  90. ccl_device_inline float4 operator*(const float4 &a, const float4 &b)
  91. {
  92. # ifdef __KERNEL_SSE__
  93. return float4(_mm_mul_ps(a.m128, b.m128));
  94. # else
  95. return make_float4(a.x * b.x, a.y * b.y, a.z * b.z, a.w * b.w);
  96. # endif
  97. }
  98. ccl_device_inline float4 operator*(const float4 &a, float f)
  99. {
  100. # if defined(__KERNEL_SSE__)
  101. return a * make_float4(f);
  102. # else
  103. return make_float4(a.x * f, a.y * f, a.z * f, a.w * f);
  104. # endif
  105. }
  106. ccl_device_inline float4 operator*(float f, const float4 &a)
  107. {
  108. return a * f;
  109. }
  110. ccl_device_inline float4 operator/(const float4 &a, float f)
  111. {
  112. return a * (1.0f / f);
  113. }
  114. ccl_device_inline float4 operator/(const float4 &a, const float4 &b)
  115. {
  116. # ifdef __KERNEL_SSE__
  117. return float4(_mm_div_ps(a.m128, b.m128));
  118. # else
  119. return make_float4(a.x / b.x, a.y / b.y, a.z / b.z, a.w / b.w);
  120. # endif
  121. }
  122. ccl_device_inline float4 operator+(const float4 &a, const float4 &b)
  123. {
  124. # ifdef __KERNEL_SSE__
  125. return float4(_mm_add_ps(a.m128, b.m128));
  126. # else
  127. return make_float4(a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w);
  128. # endif
  129. }
  130. ccl_device_inline float4 operator-(const float4 &a, const float4 &b)
  131. {
  132. # ifdef __KERNEL_SSE__
  133. return float4(_mm_sub_ps(a.m128, b.m128));
  134. # else
  135. return make_float4(a.x - b.x, a.y - b.y, a.z - b.z, a.w - b.w);
  136. # endif
  137. }
  138. ccl_device_inline float4 operator+=(float4 &a, const float4 &b)
  139. {
  140. return a = a + b;
  141. }
  142. ccl_device_inline float4 operator*=(float4 &a, const float4 &b)
  143. {
  144. return a = a * b;
  145. }
  146. ccl_device_inline float4 operator*=(float4 &a, float f)
  147. {
  148. return a = a * f;
  149. }
  150. ccl_device_inline float4 operator/=(float4 &a, float f)
  151. {
  152. return a = a / f;
  153. }
  154. ccl_device_inline int4 operator<(const float4 &a, const float4 &b)
  155. {
  156. # ifdef __KERNEL_SSE__
  157. return int4(_mm_castps_si128(_mm_cmplt_ps(a.m128, b.m128)));
  158. # else
  159. return make_int4(a.x < b.x, a.y < b.y, a.z < b.z, a.w < b.w);
  160. # endif
  161. }
  162. ccl_device_inline int4 operator>=(const float4 &a, const float4 &b)
  163. {
  164. # ifdef __KERNEL_SSE__
  165. return int4(_mm_castps_si128(_mm_cmpge_ps(a.m128, b.m128)));
  166. # else
  167. return make_int4(a.x >= b.x, a.y >= b.y, a.z >= b.z, a.w >= b.w);
  168. # endif
  169. }
  170. ccl_device_inline int4 operator<=(const float4 &a, const float4 &b)
  171. {
  172. # ifdef __KERNEL_SSE__
  173. return int4(_mm_castps_si128(_mm_cmple_ps(a.m128, b.m128)));
  174. # else
  175. return make_int4(a.x <= b.x, a.y <= b.y, a.z <= b.z, a.w <= b.w);
  176. # endif
  177. }
  178. ccl_device_inline bool operator==(const float4 &a, const float4 &b)
  179. {
  180. # ifdef __KERNEL_SSE__
  181. return (_mm_movemask_ps(_mm_cmpeq_ps(a.m128, b.m128)) & 15) == 15;
  182. # else
  183. return (a.x == b.x && a.y == b.y && a.z == b.z && a.w == b.w);
  184. # endif
  185. }
  186. ccl_device_inline float dot(const float4 &a, const float4 &b)
  187. {
  188. # if defined(__KERNEL_SSE41__) && defined(__KERNEL_SSE__)
  189. return _mm_cvtss_f32(_mm_dp_ps(a, b, 0xFF));
  190. # else
  191. return (a.x * b.x + a.y * b.y) + (a.z * b.z + a.w * b.w);
  192. # endif
  193. }
  194. ccl_device_inline float len_squared(const float4 &a)
  195. {
  196. return dot(a, a);
  197. }
  198. ccl_device_inline float4 rcp(const float4 &a)
  199. {
  200. # ifdef __KERNEL_SSE__
  201. /* Don't use _mm_rcp_ps due to poor precision. */
  202. return float4(_mm_div_ps(_mm_set_ps1(1.0f), a.m128));
  203. # else
  204. return make_float4(1.0f / a.x, 1.0f / a.y, 1.0f / a.z, 1.0f / a.w);
  205. # endif
  206. }
  207. ccl_device_inline float4 sqrt(const float4 &a)
  208. {
  209. # ifdef __KERNEL_SSE__
  210. return float4(_mm_sqrt_ps(a.m128));
  211. # else
  212. return make_float4(sqrtf(a.x), sqrtf(a.y), sqrtf(a.z), sqrtf(a.w));
  213. # endif
  214. }
  215. ccl_device_inline float4 sqr(const float4 &a)
  216. {
  217. return a * a;
  218. }
  219. ccl_device_inline float4 cross(const float4 &a, const float4 &b)
  220. {
  221. # ifdef __KERNEL_SSE__
  222. return (shuffle<1, 2, 0, 0>(a) * shuffle<2, 0, 1, 0>(b)) -
  223. (shuffle<2, 0, 1, 0>(a) * shuffle<1, 2, 0, 0>(b));
  224. # else
  225. return make_float4(a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x, 0.0f);
  226. # endif
  227. }
  228. ccl_device_inline bool is_zero(const float4 &a)
  229. {
  230. # ifdef __KERNEL_SSE__
  231. return a == make_float4(0.0f);
  232. # else
  233. return (a.x == 0.0f && a.y == 0.0f && a.z == 0.0f && a.w == 0.0f);
  234. # endif
  235. }
  236. ccl_device_inline float4 reduce_add(const float4 &a)
  237. {
  238. # ifdef __KERNEL_SSE__
  239. # ifdef __KERNEL_SSE3__
  240. float4 h(_mm_hadd_ps(a.m128, a.m128));
  241. return float4(_mm_hadd_ps(h.m128, h.m128));
  242. # else
  243. float4 h(shuffle<1, 0, 3, 2>(a) + a);
  244. return shuffle<2, 3, 0, 1>(h) + h;
  245. # endif
  246. # else
  247. float sum = (a.x + a.y) + (a.z + a.w);
  248. return make_float4(sum, sum, sum, sum);
  249. # endif
  250. }
  251. ccl_device_inline float average(const float4 &a)
  252. {
  253. return reduce_add(a).x * 0.25f;
  254. }
  255. ccl_device_inline float len(const float4 &a)
  256. {
  257. return sqrtf(dot(a, a));
  258. }
  259. ccl_device_inline float4 normalize(const float4 &a)
  260. {
  261. return a / len(a);
  262. }
  263. ccl_device_inline float4 safe_normalize(const float4 &a)
  264. {
  265. float t = len(a);
  266. return (t != 0.0f) ? a / t : a;
  267. }
  268. ccl_device_inline float4 min(const float4 &a, const float4 &b)
  269. {
  270. # ifdef __KERNEL_SSE__
  271. return float4(_mm_min_ps(a.m128, b.m128));
  272. # else
  273. return make_float4(min(a.x, b.x), min(a.y, b.y), min(a.z, b.z), min(a.w, b.w));
  274. # endif
  275. }
  276. ccl_device_inline float4 max(const float4 &a, const float4 &b)
  277. {
  278. # ifdef __KERNEL_SSE__
  279. return float4(_mm_max_ps(a.m128, b.m128));
  280. # else
  281. return make_float4(max(a.x, b.x), max(a.y, b.y), max(a.z, b.z), max(a.w, b.w));
  282. # endif
  283. }
  284. ccl_device_inline float4 clamp(const float4 &a, const float4 &mn, const float4 &mx)
  285. {
  286. return min(max(a, mn), mx);
  287. }
  288. ccl_device_inline float4 fabs(const float4 &a)
  289. {
  290. # ifdef __KERNEL_SSE__
  291. return float4(_mm_and_ps(a.m128, _mm_castsi128_ps(_mm_set1_epi32(0x7fffffff))));
  292. # else
  293. return make_float4(fabsf(a.x), fabsf(a.y), fabsf(a.z), fabsf(a.w));
  294. # endif
  295. }
  296. #endif /* !__KERNEL_OPENCL__*/
  297. #ifdef __KERNEL_SSE__
  298. template<size_t index_0, size_t index_1, size_t index_2, size_t index_3>
  299. __forceinline const float4 shuffle(const float4 &b)
  300. {
  301. return float4(_mm_castsi128_ps(
  302. _mm_shuffle_epi32(_mm_castps_si128(b), _MM_SHUFFLE(index_3, index_2, index_1, index_0))));
  303. }
  304. template<size_t index_0, size_t index_1, size_t index_2, size_t index_3>
  305. __forceinline const float4 shuffle(const float4 &a, const float4 &b)
  306. {
  307. return float4(_mm_shuffle_ps(a.m128, b.m128, _MM_SHUFFLE(index_3, index_2, index_1, index_0)));
  308. }
  309. template<> __forceinline const float4 shuffle<0, 1, 0, 1>(const float4 &b)
  310. {
  311. return float4(_mm_castpd_ps(_mm_movedup_pd(_mm_castps_pd(b))));
  312. }
  313. template<> __forceinline const float4 shuffle<0, 1, 0, 1>(const float4 &a, const float4 &b)
  314. {
  315. return float4(_mm_movelh_ps(a.m128, b.m128));
  316. }
  317. template<> __forceinline const float4 shuffle<2, 3, 2, 3>(const float4 &a, const float4 &b)
  318. {
  319. return float4(_mm_movehl_ps(b.m128, a.m128));
  320. }
  321. # ifdef __KERNEL_SSE3__
  322. template<> __forceinline const float4 shuffle<0, 0, 2, 2>(const float4 &b)
  323. {
  324. return float4(_mm_moveldup_ps(b));
  325. }
  326. template<> __forceinline const float4 shuffle<1, 1, 3, 3>(const float4 &b)
  327. {
  328. return float4(_mm_movehdup_ps(b));
  329. }
  330. # endif /* __KERNEL_SSE3__ */
  331. #endif /* __KERNEL_SSE__ */
  332. #ifndef __KERNEL_GPU__
  333. ccl_device_inline float4 select(const int4 &mask, const float4 &a, const float4 &b)
  334. {
  335. # ifdef __KERNEL_SSE__
  336. return float4(_mm_blendv_ps(b.m128, a.m128, _mm_castsi128_ps(mask.m128)));
  337. # else
  338. return make_float4(
  339. (mask.x) ? a.x : b.x, (mask.y) ? a.y : b.y, (mask.z) ? a.z : b.z, (mask.w) ? a.w : b.w);
  340. # endif
  341. }
  342. ccl_device_inline float4 mask(const int4 &mask, const float4 &a)
  343. {
  344. /* Replace elements of x with zero where mask isn't set. */
  345. return select(mask, a, make_float4(0.0f));
  346. }
  347. ccl_device_inline float4 reduce_min(const float4 &a)
  348. {
  349. # ifdef __KERNEL_SSE__
  350. float4 h = min(shuffle<1, 0, 3, 2>(a), a);
  351. return min(shuffle<2, 3, 0, 1>(h), h);
  352. # else
  353. return make_float4(min(min(a.x, a.y), min(a.z, a.w)));
  354. # endif
  355. }
  356. ccl_device_inline float4 reduce_max(const float4 &a)
  357. {
  358. # ifdef __KERNEL_SSE__
  359. float4 h = max(shuffle<1, 0, 3, 2>(a), a);
  360. return max(shuffle<2, 3, 0, 1>(h), h);
  361. # else
  362. return make_float4(max(max(a.x, a.y), max(a.z, a.w)));
  363. # endif
  364. }
  365. ccl_device_inline float4 load_float4(const float *v)
  366. {
  367. # ifdef __KERNEL_SSE__
  368. return float4(_mm_loadu_ps(v));
  369. # else
  370. return make_float4(v[0], v[1], v[2], v[3]);
  371. # endif
  372. }
  373. #endif /* !__KERNEL_GPU__ */
  374. CCL_NAMESPACE_END
  375. #endif /* __UTIL_MATH_FLOAT4_H__ */