Path: blob/master/thirdparty/embree/common/simd/vint4_sse2.h
9912 views
// Copyright 2009-2021 Intel Corporation1// SPDX-License-Identifier: Apache-2.023#pragma once45#include "../math/emath.h"67#define vboolf vboolf_impl8#define vboold vboold_impl9#define vint vint_impl10#define vuint vuint_impl11#define vllong vllong_impl12#define vfloat vfloat_impl13#define vdouble vdouble_impl1415namespace embree16{17/* 4-wide SSE integer type */18template<>19struct vint<4>20{21ALIGNED_STRUCT_(16);2223typedef vboolf4 Bool;24typedef vint4 Int;25typedef vfloat4 Float;2627enum { size = 4 }; // number of SIMD elements28union { __m128i v; int i[4]; }; // data2930////////////////////////////////////////////////////////////////////////////////31/// Constructors, Assignment & Cast Operators32////////////////////////////////////////////////////////////////////////////////3334__forceinline vint() {}35__forceinline vint(const vint4& a) { v = a.v; }36__forceinline vint4& operator =(const vint4& a) { v = a.v; return *this; }3738__forceinline vint(__m128i a) : v(a) {}39__forceinline operator const __m128i&() const { return v; }40__forceinline operator __m128i&() { return v; }4142__forceinline vint(int a) : v(_mm_set1_epi32(a)) {}43__forceinline vint(int a, int b, int c, int d) : v(_mm_set_epi32(d, c, b, a)) {}4445__forceinline explicit vint(__m128 a) : v(_mm_cvtps_epi32(a)) {}46#if defined(__AVX512VL__)47__forceinline explicit vint(const vboolf4& a) : v(_mm_movm_epi32(a)) {}48#else49__forceinline explicit vint(const vboolf4& a) : v(_mm_castps_si128((__m128)a)) {}50#endif5152__forceinline vint(long long a, long long b) : v(_mm_set_epi64x(b,a)) {}5354////////////////////////////////////////////////////////////////////////////////55/// Constants56////////////////////////////////////////////////////////////////////////////////5758__forceinline vint(ZeroTy) : v(_mm_setzero_si128()) {}59__forceinline vint(OneTy) : v(_mm_set_epi32(1, 1, 1, 1)) {}60__forceinline vint(PosInfTy) : v(_mm_set_epi32(pos_inf, pos_inf, pos_inf, pos_inf)) {}61__forceinline vint(NegInfTy) : v(_mm_set_epi32(neg_inf, neg_inf, neg_inf, neg_inf)) {}62__forceinline vint(StepTy) : v(_mm_set_epi32(3, 2, 1, 0)) {}63__forceinline vint(ReverseStepTy) : v(_mm_set_epi32(0, 1, 2, 3)) {}6465__forceinline vint(TrueTy) { v = _mm_cmpeq_epi32(v,v); }66__forceinline vint(UndefinedTy) : v(_mm_castps_si128(_mm_undefined_ps())) {}676869////////////////////////////////////////////////////////////////////////////////70/// Loads and Stores71////////////////////////////////////////////////////////////////////////////////7273static __forceinline vint4 load (const void* a) { return _mm_load_si128((__m128i*)a); }74static __forceinline vint4 loadu(const void* a) { return _mm_loadu_si128((__m128i*)a); }7576static __forceinline void store (void* ptr, const vint4& v) { _mm_store_si128((__m128i*)ptr,v); }77static __forceinline void storeu(void* ptr, const vint4& v) { _mm_storeu_si128((__m128i*)ptr,v); }7879#if defined(__AVX512VL__)8081static __forceinline vint4 compact(const vboolf4& mask, vint4 &v) {82return _mm_mask_compress_epi32(v, mask, v);83}84static __forceinline vint4 compact(const vboolf4& mask, vint4 &a, const vint4& b) {85return _mm_mask_compress_epi32(a, mask, b);86}8788static __forceinline vint4 load (const vboolf4& mask, const void* ptr) { return _mm_mask_load_epi32 (_mm_setzero_si128(),mask,ptr); }89static __forceinline vint4 loadu(const vboolf4& mask, const void* ptr) { return _mm_mask_loadu_epi32(_mm_setzero_si128(),mask,ptr); }9091static __forceinline void store (const vboolf4& mask, void* ptr, const vint4& v) { _mm_mask_store_epi32 (ptr,mask,v); }92static __forceinline void storeu(const vboolf4& mask, void* ptr, const vint4& v) { _mm_mask_storeu_epi32(ptr,mask,v); }93#elif defined(__AVX__)94static __forceinline vint4 load (const vbool4& mask, const void* a) { return _mm_castps_si128(_mm_maskload_ps((float*)a,mask)); }95static __forceinline vint4 loadu(const vbool4& mask, const void* a) { return _mm_castps_si128(_mm_maskload_ps((float*)a,mask)); }9697static __forceinline void store (const vboolf4& mask, void* ptr, const vint4& i) { _mm_maskstore_ps((float*)ptr,(__m128i)mask,_mm_castsi128_ps(i)); }98static __forceinline void storeu(const vboolf4& mask, void* ptr, const vint4& i) { _mm_maskstore_ps((float*)ptr,(__m128i)mask,_mm_castsi128_ps(i)); }99#else100static __forceinline vint4 load (const vbool4& mask, const void* a) { return _mm_and_si128(_mm_load_si128 ((__m128i*)a),mask); }101static __forceinline vint4 loadu(const vbool4& mask, const void* a) { return _mm_and_si128(_mm_loadu_si128((__m128i*)a),mask); }102103static __forceinline void store (const vboolf4& mask, void* ptr, const vint4& i) { store (ptr,select(mask,i,load (ptr))); }104static __forceinline void storeu(const vboolf4& mask, void* ptr, const vint4& i) { storeu(ptr,select(mask,i,loadu(ptr))); }105#endif106107108#if defined(__aarch64__)109static __forceinline vint4 load(const unsigned char* ptr) {110return _mm_load4epu8_epi32(((__m128i*)ptr));111}112static __forceinline vint4 loadu(const unsigned char* ptr) {113return _mm_load4epu8_epi32(((__m128i*)ptr));114}115#elif defined(__SSE4_1__)116static __forceinline vint4 load(const unsigned char* ptr) {117return _mm_cvtepu8_epi32(_mm_loadl_epi64((__m128i*)ptr));118}119120static __forceinline vint4 loadu(const unsigned char* ptr) {121return _mm_cvtepu8_epi32(_mm_loadl_epi64((__m128i*)ptr));122}123#else124125static __forceinline vint4 load(const unsigned char* ptr) {126return vint4(ptr[0],ptr[1],ptr[2],ptr[3]);127}128129static __forceinline vint4 loadu(const unsigned char* ptr) {130return vint4(ptr[0],ptr[1],ptr[2],ptr[3]);131}132133#endif134135static __forceinline vint4 load(const unsigned short* ptr) {136#if defined(__aarch64__)137return __m128i(vmovl_u16(vld1_u16(ptr)));138#elif defined (__SSE4_1__)139return _mm_cvtepu16_epi32(_mm_loadu_si128((__m128i*)ptr));140#else141return vint4(ptr[0],ptr[1],ptr[2],ptr[3]);142#endif143}144145static __forceinline void store(unsigned char* ptr, const vint4& v) {146#if defined(__aarch64__)147int32x4_t x = v;148uint16x4_t y = vqmovn_u32(uint32x4_t(x));149uint8x8_t z = vqmovn_u16(vcombine_u16(y, y));150vst1_lane_u32((uint32_t *)ptr,uint32x2_t(z), 0);151#elif defined(__SSE4_1__)152__m128i x = v;153x = _mm_packus_epi32(x, x);154x = _mm_packus_epi16(x, x);155*(int*)ptr = _mm_cvtsi128_si32(x);156#else157for (size_t i=0;i<4;i++)158ptr[i] = (unsigned char)v[i];159#endif160}161162static __forceinline void store(unsigned short* ptr, const vint4& v) {163#if defined(__aarch64__)164uint32x4_t x = uint32x4_t(v.v);165uint16x4_t y = vqmovn_u32(x);166vst1_u16(ptr, y);167#else168for (size_t i=0;i<4;i++)169ptr[i] = (unsigned short)v[i];170#endif171}172173static __forceinline vint4 load_nt(void* ptr) {174#if defined(__aarch64__) || defined(__SSE4_1__)175return _mm_stream_load_si128((__m128i*)ptr);176#else177return _mm_load_si128((__m128i*)ptr);178#endif179}180181static __forceinline void store_nt(void* ptr, const vint4& v) {182#if !defined(__aarch64__) && defined(__SSE4_1__)183_mm_stream_ps((float*)ptr, _mm_castsi128_ps(v));184#else185_mm_store_si128((__m128i*)ptr,v);186#endif187}188189template<int scale = 4>190static __forceinline vint4 gather(const int* ptr, const vint4& index) {191#if defined(__AVX2__) && !defined(__aarch64__)192return _mm_i32gather_epi32(ptr, index, scale);193#else194return vint4(195*(int*)(((char*)ptr)+scale*index[0]),196*(int*)(((char*)ptr)+scale*index[1]),197*(int*)(((char*)ptr)+scale*index[2]),198*(int*)(((char*)ptr)+scale*index[3]));199#endif200}201202template<int scale = 4>203static __forceinline vint4 gather(const vboolf4& mask, const int* ptr, const vint4& index) {204vint4 r = zero;205#if defined(__AVX512VL__)206return _mm_mmask_i32gather_epi32(r, mask, index, ptr, scale);207#elif defined(__AVX2__) && !defined(__aarch64__)208return _mm_mask_i32gather_epi32(r, ptr, index, mask, scale);209#else210if (likely(mask[0])) r[0] = *(int*)(((char*)ptr)+scale*index[0]);211if (likely(mask[1])) r[1] = *(int*)(((char*)ptr)+scale*index[1]);212if (likely(mask[2])) r[2] = *(int*)(((char*)ptr)+scale*index[2]);213if (likely(mask[3])) r[3] = *(int*)(((char*)ptr)+scale*index[3]);214return r;215#endif216}217218template<int scale = 4>219static __forceinline void scatter(void* ptr, const vint4& index, const vint4& v)220{221#if defined(__AVX512VL__)222_mm_i32scatter_epi32((int*)ptr, index, v, scale);223#else224*(int*)(((char*)ptr)+scale*index[0]) = v[0];225*(int*)(((char*)ptr)+scale*index[1]) = v[1];226*(int*)(((char*)ptr)+scale*index[2]) = v[2];227*(int*)(((char*)ptr)+scale*index[3]) = v[3];228#endif229}230231template<int scale = 4>232static __forceinline void scatter(const vboolf4& mask, void* ptr, const vint4& index, const vint4& v)233{234#if defined(__AVX512VL__)235_mm_mask_i32scatter_epi32((int*)ptr, mask, index, v, scale);236#else237if (likely(mask[0])) *(int*)(((char*)ptr)+scale*index[0]) = v[0];238if (likely(mask[1])) *(int*)(((char*)ptr)+scale*index[1]) = v[1];239if (likely(mask[2])) *(int*)(((char*)ptr)+scale*index[2]) = v[2];240if (likely(mask[3])) *(int*)(((char*)ptr)+scale*index[3]) = v[3];241#endif242}243244#if defined(__x86_64__) || defined(__aarch64__)245static __forceinline vint4 broadcast64(long long a) { return _mm_set1_epi64x(a); }246#endif247248////////////////////////////////////////////////////////////////////////////////249/// Array Access250////////////////////////////////////////////////////////////////////////////////251252__forceinline const int& operator [](size_t index) const { assert(index < 4); return i[index]; }253__forceinline int& operator [](size_t index) { assert(index < 4); return i[index]; }254255friend __forceinline vint4 select(const vboolf4& m, const vint4& t, const vint4& f) {256#if defined(__AVX512VL__)257return _mm_mask_blend_epi32(m, (__m128i)f, (__m128i)t);258#elif defined(__aarch64__)259return _mm_castps_si128(_mm_blendv_ps((__m128)f.v,(__m128) t.v, (__m128)m.v));260#elif defined(__SSE4_1__)261return _mm_castps_si128(_mm_blendv_ps(_mm_castsi128_ps(f), _mm_castsi128_ps(t), m));262#else263return _mm_or_si128(_mm_and_si128(m, t), _mm_andnot_si128(m, f));264#endif265}266};267268////////////////////////////////////////////////////////////////////////////////269/// Unary Operators270////////////////////////////////////////////////////////////////////////////////271272#if defined(__AVX512VL__)273__forceinline vboolf4 asBool(const vint4& a) { return _mm_movepi32_mask(a); }274#else275__forceinline vboolf4 asBool(const vint4& a) { return _mm_castsi128_ps(a); }276#endif277278__forceinline vint4 operator +(const vint4& a) { return a; }279__forceinline vint4 operator -(const vint4& a) { return _mm_sub_epi32(_mm_setzero_si128(), a); }280#if defined(__aarch64__)281__forceinline vint4 abs(const vint4& a) { return vabsq_s32(a.v); }282#elif defined(__SSSE3__)283__forceinline vint4 abs(const vint4& a) { return _mm_abs_epi32(a); }284#endif285286////////////////////////////////////////////////////////////////////////////////287/// Binary Operators288////////////////////////////////////////////////////////////////////////////////289290__forceinline vint4 operator +(const vint4& a, const vint4& b) { return _mm_add_epi32(a, b); }291__forceinline vint4 operator +(const vint4& a, int b) { return a + vint4(b); }292__forceinline vint4 operator +(int a, const vint4& b) { return vint4(a) + b; }293294__forceinline vint4 operator -(const vint4& a, const vint4& b) { return _mm_sub_epi32(a, b); }295__forceinline vint4 operator -(const vint4& a, int b) { return a - vint4(b); }296__forceinline vint4 operator -(int a, const vint4& b) { return vint4(a) - b; }297298#if (defined(__aarch64__)) || defined(__SSE4_1__)299__forceinline vint4 operator *(const vint4& a, const vint4& b) { return _mm_mullo_epi32(a, b); }300#else301__forceinline vint4 operator *(const vint4& a, const vint4& b) { return vint4(a[0]*b[0],a[1]*b[1],a[2]*b[2],a[3]*b[3]); }302#endif303__forceinline vint4 operator *(const vint4& a, int b) { return a * vint4(b); }304__forceinline vint4 operator *(int a, const vint4& b) { return vint4(a) * b; }305306__forceinline vint4 operator &(const vint4& a, const vint4& b) { return _mm_and_si128(a, b); }307__forceinline vint4 operator &(const vint4& a, int b) { return a & vint4(b); }308__forceinline vint4 operator &(int a, const vint4& b) { return vint4(a) & b; }309310__forceinline vint4 operator |(const vint4& a, const vint4& b) { return _mm_or_si128(a, b); }311__forceinline vint4 operator |(const vint4& a, int b) { return a | vint4(b); }312__forceinline vint4 operator |(int a, const vint4& b) { return vint4(a) | b; }313314__forceinline vint4 operator ^(const vint4& a, const vint4& b) { return _mm_xor_si128(a, b); }315__forceinline vint4 operator ^(const vint4& a, int b) { return a ^ vint4(b); }316__forceinline vint4 operator ^(int a, const vint4& b) { return vint4(a) ^ b; }317318__forceinline vint4 operator <<(const vint4& a, const int n) { return _mm_slli_epi32(a, n); }319__forceinline vint4 operator >>(const vint4& a, const int n) { return _mm_srai_epi32(a, n); }320321__forceinline vint4 sll (const vint4& a, int b) { return _mm_slli_epi32(a, b); }322__forceinline vint4 sra (const vint4& a, int b) { return _mm_srai_epi32(a, b); }323__forceinline vint4 srl (const vint4& a, int b) { return _mm_srli_epi32(a, b); }324325////////////////////////////////////////////////////////////////////////////////326/// Assignment Operators327////////////////////////////////////////////////////////////////////////////////328329__forceinline vint4& operator +=(vint4& a, const vint4& b) { return a = a + b; }330__forceinline vint4& operator +=(vint4& a, int b) { return a = a + b; }331332__forceinline vint4& operator -=(vint4& a, const vint4& b) { return a = a - b; }333__forceinline vint4& operator -=(vint4& a, int b) { return a = a - b; }334335#if (defined(__aarch64__)) || defined(__SSE4_1__)336__forceinline vint4& operator *=(vint4& a, const vint4& b) { return a = a * b; }337__forceinline vint4& operator *=(vint4& a, int b) { return a = a * b; }338#endif339340__forceinline vint4& operator &=(vint4& a, const vint4& b) { return a = a & b; }341__forceinline vint4& operator &=(vint4& a, int b) { return a = a & b; }342343__forceinline vint4& operator |=(vint4& a, const vint4& b) { return a = a | b; }344__forceinline vint4& operator |=(vint4& a, int b) { return a = a | b; }345346__forceinline vint4& operator <<=(vint4& a, int b) { return a = a << b; }347__forceinline vint4& operator >>=(vint4& a, int b) { return a = a >> b; }348349////////////////////////////////////////////////////////////////////////////////350/// Comparison Operators + Select351////////////////////////////////////////////////////////////////////////////////352353#if defined(__AVX512VL__)354__forceinline vboolf4 operator ==(const vint4& a, const vint4& b) { return _mm_cmp_epi32_mask(a,b,_MM_CMPINT_EQ); }355__forceinline vboolf4 operator !=(const vint4& a, const vint4& b) { return _mm_cmp_epi32_mask(a,b,_MM_CMPINT_NE); }356__forceinline vboolf4 operator < (const vint4& a, const vint4& b) { return _mm_cmp_epi32_mask(a,b,_MM_CMPINT_LT); }357__forceinline vboolf4 operator >=(const vint4& a, const vint4& b) { return _mm_cmp_epi32_mask(a,b,_MM_CMPINT_GE); }358__forceinline vboolf4 operator > (const vint4& a, const vint4& b) { return _mm_cmp_epi32_mask(a,b,_MM_CMPINT_GT); }359__forceinline vboolf4 operator <=(const vint4& a, const vint4& b) { return _mm_cmp_epi32_mask(a,b,_MM_CMPINT_LE); }360#else361__forceinline vboolf4 operator ==(const vint4& a, const vint4& b) { return _mm_castsi128_ps(_mm_cmpeq_epi32(a, b)); }362__forceinline vboolf4 operator !=(const vint4& a, const vint4& b) { return !(a == b); }363__forceinline vboolf4 operator < (const vint4& a, const vint4& b) { return _mm_castsi128_ps(_mm_cmplt_epi32(a, b)); }364__forceinline vboolf4 operator >=(const vint4& a, const vint4& b) { return !(a < b); }365__forceinline vboolf4 operator > (const vint4& a, const vint4& b) { return _mm_castsi128_ps(_mm_cmpgt_epi32(a, b)); }366__forceinline vboolf4 operator <=(const vint4& a, const vint4& b) { return !(a > b); }367#endif368369__forceinline vboolf4 operator ==(const vint4& a, int b) { return a == vint4(b); }370__forceinline vboolf4 operator ==(int a, const vint4& b) { return vint4(a) == b; }371372__forceinline vboolf4 operator !=(const vint4& a, int b) { return a != vint4(b); }373__forceinline vboolf4 operator !=(int a, const vint4& b) { return vint4(a) != b; }374375__forceinline vboolf4 operator < (const vint4& a, int b) { return a < vint4(b); }376__forceinline vboolf4 operator < (int a, const vint4& b) { return vint4(a) < b; }377378__forceinline vboolf4 operator >=(const vint4& a, int b) { return a >= vint4(b); }379__forceinline vboolf4 operator >=(int a, const vint4& b) { return vint4(a) >= b; }380381__forceinline vboolf4 operator > (const vint4& a, int b) { return a > vint4(b); }382__forceinline vboolf4 operator > (int a, const vint4& b) { return vint4(a) > b; }383384__forceinline vboolf4 operator <=(const vint4& a, int b) { return a <= vint4(b); }385__forceinline vboolf4 operator <=(int a, const vint4& b) { return vint4(a) <= b; }386387__forceinline vboolf4 eq(const vint4& a, const vint4& b) { return a == b; }388__forceinline vboolf4 ne(const vint4& a, const vint4& b) { return a != b; }389__forceinline vboolf4 lt(const vint4& a, const vint4& b) { return a < b; }390__forceinline vboolf4 ge(const vint4& a, const vint4& b) { return a >= b; }391__forceinline vboolf4 gt(const vint4& a, const vint4& b) { return a > b; }392__forceinline vboolf4 le(const vint4& a, const vint4& b) { return a <= b; }393394#if defined(__AVX512VL__)395__forceinline vboolf4 eq(const vboolf4& mask, const vint4& a, const vint4& b) { return _mm_mask_cmp_epi32_mask(mask, a, b, _MM_CMPINT_EQ); }396__forceinline vboolf4 ne(const vboolf4& mask, const vint4& a, const vint4& b) { return _mm_mask_cmp_epi32_mask(mask, a, b, _MM_CMPINT_NE); }397__forceinline vboolf4 lt(const vboolf4& mask, const vint4& a, const vint4& b) { return _mm_mask_cmp_epi32_mask(mask, a, b, _MM_CMPINT_LT); }398__forceinline vboolf4 ge(const vboolf4& mask, const vint4& a, const vint4& b) { return _mm_mask_cmp_epi32_mask(mask, a, b, _MM_CMPINT_GE); }399__forceinline vboolf4 gt(const vboolf4& mask, const vint4& a, const vint4& b) { return _mm_mask_cmp_epi32_mask(mask, a, b, _MM_CMPINT_GT); }400__forceinline vboolf4 le(const vboolf4& mask, const vint4& a, const vint4& b) { return _mm_mask_cmp_epi32_mask(mask, a, b, _MM_CMPINT_LE); }401#else402__forceinline vboolf4 eq(const vboolf4& mask, const vint4& a, const vint4& b) { return mask & (a == b); }403__forceinline vboolf4 ne(const vboolf4& mask, const vint4& a, const vint4& b) { return mask & (a != b); }404__forceinline vboolf4 lt(const vboolf4& mask, const vint4& a, const vint4& b) { return mask & (a < b); }405__forceinline vboolf4 ge(const vboolf4& mask, const vint4& a, const vint4& b) { return mask & (a >= b); }406__forceinline vboolf4 gt(const vboolf4& mask, const vint4& a, const vint4& b) { return mask & (a > b); }407__forceinline vboolf4 le(const vboolf4& mask, const vint4& a, const vint4& b) { return mask & (a <= b); }408#endif409410template<int mask>411__forceinline vint4 select(const vint4& t, const vint4& f) {412#if defined(__SSE4_1__)413return _mm_castps_si128(_mm_blend_ps(_mm_castsi128_ps(f), _mm_castsi128_ps(t), mask));414#else415return select(vboolf4(mask), t, f);416#endif417}418419#if defined(__aarch64__) || defined(__SSE4_1__)420__forceinline vint4 min(const vint4& a, const vint4& b) { return _mm_min_epi32(a, b); }421__forceinline vint4 max(const vint4& a, const vint4& b) { return _mm_max_epi32(a, b); }422423__forceinline vint4 umin(const vint4& a, const vint4& b) { return _mm_min_epu32(a, b); }424__forceinline vint4 umax(const vint4& a, const vint4& b) { return _mm_max_epu32(a, b); }425426#else427__forceinline vint4 min(const vint4& a, const vint4& b) { return select(a < b,a,b); }428__forceinline vint4 max(const vint4& a, const vint4& b) { return select(a < b,b,a); }429#endif430431__forceinline vint4 min(const vint4& a, int b) { return min(a,vint4(b)); }432__forceinline vint4 min(int a, const vint4& b) { return min(vint4(a),b); }433__forceinline vint4 max(const vint4& a, int b) { return max(a,vint4(b)); }434__forceinline vint4 max(int a, const vint4& b) { return max(vint4(a),b); }435436////////////////////////////////////////////////////////////////////////////////437// Movement/Shifting/Shuffling Functions438////////////////////////////////////////////////////////////////////////////////439440__forceinline vint4 unpacklo(const vint4& a, const vint4& b) { return _mm_castps_si128(_mm_unpacklo_ps(_mm_castsi128_ps(a), _mm_castsi128_ps(b))); }441__forceinline vint4 unpackhi(const vint4& a, const vint4& b) { return _mm_castps_si128(_mm_unpackhi_ps(_mm_castsi128_ps(a), _mm_castsi128_ps(b))); }442443#if defined(__aarch64__)444template<int i0, int i1, int i2, int i3>445__forceinline vint4 shuffle(const vint4& v) {446return vreinterpretq_s32_u8(vqtbl1q_u8( (uint8x16_t)v.v, _MN_SHUFFLE(i0, i1, i2, i3)));447}448template<int i0, int i1, int i2, int i3>449__forceinline vint4 shuffle(const vint4& a, const vint4& b) {450return vreinterpretq_s32_u8(vqtbl2q_u8( (uint8x16x2_t){(uint8x16_t)a.v, (uint8x16_t)b.v}, _MF_SHUFFLE(i0, i1, i2, i3)));451}452#else453template<int i0, int i1, int i2, int i3>454__forceinline vint4 shuffle(const vint4& v) {455return _mm_shuffle_epi32(v, _MM_SHUFFLE(i3, i2, i1, i0));456}457458template<int i0, int i1, int i2, int i3>459__forceinline vint4 shuffle(const vint4& a, const vint4& b) {460return _mm_castps_si128(_mm_shuffle_ps(_mm_castsi128_ps(a), _mm_castsi128_ps(b), _MM_SHUFFLE(i3, i2, i1, i0)));461}462#endif463#if defined(__SSE3__)464template<> __forceinline vint4 shuffle<0, 0, 2, 2>(const vint4& v) { return _mm_castps_si128(_mm_moveldup_ps(_mm_castsi128_ps(v))); }465template<> __forceinline vint4 shuffle<1, 1, 3, 3>(const vint4& v) { return _mm_castps_si128(_mm_movehdup_ps(_mm_castsi128_ps(v))); }466template<> __forceinline vint4 shuffle<0, 1, 0, 1>(const vint4& v) { return _mm_castpd_si128(_mm_movedup_pd (_mm_castsi128_pd(v))); }467#endif468469template<int i>470__forceinline vint4 shuffle(const vint4& v) {471return shuffle<i,i,i,i>(v);472}473474#if defined(__SSE4_1__) && !defined(__aarch64__)475template<int src> __forceinline int extract(const vint4& b) { return _mm_extract_epi32(b, src); }476template<int dst> __forceinline vint4 insert(const vint4& a, const int b) { return _mm_insert_epi32(a, b, dst); }477#else478template<int src> __forceinline int extract(const vint4& b) { return b[src&3]; }479template<int dst> __forceinline vint4 insert(const vint4& a, int b) { vint4 c = a; c[dst&3] = b; return c; }480#endif481482template<> __forceinline int extract<0>(const vint4& b) { return _mm_cvtsi128_si32(b); }483484__forceinline int toScalar(const vint4& v) { return _mm_cvtsi128_si32(v); }485486#if defined(__aarch64__)487__forceinline size_t toSizeT(const vint4& v) {488uint64x2_t x = uint64x2_t(v.v);489return x[0];490}491#else492__forceinline size_t toSizeT(const vint4& v) {493#if defined(__WIN32__) && !defined(__X86_64__) // win32 workaround494return toScalar(v);495#elif defined(__ARM_NEON)496// FIXME(LTE): Do we need a swap(i.e. use lane 1)?497return vgetq_lane_u64(*(reinterpret_cast<const uint64x2_t *>(&v)), 0);498#else499return _mm_cvtsi128_si64(v);500#endif501}502#endif503504#if defined(__AVX512VL__)505506__forceinline vint4 permute(const vint4 &a, const vint4 &index) {507return _mm_castps_si128(_mm_permutevar_ps(_mm_castsi128_ps(a),index));508}509510template<int i>511__forceinline vint4 align_shift_right(const vint4& a, const vint4& b) {512return _mm_alignr_epi32(a, b, i);513}514#endif515516////////////////////////////////////////////////////////////////////////////////517/// Reductions518////////////////////////////////////////////////////////////////////////////////519520#if defined(__aarch64__) || defined(__SSE4_1__)521522#if defined(__aarch64__)523__forceinline vint4 vreduce_min(const vint4& v) { int h = vminvq_s32(v); return vdupq_n_s32(h); }524__forceinline vint4 vreduce_max(const vint4& v) { int h = vmaxvq_s32(v); return vdupq_n_s32(h); }525__forceinline vint4 vreduce_add(const vint4& v) { int h = vaddvq_s32(v); return vdupq_n_s32(h); }526527__forceinline int reduce_min(const vint4& v) { return vminvq_s32(v); }528__forceinline int reduce_max(const vint4& v) { return vmaxvq_s32(v); }529__forceinline int reduce_add(const vint4& v) { return vaddvq_s32(v); }530#else531__forceinline vint4 vreduce_min(const vint4& v) { vint4 h = min(shuffle<1,0,3,2>(v),v); return min(shuffle<2,3,0,1>(h),h); }532__forceinline vint4 vreduce_max(const vint4& v) { vint4 h = max(shuffle<1,0,3,2>(v),v); return max(shuffle<2,3,0,1>(h),h); }533__forceinline vint4 vreduce_add(const vint4& v) { vint4 h = shuffle<1,0,3,2>(v) + v ; return shuffle<2,3,0,1>(h) + h ; }534535__forceinline int reduce_min(const vint4& v) { return toScalar(vreduce_min(v)); }536__forceinline int reduce_max(const vint4& v) { return toScalar(vreduce_max(v)); }537__forceinline int reduce_add(const vint4& v) { return toScalar(vreduce_add(v)); }538#endif539540__forceinline size_t select_min(const vint4& v) { return bsf(movemask(v == vreduce_min(v))); }541__forceinline size_t select_max(const vint4& v) { return bsf(movemask(v == vreduce_max(v))); }542543__forceinline size_t select_min(const vboolf4& valid, const vint4& v) { const vint4 a = select(valid,v,vint4(pos_inf)); return bsf(movemask(valid & (a == vreduce_min(a)))); }544__forceinline size_t select_max(const vboolf4& valid, const vint4& v) { const vint4 a = select(valid,v,vint4(neg_inf)); return bsf(movemask(valid & (a == vreduce_max(a)))); }545546#else547548__forceinline int reduce_min(const vint4& v) { return min(v[0],v[1],v[2],v[3]); }549__forceinline int reduce_max(const vint4& v) { return max(v[0],v[1],v[2],v[3]); }550__forceinline int reduce_add(const vint4& v) { return v[0]+v[1]+v[2]+v[3]; }551552#endif553554////////////////////////////////////////////////////////////////////////////////555/// Sorting networks556////////////////////////////////////////////////////////////////////////////////557558#if (defined(__aarch64__)) || defined(__SSE4_1__)559560__forceinline vint4 usort_ascending(const vint4& v)561{562const vint4 a0 = v;563const vint4 b0 = shuffle<1,0,3,2>(a0);564const vint4 c0 = umin(a0,b0);565const vint4 d0 = umax(a0,b0);566const vint4 a1 = select<0x5 /* 0b0101 */>(c0,d0);567const vint4 b1 = shuffle<2,3,0,1>(a1);568const vint4 c1 = umin(a1,b1);569const vint4 d1 = umax(a1,b1);570const vint4 a2 = select<0x3 /* 0b0011 */>(c1,d1);571const vint4 b2 = shuffle<0,2,1,3>(a2);572const vint4 c2 = umin(a2,b2);573const vint4 d2 = umax(a2,b2);574const vint4 a3 = select<0x2 /* 0b0010 */>(c2,d2);575return a3;576}577578__forceinline vint4 usort_descending(const vint4& v)579{580const vint4 a0 = v;581const vint4 b0 = shuffle<1,0,3,2>(a0);582const vint4 c0 = umax(a0,b0);583const vint4 d0 = umin(a0,b0);584const vint4 a1 = select<0x5 /* 0b0101 */>(c0,d0);585const vint4 b1 = shuffle<2,3,0,1>(a1);586const vint4 c1 = umax(a1,b1);587const vint4 d1 = umin(a1,b1);588const vint4 a2 = select<0x3 /* 0b0011 */>(c1,d1);589const vint4 b2 = shuffle<0,2,1,3>(a2);590const vint4 c2 = umax(a2,b2);591const vint4 d2 = umin(a2,b2);592const vint4 a3 = select<0x2 /* 0b0010 */>(c2,d2);593return a3;594}595596#else597598__forceinline vint4 usort_ascending(const vint4& v)599{600const vint4 a0 = v-vint4(0x80000000);601const vint4 b0 = shuffle<1,0,3,2>(a0);602const vint4 c0 = min(a0,b0);603const vint4 d0 = max(a0,b0);604const vint4 a1 = select<0x5 /* 0b0101 */>(c0,d0);605const vint4 b1 = shuffle<2,3,0,1>(a1);606const vint4 c1 = min(a1,b1);607const vint4 d1 = max(a1,b1);608const vint4 a2 = select<0x3 /* 0b0011 */>(c1,d1);609const vint4 b2 = shuffle<0,2,1,3>(a2);610const vint4 c2 = min(a2,b2);611const vint4 d2 = max(a2,b2);612const vint4 a3 = select<0x2 /* 0b0010 */>(c2,d2);613return a3+vint4(0x80000000);614}615616__forceinline vint4 usort_descending(const vint4& v)617{618const vint4 a0 = v-vint4(0x80000000);619const vint4 b0 = shuffle<1,0,3,2>(a0);620const vint4 c0 = max(a0,b0);621const vint4 d0 = min(a0,b0);622const vint4 a1 = select<0x5 /* 0b0101 */>(c0,d0);623const vint4 b1 = shuffle<2,3,0,1>(a1);624const vint4 c1 = max(a1,b1);625const vint4 d1 = min(a1,b1);626const vint4 a2 = select<0x3 /* 0b0011 */>(c1,d1);627const vint4 b2 = shuffle<0,2,1,3>(a2);628const vint4 c2 = max(a2,b2);629const vint4 d2 = min(a2,b2);630const vint4 a3 = select<0x2 /* 0b0010 */>(c2,d2);631return a3+vint4(0x80000000);632}633634#endif635636////////////////////////////////////////////////////////////////////////////////637/// Output Operators638////////////////////////////////////////////////////////////////////////////////639640__forceinline embree_ostream operator <<(embree_ostream cout, const vint4& a) {641return cout << "<" << a[0] << ", " << a[1] << ", " << a[2] << ", " << a[3] << ">";642}643}644645#undef vboolf646#undef vboold647#undef vint648#undef vuint649#undef vllong650#undef vfloat651#undef vdouble652653654