Path: blob/master/thirdparty/embree/common/simd/vboolf4_sse2.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/* 4-wide SSE bool type */16template<>17struct vboolf<4>18{19ALIGNED_STRUCT_(16);2021typedef vboolf4 Bool;22typedef vint4 Int;23typedef vfloat4 Float;2425enum { size = 4 }; // number of SIMD elements26union { __m128 v; int i[4]; }; // data2728////////////////////////////////////////////////////////////////////////////////29/// Constructors, Assignment & Cast Operators30////////////////////////////////////////////////////////////////////////////////3132__forceinline vboolf() {}33__forceinline vboolf(const vboolf4& other) { v = other.v; }34__forceinline vboolf4& operator =(const vboolf4& other) { v = other.v; return *this; }3536__forceinline vboolf(__m128 input) : v(input) {}37__forceinline operator const __m128&() const { return v; }38#if !defined(__EMSCRIPTEN__)39__forceinline operator const __m128i() const { return _mm_castps_si128(v); }40__forceinline operator const __m128d() const { return _mm_castps_pd(v); }41#endif4243__forceinline vboolf(bool a)44: v(mm_lookupmask_ps[(size_t(a) << 3) | (size_t(a) << 2) | (size_t(a) << 1) | size_t(a)]) {}45__forceinline vboolf(bool a, bool b)46: v(mm_lookupmask_ps[(size_t(b) << 3) | (size_t(a) << 2) | (size_t(b) << 1) | size_t(a)]) {}47__forceinline vboolf(bool a, bool b, bool c, bool d)48: v(mm_lookupmask_ps[(size_t(d) << 3) | (size_t(c) << 2) | (size_t(b) << 1) | size_t(a)]) {}49__forceinline vboolf(int mask) { assert(mask >= 0 && mask < 16); v = mm_lookupmask_ps[mask]; }50__forceinline vboolf(unsigned int mask) { assert(mask < 16); v = mm_lookupmask_ps[mask]; }5152/* return int32 mask */53__forceinline __m128i mask32() const {54return _mm_castps_si128(v);55}5657////////////////////////////////////////////////////////////////////////////////58/// Constants59////////////////////////////////////////////////////////////////////////////////6061__forceinline vboolf(FalseTy) : v(_mm_setzero_ps()) {}62__forceinline vboolf(TrueTy) : v(_mm_castsi128_ps(_mm_cmpeq_epi32(_mm_setzero_si128(), _mm_setzero_si128()))) {}6364////////////////////////////////////////////////////////////////////////////////65/// Array Access66////////////////////////////////////////////////////////////////////////////////6768__forceinline bool operator [](size_t index) const { assert(index < 4); return (_mm_movemask_ps(v) >> index) & 1; }69__forceinline int& operator [](size_t index) { assert(index < 4); return i[index]; }70};7172////////////////////////////////////////////////////////////////////////////////73/// Unary Operators74////////////////////////////////////////////////////////////////////////////////7576__forceinline vboolf4 operator !(const vboolf4& a) { return _mm_xor_ps(a, vboolf4(embree::True)); }7778////////////////////////////////////////////////////////////////////////////////79/// Binary Operators80////////////////////////////////////////////////////////////////////////////////8182__forceinline vboolf4 operator &(const vboolf4& a, const vboolf4& b) { return _mm_and_ps(a, b); }83__forceinline vboolf4 operator |(const vboolf4& a, const vboolf4& b) { return _mm_or_ps (a, b); }84__forceinline vboolf4 operator ^(const vboolf4& a, const vboolf4& b) { return _mm_xor_ps(a, b); }8586__forceinline vboolf4 andn(const vboolf4& a, const vboolf4& b) { return _mm_andnot_ps(b, a); }8788////////////////////////////////////////////////////////////////////////////////89/// Assignment Operators90////////////////////////////////////////////////////////////////////////////////9192__forceinline vboolf4& operator &=(vboolf4& a, const vboolf4& b) { return a = a & b; }93__forceinline vboolf4& operator |=(vboolf4& a, const vboolf4& b) { return a = a | b; }94__forceinline vboolf4& operator ^=(vboolf4& a, const vboolf4& b) { return a = a ^ b; }9596////////////////////////////////////////////////////////////////////////////////97/// Comparison Operators + Select98////////////////////////////////////////////////////////////////////////////////99100__forceinline vboolf4 operator !=(const vboolf4& a, const vboolf4& b) { return _mm_xor_ps(a, b); }101__forceinline vboolf4 operator ==(const vboolf4& a, const vboolf4& b) { return _mm_castsi128_ps(_mm_cmpeq_epi32(a, b)); }102103__forceinline vboolf4 select(const vboolf4& m, const vboolf4& t, const vboolf4& f) {104#if defined(__aarch64__) || defined(__SSE4_1__)105return _mm_blendv_ps(f, t, m);106#else107return _mm_or_ps(_mm_and_ps(m, t), _mm_andnot_ps(m, f));108#endif109}110111////////////////////////////////////////////////////////////////////////////////112/// Movement/Shifting/Shuffling Functions113////////////////////////////////////////////////////////////////////////////////114115__forceinline vboolf4 unpacklo(const vboolf4& a, const vboolf4& b) { return _mm_unpacklo_ps(a, b); }116__forceinline vboolf4 unpackhi(const vboolf4& a, const vboolf4& b) { return _mm_unpackhi_ps(a, b); }117118#if defined(__aarch64__)119template<int i0, int i1, int i2, int i3>120__forceinline vboolf4 shuffle(const vboolf4& v) {121return vreinterpretq_f32_u8(vqtbl1q_u8( vreinterpretq_u8_s32((int32x4_t)v.v), _MN_SHUFFLE(i0, i1, i2, i3)));122}123124template<int i0, int i1, int i2, int i3>125__forceinline vboolf4 shuffle(const vboolf4& a, const vboolf4& b) {126return vreinterpretq_f32_u8(vqtbl2q_u8( (uint8x16x2_t){(uint8x16_t)a.v, (uint8x16_t)b.v}, _MF_SHUFFLE(i0, i1, i2, i3)));127}128#else129template<int i0, int i1, int i2, int i3>130__forceinline vboolf4 shuffle(const vboolf4& v) {131return _mm_castsi128_ps(_mm_shuffle_epi32(v, _MM_SHUFFLE(i3, i2, i1, i0)));132}133134template<int i0, int i1, int i2, int i3>135__forceinline vboolf4 shuffle(const vboolf4& a, const vboolf4& b) {136return _mm_shuffle_ps(a, b, _MM_SHUFFLE(i3, i2, i1, i0));137}138#endif139140template<int i0>141__forceinline vboolf4 shuffle(const vboolf4& v) {142return shuffle<i0,i0,i0,i0>(v);143}144145#if defined(__SSE3__)146template<> __forceinline vboolf4 shuffle<0, 0, 2, 2>(const vboolf4& v) { return _mm_moveldup_ps(v); }147template<> __forceinline vboolf4 shuffle<1, 1, 3, 3>(const vboolf4& v) { return _mm_movehdup_ps(v); }148template<> __forceinline vboolf4 shuffle<0, 1, 0, 1>(const vboolf4& v) { return _mm_castpd_ps(_mm_movedup_pd(v)); }149#endif150151#if defined(__SSE4_1__) && !defined(__aarch64__)152template<int dst, int src, int clr> __forceinline vboolf4 insert(const vboolf4& a, const vboolf4& b) { return _mm_insert_ps(a, b, (dst << 4) | (src << 6) | clr); }153template<int dst, int src> __forceinline vboolf4 insert(const vboolf4& a, const vboolf4& b) { return insert<dst, src, 0>(a, b); }154template<int dst> __forceinline vboolf4 insert(const vboolf4& a, const bool b) { return insert<dst, 0>(a, vboolf4(b)); }155#endif156157////////////////////////////////////////////////////////////////////////////////158/// Reduction Operations159////////////////////////////////////////////////////////////////////////////////160161__forceinline bool reduce_and(const vboolf4& a) { return _mm_movemask_ps(a) == 0xf; }162__forceinline bool reduce_or (const vboolf4& a) { return _mm_movemask_ps(a) != 0x0; }163164__forceinline bool all (const vboolf4& b) { return _mm_movemask_ps(b) == 0xf; }165__forceinline bool any (const vboolf4& b) { return _mm_movemask_ps(b) != 0x0; }166__forceinline bool none(const vboolf4& b) { return _mm_movemask_ps(b) == 0x0; }167168__forceinline bool all (const vboolf4& valid, const vboolf4& b) { return all((!valid) | b); }169__forceinline bool any (const vboolf4& valid, const vboolf4& b) { return any(valid & b); }170__forceinline bool none(const vboolf4& valid, const vboolf4& b) { return none(valid & b); }171172__forceinline size_t movemask(const vboolf4& a) { return _mm_movemask_ps(a); }173#if defined(__aarch64__)174__forceinline size_t popcnt(const vboolf4& a) { return vaddvq_s32(vandq_u32(vreinterpretq_u32_f32(a.v),_mm_set1_epi32(1))); }175#elif defined(__SSE4_2__)176__forceinline size_t popcnt(const vboolf4& a) { return popcnt((size_t)_mm_movemask_ps(a)); }177#else178__forceinline size_t popcnt(const vboolf4& a) { return bool(a[0])+bool(a[1])+bool(a[2])+bool(a[3]); }179#endif180181////////////////////////////////////////////////////////////////////////////////182/// Get/Set Functions183////////////////////////////////////////////////////////////////////////////////184185__forceinline bool get(const vboolf4& a, size_t index) { return a[index]; }186__forceinline void set(vboolf4& a, size_t index) { a[index] = -1; }187__forceinline void clear(vboolf4& a, size_t index) { a[index] = 0; }188189////////////////////////////////////////////////////////////////////////////////190/// Output Operators191////////////////////////////////////////////////////////////////////////////////192193__forceinline embree_ostream operator <<(embree_ostream cout, const vboolf4& a) {194return cout << "<" << a[0] << ", " << a[1] << ", " << a[2] << ", " << a[3] << ">";195}196}197198#undef vboolf199#undef vboold200#undef vint201#undef vuint202#undef vllong203#undef vfloat204#undef vdouble205206207