vboolf4_avx512.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // ======================================================================== //
  2. // Copyright 2009-2017 Intel Corporation //
  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. #pragma once
  17. namespace embree
  18. {
  19. /* 4-wide AVX-512 bool type */
  20. template<>
  21. struct vboolf<4>
  22. {
  23. typedef vboolf4 Bool;
  24. typedef vint4 Int;
  25. enum { size = 4 }; // number of SIMD elements
  26. __mmask8 v; // data
  27. ////////////////////////////////////////////////////////////////////////////////
  28. /// Constructors, Assignment & Cast Operators
  29. ////////////////////////////////////////////////////////////////////////////////
  30. __forceinline vboolf() {}
  31. __forceinline vboolf(const vboolf4 &t) { v = t.v; }
  32. __forceinline vboolf4& operator=(const vboolf4 &f) { v = f.v; return *this; }
  33. __forceinline vboolf(const __mmask8 &t) { v = t; }
  34. __forceinline operator __mmask8() const { return v; }
  35. __forceinline vboolf(bool b) { v = b ? 0xf : 0x0; }
  36. __forceinline vboolf(int t) { v = (__mmask8)t; }
  37. __forceinline vboolf(unsigned int t) { v = (__mmask8)t; }
  38. /* return int8 mask */
  39. __forceinline __m128i mask8() const {
  40. return _mm_movm_epi8(v);
  41. }
  42. /* return int32 mask */
  43. __forceinline __m128i mask32() const {
  44. return _mm_movm_epi32(v);
  45. }
  46. /* return int64 mask */
  47. __forceinline __m256i mask64() const {
  48. return _mm256_movm_epi64(v);
  49. }
  50. ////////////////////////////////////////////////////////////////////////////////
  51. /// Constants
  52. ////////////////////////////////////////////////////////////////////////////////
  53. __forceinline vboolf( FalseTy ) : v(0x0) {}
  54. __forceinline vboolf( TrueTy ) : v(0xf) {}
  55. };
  56. ////////////////////////////////////////////////////////////////////////////////
  57. /// Unary Operators
  58. ////////////////////////////////////////////////////////////////////////////////
  59. __forceinline vboolf4 operator!(const vboolf4 &a) { return _mm512_kandn(a, 0xf); }
  60. ////////////////////////////////////////////////////////////////////////////////
  61. /// Binary Operators
  62. ////////////////////////////////////////////////////////////////////////////////
  63. __forceinline vboolf4 operator&(const vboolf4 &a, const vboolf4 &b) { return _mm512_kand(a, b); }
  64. __forceinline vboolf4 operator|(const vboolf4 &a, const vboolf4 &b) { return _mm512_kor(a, b); }
  65. __forceinline vboolf4 operator^(const vboolf4 &a, const vboolf4 &b) { return _mm512_kxor(a, b); }
  66. __forceinline vboolf4 andn(const vboolf4 &a, const vboolf4 &b) { return _mm512_kandn(b, a); }
  67. ////////////////////////////////////////////////////////////////////////////////
  68. /// Assignment Operators
  69. ////////////////////////////////////////////////////////////////////////////////
  70. __forceinline const vboolf4 operator &=( vboolf4& a, const vboolf4& b ) { return a = a & b; }
  71. __forceinline const vboolf4 operator |=( vboolf4& a, const vboolf4& b ) { return a = a | b; }
  72. __forceinline const vboolf4 operator ^=( vboolf4& a, const vboolf4& b ) { return a = a ^ b; }
  73. ////////////////////////////////////////////////////////////////////////////////
  74. /// Comparison Operators + Select
  75. ////////////////////////////////////////////////////////////////////////////////
  76. __forceinline const vboolf4 operator !=( const vboolf4& a, const vboolf4& b ) { return _mm512_kxor(a, b); }
  77. __forceinline const vboolf4 operator ==( const vboolf4& a, const vboolf4& b ) { return _mm512_kand(_mm512_kxnor(a, b), 0xf); }
  78. __forceinline vboolf4 select (const vboolf4 &s, const vboolf4 &a, const vboolf4 &b) {
  79. return _mm512_kor(_mm512_kand(s, a), _mm512_kandn(s, b));
  80. }
  81. ////////////////////////////////////////////////////////////////////////////////
  82. /// Reduction Operations
  83. ////////////////////////////////////////////////////////////////////////////////
  84. __forceinline int all (const vboolf4 &a) { return a.v == 0xf; }
  85. __forceinline int any (const vboolf4 &a) { return _mm512_kortestz(a, a) == 0; }
  86. __forceinline int none(const vboolf4 &a) { return _mm512_kortestz(a, a) != 0; }
  87. __forceinline int all ( const vboolf4& valid, const vboolf4& b ) { return all((!valid) | b); }
  88. __forceinline int any ( const vboolf4& valid, const vboolf4& b ) { return any( valid & b); }
  89. __forceinline int none( const vboolf4& valid, const vboolf4& b ) { return none(valid & b); }
  90. __forceinline size_t movemask( const vboolf4& a ) { return _mm512_kmov(a); }
  91. __forceinline size_t popcnt ( const vboolf4& a ) { return __popcnt(a.v); }
  92. ////////////////////////////////////////////////////////////////////////////////
  93. /// Conversion Operations
  94. ////////////////////////////////////////////////////////////////////////////////
  95. __forceinline unsigned int toInt(const vboolf4 &a) { return _mm512_mask2int(a); }
  96. ////////////////////////////////////////////////////////////////////////////////
  97. /// Get/Set Functions
  98. ////////////////////////////////////////////////////////////////////////////////
  99. __forceinline bool get(const vboolf4& a, size_t index) { assert(index < 4); return (toInt(a) >> index) & 1; }
  100. __forceinline void set(vboolf4& a, size_t index) { assert(index < 4); a |= 1 << index; }
  101. __forceinline void clear(vboolf4& a, size_t index) { assert(index < 4); a = andn(a, 1 << index); }
  102. ////////////////////////////////////////////////////////////////////////////////
  103. /// Output Operators
  104. ////////////////////////////////////////////////////////////////////////////////
  105. inline std::ostream& operator<<(std::ostream& cout, const vboolf4& a)
  106. {
  107. cout << "<";
  108. for (size_t i=0; i<4; i++) {
  109. if ((a.v >> i) & 1) cout << "1"; else cout << "0";
  110. }
  111. return cout << ">";
  112. }
  113. }