Add a few utils. Start converting tabs to spaces.
This commit is contained in:
parent
78b55e1e37
commit
e848645e88
@ -1,164 +1,196 @@
|
|||||||
// This code is in the public domain -- castanyo@yahoo.es
|
// This code is in the public domain -- castanyo@yahoo.es
|
||||||
|
|
||||||
#ifndef NV_MATH_H
|
#ifndef NV_MATH_H
|
||||||
#define NV_MATH_H
|
#define NV_MATH_H
|
||||||
|
|
||||||
#include <nvcore/nvcore.h>
|
#include <nvcore/nvcore.h>
|
||||||
#include <nvcore/Debug.h>
|
#include <nvcore/Debug.h>
|
||||||
|
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
|
#include <limits.h> // INT_MAX
|
||||||
// Function linkage
|
|
||||||
#if NVMATH_SHARED
|
#if NV_OS_WIN32
|
||||||
#ifdef NVMATH_EXPORTS
|
#include <float.h>
|
||||||
#define NVMATH_API DLL_EXPORT
|
#endif
|
||||||
#define NVMATH_CLASS DLL_EXPORT_CLASS
|
|
||||||
#else
|
// Function linkage
|
||||||
#define NVMATH_API DLL_IMPORT
|
#if NVMATH_SHARED
|
||||||
#define NVMATH_CLASS DLL_IMPORT
|
#ifdef NVMATH_EXPORTS
|
||||||
#endif
|
#define NVMATH_API DLL_EXPORT
|
||||||
#else // NVMATH_SHARED
|
#define NVMATH_CLASS DLL_EXPORT_CLASS
|
||||||
#define NVMATH_API
|
#else
|
||||||
#define NVMATH_CLASS
|
#define NVMATH_API DLL_IMPORT
|
||||||
#endif // NVMATH_SHARED
|
#define NVMATH_CLASS DLL_IMPORT
|
||||||
|
#endif
|
||||||
#ifndef PI
|
#else // NVMATH_SHARED
|
||||||
#define PI float(3.1415926535897932384626433833)
|
#define NVMATH_API
|
||||||
#endif
|
#define NVMATH_CLASS
|
||||||
|
#endif // NVMATH_SHARED
|
||||||
#define NV_EPSILON (0.0001f)
|
|
||||||
#define NV_NORMAL_EPSILON (0.001f)
|
#ifndef PI
|
||||||
|
#define PI float(3.1415926535897932384626433833)
|
||||||
/*
|
#endif
|
||||||
#define SQ(r) ((r)*(r))
|
|
||||||
|
#define NV_EPSILON (0.0001f)
|
||||||
#define SIGN_BITMASK 0x80000000
|
#define NV_NORMAL_EPSILON (0.001f)
|
||||||
|
|
||||||
/// Integer representation of a floating-point value.
|
/*
|
||||||
#define IR(x) ((uint32 &)(x))
|
#define SQ(r) ((r)*(r))
|
||||||
|
|
||||||
/// Absolute integer representation of a floating-point value
|
#define SIGN_BITMASK 0x80000000
|
||||||
#define AIR(x) (IR(x) & 0x7fffffff)
|
|
||||||
|
/// Integer representation of a floating-point value.
|
||||||
/// Floating-point representation of an integer value.
|
#define IR(x) ((uint32 &)(x))
|
||||||
#define FR(x) ((float&)(x))
|
|
||||||
|
/// Absolute integer representation of a floating-point value
|
||||||
/// Integer-based comparison of a floating point value.
|
#define AIR(x) (IR(x) & 0x7fffffff)
|
||||||
/// Don't use it blindly, it can be faster or slower than the FPU comparison, depends on the context.
|
|
||||||
#define IS_NEGATIVE_FLOAT(x) (IR(x)&SIGN_BITMASK)
|
/// Floating-point representation of an integer value.
|
||||||
*/
|
#define FR(x) ((float&)(x))
|
||||||
|
|
||||||
inline double sqrt_assert(const double f)
|
/// Integer-based comparison of a floating point value.
|
||||||
{
|
/// Don't use it blindly, it can be faster or slower than the FPU comparison, depends on the context.
|
||||||
nvDebugCheck(f >= 0.0f);
|
#define IS_NEGATIVE_FLOAT(x) (IR(x)&SIGN_BITMASK)
|
||||||
return sqrt(f);
|
*/
|
||||||
}
|
|
||||||
|
inline double sqrt_assert(const double f)
|
||||||
inline float sqrtf_assert(const float f)
|
{
|
||||||
{
|
nvDebugCheck(f >= 0.0f);
|
||||||
nvDebugCheck(f >= 0.0f);
|
return sqrt(f);
|
||||||
return sqrtf(f);
|
}
|
||||||
}
|
|
||||||
|
inline float sqrtf_assert(const float f)
|
||||||
inline double acos_assert(const double f)
|
{
|
||||||
{
|
nvDebugCheck(f >= 0.0f);
|
||||||
nvDebugCheck(f >= -1.0f && f <= 1.0f);
|
return sqrtf(f);
|
||||||
return acos(f);
|
}
|
||||||
}
|
|
||||||
|
inline double acos_assert(const double f)
|
||||||
inline float acosf_assert(const float f)
|
{
|
||||||
{
|
nvDebugCheck(f >= -1.0f && f <= 1.0f);
|
||||||
nvDebugCheck(f >= -1.0f && f <= 1.0f);
|
return acos(f);
|
||||||
return acosf(f);
|
}
|
||||||
}
|
|
||||||
|
inline float acosf_assert(const float f)
|
||||||
inline double asin_assert(const double f)
|
{
|
||||||
{
|
nvDebugCheck(f >= -1.0f && f <= 1.0f);
|
||||||
nvDebugCheck(f >= -1.0f && f <= 1.0f);
|
return acosf(f);
|
||||||
return asin(f);
|
}
|
||||||
}
|
|
||||||
|
inline double asin_assert(const double f)
|
||||||
inline float asinf_assert(const float f)
|
{
|
||||||
{
|
nvDebugCheck(f >= -1.0f && f <= 1.0f);
|
||||||
nvDebugCheck(f >= -1.0f && f <= 1.0f);
|
return asin(f);
|
||||||
return asinf(f);
|
}
|
||||||
}
|
|
||||||
|
inline float asinf_assert(const float f)
|
||||||
// Replace default functions with asserting ones.
|
{
|
||||||
#define sqrt sqrt_assert
|
nvDebugCheck(f >= -1.0f && f <= 1.0f);
|
||||||
#define sqrtf sqrtf_assert
|
return asinf(f);
|
||||||
#define acos acos_assert
|
}
|
||||||
#define acosf acosf_assert
|
|
||||||
#define asin asin_assert
|
// Replace default functions with asserting ones.
|
||||||
#define asinf asinf_assert
|
#define sqrt sqrt_assert
|
||||||
|
#define sqrtf sqrtf_assert
|
||||||
#if NV_OS_WIN32
|
#define acos acos_assert
|
||||||
#include <float.h>
|
#define acosf acosf_assert
|
||||||
#endif
|
#define asin asin_assert
|
||||||
|
#define asinf asinf_assert
|
||||||
namespace nv
|
|
||||||
{
|
namespace nv
|
||||||
inline float toRadian(float degree) { return degree * (PI / 180.0f); }
|
{
|
||||||
inline float toDegree(float radian) { return radian * (180.0f / PI); }
|
inline float toRadian(float degree) { return degree * (PI / 180.0f); }
|
||||||
|
inline float toDegree(float radian) { return radian * (180.0f / PI); }
|
||||||
inline bool equal(const float f0, const float f1, const float epsilon = NV_EPSILON)
|
|
||||||
{
|
inline bool equal(const float f0, const float f1, const float epsilon = NV_EPSILON)
|
||||||
return fabs(f0-f1) <= epsilon;
|
{
|
||||||
}
|
return fabs(f0-f1) <= epsilon;
|
||||||
|
}
|
||||||
inline bool isZero(const float f, const float epsilon = NV_EPSILON)
|
|
||||||
{
|
inline bool isZero(const float f, const float epsilon = NV_EPSILON)
|
||||||
return fabs(f) <= epsilon;
|
{
|
||||||
}
|
return fabs(f) <= epsilon;
|
||||||
|
}
|
||||||
inline bool isFinite(const float f)
|
|
||||||
{
|
inline bool isFinite(const float f)
|
||||||
#if NV_OS_WIN32
|
{
|
||||||
return _finite(f) != 0;
|
#if NV_OS_WIN32
|
||||||
#elif NV_OS_DARWIN || NV_OS_FREEBSD
|
return _finite(f) != 0;
|
||||||
return isfinite(f);
|
#elif NV_OS_DARWIN || NV_OS_FREEBSD
|
||||||
#elif NV_OS_LINUX
|
return isfinite(f);
|
||||||
return finitef(f);
|
#elif NV_OS_LINUX
|
||||||
#else
|
return finitef(f);
|
||||||
# error "isFinite not supported"
|
#else
|
||||||
#endif
|
# error "isFinite not supported"
|
||||||
//return std::isfinite (f);
|
#endif
|
||||||
//return finite (f);
|
//return std::isfinite (f);
|
||||||
}
|
//return finite (f);
|
||||||
|
}
|
||||||
inline bool isNan(const float f)
|
|
||||||
{
|
inline bool isNan(const float f)
|
||||||
#if NV_OS_WIN32
|
{
|
||||||
return _isnan(f) != 0;
|
#if NV_OS_WIN32
|
||||||
#elif NV_OS_DARWIN || NV_OS_FREEBSD
|
return _isnan(f) != 0;
|
||||||
return isnan(f);
|
#elif NV_OS_DARWIN || NV_OS_FREEBSD
|
||||||
#elif NV_OS_LINUX
|
return isnan(f);
|
||||||
return isnanf(f);
|
#elif NV_OS_LINUX
|
||||||
#else
|
return isnanf(f);
|
||||||
# error "isNan not supported"
|
#else
|
||||||
#endif
|
# error "isNan not supported"
|
||||||
}
|
#endif
|
||||||
|
}
|
||||||
inline uint log2(uint i)
|
|
||||||
{
|
inline uint log2(uint i)
|
||||||
uint value = 0;
|
{
|
||||||
while( i >>= 1 ) {
|
uint value = 0;
|
||||||
value++;
|
while( i >>= 1 ) {
|
||||||
}
|
value++;
|
||||||
return value;
|
}
|
||||||
}
|
return value;
|
||||||
|
}
|
||||||
inline float lerp(float f0, float f1, float t)
|
|
||||||
{
|
inline float log2f(float x)
|
||||||
const float s = 1.0f - t;
|
{
|
||||||
return f0 * s + f1 * t;
|
nvCheck(x >= 0);
|
||||||
}
|
return logf(x) / logf(2.0f);
|
||||||
|
}
|
||||||
inline float square(float f)
|
|
||||||
{
|
inline float lerp(float f0, float f1, float t)
|
||||||
return f * f;
|
{
|
||||||
}
|
const float s = 1.0f - t;
|
||||||
|
return f0 * s + f1 * t;
|
||||||
} // nv
|
}
|
||||||
|
|
||||||
#endif // NV_MATH_H
|
inline float square(float f)
|
||||||
|
{
|
||||||
|
return f * f;
|
||||||
|
}
|
||||||
|
|
||||||
|
// @@ Float to int conversions to be optimized at some point. See:
|
||||||
|
// http://cbloomrants.blogspot.com/2009/01/01-17-09-float-to-int.html
|
||||||
|
// http://www.stereopsis.com/sree/fpu2006.html
|
||||||
|
// http://assemblyrequired.crashworks.org/2009/01/12/why-you-should-never-cast-floats-to-ints/
|
||||||
|
// http://chrishecker.com/Miscellaneous_Technical_Articles#Floating_Point
|
||||||
|
inline int iround(float f)
|
||||||
|
{
|
||||||
|
return int(f);
|
||||||
|
}
|
||||||
|
|
||||||
|
inline int ifloor(float f)
|
||||||
|
{
|
||||||
|
return int(floorf(f));
|
||||||
|
}
|
||||||
|
|
||||||
|
inline int iceil(float f)
|
||||||
|
{
|
||||||
|
return int(ceilf(f));
|
||||||
|
}
|
||||||
|
|
||||||
|
inline float frac(float f)
|
||||||
|
{
|
||||||
|
return f - floor(f);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // nv
|
||||||
|
|
||||||
|
#endif // NV_MATH_H
|
||||||
|
Loading…
Reference in New Issue
Block a user