2
0

vboolf4_sse2.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. // Copyright 2009-2021 Intel Corporation
  2. // SPDX-License-Identifier: Apache-2.0
  3. #pragma once
  4. #define vboolf vboolf_impl
  5. #define vboold vboold_impl
  6. #define vint vint_impl
  7. #define vuint vuint_impl
  8. #define vllong vllong_impl
  9. #define vfloat vfloat_impl
  10. #define vdouble vdouble_impl
  11. namespace embree
  12. {
  13. /* 4-wide SSE bool type */
  14. template<>
  15. struct vboolf<4>
  16. {
  17. ALIGNED_STRUCT_(16);
  18. typedef vboolf4 Bool;
  19. typedef vint4 Int;
  20. typedef vfloat4 Float;
  21. enum { size = 4 }; // number of SIMD elements
  22. union { __m128 v; int i[4]; }; // data
  23. ////////////////////////////////////////////////////////////////////////////////
  24. /// Constructors, Assignment & Cast Operators
  25. ////////////////////////////////////////////////////////////////////////////////
  26. __forceinline vboolf() {}
  27. __forceinline vboolf(const vboolf4& other) { v = other.v; }
  28. __forceinline vboolf4& operator =(const vboolf4& other) { v = other.v; return *this; }
  29. __forceinline vboolf(__m128 input) : v(input) {}
  30. __forceinline operator const __m128&() const { return v; }
  31. __forceinline operator const __m128i() const { return _mm_castps_si128(v); }
  32. __forceinline operator const __m128d() const { return _mm_castps_pd(v); }
  33. __forceinline vboolf(bool a)
  34. : v(mm_lookupmask_ps[(size_t(a) << 3) | (size_t(a) << 2) | (size_t(a) << 1) | size_t(a)]) {}
  35. __forceinline vboolf(bool a, bool b)
  36. : v(mm_lookupmask_ps[(size_t(b) << 3) | (size_t(a) << 2) | (size_t(b) << 1) | size_t(a)]) {}
  37. __forceinline vboolf(bool a, bool b, bool c, bool d)
  38. : v(mm_lookupmask_ps[(size_t(d) << 3) | (size_t(c) << 2) | (size_t(b) << 1) | size_t(a)]) {}
  39. __forceinline vboolf(int mask) { assert(mask >= 0 && mask < 16); v = mm_lookupmask_ps[mask]; }
  40. __forceinline vboolf(unsigned int mask) { assert(mask < 16); v = mm_lookupmask_ps[mask]; }
  41. /* return int32 mask */
  42. __forceinline __m128i mask32() const {
  43. return _mm_castps_si128(v);
  44. }
  45. ////////////////////////////////////////////////////////////////////////////////
  46. /// Constants
  47. ////////////////////////////////////////////////////////////////////////////////
  48. __forceinline vboolf(FalseTy) : v(_mm_setzero_ps()) {}
  49. __forceinline vboolf(TrueTy) : v(_mm_castsi128_ps(_mm_cmpeq_epi32(_mm_setzero_si128(), _mm_setzero_si128()))) {}
  50. ////////////////////////////////////////////////////////////////////////////////
  51. /// Array Access
  52. ////////////////////////////////////////////////////////////////////////////////
  53. __forceinline bool operator [](size_t index) const { assert(index < 4); return (_mm_movemask_ps(v) >> index) & 1; }
  54. __forceinline int& operator [](size_t index) { assert(index < 4); return i[index]; }
  55. };
  56. ////////////////////////////////////////////////////////////////////////////////
  57. /// Unary Operators
  58. ////////////////////////////////////////////////////////////////////////////////
  59. __forceinline vboolf4 operator !(const vboolf4& a) { return _mm_xor_ps(a, vboolf4(embree::True)); }
  60. ////////////////////////////////////////////////////////////////////////////////
  61. /// Binary Operators
  62. ////////////////////////////////////////////////////////////////////////////////
  63. __forceinline vboolf4 operator &(const vboolf4& a, const vboolf4& b) { return _mm_and_ps(a, b); }
  64. __forceinline vboolf4 operator |(const vboolf4& a, const vboolf4& b) { return _mm_or_ps (a, b); }
  65. __forceinline vboolf4 operator ^(const vboolf4& a, const vboolf4& b) { return _mm_xor_ps(a, b); }
  66. __forceinline vboolf4 andn(const vboolf4& a, const vboolf4& b) { return _mm_andnot_ps(b, a); }
  67. ////////////////////////////////////////////////////////////////////////////////
  68. /// Assignment Operators
  69. ////////////////////////////////////////////////////////////////////////////////
  70. __forceinline vboolf4& operator &=(vboolf4& a, const vboolf4& b) { return a = a & b; }
  71. __forceinline vboolf4& operator |=(vboolf4& a, const vboolf4& b) { return a = a | b; }
  72. __forceinline vboolf4& operator ^=(vboolf4& a, const vboolf4& b) { return a = a ^ b; }
  73. ////////////////////////////////////////////////////////////////////////////////
  74. /// Comparison Operators + Select
  75. ////////////////////////////////////////////////////////////////////////////////
  76. __forceinline vboolf4 operator !=(const vboolf4& a, const vboolf4& b) { return _mm_xor_ps(a, b); }
  77. __forceinline vboolf4 operator ==(const vboolf4& a, const vboolf4& b) { return _mm_castsi128_ps(_mm_cmpeq_epi32(a, b)); }
  78. __forceinline vboolf4 select(const vboolf4& m, const vboolf4& t, const vboolf4& f) {
  79. #if defined(__SSE4_1__)
  80. return _mm_blendv_ps(f, t, m);
  81. #else
  82. return _mm_or_ps(_mm_and_ps(m, t), _mm_andnot_ps(m, f));
  83. #endif
  84. }
  85. ////////////////////////////////////////////////////////////////////////////////
  86. /// Movement/Shifting/Shuffling Functions
  87. ////////////////////////////////////////////////////////////////////////////////
  88. __forceinline vboolf4 unpacklo(const vboolf4& a, const vboolf4& b) { return _mm_unpacklo_ps(a, b); }
  89. __forceinline vboolf4 unpackhi(const vboolf4& a, const vboolf4& b) { return _mm_unpackhi_ps(a, b); }
  90. template<int i0, int i1, int i2, int i3>
  91. __forceinline vboolf4 shuffle(const vboolf4& v) {
  92. return _mm_castsi128_ps(_mm_shuffle_epi32(v, _MM_SHUFFLE(i3, i2, i1, i0)));
  93. }
  94. template<int i0, int i1, int i2, int i3>
  95. __forceinline vboolf4 shuffle(const vboolf4& a, const vboolf4& b) {
  96. return _mm_shuffle_ps(a, b, _MM_SHUFFLE(i3, i2, i1, i0));
  97. }
  98. template<int i0>
  99. __forceinline vboolf4 shuffle(const vboolf4& v) {
  100. return shuffle<i0,i0,i0,i0>(v);
  101. }
  102. #if defined(__SSE3__)
  103. template<> __forceinline vboolf4 shuffle<0, 0, 2, 2>(const vboolf4& v) { return _mm_moveldup_ps(v); }
  104. template<> __forceinline vboolf4 shuffle<1, 1, 3, 3>(const vboolf4& v) { return _mm_movehdup_ps(v); }
  105. template<> __forceinline vboolf4 shuffle<0, 1, 0, 1>(const vboolf4& v) { return _mm_castpd_ps(_mm_movedup_pd(v)); }
  106. #endif
  107. #if defined(__SSE4_1__)
  108. template<int dst, int src, int clr> __forceinline vboolf4 insert(const vboolf4& a, const vboolf4& b) { return _mm_insert_ps(a, b, (dst << 4) | (src << 6) | clr); }
  109. template<int dst, int src> __forceinline vboolf4 insert(const vboolf4& a, const vboolf4& b) { return insert<dst, src, 0>(a, b); }
  110. template<int dst> __forceinline vboolf4 insert(const vboolf4& a, const bool b) { return insert<dst, 0>(a, vboolf4(b)); }
  111. #endif
  112. ////////////////////////////////////////////////////////////////////////////////
  113. /// Reduction Operations
  114. ////////////////////////////////////////////////////////////////////////////////
  115. __forceinline bool reduce_and(const vboolf4& a) { return _mm_movemask_ps(a) == 0xf; }
  116. __forceinline bool reduce_or (const vboolf4& a) { return _mm_movemask_ps(a) != 0x0; }
  117. __forceinline bool all (const vboolf4& b) { return _mm_movemask_ps(b) == 0xf; }
  118. __forceinline bool any (const vboolf4& b) { return _mm_movemask_ps(b) != 0x0; }
  119. __forceinline bool none(const vboolf4& b) { return _mm_movemask_ps(b) == 0x0; }
  120. __forceinline bool all (const vboolf4& valid, const vboolf4& b) { return all((!valid) | b); }
  121. __forceinline bool any (const vboolf4& valid, const vboolf4& b) { return any(valid & b); }
  122. __forceinline bool none(const vboolf4& valid, const vboolf4& b) { return none(valid & b); }
  123. __forceinline size_t movemask(const vboolf4& a) { return _mm_movemask_ps(a); }
  124. #if defined(__SSE4_2__)
  125. __forceinline size_t popcnt(const vboolf4& a) { return popcnt((size_t)_mm_movemask_ps(a)); }
  126. #else
  127. __forceinline size_t popcnt(const vboolf4& a) { return bool(a[0])+bool(a[1])+bool(a[2])+bool(a[3]); }
  128. #endif
  129. ////////////////////////////////////////////////////////////////////////////////
  130. /// Get/Set Functions
  131. ////////////////////////////////////////////////////////////////////////////////
  132. __forceinline bool get(const vboolf4& a, size_t index) { return a[index]; }
  133. __forceinline void set(vboolf4& a, size_t index) { a[index] = -1; }
  134. __forceinline void clear(vboolf4& a, size_t index) { a[index] = 0; }
  135. ////////////////////////////////////////////////////////////////////////////////
  136. /// Output Operators
  137. ////////////////////////////////////////////////////////////////////////////////
  138. __forceinline embree_ostream operator <<(embree_ostream cout, const vboolf4& a) {
  139. return cout << "<" << a[0] << ", " << a[1] << ", " << a[2] << ", " << a[3] << ">";
  140. }
  141. }
  142. #undef vboolf
  143. #undef vboold
  144. #undef vint
  145. #undef vuint
  146. #undef vllong
  147. #undef vfloat
  148. #undef vdouble