// Copyright 2009-2021 Intel Corporation // SPDX-License-Identifier: Apache-2.0 #pragma once #include "emath.h" #include "vec3.h" namespace embree { //////////////////////////////////////////////////////////////////////////////// /// Generic 4D vector Class //////////////////////////////////////////////////////////////////////////////// template struct Vec4 { enum { N = 4 }; union { struct { T x, y, z, w; }; #if !(defined(__WIN32__) && _MSC_VER == 1800) // workaround for older VS 2013 compiler T components[N]; #endif }; typedef T Scalar; //////////////////////////////////////////////////////////////////////////////// /// Construction //////////////////////////////////////////////////////////////////////////////// __forceinline Vec4( ) {} __forceinline explicit Vec4( const T& a ) : x(a), y(a), z(a), w(a) {} __forceinline Vec4( const T& x, const T& y, const T& z, const T& w ) : x(x), y(y), z(z), w(w) {} __forceinline Vec4( const Vec3& xyz, const T& w ) : x(xyz.x), y(xyz.y), z(xyz.z), w(w) {} __forceinline Vec4( const Vec4& other ) { x = other.x; y = other.y; z = other.z; w = other.w; } __forceinline Vec4( const Vec3fx& other ); template __forceinline Vec4( const Vec4& a ) : x(T(a.x)), y(T(a.y)), z(T(a.z)), w(T(a.w)) {} template __forceinline Vec4& operator =(const Vec4& other) { x = other.x; y = other.y; z = other.z; w = other.w; return *this; } __forceinline Vec4& operator =(const Vec4& other) { x = other.x; y = other.y; z = other.z; w = other.w; return *this; } __forceinline operator Vec3 () const { return Vec3(x,y,z); } //////////////////////////////////////////////////////////////////////////////// /// Constants //////////////////////////////////////////////////////////////////////////////// __forceinline Vec4( ZeroTy ) : x(zero), y(zero), z(zero), w(zero) {} __forceinline Vec4( OneTy ) : x(one), y(one), z(one), w(one) {} __forceinline Vec4( PosInfTy ) : x(pos_inf), y(pos_inf), z(pos_inf), w(pos_inf) {} __forceinline Vec4( NegInfTy ) : x(neg_inf), y(neg_inf), z(neg_inf), w(neg_inf) {} #if defined(__WIN32__) && (_MSC_VER == 1800) // workaround for older VS 2013 compiler __forceinline const T& operator [](const size_t axis) const { assert(axis < 4); return (&x)[axis]; } __forceinline T& operator [](const size_t axis) { assert(axis < 4); return (&x)[axis]; } #else __forceinline const T& operator [](const size_t axis ) const { assert(axis < 4); return components[axis]; } __forceinline T& operator [](const size_t axis) { assert(axis < 4); return components[axis]; } #endif //////////////////////////////////////////////////////////////////////////////// /// Swizzles //////////////////////////////////////////////////////////////////////////////// __forceinline Vec3 xyz() const { return Vec3(x, y, z); } }; //////////////////////////////////////////////////////////////////////////////// /// Unary Operators //////////////////////////////////////////////////////////////////////////////// template __forceinline Vec4 operator +( const Vec4& a ) { return Vec4(+a.x, +a.y, +a.z, +a.w); } template __forceinline Vec4 operator -( const Vec4& a ) { return Vec4(-a.x, -a.y, -a.z, -a.w); } template __forceinline Vec4 abs ( const Vec4& a ) { return Vec4(abs (a.x), abs (a.y), abs (a.z), abs (a.w)); } template __forceinline Vec4 rcp ( const Vec4& a ) { return Vec4(rcp (a.x), rcp (a.y), rcp (a.z), rcp (a.w)); } template __forceinline Vec4 rsqrt ( const Vec4& a ) { return Vec4(rsqrt(a.x), rsqrt(a.y), rsqrt(a.z), rsqrt(a.w)); } template __forceinline Vec4 sqrt ( const Vec4& a ) { return Vec4(sqrt (a.x), sqrt (a.y), sqrt (a.z), sqrt (a.w)); } //////////////////////////////////////////////////////////////////////////////// /// Binary Operators //////////////////////////////////////////////////////////////////////////////// template __forceinline Vec4 operator +( const Vec4& a, const Vec4& b ) { return Vec4(a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w); } template __forceinline Vec4 operator -( const Vec4& a, const Vec4& b ) { return Vec4(a.x - b.x, a.y - b.y, a.z - b.z, a.w - b.w); } template __forceinline Vec4 operator *( const Vec4& a, const Vec4& b ) { return Vec4(a.x * b.x, a.y * b.y, a.z * b.z, a.w * b.w); } template __forceinline Vec4 operator *( const T& a, const Vec4& b ) { return Vec4(a * b.x, a * b.y, a * b.z, a * b.w); } template __forceinline Vec4 operator *( const Vec4& a, const T& b ) { return Vec4(a.x * b , a.y * b , a.z * b , a.w * b ); } template __forceinline Vec4 operator /( const Vec4& a, const Vec4& b ) { return Vec4(a.x / b.x, a.y / b.y, a.z / b.z, a.w / b.w); } template __forceinline Vec4 operator /( const Vec4& a, const T& b ) { return Vec4(a.x / b , a.y / b , a.z / b , a.w / b ); } template __forceinline Vec4 operator /( const T& a, const Vec4& b ) { return Vec4(a / b.x, a / b.y, a / b.z, a / b.w); } template __forceinline Vec4 min(const Vec4& a, const Vec4& b) { return Vec4(min(a.x, b.x), min(a.y, b.y), min(a.z, b.z), min(a.w, b.w)); } template __forceinline Vec4 max(const Vec4& a, const Vec4& b) { return Vec4(max(a.x, b.x), max(a.y, b.y), max(a.z, b.z), max(a.w, b.w)); } //////////////////////////////////////////////////////////////////////////////// /// Ternary Operators //////////////////////////////////////////////////////////////////////////////// template __forceinline Vec4 madd ( const Vec4& a, const Vec4& b, const Vec4& c) { return Vec4( madd(a.x,b.x,c.x), madd(a.y,b.y,c.y), madd(a.z,b.z,c.z), madd(a.w,b.w,c.w)); } template __forceinline Vec4 msub ( const Vec4& a, const Vec4& b, const Vec4& c) { return Vec4( msub(a.x,b.x,c.x), msub(a.y,b.y,c.y), msub(a.z,b.z,c.z), msub(a.w,b.w,c.w)); } template __forceinline Vec4 nmadd ( const Vec4& a, const Vec4& b, const Vec4& c) { return Vec4(nmadd(a.x,b.x,c.x),nmadd(a.y,b.y,c.y),nmadd(a.z,b.z,c.z),nmadd(a.w,b.w,c.w)); } template __forceinline Vec4 nmsub ( const Vec4& a, const Vec4& b, const Vec4& c) { return Vec4(nmsub(a.x,b.x,c.x),nmsub(a.y,b.y,c.y),nmsub(a.z,b.z,c.z),nmsub(a.w,b.w,c.w)); } template __forceinline Vec4 madd ( const T& a, const Vec4& b, const Vec4& c) { return Vec4( madd(a,b.x,c.x), madd(a,b.y,c.y), madd(a,b.z,c.z), madd(a,b.w,c.w)); } template __forceinline Vec4 msub ( const T& a, const Vec4& b, const Vec4& c) { return Vec4( msub(a,b.x,c.x), msub(a,b.y,c.y), msub(a,b.z,c.z), msub(a,b.w,c.w)); } template __forceinline Vec4 nmadd ( const T& a, const Vec4& b, const Vec4& c) { return Vec4(nmadd(a,b.x,c.x),nmadd(a,b.y,c.y),nmadd(a,b.z,c.z),nmadd(a,b.w,c.w)); } template __forceinline Vec4 nmsub ( const T& a, const Vec4& b, const Vec4& c) { return Vec4(nmsub(a,b.x,c.x),nmsub(a,b.y,c.y),nmsub(a,b.z,c.z),nmsub(a,b.w,c.w)); } //////////////////////////////////////////////////////////////////////////////// /// Assignment Operators //////////////////////////////////////////////////////////////////////////////// template __forceinline Vec4& operator +=( Vec4& a, const Vec4& b ) { a.x += b.x; a.y += b.y; a.z += b.z; a.w += b.w; return a; } template __forceinline Vec4& operator -=( Vec4& a, const Vec4& b ) { a.x -= b.x; a.y -= b.y; a.z -= b.z; a.w -= b.w; return a; } template __forceinline Vec4& operator *=( Vec4& a, const T& b ) { a.x *= b ; a.y *= b ; a.z *= b ; a.w *= b ; return a; } template __forceinline Vec4& operator /=( Vec4& a, const T& b ) { a.x /= b ; a.y /= b ; a.z /= b ; a.w /= b ; return a; } //////////////////////////////////////////////////////////////////////////////// /// Reduction Operators //////////////////////////////////////////////////////////////////////////////// template __forceinline T reduce_add( const Vec4& a ) { return a.x + a.y + a.z + a.w; } template __forceinline T reduce_mul( const Vec4& a ) { return a.x * a.y * a.z * a.w; } template __forceinline T reduce_min( const Vec4& a ) { return min(a.x, a.y, a.z, a.w); } template __forceinline T reduce_max( const Vec4& a ) { return max(a.x, a.y, a.z, a.w); } //////////////////////////////////////////////////////////////////////////////// /// Comparison Operators //////////////////////////////////////////////////////////////////////////////// template __forceinline bool operator ==( const Vec4& a, const Vec4& b ) { return a.x == b.x && a.y == b.y && a.z == b.z && a.w == b.w; } template __forceinline bool operator !=( const Vec4& a, const Vec4& b ) { return a.x != b.x || a.y != b.y || a.z != b.z || a.w != b.w; } template __forceinline bool operator < ( const Vec4& a, const Vec4& b ) { if (a.x != b.x) return a.x < b.x; if (a.y != b.y) return a.y < b.y; if (a.z != b.z) return a.z < b.z; if (a.w != b.w) return a.w < b.w; return false; } //////////////////////////////////////////////////////////////////////////////// /// Shift Operators //////////////////////////////////////////////////////////////////////////////// template __forceinline Vec4 shift_right_1( const Vec4& a ) { return Vec4(shift_right_1(a.x),shift_right_1(a.y),shift_right_1(a.z),shift_right_1(a.w)); } //////////////////////////////////////////////////////////////////////////////// /// Euclidean Space Operators //////////////////////////////////////////////////////////////////////////////// template __forceinline T dot ( const Vec4& a, const Vec4& b ) { return madd(a.x,b.x,madd(a.y,b.y,madd(a.z,b.z,a.w*b.w))); } template __forceinline T length ( const Vec4& a ) { return sqrt(dot(a,a)); } template __forceinline Vec4 normalize( const Vec4& a ) { return a*rsqrt(dot(a,a)); } template __forceinline T distance ( const Vec4& a, const Vec4& b ) { return length(a-b); } //////////////////////////////////////////////////////////////////////////////// /// Select //////////////////////////////////////////////////////////////////////////////// template __forceinline Vec4 select ( bool s, const Vec4& t, const Vec4& f ) { return Vec4(select(s,t.x,f.x),select(s,t.y,f.y),select(s,t.z,f.z),select(s,t.w,f.w)); } template __forceinline Vec4 select ( const Vec4& s, const Vec4& t, const Vec4& f ) { return Vec4(select(s.x,t.x,f.x),select(s.y,t.y,f.y),select(s.z,t.z,f.z),select(s.w,t.w,f.w)); } template __forceinline Vec4 select ( const typename T::Bool& s, const Vec4& t, const Vec4& f ) { return Vec4(select(s,t.x,f.x),select(s,t.y,f.y),select(s,t.z,f.z),select(s,t.w,f.w)); } template __forceinline Vec4 lerp(const Vec4& v0, const Vec4& v1, const T& t) { return madd(Vec4(T(1.0f)-t),v0,t*v1); } //////////////////////////////////////////////////////////////////////////////// /// Output Operators //////////////////////////////////////////////////////////////////////////////// template __forceinline embree_ostream operator<<(embree_ostream cout, const Vec4& a) { return cout << "(" << a.x << ", " << a.y << ", " << a.z << ", " << a.w << ")"; } //////////////////////////////////////////////////////////////////////////////// /// Default template instantiations //////////////////////////////////////////////////////////////////////////////// typedef Vec4 Vec4b; typedef Vec4 Vec4uc; typedef Vec4 Vec4i; typedef Vec4 Vec4f; } #include "vec3ba.h" #include "vec3ia.h" #include "vec3fa.h" //////////////////////////////////////////////////////////////////////////////// /// SSE / AVX / MIC specializations //////////////////////////////////////////////////////////////////////////////// #if defined(__SSE__) || defined(__ARM_NEON) #include "../simd/sse.h" #endif #if defined __AVX__ #include "../simd/avx.h" #endif #if defined __AVX512F__ #include "../simd/avx512.h" #endif namespace embree { template<> __forceinline Vec4::Vec4( const Vec3fx& a ) { x = a.x; y = a.y; z = a.z; w = a.w; } #if !defined(__SYCL_DEVICE_ONLY__) #if defined(__AVX__) template<> __forceinline Vec4::Vec4( const Vec3fx& a ) { x = a.x; y = a.y; z = a.z; w = a.w; } #elif defined(__SSE__) || defined(__ARM_NEON) template<> __forceinline Vec4::Vec4( const Vec3fx& a ) { const vfloat4 v = vfloat4(a.m128); x = shuffle<0,0,0,0>(v); y = shuffle<1,1,1,1>(v); z = shuffle<2,2,2,2>(v); w = shuffle<3,3,3,3>(v); } #endif #if defined(__AVX__) template<> __forceinline Vec4::Vec4( const Vec3fx& a ) { x = a.x; y = a.y; z = a.z; w = a.w; } #endif #if defined(__AVX512F__) template<> __forceinline Vec4::Vec4( const Vec3fx& a ) : x(a.x), y(a.y), z(a.z), w(a.w) {} #endif #else #if defined(__SSE__) template<> __forceinline Vec4::Vec4(const Vec3fx& a) { x = a.x; y = a.y; z = a.z; w = a.w; } #endif #if defined(__AVX__) template<> __forceinline Vec4::Vec4(const Vec3fx& a) { x = a.x; y = a.y; z = a.z; w = a.w; } #endif #if defined(__AVX512F__) template<> __forceinline Vec4::Vec4(const Vec3fx& a) { x = a.x; y = a.y; z = a.z; w = a.w; } #endif #endif }