updated vectormath (minor fixes, including a bugfix in storeXYZ)

This commit is contained in:
erwin.coumans 2009-12-19 02:08:44 +00:00
parent fa778a649c
commit d68521803f
6 changed files with 956 additions and 115 deletions

View File

@ -0,0 +1,225 @@
/*
Copyright (C) 2009 Sony Computer Entertainment Inc.
All rights reserved.
This software is provided 'as-is', without any express or implied warranty.
In no event will the authors be held liable for any damages arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it freely,
subject to the following restrictions:
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.
2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#ifndef _BOOLINVEC_H
#define _BOOLINVEC_H
#include <math.h>
namespace Vectormath {
class floatInVec;
//--------------------------------------------------------------------------------------------------
// boolInVec class
//
class boolInVec
{
private:
unsigned int mData;
public:
// Default constructor; does no initialization
//
inline boolInVec( ) { };
// Construct from a value converted from float
//
inline boolInVec(floatInVec vec);
// Explicit cast from bool
//
explicit inline boolInVec(bool scalar);
// Explicit cast to bool
//
inline bool getAsBool() const;
#ifndef _VECTORMATH_NO_SCALAR_CAST
// Implicit cast to bool
//
inline operator bool() const;
#endif
// Boolean negation operator
//
inline const boolInVec operator ! () const;
// Assignment operator
//
inline boolInVec& operator = (boolInVec vec);
// Boolean and assignment operator
//
inline boolInVec& operator &= (boolInVec vec);
// Boolean exclusive or assignment operator
//
inline boolInVec& operator ^= (boolInVec vec);
// Boolean or assignment operator
//
inline boolInVec& operator |= (boolInVec vec);
};
// Equal operator
//
inline const boolInVec operator == (boolInVec vec0, boolInVec vec1);
// Not equal operator
//
inline const boolInVec operator != (boolInVec vec0, boolInVec vec1);
// And operator
//
inline const boolInVec operator & (boolInVec vec0, boolInVec vec1);
// Exclusive or operator
//
inline const boolInVec operator ^ (boolInVec vec0, boolInVec vec1);
// Or operator
//
inline const boolInVec operator | (boolInVec vec0, boolInVec vec1);
// Conditionally select between two values
//
inline const boolInVec select(boolInVec vec0, boolInVec vec1, boolInVec select_vec1);
} // namespace Vectormath
//--------------------------------------------------------------------------------------------------
// boolInVec implementation
//
#include "floatInVec.h"
namespace Vectormath {
inline
boolInVec::boolInVec(floatInVec vec)
{
*this = (vec != floatInVec(0.0f));
}
inline
boolInVec::boolInVec(bool scalar)
{
mData = -(int)scalar;
}
inline
bool
boolInVec::getAsBool() const
{
return (mData > 0);
}
#ifndef _VECTORMATH_NO_SCALAR_CAST
inline
boolInVec::operator bool() const
{
return getAsBool();
}
#endif
inline
const boolInVec
boolInVec::operator ! () const
{
return boolInVec(!mData);
}
inline
boolInVec&
boolInVec::operator = (boolInVec vec)
{
mData = vec.mData;
return *this;
}
inline
boolInVec&
boolInVec::operator &= (boolInVec vec)
{
*this = *this & vec;
return *this;
}
inline
boolInVec&
boolInVec::operator ^= (boolInVec vec)
{
*this = *this ^ vec;
return *this;
}
inline
boolInVec&
boolInVec::operator |= (boolInVec vec)
{
*this = *this | vec;
return *this;
}
inline
const boolInVec
operator == (boolInVec vec0, boolInVec vec1)
{
return boolInVec(vec0.getAsBool() == vec1.getAsBool());
}
inline
const boolInVec
operator != (boolInVec vec0, boolInVec vec1)
{
return !(vec0 == vec1);
}
inline
const boolInVec
operator & (boolInVec vec0, boolInVec vec1)
{
return boolInVec(vec0.getAsBool() & vec1.getAsBool());
}
inline
const boolInVec
operator | (boolInVec vec0, boolInVec vec1)
{
return boolInVec(vec0.getAsBool() | vec1.getAsBool());
}
inline
const boolInVec
operator ^ (boolInVec vec0, boolInVec vec1)
{
return boolInVec(vec0.getAsBool() ^ vec1.getAsBool());
}
inline
const boolInVec
select(boolInVec vec0, boolInVec vec1, boolInVec select_vec1)
{
return (select_vec1.getAsBool() == 0) ? vec0 : vec1;
}
} // namespace Vectormath
#endif // boolInVec_h

View File

@ -0,0 +1,343 @@
/*
Copyright (C) 2009 Sony Computer Entertainment Inc.
All rights reserved.
This software is provided 'as-is', without any express or implied warranty.
In no event will the authors be held liable for any damages arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it freely,
subject to the following restrictions:
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.
2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#ifndef _FLOATINVEC_H
#define _FLOATINVEC_H
#include <math.h>
namespace Vectormath {
class boolInVec;
//--------------------------------------------------------------------------------------------------
// floatInVec class
//
// A class representing a scalar float value contained in a vector register
// This class does not support fastmath
class floatInVec
{
private:
float mData;
public:
// Default constructor; does no initialization
//
inline floatInVec( ) { };
// Construct from a value converted from bool
//
inline floatInVec(boolInVec vec);
// Explicit cast from float
//
explicit inline floatInVec(float scalar);
// Explicit cast to float
//
inline float getAsFloat() const;
#ifndef _VECTORMATH_NO_SCALAR_CAST
// Implicit cast to float
//
inline operator float() const;
#endif
// Post increment (add 1.0f)
//
inline const floatInVec operator ++ (int);
// Post decrement (subtract 1.0f)
//
inline const floatInVec operator -- (int);
// Pre increment (add 1.0f)
//
inline floatInVec& operator ++ ();
// Pre decrement (subtract 1.0f)
//
inline floatInVec& operator -- ();
// Negation operator
//
inline const floatInVec operator - () const;
// Assignment operator
//
inline floatInVec& operator = (floatInVec vec);
// Multiplication assignment operator
//
inline floatInVec& operator *= (floatInVec vec);
// Division assignment operator
//
inline floatInVec& operator /= (floatInVec vec);
// Addition assignment operator
//
inline floatInVec& operator += (floatInVec vec);
// Subtraction assignment operator
//
inline floatInVec& operator -= (floatInVec vec);
};
// Multiplication operator
//
inline const floatInVec operator * (floatInVec vec0, floatInVec vec1);
// Division operator
//
inline const floatInVec operator / (floatInVec vec0, floatInVec vec1);
// Addition operator
//
inline const floatInVec operator + (floatInVec vec0, floatInVec vec1);
// Subtraction operator
//
inline const floatInVec operator - (floatInVec vec0, floatInVec vec1);
// Less than operator
//
inline const boolInVec operator < (floatInVec vec0, floatInVec vec1);
// Less than or equal operator
//
inline const boolInVec operator <= (floatInVec vec0, floatInVec vec1);
// Greater than operator
//
inline const boolInVec operator > (floatInVec vec0, floatInVec vec1);
// Greater than or equal operator
//
inline const boolInVec operator >= (floatInVec vec0, floatInVec vec1);
// Equal operator
//
inline const boolInVec operator == (floatInVec vec0, floatInVec vec1);
// Not equal operator
//
inline const boolInVec operator != (floatInVec vec0, floatInVec vec1);
// Conditionally select between two values
//
inline const floatInVec select(floatInVec vec0, floatInVec vec1, boolInVec select_vec1);
} // namespace Vectormath
//--------------------------------------------------------------------------------------------------
// floatInVec implementation
//
#include "boolInVec.h"
namespace Vectormath {
inline
floatInVec::floatInVec(boolInVec vec)
{
mData = float(vec.getAsBool());
}
inline
floatInVec::floatInVec(float scalar)
{
mData = scalar;
}
inline
float
floatInVec::getAsFloat() const
{
return mData;
}
#ifndef _VECTORMATH_NO_SCALAR_CAST
inline
floatInVec::operator float() const
{
return getAsFloat();
}
#endif
inline
const floatInVec
floatInVec::operator ++ (int)
{
float olddata = mData;
operator ++();
return floatInVec(olddata);
}
inline
const floatInVec
floatInVec::operator -- (int)
{
float olddata = mData;
operator --();
return floatInVec(olddata);
}
inline
floatInVec&
floatInVec::operator ++ ()
{
*this += floatInVec(1.0f);
return *this;
}
inline
floatInVec&
floatInVec::operator -- ()
{
*this -= floatInVec(1.0f);
return *this;
}
inline
const floatInVec
floatInVec::operator - () const
{
return floatInVec(-mData);
}
inline
floatInVec&
floatInVec::operator = (floatInVec vec)
{
mData = vec.mData;
return *this;
}
inline
floatInVec&
floatInVec::operator *= (floatInVec vec)
{
*this = *this * vec;
return *this;
}
inline
floatInVec&
floatInVec::operator /= (floatInVec vec)
{
*this = *this / vec;
return *this;
}
inline
floatInVec&
floatInVec::operator += (floatInVec vec)
{
*this = *this + vec;
return *this;
}
inline
floatInVec&
floatInVec::operator -= (floatInVec vec)
{
*this = *this - vec;
return *this;
}
inline
const floatInVec
operator * (floatInVec vec0, floatInVec vec1)
{
return floatInVec(vec0.getAsFloat() * vec1.getAsFloat());
}
inline
const floatInVec
operator / (floatInVec num, floatInVec den)
{
return floatInVec(num.getAsFloat() / den.getAsFloat());
}
inline
const floatInVec
operator + (floatInVec vec0, floatInVec vec1)
{
return floatInVec(vec0.getAsFloat() + vec1.getAsFloat());
}
inline
const floatInVec
operator - (floatInVec vec0, floatInVec vec1)
{
return floatInVec(vec0.getAsFloat() - vec1.getAsFloat());
}
inline
const boolInVec
operator < (floatInVec vec0, floatInVec vec1)
{
return boolInVec(vec0.getAsFloat() < vec1.getAsFloat());
}
inline
const boolInVec
operator <= (floatInVec vec0, floatInVec vec1)
{
return !(vec0 > vec1);
}
inline
const boolInVec
operator > (floatInVec vec0, floatInVec vec1)
{
return boolInVec(vec0.getAsFloat() > vec1.getAsFloat());
}
inline
const boolInVec
operator >= (floatInVec vec0, floatInVec vec1)
{
return !(vec0 < vec1);
}
inline
const boolInVec
operator == (floatInVec vec0, floatInVec vec1)
{
return boolInVec(vec0.getAsFloat() == vec1.getAsFloat());
}
inline
const boolInVec
operator != (floatInVec vec0, floatInVec vec1)
{
return !(vec0 == vec1);
}
inline
const floatInVec
select(floatInVec vec0, floatInVec vec1, boolInVec select_vec1)
{
return (select_vec1.getAsBool() == 0) ? vec0 : vec1;
}
} // namespace Vectormath
#endif // floatInVec_h

View File

@ -1,30 +1,17 @@
/*
Copyright (C) 2006, 2007 Sony Computer Entertainment Inc.
Copyright (C) 2009 Sony Computer Entertainment Inc.
All rights reserved.
Redistribution and use in source and binary forms,
with or without modification, are permitted provided that the
following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the Sony Computer Entertainment Inc nor the names
of its contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
This software is provided 'as-is', without any express or implied warranty.
In no event will the authors be held liable for any damages arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it freely,
subject to the following restrictions:
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.
2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _VECTORMATH_MAT_AOS_CPP_H

View File

@ -1,34 +1,22 @@
/*
Copyright (C) 2006, 2007 Sony Computer Entertainment Inc.
Copyright (C) 2009 Sony Computer Entertainment Inc.
All rights reserved.
Redistribution and use in source and binary forms,
with or without modification, are permitted provided that the
following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the Sony Computer Entertainment Inc nor the names
of its contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
This software is provided 'as-is', without any express or implied warranty.
In no event will the authors be held liable for any damages arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it freely,
subject to the following restrictions:
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.
2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _VECTORMATH_QUAT_AOS_CPP_H
#define _VECTORMATH_QUAT_AOS_CPP_H
//-----------------------------------------------------------------------------
// Definitions
@ -40,17 +28,6 @@
namespace Vectormath {
namespace Aos {
inline void loadXYZW( Quat & quat, const float* quad )
{
quat = Quat( *quad );
}
inline void storeXYZW( Quat vec, float * fptr )
{
vec = Quat(fptr[0],fptr[1],fptr[2],fptr[3]);
}
inline Quat::Quat( const Quat & quat )
{
mX = quat.mX;
@ -130,6 +107,19 @@ inline const Quat squad( float t, const Quat & unitQuat0, const Quat & unitQuat1
return slerp( ( ( 2.0f * t ) * ( 1.0f - t ) ), tmp0, tmp1 );
}
inline void loadXYZW( Quat & quat, const float * fptr )
{
quat = Quat( fptr[0], fptr[1], fptr[2], fptr[3] );
}
inline void storeXYZW( const Quat & quat, float * fptr )
{
fptr[0] = quat.getX();
fptr[1] = quat.getY();
fptr[2] = quat.getZ();
fptr[3] = quat.getW();
}
inline Quat & Quat::operator =( const Quat & quat )
{
mX = quat.mX;
@ -318,7 +308,7 @@ inline float norm( const Quat & quat )
inline float length( const Quat & quat )
{
return sqrtf( norm( quat ) );
return ::sqrtf( norm( quat ) );
}
inline const Quat normalize( const Quat & quat )

View File

@ -1,34 +1,22 @@
/*
Copyright (C) 2006, 2007 Sony Computer Entertainment Inc.
Copyright (C) 2009 Sony Computer Entertainment Inc.
All rights reserved.
Redistribution and use in source and binary forms,
with or without modification, are permitted provided that the
following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the Sony Computer Entertainment Inc nor the names
of its contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
This software is provided 'as-is', without any express or implied warranty.
In no event will the authors be held liable for any damages arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it freely,
subject to the following restrictions:
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.
2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _VECTORMATH_VEC_AOS_CPP_H
#define _VECTORMATH_VEC_AOS_CPP_H
//-----------------------------------------------------------------------------
// Constants
@ -109,25 +97,93 @@ inline const Vector3 slerp( float t, const Vector3 & unitVec0, const Vector3 & u
return ( ( unitVec0 * scale0 ) + ( unitVec1 * scale1 ) );
}
inline void loadXYZ( Vector3 & vec, const float * quad )
inline void loadXYZ( Vector3 & vec, const float * fptr )
{
vec = Vector3( *quad );
vec = Vector3( fptr[0], fptr[1], fptr[2] );
}
inline void loadXYZW( Vector4 & vec, const float * quad )
inline void storeXYZ( const Vector3 & vec, float * fptr )
{
vec = Vector4( *quad );
fptr[0] = vec.getX();
fptr[1] = vec.getY();
fptr[2] = vec.getZ();
}
inline void storeXYZ( Vector3 vec, float * fptr )
inline void loadHalfFloats( Vector3 & vec, const unsigned short * hfptr )
{
vec = Vector3(fptr[0],fptr[1],fptr[2]);
union Data32 {
unsigned int u32;
float f32;
};
for (int i = 0; i < 3; i++) {
unsigned short fp16 = hfptr[i];
unsigned int sign = fp16 >> 15;
unsigned int exponent = (fp16 >> 10) & ((1 << 5) - 1);
unsigned int mantissa = fp16 & ((1 << 10) - 1);
if (exponent == 0) {
// zero
mantissa = 0;
} else if (exponent == 31) {
// infinity or nan -> infinity
exponent = 255;
mantissa = 0;
} else {
exponent += 127 - 15;
mantissa <<= 13;
}
Data32 d;
d.u32 = (sign << 31) | (exponent << 23) | mantissa;
vec[i] = d.f32;
}
}
inline void storeHalfFloats( const Vector3 & vec, unsigned short * hfptr )
{
union Data32 {
unsigned int u32;
float f32;
};
for (int i = 0; i < 3; i++) {
Data32 d;
d.f32 = vec[i];
unsigned int sign = d.u32 >> 31;
unsigned int exponent = (d.u32 >> 23) & ((1 << 8) - 1);
unsigned int mantissa = d.u32 & ((1 << 23) - 1);;
if (exponent == 0) {
// zero or denorm -> zero
mantissa = 0;
} else if (exponent == 255 && mantissa != 0) {
// nan -> infinity
exponent = 31;
mantissa = 0;
} else if (exponent >= 127 - 15 + 31) {
// overflow or infinity -> infinity
exponent = 31;
mantissa = 0;
} else if (exponent <= 127 - 15) {
// underflow -> zero
exponent = 0;
mantissa = 0;
} else {
exponent -= 127 - 15;
mantissa >>= 13;
}
hfptr[i] = (unsigned short)((sign << 15) | (exponent << 10) | mantissa);
}
}
inline Vector3 & Vector3::operator =( const Vector3 & vec )
{
@ -399,7 +455,7 @@ inline float lengthSqr( const Vector3 & vec )
inline float length( const Vector3 & vec )
{
return sqrtf( lengthSqr( vec ) );
return ::sqrtf( lengthSqr( vec ) );
}
inline const Vector3 normalize( const Vector3 & vec )
@ -541,6 +597,95 @@ inline const Vector4 slerp( float t, const Vector4 & unitVec0, const Vector4 & u
return ( ( unitVec0 * scale0 ) + ( unitVec1 * scale1 ) );
}
inline void loadXYZW( Vector4 & vec, const float * fptr )
{
vec = Vector4( fptr[0], fptr[1], fptr[2], fptr[3] );
}
inline void storeXYZW( const Vector4 & vec, float * fptr )
{
fptr[0] = vec.getX();
fptr[1] = vec.getY();
fptr[2] = vec.getZ();
fptr[3] = vec.getW();
}
inline void loadHalfFloats( Vector4 & vec, const unsigned short * hfptr )
{
union Data32 {
unsigned int u32;
float f32;
};
for (int i = 0; i < 4; i++) {
unsigned short fp16 = hfptr[i];
unsigned int sign = fp16 >> 15;
unsigned int exponent = (fp16 >> 10) & ((1 << 5) - 1);
unsigned int mantissa = fp16 & ((1 << 10) - 1);
if (exponent == 0) {
// zero
mantissa = 0;
} else if (exponent == 31) {
// infinity or nan -> infinity
exponent = 255;
mantissa = 0;
} else {
exponent += 127 - 15;
mantissa <<= 13;
}
Data32 d;
d.u32 = (sign << 31) | (exponent << 23) | mantissa;
vec[i] = d.f32;
}
}
inline void storeHalfFloats( const Vector4 & vec, unsigned short * hfptr )
{
union Data32 {
unsigned int u32;
float f32;
};
for (int i = 0; i < 4; i++) {
Data32 d;
d.f32 = vec[i];
unsigned int sign = d.u32 >> 31;
unsigned int exponent = (d.u32 >> 23) & ((1 << 8) - 1);
unsigned int mantissa = d.u32 & ((1 << 23) - 1);;
if (exponent == 0) {
// zero or denorm -> zero
mantissa = 0;
} else if (exponent == 255 && mantissa != 0) {
// nan -> infinity
exponent = 31;
mantissa = 0;
} else if (exponent >= 127 - 15 + 31) {
// overflow or infinity -> infinity
exponent = 31;
mantissa = 0;
} else if (exponent <= 127 - 15) {
// underflow -> zero
exponent = 0;
mantissa = 0;
} else {
exponent -= 127 - 15;
mantissa >>= 13;
}
hfptr[i] = (unsigned short)((sign << 15) | (exponent << 10) | mantissa);
}
}
inline Vector4 & Vector4::operator =( const Vector4 & vec )
{
mX = vec.mX;
@ -846,7 +991,7 @@ inline float lengthSqr( const Vector4 & vec )
inline float length( const Vector4 & vec )
{
return sqrtf( lengthSqr( vec ) );
return ::sqrtf( lengthSqr( vec ) );
}
inline const Vector4 normalize( const Vector4 & vec )
@ -919,6 +1064,94 @@ inline const Point3 lerp( float t, const Point3 & pnt0, const Point3 & pnt1 )
return ( pnt0 + ( ( pnt1 - pnt0 ) * t ) );
}
inline void loadXYZ( Point3 & pnt, const float * fptr )
{
pnt = Point3( fptr[0], fptr[1], fptr[2] );
}
inline void storeXYZ( const Point3 & pnt, float * fptr )
{
fptr[0] = pnt.getX();
fptr[1] = pnt.getY();
fptr[2] = pnt.getZ();
}
inline void loadHalfFloats( Point3 & vec, const unsigned short * hfptr )
{
union Data32 {
unsigned int u32;
float f32;
};
for (int i = 0; i < 3; i++) {
unsigned short fp16 = hfptr[i];
unsigned int sign = fp16 >> 15;
unsigned int exponent = (fp16 >> 10) & ((1 << 5) - 1);
unsigned int mantissa = fp16 & ((1 << 10) - 1);
if (exponent == 0) {
// zero
mantissa = 0;
} else if (exponent == 31) {
// infinity or nan -> infinity
exponent = 255;
mantissa = 0;
} else {
exponent += 127 - 15;
mantissa <<= 13;
}
Data32 d;
d.u32 = (sign << 31) | (exponent << 23) | mantissa;
vec[i] = d.f32;
}
}
inline void storeHalfFloats( const Point3 & vec, unsigned short * hfptr )
{
union Data32 {
unsigned int u32;
float f32;
};
for (int i = 0; i < 3; i++) {
Data32 d;
d.f32 = vec[i];
unsigned int sign = d.u32 >> 31;
unsigned int exponent = (d.u32 >> 23) & ((1 << 8) - 1);
unsigned int mantissa = d.u32 & ((1 << 23) - 1);;
if (exponent == 0) {
// zero or denorm -> zero
mantissa = 0;
} else if (exponent == 255 && mantissa != 0) {
// nan -> infinity
exponent = 31;
mantissa = 0;
} else if (exponent >= 127 - 15 + 31) {
// overflow or infinity -> infinity
exponent = 31;
mantissa = 0;
} else if (exponent <= 127 - 15) {
// underflow -> zero
exponent = 0;
mantissa = 0;
} else {
exponent -= 127 - 15;
mantissa >>= 13;
}
hfptr[i] = (unsigned short)((sign << 15) | (exponent << 10) | mantissa);
}
}
inline Point3 & Point3::operator =( const Point3 & pnt )
{
mX = pnt.mX;

View File

@ -1,34 +1,21 @@
/*
Copyright (C) 2006, 2007 Sony Computer Entertainment Inc.
Copyright (C) 2009 Sony Computer Entertainment Inc.
All rights reserved.
Redistribution and use in source and binary forms,
with or without modification, are permitted provided that the
following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the Sony Computer Entertainment Inc nor the names
of its contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
This software is provided 'as-is', without any express or implied warranty.
In no event will the authors be held liable for any damages arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it freely,
subject to the following restrictions:
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.
2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _VECTORMATH_AOS_CPP_SCALAR_H
#define _VECTORMATH_AOS_CPP_SCALAR_H
#ifndef _VECTORMATH_AOS_CPP_H
#define _VECTORMATH_AOS_CPP_H
#include <math.h>
@ -303,6 +290,28 @@ inline const Vector3 slerp( float t, const Vector3 & unitVec0, const Vector3 & u
//
inline const Vector3 select( const Vector3 & vec0, const Vector3 & vec1, bool select1 );
// Load x, y, and z elements from the first three words of a float array.
//
//
inline void loadXYZ( Vector3 & vec, const float * fptr );
// Store x, y, and z elements of a 3-D vector in the first three words of a float array.
// Memory area of previous 16 bytes and next 32 bytes from fptr might be accessed
//
inline void storeXYZ( const Vector3 & vec, float * fptr );
// Load three-half-floats as a 3-D vector
// NOTE:
// This transformation does not support either denormalized numbers or NaNs.
//
inline void loadHalfFloats( Vector3 & vec, const unsigned short * hfptr );
// Store a 3-D vector as half-floats. Memory area of previous 16 bytes and next 32 bytes from <code><i>hfptr</i></code> might be accessed.
// NOTE:
// This transformation does not support either denormalized numbers or NaNs. Memory area of previous 16 bytes and next 32 bytes from hfptr might be accessed.
//
inline void storeHalfFloats( const Vector3 & vec, unsigned short * hfptr );
#ifdef _VECTORMATH_DEBUG
// Print a 3-D vector
@ -580,6 +589,28 @@ inline const Vector4 slerp( float t, const Vector4 & unitVec0, const Vector4 & u
//
inline const Vector4 select( const Vector4 & vec0, const Vector4 & vec1, bool select1 );
// Load x, y, z, and w elements from the first four words of a float array.
//
//
inline void loadXYZW( Vector4 & vec, const float * fptr );
// Store x, y, z, and w elements of a 4-D vector in the first four words of a float array.
// Memory area of previous 16 bytes and next 32 bytes from fptr might be accessed
//
inline void storeXYZW( const Vector4 & vec, float * fptr );
// Load four-half-floats as a 4-D vector
// NOTE:
// This transformation does not support either denormalized numbers or NaNs.
//
inline void loadHalfFloats( Vector4 & vec, const unsigned short * hfptr );
// Store a 4-D vector as half-floats. Memory area of previous 16 bytes and next 32 bytes from <code><i>hfptr</i></code> might be accessed.
// NOTE:
// This transformation does not support either denormalized numbers or NaNs. Memory area of previous 16 bytes and next 32 bytes from hfptr might be accessed.
//
inline void storeHalfFloats( const Vector4 & vec, unsigned short * hfptr );
#ifdef _VECTORMATH_DEBUG
// Print a 4-D vector
@ -792,6 +823,28 @@ inline const Point3 lerp( float t, const Point3 & pnt0, const Point3 & pnt1 );
//
inline const Point3 select( const Point3 & pnt0, const Point3 & pnt1, bool select1 );
// Load x, y, and z elements from the first three words of a float array.
//
//
inline void loadXYZ( Point3 & pnt, const float * fptr );
// Store x, y, and z elements of a 3-D point in the first three words of a float array.
// Memory area of previous 16 bytes and next 32 bytes from fptr might be accessed
//
inline void storeXYZ( const Point3 & pnt, float * fptr );
// Load three-half-floats as a 3-D point
// NOTE:
// This transformation does not support either denormalized numbers or NaNs.
//
inline void loadHalfFloats( Point3 & pnt, const unsigned short * hfptr );
// Store a 3-D point as half-floats. Memory area of previous 16 bytes and next 32 bytes from <code><i>hfptr</i></code> might be accessed.
// NOTE:
// This transformation does not support either denormalized numbers or NaNs. Memory area of previous 16 bytes and next 32 bytes from hfptr might be accessed.
//
inline void storeHalfFloats( const Point3 & pnt, unsigned short * hfptr );
#ifdef _VECTORMATH_DEBUG
// Print a 3-D point
@ -1035,6 +1088,16 @@ inline const Quat squad( float t, const Quat & unitQuat0, const Quat & unitQuat1
//
inline const Quat select( const Quat & quat0, const Quat & quat1, bool select1 );
// Load x, y, z, and w elements from the first four words of a float array.
//
//
inline void loadXYZW( Quat & quat, const float * fptr );
// Store x, y, z, and w elements of a quaternion in the first four words of a float array.
// Memory area of previous 16 bytes and next 32 bytes from fptr might be accessed
//
inline void storeXYZW( const Quat & quat, float * fptr );
#ifdef _VECTORMATH_DEBUG
// Print a quaternion