vec3ba.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // ======================================================================== //
  2. // Copyright 2009-2017 Intel Corporation //
  3. // //
  4. // Licensed under the Apache License, Version 2.0 (the "License"); //
  5. // you may not use this file except in compliance with the License. //
  6. // You may obtain a copy of the License at //
  7. // //
  8. // http://www.apache.org/licenses/LICENSE-2.0 //
  9. // //
  10. // Unless required by applicable law or agreed to in writing, software //
  11. // distributed under the License is distributed on an "AS IS" BASIS, //
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. //
  13. // See the License for the specific language governing permissions and //
  14. // limitations under the License. //
  15. // ======================================================================== //
  16. #pragma once
  17. #include "../sys/alloc.h"
  18. #include "math.h"
  19. #include "../simd/sse.h"
  20. namespace embree
  21. {
  22. ////////////////////////////////////////////////////////////////////////////////
  23. /// SSE Vec3ba Type
  24. ////////////////////////////////////////////////////////////////////////////////
  25. struct __aligned(16) Vec3ba
  26. {
  27. ALIGNED_STRUCT;
  28. union {
  29. __m128 m128;
  30. struct { int x,y,z; int a; };
  31. };
  32. typedef int Scalar;
  33. enum { N = 3 };
  34. ////////////////////////////////////////////////////////////////////////////////
  35. /// Constructors, Assignment & Cast Operators
  36. ////////////////////////////////////////////////////////////////////////////////
  37. __forceinline Vec3ba( ) {}
  38. __forceinline Vec3ba( const __m128 input ) : m128(input) {}
  39. __forceinline Vec3ba( const Vec3ba& other ) : m128(other.m128) {}
  40. __forceinline Vec3ba& operator =(const Vec3ba& other) { m128 = other.m128; return *this; }
  41. __forceinline explicit Vec3ba( bool a )
  42. : m128(_mm_lookupmask_ps[(size_t(a) << 3) | (size_t(a) << 2) | (size_t(a) << 1) | size_t(a)]) {}
  43. __forceinline Vec3ba( bool a, bool b, bool c)
  44. : m128(_mm_lookupmask_ps[(size_t(c) << 2) | (size_t(b) << 1) | size_t(a)]) {}
  45. __forceinline operator const __m128&( void ) const { return m128; }
  46. __forceinline operator __m128&( void ) { return m128; }
  47. ////////////////////////////////////////////////////////////////////////////////
  48. /// Constants
  49. ////////////////////////////////////////////////////////////////////////////////
  50. __forceinline Vec3ba( FalseTy ) : m128(_mm_setzero_ps()) {}
  51. __forceinline Vec3ba( TrueTy ) : m128(_mm_castsi128_ps(_mm_cmpeq_epi32(_mm_setzero_si128(), _mm_setzero_si128()))) {}
  52. ////////////////////////////////////////////////////////////////////////////////
  53. /// Array Access
  54. ////////////////////////////////////////////////////////////////////////////////
  55. __forceinline const int& operator []( const size_t index ) const { assert(index < 3); return (&x)[index]; }
  56. __forceinline int& operator []( const size_t index ) { assert(index < 3); return (&x)[index]; }
  57. };
  58. ////////////////////////////////////////////////////////////////////////////////
  59. /// Unary Operators
  60. ////////////////////////////////////////////////////////////////////////////////
  61. __forceinline const Vec3ba operator !( const Vec3ba& a ) { return _mm_xor_ps(a.m128, Vec3ba(embree::True)); }
  62. ////////////////////////////////////////////////////////////////////////////////
  63. /// Binary Operators
  64. ////////////////////////////////////////////////////////////////////////////////
  65. __forceinline const Vec3ba operator &( const Vec3ba& a, const Vec3ba& b ) { return _mm_and_ps(a.m128, b.m128); }
  66. __forceinline const Vec3ba operator |( const Vec3ba& a, const Vec3ba& b ) { return _mm_or_ps (a.m128, b.m128); }
  67. __forceinline const Vec3ba operator ^( const Vec3ba& a, const Vec3ba& b ) { return _mm_xor_ps(a.m128, b.m128); }
  68. ////////////////////////////////////////////////////////////////////////////////
  69. /// Assignment Operators
  70. ////////////////////////////////////////////////////////////////////////////////
  71. __forceinline const Vec3ba operator &=( Vec3ba& a, const Vec3ba& b ) { return a = a & b; }
  72. __forceinline const Vec3ba operator |=( Vec3ba& a, const Vec3ba& b ) { return a = a | b; }
  73. __forceinline const Vec3ba operator ^=( Vec3ba& a, const Vec3ba& b ) { return a = a ^ b; }
  74. ////////////////////////////////////////////////////////////////////////////////
  75. /// Comparison Operators + Select
  76. ////////////////////////////////////////////////////////////////////////////////
  77. __forceinline bool operator ==( const Vec3ba& a, const Vec3ba& b ) {
  78. return (_mm_movemask_ps(_mm_castsi128_ps(_mm_cmpeq_epi32(_mm_castps_si128(a.m128), _mm_castps_si128(b.m128)))) & 7) == 7;
  79. }
  80. __forceinline bool operator !=( const Vec3ba& a, const Vec3ba& b ) {
  81. return (_mm_movemask_ps(_mm_castsi128_ps(_mm_cmpeq_epi32(_mm_castps_si128(a.m128), _mm_castps_si128(b.m128)))) & 7) != 7;
  82. }
  83. __forceinline bool operator < ( const Vec3ba& a, const Vec3ba& b ) {
  84. if (a.x != b.x) return a.x < b.x;
  85. if (a.y != b.y) return a.y < b.y;
  86. if (a.z != b.z) return a.z < b.z;
  87. return false;
  88. }
  89. ////////////////////////////////////////////////////////////////////////////////
  90. /// Reduction Operations
  91. ////////////////////////////////////////////////////////////////////////////////
  92. __forceinline bool reduce_and( const Vec3ba& a ) { return (_mm_movemask_ps(a) & 0x7) == 0x7; }
  93. __forceinline bool reduce_or ( const Vec3ba& a ) { return (_mm_movemask_ps(a) & 0x7) != 0x0; }
  94. __forceinline bool all ( const Vec3ba& b ) { return (_mm_movemask_ps(b) & 0x7) == 0x7; }
  95. __forceinline bool any ( const Vec3ba& b ) { return (_mm_movemask_ps(b) & 0x7) != 0x0; }
  96. __forceinline bool none ( const Vec3ba& b ) { return (_mm_movemask_ps(b) & 0x7) == 0x0; }
  97. ////////////////////////////////////////////////////////////////////////////////
  98. /// Output Operators
  99. ////////////////////////////////////////////////////////////////////////////////
  100. inline std::ostream& operator<<(std::ostream& cout, const Vec3ba& a) {
  101. return cout << "(" << (a.x ? "1" : "0") << ", " << (a.y ? "1" : "0") << ", " << (a.z ? "1" : "0") << ")";
  102. }
  103. }