0.2.0-dev Download ZIP
156 lines ·6.0 KB Raw
| 1 | // Copyright 2009-2021 Intel Corporation |
| 2 | // SPDX-License-Identifier: Apache-2.0 |
| 3 | |
| 4 | #pragma once |
| 5 | |
| 6 | #define vboolf vboolf_impl |
| 7 | #define vboold vboold_impl |
| 8 | #define vint vint_impl |
| 9 | #define vuint vuint_impl |
| 10 | #define vllong vllong_impl |
| 11 | #define vfloat vfloat_impl |
| 12 | #define vdouble vdouble_impl |
| 13 | |
| 14 | namespace embree |
| 15 | { |
| 16 | /* 4-wide AVX-512 bool type */ |
| 17 | template<> |
| 18 | struct vboold<4> |
| 19 | { |
| 20 | typedef vboold4 Bool; |
| 21 | typedef vint4 Int; |
| 22 | |
| 23 | enum { size = 4 }; // number of SIMD elements |
| 24 | __mmask8 v; // data |
| 25 | |
| 26 | //////////////////////////////////////////////////////////////////////////////// |
| 27 | /// Constructors, Assignment & Cast Operators |
| 28 | //////////////////////////////////////////////////////////////////////////////// |
| 29 | |
| 30 | __forceinline vboold() {} |
| 31 | __forceinline vboold(const vboold4& t) { v = t.v; } |
| 32 | __forceinline vboold4& operator =(const vboold4& f) { v = f.v; return *this; } |
| 33 | |
| 34 | __forceinline vboold(const __mmask8 &t) { v = t; } |
| 35 | __forceinline operator __mmask8() const { return v; } |
| 36 | |
| 37 | __forceinline vboold(bool b) { v = b ? 0xf : 0x0; } |
| 38 | __forceinline vboold(int t) { v = (__mmask8)t; } |
| 39 | __forceinline vboold(unsigned int t) { v = (__mmask8)t; } |
| 40 | |
| 41 | /* return int8 mask */ |
| 42 | __forceinline __m128i mask8() const { |
| 43 | return _mm_movm_epi8(v); |
| 44 | } |
| 45 | |
| 46 | /* return int32 mask */ |
| 47 | __forceinline __m128i mask32() const { |
| 48 | return _mm_movm_epi32(v); |
| 49 | } |
| 50 | |
| 51 | /* return int64 mask */ |
| 52 | __forceinline __m256i mask64() const { |
| 53 | return _mm256_movm_epi64(v); |
| 54 | } |
| 55 | |
| 56 | //////////////////////////////////////////////////////////////////////////////// |
| 57 | /// Constants |
| 58 | //////////////////////////////////////////////////////////////////////////////// |
| 59 | |
| 60 | __forceinline vboold(FalseTy) : v(0x0) {} |
| 61 | __forceinline vboold(TrueTy) : v(0xf) {} |
| 62 | |
| 63 | //////////////////////////////////////////////////////////////////////////////// |
| 64 | /// Array Access |
| 65 | //////////////////////////////////////////////////////////////////////////////// |
| 66 | |
| 67 | __forceinline bool operator [](size_t index) const { |
| 68 | assert(index < 4); return (mm512_mask2int(v) >> index) & 1; |
| 69 | } |
| 70 | }; |
| 71 | |
| 72 | //////////////////////////////////////////////////////////////////////////////// |
| 73 | /// Unary Operators |
| 74 | //////////////////////////////////////////////////////////////////////////////// |
| 75 | |
| 76 | __forceinline vboold4 operator !(const vboold4& a) { return _mm512_kandn(a, 0xf); } |
| 77 | |
| 78 | //////////////////////////////////////////////////////////////////////////////// |
| 79 | /// Binary Operators |
| 80 | //////////////////////////////////////////////////////////////////////////////// |
| 81 | |
| 82 | __forceinline vboold4 operator &(const vboold4& a, const vboold4& b) { return _mm512_kand(a, b); } |
| 83 | __forceinline vboold4 operator |(const vboold4& a, const vboold4& b) { return _mm512_kor(a, b); } |
| 84 | __forceinline vboold4 operator ^(const vboold4& a, const vboold4& b) { return _mm512_kxor(a, b); } |
| 85 | |
| 86 | __forceinline vboold4 andn(const vboold4& a, const vboold4& b) { return _mm512_kandn(b, a); } |
| 87 | |
| 88 | //////////////////////////////////////////////////////////////////////////////// |
| 89 | /// Assignment Operators |
| 90 | //////////////////////////////////////////////////////////////////////////////// |
| 91 | |
| 92 | __forceinline vboold4& operator &=(vboold4& a, const vboold4& b) { return a = a & b; } |
| 93 | __forceinline vboold4& operator |=(vboold4& a, const vboold4& b) { return a = a | b; } |
| 94 | __forceinline vboold4& operator ^=(vboold4& a, const vboold4& b) { return a = a ^ b; } |
| 95 | |
| 96 | //////////////////////////////////////////////////////////////////////////////// |
| 97 | /// Comparison Operators + Select |
| 98 | //////////////////////////////////////////////////////////////////////////////// |
| 99 | |
| 100 | __forceinline vboold4 operator !=(const vboold4& a, const vboold4& b) { return _mm512_kxor(a, b); } |
| 101 | __forceinline vboold4 operator ==(const vboold4& a, const vboold4& b) { return _mm512_kand(_mm512_kxnor(a, b), 0xf); } |
| 102 | |
| 103 | __forceinline vboold4 select(const vboold4& s, const vboold4& a, const vboold4& b) { |
| 104 | return _mm512_kor(_mm512_kand(s, a), _mm512_kandn(s, b)); |
| 105 | } |
| 106 | |
| 107 | //////////////////////////////////////////////////////////////////////////////// |
| 108 | /// Reduction Operations |
| 109 | //////////////////////////////////////////////////////////////////////////////// |
| 110 | |
| 111 | __forceinline int all (const vboold4& a) { return a.v == 0xf; } |
| 112 | __forceinline int any (const vboold4& a) { return _mm512_kortestz(a, a) == 0; } |
| 113 | __forceinline int none(const vboold4& a) { return _mm512_kortestz(a, a) != 0; } |
| 114 | |
| 115 | __forceinline int all (const vboold4& valid, const vboold4& b) { return all((!valid) | b); } |
| 116 | __forceinline int any (const vboold4& valid, const vboold4& b) { return any(valid & b); } |
| 117 | __forceinline int none(const vboold4& valid, const vboold4& b) { return none(valid & b); } |
| 118 | |
| 119 | __forceinline size_t movemask(const vboold4& a) { return _mm512_kmov(a); } |
| 120 | __forceinline size_t popcnt (const vboold4& a) { return popcnt(a.v); } |
| 121 | |
| 122 | //////////////////////////////////////////////////////////////////////////////// |
| 123 | /// Conversion Operations |
| 124 | //////////////////////////////////////////////////////////////////////////////// |
| 125 | |
| 126 | __forceinline unsigned int toInt(const vboold4& a) { return mm512_mask2int(a); } |
| 127 | |
| 128 | //////////////////////////////////////////////////////////////////////////////// |
| 129 | /// Get/Set Functions |
| 130 | //////////////////////////////////////////////////////////////////////////////// |
| 131 | |
| 132 | __forceinline bool get(const vboold4& a, size_t index) { assert(index < 4); return (toInt(a) >> index) & 1; } |
| 133 | __forceinline void set(vboold4& a, size_t index) { assert(index < 4); a |= 1 << index; } |
| 134 | __forceinline void clear(vboold4& a, size_t index) { assert(index < 4); a = andn(a, 1 << index); } |
| 135 | |
| 136 | //////////////////////////////////////////////////////////////////////////////// |
| 137 | /// Output Operators |
| 138 | //////////////////////////////////////////////////////////////////////////////// |
| 139 | |
| 140 | __forceinline embree_ostream operator <<(embree_ostream cout, const vboold4& a) |
| 141 | { |
| 142 | cout << "<"; |
| 143 | for (size_t i=0; i<4; i++) { |
| 144 | if ((a.v >> i) & 1) cout << "1"; else cout << "0"; |
| 145 | } |
| 146 | return cout << ">"; |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | #undef vboolf |
| 151 | #undef vboold |
| 152 | #undef vint |
| 153 | #undef vuint |
| 154 | #undef vllong |
| 155 | #undef vfloat |
| 156 | #undef vdouble |