godot/thirdparty/embree/common/simd/vint4_sse2.h

// Copyright 2009-2021 Intel Corporation
// SPDX-License-Identifier: Apache-2.0

#pragma once

#include "../math/emath.h"

#define vboolf
#define vboold
#define vint
#define vuint
#define vllong
#define vfloat
#define vdouble

namespace embree
{
  /* 4-wide SSE integer type */
  template<>
  struct vint<4>
  {};

  ////////////////////////////////////////////////////////////////////////////////
  /// Unary Operators
  ////////////////////////////////////////////////////////////////////////////////

#if defined(__AVX512VL__)
  __forceinline vboolf4 asBool(const vint4& a) { return _mm_movepi32_mask(a); }
#else
  __forceinline vboolf4 asBool(const vint4& a) {}
#endif

  __forceinline vint4 operator +(const vint4& a) {}
  __forceinline vint4 operator -(const vint4& a) {}
#if defined(__aarch64__)
  __forceinline vint4 abs(const vint4& a) { return vabsq_s32(a.v); }
#elif defined(__SSSE3__)
  __forceinline vint4 abs(const vint4& a) { return _mm_abs_epi32(a); }
#endif

  ////////////////////////////////////////////////////////////////////////////////
  /// Binary Operators
  ////////////////////////////////////////////////////////////////////////////////

  __forceinline vint4 operator +(const vint4& a, const vint4& b) {}
  __forceinline vint4 operator +(const vint4& a, int          b) {}
  __forceinline vint4 operator +(int          a, const vint4& b) {}

  __forceinline vint4 operator -(const vint4& a, const vint4& b) {}
  __forceinline vint4 operator -(const vint4& a, int          b) {}
  __forceinline vint4 operator -(int          a, const vint4& b) {}

#if (defined(__aarch64__)) || defined(__SSE4_1__)
  __forceinline vint4 operator *(const vint4& a, const vint4& b) { return _mm_mullo_epi32(a, b); }
#else
  __forceinline vint4 operator *(const vint4& a, const vint4& b) {}
#endif
  __forceinline vint4 operator *(const vint4& a, int          b) {}
  __forceinline vint4 operator *(int          a, const vint4& b) {}

  __forceinline vint4 operator &(const vint4& a, const vint4& b) {}
  __forceinline vint4 operator &(const vint4& a, int          b) {}
  __forceinline vint4 operator &(int          a, const vint4& b) {}

  __forceinline vint4 operator |(const vint4& a, const vint4& b) {}
  __forceinline vint4 operator |(const vint4& a, int          b) {}
  __forceinline vint4 operator |(int          a, const vint4& b) {}

  __forceinline vint4 operator ^(const vint4& a, const vint4& b) {}
  __forceinline vint4 operator ^(const vint4& a, int          b) {}
  __forceinline vint4 operator ^(int          a, const vint4& b) {}

  __forceinline vint4 operator <<(const vint4& a, const int n) {}
  __forceinline vint4 operator >>(const vint4& a, const int n) {}

  __forceinline vint4 sll (const vint4& a, int b) {}
  __forceinline vint4 sra (const vint4& a, int b) {}
  __forceinline vint4 srl (const vint4& a, int b) {}
  
  ////////////////////////////////////////////////////////////////////////////////
  /// Assignment Operators
  ////////////////////////////////////////////////////////////////////////////////

  __forceinline vint4& operator +=(vint4& a, const vint4& b) {}
  __forceinline vint4& operator +=(vint4& a, int          b) {}
  
  __forceinline vint4& operator -=(vint4& a, const vint4& b) {}
  __forceinline vint4& operator -=(vint4& a, int          b) {}

#if (defined(__aarch64__)) || defined(__SSE4_1__)
  __forceinline vint4& operator *=(vint4& a, const vint4& b) { return a = a * b; }
  __forceinline vint4& operator *=(vint4& a, int          b) { return a = a * b; }
#endif
  
  __forceinline vint4& operator &=(vint4& a, const vint4& b) {}
  __forceinline vint4& operator &=(vint4& a, int          b) {}
  
  __forceinline vint4& operator |=(vint4& a, const vint4& b) {}
  __forceinline vint4& operator |=(vint4& a, int          b) {}
  
  __forceinline vint4& operator <<=(vint4& a, int b) {}
  __forceinline vint4& operator >>=(vint4& a, int b) {}

