simd_float.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /* -----------------------------------------------------------------------------
  2. Copyright (c) 2006 Simon Brown si@sjbrown.co.uk
  3. Permission is hereby granted, free of charge, to any person obtaining
  4. a copy of this software and associated documentation files (the
  5. "Software"), to deal in the Software without restriction, including
  6. without limitation the rights to use, copy, modify, merge, publish,
  7. distribute, sublicense, and/or sell copies of the Software, and to
  8. permit persons to whom the Software is furnished to do so, subject to
  9. the following conditions:
  10. The above copyright notice and this permission notice shall be included
  11. in all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  13. OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  14. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  15. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  16. CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  17. TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  18. SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  19. -------------------------------------------------------------------------- */
  20. #ifndef SQUISH_SIMD_FLOAT_H
  21. #define SQUISH_SIMD_FLOAT_H
  22. #include <algorithm>
  23. namespace squish {
  24. #define VEC4_CONST( X ) Vec4( X )
  25. class Vec4
  26. {
  27. public:
  28. typedef Vec4 const& Arg;
  29. Vec4() {}
  30. explicit Vec4( float s )
  31. : m_x( s ),
  32. m_y( s ),
  33. m_z( s ),
  34. m_w( s )
  35. {
  36. }
  37. Vec4( float x, float y, float z, float w )
  38. : m_x( x ),
  39. m_y( y ),
  40. m_z( z ),
  41. m_w( w )
  42. {
  43. }
  44. Vec3 GetVec3() const
  45. {
  46. return Vec3( m_x, m_y, m_z );
  47. }
  48. Vec4 SplatX() const { return Vec4( m_x ); }
  49. Vec4 SplatY() const { return Vec4( m_y ); }
  50. Vec4 SplatZ() const { return Vec4( m_z ); }
  51. Vec4 SplatW() const { return Vec4( m_w ); }
  52. Vec4& operator+=( Arg v )
  53. {
  54. m_x += v.m_x;
  55. m_y += v.m_y;
  56. m_z += v.m_z;
  57. m_w += v.m_w;
  58. return *this;
  59. }
  60. Vec4& operator-=( Arg v )
  61. {
  62. m_x -= v.m_x;
  63. m_y -= v.m_y;
  64. m_z -= v.m_z;
  65. m_w -= v.m_w;
  66. return *this;
  67. }
  68. Vec4& operator*=( Arg v )
  69. {
  70. m_x *= v.m_x;
  71. m_y *= v.m_y;
  72. m_z *= v.m_z;
  73. m_w *= v.m_w;
  74. return *this;
  75. }
  76. friend Vec4 operator+( Vec4::Arg left, Vec4::Arg right )
  77. {
  78. Vec4 copy( left );
  79. return copy += right;
  80. }
  81. friend Vec4 operator-( Vec4::Arg left, Vec4::Arg right )
  82. {
  83. Vec4 copy( left );
  84. return copy -= right;
  85. }
  86. friend Vec4 operator*( Vec4::Arg left, Vec4::Arg right )
  87. {
  88. Vec4 copy( left );
  89. return copy *= right;
  90. }
  91. //! Returns a*b + c
  92. friend Vec4 MultiplyAdd( Vec4::Arg a, Vec4::Arg b, Vec4::Arg c )
  93. {
  94. return a*b + c;
  95. }
  96. //! Returns -( a*b - c )
  97. friend Vec4 NegativeMultiplySubtract( Vec4::Arg a, Vec4::Arg b, Vec4::Arg c )
  98. {
  99. return c - a*b;
  100. }
  101. friend Vec4 Reciprocal( Vec4::Arg v )
  102. {
  103. return Vec4(
  104. 1.0f/v.m_x,
  105. 1.0f/v.m_y,
  106. 1.0f/v.m_z,
  107. 1.0f/v.m_w
  108. );
  109. }
  110. friend Vec4 Min( Vec4::Arg left, Vec4::Arg right )
  111. {
  112. return Vec4(
  113. std::min( left.m_x, right.m_x ),
  114. std::min( left.m_y, right.m_y ),
  115. std::min( left.m_z, right.m_z ),
  116. std::min( left.m_w, right.m_w )
  117. );
  118. }
  119. friend Vec4 Max( Vec4::Arg left, Vec4::Arg right )
  120. {
  121. return Vec4(
  122. std::max( left.m_x, right.m_x ),
  123. std::max( left.m_y, right.m_y ),
  124. std::max( left.m_z, right.m_z ),
  125. std::max( left.m_w, right.m_w )
  126. );
  127. }
  128. friend Vec4 Truncate( Vec4::Arg v )
  129. {
  130. return Vec4(
  131. v.m_x > 0.0f ? std::floor( v.m_x ) : std::ceil( v.m_x ),
  132. v.m_y > 0.0f ? std::floor( v.m_y ) : std::ceil( v.m_y ),
  133. v.m_z > 0.0f ? std::floor( v.m_z ) : std::ceil( v.m_z ),
  134. v.m_w > 0.0f ? std::floor( v.m_w ) : std::ceil( v.m_w )
  135. );
  136. }
  137. friend bool CompareAnyLessThan( Vec4::Arg left, Vec4::Arg right )
  138. {
  139. return left.m_x < right.m_x
  140. || left.m_y < right.m_y
  141. || left.m_z < right.m_z
  142. || left.m_w < right.m_w;
  143. }
  144. private:
  145. float m_x;
  146. float m_y;
  147. float m_z;
  148. float m_w;
  149. };
  150. } // namespace squish
  151. #endif // ndef SQUISH_SIMD_FLOAT_H