Path: blob/master/thirdparty/embree/common/math/vec4.h
9912 views
// Copyright 2009-2021 Intel Corporation1// SPDX-License-Identifier: Apache-2.023#pragma once45#include "emath.h"6#include "vec3.h"78namespace embree9{10////////////////////////////////////////////////////////////////////////////////11/// Generic 4D vector Class12////////////////////////////////////////////////////////////////////////////////1314template<typename T> struct Vec415{16enum { N = 4 };17union {18struct { T x, y, z, w; };19#if !(defined(__WIN32__) && _MSC_VER == 1800) // workaround for older VS 2013 compiler20T components[N];21#endif22};2324typedef T Scalar;2526////////////////////////////////////////////////////////////////////////////////27/// Construction28////////////////////////////////////////////////////////////////////////////////2930__forceinline Vec4( ) {}31__forceinline explicit Vec4( const T& a ) : x(a), y(a), z(a), w(a) {}32__forceinline Vec4( const T& x, const T& y, const T& z, const T& w ) : x(x), y(y), z(z), w(w) {}33__forceinline Vec4( const Vec3<T>& xyz, const T& w ) : x(xyz.x), y(xyz.y), z(xyz.z), w(w) {}3435__forceinline Vec4( const Vec4& other ) { x = other.x; y = other.y; z = other.z; w = other.w; }36__forceinline Vec4( const Vec3fx& other );3738template<typename T1> __forceinline Vec4( const Vec4<T1>& a ) : x(T(a.x)), y(T(a.y)), z(T(a.z)), w(T(a.w)) {}39template<typename T1> __forceinline Vec4& operator =(const Vec4<T1>& other) { x = other.x; y = other.y; z = other.z; w = other.w; return *this; }4041__forceinline Vec4& operator =(const Vec4& other) { x = other.x; y = other.y; z = other.z; w = other.w; return *this; }4243__forceinline operator Vec3<T> () const { return Vec3<T>(x,y,z); }4445////////////////////////////////////////////////////////////////////////////////46/// Constants47////////////////////////////////////////////////////////////////////////////////4849__forceinline Vec4( ZeroTy ) : x(zero), y(zero), z(zero), w(zero) {}50__forceinline Vec4( OneTy ) : x(one), y(one), z(one), w(one) {}51__forceinline Vec4( PosInfTy ) : x(pos_inf), y(pos_inf), z(pos_inf), w(pos_inf) {}52__forceinline Vec4( NegInfTy ) : x(neg_inf), y(neg_inf), z(neg_inf), w(neg_inf) {}5354#if defined(__WIN32__) && (_MSC_VER == 1800) // workaround for older VS 2013 compiler55__forceinline const T& operator [](const size_t axis) const { assert(axis < 4); return (&x)[axis]; }56__forceinline T& operator [](const size_t axis) { assert(axis < 4); return (&x)[axis]; }57#else58__forceinline const T& operator [](const size_t axis ) const { assert(axis < 4); return components[axis]; }59__forceinline T& operator [](const size_t axis) { assert(axis < 4); return components[axis]; }60#endif6162////////////////////////////////////////////////////////////////////////////////63/// Swizzles64////////////////////////////////////////////////////////////////////////////////6566__forceinline Vec3<T> xyz() const { return Vec3<T>(x, y, z); }67};6869////////////////////////////////////////////////////////////////////////////////70/// Unary Operators71////////////////////////////////////////////////////////////////////////////////7273template<typename T> __forceinline Vec4<T> operator +( const Vec4<T>& a ) { return Vec4<T>(+a.x, +a.y, +a.z, +a.w); }74template<typename T> __forceinline Vec4<T> operator -( const Vec4<T>& a ) { return Vec4<T>(-a.x, -a.y, -a.z, -a.w); }75template<typename T> __forceinline Vec4<T> abs ( const Vec4<T>& a ) { return Vec4<T>(abs (a.x), abs (a.y), abs (a.z), abs (a.w)); }76template<typename T> __forceinline Vec4<T> rcp ( const Vec4<T>& a ) { return Vec4<T>(rcp (a.x), rcp (a.y), rcp (a.z), rcp (a.w)); }77template<typename T> __forceinline Vec4<T> rsqrt ( const Vec4<T>& a ) { return Vec4<T>(rsqrt(a.x), rsqrt(a.y), rsqrt(a.z), rsqrt(a.w)); }78template<typename T> __forceinline Vec4<T> sqrt ( const Vec4<T>& a ) { return Vec4<T>(sqrt (a.x), sqrt (a.y), sqrt (a.z), sqrt (a.w)); }7980////////////////////////////////////////////////////////////////////////////////81/// Binary Operators82////////////////////////////////////////////////////////////////////////////////8384template<typename T> __forceinline Vec4<T> operator +( const Vec4<T>& a, const Vec4<T>& b ) { return Vec4<T>(a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w); }85template<typename T> __forceinline Vec4<T> operator -( const Vec4<T>& a, const Vec4<T>& b ) { return Vec4<T>(a.x - b.x, a.y - b.y, a.z - b.z, a.w - b.w); }86template<typename T> __forceinline Vec4<T> operator *( const Vec4<T>& a, const Vec4<T>& b ) { return Vec4<T>(a.x * b.x, a.y * b.y, a.z * b.z, a.w * b.w); }87template<typename T> __forceinline Vec4<T> operator *( const T& a, const Vec4<T>& b ) { return Vec4<T>(a * b.x, a * b.y, a * b.z, a * b.w); }88template<typename T> __forceinline Vec4<T> operator *( const Vec4<T>& a, const T& b ) { return Vec4<T>(a.x * b , a.y * b , a.z * b , a.w * b ); }89template<typename T> __forceinline Vec4<T> operator /( const Vec4<T>& a, const Vec4<T>& b ) { return Vec4<T>(a.x / b.x, a.y / b.y, a.z / b.z, a.w / b.w); }90template<typename T> __forceinline Vec4<T> operator /( const Vec4<T>& a, const T& b ) { return Vec4<T>(a.x / b , a.y / b , a.z / b , a.w / b ); }91template<typename T> __forceinline Vec4<T> operator /( const T& a, const Vec4<T>& b ) { return Vec4<T>(a / b.x, a / b.y, a / b.z, a / b.w); }9293template<typename T> __forceinline Vec4<T> min(const Vec4<T>& a, const Vec4<T>& b) { return Vec4<T>(min(a.x, b.x), min(a.y, b.y), min(a.z, b.z), min(a.w, b.w)); }94template<typename T> __forceinline Vec4<T> max(const Vec4<T>& a, const Vec4<T>& b) { return Vec4<T>(max(a.x, b.x), max(a.y, b.y), max(a.z, b.z), max(a.w, b.w)); }9596////////////////////////////////////////////////////////////////////////////////97/// Ternary Operators98////////////////////////////////////////////////////////////////////////////////99100template<typename T> __forceinline Vec4<T> madd ( const Vec4<T>& a, const Vec4<T>& b, const Vec4<T>& c) { return Vec4<T>( 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)); }101template<typename T> __forceinline Vec4<T> msub ( const Vec4<T>& a, const Vec4<T>& b, const Vec4<T>& c) { return Vec4<T>( 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)); }102template<typename T> __forceinline Vec4<T> nmadd ( const Vec4<T>& a, const Vec4<T>& b, const Vec4<T>& c) { return Vec4<T>(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)); }103template<typename T> __forceinline Vec4<T> nmsub ( const Vec4<T>& a, const Vec4<T>& b, const Vec4<T>& c) { return Vec4<T>(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)); }104105template<typename T> __forceinline Vec4<T> madd ( const T& a, const Vec4<T>& b, const Vec4<T>& c) { return Vec4<T>( madd(a,b.x,c.x), madd(a,b.y,c.y), madd(a,b.z,c.z), madd(a,b.w,c.w)); }106template<typename T> __forceinline Vec4<T> msub ( const T& a, const Vec4<T>& b, const Vec4<T>& c) { return Vec4<T>( msub(a,b.x,c.x), msub(a,b.y,c.y), msub(a,b.z,c.z), msub(a,b.w,c.w)); }107template<typename T> __forceinline Vec4<T> nmadd ( const T& a, const Vec4<T>& b, const Vec4<T>& c) { return Vec4<T>(nmadd(a,b.x,c.x),nmadd(a,b.y,c.y),nmadd(a,b.z,c.z),nmadd(a,b.w,c.w)); }108template<typename T> __forceinline Vec4<T> nmsub ( const T& a, const Vec4<T>& b, const Vec4<T>& c) { return Vec4<T>(nmsub(a,b.x,c.x),nmsub(a,b.y,c.y),nmsub(a,b.z,c.z),nmsub(a,b.w,c.w)); }109110////////////////////////////////////////////////////////////////////////////////111/// Assignment Operators112////////////////////////////////////////////////////////////////////////////////113114template<typename T> __forceinline Vec4<T>& operator +=( Vec4<T>& a, const Vec4<T>& b ) { a.x += b.x; a.y += b.y; a.z += b.z; a.w += b.w; return a; }115template<typename T> __forceinline Vec4<T>& operator -=( Vec4<T>& a, const Vec4<T>& b ) { a.x -= b.x; a.y -= b.y; a.z -= b.z; a.w -= b.w; return a; }116template<typename T> __forceinline Vec4<T>& operator *=( Vec4<T>& a, const T& b ) { a.x *= b ; a.y *= b ; a.z *= b ; a.w *= b ; return a; }117template<typename T> __forceinline Vec4<T>& operator /=( Vec4<T>& a, const T& b ) { a.x /= b ; a.y /= b ; a.z /= b ; a.w /= b ; return a; }118119////////////////////////////////////////////////////////////////////////////////120/// Reduction Operators121////////////////////////////////////////////////////////////////////////////////122123template<typename T> __forceinline T reduce_add( const Vec4<T>& a ) { return a.x + a.y + a.z + a.w; }124template<typename T> __forceinline T reduce_mul( const Vec4<T>& a ) { return a.x * a.y * a.z * a.w; }125template<typename T> __forceinline T reduce_min( const Vec4<T>& a ) { return min(a.x, a.y, a.z, a.w); }126template<typename T> __forceinline T reduce_max( const Vec4<T>& a ) { return max(a.x, a.y, a.z, a.w); }127128////////////////////////////////////////////////////////////////////////////////129/// Comparison Operators130////////////////////////////////////////////////////////////////////////////////131132template<typename T> __forceinline bool operator ==( const Vec4<T>& a, const Vec4<T>& b ) { return a.x == b.x && a.y == b.y && a.z == b.z && a.w == b.w; }133template<typename T> __forceinline bool operator !=( const Vec4<T>& a, const Vec4<T>& b ) { return a.x != b.x || a.y != b.y || a.z != b.z || a.w != b.w; }134template<typename T> __forceinline bool operator < ( const Vec4<T>& a, const Vec4<T>& b ) {135if (a.x != b.x) return a.x < b.x;136if (a.y != b.y) return a.y < b.y;137if (a.z != b.z) return a.z < b.z;138if (a.w != b.w) return a.w < b.w;139return false;140}141142////////////////////////////////////////////////////////////////////////////////143/// Shift Operators144////////////////////////////////////////////////////////////////////////////////145146template<typename T> __forceinline Vec4<T> shift_right_1( const Vec4<T>& a ) {147return Vec4<T>(shift_right_1(a.x),shift_right_1(a.y),shift_right_1(a.z),shift_right_1(a.w));148}149150////////////////////////////////////////////////////////////////////////////////151/// Euclidean Space Operators152////////////////////////////////////////////////////////////////////////////////153154template<typename T> __forceinline T dot ( const Vec4<T>& a, const Vec4<T>& b ) { return madd(a.x,b.x,madd(a.y,b.y,madd(a.z,b.z,a.w*b.w))); }155156template<typename T> __forceinline T length ( const Vec4<T>& a ) { return sqrt(dot(a,a)); }157template<typename T> __forceinline Vec4<T> normalize( const Vec4<T>& a ) { return a*rsqrt(dot(a,a)); }158template<typename T> __forceinline T distance ( const Vec4<T>& a, const Vec4<T>& b ) { return length(a-b); }159160////////////////////////////////////////////////////////////////////////////////161/// Select162////////////////////////////////////////////////////////////////////////////////163164template<typename T> __forceinline Vec4<T> select ( bool s, const Vec4<T>& t, const Vec4<T>& f ) {165return Vec4<T>(select(s,t.x,f.x),select(s,t.y,f.y),select(s,t.z,f.z),select(s,t.w,f.w));166}167168template<typename T> __forceinline Vec4<T> select ( const Vec4<bool>& s, const Vec4<T>& t, const Vec4<T>& f ) {169return Vec4<T>(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));170}171172template<typename T> __forceinline Vec4<T> select ( const typename T::Bool& s, const Vec4<T>& t, const Vec4<T>& f ) {173return Vec4<T>(select(s,t.x,f.x),select(s,t.y,f.y),select(s,t.z,f.z),select(s,t.w,f.w));174}175176template<typename T>177__forceinline Vec4<T> lerp(const Vec4<T>& v0, const Vec4<T>& v1, const T& t) {178return madd(Vec4<T>(T(1.0f)-t),v0,t*v1);179}180181////////////////////////////////////////////////////////////////////////////////182/// Output Operators183////////////////////////////////////////////////////////////////////////////////184185template<typename T> __forceinline embree_ostream operator<<(embree_ostream cout, const Vec4<T>& a) {186return cout << "(" << a.x << ", " << a.y << ", " << a.z << ", " << a.w << ")";187}188189////////////////////////////////////////////////////////////////////////////////190/// Default template instantiations191////////////////////////////////////////////////////////////////////////////////192193typedef Vec4<bool > Vec4b;194typedef Vec4<unsigned char> Vec4uc;195typedef Vec4<int > Vec4i;196typedef Vec4<float > Vec4f;197}198199#include "vec3ba.h"200#include "vec3ia.h"201#include "vec3fa.h"202203////////////////////////////////////////////////////////////////////////////////204/// SSE / AVX / MIC specializations205////////////////////////////////////////////////////////////////////////////////206207#if defined(__SSE__) || defined(__ARM_NEON)208#include "../simd/sse.h"209#endif210211#if defined __AVX__212#include "../simd/avx.h"213#endif214215#if defined __AVX512F__216#include "../simd/avx512.h"217#endif218219namespace embree220{221template<> __forceinline Vec4<float>::Vec4( const Vec3fx& a ) { x = a.x; y = a.y; z = a.z; w = a.w; }222223#if !defined(__SYCL_DEVICE_ONLY__)224225#if defined(__AVX__)226template<> __forceinline Vec4<vfloat4>::Vec4( const Vec3fx& a ) {227x = a.x; y = a.y; z = a.z; w = a.w;228}229#elif defined(__SSE__) || defined(__ARM_NEON)230template<> __forceinline Vec4<vfloat4>::Vec4( const Vec3fx& a ) {231const 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);232}233#endif234235#if defined(__AVX__)236template<> __forceinline Vec4<vfloat8>::Vec4( const Vec3fx& a ) {237x = a.x; y = a.y; z = a.z; w = a.w;238}239#endif240241#if defined(__AVX512F__)242template<> __forceinline Vec4<vfloat16>::Vec4( const Vec3fx& a ) : x(a.x), y(a.y), z(a.z), w(a.w) {}243#endif244245#else246247#if defined(__SSE__)248template<> __forceinline Vec4<vfloat4>::Vec4(const Vec3fx& a) {249x = a.x; y = a.y; z = a.z; w = a.w;250}251#endif252#if defined(__AVX__)253template<> __forceinline Vec4<vfloat8>::Vec4(const Vec3fx& a) {254x = a.x; y = a.y; z = a.z; w = a.w;255}256#endif257#if defined(__AVX512F__)258template<> __forceinline Vec4<vfloat16>::Vec4(const Vec3fx& a) {259x = a.x; y = a.y; z = a.z; w = a.w;260}261#endif262263#endif264}265266267268