neon/floatInVec.h

Go to the documentation of this file.
00001 /*
00002    Copyright (C) 2009 Sony Computer Entertainment Inc.
00003    All rights reserved.
00004 
00005 This software is provided 'as-is', without any express or implied warranty.
00006 In no event will the authors be held liable for any damages arising from the use of this software.
00007 Permission is granted to anyone to use this software for any purpose, 
00008 including commercial applications, and to alter it and redistribute it freely, 
00009 subject to the following restrictions:
00010 
00011 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
00012 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
00013 3. This notice may not be removed or altered from any source distribution.
00014 
00015 */
00016 #ifndef _FLOATINVEC_H
00017 #define _FLOATINVEC_H
00018 
00019 #include <math.h>
00020 namespace Vectormath {
00021 
00022 class boolInVec;
00023 
00024 //--------------------------------------------------------------------------------------------------
00025 // floatInVec class
00026 //
00027 
00028 // A class representing a scalar float value contained in a vector register
00029 // This class does not support fastmath
00030 class floatInVec
00031 {
00032 private:
00033     float mData;
00034 
00035 public:
00036     // Default constructor; does no initialization
00037     //
00038     inline floatInVec( ) { };
00039 
00040     // Construct from a value converted from bool
00041     //
00042     inline floatInVec(boolInVec vec);
00043 
00044     // Explicit cast from float
00045     //
00046     explicit inline floatInVec(float scalar);
00047 
00048     // Explicit cast to float
00049     //
00050     inline float getAsFloat() const;
00051 
00052 #ifndef _VECTORMATH_NO_SCALAR_CAST
00053     // Implicit cast to float
00054     //
00055     inline operator float() const;
00056 #endif
00057 
00058     // Post increment (add 1.0f)
00059     //
00060     inline const floatInVec operator ++ (int);
00061 
00062     // Post decrement (subtract 1.0f)
00063     //
00064     inline const floatInVec operator -- (int);
00065 
00066     // Pre increment (add 1.0f)
00067     //
00068     inline floatInVec& operator ++ ();
00069 
00070     // Pre decrement (subtract 1.0f)
00071     //
00072     inline floatInVec& operator -- ();
00073 
00074     // Negation operator
00075     //
00076     inline const floatInVec operator - () const;
00077 
00078     // Assignment operator
00079     //
00080     inline floatInVec& operator = (floatInVec vec);
00081 
00082     // Multiplication assignment operator
00083     //
00084     inline floatInVec& operator *= (floatInVec vec);
00085 
00086     // Division assignment operator
00087     //
00088     inline floatInVec& operator /= (floatInVec vec);
00089 
00090     // Addition assignment operator
00091     //
00092     inline floatInVec& operator += (floatInVec vec);
00093 
00094     // Subtraction assignment operator
00095     //
00096     inline floatInVec& operator -= (floatInVec vec);
00097 
00098 };
00099 
00100 // Multiplication operator
00101 //
00102 inline const floatInVec operator * (floatInVec vec0, floatInVec vec1);
00103 
00104 // Division operator
00105 //
00106 inline const floatInVec operator / (floatInVec vec0, floatInVec vec1);
00107 
00108 // Addition operator
00109 //
00110 inline const floatInVec operator + (floatInVec vec0, floatInVec vec1);
00111 
00112 // Subtraction operator
00113 //
00114 inline const floatInVec operator - (floatInVec vec0, floatInVec vec1);
00115 
00116 // Less than operator
00117 //
00118 inline const boolInVec operator < (floatInVec vec0, floatInVec vec1);
00119 
00120 // Less than or equal operator
00121 //
00122 inline const boolInVec operator <= (floatInVec vec0, floatInVec vec1);
00123 
00124 // Greater than operator
00125 //
00126 inline const boolInVec operator > (floatInVec vec0, floatInVec vec1);
00127 
00128 // Greater than or equal operator
00129 //
00130 inline const boolInVec operator >= (floatInVec vec0, floatInVec vec1);
00131 
00132 // Equal operator
00133 //
00134 inline const boolInVec operator == (floatInVec vec0, floatInVec vec1);
00135 
00136 // Not equal operator
00137 //
00138 inline const boolInVec operator != (floatInVec vec0, floatInVec vec1);
00139 
00140 // Conditionally select between two values
00141 //
00142 inline const floatInVec select(floatInVec vec0, floatInVec vec1, boolInVec select_vec1);
00143 
00144 
00145 } // namespace Vectormath
00146 
00147 
00148 //--------------------------------------------------------------------------------------------------
00149 // floatInVec implementation
00150 //
00151 
00152 #include "boolInVec.h"
00153 
00154 namespace Vectormath {
00155 
00156 inline
00157 floatInVec::floatInVec(boolInVec vec)
00158 {
00159     mData = float(vec.getAsBool());
00160 }
00161 
00162 inline
00163 floatInVec::floatInVec(float scalar)
00164 {
00165     mData = scalar;
00166 }
00167 
00168 inline
00169 float
00170 floatInVec::getAsFloat() const
00171 {
00172     return mData;
00173 }
00174 
00175 #ifndef _VECTORMATH_NO_SCALAR_CAST
00176 inline
00177 floatInVec::operator float() const
00178 {
00179     return getAsFloat();
00180 }
00181 #endif
00182 
00183 inline
00184 const floatInVec
00185 floatInVec::operator ++ (int)
00186 {
00187     float olddata = mData;
00188     operator ++();
00189     return floatInVec(olddata);
00190 }
00191 
00192 inline
00193 const floatInVec
00194 floatInVec::operator -- (int)
00195 {
00196     float olddata = mData;
00197     operator --();
00198     return floatInVec(olddata);
00199 }
00200 
00201 inline
00202 floatInVec&
00203 floatInVec::operator ++ ()
00204 {
00205     *this += floatInVec(1.0f);
00206     return *this;
00207 }
00208 
00209 inline
00210 floatInVec&
00211 floatInVec::operator -- ()
00212 {
00213     *this -= floatInVec(1.0f);
00214     return *this;
00215 }
00216 
00217 inline
00218 const floatInVec
00219 floatInVec::operator - () const
00220 {
00221     return floatInVec(-mData);
00222 }
00223 
00224 inline
00225 floatInVec&
00226 floatInVec::operator = (floatInVec vec)
00227 {
00228     mData = vec.mData;
00229     return *this;
00230 }
00231 
00232 inline
00233 floatInVec&
00234 floatInVec::operator *= (floatInVec vec)
00235 {
00236     *this = *this * vec;
00237     return *this;
00238 }
00239 
00240 inline
00241 floatInVec&
00242 floatInVec::operator /= (floatInVec vec)
00243 {
00244     *this = *this / vec;
00245     return *this;
00246 }
00247 
00248 inline
00249 floatInVec&
00250 floatInVec::operator += (floatInVec vec)
00251 {
00252     *this = *this + vec;
00253     return *this;
00254 }
00255 
00256 inline
00257 floatInVec&
00258 floatInVec::operator -= (floatInVec vec)
00259 {
00260     *this = *this - vec;
00261     return *this;
00262 }
00263 
00264 inline
00265 const floatInVec
00266 operator * (floatInVec vec0, floatInVec vec1)
00267 {
00268     return floatInVec(vec0.getAsFloat() * vec1.getAsFloat());
00269 }
00270 
00271 inline
00272 const floatInVec
00273 operator / (floatInVec num, floatInVec den)
00274 {
00275     return floatInVec(num.getAsFloat() / den.getAsFloat());
00276 }
00277 
00278 inline
00279 const floatInVec
00280 operator + (floatInVec vec0, floatInVec vec1)
00281 {
00282     return floatInVec(vec0.getAsFloat() + vec1.getAsFloat());
00283 }
00284 
00285 inline
00286 const floatInVec
00287 operator - (floatInVec vec0, floatInVec vec1)
00288 {
00289     return floatInVec(vec0.getAsFloat() - vec1.getAsFloat());
00290 }
00291 
00292 inline
00293 const boolInVec
00294 operator < (floatInVec vec0, floatInVec vec1)
00295 {
00296     return boolInVec(vec0.getAsFloat() < vec1.getAsFloat());
00297 }
00298 
00299 inline
00300 const boolInVec
00301 operator <= (floatInVec vec0, floatInVec vec1)
00302 {
00303     return !(vec0 > vec1);
00304 }
00305 
00306 inline
00307 const boolInVec
00308 operator > (floatInVec vec0, floatInVec vec1)
00309 {
00310     return boolInVec(vec0.getAsFloat() > vec1.getAsFloat());
00311 }
00312 
00313 inline
00314 const boolInVec
00315 operator >= (floatInVec vec0, floatInVec vec1)
00316 {
00317     return !(vec0 < vec1);
00318 }
00319 
00320 inline
00321 const boolInVec
00322 operator == (floatInVec vec0, floatInVec vec1)
00323 {
00324     return boolInVec(vec0.getAsFloat() == vec1.getAsFloat());
00325 }
00326 
00327 inline
00328 const boolInVec
00329 operator != (floatInVec vec0, floatInVec vec1)
00330 {
00331     return !(vec0 == vec1);
00332 }
00333 
00334 inline
00335 const floatInVec
00336 select(floatInVec vec0, floatInVec vec1, boolInVec select_vec1)
00337 {
00338     return (select_vec1.getAsBool() == 0) ? vec0 : vec1;
00339 }
00340 
00341 } // namespace Vectormath
00342 
00343 #endif // floatInVec_h
00344