AGXDynamics  2.31.1.0
IMD/Vec3.h
Go to the documentation of this file.
1 /*
2 Copyright 2007-2021. Algoryx Simulation AB.
3 
4 All AGX source code, intellectual property, documentation, sample code,
5 tutorials, scene files and technical white papers, are copyrighted, proprietary
6 and confidential material of Algoryx Simulation AB. You may not download, read,
7 store, distribute, publish, copy or otherwise disseminate, use or expose this
8 material unless having a written signed agreement with Algoryx Simulation AB, or having been
9 advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10 valid commercial license from Algoryx Simulation AB.
11 
12 Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13 from using this software, unless otherwise stated in written agreements with
14 Algoryx Simulation AB.
15 */
16 
17 #ifndef AGX_SIMD_VEC3_H
18 #define AGX_SIMD_VEC3_H
19 
20 
21 #include <agx/config/AGX_USE_SSE4.h>
22 #include <agx/config/AGX_USE_SSE.h>
23 #include <agx/config/AGX_USE_AVX.h>
24 #include <agx/Integer.h>
25 #include <agx/Real.h>
26 #include <agx/Vec3.h>
27 #include <iosfwd>
28 
29 #if AGX_USE_SSE()
30 #include <pmmintrin.h>
31 #endif
32 
33 #if AGX_USE_SSE4()
34 #include <smmintrin.h>
35 #endif
36 
37 #if AGX_USE_AVX()
38 #include <immintrin.h>
39 #endif
40 
42 namespace agxSIMD
43 {
44  // Vec3 base class, explicit specializations for Real32 and Real64
45  template <typename T>
46  class Vec3T
47  {
48  };
49 
50  // 32bit real specialization
51  template <>
52  class Vec3T<agx::Real32>
53  {
54  public:
55  typedef agx::Real32 RealT;
56  typedef agx::Real32 Type;
57  static const agx::UInt ALIGNMENT = 16;
58 
59  public:
60 
61  Vec3T();
62  Vec3T(agx::Real32 val);
63  Vec3T(agx::Real32 x, agx::Real32 y, agx::Real32 z);
64 
65 #if AGX_USE_SSE()
66  Vec3T(const __m128 _xyzw);
67 #endif
68 
69  explicit Vec3T(const agx::Vec3f& vec);
70  explicit Vec3T(const agx::Vec3d&);
71 
72  agx::Real32 x() const;
73  agx::Real32 y() const;
74  agx::Real32 z() const;
75 
76  agx::Real32 length() const;
77  agx::Real32 length2() const;
78 
79  void store(agx::Vec3f& target) const;
80  Vec3T absolute() const;
81  Vec3T negate() const;
82  Vec3T scale(const Vec3T& val) const;
83 
84 #if AGX_USE_SSE()
85  Vec3T scale(const __m128 val) const;
86 #endif
87 
88  Vec3T scale(agx::Real32 val) const;
89 
90  static Vec3T madd(const Vec3T& a, const Vec3T& b, const Vec3T& c);
91  static Vec3T cross(const Vec3T& lhs, const Vec3T& rhs);
92 
93  static agx::Real32 dot(const Vec3T& lhs, const Vec3T& rhs);
94 
95  static agx::Real32 innerProduct(const Vec3T& v01, const Vec3T& v02, const Vec3T& v11, const Vec3T& v12);
96 
97  public:
98 #if AGX_USE_SSE()
99  __m128 xyzw;
100 #else
101  agx::Vec3f xyzw;
102 #endif
103  };
104 
105  // 64bit real specialization
106  template <>
107  class Vec3T<agx::Real64>
108  {
109  public:
110  typedef agx::Real64 RealT;
111  typedef agx::Real64 Type;
112 
113 #if AGX_USE_AVX()
114  static const agx::UInt ALIGNMENT = 32;
115 #else
116  static const agx::UInt ALIGNMENT = 16;
117 #endif
118 
119  public:
120  Vec3T();
121  Vec3T(agx::Real64 val);
122  Vec3T(agx::Real64 x, agx::Real64 y, agx::Real64 z);
123 
124 #if AGX_USE_AVX()
125  Vec3T(const __m256d _xyzw);
126 #elif AGX_USE_SSE()
127  Vec3T(const __m128d _xy, const __m128d _zw);
128 #endif
129 
130  explicit Vec3T(const agx::Vec3d& vec);
131  explicit Vec3T(const agx::Vec3f& vec);
132 
133  agx::Real64 x() const;
134  agx::Real64 y() const;
135  agx::Real64 z() const;
136 
137  agx::Real64 length() const;
138  agx::Real64 length2() const;
139 
140  void store(agx::Vec3d& target) const;
141  void store(agx::Vec3f& target) const;
142 
143  Vec3T absolute() const;
144  Vec3T negate() const;
145  Vec3T scale(const Vec3T& val) const;
146 
147 #if AGX_USE_SSE() && !AGX_USE_AVX()
148  Vec3T scale(const __m128d val) const;
149 #endif
150 
151  Vec3T scale(agx::Real64 val) const;
152 
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);
156 
157 
158  static agx::Real64 innerProduct(const Vec3T& v01, const Vec3T& v02, const Vec3T& v11, const Vec3T& v12);
159 
160  public:
161 #if AGX_USE_AVX()
162  __m256d xyzw;
163 #elif AGX_USE_SSE()
164  __m128d xy;
165  __m128d zw;
166 #else
167  agx::Vec3d xyzw;
168 #endif
169  };
170 
171 
172  typedef Vec3T<agx::Real32> Vec3f;
173  typedef Vec3T<agx::Real64> Vec3d;
174  typedef Vec3T<agx::Real> Vec3;
175 
176  std::ostream& operator << ( std::ostream& output, const Vec3d& v );
177  std::ostream& operator << ( std::ostream& output, const Vec3f& v );
178 
179 
180 
181  /* Implementation */
182 
183  AGX_FORCE_INLINE Vec3T<agx::Real32>::Vec3T() : Vec3T(0.0f)
184  {}
185 
186 #if AGX_USE_SSE()
187  AGX_FORCE_INLINE Vec3T<agx::Real32>::Vec3T(agx::Real32 val) : xyzw(_mm_setr_ps(val, val, val, 0))
188  {
189  }
190 
191  AGX_FORCE_INLINE Vec3T<agx::Real32>::Vec3T(agx::Real32 x, agx::Real32 y, agx::Real32 z) : xyzw(_mm_setr_ps(x, y, z, 0))
192  {
193  }
194 
195  AGX_FORCE_INLINE Vec3T<agx::Real32>::Vec3T(const __m128 _xyzw) : xyzw(_xyzw)
196  {}
197 
198  AGX_FORCE_INLINE Vec3T<agx::Real32>::Vec3T(const agx::Vec3f& vec) : xyzw(_mm_load_ps(vec.ptr()))
199  {
200  }
201 
203  {
204  #if AGX_USE_AVX()
205  xyzw = _mm256_cvtpd_ps(_mm256_load_pd(vec.ptr()));
206  #else
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)));
210  #endif
211  }
212 
213 #else
214 
216  {
217  }
218 
220  {
221  }
222 
224  {
225  }
226 
228  {
229  }
230 #endif
231 
232 
234  {
235 #if AGX_USE_SSE()
236  AGX_ALIGNED( agx::Real32, 16 ) tmp[4];
237  _mm_store_ps( tmp, xyzw );
238  return tmp[0];
239 #else
240  return xyzw.x();
241 #endif
242  }
243 
245  {
246 #if AGX_USE_SSE()
247  AGX_ALIGNED( agx::Real32, 16 ) tmp[4];
248  _mm_store_ps( tmp, xyzw );
249  return tmp[1];
250 #else
251  return xyzw.y();
252 #endif
253  }
254 
255 
257  {
258 #if AGX_USE_SSE()
259  AGX_ALIGNED( agx::Real32, 16 ) tmp[4];
260  _mm_store_ps( tmp, xyzw );
261  return tmp[2];
262 #else
263  return xyzw.z();
264 #endif
265  }
266 
267  AGX_FORCE_INLINE void Vec3T<agx::Real32>::store(agx::Vec3f& target) const
268  {
269 #if AGX_USE_SSE()
270  _mm_store_ps(target.ptr(), xyzw);
271 #else
272  target = xyzw;
273 #endif
274  }
275 
277  {
278 #if AGX_USE_SSE()
279  const __m128 SIGNMASK = _mm_set1_ps(-0.0f); // -0.0f = 1 << 31 // _mm_castsi128_ps(_mm_set1_epi32(0x80000000));
280  return Vec3T(_mm_andnot_ps(xyzw, SIGNMASK));
281 #else
282  return Vec3T(std::abs(xyzw));
283 #endif
284  }
285 
286  AGX_FORCE_INLINE Vec3f Vec3T<agx::Real32>::negate() const
287  {
288 #if AGX_USE_SSE()
289  const __m128 SIGNMASK = _mm_set1_ps(-0.0f); // -0.0f = 1 << 31 // _mm_castsi128_ps(_mm_set1_epi32(0x80000000));
290  return Vec3T(_mm_xor_ps(xyzw, SIGNMASK));
291 #else
292  return Vec3T(-xyzw);
293 #endif
294  }
295 
296  AGX_FORCE_INLINE Vec3f Vec3T<agx::Real32>::scale(const Vec3T& val) const
297  {
298 #if AGX_USE_SSE()
299  return Vec3T(_mm_mul_ps(xyzw, val.xyzw));
300 #else
301  return Vec3T(agx::Vec3f::mul(xyzw, val.xyzw));
302 #endif
303  }
304 
305 
306 #if AGX_USE_SSE()
307  AGX_FORCE_INLINE Vec3f Vec3T<agx::Real32>::scale(const __m128 val) const
308  {
309  return Vec3T(_mm_mul_ps(xyzw, val));
310  }
311 #endif
312 
313  AGX_FORCE_INLINE Vec3f Vec3T<agx::Real32>::scale(agx::Real32 val) const
314  {
315 #if AGX_USE_SSE()
316  return scale(_mm_set1_ps(val));
317 #else
318  return Vec3T(xyzw * val);
319 #endif
320  }
321 
322  AGX_FORCE_INLINE Vec3f Vec3T<agx::Real32>::madd(const Vec3T& a, const Vec3T& b, const Vec3T& c)
323  {
324 #if AGX_USE_SSE()
325  return Vec3T( _mm_add_ps( a.xyzw, _mm_mul_ps( b.xyzw, c.xyzw ) ) );
326 #else
327  return Vec3T(a.xyzw + agx::Vec3f::mul(b.xyzw, c.xyzw));
328 #endif
329  }
330 
331  AGX_FORCE_INLINE Vec3f Vec3T<agx::Real32>::cross(const Vec3T& lhs, const Vec3T& rhs)
332  {
333 #if AGX_USE_SSE()
334  const agx::UInt32 shuffle_3021 = _MM_SHUFFLE(3, 0, 2, 1);
335  const agx::UInt32 shuffle_3102 = _MM_SHUFFLE(3, 1, 0, 2);
336 
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));
339 
340  #ifdef AGX_DEBUG
341  AGX_ALIGNED( agx::Real32, 16 ) tmp[4];
342  _mm_store_ps( tmp, _mm_sub_ps(a,b) );
343  agxAssert(tmp[3] == 0);
344  #endif
345 
346  return Vec3T(_mm_sub_ps(a,b));
347 #else
348  return Vec3T(lhs.xyzw ^ rhs.xyzw);
349 #endif
350  }
351 
352  AGX_FORCE_INLINE agx::Real32 Vec3T<agx::Real32>::dot(const Vec3T& lhs, const Vec3T& rhs)
353  {
354 #if AGX_USE_SSE4()
355  const int mask = 0x71;
356  AGX_ALIGNED( agx::Real32, 16 ) output[4];
357  _mm_store_ps( output, _mm_dp_ps(lhs.xyzw, rhs.xyzw, mask) );
358  return output[0];
359 #elif AGX_USE_SSE()
360  AGX_ALIGNED( agx::Real32, 16 ) scalarLhs[4];
361  AGX_ALIGNED( agx::Real32, 16 ) scalarRhs[4];
362 
363  _mm_store_ps( scalarLhs, lhs.xyzw );
364  _mm_store_ps( scalarRhs, rhs.xyzw );
365 
366  return scalarLhs[0] * scalarRhs[0] + scalarLhs[1] * scalarRhs[1] + scalarLhs[2] * scalarRhs[2];
367 #else
368  return lhs.xyzw * rhs.xyzw;
369 #endif
370  }
371 
373  {
374 #if AGX_USE_SSE()
375  return dot(*this, *this);
376 #else
377  return (agx::Real32)xyzw.length2();
378 #endif
379  }
380 
382  {
383 #if AGX_USE_SSE()
384 
385  #if AGX_USE_SSE4()
386  return _mm_cvtss_f32(_mm_sqrt_ss(_mm_dp_ps(xyzw, xyzw, 0x71)));
387  #else
388  return std::sqrt(length2());
389  #endif
390 
391 #else
392  return (agx::Real32)xyzw.length();
393 #endif
394  }
395 
396 
397  AGX_FORCE_INLINE agx::Real32 Vec3T<agx::Real32>::innerProduct(const Vec3T& v01, const Vec3T& v02, const Vec3T& v11, const Vec3T& v12)
398  {
399 #if AGX_USE_SSE()
400  AGX_ALIGNED( agx::Real32, 16 ) output[4];
401 
402  __m128 tmp1 = _mm_mul_ps( v01.xyzw, v11.xyzw );
403  __m128 tmp2 = _mm_mul_ps( v02.xyzw, v12.xyzw );
404 
405  tmp1 = _mm_add_ps( tmp1, tmp2 );
406 
407  _mm_store_ps( output, tmp1 );
408 
409  return output[0]+output[1]+output[2];
410 #else
411  return v01.xyzw * v11.xyzw + v02.xyzw * v12.xyzw;
412 #endif
413  }
414 
415 
416 
418 
419 #if AGX_USE_AVX()
420 
422  {}
423 
424  AGX_FORCE_INLINE Vec3T<agx::Real64>::Vec3T(agx::Real64 val) : xyzw(_mm256_setr_pd(val, val, val, 0))
425  {}
426 
427  AGX_FORCE_INLINE Vec3T<agx::Real64>::Vec3T(agx::Real64 x, agx::Real64 y, agx::Real64 z) : xyzw(_mm256_setr_pd(x, y, z, 0))
428  {
429  }
430 
431  AGX_FORCE_INLINE Vec3T<agx::Real64>::Vec3T(const __m256d _xyzw) : xyzw(_xyzw)
432  {}
433 
434  AGX_FORCE_INLINE Vec3T<agx::Real64>::Vec3T(const agx::Vec3d& vec) : xyzw(_mm256_load_pd(vec.ptr()))
435  {}
436 
437  AGX_FORCE_INLINE Vec3T<agx::Real64>::Vec3T(const agx::Vec3f& vec) : xyzw(_mm256_cvtps_pd(_mm_load_ps(vec.ptr())))
438  {}
439 
440 #elif AGX_USE_SSE()
441 
443  {}
444 
445  AGX_FORCE_INLINE Vec3T<agx::Real64>::Vec3T(agx::Real64 val) : xy(_mm_set1_pd(val)), zw(_mm_setr_pd(val, 0))
446  {}
447 
448  AGX_FORCE_INLINE Vec3T<agx::Real64>::Vec3T(agx::Real64 x, agx::Real64 y, agx::Real64 z) : xy(_mm_setr_pd(x, y)), zw(_mm_setr_pd(z, 0))
449  {
450  }
451 
452 
453  AGX_FORCE_INLINE Vec3T<agx::Real64>::Vec3T(const __m128d _xy, const __m128d _zw) : xy(_xy), zw(_zw)
454  {}
455 
456  AGX_FORCE_INLINE Vec3T<agx::Real64>::Vec3T(const agx::Vec3d& vec) : xy(_mm_load_pd(vec.ptr())), zw(_mm_load_pd(vec.ptr()+2))
457  {}
458 
460  {
461  const __m128 lower = _mm_load_ps(vec.ptr());
462  xy = _mm_cvtps_pd(lower);
463 
464  const __m128 upper = _mm_castsi128_ps(_mm_srli_si128(_mm_castps_si128(lower), 8));
465  zw = _mm_cvtps_pd(upper);
466  }
467 
468 #else
469 
471  {}
472 
474  {}
475 
477  {
478  }
479 
481  {}
482 
484  {}
485 
486 #endif
487 
489  {
490 #if AGX_USE_AVX()
491  AGX_ALIGNED( agx::Real64, 32 ) tmp[4];
492  _mm256_store_pd( tmp, xyzw );
493  return tmp[0];
494 #elif AGX_USE_SSE()
495  AGX_ALIGNED( agx::Real64, 16 ) tmp[2];
496  _mm_store_pd( tmp, xy );
497  return tmp[0];
498 #else
499  return xyzw.x();
500 #endif
501  }
502 
504  {
505 #if AGX_USE_AVX()
506  AGX_ALIGNED( agx::Real64, 32 ) tmp[4];
507  _mm256_store_pd( tmp, xyzw );
508  return tmp[1];
509 #elif AGX_USE_SSE()
510  AGX_ALIGNED( agx::Real64, 16 ) tmp[2];
511  _mm_store_pd( tmp, xy );
512  return tmp[1];
513 #else
514  return xyzw.y();
515 #endif
516  }
517 
519  {
520 #if AGX_USE_AVX()
521  AGX_ALIGNED( agx::Real64, 32 ) tmp[4];
522  _mm256_store_pd( tmp, xyzw );
523  return tmp[2];
524 #elif AGX_USE_SSE()
525  AGX_ALIGNED( agx::Real64, 16 ) tmp[2];
526  _mm_store_pd( tmp, zw );
527  return tmp[0];
528 #else
529  return xyzw.z();
530 #endif
531  }
532 
533  AGX_FORCE_INLINE void Vec3T<agx::Real64>::store(agx::Vec3d& target) const
534  {
535 #if AGX_USE_AVX()
536  _mm256_store_pd(target.ptr(), xyzw);
537 #elif AGX_USE_SSE()
538  _mm_store_pd(target.ptr() , xy);
539  _mm_store_pd(target.ptr()+2, zw);
540 #else
541  target = xyzw;
542 #endif
543  }
544 
545  AGX_FORCE_INLINE void Vec3T<agx::Real64>::store(agx::Vec3f& target) const
546  {
547 #if AGX_USE_AVX()
548  _mm_store_ps(target.ptr(), _mm256_cvtpd_ps(xyzw));
549 #elif AGX_USE_SSE()
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);
554 #else
555  target = xyzw;
556 #endif
557  }
558 
560  {
561 #if AGX_USE_AVX()
562  const __m256d SIGNMASK = _mm256_set1_pd(-0.0); // -0.0 = 1 << 63
563  return Vec3T(_mm256_andnot_pd(xyzw, SIGNMASK));
564 #elif AGX_USE_SSE()
565  const __m128d SIGNMASK = _mm_set1_pd(-0.0); // -0.0 = 1 << 63
566  return Vec3T(_mm_andnot_pd(xy, SIGNMASK), _mm_andnot_pd(zw, SIGNMASK));
567 #else
568  return Vec3T(std::abs(xyzw));
569 #endif
570  }
571 
572  AGX_FORCE_INLINE Vec3d Vec3T<agx::Real64>::negate() const
573  {
574 #if AGX_USE_AVX()
575  const __m256d SIGNMASK = _mm256_set1_pd(-0.0); // -0.0 = 1 << 63
576  return Vec3T(_mm256_xor_pd(xyzw, SIGNMASK));
577 #elif AGX_USE_SSE()
578  const __m128d SIGNMASK = _mm_set1_pd(-0.0); // -0.0 = 1 << 63
579  return Vec3T(_mm_xor_pd(xy, SIGNMASK), _mm_xor_pd(zw, SIGNMASK));
580 #else
581  return Vec3T(-xyzw);
582 #endif
583  }
584 
585  AGX_FORCE_INLINE Vec3d Vec3T<agx::Real64>::scale(const Vec3T& val) const
586  {
587 #if AGX_USE_AVX()
588  return Vec3T(_mm256_mul_pd(xyzw, val.xyzw));
589 #elif AGX_USE_SSE()
590  return Vec3T(_mm_mul_pd(xy, val.xy), _mm_mul_pd(zw, val.zw));
591 #else
592  return Vec3T(agx::Vec3d::mul(xyzw, val.xyzw));
593 #endif
594  }
595 
596 #if AGX_USE_SSE() && !AGX_USE_AVX()
597  AGX_FORCE_INLINE Vec3d Vec3T<agx::Real64>::scale(const __m128d val) const
598  {
599  return Vec3T(_mm_mul_pd(xy, val), _mm_mul_pd(zw, val));
600  }
601 #endif
602 
603  AGX_FORCE_INLINE Vec3d Vec3T<agx::Real64>::scale(agx::Real64 val) const
604  {
605 #if AGX_USE_AVX()
606  return scale(Vec3T(val));
607 #elif AGX_USE_SSE()
608  return scale(_mm_set1_pd(val));
609 #else
610  return Vec3T(xyzw * val);
611 #endif
612  }
613 
614  AGX_FORCE_INLINE Vec3d Vec3T<agx::Real64>::madd(const Vec3T& a, const Vec3T& b, const Vec3T& c)
615  {
616 #if AGX_USE_AVX()
617  return Vec3T( _mm256_add_pd( a.xyzw, _mm256_mul_pd( b.xyzw, c.xyzw ) ) );
618 #elif AGX_USE_SSE()
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 ) );
621 
622  return Vec3T(xy, zw);
623 #else
624  return Vec3T(a.xyzw + agx::Vec3d::mul(b.xyzw, c.xyzw));
625 #endif
626  }
627 
628 
629  AGX_FORCE_INLINE Vec3d Vec3T<agx::Real64>::cross(const Vec3T& lhs, const Vec3T& rhs)
630  {
631 #if AGX_USE_AVX()
632 #error "This needs to be fixed!"
633  // NOTE: Disabled, permute256 semantics are weird :/
634  const agx::UInt32 shuffle_3021 = _MM_SHUFFLE(3, 0, 2, 1);
635  const agx::UInt32 shuffle_3102 = _MM_SHUFFLE(3, 1, 0, 2);
636 
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));
639 
640  #ifdef AGX_DEBUG
641  AGX_ALIGNED( agx::Real64, 32 ) tmp[4];
642  _mm256_store_pd( tmp, _mm256_sub_pd(a,b) );
643  agxAssert(tmp[3] == 0);
644  #endif
645 
646  return Vec3T(_mm256_sub_pd(a,b));
647 #elif AGX_USE_SSE()
648  const __m128d SIGN_NP = _mm_set_pd ( 0.0 , -0.0 );
649 
650 
651  // lhs.z * rhs.x, lhs.z * rhs.y
652  __m128d l1 = _mm_mul_pd ( _mm_unpacklo_pd ( lhs.zw , lhs.zw ) , rhs.xy );
653 
654  // rhs.z * lhs.x, rhs.z * lhs.y
655  __m128d l2 = _mm_mul_pd ( _mm_unpacklo_pd ( rhs.zw , rhs.zw ) , lhs.xy );
656  __m128d m1 = _mm_sub_pd ( l1 , l2 ); // l1 - l2
657  m1 = _mm_shuffle_pd ( m1 , m1 , 1 ); // switch the elements
658  m1 = _mm_xor_pd ( m1 , SIGN_NP ); // change the sign of the first element
659 
660  // lhs.x * rhs.y, lhs.y * rhs.x
661  l1 = _mm_mul_pd ( lhs.xy , _mm_shuffle_pd ( rhs.xy , rhs.xy , 1 ) );
662  // lhs.x * rhs.y - lhs.y * rhs.x
663  __m128d m2 = _mm_sub_sd ( l1 , _mm_unpackhi_pd ( l1 , l1 ) );
664 
665  // Clear w-component
666  m2 = _mm_move_sd(_mm_setzero_pd(), m2);
667 
668  return Vec3T(m1, m2);
669 #else
670  return Vec3T(lhs.xyzw ^ rhs.xyzw);
671 #endif
672  }
673 
674  AGX_FORCE_INLINE agx::Real64 Vec3T<agx::Real64>::dot(const Vec3T& lhs, const Vec3T& rhs)
675  {
676 #if AGX_USE_AVX()
677  // NOTE: No _mm256_dp_pd instruction available!
678  // const int mask = 0x71;
679  // AGX_ALIGNED( agx::Real64, 32 ) output[4];
680  // _mm256_store_pd( output, _mm256_dp_pd(lhs.xyzw, rhs.xyzw, mask) );
681  // return output[0];
682 
683  #if 1
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 );
688 
689  const int mask = 0x31;
690  AGX_ALIGNED( agx::Real64, 16 ) output[2];
691 
692  const __m128d xyDot = _mm_dp_pd(lhsXY, rhsXY, mask);
693  const __m128d zDot = _mm_mul_pd(lhsZW, rhsZW);
694 
695  _mm_store_pd( output, _mm_add_pd(xyDot, zDot));
696  return output[0];
697 
698  #else
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 );
703 
704  AGX_ALIGNED( agx::Real64, 16 ) output[2];
705  _mm_store_pd( output, dotproduct );
706  return output[0];
707  #endif
708 
709 #elif AGX_USE_SSE4()
710  const int mask = 0x31;
711  AGX_ALIGNED( agx::Real64, 16 ) output[2];
712 
713  const __m128d xyDot = _mm_dp_pd(lhs.xy, rhs.xy, mask);
714  const __m128d zDot = _mm_mul_pd(lhs.zw, rhs.zw);
715 
716  _mm_store_pd( output, _mm_add_pd(xyDot, zDot));
717  return output[0];
718 #elif AGX_USE_SSE()
719  AGX_ALIGNED( agx::Real64, 16 ) scalarLhs[4];
720  AGX_ALIGNED( agx::Real64, 16 ) scalarRhs[4];
721 
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 );
726 
727  return scalarLhs[0] * scalarRhs[0] + scalarLhs[1] * scalarRhs[1] + scalarLhs[2] * scalarRhs[2];
728 #else
729  return lhs.xyzw * rhs.xyzw;
730 #endif
731  }
732 
734  {
735  return dot(*this, *this);
736  }
737 
739  {
740 #if AGX_USE_AVX() && 0
741  // NOTE: No _mm256_dp_pd instruction available!
742  AGX_ALIGNED( agx::Real64, 32 ) output[4];
743  _mm256_store_pd(output, _mm256_sqrt_pd(_mm256_dp_pd(xyzw, xyzw, 0x71)));
744  return output[0];
745 #elif AGX_USE_SSE()
746  return std::sqrt(this->length2());
747 #else
748  return (agx::Real64)xyzw.length();
749 #endif
750  }
751 
752 
753  AGX_FORCE_INLINE agx::Real64 Vec3T<agx::Real64>::innerProduct(const Vec3T& v01, const Vec3T& v02, const Vec3T& v11, const Vec3T& v12)
754  {
755 #if AGX_USE_AVX()
756 
757  AGX_ALIGNED( agx::Real64, 32 ) output[4];
758 
759  __m256d tmp1 = _mm256_mul_pd( v01.xyzw, v11.xyzw );
760  __m256d tmp2 = _mm256_mul_pd( v02.xyzw, v12.xyzw );
761 
762  tmp1 = _mm256_add_pd( tmp1, tmp2 );
763 
764  _mm256_store_pd( output, tmp1 );
765 
766  return output[0]+output[1]+output[2];
767 
768 #elif AGX_USE_SSE()
769 
770  // A0 B0 A0 * B0
771  // A1 B1 A1 * B1 A0 * B0 + A2 * B2
772  // --------- mul => ------- add => ----------------- hadd => A0 * B0 + A2 * B2 + A1 * B1 + A3 * B3
773  // A2 B2 A2 * B2 A1 * B1 + A3 * B3
774  // A3 B3 A3 * B3
775 
776  __m128d tmp1 = _mm_mul_pd( v01.xy, v11.xy );
777  __m128d tmp2 = _mm_mul_pd( v01.zw, v11.zw );
778 
779  __m128d tmp3 = _mm_mul_pd( v02.xy, v12.xy );
780  __m128d tmp4 = _mm_mul_pd( v02.zw, v12.zw );
781 
782 
783  tmp1 = _mm_add_pd( tmp1, tmp2 );
784  tmp3 = _mm_add_pd( tmp3, tmp4 );
785 
786  AGX_ALIGNED( agx::Real64, 16 ) output[2];
787 
788  tmp1 = _mm_hadd_pd( tmp1, tmp3 );
789  _mm_store_pd( output, tmp1 );
790 
791  return output[0] + output[1];
792 #else
793  return v01.xyzw * v11.xyzw + v02.xyzw * v12.xyzw;
794 #endif
795  }
796 
798 
799  AGX_FORCE_INLINE std::ostream& operator << ( std::ostream& output, const Vec3d& v )
800  {
801  AGX_ALIGNED(agx::Vec3d, 32) tmp;
802  v.store(tmp);
803  return output << tmp;
804  }
805 
806  AGX_FORCE_INLINE std::ostream& operator << ( std::ostream& output, const Vec3f& v )
807  {
808  AGX_ALIGNED(agx::Vec3f, 16) tmp;
809  v.store(tmp);
810  return output << tmp;
811  }
812 
813 }
815 
816 #endif /* AGX_SIMD_VEC3_H */
float Real32
Definition: Real.h:44
double Real64
Definition: Real.h:45
Vec3T< Real > Vec3
The object holding 3 dimensional vectors and providing basic arithmetic.
Definition: Vec3.h:36
Real length2() const
Length squared of the vector = vec .
Definition: Vec3Template.h:695
std::ostream & operator<<(std::ostream &stream, const AtomicValueT< T > &value)
Definition: AtomicValue.h:109
Vec3T< Real64 > Vec3d
Definition: Vec3.h:40
const Vec3T cross(const Vec3T &rhs) const
Cross product method.
Definition: Vec3Template.h:567
STL namespace.
GLenum GLuint GLenum GLsizei length
Definition: gl.h:5539
#define AGX_ALIGNED(t, a)
Definition: Object.h:69
#define AGX_FORCE_INLINE
Definition: Object.h:63
uint32_t UInt
Definition: Integer.h:34
std::ostream & operator<<(std::ostream &o, const agx::Vec6 &v)
Definition: Vec6.h:230
Vec3T()
Default constructor.
Definition: Vec3Template.h:329
Namespace agx contains the dynamics/math part of the AGX Dynamics API.
Vec3T< Real32 > Vec3f
Definition: Vec3.h:39
#define agxAssert(expr)
Definition: debug.h:143
T absolute(T v)
return the absolute value.
Definition: Math.h:289
Real length() const
Length of the vector = sqrt( vec .
Definition: Vec3Template.h:689
static Vec3T mul(const Vec3T &lhs, const Vec3T &rhs)
Element-wise-multiplication.
Definition: Vec3Template.h:575
uint32_t UInt32
Definition: Integer.h:40
#define DOXYGEN_END_INTERNAL_BLOCK()
Definition: Object.h:102
#define DOXYGEN_START_INTERNAL_BLOCK()
Definition: Object.h:101