vboolf8_avx.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. /* 8-wide AVX bool type */
  14. template<>
  15. struct vboolf<8>
  16. {
  17. ALIGNED_STRUCT_(32);
  18. typedef vboolf8 Bool;
  19. typedef vint8 Int;
  20. typedef vfloat8 Float;
  21. enum { size = 8 }; // number of SIMD elements
  22. union { // data
  23. __m256 v;
  24. struct { __m128 vl,vh; };
  25. int i[8];
  26. };
  27. ////////////////////////////////////////////////////////////////////////////////
  28. /// Constructors, Assignment & Cast Operators
  29. ////////////////////////////////////////////////////////////////////////////////
  30. __forceinline vboolf() {}
  31. __forceinline vboolf(const vboolf8& a) { v = a.v; }
  32. __forceinline vboolf8& operator =(const vboolf8& a) { v = a.v; return *this; }
  33. __forceinline vboolf(__m256 a) : v(a) {}
  34. __forceinline operator const __m256&() const { return v; }
  35. __forceinline operator const __m256i() const { return _mm256_castps_si256(v); }
  36. __forceinline operator const __m256d() const { return _mm256_castps_pd(v); }
  37. __forceinline vboolf(int a)
  38. {
  39. assert(a >= 0 && a <= 255);
  40. #if defined (__AVX2__)
  41. const __m256i mask = _mm256_set_epi32(0x80, 0x40, 0x20, 0x10, 0x8, 0x4, 0x2, 0x1);
  42. const __m256i b = _mm256_set1_epi32(a);
  43. const __m256i c = _mm256_and_si256(b,mask);
  44. v = _mm256_castsi256_ps(_mm256_cmpeq_epi32(c,mask));
  45. #else
  46. vl = mm_lookupmask_ps[a & 0xF];
  47. vh = mm_lookupmask_ps[a >> 4];
  48. #endif
  49. }
  50. __forceinline vboolf(const vboolf4& a) : v(_mm256_insertf128_ps(_mm256_castps128_ps256(a),a,1)) {}
  51. __forceinline vboolf(const vboolf4& a, const vboolf4& b) : v(_mm256_insertf128_ps(_mm256_castps128_ps256(a),b,1)) {}
  52. __forceinline vboolf(__m128 a, __m128 b) : vl(a), vh(b) {}
  53. __forceinline vboolf(bool a) : v(vboolf8(vboolf4(a), vboolf4(a))) {}
  54. __forceinline vboolf(bool a, bool b) : v(vboolf8(vboolf4(a), vboolf4(b))) {}
  55. __forceinline vboolf(bool a, bool b, bool c, bool d) : v(vboolf8(vboolf4(a,b), vboolf4(c,d))) {}
  56. __forceinline vboolf(bool a, bool b, bool c, bool d, bool e, bool f, bool g, bool h) : v(vboolf8(vboolf4(a,b,c,d), vboolf4(e,f,g,h))) {}
  57. /* return int32 mask */
  58. __forceinline __m256i mask32() const {
  59. return _mm256_castps_si256(v);
  60. }
  61. ////////////////////////////////////////////////////////////////////////////////
  62. /// Constants
  63. ////////////////////////////////////////////////////////////////////////////////
  64. __forceinline vboolf(FalseTy) : v(_mm256_setzero_ps()) {}
  65. __forceinline vboolf(TrueTy) : v(_mm256_castsi256_ps(_mm256_set1_epi32(0xFFFFFFFF))) {}
  66. ////////////////////////////////////////////////////////////////////////////////
  67. /// Array Access
  68. ////////////////////////////////////////////////////////////////////////////////
  69. __forceinline bool operator [](size_t index) const { assert(index < 8); return (_mm256_movemask_ps(v) >> index) & 1; }
  70. __forceinline int& operator [](size_t index) { assert(index < 8); return i[index]; }
  71. };
  72. ////////////////////////////////////////////////////////////////////////////////
  73. /// Unary Operators
  74. ////////////////////////////////////////////////////////////////////////////////
  75. __forceinline vboolf8 operator !(const vboolf8& a) { return _mm256_xor_ps(a, vboolf8(embree::True)); }
  76. ////////////////////////////////////////////////////////////////////////////////
  77. /// Binary Operators
  78. ////////////////////////////////////////////////////////////////////////////////
  79. __forceinline vboolf8 operator &(const vboolf8& a, const vboolf8& b) { return _mm256_and_ps(a, b); }
  80. __forceinline vboolf8 operator |(const vboolf8& a, const vboolf8& b) { return _mm256_or_ps (a, b); }
  81. __forceinline vboolf8 operator ^(const vboolf8& a, const vboolf8& b) { return _mm256_xor_ps(a, b); }
  82. __forceinline vboolf8 andn(const vboolf8& a, const vboolf8& b) { return _mm256_andnot_ps(b, a); }
  83. __forceinline vboolf8& operator &=(vboolf8& a, const vboolf8& b) { return a = a & b; }
  84. __forceinline vboolf8& operator |=(vboolf8& a, const vboolf8& b) { return a = a | b; }
  85. __forceinline vboolf8& operator ^=(vboolf8& a, const vboolf8& b) { return a = a ^ b; }
  86. ////////////////////////////////////////////////////////////////////////////////
  87. /// Comparison Operators + Select
  88. ////////////////////////////////////////////////////////////////////////////////
  89. __forceinline vboolf8 operator !=(const vboolf8& a, const vboolf8& b) { return _mm256_xor_ps(a, b); }
  90. __forceinline vboolf8 operator ==(const vboolf8& a, const vboolf8& b) { return _mm256_xor_ps(_mm256_xor_ps(a,b),vboolf8(embree::True)); }
  91. __forceinline vboolf8 select(const vboolf8& mask, const vboolf8& t, const vboolf8& f) {
  92. return _mm256_blendv_ps(f, t, mask);
  93. }
  94. ////////////////////////////////////////////////////////////////////////////////
  95. /// Movement/Shifting/Shuffling Functions
  96. ////////////////////////////////////////////////////////////////////////////////
  97. __forceinline vboolf8 unpacklo(const vboolf8& a, const vboolf8& b) { return _mm256_unpacklo_ps(a, b); }
  98. __forceinline vboolf8 unpackhi(const vboolf8& a, const vboolf8& b) { return _mm256_unpackhi_ps(a, b); }
  99. template<int i>
  100. __forceinline vboolf8 shuffle(const vboolf8& v) {
  101. return _mm256_permute_ps(v, _MM_SHUFFLE(i, i, i, i));
  102. }
  103. template<int i0, int i1>
  104. __forceinline vboolf8 shuffle4(const vboolf8& v) {
  105. return _mm256_permute2f128_ps(v, v, (i1 << 4) | (i0 << 0));
  106. }
  107. template<int i0, int i1>
  108. __forceinline vboolf8 shuffle4(const vboolf8& a, const vboolf8& b) {
  109. return _mm256_permute2f128_ps(a, b, (i1 << 4) | (i0 << 0));
  110. }
  111. template<int i0, int i1, int i2, int i3>
  112. __forceinline vboolf8 shuffle(const vboolf8& v) {
  113. return _mm256_permute_ps(v, _MM_SHUFFLE(i3, i2, i1, i0));
  114. }
  115. template<int i0, int i1, int i2, int i3>
  116. __forceinline vboolf8 shuffle(const vboolf8& a, const vboolf8& b) {
  117. return _mm256_shuffle_ps(a, b, _MM_SHUFFLE(i3, i2, i1, i0));
  118. }
  119. template<> __forceinline vboolf8 shuffle<0, 0, 2, 2>(const vboolf8& v) { return _mm256_moveldup_ps(v); }
  120. template<> __forceinline vboolf8 shuffle<1, 1, 3, 3>(const vboolf8& v) { return _mm256_movehdup_ps(v); }
  121. template<> __forceinline vboolf8 shuffle<0, 1, 0, 1>(const vboolf8& v) { return _mm256_castpd_ps(_mm256_movedup_pd(_mm256_castps_pd(v))); }
  122. template<int i> __forceinline vboolf8 insert4(const vboolf8& a, const vboolf4& b) { return _mm256_insertf128_ps(a, b, i); }
  123. template<int i> __forceinline vboolf4 extract4 (const vboolf8& a) { return _mm256_extractf128_ps(a, i); }
  124. template<> __forceinline vboolf4 extract4<0>(const vboolf8& a) { return _mm256_castps256_ps128(a); }
  125. ////////////////////////////////////////////////////////////////////////////////
  126. /// Reduction Operations
  127. ////////////////////////////////////////////////////////////////////////////////
  128. __forceinline bool reduce_and(const vboolf8& a) { return _mm256_movemask_ps(a) == (unsigned int)0xff; }
  129. __forceinline bool reduce_or (const vboolf8& a) { return !_mm256_testz_ps(a,a); }
  130. __forceinline bool all (const vboolf8& a) { return _mm256_movemask_ps(a) == (unsigned int)0xff; }
  131. __forceinline bool any (const vboolf8& a) { return !_mm256_testz_ps(a,a); }
  132. __forceinline bool none(const vboolf8& a) { return _mm256_testz_ps(a,a) != 0; }
  133. __forceinline bool all (const vboolf8& valid, const vboolf8& b) { return all((!valid) | b); }
  134. __forceinline bool any (const vboolf8& valid, const vboolf8& b) { return any(valid & b); }
  135. __forceinline bool none(const vboolf8& valid, const vboolf8& b) { return none(valid & b); }
  136. __forceinline unsigned int movemask(const vboolf8& a) { return _mm256_movemask_ps(a); }
  137. __forceinline size_t popcnt (const vboolf8& a) { return popcnt((size_t)_mm256_movemask_ps(a)); }
  138. ////////////////////////////////////////////////////////////////////////////////
  139. /// Get/Set Functions
  140. ////////////////////////////////////////////////////////////////////////////////
  141. __forceinline bool get(const vboolf8& a, size_t index) { return a[index]; }
  142. __forceinline void set(vboolf8& a, size_t index) { a[index] = -1; }
  143. __forceinline void clear(vboolf8& a, size_t index) { a[index] = 0; }
  144. ////////////////////////////////////////////////////////////////////////////////
  145. /// Output Operators
  146. ////////////////////////////////////////////////////////////////////////////////
  147. __forceinline embree_ostream operator <<(embree_ostream cout, const vboolf8& a) {
  148. return cout << "<" << a[0] << ", " << a[1] << ", " << a[2] << ", " << a[3] << ", "
  149. << a[4] << ", " << a[5] << ", " << a[6] << ", " << a[7] << ">";
  150. }
  151. }
  152. #undef vboolf
  153. #undef vboold
  154. #undef vint
  155. #undef vuint
  156. #undef vllong
  157. #undef vfloat
  158. #undef vdouble