Path: blob/master/thirdparty/embree/common/simd/vboolf8_avx.h
9912 views
// Copyright 2009-2021 Intel Corporation1// SPDX-License-Identifier: Apache-2.023#pragma once45#define vboolf vboolf_impl6#define vboold vboold_impl7#define vint vint_impl8#define vuint vuint_impl9#define vllong vllong_impl10#define vfloat vfloat_impl11#define vdouble vdouble_impl1213namespace embree14{15/* 8-wide AVX bool type */16template<>17struct vboolf<8>18{19ALIGNED_STRUCT_(32);2021typedef vboolf8 Bool;22typedef vint8 Int;23typedef vfloat8 Float;2425enum { size = 8 }; // number of SIMD elements26union { // data27__m256 v;28struct { __m128 vl,vh; };29int i[8];30};3132////////////////////////////////////////////////////////////////////////////////33/// Constructors, Assignment & Cast Operators34////////////////////////////////////////////////////////////////////////////////3536__forceinline vboolf() {}37__forceinline vboolf(const vboolf8& a) { v = a.v; }38__forceinline vboolf8& operator =(const vboolf8& a) { v = a.v; return *this; }3940__forceinline vboolf(__m256 a) : v(a) {}41__forceinline operator const __m256&() const { return v; }42__forceinline operator const __m256i() const { return _mm256_castps_si256(v); }43__forceinline operator const __m256d() const { return _mm256_castps_pd(v); }4445__forceinline vboolf(int a)46{47assert(a >= 0 && a <= 255);48#if defined (__AVX2__)49const __m256i mask = _mm256_set_epi32(0x80, 0x40, 0x20, 0x10, 0x8, 0x4, 0x2, 0x1);50const __m256i b = _mm256_set1_epi32(a);51const __m256i c = _mm256_and_si256(b,mask);52v = _mm256_castsi256_ps(_mm256_cmpeq_epi32(c,mask));53#else54vl = mm_lookupmask_ps[a & 0xF];55vh = mm_lookupmask_ps[a >> 4];56#endif57}5859__forceinline vboolf(const vboolf4& a) : v(_mm256_insertf128_ps(_mm256_castps128_ps256(a),a,1)) {}60__forceinline vboolf(const vboolf4& a, const vboolf4& b) : v(_mm256_insertf128_ps(_mm256_castps128_ps256(a),b,1)) {}61__forceinline vboolf(__m128 a, __m128 b) : vl(a), vh(b) {}6263__forceinline vboolf(bool a) : v(vboolf8(vboolf4(a), vboolf4(a))) {}64__forceinline vboolf(bool a, bool b) : v(vboolf8(vboolf4(a), vboolf4(b))) {}65__forceinline vboolf(bool a, bool b, bool c, bool d) : v(vboolf8(vboolf4(a,b), vboolf4(c,d))) {}66__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))) {}6768/* return int32 mask */69__forceinline __m256i mask32() const {70return _mm256_castps_si256(v);71}7273////////////////////////////////////////////////////////////////////////////////74/// Constants75////////////////////////////////////////////////////////////////////////////////7677__forceinline vboolf(FalseTy) : v(_mm256_setzero_ps()) {}78__forceinline vboolf(TrueTy) : v(_mm256_castsi256_ps(_mm256_set1_epi32(0xFFFFFFFF))) {}7980////////////////////////////////////////////////////////////////////////////////81/// Array Access82////////////////////////////////////////////////////////////////////////////////8384__forceinline bool operator [](size_t index) const { assert(index < 8); return (_mm256_movemask_ps(v) >> index) & 1; }85__forceinline int& operator [](size_t index) { assert(index < 8); return i[index]; }86};8788////////////////////////////////////////////////////////////////////////////////89/// Unary Operators90////////////////////////////////////////////////////////////////////////////////9192__forceinline vboolf8 operator !(const vboolf8& a) { return _mm256_xor_ps(a, vboolf8(embree::True)); }9394////////////////////////////////////////////////////////////////////////////////95/// Binary Operators96////////////////////////////////////////////////////////////////////////////////9798__forceinline vboolf8 operator &(const vboolf8& a, const vboolf8& b) { return _mm256_and_ps(a, b); }99__forceinline vboolf8 operator |(const vboolf8& a, const vboolf8& b) { return _mm256_or_ps (a, b); }100__forceinline vboolf8 operator ^(const vboolf8& a, const vboolf8& b) { return _mm256_xor_ps(a, b); }101102__forceinline vboolf8 andn(const vboolf8& a, const vboolf8& b) { return _mm256_andnot_ps(b, a); }103104__forceinline vboolf8& operator &=(vboolf8& a, const vboolf8& b) { return a = a & b; }105__forceinline vboolf8& operator |=(vboolf8& a, const vboolf8& b) { return a = a | b; }106__forceinline vboolf8& operator ^=(vboolf8& a, const vboolf8& b) { return a = a ^ b; }107108////////////////////////////////////////////////////////////////////////////////109/// Comparison Operators + Select110////////////////////////////////////////////////////////////////////////////////111112__forceinline vboolf8 operator !=(const vboolf8& a, const vboolf8& b) { return _mm256_xor_ps(a, b); }113__forceinline vboolf8 operator ==(const vboolf8& a, const vboolf8& b) { return _mm256_xor_ps(_mm256_xor_ps(a,b),vboolf8(embree::True)); }114115__forceinline vboolf8 select(const vboolf8& mask, const vboolf8& t, const vboolf8& f) {116return _mm256_blendv_ps(f, t, mask);117}118119////////////////////////////////////////////////////////////////////////////////120/// Movement/Shifting/Shuffling Functions121////////////////////////////////////////////////////////////////////////////////122123__forceinline vboolf8 unpacklo(const vboolf8& a, const vboolf8& b) { return _mm256_unpacklo_ps(a, b); }124__forceinline vboolf8 unpackhi(const vboolf8& a, const vboolf8& b) { return _mm256_unpackhi_ps(a, b); }125126template<int i>127__forceinline vboolf8 shuffle(const vboolf8& v) {128return _mm256_permute_ps(v, _MM_SHUFFLE(i, i, i, i));129}130131template<int i0, int i1>132__forceinline vboolf8 shuffle4(const vboolf8& v) {133return _mm256_permute2f128_ps(v, v, (i1 << 4) | (i0 << 0));134}135136template<int i0, int i1>137__forceinline vboolf8 shuffle4(const vboolf8& a, const vboolf8& b) {138return _mm256_permute2f128_ps(a, b, (i1 << 4) | (i0 << 0));139}140141template<int i0, int i1, int i2, int i3>142__forceinline vboolf8 shuffle(const vboolf8& v) {143return _mm256_permute_ps(v, _MM_SHUFFLE(i3, i2, i1, i0));144}145146template<int i0, int i1, int i2, int i3>147__forceinline vboolf8 shuffle(const vboolf8& a, const vboolf8& b) {148return _mm256_shuffle_ps(a, b, _MM_SHUFFLE(i3, i2, i1, i0));149}150151template<> __forceinline vboolf8 shuffle<0, 0, 2, 2>(const vboolf8& v) { return _mm256_moveldup_ps(v); }152template<> __forceinline vboolf8 shuffle<1, 1, 3, 3>(const vboolf8& v) { return _mm256_movehdup_ps(v); }153template<> __forceinline vboolf8 shuffle<0, 1, 0, 1>(const vboolf8& v) { return _mm256_castpd_ps(_mm256_movedup_pd(_mm256_castps_pd(v))); }154155template<int i> __forceinline vboolf8 insert4(const vboolf8& a, const vboolf4& b) { return _mm256_insertf128_ps(a, b, i); }156template<int i> __forceinline vboolf4 extract4 (const vboolf8& a) { return _mm256_extractf128_ps(a, i); }157template<> __forceinline vboolf4 extract4<0>(const vboolf8& a) { return _mm256_castps256_ps128(a); }158159////////////////////////////////////////////////////////////////////////////////160/// Reduction Operations161////////////////////////////////////////////////////////////////////////////////162163__forceinline bool reduce_and(const vboolf8& a) { return _mm256_movemask_ps(a) == (unsigned int)0xff; }164__forceinline bool reduce_or (const vboolf8& a) { return !_mm256_testz_ps(a,a); }165166__forceinline bool all (const vboolf8& a) { return _mm256_movemask_ps(a) == (unsigned int)0xff; }167__forceinline bool any (const vboolf8& a) { return !_mm256_testz_ps(a,a); }168__forceinline bool none(const vboolf8& a) { return _mm256_testz_ps(a,a) != 0; }169170__forceinline bool all (const vboolf8& valid, const vboolf8& b) { return all((!valid) | b); }171__forceinline bool any (const vboolf8& valid, const vboolf8& b) { return any(valid & b); }172__forceinline bool none(const vboolf8& valid, const vboolf8& b) { return none(valid & b); }173174__forceinline unsigned int movemask(const vboolf8& a) { return _mm256_movemask_ps(a); }175__forceinline size_t popcnt (const vboolf8& a) { return popcnt((size_t)_mm256_movemask_ps(a)); }176177////////////////////////////////////////////////////////////////////////////////178/// Get/Set Functions179////////////////////////////////////////////////////////////////////////////////180181__forceinline bool get(const vboolf8& a, size_t index) { return a[index]; }182__forceinline void set(vboolf8& a, size_t index) { a[index] = -1; }183__forceinline void clear(vboolf8& a, size_t index) { a[index] = 0; }184185////////////////////////////////////////////////////////////////////////////////186/// Output Operators187////////////////////////////////////////////////////////////////////////////////188189__forceinline embree_ostream operator <<(embree_ostream cout, const vboolf8& a) {190return cout << "<" << a[0] << ", " << a[1] << ", " << a[2] << ", " << a[3] << ", "191<< a[4] << ", " << a[5] << ", " << a[6] << ", " << a[7] << ">";192}193}194195#undef vboolf196#undef vboold197#undef vint198#undef vuint199#undef vllong200#undef vfloat201#undef vdouble202203204