util_ssef.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011
  1. /*
  2. * Copyright 2011-2013 Intel Corporation
  3. * Modifications Copyright 2014, Blender Foundation.
  4. *
  5. * Licensed under the Apache License, Version 2.0(the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. #ifndef __UTIL_SSEF_H__
  18. #define __UTIL_SSEF_H__
  19. CCL_NAMESPACE_BEGIN
  20. #ifdef __KERNEL_SSE2__
  21. struct sseb;
  22. struct ssef;
  23. /*! 4-wide SSE float type. */
  24. struct ssef {
  25. typedef sseb Mask; // mask type
  26. typedef ssei Int; // int type
  27. typedef ssef Float; // float type
  28. enum { size = 4 }; // number of SIMD elements
  29. union {
  30. __m128 m128;
  31. float f[4];
  32. int i[4];
  33. }; // data
  34. ////////////////////////////////////////////////////////////////////////////////
  35. /// Constructors, Assignment & Cast Operators
  36. ////////////////////////////////////////////////////////////////////////////////
  37. __forceinline ssef()
  38. {
  39. }
  40. __forceinline ssef(const ssef &other)
  41. {
  42. m128 = other.m128;
  43. }
  44. __forceinline ssef &operator=(const ssef &other)
  45. {
  46. m128 = other.m128;
  47. return *this;
  48. }
  49. __forceinline ssef(const __m128 a) : m128(a)
  50. {
  51. }
  52. __forceinline operator const __m128 &() const
  53. {
  54. return m128;
  55. }
  56. __forceinline operator __m128 &()
  57. {
  58. return m128;
  59. }
  60. __forceinline ssef(float a) : m128(_mm_set1_ps(a))
  61. {
  62. }
  63. __forceinline ssef(float a, float b, float c, float d) : m128(_mm_setr_ps(a, b, c, d))
  64. {
  65. }
  66. __forceinline explicit ssef(const __m128i a) : m128(_mm_cvtepi32_ps(a))
  67. {
  68. }
  69. ////////////////////////////////////////////////////////////////////////////////
  70. /// Loads and Stores
  71. ////////////////////////////////////////////////////////////////////////////////
  72. # if defined(__KERNEL_AVX__)
  73. static __forceinline ssef broadcast(const void *const a)
  74. {
  75. return _mm_broadcast_ss((float *)a);
  76. }
  77. # else
  78. static __forceinline ssef broadcast(const void *const a)
  79. {
  80. return _mm_set1_ps(*(float *)a);
  81. }
  82. # endif
  83. ////////////////////////////////////////////////////////////////////////////////
  84. /// Array Access
  85. ////////////////////////////////////////////////////////////////////////////////
  86. __forceinline const float &operator[](const size_t i) const
  87. {
  88. assert(i < 4);
  89. return f[i];
  90. }
  91. __forceinline float &operator[](const size_t i)
  92. {
  93. assert(i < 4);
  94. return f[i];
  95. }
  96. };
  97. ////////////////////////////////////////////////////////////////////////////////
  98. /// Unary Operators
  99. ////////////////////////////////////////////////////////////////////////////////
  100. __forceinline const ssef cast(const __m128i &a)
  101. {
  102. return _mm_castsi128_ps(a);
  103. }
  104. __forceinline const ssef operator+(const ssef &a)
  105. {
  106. return a;
  107. }
  108. __forceinline const ssef operator-(const ssef &a)
  109. {
  110. return _mm_xor_ps(a.m128, _mm_castsi128_ps(_mm_set1_epi32(0x80000000)));
  111. }
  112. __forceinline const ssef abs(const ssef &a)
  113. {
  114. return _mm_and_ps(a.m128, _mm_castsi128_ps(_mm_set1_epi32(0x7fffffff)));
  115. }
  116. # if defined(__KERNEL_SSE41__)
  117. __forceinline const ssef sign(const ssef &a)
  118. {
  119. return _mm_blendv_ps(ssef(1.0f), -ssef(1.0f), _mm_cmplt_ps(a, ssef(0.0f)));
  120. }
  121. # endif
  122. __forceinline const ssef signmsk(const ssef &a)
  123. {
  124. return _mm_and_ps(a.m128, _mm_castsi128_ps(_mm_set1_epi32(0x80000000)));
  125. }
  126. __forceinline const ssef rcp(const ssef &a)
  127. {
  128. const ssef r = _mm_rcp_ps(a.m128);
  129. return _mm_sub_ps(_mm_add_ps(r, r), _mm_mul_ps(_mm_mul_ps(r, r), a));
  130. }
  131. __forceinline const ssef sqr(const ssef &a)
  132. {
  133. return _mm_mul_ps(a, a);
  134. }
  135. __forceinline const ssef mm_sqrt(const ssef &a)
  136. {
  137. return _mm_sqrt_ps(a.m128);
  138. }
  139. __forceinline const ssef rsqrt(const ssef &a)
  140. {
  141. const ssef r = _mm_rsqrt_ps(a.m128);
  142. return _mm_add_ps(
  143. _mm_mul_ps(_mm_set_ps(1.5f, 1.5f, 1.5f, 1.5f), r),
  144. _mm_mul_ps(_mm_mul_ps(_mm_mul_ps(a, _mm_set_ps(-0.5f, -0.5f, -0.5f, -0.5f)), r),
  145. _mm_mul_ps(r, r)));
  146. }
  147. ////////////////////////////////////////////////////////////////////////////////
  148. /// Binary Operators
  149. ////////////////////////////////////////////////////////////////////////////////
  150. __forceinline const ssef operator+(const ssef &a, const ssef &b)
  151. {
  152. return _mm_add_ps(a.m128, b.m128);
  153. }
  154. __forceinline const ssef operator+(const ssef &a, const float &b)
  155. {
  156. return a + ssef(b);
  157. }
  158. __forceinline const ssef operator+(const float &a, const ssef &b)
  159. {
  160. return ssef(a) + b;
  161. }
  162. __forceinline const ssef operator-(const ssef &a, const ssef &b)
  163. {
  164. return _mm_sub_ps(a.m128, b.m128);
  165. }
  166. __forceinline const ssef operator-(const ssef &a, const float &b)
  167. {
  168. return a - ssef(b);
  169. }
  170. __forceinline const ssef operator-(const float &a, const ssef &b)
  171. {
  172. return ssef(a) - b;
  173. }
  174. __forceinline const ssef operator*(const ssef &a, const ssef &b)
  175. {
  176. return _mm_mul_ps(a.m128, b.m128);
  177. }
  178. __forceinline const ssef operator*(const ssef &a, const float &b)
  179. {
  180. return a * ssef(b);
  181. }
  182. __forceinline const ssef operator*(const float &a, const ssef &b)
  183. {
  184. return ssef(a) * b;
  185. }
  186. __forceinline const ssef operator/(const ssef &a, const ssef &b)
  187. {
  188. return _mm_div_ps(a.m128, b.m128);
  189. }
  190. __forceinline const ssef operator/(const ssef &a, const float &b)
  191. {
  192. return a / ssef(b);
  193. }
  194. __forceinline const ssef operator/(const float &a, const ssef &b)
  195. {
  196. return ssef(a) / b;
  197. }
  198. __forceinline const ssef operator^(const ssef &a, const ssef &b)
  199. {
  200. return _mm_xor_ps(a.m128, b.m128);
  201. }
  202. __forceinline const ssef operator^(const ssef &a, const ssei &b)
  203. {
  204. return _mm_xor_ps(a.m128, _mm_castsi128_ps(b.m128));
  205. }
  206. __forceinline const ssef operator&(const ssef &a, const ssef &b)
  207. {
  208. return _mm_and_ps(a.m128, b.m128);
  209. }
  210. __forceinline const ssef operator&(const ssef &a, const ssei &b)
  211. {
  212. return _mm_and_ps(a.m128, _mm_castsi128_ps(b.m128));
  213. }
  214. __forceinline const ssef operator|(const ssef &a, const ssef &b)
  215. {
  216. return _mm_or_ps(a.m128, b.m128);
  217. }
  218. __forceinline const ssef operator|(const ssef &a, const ssei &b)
  219. {
  220. return _mm_or_ps(a.m128, _mm_castsi128_ps(b.m128));
  221. }
  222. __forceinline const ssef andnot(const ssef &a, const ssef &b)
  223. {
  224. return _mm_andnot_ps(a.m128, b.m128);
  225. }
  226. __forceinline const ssef min(const ssef &a, const ssef &b)
  227. {
  228. return _mm_min_ps(a.m128, b.m128);
  229. }
  230. __forceinline const ssef min(const ssef &a, const float &b)
  231. {
  232. return _mm_min_ps(a.m128, ssef(b));
  233. }
  234. __forceinline const ssef min(const float &a, const ssef &b)
  235. {
  236. return _mm_min_ps(ssef(a), b.m128);
  237. }
  238. __forceinline const ssef max(const ssef &a, const ssef &b)
  239. {
  240. return _mm_max_ps(a.m128, b.m128);
  241. }
  242. __forceinline const ssef max(const ssef &a, const float &b)
  243. {
  244. return _mm_max_ps(a.m128, ssef(b));
  245. }
  246. __forceinline const ssef max(const float &a, const ssef &b)
  247. {
  248. return _mm_max_ps(ssef(a), b.m128);
  249. }
  250. # if defined(__KERNEL_SSE41__)
  251. __forceinline ssef mini(const ssef &a, const ssef &b)
  252. {
  253. const ssei ai = _mm_castps_si128(a);
  254. const ssei bi = _mm_castps_si128(b);
  255. const ssei ci = _mm_min_epi32(ai, bi);
  256. return _mm_castsi128_ps(ci);
  257. }
  258. # endif
  259. # if defined(__KERNEL_SSE41__)
  260. __forceinline ssef maxi(const ssef &a, const ssef &b)
  261. {
  262. const ssei ai = _mm_castps_si128(a);
  263. const ssei bi = _mm_castps_si128(b);
  264. const ssei ci = _mm_max_epi32(ai, bi);
  265. return _mm_castsi128_ps(ci);
  266. }
  267. # endif
  268. ////////////////////////////////////////////////////////////////////////////////
  269. /// Ternary Operators
  270. ////////////////////////////////////////////////////////////////////////////////
  271. # if defined(__KERNEL_AVX2__)
  272. __forceinline const ssef madd(const ssef &a, const ssef &b, const ssef &c)
  273. {
  274. return _mm_fmadd_ps(a, b, c);
  275. }
  276. __forceinline const ssef msub(const ssef &a, const ssef &b, const ssef &c)
  277. {
  278. return _mm_fmsub_ps(a, b, c);
  279. }
  280. __forceinline const ssef nmadd(const ssef &a, const ssef &b, const ssef &c)
  281. {
  282. return _mm_fnmadd_ps(a, b, c);
  283. }
  284. __forceinline const ssef nmsub(const ssef &a, const ssef &b, const ssef &c)
  285. {
  286. return _mm_fnmsub_ps(a, b, c);
  287. }
  288. # else
  289. __forceinline const ssef madd(const ssef &a, const ssef &b, const ssef &c)
  290. {
  291. return a * b + c;
  292. }
  293. __forceinline const ssef msub(const ssef &a, const ssef &b, const ssef &c)
  294. {
  295. return a * b - c;
  296. }
  297. __forceinline const ssef nmadd(const ssef &a, const ssef &b, const ssef &c)
  298. {
  299. return c - a * b;
  300. }
  301. __forceinline const ssef nmsub(const ssef &a, const ssef &b, const ssef &c)
  302. {
  303. return -a * b - c;
  304. }
  305. # endif
  306. ////////////////////////////////////////////////////////////////////////////////
  307. /// Assignment Operators
  308. ////////////////////////////////////////////////////////////////////////////////
  309. __forceinline ssef &operator+=(ssef &a, const ssef &b)
  310. {
  311. return a = a + b;
  312. }
  313. __forceinline ssef &operator+=(ssef &a, const float &b)
  314. {
  315. return a = a + b;
  316. }
  317. __forceinline ssef &operator-=(ssef &a, const ssef &b)
  318. {
  319. return a = a - b;
  320. }
  321. __forceinline ssef &operator-=(ssef &a, const float &b)
  322. {
  323. return a = a - b;
  324. }
  325. __forceinline ssef &operator*=(ssef &a, const ssef &b)
  326. {
  327. return a = a * b;
  328. }
  329. __forceinline ssef &operator*=(ssef &a, const float &b)
  330. {
  331. return a = a * b;
  332. }
  333. __forceinline ssef &operator/=(ssef &a, const ssef &b)
  334. {
  335. return a = a / b;
  336. }
  337. __forceinline ssef &operator/=(ssef &a, const float &b)
  338. {
  339. return a = a / b;
  340. }
  341. ////////////////////////////////////////////////////////////////////////////////
  342. /// Comparison Operators + Select
  343. ////////////////////////////////////////////////////////////////////////////////
  344. __forceinline const sseb operator==(const ssef &a, const ssef &b)
  345. {
  346. return _mm_cmpeq_ps(a.m128, b.m128);
  347. }
  348. __forceinline const sseb operator==(const ssef &a, const float &b)
  349. {
  350. return a == ssef(b);
  351. }
  352. __forceinline const sseb operator==(const float &a, const ssef &b)
  353. {
  354. return ssef(a) == b;
  355. }
  356. __forceinline const sseb operator!=(const ssef &a, const ssef &b)
  357. {
  358. return _mm_cmpneq_ps(a.m128, b.m128);
  359. }
  360. __forceinline const sseb operator!=(const ssef &a, const float &b)
  361. {
  362. return a != ssef(b);
  363. }
  364. __forceinline const sseb operator!=(const float &a, const ssef &b)
  365. {
  366. return ssef(a) != b;
  367. }
  368. __forceinline const sseb operator<(const ssef &a, const ssef &b)
  369. {
  370. return _mm_cmplt_ps(a.m128, b.m128);
  371. }
  372. __forceinline const sseb operator<(const ssef &a, const float &b)
  373. {
  374. return a < ssef(b);
  375. }
  376. __forceinline const sseb operator<(const float &a, const ssef &b)
  377. {
  378. return ssef(a) < b;
  379. }
  380. __forceinline const sseb operator>=(const ssef &a, const ssef &b)
  381. {
  382. return _mm_cmpnlt_ps(a.m128, b.m128);
  383. }
  384. __forceinline const sseb operator>=(const ssef &a, const float &b)
  385. {
  386. return a >= ssef(b);
  387. }
  388. __forceinline const sseb operator>=(const float &a, const ssef &b)
  389. {
  390. return ssef(a) >= b;
  391. }
  392. __forceinline const sseb operator>(const ssef &a, const ssef &b)
  393. {
  394. return _mm_cmpnle_ps(a.m128, b.m128);
  395. }
  396. __forceinline const sseb operator>(const ssef &a, const float &b)
  397. {
  398. return a > ssef(b);
  399. }
  400. __forceinline const sseb operator>(const float &a, const ssef &b)
  401. {
  402. return ssef(a) > b;
  403. }
  404. __forceinline const sseb operator<=(const ssef &a, const ssef &b)
  405. {
  406. return _mm_cmple_ps(a.m128, b.m128);
  407. }
  408. __forceinline const sseb operator<=(const ssef &a, const float &b)
  409. {
  410. return a <= ssef(b);
  411. }
  412. __forceinline const sseb operator<=(const float &a, const ssef &b)
  413. {
  414. return ssef(a) <= b;
  415. }
  416. __forceinline const ssef select(const sseb &m, const ssef &t, const ssef &f)
  417. {
  418. # ifdef __KERNEL_SSE41__
  419. return _mm_blendv_ps(f, t, m);
  420. # else
  421. return _mm_or_ps(_mm_and_ps(m, t), _mm_andnot_ps(m, f));
  422. # endif
  423. }
  424. __forceinline const ssef select(const ssef &m, const ssef &t, const ssef &f)
  425. {
  426. # ifdef __KERNEL_SSE41__
  427. return _mm_blendv_ps(f, t, m);
  428. # else
  429. return _mm_or_ps(_mm_and_ps(m, t), _mm_andnot_ps(m, f));
  430. # endif
  431. }
  432. __forceinline const ssef select(const int mask, const ssef &t, const ssef &f)
  433. {
  434. # if defined(__KERNEL_SSE41__) && \
  435. ((!defined(__clang__) && !defined(_MSC_VER)) || defined(__INTEL_COMPILER))
  436. return _mm_blend_ps(f, t, mask);
  437. # else
  438. return select(sseb(mask), t, f);
  439. # endif
  440. }
  441. ////////////////////////////////////////////////////////////////////////////////
  442. /// Rounding Functions
  443. ////////////////////////////////////////////////////////////////////////////////
  444. # if defined(__KERNEL_SSE41__)
  445. __forceinline const ssef round_even(const ssef &a)
  446. {
  447. return _mm_round_ps(a, _MM_FROUND_TO_NEAREST_INT);
  448. }
  449. __forceinline const ssef round_down(const ssef &a)
  450. {
  451. return _mm_round_ps(a, _MM_FROUND_TO_NEG_INF);
  452. }
  453. __forceinline const ssef round_up(const ssef &a)
  454. {
  455. return _mm_round_ps(a, _MM_FROUND_TO_POS_INF);
  456. }
  457. __forceinline const ssef round_zero(const ssef &a)
  458. {
  459. return _mm_round_ps(a, _MM_FROUND_TO_ZERO);
  460. }
  461. __forceinline const ssef floor(const ssef &a)
  462. {
  463. return _mm_round_ps(a, _MM_FROUND_TO_NEG_INF);
  464. }
  465. __forceinline const ssef ceil(const ssef &a)
  466. {
  467. return _mm_round_ps(a, _MM_FROUND_TO_POS_INF);
  468. }
  469. # endif
  470. __forceinline ssei truncatei(const ssef &a)
  471. {
  472. return _mm_cvttps_epi32(a.m128);
  473. }
  474. __forceinline ssei floori(const ssef &a)
  475. {
  476. # if defined(__KERNEL_SSE41__)
  477. return ssei(floor(a));
  478. # else
  479. return ssei(a - ssef(0.5f));
  480. # endif
  481. }
  482. ////////////////////////////////////////////////////////////////////////////////
  483. /// Movement/Shifting/Shuffling Functions
  484. ////////////////////////////////////////////////////////////////////////////////
  485. __forceinline ssef unpacklo(const ssef &a, const ssef &b)
  486. {
  487. return _mm_unpacklo_ps(a.m128, b.m128);
  488. }
  489. __forceinline ssef unpackhi(const ssef &a, const ssef &b)
  490. {
  491. return _mm_unpackhi_ps(a.m128, b.m128);
  492. }
  493. template<size_t i0, size_t i1, size_t i2, size_t i3>
  494. __forceinline const ssef shuffle(const ssef &b)
  495. {
  496. return _mm_castsi128_ps(_mm_shuffle_epi32(_mm_castps_si128(b), _MM_SHUFFLE(i3, i2, i1, i0)));
  497. }
  498. template<> __forceinline const ssef shuffle<0, 1, 0, 1>(const ssef &a)
  499. {
  500. return _mm_movelh_ps(a, a);
  501. }
  502. template<> __forceinline const ssef shuffle<2, 3, 2, 3>(const ssef &a)
  503. {
  504. return _mm_movehl_ps(a, a);
  505. }
  506. template<size_t i0, size_t i1, size_t i2, size_t i3>
  507. __forceinline const ssef shuffle(const ssef &a, const ssef &b)
  508. {
  509. return _mm_shuffle_ps(a, b, _MM_SHUFFLE(i3, i2, i1, i0));
  510. }
  511. template<size_t i0> __forceinline const ssef shuffle(const ssef &a, const ssef &b)
  512. {
  513. return _mm_shuffle_ps(a, b, _MM_SHUFFLE(i0, i0, i0, i0));
  514. }
  515. template<> __forceinline const ssef shuffle<0, 1, 0, 1>(const ssef &a, const ssef &b)
  516. {
  517. return _mm_movelh_ps(a, b);
  518. }
  519. template<> __forceinline const ssef shuffle<2, 3, 2, 3>(const ssef &a, const ssef &b)
  520. {
  521. return _mm_movehl_ps(b, a);
  522. }
  523. # if defined(__KERNEL_SSSE3__)
  524. __forceinline const ssef shuffle8(const ssef &a, const ssei &shuf)
  525. {
  526. return _mm_castsi128_ps(_mm_shuffle_epi8(_mm_castps_si128(a), shuf));
  527. }
  528. # endif
  529. # if defined(__KERNEL_SSE3__)
  530. template<> __forceinline const ssef shuffle<0, 0, 2, 2>(const ssef &b)
  531. {
  532. return _mm_moveldup_ps(b);
  533. }
  534. template<> __forceinline const ssef shuffle<1, 1, 3, 3>(const ssef &b)
  535. {
  536. return _mm_movehdup_ps(b);
  537. }
  538. # endif
  539. template<size_t i0> __forceinline const ssef shuffle(const ssef &b)
  540. {
  541. return shuffle<i0, i0, i0, i0>(b);
  542. }
  543. # if defined(__KERNEL_AVX__)
  544. __forceinline const ssef shuffle(const ssef &a, const ssei &shuf)
  545. {
  546. return _mm_permutevar_ps(a, shuf);
  547. }
  548. # endif
  549. template<size_t i> __forceinline float extract(const ssef &a)
  550. {
  551. return _mm_cvtss_f32(shuffle<i, i, i, i>(a));
  552. }
  553. template<> __forceinline float extract<0>(const ssef &a)
  554. {
  555. return _mm_cvtss_f32(a);
  556. }
  557. # if defined(__KERNEL_SSE41__)
  558. template<size_t dst, size_t src, size_t clr>
  559. __forceinline const ssef insert(const ssef &a, const ssef &b)
  560. {
  561. return _mm_insert_ps(a, b, (dst << 4) | (src << 6) | clr);
  562. }
  563. template<size_t dst, size_t src> __forceinline const ssef insert(const ssef &a, const ssef &b)
  564. {
  565. return insert<dst, src, 0>(a, b);
  566. }
  567. template<size_t dst> __forceinline const ssef insert(const ssef &a, const float b)
  568. {
  569. return insert<dst, 0>(a, _mm_set_ss(b));
  570. }
  571. # else
  572. template<size_t dst> __forceinline const ssef insert(const ssef &a, const float b)
  573. {
  574. ssef c = a;
  575. c[dst] = b;
  576. return c;
  577. }
  578. # endif
  579. ////////////////////////////////////////////////////////////////////////////////
  580. /// Transpose
  581. ////////////////////////////////////////////////////////////////////////////////
  582. __forceinline void transpose(const ssef &r0,
  583. const ssef &r1,
  584. const ssef &r2,
  585. const ssef &r3,
  586. ssef &c0,
  587. ssef &c1,
  588. ssef &c2,
  589. ssef &c3)
  590. {
  591. ssef l02 = unpacklo(r0, r2);
  592. ssef h02 = unpackhi(r0, r2);
  593. ssef l13 = unpacklo(r1, r3);
  594. ssef h13 = unpackhi(r1, r3);
  595. c0 = unpacklo(l02, l13);
  596. c1 = unpackhi(l02, l13);
  597. c2 = unpacklo(h02, h13);
  598. c3 = unpackhi(h02, h13);
  599. }
  600. __forceinline void transpose(
  601. const ssef &r0, const ssef &r1, const ssef &r2, const ssef &r3, ssef &c0, ssef &c1, ssef &c2)
  602. {
  603. ssef l02 = unpacklo(r0, r2);
  604. ssef h02 = unpackhi(r0, r2);
  605. ssef l13 = unpacklo(r1, r3);
  606. ssef h13 = unpackhi(r1, r3);
  607. c0 = unpacklo(l02, l13);
  608. c1 = unpackhi(l02, l13);
  609. c2 = unpacklo(h02, h13);
  610. }
  611. ////////////////////////////////////////////////////////////////////////////////
  612. /// Reductions
  613. ////////////////////////////////////////////////////////////////////////////////
  614. __forceinline const ssef vreduce_min(const ssef &v)
  615. {
  616. ssef h = min(shuffle<1, 0, 3, 2>(v), v);
  617. return min(shuffle<2, 3, 0, 1>(h), h);
  618. }
  619. __forceinline const ssef vreduce_max(const ssef &v)
  620. {
  621. ssef h = max(shuffle<1, 0, 3, 2>(v), v);
  622. return max(shuffle<2, 3, 0, 1>(h), h);
  623. }
  624. __forceinline const ssef vreduce_add(const ssef &v)
  625. {
  626. ssef h = shuffle<1, 0, 3, 2>(v) + v;
  627. return shuffle<2, 3, 0, 1>(h) + h;
  628. }
  629. __forceinline float reduce_min(const ssef &v)
  630. {
  631. return _mm_cvtss_f32(vreduce_min(v));
  632. }
  633. __forceinline float reduce_max(const ssef &v)
  634. {
  635. return _mm_cvtss_f32(vreduce_max(v));
  636. }
  637. __forceinline float reduce_add(const ssef &v)
  638. {
  639. return _mm_cvtss_f32(vreduce_add(v));
  640. }
  641. __forceinline size_t select_min(const ssef &v)
  642. {
  643. return __bsf(movemask(v == vreduce_min(v)));
  644. }
  645. __forceinline size_t select_max(const ssef &v)
  646. {
  647. return __bsf(movemask(v == vreduce_max(v)));
  648. }
  649. __forceinline size_t select_min(const sseb &valid, const ssef &v)
  650. {
  651. const ssef a = select(valid, v, ssef(pos_inf));
  652. return __bsf(movemask(valid & (a == vreduce_min(a))));
  653. }
  654. __forceinline size_t select_max(const sseb &valid, const ssef &v)
  655. {
  656. const ssef a = select(valid, v, ssef(neg_inf));
  657. return __bsf(movemask(valid & (a == vreduce_max(a))));
  658. }
  659. __forceinline size_t movemask(const ssef &a)
  660. {
  661. return _mm_movemask_ps(a);
  662. }
  663. ////////////////////////////////////////////////////////////////////////////////
  664. /// Memory load and store operations
  665. ////////////////////////////////////////////////////////////////////////////////
  666. __forceinline ssef load4f(const float4 &a)
  667. {
  668. # ifdef __KERNEL_WITH_SSE_ALIGN__
  669. return _mm_load_ps(&a.x);
  670. # else
  671. return _mm_loadu_ps(&a.x);
  672. # endif
  673. }
  674. __forceinline ssef load4f(const float3 &a)
  675. {
  676. # ifdef __KERNEL_WITH_SSE_ALIGN__
  677. return _mm_load_ps(&a.x);
  678. # else
  679. return _mm_loadu_ps(&a.x);
  680. # endif
  681. }
  682. __forceinline ssef load4f(const void *const a)
  683. {
  684. return _mm_load_ps((float *)a);
  685. }
  686. __forceinline ssef load1f_first(const float a)
  687. {
  688. return _mm_set_ss(a);
  689. }
  690. __forceinline void store4f(void *ptr, const ssef &v)
  691. {
  692. _mm_store_ps((float *)ptr, v);
  693. }
  694. __forceinline ssef loadu4f(const void *const a)
  695. {
  696. return _mm_loadu_ps((float *)a);
  697. }
  698. __forceinline void storeu4f(void *ptr, const ssef &v)
  699. {
  700. _mm_storeu_ps((float *)ptr, v);
  701. }
  702. __forceinline void store4f(const sseb &mask, void *ptr, const ssef &f)
  703. {
  704. # if defined(__KERNEL_AVX__)
  705. _mm_maskstore_ps((float *)ptr, (__m128i)mask, f);
  706. # else
  707. *(ssef *)ptr = select(mask, f, *(ssef *)ptr);
  708. # endif
  709. }
  710. __forceinline ssef load4f_nt(void *ptr)
  711. {
  712. # if defined(__KERNEL_SSE41__)
  713. return _mm_castsi128_ps(_mm_stream_load_si128((__m128i *)ptr));
  714. # else
  715. return _mm_load_ps((float *)ptr);
  716. # endif
  717. }
  718. __forceinline void store4f_nt(void *ptr, const ssef &v)
  719. {
  720. # if defined(__KERNEL_SSE41__)
  721. _mm_stream_ps((float *)ptr, v);
  722. # else
  723. _mm_store_ps((float *)ptr, v);
  724. # endif
  725. }
  726. ////////////////////////////////////////////////////////////////////////////////
  727. /// Euclidian Space Operators
  728. ////////////////////////////////////////////////////////////////////////////////
  729. __forceinline float dot(const ssef &a, const ssef &b)
  730. {
  731. return reduce_add(a * b);
  732. }
  733. /* calculate shuffled cross product, useful when order of components does not matter */
  734. __forceinline ssef cross_zxy(const ssef &a, const ssef &b)
  735. {
  736. const ssef a0 = a;
  737. const ssef b0 = shuffle<1, 2, 0, 3>(b);
  738. const ssef a1 = shuffle<1, 2, 0, 3>(a);
  739. const ssef b1 = b;
  740. return msub(a0, b0, a1 * b1);
  741. }
  742. __forceinline ssef cross(const ssef &a, const ssef &b)
  743. {
  744. return shuffle<1, 2, 0, 3>(cross_zxy(a, b));
  745. }
  746. ccl_device_inline const ssef dot3_splat(const ssef &a, const ssef &b)
  747. {
  748. # ifdef __KERNEL_SSE41__
  749. return _mm_dp_ps(a.m128, b.m128, 0x7f);
  750. # else
  751. ssef t = a * b;
  752. return ssef(((float *)&t)[0] + ((float *)&t)[1] + ((float *)&t)[2]);
  753. # endif
  754. }
  755. /* squared length taking only specified axes into account */
  756. template<size_t X, size_t Y, size_t Z, size_t W> ccl_device_inline float len_squared(const ssef &a)
  757. {
  758. # ifndef __KERNEL_SSE41__
  759. float4 &t = (float4 &)a;
  760. return (X ? t.x * t.x : 0.0f) + (Y ? t.y * t.y : 0.0f) + (Z ? t.z * t.z : 0.0f) +
  761. (W ? t.w * t.w : 0.0f);
  762. # else
  763. return extract<0>(
  764. ssef(_mm_dp_ps(a.m128, a.m128, (X << 4) | (Y << 5) | (Z << 6) | (W << 7) | 0xf)));
  765. # endif
  766. }
  767. ccl_device_inline float dot3(const ssef &a, const ssef &b)
  768. {
  769. # ifdef __KERNEL_SSE41__
  770. return extract<0>(ssef(_mm_dp_ps(a.m128, b.m128, 0x7f)));
  771. # else
  772. ssef t = a * b;
  773. return ((float *)&t)[0] + ((float *)&t)[1] + ((float *)&t)[2];
  774. # endif
  775. }
  776. ccl_device_inline const ssef len3_squared_splat(const ssef &a)
  777. {
  778. return dot3_splat(a, a);
  779. }
  780. ccl_device_inline float len3_squared(const ssef &a)
  781. {
  782. return dot3(a, a);
  783. }
  784. ccl_device_inline float len3(const ssef &a)
  785. {
  786. return extract<0>(mm_sqrt(dot3_splat(a, a)));
  787. }
  788. /* SSE shuffle utility functions */
  789. # ifdef __KERNEL_SSSE3__
  790. /* faster version for SSSE3 */
  791. typedef ssei shuffle_swap_t;
  792. ccl_device_inline shuffle_swap_t shuffle_swap_identity()
  793. {
  794. return _mm_set_epi8(15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0);
  795. }
  796. ccl_device_inline shuffle_swap_t shuffle_swap_swap()
  797. {
  798. return _mm_set_epi8(7, 6, 5, 4, 3, 2, 1, 0, 15, 14, 13, 12, 11, 10, 9, 8);
  799. }
  800. ccl_device_inline const ssef shuffle_swap(const ssef &a, const shuffle_swap_t &shuf)
  801. {
  802. return cast(_mm_shuffle_epi8(cast(a), shuf));
  803. }
  804. # else
  805. /* somewhat slower version for SSE2 */
  806. typedef int shuffle_swap_t;
  807. ccl_device_inline shuffle_swap_t shuffle_swap_identity()
  808. {
  809. return 0;
  810. }
  811. ccl_device_inline shuffle_swap_t shuffle_swap_swap()
  812. {
  813. return 1;
  814. }
  815. ccl_device_inline const ssef shuffle_swap(const ssef &a, shuffle_swap_t shuf)
  816. {
  817. /* shuffle value must be a constant, so we need to branch */
  818. if (shuf)
  819. return ssef(_mm_shuffle_ps(a.m128, a.m128, _MM_SHUFFLE(1, 0, 3, 2)));
  820. else
  821. return ssef(_mm_shuffle_ps(a.m128, a.m128, _MM_SHUFFLE(3, 2, 1, 0)));
  822. }
  823. # endif
  824. # ifdef __KERNEL_SSE41__
  825. ccl_device_inline void gen_idirsplat_swap(const ssef &pn,
  826. const shuffle_swap_t &shuf_identity,
  827. const shuffle_swap_t &shuf_swap,
  828. const float3 &idir,
  829. ssef idirsplat[3],
  830. shuffle_swap_t shufflexyz[3])
  831. {
  832. const __m128 idirsplat_raw[] = {_mm_set_ps1(idir.x), _mm_set_ps1(idir.y), _mm_set_ps1(idir.z)};
  833. idirsplat[0] = _mm_xor_ps(idirsplat_raw[0], pn);
  834. idirsplat[1] = _mm_xor_ps(idirsplat_raw[1], pn);
  835. idirsplat[2] = _mm_xor_ps(idirsplat_raw[2], pn);
  836. const ssef signmask = cast(ssei(0x80000000));
  837. const ssef shuf_identity_f = cast(shuf_identity);
  838. const ssef shuf_swap_f = cast(shuf_swap);
  839. shufflexyz[0] = _mm_castps_si128(
  840. _mm_blendv_ps(shuf_identity_f, shuf_swap_f, _mm_and_ps(idirsplat_raw[0], signmask)));
  841. shufflexyz[1] = _mm_castps_si128(
  842. _mm_blendv_ps(shuf_identity_f, shuf_swap_f, _mm_and_ps(idirsplat_raw[1], signmask)));
  843. shufflexyz[2] = _mm_castps_si128(
  844. _mm_blendv_ps(shuf_identity_f, shuf_swap_f, _mm_and_ps(idirsplat_raw[2], signmask)));
  845. }
  846. # else
  847. ccl_device_inline void gen_idirsplat_swap(const ssef &pn,
  848. const shuffle_swap_t &shuf_identity,
  849. const shuffle_swap_t &shuf_swap,
  850. const float3 &idir,
  851. ssef idirsplat[3],
  852. shuffle_swap_t shufflexyz[3])
  853. {
  854. idirsplat[0] = ssef(idir.x) ^ pn;
  855. idirsplat[1] = ssef(idir.y) ^ pn;
  856. idirsplat[2] = ssef(idir.z) ^ pn;
  857. shufflexyz[0] = (idir.x >= 0) ? shuf_identity : shuf_swap;
  858. shufflexyz[1] = (idir.y >= 0) ? shuf_identity : shuf_swap;
  859. shufflexyz[2] = (idir.z >= 0) ? shuf_identity : shuf_swap;
  860. }
  861. # endif
  862. ccl_device_inline const ssef uint32_to_float(const ssei &in)
  863. {
  864. ssei a = _mm_srli_epi32(in, 16);
  865. ssei b = _mm_and_si128(in, _mm_set1_epi32(0x0000ffff));
  866. ssei c = _mm_or_si128(a, _mm_set1_epi32(0x53000000));
  867. ssef d = _mm_cvtepi32_ps(b);
  868. ssef e = _mm_sub_ps(_mm_castsi128_ps(c), _mm_castsi128_ps(_mm_set1_epi32(0x53000000)));
  869. return _mm_add_ps(e, d);
  870. }
  871. template<size_t S1, size_t S2, size_t S3, size_t S4>
  872. ccl_device_inline const ssef set_sign_bit(const ssef &a)
  873. {
  874. return cast(cast(a) ^ ssei(S1 << 31, S2 << 31, S3 << 31, S4 << 31));
  875. }
  876. ////////////////////////////////////////////////////////////////////////////////
  877. /// Debug Functions
  878. ////////////////////////////////////////////////////////////////////////////////
  879. ccl_device_inline void print_ssef(const char *label, const ssef &a)
  880. {
  881. printf(
  882. "%s: %.8f %.8f %.8f %.8f\n", label, (double)a[0], (double)a[1], (double)a[2], (double)a[3]);
  883. }
  884. #endif
  885. CCL_NAMESPACE_END
  886. #endif