Vector3SSE.h

Go to the documentation of this file.
00001 /**<!-------------------------------------------------------------------->
00002    @file   Vector3SSE.h
00003    @author tRAVIS fIscher (fisch0920@gmail.com)
00004    @date   Fall 2008
00005    
00006    @brief
00007       Specialized implementation of Vector3, accelerated to support efficient 
00008    SIMD vectorization via SSE intrinsics
00009    
00010    @note this file is not meant to be #included directly
00011    @see Vector.h for more details
00012    <!-------------------------------------------------------------------->**/
00013 
00014 #ifndef VECTOR3_SSE_H_
00015 #define VECTOR3_SSE_H_
00016 
00017 #include <common/math/simd/SIMD.h>
00018 
00019 #define VECTOR3_NO_ELEMENTS    4
00020 #define VECTOR3_SIZE           (sizeof(real_t) * VECTOR3_NO_ELEMENTS)
00021 
00022 /**
00023  * @brief
00024  *    Specialized template implementation for Vector3, accelerated to support 
00025  * efficient SIMD vectorization via SSE intrinsics
00026  * 
00027  * @note implementation uses single-precision floating point arithmetic
00028  * @see for more information on SSE and SIMD:
00029  *    ftp://download.intel.com/support/performancetools/c/linux/v9/intref_cls.pdf
00030  */
00031 template <>
00032 SSE_ALIGN16_PRE struct Vector<3, real_t> : public SSEAligned {
00033    
00034    SSE_ALIGN16_PRE union {
00035       /// Underlying data stored as an N-length array of type T
00036       float   data[VECTOR3_NO_ELEMENTS]; // padded to 16 bytes
00037       
00038       /// Also accessible via a native SSE 128-bit register
00039       m128f_t vec;
00040       
00041       struct { real_t x, y, z, w; };
00042    } SSE_ALIGN16_POST;
00043    
00044    
00045    ///@name Constructors
00046    //@{-----------------------------------------------------------------
00047    
00048    /// Expects N arguments of type float
00049    inline Vector(const real_t *srcData);
00050    
00051    /// Constructs a zero Vector
00052    inline Vector();
00053    
00054    /// Copy Constructor
00055    inline Vector(const Vector<3, real_t> &v);
00056    
00057    /// SSE copy constructor
00058    inline Vector(const m128f_t &v);
00059    
00060    
00061    //@}-----------------------------------------------------------------
00062    ///@name Overridden memory operations for alignment guarantees
00063    //@{-----------------------------------------------------------------
00064    
00065    /// @see also SIMD.h
00066    DECLARE_ALIGNED_MEMORY_OPERATORS
00067    
00068    
00069    //@}-----------------------------------------------------------------
00070    ///@name Static convenience constructors to generate common matrices
00071    //@{-----------------------------------------------------------------
00072    
00073    /// Generates a vector full of zeroes
00074    static inline Vector<3, real_t> zero() {
00075       return Vector<3, real_t>();
00076    }
00077    
00078    /// Generates a vector full of ones
00079    static inline Vector<3, real_t> ones() {
00080       return Vector<3, real_t>::fill(1);
00081    }
00082    
00083    /// Generates a vector filled with the specified value
00084    static inline Vector<3, real_t> fill(const real_t &val) {
00085       return Vector<3, real_t>(_mm_set_ps1(val));
00086    }
00087    
00088    /// @returns the element-wise minimum vector from the two given vectors
00089    static inline Vector<3, real_t> min(const Vector<3, real_t> &v1, 
00090                                        const Vector<3, real_t> &v2)
00091    {
00092       return Vector<3, real_t>(_mm_min_ps(v1.vec, v2.vec));
00093    }
00094    
00095    /// @returns the element-wise maximum vector from the two given vectors
00096    static inline Vector<3, real_t> max(const Vector<3, real_t> &v1, 
00097                                        const Vector<3, real_t> &v2)
00098    {
00099       return Vector<3, real_t>(_mm_max_ps(v1.vec, v2.vec));
00100    }
00101    
00102    /// Generates a vector filled with the maximum real_t (infinity)
00103    static inline Vector<3, real_t> infinity() {
00104       return Vector<3, real_t>::fill(std::numeric_limits<real_t>::max());
00105    }
00106    
00107    /// Generates a vector filled with the minimum real_t (negative infinity)
00108    static inline Vector<3, real_t> negativeInfinity() {
00109       // note std::numeric_limits<real_t>::min() for float and double returns 
00110       // the smallest positive number representable, not the smallest 
00111       // valid floating point number that type can hold...
00112       return Vector<3, real_t>::fill(-std::numeric_limits<real_t>::max());
00113    }
00114    
00115    /// theta is latitude angle; phi is longitude angle
00116    static inline Vector<3, real_t> fromSpherical(real_t theta, real_t phi) {
00117       return Vector<3, real_t>(sin(theta) * cos(phi), 
00118                                cos(theta), 
00119                                sin(theta) * sin(phi)).getNormalized();
00120    }
00121    
00122    
00123    //@}-----------------------------------------------------------------
00124    ///@name Accessor Operators
00125    //@{-----------------------------------------------------------------
00126 
00127    /// @returns a const reference to the element at the given index
00128    inline const real_t &operator[](const unsigned index) const;
00129    /// @returns a       reference to the element at the given index
00130    /// @note changes to the returned element will affect this vector
00131    inline       real_t &operator[](const unsigned index);
00132    
00133    /// @returns a pointer to the underlying data (N-length array of type real_t)
00134    inline const real_t *operator*() const;
00135    /// @returns a pointer to the underlying data (N-length array of type real_t)
00136    inline       real_t *operator*();
00137    
00138    
00139    //@}-----------------------------------------------------------------
00140    ///@name Implicit casting Operators
00141    //@{-----------------------------------------------------------------
00142    
00143    inline operator m128f_t() const {
00144       return vec;
00145    }
00146    
00147    
00148    //@}-----------------------------------------------------------------
00149    ///@name Equality Operators
00150    //@{-----------------------------------------------------------------
00151    
00152    inline       bool       operator==(const Vector<3, real_t> &v) const;
00153    inline       bool       operator!=(const Vector<3, real_t> &v) const;
00154    
00155    
00156    //@}-----------------------------------------------------------------
00157    ///@name Mutator Operators
00158    //@{-----------------------------------------------------------------
00159    
00160    inline       Vector<3, real_t> &operator =(const Vector<3, real_t> &v);
00161    inline       Vector<3, real_t> &operator+=(const Vector<3, real_t> &rhs);
00162    inline       Vector<3, real_t> &operator-=(const Vector<3, real_t> &rhs);
00163    
00164    
00165    //@}-----------------------------------------------------------------
00166    ///@name Scalar Mutator Operators
00167    //@{-----------------------------------------------------------------
00168    
00169    inline       Vector<3, real_t> &operator*=(const real_t &scale);
00170    inline       Vector<3, real_t> &operator/=(const real_t &scale);
00171    
00172    
00173    //@}-----------------------------------------------------------------
00174    ///@name Arithmetic Operators
00175    //@{-----------------------------------------------------------------
00176    
00177    inline       Vector<3, real_t>  operator+ (const Vector<3, real_t> &rhs) const;
00178    inline       Vector<3, real_t>  operator- (const Vector<3, real_t> &rhs) const;
00179    
00180    /// @returns element-wise multiplication of two vectors
00181    inline       Vector<3, real_t>  operator* (const Vector<3, real_t> &rhs) const;
00182    
00183    
00184    //@}-----------------------------------------------------------------
00185    ///@name Scalar Arithmetic Operators
00186    //@{-----------------------------------------------------------------
00187    
00188    inline       Vector<3, real_t>  operator* (const real_t &scale) const;
00189    inline       Vector<3, real_t>  operator/ (const real_t &scale) const;
00190    
00191    
00192    //@}-----------------------------------------------------------------
00193    ///@name More Complex Functionality
00194    //@{-----------------------------------------------------------------
00195    
00196    /// @returns whether or not this Vector is unitized
00197    inline bool isUnit() const;
00198    
00199    /// @returns whether or not this Vector is the zero vector
00200    inline bool isZero() const;
00201    
00202    /// @returns a normalized version of this vector
00203    inline Vector<3, real_t> getNormalized() const;   
00204    
00205    /// @returns the reciprocal version of this vector (1.0f / this)
00206    inline Vector<3, real_t> getReciprocal() const;
00207    
00208    /// Normalizes this Vector and returns the old magnitude
00209    inline real_t normalize();
00210    
00211    /// @returns the magnitude of this vector
00212    inline real_t getMagnitude() const;
00213    
00214    /// @returns the squared magnitude of this vector
00215    inline real_t getMagnitude2() const;
00216    
00217    /// @returns the magnitude of the vector connecting this vector to the one 
00218    ///    passed in
00219    inline real_t getDistance(const Vector<3, real_t> &v) const; 
00220    
00221    /// @returns the squared magnitude of the vector connecting this vector to 
00222    ///    the one passed in
00223    inline real_t getDistance2(const Vector<3, real_t> &v) const;
00224    
00225    /// @returns the sum of the components in this Vector
00226    inline real_t getSum() const;
00227    
00228    /// @returns the average of the components in this Vector
00229    inline real_t getAverage() const;
00230    
00231    /// @returns the dot (inner) product of two vectors
00232    inline real_t dot(const Vector<3, real_t> &rhs) const;
00233    
00234    /// @returns dimension (0,1,...,N) of maximum length
00235    inline unsigned getMaxDimension() const;
00236    
00237    /// @returns dimension (0,1,...,N) of minimum length
00238    inline unsigned getMinDimension() const;
00239    
00240    /// Cleans up vector (0's out entries that are less than epsilon)
00241    void cleanup();
00242    
00243    
00244    //@}-----------------------------------------------------------------
00245    ///@name Specialized Vector Functionality
00246    //@{-----------------------------------------------------------------
00247    
00248    /// Convenience Constructor
00249    inline explicit Vector(const real_t &v0, const real_t &v1 = 0, 
00250                           const real_t &v2 = 0);
00251    
00252    /// @returns the cross product of two vectors
00253    inline Vector<3, real_t> cross(const Vector<3, real_t> &rhs) const;
00254    
00255    /**
00256     * Assumes normal is normalized, incident vector is incoming and
00257     * resultant vector is exiting
00258     * 
00259     * @note only defined for Vector3
00260     */
00261    Vector<3, real_t> reflectVector(const Vector<3, real_t> &normal) const;
00262    Vector<3, real_t> refractVector(const Vector<3, real_t> &normal, 
00263                                    real_t in, real_t out = 1.0f) const;
00264    
00265    /**
00266     * @returns an orthonormal basis constructed from this vector in out 
00267     *    parameters U and V
00268     * 
00269     * @note only defined for Vector3
00270     */
00271    void getOrthonormalBasis(Vector<3, real_t> &U, Vector<3, real_t> &V);
00272    
00273    
00274    //@}-----------------------------------------------------------------
00275 } SSE_ALIGN16_POST;
00276 
00277 /* Include inline implementations */
00278 #include <common/math/Vector3SSE.inl>
00279 
00280 #endif // VECTOR3_SSE_H_
00281 

Generated on 28 Feb 2009 for Milton by doxygen 1.5.6