vboolf8_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. /* 8-wide AVX-512 bool type */
  20. template<>
  21. struct vboolf<8>
  22. {
  23. typedef vboolf8 Bool;
  24. typedef vint8 Int;
  25. enum { size = 8 }; // number of SIMD elements
  26. __mmask8 v; // data
  27. ////////////////////////////////////////////////////////////////////////////////
  28. /// Constructors, Assignment & Cast Operators
  29. ////////////////////////////////////////////////////////////////////////////////
  30. __forceinline vboolf() {}
  31. __forceinline vboolf(const vboolf8 &t) { v = t.v; }
  32. __forceinline vboolf8& operator=(const vboolf8 &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 ? 0xff : 0x00; }
  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 __m256i mask32() const {
  44. return _mm256_movm_epi32(v);
  45. }
  46. /* return int64 mask */
  47. __forceinline __m512i mask64() const {
  48. return _mm512_movm_epi64(v);
  49. }
  50. ////////////////////////////////////////////////////////////////////////////////
  51. /// Constants
  52. ////////////////////////////////////////////////////////////////////////////////
  53. __forceinline vboolf( FalseTy ) : v(0x00) {}
  54. __forceinline vboolf( TrueTy ) : v(0xff) {}
  55. };
  56. ////////////////////////////////////////////////////////////////////////////////
  57. /// Unary Operators
  58. ////////////////////////////////////////////////////////////////////////////////
  59. __forceinline vboolf8 operator!(const vboolf8 &a) { return _mm512_knot(a); }
  60. ////////////////////////////////////////////////////////////////////////////////
  61. /// Binary Operators
  62. ////////////////////////////////////////////////////////////////////////////////
  63. __forceinline vboolf8 operator&(const vboolf8 &a, const vboolf8 &b) { return _mm512_kand(a, b); }
  64. __forceinline vboolf8 operator|(const vboolf8 &a, const vboolf8 &b) { return _mm512_kor(a, b); }
  65. __forceinline vboolf8 operator^(const vboolf8 &a, const vboolf8 &b) { return _mm512_kxor(a, b); }
  66. __forceinline vboolf8 andn(const vboolf8 &a, const vboolf8 &b) { return _mm512_kandn(b, a); }
  67. ////////////////////////////////////////////////////////////////////////////////
  68. /// Assignment Operators
  69. ////////////////////////////////////////////////////////////////////////////////
  70. __forceinline const vboolf8 operator &=( vboolf8& a, const vboolf8& b ) { return a = a & b; }
  71. __forceinline const vboolf8 operator |=( vboolf8& a, const vboolf8& b ) { return a = a | b; }
  72. __forceinline const vboolf8 operator ^=( vboolf8& a, const vboolf8& b ) { return a = a ^ b; }
  73. ////////////////////////////////////////////////////////////////////////////////
  74. /// Comparison Operators + Select
  75. ////////////////////////////////////////////////////////////////////////////////
  76. __forceinline const vboolf8 operator !=( const vboolf8& a, const vboolf8& b ) { return _mm512_kxor(a, b); }
  77. __forceinline const vboolf8 operator ==( const vboolf8& a, const vboolf8& b ) { return _mm512_kxnor(a, b); }
  78. __forceinline vboolf8 select(const vboolf8 &s, const vboolf8 &a, const vboolf8 &b) {
  79. return _mm512_kor(_mm512_kand(s, a), _mm512_kandn(s, b));
  80. }
  81. ////////////////////////////////////////////////////////////////////////////////
  82. /// Reduction Operations
  83. ////////////////////////////////////////////////////////////////////////////////
  84. __forceinline int all (const vboolf8 &a) { return a.v == 0xff; }
  85. __forceinline int any (const vboolf8 &a) { return _mm512_kortestz(a, a) == 0; }
  86. __forceinline int none(const vboolf8 &a) { return _mm512_kortestz(a, a) != 0; }
  87. __forceinline int all ( const vboolf8& valid, const vboolf8& b ) { return all((!valid) | b); }
  88. __forceinline int any ( const vboolf8& valid, const vboolf8& b ) { return any( valid & b); }
  89. __forceinline int none( const vboolf8& valid, const vboolf8& b ) { return none(valid & b); }
  90. __forceinline size_t movemask( const vboolf8& a ) { return _mm512_kmov(a); }
  91. __forceinline size_t popcnt ( const vboolf8& a ) { return __popcnt(a.v); }
  92. ////////////////////////////////////////////////////////////////////////////////
  93. /// Conversion Operations
  94. ////////////////////////////////////////////////////////////////////////////////
  95. __forceinline unsigned int toInt(const vboolf8 &a) { return _mm512_mask2int(a); }
  96. ////////////////////////////////////////////////////////////////////////////////
  97. /// Get/Set Functions
  98. ////////////////////////////////////////////////////////////////////////////////
  99. __forceinline bool get(const vboolf8& a, size_t index) { assert(index < 8); return (toInt(a) >> index) & 1; }
  100. __forceinline void set(vboolf8& a, size_t index) { assert(index < 8); a |= 1 << index; }
  101. __forceinline void clear(vboolf8& a, size_t index) { assert(index < 8); a = andn(a, 1 << index); }
  102. ////////////////////////////////////////////////////////////////////////////////
  103. /// Output Operators
  104. ////////////////////////////////////////////////////////////////////////////////
  105. inline std::ostream& operator<<(std::ostream& cout, const vboolf8& a)
  106. {
  107. cout << "<";
  108. for (size_t i=0; i<8; i++) {
  109. if ((a.v >> i) & 1) cout << "1"; else cout << "0";
  110. }
  111. return cout << ">";
  112. }
  113. }