Bullet Collision Detection & Physics Library
sse/floatInVec.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 _FLOATINVEC_H
31 #define _FLOATINVEC_H
32 
33 #include <math.h>
34 #include <xmmintrin.h>
35 
36 namespace Vectormath {
37 
38 class boolInVec;
39 
40 //--------------------------------------------------------------------------------------------------
41 // floatInVec class
42 //
43 
44 class floatInVec
45 {
46  private:
47  __m128 mData;
48 
49  public:
50  inline floatInVec(__m128 vec);
51 
52  inline floatInVec() {}
53 
54  // matches standard type conversions
55  //
56  inline floatInVec(const boolInVec &vec);
57 
58  // construct from a slot of __m128
59  //
60  inline floatInVec(__m128 vec, int slot);
61 
62  // explicit cast from float
63  //
64  explicit inline floatInVec(float scalar);
65 
66 #ifdef _VECTORMATH_NO_SCALAR_CAST
67  // explicit cast to float
68  //
69  inline float getAsFloat() const;
70 #else
71  // implicit cast to float
72  //
73  inline operator float() const;
74 #endif
75 
76  // get vector data
77  // float value is splatted across all word slots of vector
78  //
79  inline __m128 get128() const;
80 
81  // operators
82  //
83  inline const floatInVec operator ++ (int);
84  inline const floatInVec operator -- (int);
85  inline floatInVec& operator ++ ();
86  inline floatInVec& operator -- ();
87  inline const floatInVec operator - () const;
88  inline floatInVec& operator = (const floatInVec &vec);
89  inline floatInVec& operator *= (const floatInVec &vec);
90  inline floatInVec& operator /= (const floatInVec &vec);
91  inline floatInVec& operator += (const floatInVec &vec);
92  inline floatInVec& operator -= (const floatInVec &vec);
93 
94  // friend functions
95  //
96  friend inline const floatInVec operator * (const floatInVec &vec0, const floatInVec &vec1);
97  friend inline const floatInVec operator / (const floatInVec &vec0, const floatInVec &vec1);
98  friend inline const floatInVec operator + (const floatInVec &vec0, const floatInVec &vec1);
99  friend inline const floatInVec operator - (const floatInVec &vec0, const floatInVec &vec1);
100  friend inline const floatInVec select(const floatInVec &vec0, const floatInVec &vec1, boolInVec select_vec1);
101 };
102 
103 //--------------------------------------------------------------------------------------------------
104 // floatInVec functions
105 //
106 
107 // operators
108 //
109 inline const floatInVec operator * (const floatInVec &vec0, const floatInVec &vec1);
110 inline const floatInVec operator / (const floatInVec &vec0, const floatInVec &vec1);
111 inline const floatInVec operator + (const floatInVec &vec0, const floatInVec &vec1);
112 inline const floatInVec operator - (const floatInVec &vec0, const floatInVec &vec1);
113 inline const boolInVec operator < (const floatInVec &vec0, const floatInVec &vec1);
114 inline const boolInVec operator <= (const floatInVec &vec0, const floatInVec &vec1);
115 inline const boolInVec operator > (const floatInVec &vec0, const floatInVec &vec1);
116 inline const boolInVec operator >= (const floatInVec &vec0, const floatInVec &vec1);
117 inline const boolInVec operator == (const floatInVec &vec0, const floatInVec &vec1);
118 inline const boolInVec operator != (const floatInVec &vec0, const floatInVec &vec1);
119 
120 // select between vec0 and vec1 using boolInVec.
121 // false selects vec0, true selects vec1
122 //
123 inline const floatInVec select(const floatInVec &vec0, const floatInVec &vec1, const boolInVec &select_vec1);
124 
125 } // namespace Vectormath
126 
127 //--------------------------------------------------------------------------------------------------
128 // floatInVec implementation
129 //
130 
131 #include "boolInVec.h"
132 
133 namespace Vectormath {
134 
135 inline
137 {
138  mData = vec;
139 }
140 
141 inline
143 {
144  mData = vec_sel(_mm_setzero_ps(), _mm_set1_ps(1.0f), vec.get128());
145 }
146 
147 inline
148 floatInVec::floatInVec(__m128 vec, int slot)
149 {
150  SSEFloat v;
151  v.m128 = vec;
152  mData = _mm_set1_ps(v.f[slot]);
153 }
154 
155 inline
156 floatInVec::floatInVec(float scalar)
157 {
158  mData = _mm_set1_ps(scalar);
159 }
160 
161 #ifdef _VECTORMATH_NO_SCALAR_CAST
162 inline
163 float
165 #else
166 inline
167 floatInVec::operator float() const
168 #endif
169 {
170  return *((float *)&mData);
171 }
172 
173 inline
174 __m128
176 {
177  return mData;
178 }
179 
180 inline
181 const floatInVec
183 {
184  __m128 olddata = mData;
185  operator ++();
186  return floatInVec(olddata);
187 }
188 
189 inline
190 const floatInVec
192 {
193  __m128 olddata = mData;
194  operator --();
195  return floatInVec(olddata);
196 }
197 
198 inline
199 floatInVec&
201 {
202  *this += floatInVec(_mm_set1_ps(1.0f));
203  return *this;
204 }
205 
206 inline
207 floatInVec&
209 {
210  *this -= floatInVec(_mm_set1_ps(1.0f));
211  return *this;
212 }
213 
214 inline
215 const floatInVec
217 {
218  return floatInVec(_mm_sub_ps(_mm_setzero_ps(), mData));
219 }
220 
221 inline
222 floatInVec&
224 {
225  mData = vec.mData;
226  return *this;
227 }
228 
229 inline
230 floatInVec&
232 {
233  *this = *this * vec;
234  return *this;
235 }
236 
237 inline
238 floatInVec&
240 {
241  *this = *this / vec;
242  return *this;
243 }
244 
245 inline
246 floatInVec&
248 {
249  *this = *this + vec;
250  return *this;
251 }
252 
253 inline
254 floatInVec&
256 {
257  *this = *this - vec;
258  return *this;
259 }
260 
261 inline
262 const floatInVec
263 operator * (const floatInVec &vec0, const floatInVec &vec1)
264 {
265  return floatInVec(_mm_mul_ps(vec0.get128(), vec1.get128()));
266 }
267 
268 inline
269 const floatInVec
270 operator / (const floatInVec &num, const floatInVec &den)
271 {
272  return floatInVec(_mm_div_ps(num.get128(), den.get128()));
273 }
274 
275 inline
276 const floatInVec
277 operator + (const floatInVec &vec0, const floatInVec &vec1)
278 {
279  return floatInVec(_mm_add_ps(vec0.get128(), vec1.get128()));
280 }
281 
282 inline
283 const floatInVec
284 operator - (const floatInVec &vec0, const floatInVec &vec1)
285 {
286  return floatInVec(_mm_sub_ps(vec0.get128(), vec1.get128()));
287 }
288 
289 inline
290 const boolInVec
291 operator < (const floatInVec &vec0, const floatInVec &vec1)
292 {
293  return boolInVec(_mm_cmpgt_ps(vec1.get128(), vec0.get128()));
294 }
295 
296 inline
297 const boolInVec
298 operator <= (const floatInVec &vec0, const floatInVec &vec1)
299 {
300  return boolInVec(_mm_cmpge_ps(vec1.get128(), vec0.get128()));
301 }
302 
303 inline
304 const boolInVec
305 operator > (const floatInVec &vec0, const floatInVec &vec1)
306 {
307  return boolInVec(_mm_cmpgt_ps(vec0.get128(), vec1.get128()));
308 }
309 
310 inline
311 const boolInVec
312 operator >= (const floatInVec &vec0, const floatInVec &vec1)
313 {
314  return boolInVec(_mm_cmpge_ps(vec0.get128(), vec1.get128()));
315 }
316 
317 inline
318 const boolInVec
319 operator == (const floatInVec &vec0, const floatInVec &vec1)
320 {
321  return boolInVec(_mm_cmpeq_ps(vec0.get128(), vec1.get128()));
322 }
323 
324 inline
325 const boolInVec
326 operator != (const floatInVec &vec0, const floatInVec &vec1)
327 {
328  return boolInVec(_mm_cmpneq_ps(vec0.get128(), vec1.get128()));
329 }
330 
331 inline
332 const floatInVec
333 select(const floatInVec &vec0, const floatInVec &vec1, const boolInVec &select_vec1)
334 {
335  return floatInVec(vec_sel(vec0.get128(), vec1.get128(), select_vec1.get128()));
336 }
337 
338 } // namespace Vectormath
339 
340 #endif // floatInVec_h
const boolInVec operator!=(boolInVec vec0, boolInVec vec1)
const floatInVec operator+(floatInVec vec0, floatInVec vec1)
const boolInVec operator<(floatInVec vec0, floatInVec vec1)
floatInVec & operator-=(floatInVec vec)
floatInVec & operator=(floatInVec vec)
friend const floatInVec operator+(const floatInVec &vec0, const floatInVec &vec1)
friend const floatInVec operator*(const floatInVec &vec0, const floatInVec &vec1)
const floatInVec operator*(floatInVec vec0, floatInVec vec1)
const boolInVec operator>=(floatInVec vec0, floatInVec vec1)
friend const floatInVec operator/(const floatInVec &vec0, const floatInVec &vec1)
const floatInVec operator-() const
floatInVec & operator--()
const boolInVec select(boolInVec vec0, boolInVec vec1, boolInVec select_vec1)
__m128 get128() const
friend const floatInVec select(const floatInVec &vec0, const floatInVec &vec1, boolInVec select_vec1)
__m128 get128() const
const boolInVec operator>(floatInVec vec0, floatInVec vec1)
floatInVec & operator++()
floatInVec & operator+=(floatInVec vec)
floatInVec & operator/=(floatInVec vec)
floatInVec & operator*=(floatInVec vec)
const boolInVec operator==(boolInVec vec0, boolInVec vec1)
float getAsFloat() const
const boolInVec operator<=(floatInVec vec0, floatInVec vec1)
static __m128 vec_sel(__m128 a, __m128 b, __m128 mask)
const floatInVec operator/(floatInVec vec0, floatInVec vec1)
const floatInVec operator-(floatInVec vec0, floatInVec vec1)