boolInVec.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*
  2. Copyright (C) 2006, 2007 Sony Computer Entertainment Inc.
  3. All rights reserved.
  4. Redistribution and use in source and binary forms,
  5. with or without modification, are permitted provided that the
  6. following conditions are met:
  7. * Redistributions of source code must retain the above copyright
  8. notice, this list of conditions and the following disclaimer.
  9. * Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in the
  11. documentation and/or other materials provided with the distribution.
  12. * Neither the name of the Sony Computer Entertainment Inc nor the names
  13. of its contributors may be used to endorse or promote products derived
  14. from this software without specific prior written permission.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  19. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  20. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  21. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  22. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  23. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  24. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  25. POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #ifndef _BOOLINVEC_H
  28. #define _BOOLINVEC_H
  29. #include <math.h>
  30. namespace Vectormath {
  31. class floatInVec;
  32. //--------------------------------------------------------------------------------------------------
  33. // boolInVec class
  34. //
  35. class boolInVec
  36. {
  37. private:
  38. __m128 mData;
  39. inline boolInVec(__m128 vec);
  40. public:
  41. inline boolInVec() {}
  42. // matches standard type conversions
  43. //
  44. inline boolInVec(const floatInVec &vec);
  45. // explicit cast from bool
  46. //
  47. explicit inline boolInVec(bool scalar);
  48. #ifdef _VECTORMATH_NO_SCALAR_CAST
  49. // explicit cast to bool
  50. //
  51. inline bool getAsBool() const;
  52. #else
  53. // implicit cast to bool
  54. //
  55. inline operator bool() const;
  56. #endif
  57. // get vector data
  58. // bool value is splatted across all word slots of vector as 0 (false) or -1 (true)
  59. //
  60. inline __m128 get128() const;
  61. // operators
  62. //
  63. inline const boolInVec operator ! () const;
  64. inline boolInVec& operator = (const boolInVec &vec);
  65. inline boolInVec& operator &= (const boolInVec &vec);
  66. inline boolInVec& operator ^= (const boolInVec &vec);
  67. inline boolInVec& operator |= (const boolInVec &vec);
  68. // friend functions
  69. //
  70. friend inline const boolInVec operator == (const boolInVec &vec0, const boolInVec &vec1);
  71. friend inline const boolInVec operator != (const boolInVec &vec0, const boolInVec &vec1);
  72. friend inline const boolInVec operator < (const floatInVec &vec0, const floatInVec &vec1);
  73. friend inline const boolInVec operator <= (const floatInVec &vec0, const floatInVec &vec1);
  74. friend inline const boolInVec operator > (const floatInVec &vec0, const floatInVec &vec1);
  75. friend inline const boolInVec operator >= (const floatInVec &vec0, const floatInVec &vec1);
  76. friend inline const boolInVec operator == (const floatInVec &vec0, const floatInVec &vec1);
  77. friend inline const boolInVec operator != (const floatInVec &vec0, const floatInVec &vec1);
  78. friend inline const boolInVec operator & (const boolInVec &vec0, const boolInVec &vec1);
  79. friend inline const boolInVec operator ^ (const boolInVec &vec0, const boolInVec &vec1);
  80. friend inline const boolInVec operator | (const boolInVec &vec0, const boolInVec &vec1);
  81. friend inline const boolInVec select(const boolInVec &vec0, const boolInVec &vec1, const boolInVec &select_vec1);
  82. };
  83. //--------------------------------------------------------------------------------------------------
  84. // boolInVec functions
  85. //
  86. // operators
  87. //
  88. inline const boolInVec operator == (const boolInVec &vec0, const boolInVec &vec1);
  89. inline const boolInVec operator != (const boolInVec &vec0, const boolInVec &vec1);
  90. inline const boolInVec operator & (const boolInVec &vec0, const boolInVec &vec1);
  91. inline const boolInVec operator ^ (const boolInVec &vec0, const boolInVec &vec1);
  92. inline const boolInVec operator | (const boolInVec &vec0, const boolInVec &vec1);
  93. // select between vec0 and vec1 using boolInVec.
  94. // false selects vec0, true selects vec1
  95. //
  96. inline const boolInVec select(const boolInVec &vec0, const boolInVec &vec1, const boolInVec &select_vec1);
  97. } // namespace Vectormath
  98. //--------------------------------------------------------------------------------------------------
  99. // boolInVec implementation
  100. //
  101. #include "floatInVec.h"
  102. namespace Vectormath {
  103. inline
  104. boolInVec::boolInVec(__m128 vec)
  105. {
  106. mData = vec;
  107. }
  108. inline
  109. boolInVec::boolInVec(const floatInVec &vec)
  110. {
  111. *this = (vec != floatInVec(0.0f));
  112. }
  113. inline
  114. boolInVec::boolInVec(bool scalar)
  115. {
  116. unsigned int mask = -(int)scalar;
  117. mData = _mm_set1_ps(*(float *)&mask); // TODO: Union
  118. }
  119. #ifdef _VECTORMATH_NO_SCALAR_CAST
  120. inline
  121. bool
  122. boolInVec::getAsBool() const
  123. #else
  124. inline
  125. boolInVec::operator bool() const
  126. #endif
  127. {
  128. return *(bool *)&mData;
  129. }
  130. inline
  131. __m128
  132. boolInVec::get128() const
  133. {
  134. return mData;
  135. }
  136. inline
  137. const boolInVec
  138. boolInVec::operator ! () const
  139. {
  140. return boolInVec(_mm_andnot_ps(mData, _mm_cmpneq_ps(_mm_setzero_ps(),_mm_setzero_ps())));
  141. }
  142. inline
  143. boolInVec&
  144. boolInVec::operator = (const boolInVec &vec)
  145. {
  146. mData = vec.mData;
  147. return *this;
  148. }
  149. inline
  150. boolInVec&
  151. boolInVec::operator &= (const boolInVec &vec)
  152. {
  153. *this = *this & vec;
  154. return *this;
  155. }
  156. inline
  157. boolInVec&
  158. boolInVec::operator ^= (const boolInVec &vec)
  159. {
  160. *this = *this ^ vec;
  161. return *this;
  162. }
  163. inline
  164. boolInVec&
  165. boolInVec::operator |= (const boolInVec &vec)
  166. {
  167. *this = *this | vec;
  168. return *this;
  169. }
  170. inline
  171. const boolInVec
  172. operator == (const boolInVec &vec0, const boolInVec &vec1)
  173. {
  174. return boolInVec(_mm_cmpeq_ps(vec0.get128(), vec1.get128()));
  175. }
  176. inline
  177. const boolInVec
  178. operator != (const boolInVec &vec0, const boolInVec &vec1)
  179. {
  180. return boolInVec(_mm_cmpneq_ps(vec0.get128(), vec1.get128()));
  181. }
  182. inline
  183. const boolInVec
  184. operator & (const boolInVec &vec0, const boolInVec &vec1)
  185. {
  186. return boolInVec(_mm_and_ps(vec0.get128(), vec1.get128()));
  187. }
  188. inline
  189. const boolInVec
  190. operator | (const boolInVec &vec0, const boolInVec &vec1)
  191. {
  192. return boolInVec(_mm_or_ps(vec0.get128(), vec1.get128()));
  193. }
  194. inline
  195. const boolInVec
  196. operator ^ (const boolInVec &vec0, const boolInVec &vec1)
  197. {
  198. return boolInVec(_mm_xor_ps(vec0.get128(), vec1.get128()));
  199. }
  200. inline
  201. const boolInVec
  202. select(const boolInVec &vec0, const boolInVec &vec1, const boolInVec &select_vec1)
  203. {
  204. return boolInVec(vec_sel(vec0.get128(), vec1.get128(), select_vec1.get128()));
  205. }
  206. } // namespace Vectormath
  207. #endif // boolInVec_h