  ////////////////////////////////////////////////////////////////////////////////
  /// Comparison Operators + Select
  ////////////////////////////////////////////////////////////////////////////////

#if defined(__AVX512VL__)
  __forceinline vboolf4 operator ==(const vint4& a, const vint4& b) { return _mm_cmp_epi32_mask(a,b,_MM_CMPINT_EQ); }
  __forceinline vboolf4 operator !=(const vint4& a, const vint4& b) { return _mm_cmp_epi32_mask(a,b,_MM_CMPINT_NE); }
  __forceinline vboolf4 operator < (const vint4& a, const vint4& b) { return _mm_cmp_epi32_mask(a,b,_MM_CMPINT_LT); }
  __forceinline vboolf4 operator >=(const vint4& a, const vint4& b) { return _mm_cmp_epi32_mask(a,b,_MM_CMPINT_GE); }
  __forceinline vboolf4 operator > (const vint4& a, const vint4& b) { return _mm_cmp_epi32_mask(a,b,_MM_CMPINT_GT); }
  __forceinline vboolf4 operator <=(const vint4& a, const vint4& b) { return _mm_cmp_epi32_mask(a,b,_MM_CMPINT_LE); }
#else
  __forceinline vboolf4 operator ==(const vint4& a, const vint4& b) {}
  __forceinline vboolf4 operator !=(const vint4& a, const vint4& b) {}
  __forceinline vboolf4 operator < (const vint4& a, const vint4& b) {}
  __forceinline vboolf4 operator >=(const vint4& a, const vint4& b) {}
  __forceinline vboolf4 operator > (const vint4& a, const vint4& b) {}
  __forceinline vboolf4 operator <=(const vint4& a, const vint4& b) {}
#endif

  __forceinline vboolf4 operator ==(const vint4& a, int          b) {}
  __forceinline vboolf4 operator ==(int          a, const vint4& b) {}

  __forceinline vboolf4 operator !=(const vint4& a, int          b) {}
  __forceinline vboolf4 operator !=(int          a, const vint4& b) {}

  __forceinline vboolf4 operator < (const vint4& a, int          b) {}
  __forceinline vboolf4 operator < (int          a, const vint4& b) {}

  __forceinline vboolf4 operator >=(const vint4& a, int          b) {}
  __forceinline vboolf4 operator >=(int          a, const vint4& b) {}

  __forceinline vboolf4 operator > (const vint4& a, int          b) {}
  __forceinline vboolf4 operator > (int          a, const vint4& b) {}

  __forceinline vboolf4 operator <=(const vint4& a, int          b) {}
  __forceinline vboolf4 operator <=(int          a, const vint4& b) {}

  __forceinline vboolf4 eq(const vint4& a, const vint4& b) {}
  __forceinline vboolf4 ne(const vint4& a, const vint4& b) {}
  __forceinline vboolf4 lt(const vint4& a, const vint4& b) {}
  __forceinline vboolf4 ge(const vint4& a, const vint4& b) {}
  __forceinline vboolf4 gt(const vint4& a, const vint4& b) {}
  __forceinline vboolf4 le(const vint4& a, const vint4& b) {}

#if defined(__AVX512VL__)
  __forceinline vboolf4 eq(const vboolf4& mask, const vint4& a, const vint4& b) { return _mm_mask_cmp_epi32_mask(mask, a, b, _MM_CMPINT_EQ); }
  __forceinline vboolf4 ne(const vboolf4& mask, const vint4& a, const vint4& b) { return _mm_mask_cmp_epi32_mask(mask, a, b, _MM_CMPINT_NE); }
  __forceinline vboolf4 lt(const vboolf4& mask, const vint4& a, const vint4& b) { return _mm_mask_cmp_epi32_mask(mask, a, b, _MM_CMPINT_LT); }
  __forceinline vboolf4 ge(const vboolf4& mask, const vint4& a, const vint4& b) { return _mm_mask_cmp_epi32_mask(mask, a, b, _MM_CMPINT_GE); }
  __forceinline vboolf4 gt(const vboolf4& mask, const vint4& a, const vint4& b) { return _mm_mask_cmp_epi32_mask(mask, a, b, _MM_CMPINT_GT); }
  __forceinline vboolf4 le(const vboolf4& mask, const vint4& a, const vint4& b) { return _mm_mask_cmp_epi32_mask(mask, a, b, _MM_CMPINT_LE); }
#else
  __forceinline vboolf4 eq(const vboolf4& mask, const vint4& a, const vint4& b) {}
  __forceinline vboolf4 ne(const vboolf4& mask, const vint4& a, const vint4& b) {}
  __forceinline vboolf4 lt(const vboolf4& mask, const vint4& a, const vint4& b) {}
  __forceinline vboolf4 ge(const vboolf4& mask, const vint4& a, const vint4& b) {}
  __forceinline vboolf4 gt(const vboolf4& mask, const vint4& a, const vint4& b) {}
  __forceinline vboolf4 le(const vboolf4& mask, const vint4& a, const vint4& b) {}
#endif

