Path: blob/master/thirdparty/embree/common/simd/vint8_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 integer type */16template<>17struct vint<8>18{19ALIGNED_STRUCT_(32);2021typedef vboolf8 Bool;22typedef vint8 Int;23typedef vfloat8 Float;2425enum { size = 8 }; // number of SIMD elements26union { // data27__m256i v;28struct { __m128i vl,vh; };29int i[8];30};3132////////////////////////////////////////////////////////////////////////////////33/// Constructors, Assignment & Cast Operators34////////////////////////////////////////////////////////////////////////////////3536__forceinline vint() {}37__forceinline vint(const vint8& a) { v = a.v; }38__forceinline vint8& operator =(const vint8& a) { v = a.v; return *this; }3940__forceinline vint(__m256i a) : v(a) {}41__forceinline operator const __m256i&() const { return v; }42__forceinline operator __m256i&() { return v; }4344__forceinline explicit vint(const vint4& a) : v(_mm256_insertf128_si256(_mm256_castsi128_si256(a),a,1)) {}45__forceinline vint(const vint4& a, const vint4& b) : v(_mm256_insertf128_si256(_mm256_castsi128_si256(a),b,1)) {}46__forceinline vint(const __m128i& a, const __m128i& b) : vl(a), vh(b) {}4748__forceinline explicit vint(const int* a) : v(_mm256_castps_si256(_mm256_loadu_ps((const float*)a))) {}49__forceinline vint(int a) : v(_mm256_set1_epi32(a)) {}50__forceinline vint(int a, int b) : v(_mm256_set_epi32(b, a, b, a, b, a, b, a)) {}51__forceinline vint(int a, int b, int c, int d) : v(_mm256_set_epi32(d, c, b, a, d, c, b, a)) {}52__forceinline vint(int a, int b, int c, int d, int e, int f, int g, int vh) : v(_mm256_set_epi32(vh, g, f, e, d, c, b, a)) {}5354__forceinline explicit vint(__m256 a) : v(_mm256_cvtps_epi32(a)) {}5556////////////////////////////////////////////////////////////////////////////////57/// Constants58////////////////////////////////////////////////////////////////////////////////5960__forceinline vint(ZeroTy) : v(_mm256_setzero_si256()) {}61__forceinline vint(OneTy) : v(_mm256_set_epi32(1,1,1,1,1,1,1,1)) {}62__forceinline vint(PosInfTy) : v(_mm256_set_epi32(pos_inf,pos_inf,pos_inf,pos_inf,pos_inf,pos_inf,pos_inf,pos_inf)) {}63__forceinline vint(NegInfTy) : v(_mm256_set_epi32(neg_inf,neg_inf,neg_inf,neg_inf,neg_inf,neg_inf,neg_inf,neg_inf)) {}64__forceinline vint(StepTy) : v(_mm256_set_epi32(7, 6, 5, 4, 3, 2, 1, 0)) {}65__forceinline vint(ReverseStepTy) : v(_mm256_set_epi32(0, 1, 2, 3, 4, 5, 6, 7)) {}66__forceinline vint(UndefinedTy) : v(_mm256_undefined_si256()) {}6768////////////////////////////////////////////////////////////////////////////////69/// Loads and Stores70////////////////////////////////////////////////////////////////////////////////7172static __forceinline vint8 load (const void* a) { return _mm256_castps_si256(_mm256_load_ps((float*)a)); }73static __forceinline vint8 loadu(const void* a) { return _mm256_castps_si256(_mm256_loadu_ps((float*)a)); }7475static __forceinline vint8 load (const vboolf8& mask, const void* a) { return _mm256_castps_si256(_mm256_maskload_ps((float*)a,mask)); }76static __forceinline vint8 loadu(const vboolf8& mask, const void* a) { return _mm256_castps_si256(_mm256_maskload_ps((float*)a,mask)); }7778static __forceinline void store (void* ptr, const vint8& f) { _mm256_store_ps((float*)ptr,_mm256_castsi256_ps(f)); }79static __forceinline void storeu(void* ptr, const vint8& f) { _mm256_storeu_ps((float*)ptr,_mm256_castsi256_ps(f)); }8081static __forceinline void store (const vboolf8& mask, void* ptr, const vint8& f) { _mm256_maskstore_ps((float*)ptr,_mm256_castps_si256(mask.v),_mm256_castsi256_ps(f)); }82static __forceinline void storeu(const vboolf8& mask, void* ptr, const vint8& f) { _mm256_maskstore_ps((float*)ptr,_mm256_castps_si256(mask.v),_mm256_castsi256_ps(f)); }8384static __forceinline void store_nt(void* ptr, const vint8& v) {85_mm256_stream_ps((float*)ptr,_mm256_castsi256_ps(v));86}8788static __forceinline vint8 load(const unsigned char* ptr) {89vint4 il = vint4::load(ptr+0);90vint4 ih = vint4::load(ptr+4);91return vint8(il,ih);92}9394static __forceinline vint8 loadu(const unsigned char* ptr) {95vint4 il = vint4::loadu(ptr+0);96vint4 ih = vint4::loadu(ptr+4);97return vint8(il,ih);98}99100static __forceinline vint8 load(const unsigned short* ptr) {101vint4 il = vint4::load(ptr+0);102vint4 ih = vint4::load(ptr+4);103return vint8(il,ih);104}105106static __forceinline vint8 loadu(const unsigned short* ptr) {107vint4 il = vint4::loadu(ptr+0);108vint4 ih = vint4::loadu(ptr+4);109return vint8(il,ih);110}111112static __forceinline void store(unsigned char* ptr, const vint8& i) {113vint4 il(i.vl);114vint4 ih(i.vh);115vint4::store(ptr + 0,il);116vint4::store(ptr + 4,ih);117}118119static __forceinline void store(unsigned short* ptr, const vint8& v) {120for (size_t i=0;i<8;i++)121ptr[i] = (unsigned short)v[i];122}123124template<int scale = 4>125static __forceinline vint8 gather(const int* ptr, const vint8& index) {126return vint8(127*(int*)(((char*)ptr)+scale*index[0]),128*(int*)(((char*)ptr)+scale*index[1]),129*(int*)(((char*)ptr)+scale*index[2]),130*(int*)(((char*)ptr)+scale*index[3]),131*(int*)(((char*)ptr)+scale*index[4]),132*(int*)(((char*)ptr)+scale*index[5]),133*(int*)(((char*)ptr)+scale*index[6]),134*(int*)(((char*)ptr)+scale*index[7]));135}136137template<int scale = 4>138static __forceinline vint8 gather(const vboolf8& mask, const int* ptr, const vint8& index) {139vint8 r = zero;140if (likely(mask[0])) r[0] = *(int*)(((char*)ptr)+scale*index[0]);141if (likely(mask[1])) r[1] = *(int*)(((char*)ptr)+scale*index[1]);142if (likely(mask[2])) r[2] = *(int*)(((char*)ptr)+scale*index[2]);143if (likely(mask[3])) r[3] = *(int*)(((char*)ptr)+scale*index[3]);144if (likely(mask[4])) r[4] = *(int*)(((char*)ptr)+scale*index[4]);145if (likely(mask[5])) r[5] = *(int*)(((char*)ptr)+scale*index[5]);146if (likely(mask[6])) r[6] = *(int*)(((char*)ptr)+scale*index[6]);147if (likely(mask[7])) r[7] = *(int*)(((char*)ptr)+scale*index[7]);148return r;149}150151template<int scale = 4>152static __forceinline void scatter(void* ptr, const vint8& ofs, const vint8& v)153{154*(int*)(((char*)ptr)+scale*ofs[0]) = v[0];155*(int*)(((char*)ptr)+scale*ofs[1]) = v[1];156*(int*)(((char*)ptr)+scale*ofs[2]) = v[2];157*(int*)(((char*)ptr)+scale*ofs[3]) = v[3];158*(int*)(((char*)ptr)+scale*ofs[4]) = v[4];159*(int*)(((char*)ptr)+scale*ofs[5]) = v[5];160*(int*)(((char*)ptr)+scale*ofs[6]) = v[6];161*(int*)(((char*)ptr)+scale*ofs[7]) = v[7];162}163164template<int scale = 4>165static __forceinline void scatter(const vboolf8& mask, void* ptr, const vint8& ofs, const vint8& v)166{167if (likely(mask[0])) *(int*)(((char*)ptr)+scale*ofs[0]) = v[0];168if (likely(mask[1])) *(int*)(((char*)ptr)+scale*ofs[1]) = v[1];169if (likely(mask[2])) *(int*)(((char*)ptr)+scale*ofs[2]) = v[2];170if (likely(mask[3])) *(int*)(((char*)ptr)+scale*ofs[3]) = v[3];171if (likely(mask[4])) *(int*)(((char*)ptr)+scale*ofs[4]) = v[4];172if (likely(mask[5])) *(int*)(((char*)ptr)+scale*ofs[5]) = v[5];173if (likely(mask[6])) *(int*)(((char*)ptr)+scale*ofs[6]) = v[6];174if (likely(mask[7])) *(int*)(((char*)ptr)+scale*ofs[7]) = v[7];175}176177178static __forceinline vint8 broadcast64(const long long& a) { return _mm256_set1_epi64x(a); }179180////////////////////////////////////////////////////////////////////////////////181/// Array Access182////////////////////////////////////////////////////////////////////////////////183184__forceinline const int& operator [](size_t index) const { assert(index < 8); return i[index]; }185__forceinline int& operator [](size_t index) { assert(index < 8); return i[index]; }186};187188////////////////////////////////////////////////////////////////////////////////189/// Unary Operators190////////////////////////////////////////////////////////////////////////////////191192__forceinline vboolf8 asBool(const vint8& a) { return _mm256_castsi256_ps(a); }193194__forceinline vint8 operator +(const vint8& a) { return a; }195__forceinline vint8 operator -(const vint8& a) { return vint8(_mm_sub_epi32(_mm_setzero_si128(), a.vl), _mm_sub_epi32(_mm_setzero_si128(), a.vh)); }196__forceinline vint8 abs (const vint8& a) { return vint8(_mm_abs_epi32(a.vl), _mm_abs_epi32(a.vh)); }197198////////////////////////////////////////////////////////////////////////////////199/// Binary Operators200////////////////////////////////////////////////////////////////////////////////201202__forceinline vint8 operator +(const vint8& a, const vint8& b) { return vint8(_mm_add_epi32(a.vl, b.vl), _mm_add_epi32(a.vh, b.vh)); }203__forceinline vint8 operator +(const vint8& a, int b) { return a + vint8(b); }204__forceinline vint8 operator +(int a, const vint8& b) { return vint8(a) + b; }205206__forceinline vint8 operator -(const vint8& a, const vint8& b) { return vint8(_mm_sub_epi32(a.vl, b.vl), _mm_sub_epi32(a.vh, b.vh)); }207__forceinline vint8 operator -(const vint8& a, int b) { return a - vint8(b); }208__forceinline vint8 operator -(int a, const vint8& b) { return vint8(a) - b; }209210__forceinline vint8 operator *(const vint8& a, const vint8& b) { return vint8(_mm_mullo_epi32(a.vl, b.vl), _mm_mullo_epi32(a.vh, b.vh)); }211__forceinline vint8 operator *(const vint8& a, int b) { return a * vint8(b); }212__forceinline vint8 operator *(int a, const vint8& b) { return vint8(a) * b; }213214__forceinline vint8 operator &(const vint8& a, const vint8& b) { return _mm256_castps_si256(_mm256_and_ps(_mm256_castsi256_ps(a), _mm256_castsi256_ps(b))); }215__forceinline vint8 operator &(const vint8& a, int b) { return a & vint8(b); }216__forceinline vint8 operator &(int a, const vint8& b) { return vint8(a) & b; }217218__forceinline vint8 operator |(const vint8& a, const vint8& b) { return _mm256_castps_si256(_mm256_or_ps (_mm256_castsi256_ps(a), _mm256_castsi256_ps(b))); }219__forceinline vint8 operator |(const vint8& a, int b) { return a | vint8(b); }220__forceinline vint8 operator |(int a, const vint8& b) { return vint8(a) | b; }221222__forceinline vint8 operator ^(const vint8& a, const vint8& b) { return _mm256_castps_si256(_mm256_xor_ps(_mm256_castsi256_ps(a), _mm256_castsi256_ps(b))); }223__forceinline vint8 operator ^(const vint8& a, int b) { return a ^ vint8(b); }224__forceinline vint8 operator ^(int a, const vint8& b) { return vint8(a) ^ b; }225226__forceinline vint8 operator <<(const vint8& a, int n) { return vint8(_mm_slli_epi32(a.vl, n), _mm_slli_epi32(a.vh, n)); }227__forceinline vint8 operator >>(const vint8& a, int n) { return vint8(_mm_srai_epi32(a.vl, n), _mm_srai_epi32(a.vh, n)); }228229__forceinline vint8 sll (const vint8& a, int b) { return vint8(_mm_slli_epi32(a.vl, b), _mm_slli_epi32(a.vh, b)); }230__forceinline vint8 sra (const vint8& a, int b) { return vint8(_mm_srai_epi32(a.vl, b), _mm_srai_epi32(a.vh, b)); }231__forceinline vint8 srl (const vint8& a, int b) { return vint8(_mm_srli_epi32(a.vl, b), _mm_srli_epi32(a.vh, b)); }232233__forceinline vint8 min(const vint8& a, const vint8& b) { return vint8(_mm_min_epi32(a.vl, b.vl), _mm_min_epi32(a.vh, b.vh)); }234__forceinline vint8 min(const vint8& a, int b) { return min(a,vint8(b)); }235__forceinline vint8 min(int a, const vint8& b) { return min(vint8(a),b); }236237__forceinline vint8 max(const vint8& a, const vint8& b) { return vint8(_mm_max_epi32(a.vl, b.vl), _mm_max_epi32(a.vh, b.vh)); }238__forceinline vint8 max(const vint8& a, int b) { return max(a,vint8(b)); }239__forceinline vint8 max(int a, const vint8& b) { return max(vint8(a),b); }240241__forceinline vint8 umin(const vint8& a, const vint8& b) { return vint8(_mm_min_epu32(a.vl, b.vl), _mm_min_epu32(a.vh, b.vh)); }242__forceinline vint8 umin(const vint8& a, int b) { return umin(a,vint8(b)); }243__forceinline vint8 umin(int a, const vint8& b) { return umin(vint8(a),b); }244245__forceinline vint8 umax(const vint8& a, const vint8& b) { return vint8(_mm_max_epu32(a.vl, b.vl), _mm_max_epu32(a.vh, b.vh)); }246__forceinline vint8 umax(const vint8& a, int b) { return umax(a,vint8(b)); }247__forceinline vint8 umax(int a, const vint8& b) { return umax(vint8(a),b); }248249////////////////////////////////////////////////////////////////////////////////250/// Assignment Operators251////////////////////////////////////////////////////////////////////////////////252253__forceinline vint8& operator +=(vint8& a, const vint8& b) { return a = a + b; }254__forceinline vint8& operator +=(vint8& a, int b) { return a = a + b; }255256__forceinline vint8& operator -=(vint8& a, const vint8& b) { return a = a - b; }257__forceinline vint8& operator -=(vint8& a, int b) { return a = a - b; }258259__forceinline vint8& operator *=(vint8& a, const vint8& b) { return a = a * b; }260__forceinline vint8& operator *=(vint8& a, int b) { return a = a * b; }261262__forceinline vint8& operator &=(vint8& a, const vint8& b) { return a = a & b; }263__forceinline vint8& operator &=(vint8& a, int b) { return a = a & b; }264265__forceinline vint8& operator |=(vint8& a, const vint8& b) { return a = a | b; }266__forceinline vint8& operator |=(vint8& a, int b) { return a = a | b; }267268__forceinline vint8& operator <<=(vint8& a, int b) { return a = a << b; }269__forceinline vint8& operator >>=(vint8& a, int b) { return a = a >> b; }270271////////////////////////////////////////////////////////////////////////////////272/// Comparison Operators + Select273////////////////////////////////////////////////////////////////////////////////274275__forceinline vboolf8 operator ==(const vint8& a, const vint8& b) { return vboolf8(_mm_castsi128_ps(_mm_cmpeq_epi32 (a.vl, b.vl)),276_mm_castsi128_ps(_mm_cmpeq_epi32 (a.vh, b.vh))); }277__forceinline vboolf8 operator ==(const vint8& a, int b) { return a == vint8(b); }278__forceinline vboolf8 operator ==(int a, const vint8& b) { return vint8(a) == b; }279280__forceinline vboolf8 operator !=(const vint8& a, const vint8& b) { return !(a == b); }281__forceinline vboolf8 operator !=(const vint8& a, int b) { return a != vint8(b); }282__forceinline vboolf8 operator !=(int a, const vint8& b) { return vint8(a) != b; }283284__forceinline vboolf8 operator < (const vint8& a, const vint8& b) { return vboolf8(_mm_castsi128_ps(_mm_cmplt_epi32 (a.vl, b.vl)),285_mm_castsi128_ps(_mm_cmplt_epi32 (a.vh, b.vh))); }286__forceinline vboolf8 operator < (const vint8& a, int b) { return a < vint8(b); }287__forceinline vboolf8 operator < (int a, const vint8& b) { return vint8(a) < b; }288289__forceinline vboolf8 operator >=(const vint8& a, const vint8& b) { return !(a < b); }290__forceinline vboolf8 operator >=(const vint8& a, int b) { return a >= vint8(b); }291__forceinline vboolf8 operator >=(int a, const vint8& b) { return vint8(a) >= b; }292293__forceinline vboolf8 operator > (const vint8& a, const vint8& b) { return vboolf8(_mm_castsi128_ps(_mm_cmpgt_epi32 (a.vl, b.vl)),294_mm_castsi128_ps(_mm_cmpgt_epi32 (a.vh, b.vh))); }295__forceinline vboolf8 operator > (const vint8& a, int b) { return a > vint8(b); }296__forceinline vboolf8 operator > (int a, const vint8& b) { return vint8(a) > b; }297298__forceinline vboolf8 operator <=(const vint8& a, const vint8& b) { return !(a > b); }299__forceinline vboolf8 operator <=(const vint8& a, int b) { return a <= vint8(b); }300__forceinline vboolf8 operator <=(int a, const vint8& b) { return vint8(a) <= b; }301302__forceinline vboolf8 eq(const vint8& a, const vint8& b) { return a == b; }303__forceinline vboolf8 ne(const vint8& a, const vint8& b) { return a != b; }304__forceinline vboolf8 lt(const vint8& a, const vint8& b) { return a < b; }305__forceinline vboolf8 ge(const vint8& a, const vint8& b) { return a >= b; }306__forceinline vboolf8 gt(const vint8& a, const vint8& b) { return a > b; }307__forceinline vboolf8 le(const vint8& a, const vint8& b) { return a <= b; }308309__forceinline vboolf8 eq(const vboolf8& mask, const vint8& a, const vint8& b) { return mask & (a == b); }310__forceinline vboolf8 ne(const vboolf8& mask, const vint8& a, const vint8& b) { return mask & (a != b); }311__forceinline vboolf8 lt(const vboolf8& mask, const vint8& a, const vint8& b) { return mask & (a < b); }312__forceinline vboolf8 ge(const vboolf8& mask, const vint8& a, const vint8& b) { return mask & (a >= b); }313__forceinline vboolf8 gt(const vboolf8& mask, const vint8& a, const vint8& b) { return mask & (a > b); }314__forceinline vboolf8 le(const vboolf8& mask, const vint8& a, const vint8& b) { return mask & (a <= b); }315316__forceinline vint8 select(const vboolf8& m, const vint8& t, const vint8& f) {317return _mm256_castps_si256(_mm256_blendv_ps(_mm256_castsi256_ps(f), _mm256_castsi256_ps(t), m));318}319320////////////////////////////////////////////////////////////////////////////////321/// Movement/Shifting/Shuffling Functions322////////////////////////////////////////////////////////////////////////////////323324__forceinline vint8 unpacklo(const vint8& a, const vint8& b) { return _mm256_castps_si256(_mm256_unpacklo_ps(_mm256_castsi256_ps(a), _mm256_castsi256_ps(b))); }325__forceinline vint8 unpackhi(const vint8& a, const vint8& b) { return _mm256_castps_si256(_mm256_unpackhi_ps(_mm256_castsi256_ps(a), _mm256_castsi256_ps(b))); }326327template<int i>328__forceinline vint8 shuffle(const vint8& v) {329return _mm256_castps_si256(_mm256_permute_ps(_mm256_castsi256_ps(v), _MM_SHUFFLE(i, i, i, i)));330}331332template<int i0, int i1>333__forceinline vint8 shuffle4(const vint8& v) {334return _mm256_permute2f128_si256(v, v, (i1 << 4) | (i0 << 0));335}336337template<int i0, int i1>338__forceinline vint8 shuffle4(const vint8& a, const vint8& b) {339return _mm256_permute2f128_si256(a, b, (i1 << 4) | (i0 << 0));340}341342template<int i0, int i1, int i2, int i3>343__forceinline vint8 shuffle(const vint8& v) {344return _mm256_castps_si256(_mm256_permute_ps(_mm256_castsi256_ps(v), _MM_SHUFFLE(i3, i2, i1, i0)));345}346347template<int i0, int i1, int i2, int i3>348__forceinline vint8 shuffle(const vint8& a, const vint8& b) {349return _mm256_castps_si256(_mm256_shuffle_ps(_mm256_castsi256_ps(a), _mm256_castsi256_ps(b), _MM_SHUFFLE(i3, i2, i1, i0)));350}351352template<> __forceinline vint8 shuffle<0, 0, 2, 2>(const vint8& v) { return _mm256_castps_si256(_mm256_moveldup_ps(_mm256_castsi256_ps(v))); }353template<> __forceinline vint8 shuffle<1, 1, 3, 3>(const vint8& v) { return _mm256_castps_si256(_mm256_movehdup_ps(_mm256_castsi256_ps(v))); }354template<> __forceinline vint8 shuffle<0, 1, 0, 1>(const vint8& v) { return _mm256_castps_si256(_mm256_castpd_ps(_mm256_movedup_pd(_mm256_castps_pd(_mm256_castsi256_ps(v))))); }355356__forceinline vint8 broadcast(const int* ptr) { return _mm256_castps_si256(_mm256_broadcast_ss((const float*)ptr)); }357template<int i> __forceinline vint8 insert4(const vint8& a, const vint4& b) { return _mm256_insertf128_si256(a, b, i); }358template<int i> __forceinline vint4 extract4(const vint8& a) { return _mm256_extractf128_si256(a, i); }359template<> __forceinline vint4 extract4<0>(const vint8& a) { return _mm256_castsi256_si128(a); }360361__forceinline int toScalar(const vint8& v) { return _mm_cvtsi128_si32(_mm256_castsi256_si128(v)); }362363364////////////////////////////////////////////////////////////////////////////////365/// Reductions366////////////////////////////////////////////////////////////////////////////////367368__forceinline vint8 vreduce_min2(const vint8& v) { return min(v,shuffle<1,0,3,2>(v)); }369__forceinline vint8 vreduce_min4(const vint8& v) { vint8 v1 = vreduce_min2(v); return min(v1,shuffle<2,3,0,1>(v1)); }370__forceinline vint8 vreduce_min (const vint8& v) { vint8 v1 = vreduce_min4(v); return min(v1,shuffle4<1,0>(v1)); }371372__forceinline vint8 vreduce_max2(const vint8& v) { return max(v,shuffle<1,0,3,2>(v)); }373__forceinline vint8 vreduce_max4(const vint8& v) { vint8 v1 = vreduce_max2(v); return max(v1,shuffle<2,3,0,1>(v1)); }374__forceinline vint8 vreduce_max (const vint8& v) { vint8 v1 = vreduce_max4(v); return max(v1,shuffle4<1,0>(v1)); }375376__forceinline vint8 vreduce_add2(const vint8& v) { return v + shuffle<1,0,3,2>(v); }377__forceinline vint8 vreduce_add4(const vint8& v) { vint8 v1 = vreduce_add2(v); return v1 + shuffle<2,3,0,1>(v1); }378__forceinline vint8 vreduce_add (const vint8& v) { vint8 v1 = vreduce_add4(v); return v1 + shuffle4<1,0>(v1); }379380__forceinline int reduce_min(const vint8& v) { return toScalar(vreduce_min(v)); }381__forceinline int reduce_max(const vint8& v) { return toScalar(vreduce_max(v)); }382__forceinline int reduce_add(const vint8& v) { return toScalar(vreduce_add(v)); }383384__forceinline size_t select_min(const vint8& v) { return bsf(movemask(v == vreduce_min(v))); }385__forceinline size_t select_max(const vint8& v) { return bsf(movemask(v == vreduce_max(v))); }386387__forceinline size_t select_min(const vboolf8& valid, const vint8& v) { const vint8 a = select(valid,v,vint8(pos_inf)); return bsf(movemask(valid & (a == vreduce_min(a)))); }388__forceinline size_t select_max(const vboolf8& valid, const vint8& v) { const vint8 a = select(valid,v,vint8(neg_inf)); return bsf(movemask(valid & (a == vreduce_max(a)))); }389390////////////////////////////////////////////////////////////////////////////////391/// Sorting networks392////////////////////////////////////////////////////////////////////////////////393394__forceinline vint8 usort_ascending(const vint8& v)395{396const vint8 a0 = v;397const vint8 b0 = shuffle<1,0,3,2>(a0);398const vint8 c0 = umin(a0,b0);399const vint8 d0 = umax(a0,b0);400const vint8 a1 = select(0x99 /* 0b10011001 */,c0,d0);401const vint8 b1 = shuffle<2,3,0,1>(a1);402const vint8 c1 = umin(a1,b1);403const vint8 d1 = umax(a1,b1);404const vint8 a2 = select(0xc3 /* 0b11000011 */,c1,d1);405const vint8 b2 = shuffle<1,0,3,2>(a2);406const vint8 c2 = umin(a2,b2);407const vint8 d2 = umax(a2,b2);408const vint8 a3 = select(0xa5 /* 0b10100101 */,c2,d2);409const vint8 b3 = shuffle4<1,0>(a3);410const vint8 c3 = umin(a3,b3);411const vint8 d3 = umax(a3,b3);412const vint8 a4 = select(0xf /* 0b00001111 */,c3,d3);413const vint8 b4 = shuffle<2,3,0,1>(a4);414const vint8 c4 = umin(a4,b4);415const vint8 d4 = umax(a4,b4);416const vint8 a5 = select(0x33 /* 0b00110011 */,c4,d4);417const vint8 b5 = shuffle<1,0,3,2>(a5);418const vint8 c5 = umin(a5,b5);419const vint8 d5 = umax(a5,b5);420const vint8 a6 = select(0x55 /* 0b01010101 */,c5,d5);421return a6;422}423424__forceinline vint8 usort_descending(const vint8& v)425{426const vint8 a0 = v;427const vint8 b0 = shuffle<1,0,3,2>(a0);428const vint8 c0 = umax(a0,b0);429const vint8 d0 = umin(a0,b0);430const vint8 a1 = select(0x99 /* 0b10011001 */,c0,d0);431const vint8 b1 = shuffle<2,3,0,1>(a1);432const vint8 c1 = umax(a1,b1);433const vint8 d1 = umin(a1,b1);434const vint8 a2 = select(0xc3 /* 0b11000011 */,c1,d1);435const vint8 b2 = shuffle<1,0,3,2>(a2);436const vint8 c2 = umax(a2,b2);437const vint8 d2 = umin(a2,b2);438const vint8 a3 = select(0xa5 /* 0b10100101 */,c2,d2);439const vint8 b3 = shuffle4<1,0>(a3);440const vint8 c3 = umax(a3,b3);441const vint8 d3 = umin(a3,b3);442const vint8 a4 = select(0xf /* 0b00001111 */,c3,d3);443const vint8 b4 = shuffle<2,3,0,1>(a4);444const vint8 c4 = umax(a4,b4);445const vint8 d4 = umin(a4,b4);446const vint8 a5 = select(0x33 /* 0b00110011 */,c4,d4);447const vint8 b5 = shuffle<1,0,3,2>(a5);448const vint8 c5 = umax(a5,b5);449const vint8 d5 = umin(a5,b5);450const vint8 a6 = select(0x55 /* 0b01010101 */,c5,d5);451return a6;452}453454////////////////////////////////////////////////////////////////////////////////455/// Output Operators456////////////////////////////////////////////////////////////////////////////////457458__forceinline embree_ostream operator <<(embree_ostream cout, const vint8& a) {459return cout << "<" << a[0] << ", " << a[1] << ", " << a[2] << ", " << a[3] << ", " << a[4] << ", " << a[5] << ", " << a[6] << ", " << a[7] << ">";460}461}462463#undef vboolf464#undef vboold465#undef vint466#undef vuint467#undef vllong468#undef vfloat469#undef vdouble470471472