Path: blob/master/thirdparty/embree/common/simd/vint8_avx2.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;28int i[8];29};3031////////////////////////////////////////////////////////////////////////////////32/// Constructors, Assignment & Cast Operators33////////////////////////////////////////////////////////////////////////////////3435__forceinline vint() {}36__forceinline vint(const vint8& a) { v = a.v; }37__forceinline vint8& operator =(const vint8& a) { v = a.v; return *this; }3839__forceinline vint(__m256i a) : v(a) {}40__forceinline operator const __m256i&() const { return v; }41__forceinline operator __m256i&() { return v; }4243__forceinline explicit vint(const vint4& a) : v(_mm256_insertf128_si256(_mm256_castsi128_si256(a),a,1)) {}44__forceinline vint(const vint4& a, const vint4& b) : v(_mm256_insertf128_si256(_mm256_castsi128_si256(a),b,1)) {}45__forceinline vint(const __m128i& a, const __m128i& b) : v(_mm256_insertf128_si256(_mm256_castsi128_si256(a),b,1)) {}4647__forceinline explicit vint(const int* a) : v(_mm256_castps_si256(_mm256_loadu_ps((const float*)a))) {}48__forceinline vint(int a) : v(_mm256_set1_epi32(a)) {}49__forceinline vint(int a, int b) : v(_mm256_set_epi32(b, a, b, a, b, a, b, a)) {}50__forceinline vint(int a, int b, int c, int d) : v(_mm256_set_epi32(d, c, b, a, d, c, b, a)) {}51__forceinline vint(int a, int b, int c, int d, int e, int f, int g, int h) : v(_mm256_set_epi32(h, g, f, e, d, c, b, a)) {}5253__forceinline explicit vint(__m256 a) : v(_mm256_cvtps_epi32(a)) {}5455#if defined(__AVX512VL__)56__forceinline explicit vint(const vboolf8& a) : v(_mm256_movm_epi32(a)) {}57#else58__forceinline explicit vint(const vboolf8& a) : v(_mm256_castps_si256((__m256)a)) {}59#endif6061////////////////////////////////////////////////////////////////////////////////62/// Constants63////////////////////////////////////////////////////////////////////////////////6465__forceinline vint(ZeroTy) : v(_mm256_setzero_si256()) {}66__forceinline vint(OneTy) : v(_mm256_set1_epi32(1)) {}67__forceinline vint(PosInfTy) : v(_mm256_set1_epi32(pos_inf)) {}68__forceinline vint(NegInfTy) : v(_mm256_set1_epi32(neg_inf)) {}69__forceinline vint(StepTy) : v(_mm256_set_epi32(7, 6, 5, 4, 3, 2, 1, 0)) {}70__forceinline vint(ReverseStepTy) : v(_mm256_set_epi32(0, 1, 2, 3, 4, 5, 6, 7)) {}71__forceinline vint(UndefinedTy) : v(_mm256_undefined_si256()) {}7273////////////////////////////////////////////////////////////////////////////////74/// Loads and Stores75////////////////////////////////////////////////////////////////////////////////7677static __forceinline vint8 load(const unsigned char* ptr) { return _mm256_cvtepu8_epi32(_mm_loadl_epi64((__m128i*)ptr)); }78static __forceinline vint8 loadu(const unsigned char* ptr) { return _mm256_cvtepu8_epi32(_mm_loadl_epi64((__m128i*)ptr)); }79static __forceinline vint8 load(const unsigned short* ptr) { return _mm256_cvtepu16_epi32(_mm_load_si128((__m128i*)ptr)); }80static __forceinline vint8 loadu(const unsigned short* ptr) { return _mm256_cvtepu16_epi32(_mm_loadu_si128((__m128i*)ptr)); }8182static __forceinline vint8 load(const void* ptr) { return _mm256_load_si256((__m256i*)ptr); }83static __forceinline vint8 loadu(const void* ptr) { return _mm256_loadu_si256((__m256i*)ptr); }8485static __forceinline void store (void* ptr, const vint8& v) { _mm256_store_si256((__m256i*)ptr,v); }86static __forceinline void storeu(void* ptr, const vint8& v) { _mm256_storeu_ps((float*)ptr,_mm256_castsi256_ps(v)); }8788#if defined(__AVX512VL__)8990static __forceinline vint8 compact(const vboolf8& mask, vint8 &v) {91return _mm256_mask_compress_epi32(v, mask, v);92}93static __forceinline vint8 compact(const vboolf8& mask, vint8 &a, const vint8& b) {94return _mm256_mask_compress_epi32(a, mask, b);95}9697static __forceinline vint8 load (const vboolf8& mask, const void* ptr) { return _mm256_mask_load_epi32 (_mm256_setzero_si256(),mask,ptr); }98static __forceinline vint8 loadu(const vboolf8& mask, const void* ptr) { return _mm256_mask_loadu_epi32(_mm256_setzero_si256(),mask,ptr); }99100static __forceinline void store (const vboolf8& mask, void* ptr, const vint8& v) { _mm256_mask_store_epi32 (ptr,mask,v); }101static __forceinline void storeu(const vboolf8& mask, void* ptr, const vint8& v) { _mm256_mask_storeu_epi32(ptr,mask,v); }102#else103static __forceinline vint8 load (const vboolf8& mask, const void* ptr) { return _mm256_castps_si256(_mm256_maskload_ps((float*)ptr,mask)); }104static __forceinline vint8 loadu(const vboolf8& mask, const void* ptr) { return _mm256_castps_si256(_mm256_maskload_ps((float*)ptr,mask)); }105106static __forceinline void store (const vboolf8& mask, void* ptr, const vint8& v) { _mm256_maskstore_epi32((int*)ptr,mask,v); }107static __forceinline void storeu(const vboolf8& mask, void* ptr, const vint8& v) { _mm256_maskstore_epi32((int*)ptr,mask,v); }108#endif109110static __forceinline vint8 load_nt(void* ptr) {111return _mm256_stream_load_si256((__m256i*)ptr);112}113114static __forceinline void store_nt(void* ptr, const vint8& v) {115_mm256_stream_ps((float*)ptr,_mm256_castsi256_ps(v));116}117118static __forceinline void store(unsigned char* ptr, const vint8& i)119{120for (size_t j=0; j<8; j++)121ptr[j] = i[j];122}123124static __forceinline void store(unsigned short* ptr, const vint8& v) {125for (size_t i=0;i<8;i++)126ptr[i] = (unsigned short)v[i];127}128129template<int scale = 4>130static __forceinline vint8 gather(const int *const ptr, const vint8& index) {131return _mm256_i32gather_epi32(ptr, index, scale);132}133134template<int scale = 4>135static __forceinline vint8 gather(const vboolf8& mask, const int *const ptr, const vint8& index) {136vint8 r = zero;137#if defined(__AVX512VL__)138return _mm256_mmask_i32gather_epi32(r, mask, index, ptr, scale);139#else140return _mm256_mask_i32gather_epi32(r, ptr, index, mask, scale);141#endif142}143144template<int scale = 4>145static __forceinline void scatter(void* ptr, const vint8& ofs, const vint8& v)146{147#if defined(__AVX512VL__)148_mm256_i32scatter_epi32((int*)ptr, ofs, v, scale);149#else150*(int*)(((char*)ptr)+scale*ofs[0]) = v[0];151*(int*)(((char*)ptr)+scale*ofs[1]) = v[1];152*(int*)(((char*)ptr)+scale*ofs[2]) = v[2];153*(int*)(((char*)ptr)+scale*ofs[3]) = v[3];154*(int*)(((char*)ptr)+scale*ofs[4]) = v[4];155*(int*)(((char*)ptr)+scale*ofs[5]) = v[5];156*(int*)(((char*)ptr)+scale*ofs[6]) = v[6];157*(int*)(((char*)ptr)+scale*ofs[7]) = v[7];158#endif159}160161template<int scale = 4>162static __forceinline void scatter(const vboolf8& mask, void* ptr, const vint8& ofs, const vint8& v)163{164#if defined(__AVX512VL__)165_mm256_mask_i32scatter_epi32((int*)ptr, mask, ofs, v, scale);166#else167if (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#endif176}177178static __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#if defined(__AVX512VL__)193static __forceinline vboolf8 asBool(const vint8& a) { return _mm256_movepi32_mask(a); }194#else195static __forceinline vboolf8 asBool(const vint8& a) { return _mm256_castsi256_ps(a); }196#endif197198__forceinline vint8 operator +(const vint8& a) { return a; }199__forceinline vint8 operator -(const vint8& a) { return _mm256_sub_epi32(_mm256_setzero_si256(), a); }200__forceinline vint8 abs (const vint8& a) { return _mm256_abs_epi32(a); }201202////////////////////////////////////////////////////////////////////////////////203/// Binary Operators204////////////////////////////////////////////////////////////////////////////////205206__forceinline vint8 operator +(const vint8& a, const vint8& b) { return _mm256_add_epi32(a, b); }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 _mm256_sub_epi32(a, b); }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_mullo_epi32(a, 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_and_si256(a, 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_or_si256(a, 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, const vint8& b) { return _mm256_xor_si256(a, b); }227__forceinline vint8 operator ^(const vint8& a, int b) { return a ^ vint8(b); }228__forceinline vint8 operator ^(int a, const vint8& b) { return vint8(a) ^ b; }229230__forceinline vint8 operator <<(const vint8& a, int n) { return _mm256_slli_epi32(a, n); }231__forceinline vint8 operator >>(const vint8& a, int n) { return _mm256_srai_epi32(a, n); }232233__forceinline vint8 operator <<(const vint8& a, const vint8& n) { return _mm256_sllv_epi32(a, n); }234__forceinline vint8 operator >>(const vint8& a, const vint8& n) { return _mm256_srav_epi32(a, n); }235236__forceinline vint8 sll(const vint8& a, int b) { return _mm256_slli_epi32(a, b); }237__forceinline vint8 sra(const vint8& a, int b) { return _mm256_srai_epi32(a, b); }238__forceinline vint8 srl(const vint8& a, int b) { return _mm256_srli_epi32(a, b); }239240__forceinline vint8 sll(const vint8& a, const vint8& b) { return _mm256_sllv_epi32(a, b); }241__forceinline vint8 sra(const vint8& a, const vint8& b) { return _mm256_srav_epi32(a, b); }242__forceinline vint8 srl(const vint8& a, const vint8& b) { return _mm256_srlv_epi32(a, b); }243244__forceinline vint8 min(const vint8& a, const vint8& b) { return _mm256_min_epi32(a, b); }245__forceinline vint8 min(const vint8& a, int b) { return min(a,vint8(b)); }246__forceinline vint8 min(int a, const vint8& b) { return min(vint8(a),b); }247248__forceinline vint8 max(const vint8& a, const vint8& b) { return _mm256_max_epi32(a, b); }249__forceinline vint8 max(const vint8& a, int b) { return max(a,vint8(b)); }250__forceinline vint8 max(int a, const vint8& b) { return max(vint8(a),b); }251252__forceinline vint8 umin(const vint8& a, const vint8& b) { return _mm256_min_epu32(a, b); }253__forceinline vint8 umax(const vint8& a, const vint8& b) { return _mm256_max_epu32(a, b); }254255////////////////////////////////////////////////////////////////////////////////256/// Assignment Operators257////////////////////////////////////////////////////////////////////////////////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, const vint8& b) { return a = a & b; }269__forceinline vint8& operator &=(vint8& a, int b) { return a = a & b; }270271__forceinline vint8& operator |=(vint8& a, const vint8& b) { return a = a | b; }272__forceinline vint8& operator |=(vint8& a, int b) { return a = a | b; }273274__forceinline vint8& operator <<=(vint8& a, const int b) { return a = a << b; }275__forceinline vint8& operator >>=(vint8& a, const int b) { return a = a >> b; }276277////////////////////////////////////////////////////////////////////////////////278/// Comparison Operators + Select279////////////////////////////////////////////////////////////////////////////////280281#if defined(__AVX512VL__)282static __forceinline vboolf8 operator ==(const vint8& a, const vint8& b) { return _mm256_cmp_epi32_mask(a,b,_MM_CMPINT_EQ); }283static __forceinline vboolf8 operator !=(const vint8& a, const vint8& b) { return _mm256_cmp_epi32_mask(a,b,_MM_CMPINT_NE); }284static __forceinline vboolf8 operator < (const vint8& a, const vint8& b) { return _mm256_cmp_epi32_mask(a,b,_MM_CMPINT_LT); }285static __forceinline vboolf8 operator >=(const vint8& a, const vint8& b) { return _mm256_cmp_epi32_mask(a,b,_MM_CMPINT_GE); }286static __forceinline vboolf8 operator > (const vint8& a, const vint8& b) { return _mm256_cmp_epi32_mask(a,b,_MM_CMPINT_GT); }287static __forceinline vboolf8 operator <=(const vint8& a, const vint8& b) { return _mm256_cmp_epi32_mask(a,b,_MM_CMPINT_LE); }288289static __forceinline vint8 select(const vboolf8& m, const vint8& t, const vint8& f) {290return _mm256_mask_blend_epi32(m, (__m256i)f, (__m256i)t);291}292#else293static __forceinline vboolf8 operator ==(const vint8& a, const vint8& b) { return _mm256_castsi256_ps(_mm256_cmpeq_epi32(a, b)); }294static __forceinline vboolf8 operator !=(const vint8& a, const vint8& b) { return !(a == b); }295static __forceinline vboolf8 operator < (const vint8& a, const vint8& b) { return _mm256_castsi256_ps(_mm256_cmpgt_epi32(b, a)); }296static __forceinline vboolf8 operator >=(const vint8& a, const vint8& b) { return !(a < b); }297static __forceinline vboolf8 operator > (const vint8& a, const vint8& b) { return _mm256_castsi256_ps(_mm256_cmpgt_epi32(a, b)); }298static __forceinline vboolf8 operator <=(const vint8& a, const vint8& b) { return !(a > b); }299300static __forceinline vint8 select(const vboolf8& m, const vint8& t, const vint8& f) {301return _mm256_castps_si256(_mm256_blendv_ps(_mm256_castsi256_ps(f), _mm256_castsi256_ps(t), m));302}303#endif304305template<int mask>306__forceinline vint8 select(const vint8& t, const vint8& f) {307return _mm256_blend_epi32(f, t, mask);308}309310__forceinline vboolf8 operator ==(const vint8& a, int b) { return a == vint8(b); }311__forceinline vboolf8 operator ==(int a, const vint8& b) { return vint8(a) == b; }312313__forceinline vboolf8 operator !=(const vint8& a, int b) { return a != vint8(b); }314__forceinline vboolf8 operator !=(int a, const vint8& b) { return vint8(a) != b; }315316__forceinline vboolf8 operator < (const vint8& a, int b) { return a < vint8(b); }317__forceinline vboolf8 operator < (int a, const vint8& b) { return vint8(a) < b; }318319__forceinline vboolf8 operator >=(const vint8& a, int b) { return a >= vint8(b); }320__forceinline vboolf8 operator >=(int a, const vint8& b) { return vint8(a) >= b; }321322__forceinline vboolf8 operator > (const vint8& a, int b) { return a > vint8(b); }323__forceinline vboolf8 operator > (int a, const vint8& b) { return vint8(a) > b; }324325__forceinline vboolf8 operator <=(const vint8& a, int b) { return a <= vint8(b); }326__forceinline vboolf8 operator <=(int a, const vint8& b) { return vint8(a) <= b; }327328__forceinline vboolf8 eq(const vint8& a, const vint8& b) { return a == b; }329__forceinline vboolf8 ne(const vint8& a, const vint8& b) { return a != b; }330__forceinline vboolf8 lt(const vint8& a, const vint8& b) { return a < b; }331__forceinline vboolf8 ge(const vint8& a, const vint8& b) { return a >= b; }332__forceinline vboolf8 gt(const vint8& a, const vint8& b) { return a > b; }333__forceinline vboolf8 le(const vint8& a, const vint8& b) { return a <= b; }334335#if defined(__AVX512VL__)336static __forceinline vboolf8 eq(const vboolf8& mask, const vint8& a, const vint8& b) { return _mm256_mask_cmp_epi32_mask(mask, a, b, _MM_CMPINT_EQ); }337static __forceinline vboolf8 ne(const vboolf8& mask, const vint8& a, const vint8& b) { return _mm256_mask_cmp_epi32_mask(mask, a, b, _MM_CMPINT_NE); }338static __forceinline vboolf8 lt(const vboolf8& mask, const vint8& a, const vint8& b) { return _mm256_mask_cmp_epi32_mask(mask, a, b, _MM_CMPINT_LT); }339static __forceinline vboolf8 ge(const vboolf8& mask, const vint8& a, const vint8& b) { return _mm256_mask_cmp_epi32_mask(mask, a, b, _MM_CMPINT_GE); }340static __forceinline vboolf8 gt(const vboolf8& mask, const vint8& a, const vint8& b) { return _mm256_mask_cmp_epi32_mask(mask, a, b, _MM_CMPINT_GT); }341static __forceinline vboolf8 le(const vboolf8& mask, const vint8& a, const vint8& b) { return _mm256_mask_cmp_epi32_mask(mask, a, b, _MM_CMPINT_LE); }342#else343static __forceinline vboolf8 eq(const vboolf8& mask, const vint8& a, const vint8& b) { return mask & (a == b); }344static __forceinline vboolf8 ne(const vboolf8& mask, const vint8& a, const vint8& b) { return mask & (a != b); }345static __forceinline vboolf8 lt(const vboolf8& mask, const vint8& a, const vint8& b) { return mask & (a < b); }346static __forceinline vboolf8 ge(const vboolf8& mask, const vint8& a, const vint8& b) { return mask & (a >= b); }347static __forceinline vboolf8 gt(const vboolf8& mask, const vint8& a, const vint8& b) { return mask & (a > b); }348static __forceinline vboolf8 le(const vboolf8& mask, const vint8& a, const vint8& b) { return mask & (a <= b); }349#endif350351////////////////////////////////////////////////////////////////////////////////352/// Movement/Shifting/Shuffling Functions353////////////////////////////////////////////////////////////////////////////////354355__forceinline vint8 unpacklo(const vint8& a, const vint8& b) { return _mm256_unpacklo_epi32(a, b); }356__forceinline vint8 unpackhi(const vint8& a, const vint8& b) { return _mm256_unpackhi_epi32(a, b); }357358template<int i>359__forceinline vint8 shuffle(const vint8& v) {360return _mm256_castps_si256(_mm256_permute_ps(_mm256_castsi256_ps(v), _MM_SHUFFLE(i, i, i, i)));361}362363template<int i0, int i1>364__forceinline vint8 shuffle4(const vint8& v) {365return _mm256_permute2f128_si256(v, v, (i1 << 4) | (i0 << 0));366}367368template<int i0, int i1>369__forceinline vint8 shuffle4(const vint8& a, const vint8& b) {370return _mm256_permute2f128_si256(a, b, (i1 << 4) | (i0 << 0));371}372373template<int i0, int i1, int i2, int i3>374__forceinline vint8 shuffle(const vint8& v) {375return _mm256_castps_si256(_mm256_permute_ps(_mm256_castsi256_ps(v), _MM_SHUFFLE(i3, i2, i1, i0)));376}377378template<int i0, int i1, int i2, int i3>379__forceinline vint8 shuffle(const vint8& a, const vint8& b) {380return _mm256_castps_si256(_mm256_shuffle_ps(_mm256_castsi256_ps(a), _mm256_castsi256_ps(b), _MM_SHUFFLE(i3, i2, i1, i0)));381}382383template<> __forceinline vint8 shuffle<0, 0, 2, 2>(const vint8& v) { return _mm256_castps_si256(_mm256_moveldup_ps(_mm256_castsi256_ps(v))); }384template<> __forceinline vint8 shuffle<1, 1, 3, 3>(const vint8& v) { return _mm256_castps_si256(_mm256_movehdup_ps(_mm256_castsi256_ps(v))); }385template<> __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))))); }386387__forceinline vint8 broadcast(const int* ptr) { return _mm256_castps_si256(_mm256_broadcast_ss((const float*)ptr)); }388389template<int i> __forceinline vint8 insert4(const vint8& a, const vint4& b) { return _mm256_insertf128_si256(a, b, i); }390template<int i> __forceinline vint4 extract4(const vint8& a) { return _mm256_extractf128_si256(a, i); }391template<> __forceinline vint4 extract4<0>(const vint8& a) { return _mm256_castsi256_si128(a); }392393__forceinline int toScalar(const vint8& v) { return _mm_cvtsi128_si32(_mm256_castsi256_si128(v)); }394395#if !defined(__aarch64__)396__forceinline vint8 permute(const vint8& v, const __m256i& index) {397return _mm256_permutevar8x32_epi32(v, index);398}399400__forceinline vint8 shuffle(const vint8& v, const __m256i& index) {401return _mm256_castps_si256(_mm256_permutevar_ps(_mm256_castsi256_ps(v), index));402}403404template<int i>405static __forceinline vint8 align_shift_right(const vint8& a, const vint8& b) {406#if defined(__AVX512VL__)407return _mm256_alignr_epi32(a, b, i);408#else409return _mm256_alignr_epi8(a, b, 4*i);410#endif411}412413#endif414415416////////////////////////////////////////////////////////////////////////////////417/// Reductions418////////////////////////////////////////////////////////////////////////////////419420__forceinline vint8 vreduce_min2(const vint8& v) { return min(v,shuffle<1,0,3,2>(v)); }421__forceinline vint8 vreduce_min4(const vint8& v) { vint8 v1 = vreduce_min2(v); return min(v1,shuffle<2,3,0,1>(v1)); }422__forceinline vint8 vreduce_min (const vint8& v) { vint8 v1 = vreduce_min4(v); return min(v1,shuffle4<1,0>(v1)); }423424__forceinline vint8 vreduce_max2(const vint8& v) { return max(v,shuffle<1,0,3,2>(v)); }425__forceinline vint8 vreduce_max4(const vint8& v) { vint8 v1 = vreduce_max2(v); return max(v1,shuffle<2,3,0,1>(v1)); }426__forceinline vint8 vreduce_max (const vint8& v) { vint8 v1 = vreduce_max4(v); return max(v1,shuffle4<1,0>(v1)); }427428__forceinline vint8 vreduce_add2(const vint8& v) { return v + shuffle<1,0,3,2>(v); }429__forceinline vint8 vreduce_add4(const vint8& v) { vint8 v1 = vreduce_add2(v); return v1 + shuffle<2,3,0,1>(v1); }430__forceinline vint8 vreduce_add (const vint8& v) { vint8 v1 = vreduce_add4(v); return v1 + shuffle4<1,0>(v1); }431432__forceinline int reduce_min(const vint8& v) { return toScalar(vreduce_min(v)); }433__forceinline int reduce_max(const vint8& v) { return toScalar(vreduce_max(v)); }434__forceinline int reduce_add(const vint8& v) { return toScalar(vreduce_add(v)); }435436__forceinline size_t select_min(const vint8& v) { return bsf(movemask(v == vreduce_min(v))); }437__forceinline size_t select_max(const vint8& v) { return bsf(movemask(v == vreduce_max(v))); }438439__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)))); }440__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)))); }441442////////////////////////////////////////////////////////////////////////////////443/// Sorting networks444////////////////////////////////////////////////////////////////////////////////445446__forceinline vint8 usort_ascending(const vint8& v)447{448const vint8 a0 = v;449const vint8 b0 = shuffle<1,0,3,2>(a0);450const vint8 c0 = umin(a0,b0);451const vint8 d0 = umax(a0,b0);452const vint8 a1 = select<0x99 /* 0b10011001 */>(c0,d0);453const vint8 b1 = shuffle<2,3,0,1>(a1);454const vint8 c1 = umin(a1,b1);455const vint8 d1 = umax(a1,b1);456const vint8 a2 = select<0xc3 /* 0b11000011 */>(c1,d1);457const vint8 b2 = shuffle<1,0,3,2>(a2);458const vint8 c2 = umin(a2,b2);459const vint8 d2 = umax(a2,b2);460const vint8 a3 = select<0xa5 /* 0b10100101 */>(c2,d2);461const vint8 b3 = shuffle4<1,0>(a3);462const vint8 c3 = umin(a3,b3);463const vint8 d3 = umax(a3,b3);464const vint8 a4 = select<0xf /* 0b00001111 */>(c3,d3);465const vint8 b4 = shuffle<2,3,0,1>(a4);466const vint8 c4 = umin(a4,b4);467const vint8 d4 = umax(a4,b4);468const vint8 a5 = select<0x33 /* 0b00110011 */>(c4,d4);469const vint8 b5 = shuffle<1,0,3,2>(a5);470const vint8 c5 = umin(a5,b5);471const vint8 d5 = umax(a5,b5);472const vint8 a6 = select<0x55 /* 0b01010101 */>(c5,d5);473return a6;474}475476__forceinline vint8 usort_descending(const vint8& v)477{478const vint8 a0 = v;479const vint8 b0 = shuffle<1,0,3,2>(a0);480const vint8 c0 = umax(a0,b0);481const vint8 d0 = umin(a0,b0);482const vint8 a1 = select<0x99 /* 0b10011001 */>(c0,d0);483const vint8 b1 = shuffle<2,3,0,1>(a1);484const vint8 c1 = umax(a1,b1);485const vint8 d1 = umin(a1,b1);486const vint8 a2 = select<0xc3 /* 0b11000011 */>(c1,d1);487const vint8 b2 = shuffle<1,0,3,2>(a2);488const vint8 c2 = umax(a2,b2);489const vint8 d2 = umin(a2,b2);490const vint8 a3 = select<0xa5 /* 0b10100101 */>(c2,d2);491const vint8 b3 = shuffle4<1,0>(a3);492const vint8 c3 = umax(a3,b3);493const vint8 d3 = umin(a3,b3);494const vint8 a4 = select<0xf /* 0b00001111 */>(c3,d3);495const vint8 b4 = shuffle<2,3,0,1>(a4);496const vint8 c4 = umax(a4,b4);497const vint8 d4 = umin(a4,b4);498const vint8 a5 = select<0x33 /* 0b00110011 */>(c4,d4);499const vint8 b5 = shuffle<1,0,3,2>(a5);500const vint8 c5 = umax(a5,b5);501const vint8 d5 = umin(a5,b5);502const vint8 a6 = select<0x55 /* 0b01010101 */>(c5,d5);503return a6;504}505506////////////////////////////////////////////////////////////////////////////////507/// Output Operators508////////////////////////////////////////////////////////////////////////////////509510__forceinline embree_ostream operator <<(embree_ostream cout, const vint8& a) {511return cout << "<" << a[0] << ", " << a[1] << ", " << a[2] << ", " << a[3] << ", " << a[4] << ", " << a[5] << ", " << a[6] << ", " << a[7] << ">";512}513}514515#undef vboolf516#undef vboold517#undef vint518#undef vuint519#undef vllong520#undef vfloat521#undef vdouble522523524