simd_ve.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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_VE_H
  21. #define SQUISH_SIMD_VE_H
  22. #include <altivec.h>
  23. #undef bool
  24. namespace squish {
  25. #define VEC4_CONST( X ) Vec4( ( vector float ){ X } )
  26. class Vec4
  27. {
  28. public:
  29. typedef Vec4 Arg;
  30. Vec4() {}
  31. explicit Vec4( vector float v ) : m_v( v ) {}
  32. Vec4( Vec4 const& arg ) : m_v( arg.m_v ) {}
  33. Vec4& operator=( Vec4 const& arg )
  34. {
  35. m_v = arg.m_v;
  36. return *this;
  37. }
  38. explicit Vec4( float s )
  39. {
  40. union { vector float v; float c[4]; } u;
  41. u.c[0] = s;
  42. u.c[1] = s;
  43. u.c[2] = s;
  44. u.c[3] = s;
  45. m_v = u.v;
  46. }
  47. Vec4( float x, float y, float z, float w )
  48. {
  49. union { vector float v; float c[4]; } u;
  50. u.c[0] = x;
  51. u.c[1] = y;
  52. u.c[2] = z;
  53. u.c[3] = w;
  54. m_v = u.v;
  55. }
  56. Vec3 GetVec3() const
  57. {
  58. union { vector float v; float c[4]; } u;
  59. u.v = m_v;
  60. return Vec3( u.c[0], u.c[1], u.c[2] );
  61. }
  62. Vec4 SplatX() const { return Vec4( vec_splat( m_v, 0 ) ); }
  63. Vec4 SplatY() const { return Vec4( vec_splat( m_v, 1 ) ); }
  64. Vec4 SplatZ() const { return Vec4( vec_splat( m_v, 2 ) ); }
  65. Vec4 SplatW() const { return Vec4( vec_splat( m_v, 3 ) ); }
  66. Vec4& operator+=( Arg v )
  67. {
  68. m_v = vec_add( m_v, v.m_v );
  69. return *this;
  70. }
  71. Vec4& operator-=( Arg v )
  72. {
  73. m_v = vec_sub( m_v, v.m_v );
  74. return *this;
  75. }
  76. Vec4& operator*=( Arg v )
  77. {
  78. m_v = vec_madd( m_v, v.m_v, ( vector float ){ -0.0f } );
  79. return *this;
  80. }
  81. friend Vec4 operator+( Vec4::Arg left, Vec4::Arg right )
  82. {
  83. return Vec4( vec_add( left.m_v, right.m_v ) );
  84. }
  85. friend Vec4 operator-( Vec4::Arg left, Vec4::Arg right )
  86. {
  87. return Vec4( vec_sub( left.m_v, right.m_v ) );
  88. }
  89. friend Vec4 operator*( Vec4::Arg left, Vec4::Arg right )
  90. {
  91. return Vec4( vec_madd( left.m_v, right.m_v, ( vector float ){ -0.0f } ) );
  92. }
  93. //! Returns a*b + c
  94. friend Vec4 MultiplyAdd( Vec4::Arg a, Vec4::Arg b, Vec4::Arg c )
  95. {
  96. return Vec4( vec_madd( a.m_v, b.m_v, c.m_v ) );
  97. }
  98. //! Returns -( a*b - c )
  99. friend Vec4 NegativeMultiplySubtract( Vec4::Arg a, Vec4::Arg b, Vec4::Arg c )
  100. {
  101. return Vec4( vec_nmsub( a.m_v, b.m_v, c.m_v ) );
  102. }
  103. friend Vec4 Reciprocal( Vec4::Arg v )
  104. {
  105. // get the reciprocal estimate
  106. vector float estimate = vec_re( v.m_v );
  107. // one round of Newton-Rhaphson refinement
  108. vector float diff = vec_nmsub( estimate, v.m_v, ( vector float ){ 1.0f } );
  109. return Vec4( vec_madd( diff, estimate, estimate ) );
  110. }
  111. friend Vec4 Min( Vec4::Arg left, Vec4::Arg right )
  112. {
  113. return Vec4( vec_min( left.m_v, right.m_v ) );
  114. }
  115. friend Vec4 Max( Vec4::Arg left, Vec4::Arg right )
  116. {
  117. return Vec4( vec_max( left.m_v, right.m_v ) );
  118. }
  119. friend Vec4 Truncate( Vec4::Arg v )
  120. {
  121. return Vec4( vec_trunc( v.m_v ) );
  122. }
  123. friend bool CompareAnyLessThan( Vec4::Arg left, Vec4::Arg right )
  124. {
  125. return vec_any_lt( left.m_v, right.m_v ) != 0;
  126. }
  127. private:
  128. vector float m_v;
  129. };
  130. } // namespace squish
  131. #endif // ndef SQUISH_SIMD_VE_H