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