  template<int mask>
  __forceinline vint4 select(const vint4& t, const vint4& f) {}

#if defined(__aarch64__) || defined(__SSE4_1__)
  __forceinline vint4 min(const vint4& a, const vint4& b) { return _mm_min_epi32(a, b); }
  __forceinline vint4 max(const vint4& a, const vint4& b) { return _mm_max_epi32(a, b); }

  __forceinline vint4 umin(const vint4& a, const vint4& b) { return _mm_min_epu32(a, b); }
  __forceinline vint4 umax(const vint4& a, const vint4& b) { return _mm_max_epu32(a, b); }

#else
  __forceinline vint4 min(const vint4& a, const vint4& b) {}
  __forceinline vint4 max(const vint4& a, const vint4& b) {}
#endif

  __forceinline vint4 min(const vint4& a, int          b) {}
  __forceinline vint4 min(int          a, const vint4& b) {}
  __forceinline vint4 max(const vint4& a, int          b) {}
  __forceinline vint4 max(int          a, const vint4& b) {}

  ////////////////////////////////////////////////////////////////////////////////
  // Movement/Shifting/Shuffling Functions
  ////////////////////////////////////////////////////////////////////////////////

  __forceinline vint4 unpacklo(const vint4& a, const vint4& b) {}
  __forceinline vint4 unpackhi(const vint4& a, const vint4& b) {}

#if defined(__aarch64__)
    template<int i0, int i1, int i2, int i3>
    __forceinline vint4 shuffle(const vint4& v) {
        return vreinterpretq_s32_u8(vqtbl1q_u8( (uint8x16_t)v.v, _MN_SHUFFLE(i0, i1, i2, i3)));
    }
    template<int i0, int i1, int i2, int i3>
    __forceinline vint4 shuffle(const vint4& a, const vint4& b) {
        return vreinterpretq_s32_u8(vqtbl2q_u8( (uint8x16x2_t){(uint8x16_t)a.v, (uint8x16_t)b.v}, _MF_SHUFFLE(i0, i1, i2, i3)));
    }
#else
  template<int i0, int i1, int i2, int i3>
  __forceinline vint4 shuffle(const vint4& v) {}

  template<int i0, int i1, int i2, int i3>
  __forceinline vint4 shuffle(const vint4& a, const vint4& b) {}
#endif
#if defined(__SSE3__)
  template<> __forceinline vint4 shuffle<0, 0, 2, 2>(const vint4& v) { return _mm_castps_si128(_mm_moveldup_ps(_mm_castsi128_ps(v))); }
  template<> __forceinline vint4 shuffle<1, 1, 3, 3>(const vint4& v) { return _mm_castps_si128(_mm_movehdup_ps(_mm_castsi128_ps(v))); }
  template<> __forceinline vint4 shuffle<0, 1, 0, 1>(const vint4& v) { return _mm_castpd_si128(_mm_movedup_pd (_mm_castsi128_pd(v))); }
#endif

  template<int i>
  __forceinline vint4 shuffle(const vint4& v) {}

#if defined(__SSE4_1__) && !defined(__aarch64__)
  template<int src> __forceinline int extract(const vint4& b) { return _mm_extract_epi32(b, src); }
  template<int dst> __forceinline vint4 insert(const vint4& a, const int b) { return _mm_insert_epi32(a, b, dst); }
#else
  template<int src> __forceinline int extract(const vint4& b) {}
  template<int dst> __forceinline vint4 insert(const vint4& a, int b) {}
#endif

  template<> __forceinline int extract<0>(const vint4& b) {}
  
  __forceinline int toScalar(const vint4& v) {}
  
#if defined(__aarch64__)
  __forceinline size_t toSizeT(const vint4& v) {
    uint64x2_t x = uint64x2_t(v.v);
    return x[0];
  }
#else
__forceinline size_t toSizeT(const vint4& v) {}
#endif

#if defined(__AVX512VL__)

  __forceinline vint4 permute(const vint4 &a, const vint4 &index) {
    return  _mm_castps_si128(_mm_permutevar_ps(_mm_castsi128_ps(a),index));
  }

