Merge pull request #1874 from John3/readmeUpdate
[Torque-3d.git] / Engine / lib / bullet / src / vectormath / sse / floatInVec.h
blobe8ac5959e4e91687fa288814e198eee55223c71e
1 /*
2 Copyright (C) 2006, 2007 Sony Computer Entertainment Inc.
3 All rights reserved.
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.
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.
30 #ifndef _FLOATINVEC_H
31 #define _FLOATINVEC_H
33 #include <math.h>
34 #include <xmmintrin.h>
36 namespace Vectormath {
38 class boolInVec;
40 //--------------------------------------------------------------------------------------------------
41 // floatInVec class
44 class floatInVec
46 private:
47 __m128 mData;
49 public:
50 inline floatInVec(__m128 vec);
52 inline floatInVec() {}
54 // matches standard type conversions
56 inline floatInVec(const boolInVec &vec);
58 // construct from a slot of __m128
60 inline floatInVec(__m128 vec, int slot);
62 // explicit cast from float
64 explicit inline floatInVec(float scalar);
66 #ifdef _VECTORMATH_NO_SCALAR_CAST
67 // explicit cast to float
68 //
69 inline float getAsFloat() const;
70 #else
71 // implicit cast to float
73 inline operator float() const;
74 #endif
76 // get vector data
77 // float value is splatted across all word slots of vector
79 inline __m128 get128() const;
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);
94 // friend functions
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);
103 //--------------------------------------------------------------------------------------------------
104 // floatInVec functions
107 // operators
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);
120 // select between vec0 and vec1 using boolInVec.
121 // false selects vec0, true selects vec1
123 inline const floatInVec select(const floatInVec &vec0, const floatInVec &vec1, const boolInVec &select_vec1);
125 } // namespace Vectormath
127 //--------------------------------------------------------------------------------------------------
128 // floatInVec implementation
131 #include "boolInVec.h"
133 namespace Vectormath {
135 inline
136 floatInVec::floatInVec(__m128 vec)
138 mData = vec;
141 inline
142 floatInVec::floatInVec(const boolInVec &vec)
144 mData = vec_sel(_mm_setzero_ps(), _mm_set1_ps(1.0f), vec.get128());
147 inline
148 floatInVec::floatInVec(__m128 vec, int slot)
150 SSEFloat v;
151 v.m128 = vec;
152 mData = _mm_set1_ps(v.f[slot]);
155 inline
156 floatInVec::floatInVec(float scalar)
158 mData = _mm_set1_ps(scalar);
161 #ifdef _VECTORMATH_NO_SCALAR_CAST
162 inline
163 float
164 floatInVec::getAsFloat() const
165 #else
166 inline
167 floatInVec::operator float() const
168 #endif
170 return *((float *)&mData);
173 inline
174 __m128
175 floatInVec::get128() const
177 return mData;
180 inline
181 const floatInVec
182 floatInVec::operator ++ (int)
184 __m128 olddata = mData;
185 operator ++();
186 return floatInVec(olddata);
189 inline
190 const floatInVec
191 floatInVec::operator -- (int)
193 __m128 olddata = mData;
194 operator --();
195 return floatInVec(olddata);
198 inline
199 floatInVec&
200 floatInVec::operator ++ ()
202 *this += floatInVec(_mm_set1_ps(1.0f));
203 return *this;
206 inline
207 floatInVec&
208 floatInVec::operator -- ()
210 *this -= floatInVec(_mm_set1_ps(1.0f));
211 return *this;
214 inline
215 const floatInVec
216 floatInVec::operator - () const
218 return floatInVec(_mm_sub_ps(_mm_setzero_ps(), mData));
221 inline
222 floatInVec&
223 floatInVec::operator = (const floatInVec &vec)
225 mData = vec.mData;
226 return *this;
229 inline
230 floatInVec&
231 floatInVec::operator *= (const floatInVec &vec)
233 *this = *this * vec;
234 return *this;
237 inline
238 floatInVec&
239 floatInVec::operator /= (const floatInVec &vec)
241 *this = *this / vec;
242 return *this;
245 inline
246 floatInVec&
247 floatInVec::operator += (const floatInVec &vec)
249 *this = *this + vec;
250 return *this;
253 inline
254 floatInVec&
255 floatInVec::operator -= (const floatInVec &vec)
257 *this = *this - vec;
258 return *this;
261 inline
262 const floatInVec
263 operator * (const floatInVec &vec0, const floatInVec &vec1)
265 return floatInVec(_mm_mul_ps(vec0.get128(), vec1.get128()));
268 inline
269 const floatInVec
270 operator / (const floatInVec &num, const floatInVec &den)
272 return floatInVec(_mm_div_ps(num.get128(), den.get128()));
275 inline
276 const floatInVec
277 operator + (const floatInVec &vec0, const floatInVec &vec1)
279 return floatInVec(_mm_add_ps(vec0.get128(), vec1.get128()));
282 inline
283 const floatInVec
284 operator - (const floatInVec &vec0, const floatInVec &vec1)
286 return floatInVec(_mm_sub_ps(vec0.get128(), vec1.get128()));
289 inline
290 const boolInVec
291 operator < (const floatInVec &vec0, const floatInVec &vec1)
293 return boolInVec(_mm_cmpgt_ps(vec1.get128(), vec0.get128()));
296 inline
297 const boolInVec
298 operator <= (const floatInVec &vec0, const floatInVec &vec1)
300 return boolInVec(_mm_cmpge_ps(vec1.get128(), vec0.get128()));
303 inline
304 const boolInVec
305 operator > (const floatInVec &vec0, const floatInVec &vec1)
307 return boolInVec(_mm_cmpgt_ps(vec0.get128(), vec1.get128()));
310 inline
311 const boolInVec
312 operator >= (const floatInVec &vec0, const floatInVec &vec1)
314 return boolInVec(_mm_cmpge_ps(vec0.get128(), vec1.get128()));
317 inline
318 const boolInVec
319 operator == (const floatInVec &vec0, const floatInVec &vec1)
321 return boolInVec(_mm_cmpeq_ps(vec0.get128(), vec1.get128()));
324 inline
325 const boolInVec
326 operator != (const floatInVec &vec0, const floatInVec &vec1)
328 return boolInVec(_mm_cmpneq_ps(vec0.get128(), vec1.get128()));
331 inline
332 const floatInVec
333 select(const floatInVec &vec0, const floatInVec &vec1, const boolInVec &select_vec1)
335 return floatInVec(vec_sel(vec0.get128(), vec1.get128(), select_vec1.get128()));
338 } // namespace Vectormath
340 #endif // floatInVec_h