Path: blob/master/thirdparty/embree/common/simd/vuint8_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 vuint<8>18{19ALIGNED_STRUCT_(32);2021typedef vboolf8 Bool;22typedef vuint8 Int;23typedef vfloat8 Float;2425enum { size = 8 }; // number of SIMD elements26union { // data27__m256i v;28unsigned int i[8];29};3031////////////////////////////////////////////////////////////////////////////////32/// Constructors, Assignment & Cast Operators33////////////////////////////////////////////////////////////////////////////////3435__forceinline vuint() {}36__forceinline vuint(const vuint8& a) { v = a.v; }37__forceinline vuint8& operator =(const vuint8& a) { v = a.v; return *this; }3839__forceinline vuint(__m256i a) : v(a) {}40__forceinline operator const __m256i&() const { return v; }41__forceinline operator __m256i&() { return v; }4243__forceinline explicit vuint(const vuint4& a) : v(_mm256_insertf128_si256(_mm256_castsi128_si256(a),a,1)) {}44__forceinline vuint(const vuint4& a, const vuint4& b) : v(_mm256_insertf128_si256(_mm256_castsi128_si256(a),b,1)) {}45__forceinline vuint(const __m128i& a, const __m128i& b) : v(_mm256_insertf128_si256(_mm256_castsi128_si256(a),b,1)) {}4647__forceinline explicit vuint(const unsigned int* a) : v(_mm256_castps_si256(_mm256_loadu_ps((const float*)a))) {}48__forceinline vuint(unsigned int a) : v(_mm256_set1_epi32(a)) {}49__forceinline vuint(unsigned int a, unsigned int b) : v(_mm256_set_epi32(b, a, b, a, b, a, b, a)) {}50__forceinline vuint(unsigned int a, unsigned int b, unsigned int c, unsigned int d) : v(_mm256_set_epi32(d, c, b, a, d, c, b, a)) {}51__forceinline vuint(unsigned int a, unsigned int b, unsigned int c, unsigned int d, unsigned int e, unsigned int f, unsigned int g, unsigned int h) : v(_mm256_set_epi32(h, g, f, e, d, c, b, a)) {}5253__forceinline explicit vuint(__m256 a) : v(_mm256_cvtps_epi32(a)) {}5455#if defined(__AVX512VL__)56__forceinline explicit vuint(const vboolf8& a) : v(_mm256_movm_epi32(a)) {}57#else58__forceinline explicit vuint(const vboolf8& a) : v(_mm256_castps_si256((__m256)a)) {}59#endif6061////////////////////////////////////////////////////////////////////////////////62/// Constants63////////////////////////////////////////////////////////////////////////////////6465__forceinline vuint(ZeroTy) : v(_mm256_setzero_si256()) {}66__forceinline vuint(OneTy) : v(_mm256_set1_epi32(1)) {}67__forceinline vuint(PosInfTy) : v(_mm256_set1_epi32(pos_inf)) {}68__forceinline vuint(NegInfTy) : v(_mm256_set1_epi32(neg_inf)) {}69__forceinline vuint(StepTy) : v(_mm256_set_epi32(7, 6, 5, 4, 3, 2, 1, 0)) {}70__forceinline vuint(UndefinedTy) : v(_mm256_undefined_si256()) {}7172////////////////////////////////////////////////////////////////////////////////73/// Loads and Stores74////////////////////////////////////////////////////////////////////////////////7576static __forceinline vuint8 load(const unsigned char* ptr) { return _mm256_cvtepu8_epi32(_mm_loadl_epi64((__m128i*)ptr)); }77static __forceinline vuint8 loadu(const unsigned char* ptr) { return _mm256_cvtepu8_epi32(_mm_loadl_epi64((__m128i*)ptr)); }78static __forceinline vuint8 load(const unsigned short* ptr) { return _mm256_cvtepu16_epi32(_mm_load_si128((__m128i*)ptr)); }79static __forceinline vuint8 loadu(const unsigned short* ptr) { return _mm256_cvtepu16_epi32(_mm_loadu_si128((__m128i*)ptr)); }8081static __forceinline vuint8 load(const void* ptr) { return _mm256_load_si256((__m256i*)ptr); }82static __forceinline vuint8 loadu(const void* ptr) { return _mm256_loadu_si256((__m256i*)ptr); }8384static __forceinline void store (void* ptr, const vuint8& v) { _mm256_store_si256((__m256i*)ptr,v); }85static __forceinline void storeu(void* ptr, const vuint8& v) { _mm256_storeu_ps((float*)ptr,_mm256_castsi256_ps(v)); }8687#if defined(__AVX512VL__)8889static __forceinline vuint8 compact(const vboolf8& mask, vuint8 &v) {90return _mm256_mask_compress_epi32(v, mask, v);91}92static __forceinline vuint8 compact(const vboolf8& mask, vuint8 &a, const vuint8& b) {93return _mm256_mask_compress_epi32(a, mask, b);94}9596static __forceinline vuint8 load (const vboolf8& mask, const void* ptr) { return _mm256_mask_load_epi32 (_mm256_setzero_si256(),mask,ptr); }97static __forceinline vuint8 loadu(const vboolf8& mask, const void* ptr) { return _mm256_mask_loadu_epi32(_mm256_setzero_si256(),mask,ptr); }9899static __forceinline void store (const vboolf8& mask, void* ptr, const vuint8& v) { _mm256_mask_store_epi32 (ptr,mask,v); }100static __forceinline void storeu(const vboolf8& mask, void* ptr, const vuint8& v) { _mm256_mask_storeu_epi32(ptr,mask,v); }101#else102static __forceinline vuint8 load (const vboolf8& mask, const void* ptr) { return _mm256_castps_si256(_mm256_maskload_ps((float*)ptr,mask)); }103static __forceinline vuint8 loadu(const vboolf8& mask, const void* ptr) { return _mm256_castps_si256(_mm256_maskload_ps((float*)ptr,mask)); }104105static __forceinline void store (const vboolf8& mask, void* ptr, const vuint8& v) { _mm256_maskstore_epi32((int*)ptr,mask,v); }106static __forceinline void storeu(const vboolf8& mask, void* ptr, const vuint8& v) { _mm256_maskstore_epi32((int*)ptr,mask,v); }107#endif108109static __forceinline vuint8 load_nt(void* ptr) {110return _mm256_stream_load_si256((__m256i*)ptr);111}112113static __forceinline void store_nt(void* ptr, const vuint8& v) {114_mm256_stream_ps((float*)ptr,_mm256_castsi256_ps(v));115}116117static __forceinline void store(unsigned char* ptr, const vuint8& i)118{119for (size_t j=0; j<8; j++)120ptr[j] = i[j];121}122123static __forceinline void store(unsigned short* ptr, const vuint8& v) {124for (size_t i=0;i<8;i++)125ptr[i] = (unsigned short)v[i];126}127128template<int scale = 4>129static __forceinline vuint8 gather(const unsigned int *const ptr, const vint8& index) {130return _mm256_i32gather_epi32((const int*) ptr, index, scale);131}132133template<int scale = 4>134static __forceinline vuint8 gather(const vboolf8& mask, const unsigned int *const ptr, const vint8& index) {135vuint8 r = zero;136#if defined(__AVX512VL__)137return _mm256_mmask_i32gather_epi32(r, mask, index, (const int*) ptr, scale);138#else139return _mm256_mask_i32gather_epi32(r, (const int*) ptr, index, mask, scale);140#endif141}142143template<int scale = 4>144static __forceinline void scatter(void* ptr, const vint8& ofs, const vuint8& v)145{146#if defined(__AVX512VL__)147_mm256_i32scatter_epi32((int*)ptr, ofs, v, scale);148#else149*(unsigned int*)(((char*)ptr)+scale*ofs[0]) = v[0];150*(unsigned int*)(((char*)ptr)+scale*ofs[1]) = v[1];151*(unsigned int*)(((char*)ptr)+scale*ofs[2]) = v[2];152*(unsigned int*)(((char*)ptr)+scale*ofs[3]) = v[3];153*(unsigned int*)(((char*)ptr)+scale*ofs[4]) = v[4];154*(unsigned int*)(((char*)ptr)+scale*ofs[5]) = v[5];155*(unsigned int*)(((char*)ptr)+scale*ofs[6]) = v[6];156*(unsigned int*)(((char*)ptr)+scale*ofs[7]) = v[7];157#endif158}159160template<int scale = 4>161static __forceinline void scatter(const vboolf8& mask, void* ptr, const vint8& ofs, const vuint8& v)162{163#if defined(__AVX512VL__)164_mm256_mask_i32scatter_epi32((int*)ptr, mask, ofs, v, scale);165#else166if (likely(mask[0])) *(unsigned int*)(((char*)ptr)+scale*ofs[0]) = v[0];167if (likely(mask[1])) *(unsigned int*)(((char*)ptr)+scale*ofs[1]) = v[1];168if (likely(mask[2])) *(unsigned int*)(((char*)ptr)+scale*ofs[2]) = v[2];169if (likely(mask[3])) *(unsigned int*)(((char*)ptr)+scale*ofs[3]) = v[3];170if (likely(mask[4])) *(unsigned int*)(((char*)ptr)+scale*ofs[4]) = v[4];171if (likely(mask[5])) *(unsigned int*)(((char*)ptr)+scale*ofs[5]) = v[5];172if (likely(mask[6])) *(unsigned int*)(((char*)ptr)+scale*ofs[6]) = v[6];173if (likely(mask[7])) *(unsigned int*)(((char*)ptr)+scale*ofs[7]) = v[7];174#endif175}176177static __forceinline vuint8 broadcast64(const long long &a) { return _mm256_set1_epi64x(a); }178179////////////////////////////////////////////////////////////////////////////////180/// Array Access181////////////////////////////////////////////////////////////////////////////////182183__forceinline const unsigned int& operator [](size_t index) const { assert(index < 8); return i[index]; }184__forceinline unsigned int& operator [](size_t index) { assert(index < 8); return i[index]; }185};186187////////////////////////////////////////////////////////////////////////////////188/// Unary Operators189////////////////////////////////////////////////////////////////////////////////190191#if defined(__AVX512VL__)192__forceinline vboolf8 asBool(const vuint8& a) { return _mm256_movepi32_mask(a); }193#else194__forceinline vboolf8 asBool(const vuint8& a) { return _mm256_castsi256_ps(a); }195#endif196197__forceinline vuint8 operator +(const vuint8& a) { return a; }198199////////////////////////////////////////////////////////////////////////////////200/// Binary Operators201////////////////////////////////////////////////////////////////////////////////202203__forceinline vuint8 operator +(const vuint8& a, const vuint8& b) { return _mm256_add_epi32(a, b); }204__forceinline vuint8 operator +(const vuint8& a, unsigned int b) { return a + vuint8(b); }205__forceinline vuint8 operator +(unsigned int a, const vuint8& b) { return vuint8(a) + b; }206207__forceinline vuint8 operator -(const vuint8& a, const vuint8& b) { return _mm256_sub_epi32(a, b); }208__forceinline vuint8 operator -(const vuint8& a, unsigned int b) { return a - vuint8(b); }209__forceinline vuint8 operator -(unsigned int a, const vuint8& b) { return vuint8(a) - b; }210211//__forceinline vuint8 operator *(const vuint8& a, const vuint8& b) { return _mm256_mullo_epu32(a, b); }212//__forceinline vuint8 operator *(const vuint8& a, unsigned int b) { return a * vuint8(b); }213//__forceinline vuint8 operator *(unsigned int a, const vuint8& b) { return vuint8(a) * b; }214215__forceinline vuint8 operator &(const vuint8& a, const vuint8& b) { return _mm256_and_si256(a, b); }216__forceinline vuint8 operator &(const vuint8& a, unsigned int b) { return a & vuint8(b); }217__forceinline vuint8 operator &(unsigned int a, const vuint8& b) { return vuint8(a) & b; }218219__forceinline vuint8 operator |(const vuint8& a, const vuint8& b) { return _mm256_or_si256(a, b); }220__forceinline vuint8 operator |(const vuint8& a, unsigned int b) { return a | vuint8(b); }221__forceinline vuint8 operator |(unsigned int a, const vuint8& b) { return vuint8(a) | b; }222223__forceinline vuint8 operator ^(const vuint8& a, const vuint8& b) { return _mm256_xor_si256(a, b); }224__forceinline vuint8 operator ^(const vuint8& a, unsigned int b) { return a ^ vuint8(b); }225__forceinline vuint8 operator ^(unsigned int a, const vuint8& b) { return vuint8(a) ^ b; }226227__forceinline vuint8 operator <<(const vuint8& a, unsigned int n) { return _mm256_slli_epi32(a, n); }228__forceinline vuint8 operator >>(const vuint8& a, unsigned int n) { return _mm256_srli_epi32(a, n); }229230__forceinline vuint8 operator <<(const vuint8& a, const vuint8& n) { return _mm256_sllv_epi32(a, n); }231__forceinline vuint8 operator >>(const vuint8& a, const vuint8& n) { return _mm256_srlv_epi32(a, n); }232233__forceinline vuint8 sll(const vuint8& a, unsigned int b) { return _mm256_slli_epi32(a, b); }234__forceinline vuint8 sra(const vuint8& a, unsigned int b) { return _mm256_srai_epi32(a, b); }235__forceinline vuint8 srl(const vuint8& a, unsigned int b) { return _mm256_srli_epi32(a, b); }236237__forceinline vuint8 sll(const vuint8& a, const vuint8& b) { return _mm256_sllv_epi32(a, b); }238__forceinline vuint8 sra(const vuint8& a, const vuint8& b) { return _mm256_srav_epi32(a, b); }239__forceinline vuint8 srl(const vuint8& a, const vuint8& b) { return _mm256_srlv_epi32(a, b); }240241__forceinline vuint8 min(const vuint8& a, const vuint8& b) { return _mm256_min_epu32(a, b); }242__forceinline vuint8 min(const vuint8& a, unsigned int b) { return min(a,vuint8(b)); }243__forceinline vuint8 min(unsigned int a, const vuint8& b) { return min(vuint8(a),b); }244245__forceinline vuint8 max(const vuint8& a, const vuint8& b) { return _mm256_max_epu32(a, b); }246__forceinline vuint8 max(const vuint8& a, unsigned int b) { return max(a,vuint8(b)); }247__forceinline vuint8 max(unsigned int a, const vuint8& b) { return max(vuint8(a),b); }248249////////////////////////////////////////////////////////////////////////////////250/// Assignment Operators251////////////////////////////////////////////////////////////////////////////////252253__forceinline vuint8& operator +=(vuint8& a, const vuint8& b) { return a = a + b; }254__forceinline vuint8& operator +=(vuint8& a, unsigned int b) { return a = a + b; }255256__forceinline vuint8& operator -=(vuint8& a, const vuint8& b) { return a = a - b; }257__forceinline vuint8& operator -=(vuint8& a, unsigned int b) { return a = a - b; }258259//__forceinline vuint8& operator *=(vuint8& a, const vuint8& b) { return a = a * b; }260//__forceinline vuint8& operator *=(vuint8& a, unsigned int b) { return a = a * b; }261262__forceinline vuint8& operator &=(vuint8& a, const vuint8& b) { return a = a & b; }263__forceinline vuint8& operator &=(vuint8& a, unsigned int b) { return a = a & b; }264265__forceinline vuint8& operator |=(vuint8& a, const vuint8& b) { return a = a | b; }266__forceinline vuint8& operator |=(vuint8& a, unsigned int b) { return a = a | b; }267268__forceinline vuint8& operator <<=(vuint8& a, const unsigned int b) { return a = a << b; }269__forceinline vuint8& operator >>=(vuint8& a, const unsigned int b) { return a = a >> b; }270271////////////////////////////////////////////////////////////////////////////////272/// Comparison Operators + Select273////////////////////////////////////////////////////////////////////////////////274275#if defined(__AVX512VL__)276__forceinline vboolf8 operator ==(const vuint8& a, const vuint8& b) { return _mm256_cmp_epu32_mask(a,b,_MM_CMPINT_EQ); }277__forceinline vboolf8 operator !=(const vuint8& a, const vuint8& b) { return _mm256_cmp_epu32_mask(a,b,_MM_CMPINT_NE); }278__forceinline vboolf8 operator < (const vuint8& a, const vuint8& b) { return _mm256_cmp_epu32_mask(a,b,_MM_CMPINT_LT); }279__forceinline vboolf8 operator >=(const vuint8& a, const vuint8& b) { return _mm256_cmp_epu32_mask(a,b,_MM_CMPINT_GE); }280__forceinline vboolf8 operator > (const vuint8& a, const vuint8& b) { return _mm256_cmp_epu32_mask(a,b,_MM_CMPINT_GT); }281__forceinline vboolf8 operator <=(const vuint8& a, const vuint8& b) { return _mm256_cmp_epu32_mask(a,b,_MM_CMPINT_LE); }282283__forceinline vuint8 select(const vboolf8& m, const vuint8& t, const vuint8& f) {284return _mm256_mask_blend_epi32(m, (__m256i)f, (__m256i)t);285}286#else287__forceinline vboolf8 operator ==(const vuint8& a, const vuint8& b) { return _mm256_castsi256_ps(_mm256_cmpeq_epi32(a, b)); }288__forceinline vboolf8 operator !=(const vuint8& a, const vuint8& b) { return !(a == b); }289//__forceinline vboolf8 operator < (const vuint8& a, const vuint8& b) { return _mm256_castsi256_ps(_mm256_cmpgt_epu32(b, a)); }290//__forceinline vboolf8 operator >=(const vuint8& a, const vuint8& b) { return !(a < b); }291//__forceinline vboolf8 operator > (const vuint8& a, const vuint8& b) { return _mm256_castsi256_ps(_mm256_cmpgt_epu32(a, b)); }292//__forceinline vboolf8 operator <=(const vuint8& a, const vuint8& b) { return !(a > b); }293294__forceinline vuint8 select(const vboolf8& m, const vuint8& t, const vuint8& f) {295return _mm256_castps_si256(_mm256_blendv_ps(_mm256_castsi256_ps(f), _mm256_castsi256_ps(t), m));296}297#endif298299template<int mask>300__forceinline vuint8 select(const vuint8& t, const vuint8& f) {301return _mm256_blend_epi32(f, t, mask);302}303304__forceinline vboolf8 operator ==(const vuint8& a, unsigned int b) { return a == vuint8(b); }305__forceinline vboolf8 operator ==(unsigned int a, const vuint8& b) { return vuint8(a) == b; }306307__forceinline vboolf8 operator !=(const vuint8& a, unsigned int b) { return a != vuint8(b); }308__forceinline vboolf8 operator !=(unsigned int a, const vuint8& b) { return vuint8(a) != b; }309310//__forceinline vboolf8 operator < (const vuint8& a, unsigned int b) { return a < vuint8(b); }311//__forceinline vboolf8 operator < (unsigned int a, const vuint8& b) { return vuint8(a) < b; }312313//__forceinline vboolf8 operator >=(const vuint8& a, unsigned int b) { return a >= vuint8(b); }314//__forceinline vboolf8 operator >=(unsigned int a, const vuint8& b) { return vuint8(a) >= b; }315316//__forceinline vboolf8 operator > (const vuint8& a, unsigned int b) { return a > vuint8(b); }317//__forceinline vboolf8 operator > (unsigned int a, const vuint8& b) { return vuint8(a) > b; }318319//__forceinline vboolf8 operator <=(const vuint8& a, unsigned int b) { return a <= vuint8(b); }320//__forceinline vboolf8 operator <=(unsigned int a, const vuint8& b) { return vuint8(a) <= b; }321322__forceinline vboolf8 eq(const vuint8& a, const vuint8& b) { return a == b; }323__forceinline vboolf8 ne(const vuint8& a, const vuint8& b) { return a != b; }324//__forceinline vboolf8 lt(const vuint8& a, const vuint8& b) { return a < b; }325//__forceinline vboolf8 ge(const vuint8& a, const vuint8& b) { return a >= b; }326//__forceinline vboolf8 gt(const vuint8& a, const vuint8& b) { return a > b; }327//__forceinline vboolf8 le(const vuint8& a, const vuint8& b) { return a <= b; }328329#if defined(__AVX512VL__)330__forceinline vboolf8 eq(const vboolf8& mask, const vuint8& a, const vuint8& b) { return _mm256_mask_cmp_epu32_mask(mask, a, b, _MM_CMPINT_EQ); }331__forceinline vboolf8 ne(const vboolf8& mask, const vuint8& a, const vuint8& b) { return _mm256_mask_cmp_epu32_mask(mask, a, b, _MM_CMPINT_NE); }332__forceinline vboolf8 lt(const vboolf8& mask, const vuint8& a, const vuint8& b) { return _mm256_mask_cmp_epu32_mask(mask, a, b, _MM_CMPINT_LT); }333__forceinline vboolf8 ge(const vboolf8& mask, const vuint8& a, const vuint8& b) { return _mm256_mask_cmp_epu32_mask(mask, a, b, _MM_CMPINT_GE); }334__forceinline vboolf8 gt(const vboolf8& mask, const vuint8& a, const vuint8& b) { return _mm256_mask_cmp_epu32_mask(mask, a, b, _MM_CMPINT_GT); }335__forceinline vboolf8 le(const vboolf8& mask, const vuint8& a, const vuint8& b) { return _mm256_mask_cmp_epu32_mask(mask, a, b, _MM_CMPINT_LE); }336#else337__forceinline vboolf8 eq(const vboolf8& mask, const vuint8& a, const vuint8& b) { return mask & (a == b); }338__forceinline vboolf8 ne(const vboolf8& mask, const vuint8& a, const vuint8& b) { return mask & (a != b); }339//__forceinline vboolf8 lt(const vboolf8& mask, const vuint8& a, const vuint8& b) { return mask & (a < b); }340//__forceinline vboolf8 ge(const vboolf8& mask, const vuint8& a, const vuint8& b) { return mask & (a >= b); }341//__forceinline vboolf8 gt(const vboolf8& mask, const vuint8& a, const vuint8& b) { return mask & (a > b); }342//__forceinline vboolf8 le(const vboolf8& mask, const vuint8& a, const vuint8& b) { return mask & (a <= b); }343#endif344345////////////////////////////////////////////////////////////////////////////////346/// Movement/Shifting/Shuffling Functions347////////////////////////////////////////////////////////////////////////////////348349__forceinline vuint8 unpacklo(const vuint8& a, const vuint8& b) { return _mm256_unpacklo_epi32(a, b); }350__forceinline vuint8 unpackhi(const vuint8& a, const vuint8& b) { return _mm256_unpackhi_epi32(a, b); }351352template<int i>353__forceinline vuint8 shuffle(const vuint8& v) {354return _mm256_castps_si256(_mm256_permute_ps(_mm256_castsi256_ps(v), _MM_SHUFFLE(i, i, i, i)));355}356357template<int i0, int i1>358__forceinline vuint8 shuffle4(const vuint8& v) {359return _mm256_permute2f128_si256(v, v, (i1 << 4) | (i0 << 0));360}361362template<int i0, int i1>363__forceinline vuint8 shuffle4(const vuint8& a, const vuint8& b) {364return _mm256_permute2f128_si256(a, b, (i1 << 4) | (i0 << 0));365}366367template<int i0, int i1, int i2, int i3>368__forceinline vuint8 shuffle(const vuint8& v) {369return _mm256_castps_si256(_mm256_permute_ps(_mm256_castsi256_ps(v), _MM_SHUFFLE(i3, i2, i1, i0)));370}371372template<int i0, int i1, int i2, int i3>373__forceinline vuint8 shuffle(const vuint8& a, const vuint8& b) {374return _mm256_castps_si256(_mm256_shuffle_ps(_mm256_castsi256_ps(a), _mm256_castsi256_ps(b), _MM_SHUFFLE(i3, i2, i1, i0)));375}376377template<> __forceinline vuint8 shuffle<0, 0, 2, 2>(const vuint8& v) { return _mm256_castps_si256(_mm256_moveldup_ps(_mm256_castsi256_ps(v))); }378template<> __forceinline vuint8 shuffle<1, 1, 3, 3>(const vuint8& v) { return _mm256_castps_si256(_mm256_movehdup_ps(_mm256_castsi256_ps(v))); }379template<> __forceinline vuint8 shuffle<0, 1, 0, 1>(const vuint8& v) { return _mm256_castps_si256(_mm256_castpd_ps(_mm256_movedup_pd(_mm256_castps_pd(_mm256_castsi256_ps(v))))); }380381template<int i> __forceinline vuint8 insert4(const vuint8& a, const vuint4& b) { return _mm256_insertf128_si256(a, b, i); }382template<int i> __forceinline vuint4 extract4(const vuint8& a) { return _mm256_extractf128_si256(a, i); }383template<> __forceinline vuint4 extract4<0>(const vuint8& a) { return _mm256_castsi256_si128(a); }384385__forceinline int toScalar(const vuint8& v) { return _mm_cvtsi128_si32(_mm256_castsi256_si128(v)); }386387#if !defined(__aarch64__)388__forceinline vuint8 permute(const vuint8& v, const __m256i& index) {389return _mm256_permutevar8x32_epi32(v, index);390}391392__forceinline vuint8 shuffle(const vuint8& v, const __m256i& index) {393return _mm256_castps_si256(_mm256_permutevar_ps(_mm256_castsi256_ps(v), index));394}395396template<int i>397__forceinline vuint8 align_shift_right(const vuint8& a, const vuint8& b) {398#if defined(__AVX512VL__)399return _mm256_alignr_epi32(a, b, i);400#else401return _mm256_alignr_epi8(a, b, 4*i);402#endif403}404#endif // !defined(__aarch64__)405406////////////////////////////////////////////////////////////////////////////////407/// Reductions408////////////////////////////////////////////////////////////////////////////////409410//__forceinline vuint8 vreduce_min2(const vuint8& v) { return min(v,shuffle<1,0,3,2>(v)); }411//__forceinline vuint8 vreduce_min4(const vuint8& v) { vuint8 v1 = vreduce_min2(v); return min(v1,shuffle<2,3,0,1>(v1)); }412//__forceinline vuint8 vreduce_min (const vuint8& v) { vuint8 v1 = vreduce_min4(v); return min(v1,shuffle4<1,0>(v1)); }413414//__forceinline vuint8 vreduce_max2(const vuint8& v) { return max(v,shuffle<1,0,3,2>(v)); }415//__forceinline vuint8 vreduce_max4(const vuint8& v) { vuint8 v1 = vreduce_max2(v); return max(v1,shuffle<2,3,0,1>(v1)); }416//__forceinline vuint8 vreduce_max (const vuint8& v) { vuint8 v1 = vreduce_max4(v); return max(v1,shuffle4<1,0>(v1)); }417418__forceinline vuint8 vreduce_add2(const vuint8& v) { return v + shuffle<1,0,3,2>(v); }419__forceinline vuint8 vreduce_add4(const vuint8& v) { vuint8 v1 = vreduce_add2(v); return v1 + shuffle<2,3,0,1>(v1); }420__forceinline vuint8 vreduce_add (const vuint8& v) { vuint8 v1 = vreduce_add4(v); return v1 + shuffle4<1,0>(v1); }421422//__forceinline int reduce_min(const vuint8& v) { return toScalar(vreduce_min(v)); }423//__forceinline int reduce_max(const vuint8& v) { return toScalar(vreduce_max(v)); }424__forceinline int reduce_add(const vuint8& v) { return toScalar(vreduce_add(v)); }425426//__forceinline size_t select_min(const vuint8& v) { return bsf(movemask(v == vreduce_min(v))); }427//__forceinline size_t select_max(const vuint8& v) { return bsf(movemask(v == vreduce_max(v))); }428429//__forceinline size_t select_min(const vboolf8& valid, const vuint8& v) { const vuint8 a = select(valid,v,vuint8(pos_inf)); return bsf(movemask(valid & (a == vreduce_min(a)))); }430//__forceinline size_t select_max(const vboolf8& valid, const vuint8& v) { const vuint8 a = select(valid,v,vuint8(neg_inf)); return bsf(movemask(valid & (a == vreduce_max(a)))); }431432////////////////////////////////////////////////////////////////////////////////433/// Output Operators434////////////////////////////////////////////////////////////////////////////////435436__forceinline embree_ostream operator <<(embree_ostream cout, const vuint8& a) {437return cout << "<" << a[0] << ", " << a[1] << ", " << a[2] << ", " << a[3] << ", " << a[4] << ", " << a[5] << ", " << a[6] << ", " << a[7] << ">";438}439}440441#undef vboolf442#undef vboold443#undef vint444#undef vuint445#undef vllong446#undef vfloat447#undef vdouble448449450