sse/boolInVec.h

Go to the documentation of this file.
00001 /*
00002    Copyright (C) 2006, 2007 Sony Computer Entertainment Inc.
00003    All rights reserved.
00004 
00005    Redistribution and use in source and binary forms,
00006    with or without modification, are permitted provided that the
00007    following conditions are met:
00008     * Redistributions of source code must retain the above copyright
00009       notice, this list of conditions and the following disclaimer.
00010     * Redistributions in binary form must reproduce the above copyright
00011       notice, this list of conditions and the following disclaimer in the
00012       documentation and/or other materials provided with the distribution.
00013     * Neither the name of the Sony Computer Entertainment Inc nor the names
00014       of its contributors may be used to endorse or promote products derived
00015       from this software without specific prior written permission.
00016 
00017    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00018    AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019    IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020    ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00021    LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00022    CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00023    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00024    INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00025    CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00026    ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00027    POSSIBILITY OF SUCH DAMAGE.
00028 */
00029 
00030 #ifndef _BOOLINVEC_H
00031 #define _BOOLINVEC_H
00032 
00033 #include <math.h>
00034 
00035 namespace Vectormath {
00036 
00037 class floatInVec;
00038 
00039 //--------------------------------------------------------------------------------------------------
00040 // boolInVec class
00041 //
00042 
00043 class boolInVec
00044 {
00045     private:
00046         __m128 mData;
00047 
00048         inline boolInVec(__m128 vec);
00049     public:
00050         inline boolInVec() {}
00051 
00052         // matches standard type conversions
00053         //
00054         inline boolInVec(const floatInVec &vec);
00055 
00056         // explicit cast from bool
00057         //
00058         explicit inline boolInVec(bool scalar);
00059 
00060 #ifdef _VECTORMATH_NO_SCALAR_CAST
00061         // explicit cast to bool
00062         // 
00063         inline bool getAsBool() const;
00064 #else
00065         // implicit cast to bool
00066         // 
00067         inline operator bool() const;
00068 #endif
00069         
00070         // get vector data
00071         // bool value is splatted across all word slots of vector as 0 (false) or -1 (true)
00072         //
00073         inline __m128 get128() const;
00074 
00075         // operators
00076         //
00077         inline const boolInVec operator ! () const;
00078         inline boolInVec& operator = (const boolInVec &vec);
00079         inline boolInVec& operator &= (const boolInVec &vec);
00080         inline boolInVec& operator ^= (const boolInVec &vec);
00081         inline boolInVec& operator |= (const boolInVec &vec);
00082 
00083         // friend functions
00084         //
00085         friend inline const boolInVec operator == (const boolInVec &vec0, const boolInVec &vec1);
00086         friend inline const boolInVec operator != (const boolInVec &vec0, const boolInVec &vec1);
00087         friend inline const boolInVec operator < (const floatInVec &vec0, const floatInVec &vec1);
00088         friend inline const boolInVec operator <= (const floatInVec &vec0, const floatInVec &vec1);
00089         friend inline const boolInVec operator > (const floatInVec &vec0, const floatInVec &vec1);
00090         friend inline const boolInVec operator >= (const floatInVec &vec0, const floatInVec &vec1);
00091         friend inline const boolInVec operator == (const floatInVec &vec0, const floatInVec &vec1);
00092         friend inline const boolInVec operator != (const floatInVec &vec0, const floatInVec &vec1);
00093         friend inline const boolInVec operator & (const boolInVec &vec0, const boolInVec &vec1);
00094         friend inline const boolInVec operator ^ (const boolInVec &vec0, const boolInVec &vec1);
00095         friend inline const boolInVec operator | (const boolInVec &vec0, const boolInVec &vec1);
00096         friend inline const boolInVec select(const boolInVec &vec0, const boolInVec &vec1, const boolInVec &select_vec1);
00097 };
00098 
00099 //--------------------------------------------------------------------------------------------------
00100 // boolInVec functions
00101 //
00102 
00103 // operators
00104 //
00105 inline const boolInVec operator == (const boolInVec &vec0, const boolInVec &vec1);
00106 inline const boolInVec operator != (const boolInVec &vec0, const boolInVec &vec1);
00107 inline const boolInVec operator & (const boolInVec &vec0, const boolInVec &vec1);
00108 inline const boolInVec operator ^ (const boolInVec &vec0, const boolInVec &vec1);
00109 inline const boolInVec operator | (const boolInVec &vec0, const boolInVec &vec1);
00110 
00111 // select between vec0 and vec1 using boolInVec.
00112 // false selects vec0, true selects vec1
00113 //
00114 inline const boolInVec select(const boolInVec &vec0, const boolInVec &vec1, const boolInVec &select_vec1);
00115 
00116 } // namespace Vectormath
00117 
00118 //--------------------------------------------------------------------------------------------------
00119 // boolInVec implementation
00120 //
00121 
00122 #include "floatInVec.h"
00123 
00124 namespace Vectormath {
00125 
00126 inline
00127 boolInVec::boolInVec(__m128 vec)
00128 {
00129     mData = vec;
00130 }
00131 
00132 inline
00133 boolInVec::boolInVec(const floatInVec &vec)
00134 {
00135     *this = (vec != floatInVec(0.0f));
00136 }
00137 
00138 inline
00139 boolInVec::boolInVec(bool scalar)
00140 {
00141     unsigned int mask = -(int)scalar;
00142         mData = _mm_set1_ps(*(float *)&mask); // TODO: Union
00143 }
00144 
00145 #ifdef _VECTORMATH_NO_SCALAR_CAST
00146 inline
00147 bool
00148 boolInVec::getAsBool() const
00149 #else
00150 inline
00151 boolInVec::operator bool() const
00152 #endif
00153 {
00154         return *(bool *)&mData;
00155 }
00156 
00157 inline
00158 __m128
00159 boolInVec::get128() const
00160 {
00161     return mData;
00162 }
00163 
00164 inline
00165 const boolInVec
00166 boolInVec::operator ! () const
00167 {
00168     return boolInVec(_mm_andnot_ps(mData, _mm_cmpneq_ps(_mm_setzero_ps(),_mm_setzero_ps())));
00169 }
00170 
00171 inline
00172 boolInVec&
00173 boolInVec::operator = (const boolInVec &vec)
00174 {
00175     mData = vec.mData;
00176     return *this;
00177 }
00178 
00179 inline
00180 boolInVec&
00181 boolInVec::operator &= (const boolInVec &vec)
00182 {
00183     *this = *this & vec;
00184     return *this;
00185 }
00186 
00187 inline
00188 boolInVec&
00189 boolInVec::operator ^= (const boolInVec &vec)
00190 {
00191     *this = *this ^ vec;
00192     return *this;
00193 }
00194 
00195 inline
00196 boolInVec&
00197 boolInVec::operator |= (const boolInVec &vec)
00198 {
00199     *this = *this | vec;
00200     return *this;
00201 }
00202 
00203 inline
00204 const boolInVec
00205 operator == (const boolInVec &vec0, const boolInVec &vec1)
00206 {
00207         return boolInVec(_mm_cmpeq_ps(vec0.get128(), vec1.get128()));
00208 }
00209 
00210 inline
00211 const boolInVec
00212 operator != (const boolInVec &vec0, const boolInVec &vec1)
00213 {
00214         return boolInVec(_mm_cmpneq_ps(vec0.get128(), vec1.get128()));
00215 }
00216     
00217 inline
00218 const boolInVec
00219 operator & (const boolInVec &vec0, const boolInVec &vec1)
00220 {
00221         return boolInVec(_mm_and_ps(vec0.get128(), vec1.get128()));
00222 }
00223 
00224 inline
00225 const boolInVec
00226 operator | (const boolInVec &vec0, const boolInVec &vec1)
00227 {
00228         return boolInVec(_mm_or_ps(vec0.get128(), vec1.get128()));
00229 }
00230 
00231 inline
00232 const boolInVec
00233 operator ^ (const boolInVec &vec0, const boolInVec &vec1)
00234 {
00235         return boolInVec(_mm_xor_ps(vec0.get128(), vec1.get128()));
00236 }
00237 
00238 inline
00239 const boolInVec
00240 select(const boolInVec &vec0, const boolInVec &vec1, const boolInVec &select_vec1)
00241 {
00242         return boolInVec(vec_sel(vec0.get128(), vec1.get128(), select_vec1.get128()));
00243 }
00244  
00245 } // namespace Vectormath
00246 
00247 #endif // boolInVec_h