  template<int i>
  __forceinline vint4 align_shift_right(const vint4& a, const vint4& b) {
    return _mm_alignr_epi32(a, b, i);    
  }  
#endif

  ////////////////////////////////////////////////////////////////////////////////
  /// Reductions
  ////////////////////////////////////////////////////////////////////////////////

#if defined(__aarch64__) || defined(__SSE4_1__)

#if defined(__aarch64__)
    __forceinline vint4 vreduce_min(const vint4& v) { int h = vminvq_s32(v); return vdupq_n_s32(h); }
    __forceinline vint4 vreduce_max(const vint4& v) { int h = vmaxvq_s32(v); return vdupq_n_s32(h); }
    __forceinline vint4 vreduce_add(const vint4& v) { int h = vaddvq_s32(v); return vdupq_n_s32(h); }

    __forceinline int reduce_min(const vint4& v) { return vminvq_s32(v); }
    __forceinline int reduce_max(const vint4& v) { return vmaxvq_s32(v); }
    __forceinline int reduce_add(const vint4& v) { return vaddvq_s32(v); }
#else
  __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); }
  __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); }
  __forceinline vint4 vreduce_add(const vint4& v) { vint4 h = shuffle<1,0,3,2>(v)   + v ; return shuffle<2,3,0,1>(h)   + h ; }

  __forceinline int reduce_min(const vint4& v) { return toScalar(vreduce_min(v)); }
  __forceinline int reduce_max(const vint4& v) { return toScalar(vreduce_max(v)); }
  __forceinline int reduce_add(const vint4& v) { return toScalar(vreduce_add(v)); }
#endif

  __forceinline size_t select_min(const vint4& v) { return bsf(movemask(v == vreduce_min(v))); }
  __forceinline size_t select_max(const vint4& v) { return bsf(movemask(v == vreduce_max(v))); }

  __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)))); }
  __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)))); }

#else

  __forceinline int reduce_min(const vint4& v) {}
  __forceinline int reduce_max(const vint4& v) {}
  __forceinline int reduce_add(const vint4& v) {}

#endif

  ////////////////////////////////////////////////////////////////////////////////
  /// Sorting networks
  ////////////////////////////////////////////////////////////////////////////////

#if (defined(__aarch64__)) || defined(__SSE4_1__)

  __forceinline vint4 usort_ascending(const vint4& v)
  {
    const vint4 a0 = v;
    const vint4 b0 = shuffle<1,0,3,2>(a0);
    const vint4 c0 = umin(a0,b0);
    const vint4 d0 = umax(a0,b0);
    const vint4 a1 = select<0x5 /* 0b0101 */>(c0,d0);
    const vint4 b1 = shuffle<2,3,0,1>(a1);
    const vint4 c1 = umin(a1,b1);
    const vint4 d1 = umax(a1,b1);
    const vint4 a2 = select<0x3 /* 0b0011 */>(c1,d1);
    const vint4 b2 = shuffle<0,2,1,3>(a2);
    const vint4 c2 = umin(a2,b2);
    const vint4 d2 = umax(a2,b2);
    const vint4 a3 = select<0x2 /* 0b0010 */>(c2,d2);
    return a3;
  }

  __forceinline vint4 usort_descending(const vint4& v)
  {
    const vint4 a0 = v;
    const vint4 b0 = shuffle<1,0,3,2>(a0);
    const vint4 c0 = umax(a0,b0);
    const vint4 d0 = umin(a0,b0);
    const vint4 a1 = select<0x5 /* 0b0101 */>(c0,d0);
    const vint4 b1 = shuffle<2,3,0,1>(a1);
    const vint4 c1 = umax(a1,b1);
    const vint4 d1 = umin(a1,b1);
    const vint4 a2 = select<0x3 /* 0b0011 */>(c1,d1);
    const vint4 b2 = shuffle<0,2,1,3>(a2);
    const vint4 c2 = umax(a2,b2);
    const vint4 d2 = umin(a2,b2);
    const vint4 a3 = select<0x2 /* 0b0010 */>(c2,d2);
    return a3;
  }

#else

  __forceinline vint4 usort_ascending(const vint4& v)
  {}

  __forceinline vint4 usort_descending(const vint4& v)
  {}

#endif

  ////////////////////////////////////////////////////////////////////////////////
  /// Output Operators
  ////////////////////////////////////////////////////////////////////////////////

  __forceinline embree_ostream operator <<(embree_ostream cout, const vint4& a) {}
}

#undef vboolf
#undef vboold
#undef vint
#undef vuint
#undef vllong
#undef vfloat
#undef vdouble