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