17 #ifndef AGX_SIMD_VEC3_H 18 #define AGX_SIMD_VEC3_H 21 #include <agx/config/AGX_USE_SSE4.h> 22 #include <agx/config/AGX_USE_SSE.h> 23 #include <agx/config/AGX_USE_AVX.h> 30 #include <pmmintrin.h> 34 #include <smmintrin.h> 38 #include <immintrin.h> 66 Vec3T(
const __m128 _xyzw);
82 Vec3T scale(
const Vec3T& val)
const;
85 Vec3T scale(
const __m128 val)
const;
90 static Vec3T madd(
const Vec3T& a,
const Vec3T& b,
const Vec3T& c);
91 static Vec3T cross(
const Vec3T& lhs,
const Vec3T& rhs);
93 static agx::Real32 dot(
const Vec3T& lhs,
const Vec3T& rhs);
95 static agx::Real32 innerProduct(
const Vec3T& v01,
const Vec3T& v02,
const Vec3T& v11,
const Vec3T& v12);
125 Vec3T(
const __m256d _xyzw);
127 Vec3T(
const __m128d _xy,
const __m128d _zw);
144 Vec3T negate()
const;
145 Vec3T scale(
const Vec3T& val)
const;
147 #if AGX_USE_SSE() && !AGX_USE_AVX() 148 Vec3T scale(
const __m128d val)
const;
153 static Vec3T madd(
const Vec3T& a,
const Vec3T& b,
const Vec3T& c);
154 static Vec3T cross(
const Vec3T& lhs,
const Vec3T& rhs);
155 static agx::Real64 dot(
const Vec3T& lhs,
const Vec3T& rhs);
158 static agx::Real64 innerProduct(
const Vec3T& v01,
const Vec3T& v02,
const Vec3T& v11,
const Vec3T& v12);
172 typedef Vec3T<agx::Real32>
Vec3f;
173 typedef Vec3T<agx::Real64>
Vec3d;
174 typedef Vec3T<agx::Real>
Vec3;
176 std::ostream&
operator << ( std::ostream& output,
const Vec3d& v );
177 std::ostream&
operator << ( std::ostream& output,
const Vec3f& v );
205 xyzw = _mm256_cvtpd_ps(_mm256_load_pd(vec.
ptr()));
207 __m128 lower = _mm_cvtpd_ps(_mm_load_pd(vec.
ptr()));
208 __m128 upper = _mm_cvtpd_ps(_mm_load_pd(vec.
ptr()+2));
209 xyzw = _mm_or_ps(lower, _mm_castsi128_ps(_mm_slli_si128(_mm_castps_si128(upper), 8)));
237 _mm_store_ps( tmp, xyzw );
248 _mm_store_ps( tmp, xyzw );
260 _mm_store_ps( tmp, xyzw );
270 _mm_store_ps(target.
ptr(), xyzw);
279 const __m128 SIGNMASK = _mm_set1_ps(-0.0f);
280 return Vec3T(_mm_andnot_ps(xyzw, SIGNMASK));
282 return Vec3T(std::abs(xyzw));
289 const __m128 SIGNMASK = _mm_set1_ps(-0.0f);
290 return Vec3T(_mm_xor_ps(xyzw, SIGNMASK));
299 return Vec3T(_mm_mul_ps(xyzw, val.xyzw));
309 return Vec3T(_mm_mul_ps(xyzw, val));
316 return scale(_mm_set1_ps(val));
318 return Vec3T(xyzw * val);
322 AGX_FORCE_INLINE Vec3f Vec3T<agx::Real32>::madd(
const Vec3T& a,
const Vec3T& b,
const Vec3T& c)
325 return Vec3T( _mm_add_ps( a.xyzw, _mm_mul_ps( b.xyzw, c.xyzw ) ) );
334 const agx::UInt32 shuffle_3021 = _MM_SHUFFLE(3, 0, 2, 1);
335 const agx::UInt32 shuffle_3102 = _MM_SHUFFLE(3, 1, 0, 2);
337 const __m128 a = _mm_mul_ps(_mm_shuffle_ps(lhs.xyzw, lhs.xyzw, shuffle_3021), _mm_shuffle_ps(rhs.xyzw, rhs.xyzw, shuffle_3102));
338 const __m128 b = _mm_mul_ps(_mm_shuffle_ps(lhs.xyzw, lhs.xyzw, shuffle_3102), _mm_shuffle_ps(rhs.xyzw, rhs.xyzw, shuffle_3021));
342 _mm_store_ps( tmp, _mm_sub_ps(a,b) );
346 return Vec3T(_mm_sub_ps(a,b));
348 return Vec3T(lhs.xyzw ^ rhs.xyzw);
355 const int mask = 0x71;
357 _mm_store_ps( output, _mm_dp_ps(lhs.xyzw, rhs.xyzw, mask) );
363 _mm_store_ps( scalarLhs, lhs.xyzw );
364 _mm_store_ps( scalarRhs, rhs.xyzw );
366 return scalarLhs[0] * scalarRhs[0] + scalarLhs[1] * scalarRhs[1] + scalarLhs[2] * scalarRhs[2];
368 return lhs.xyzw * rhs.xyzw;
375 return dot(*
this, *
this);
386 return _mm_cvtss_f32(_mm_sqrt_ss(_mm_dp_ps(xyzw, xyzw, 0x71)));
388 return std::sqrt(length2());
397 AGX_FORCE_INLINE agx::Real32 Vec3T<agx::Real32>::innerProduct(
const Vec3T& v01,
const Vec3T& v02,
const Vec3T& v11,
const Vec3T& v12)
402 __m128 tmp1 = _mm_mul_ps( v01.xyzw, v11.xyzw );
403 __m128 tmp2 = _mm_mul_ps( v02.xyzw, v12.xyzw );
405 tmp1 = _mm_add_ps( tmp1, tmp2 );
407 _mm_store_ps( output, tmp1 );
409 return output[0]+output[1]+output[2];
411 return v01.xyzw * v11.xyzw + v02.xyzw * v12.xyzw;
461 const __m128 lower = _mm_load_ps(vec.
ptr());
462 xy = _mm_cvtps_pd(lower);
464 const __m128 upper = _mm_castsi128_ps(_mm_srli_si128(_mm_castps_si128(lower), 8));
465 zw = _mm_cvtps_pd(upper);
492 _mm256_store_pd( tmp, xyzw );
496 _mm_store_pd( tmp, xy );
507 _mm256_store_pd( tmp, xyzw );
511 _mm_store_pd( tmp, xy );
522 _mm256_store_pd( tmp, xyzw );
526 _mm_store_pd( tmp, zw );
536 _mm256_store_pd(target.
ptr(), xyzw);
538 _mm_store_pd(target.
ptr() , xy);
539 _mm_store_pd(target.
ptr()+2, zw);
548 _mm_store_ps(target.
ptr(), _mm256_cvtpd_ps(xyzw));
550 __m128 lower = _mm_cvtpd_ps(xy);
551 __m128 upper = _mm_cvtpd_ps(zw);
552 __m128 result = _mm_or_ps(lower, _mm_castsi128_ps(_mm_slli_si128(_mm_castps_si128(upper), 8)));
553 _mm_store_ps(target.
ptr(), result);
562 const __m256d SIGNMASK = _mm256_set1_pd(-0.0);
563 return Vec3T(_mm256_andnot_pd(xyzw, SIGNMASK));
565 const __m128d SIGNMASK = _mm_set1_pd(-0.0);
566 return Vec3T(_mm_andnot_pd(xy, SIGNMASK), _mm_andnot_pd(zw, SIGNMASK));
568 return Vec3T(std::abs(xyzw));
575 const __m256d SIGNMASK = _mm256_set1_pd(-0.0);
576 return Vec3T(_mm256_xor_pd(xyzw, SIGNMASK));
578 const __m128d SIGNMASK = _mm_set1_pd(-0.0);
579 return Vec3T(_mm_xor_pd(xy, SIGNMASK), _mm_xor_pd(zw, SIGNMASK));
588 return Vec3T(_mm256_mul_pd(xyzw, val.xyzw));
590 return Vec3T(_mm_mul_pd(xy, val.xy), _mm_mul_pd(zw, val.zw));
596 #if AGX_USE_SSE() && !AGX_USE_AVX() 599 return Vec3T(_mm_mul_pd(xy, val), _mm_mul_pd(zw, val));
606 return scale(Vec3T(val));
608 return scale(_mm_set1_pd(val));
610 return Vec3T(xyzw * val);
614 AGX_FORCE_INLINE Vec3d Vec3T<agx::Real64>::madd(
const Vec3T& a,
const Vec3T& b,
const Vec3T& c)
617 return Vec3T( _mm256_add_pd( a.xyzw, _mm256_mul_pd( b.xyzw, c.xyzw ) ) );
619 const __m128d xy = _mm_add_pd( a.xy, _mm_mul_pd( b.xy, c.xy ) );
620 const __m128d zw = _mm_add_pd( a.zw, _mm_mul_pd( b.zw, c.zw ) );
622 return Vec3T(xy, zw);
632 #error "This needs to be fixed!" 634 const agx::UInt32 shuffle_3021 = _MM_SHUFFLE(3, 0, 2, 1);
635 const agx::UInt32 shuffle_3102 = _MM_SHUFFLE(3, 1, 0, 2);
637 const __m256d a = _mm256_mul_pd(_mm256_shuffle_pd(lhs.xyzw, lhs.xyzw, shuffle_3021), _mm256_shuffle_pd(rhs.xyzw, rhs.xyzw, shuffle_3102));
638 const __m256d b = _mm256_mul_pd(_mm256_shuffle_pd(lhs.xyzw, lhs.xyzw, shuffle_3102), _mm256_shuffle_pd(rhs.xyzw, rhs.xyzw, shuffle_3021));
642 _mm256_store_pd( tmp, _mm256_sub_pd(a,b) );
646 return Vec3T(_mm256_sub_pd(a,b));
648 const __m128d SIGN_NP = _mm_set_pd ( 0.0 , -0.0 );
652 __m128d l1 = _mm_mul_pd ( _mm_unpacklo_pd ( lhs.zw , lhs.zw ) , rhs.xy );
655 __m128d l2 = _mm_mul_pd ( _mm_unpacklo_pd ( rhs.zw , rhs.zw ) , lhs.xy );
656 __m128d m1 = _mm_sub_pd ( l1 , l2 );
657 m1 = _mm_shuffle_pd ( m1 , m1 , 1 );
658 m1 = _mm_xor_pd ( m1 , SIGN_NP );
661 l1 = _mm_mul_pd ( lhs.xy , _mm_shuffle_pd ( rhs.xy , rhs.xy , 1 ) );
663 __m128d m2 = _mm_sub_sd ( l1 , _mm_unpackhi_pd ( l1 , l1 ) );
666 m2 = _mm_move_sd(_mm_setzero_pd(), m2);
668 return Vec3T(m1, m2);
670 return Vec3T(lhs.xyzw ^ rhs.xyzw);
684 __m128d lhsXY = _mm256_extractf128_pd( lhs.xyzw, 0 );
685 __m128d lhsZW = _mm256_extractf128_pd( lhs.xyzw, 1 );
686 __m128d rhsXY = _mm256_extractf128_pd( rhs.xyzw, 0 );
687 __m128d rhsZW = _mm256_extractf128_pd( rhs.xyzw, 1 );
689 const int mask = 0x31;
692 const __m128d xyDot = _mm_dp_pd(lhsXY, rhsXY, mask);
693 const __m128d zDot = _mm_mul_pd(lhsZW, rhsZW);
695 _mm_store_pd( output, _mm_add_pd(xyDot, zDot));
699 __m256d a = _mm256_mul_pd( lhs.xyzw, rhs.xyzw );
700 __m256d b = _mm256_hadd_pd( a, a );
701 __m128d hi128 = _mm256_extractf128_pd( b, 1 );
702 __m128d dotproduct = _mm_add_pd( (__m128d)b, hi128 );
705 _mm_store_pd( output, dotproduct );
710 const int mask = 0x31;
713 const __m128d xyDot = _mm_dp_pd(lhs.xy, rhs.xy, mask);
714 const __m128d zDot = _mm_mul_pd(lhs.zw, rhs.zw);
716 _mm_store_pd( output, _mm_add_pd(xyDot, zDot));
722 _mm_store_pd( scalarLhs, lhs.xy );
723 _mm_store_pd( scalarLhs + 2, lhs.zw );
724 _mm_store_pd( scalarRhs, rhs.xy );
725 _mm_store_pd( scalarRhs + 2, rhs.zw );
727 return scalarLhs[0] * scalarRhs[0] + scalarLhs[1] * scalarRhs[1] + scalarLhs[2] * scalarRhs[2];
729 return lhs.xyzw * rhs.xyzw;
735 return dot(*
this, *
this);
740 #if AGX_USE_AVX() && 0 743 _mm256_store_pd(output, _mm256_sqrt_pd(_mm256_dp_pd(xyzw, xyzw, 0x71)));
746 return std::sqrt(this->length2());
753 AGX_FORCE_INLINE agx::Real64 Vec3T<agx::Real64>::innerProduct(
const Vec3T& v01,
const Vec3T& v02,
const Vec3T& v11,
const Vec3T& v12)
759 __m256d tmp1 = _mm256_mul_pd( v01.xyzw, v11.xyzw );
760 __m256d tmp2 = _mm256_mul_pd( v02.xyzw, v12.xyzw );
762 tmp1 = _mm256_add_pd( tmp1, tmp2 );
764 _mm256_store_pd( output, tmp1 );
766 return output[0]+output[1]+output[2];
776 __m128d tmp1 = _mm_mul_pd( v01.xy, v11.xy );
777 __m128d tmp2 = _mm_mul_pd( v01.zw, v11.zw );
779 __m128d tmp3 = _mm_mul_pd( v02.xy, v12.xy );
780 __m128d tmp4 = _mm_mul_pd( v02.zw, v12.zw );
783 tmp1 = _mm_add_pd( tmp1, tmp2 );
784 tmp3 = _mm_add_pd( tmp3, tmp4 );
788 tmp1 = _mm_hadd_pd( tmp1, tmp3 );
789 _mm_store_pd( output, tmp1 );
791 return output[0] + output[1];
793 return v01.xyzw * v11.xyzw + v02.xyzw * v12.xyzw;
803 return output << tmp;
810 return output << tmp;
Vec3T< Real > Vec3
The object holding 3 dimensional vectors and providing basic arithmetic.
Real length2() const
Length squared of the vector = vec .
std::ostream & operator<<(std::ostream &stream, const AtomicValueT< T > &value)
const Vec3T cross(const Vec3T &rhs) const
Cross product method.
GLenum GLuint GLenum GLsizei length
#define AGX_ALIGNED(t, a)
std::ostream & operator<<(std::ostream &o, const agx::Vec6 &v)
Vec3T()
Default constructor.
Namespace agx contains the dynamics/math part of the AGX Dynamics API.
T absolute(T v)
return the absolute value.
Real length() const
Length of the vector = sqrt( vec .
static Vec3T mul(const Vec3T &lhs, const Vec3T &rhs)
Element-wise-multiplication.
#define DOXYGEN_END_INTERNAL_BLOCK()
#define DOXYGEN_START_INTERNAL_BLOCK()