2 Copyright (C) 2006, 2007 Sony Computer Entertainment Inc.
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.
34 #include <xmmintrin.h>
36 namespace Vectormath
{
40 //--------------------------------------------------------------------------------------------------
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
69 inline float getAsFloat() const;
71 // implicit cast to float
73 inline operator float() const;
77 // float value is splatted across all word slots of vector
79 inline __m128
get128() const;
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
);
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
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
{
136 floatInVec::floatInVec(__m128 vec
)
142 floatInVec::floatInVec(const boolInVec
&vec
)
144 mData
= vec_sel(_mm_setzero_ps(), _mm_set1_ps(1.0f
), vec
.get128());
148 floatInVec::floatInVec(__m128 vec
, int slot
)
152 mData
= _mm_set1_ps(v
.f
[slot
]);
156 floatInVec::floatInVec(float scalar
)
158 mData
= _mm_set1_ps(scalar
);
161 #ifdef _VECTORMATH_NO_SCALAR_CAST
164 floatInVec::getAsFloat() const
167 floatInVec::operator float() const
170 return *((float *)&mData
);
175 floatInVec::get128() const
182 floatInVec::operator ++ (int)
184 __m128 olddata
= mData
;
186 return floatInVec(olddata
);
191 floatInVec::operator -- (int)
193 __m128 olddata
= mData
;
195 return floatInVec(olddata
);
200 floatInVec::operator ++ ()
202 *this += floatInVec(_mm_set1_ps(1.0f
));
208 floatInVec::operator -- ()
210 *this -= floatInVec(_mm_set1_ps(1.0f
));
216 floatInVec::operator - () const
218 return floatInVec(_mm_sub_ps(_mm_setzero_ps(), mData
));
223 floatInVec::operator = (const floatInVec
&vec
)
231 floatInVec::operator *= (const floatInVec
&vec
)
239 floatInVec::operator /= (const floatInVec
&vec
)
247 floatInVec::operator += (const floatInVec
&vec
)
255 floatInVec::operator -= (const floatInVec
&vec
)
263 operator * (const floatInVec
&vec0
, const floatInVec
&vec1
)
265 return floatInVec(_mm_mul_ps(vec0
.get128(), vec1
.get128()));
270 operator / (const floatInVec
&num
, const floatInVec
&den
)
272 return floatInVec(_mm_div_ps(num
.get128(), den
.get128()));
277 operator + (const floatInVec
&vec0
, const floatInVec
&vec1
)
279 return floatInVec(_mm_add_ps(vec0
.get128(), vec1
.get128()));
284 operator - (const floatInVec
&vec0
, const floatInVec
&vec1
)
286 return floatInVec(_mm_sub_ps(vec0
.get128(), vec1
.get128()));
291 operator < (const floatInVec
&vec0
, const floatInVec
&vec1
)
293 return boolInVec(_mm_cmpgt_ps(vec1
.get128(), vec0
.get128()));
298 operator <= (const floatInVec
&vec0
, const floatInVec
&vec1
)
300 return boolInVec(_mm_cmpge_ps(vec1
.get128(), vec0
.get128()));
305 operator > (const floatInVec
&vec0
, const floatInVec
&vec1
)
307 return boolInVec(_mm_cmpgt_ps(vec0
.get128(), vec1
.get128()));
312 operator >= (const floatInVec
&vec0
, const floatInVec
&vec1
)
314 return boolInVec(_mm_cmpge_ps(vec0
.get128(), vec1
.get128()));
319 operator == (const floatInVec
&vec0
, const floatInVec
&vec1
)
321 return boolInVec(_mm_cmpeq_ps(vec0
.get128(), vec1
.get128()));
326 operator != (const floatInVec
&vec0
, const floatInVec
&vec1
)
328 return boolInVec(_mm_cmpneq_ps(vec0
.get128(), vec1
.get128()));
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