Added vector ulp functions

This commit is contained in:
Christophe Riccio 2018-09-26 18:27:55 +02:00
parent 32573c86ec
commit 76683aa287
11 changed files with 796 additions and 426 deletions

73
glm/ext/scalar_ulp.hpp Normal file
View File

@ -0,0 +1,73 @@
/// @ref ext_scalar_ulp
/// @file glm/ext/scalar_ulp.hpp
///
/// @defgroup ext_scalar_ulp GLM_EXT_scalar_ulp
/// @ingroup ext
///
/// Allow the measurement of the accuracy of a function against a reference
/// implementation. This extension works on floating-point data and provide results
/// in ULP.
///
/// Include <glm/ext/scalar_ulp.hpp> to use the features of this extension.
///
/// @see ext_vector_ulp
/// @see ext_scalar_relational
#pragma once
// Dependencies
#include "../ext/scalar_int_sized.hpp"
#include "../detail/qualifier.hpp"
#if GLM_MESSAGES == GLM_ENABLE && !defined(GLM_EXT_INCLUDED)
# pragma message("GLM: GLM_EXT_scalar_ulp extension included")
#endif
namespace glm
{
/// Return the next ULP value(s) after the input value(s).
///
/// @tparam genType A floating-point scalar type.
///
/// @see ext_scalar_ulp
template<typename genType>
GLM_FUNC_DECL genType next_float(genType x);
/// Return the previous ULP value(s) before the input value(s).
///
/// @tparam genType A floating-point scalar type.
///
/// @see ext_scalar_ulp
template<typename genType>
GLM_FUNC_DECL genType prev_float(genType x);
/// Return the value(s) ULP distance after the input value(s).
///
/// @tparam genType A floating-point scalar type.
///
/// @see ext_scalar_ulp
template<typename genType>
GLM_FUNC_DECL genType next_float(genType x, int ULPs);
/// Return the value(s) ULP distance before the input value(s).
///
/// @tparam genType A floating-point scalar type.
///
/// @see ext_scalar_ulp
template<typename genType>
GLM_FUNC_DECL genType prev_float(genType x, int ULPs);
/// Return the distance in the number of ULP between 2 single-precision floating-point scalars.
///
/// @see ext_scalar_ulp
GLM_FUNC_DECL int float_distance(float x, float y);
/// Return the distance in the number of ULP between 2 double-precision floating-point scalars.
///
/// @see ext_scalar_ulp
GLM_FUNC_DECL int64 float_distance(double x, double y);
/// @}
}//namespace glm
#include "scalar_ulp.inl"

282
glm/ext/scalar_ulp.inl Normal file
View File

@ -0,0 +1,282 @@
/// Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
///
/// Developed at SunPro, a Sun Microsystems, Inc. business.
/// Permission to use, copy, modify, and distribute this
/// software is freely granted, provided that this notice
/// is preserved.
#include "../detail/type_float.hpp"
#include "../ext/scalar_constants.hpp"
#if(GLM_COMPILER & GLM_COMPILER_VC)
# pragma warning(push)
# pragma warning(disable : 4127)
#endif
typedef union
{
float value;
/* FIXME: Assumes 32 bit int. */
unsigned int word;
} ieee_float_shape_type;
typedef union
{
double value;
struct
{
int lsw;
int msw;
} parts;
} ieee_double_shape_type;
#define GLM_EXTRACT_WORDS(ix0,ix1,d) \
do { \
ieee_double_shape_type ew_u; \
ew_u.value = (d); \
(ix0) = ew_u.parts.msw; \
(ix1) = ew_u.parts.lsw; \
} while (0)
#define GLM_GET_FLOAT_WORD(i,d) \
do { \
ieee_float_shape_type gf_u; \
gf_u.value = (d); \
(i) = gf_u.word; \
} while (0)
#define GLM_SET_FLOAT_WORD(d,i) \
do { \
ieee_float_shape_type sf_u; \
sf_u.word = (i); \
(d) = sf_u.value; \
} while (0)
#define GLM_INSERT_WORDS(d,ix0,ix1) \
do { \
ieee_double_shape_type iw_u; \
iw_u.parts.msw = (ix0); \
iw_u.parts.lsw = (ix1); \
(d) = iw_u.value; \
} while (0)
namespace glm{
namespace detail
{
GLM_FUNC_QUALIFIER float nextafterf(float x, float y)
{
volatile float t;
int hx, hy, ix, iy;
GLM_GET_FLOAT_WORD(hx, x);
GLM_GET_FLOAT_WORD(hy, y);
ix = hx & 0x7fffffff; // |x|
iy = hy & 0x7fffffff; // |y|
if((ix > 0x7f800000) || // x is nan
(iy > 0x7f800000)) // y is nan
return x + y;
if(abs(y - x) <= epsilon<float>())
return y; // x=y, return y
if(ix == 0)
{ // x == 0
GLM_SET_FLOAT_WORD(x, (hy & 0x80000000) | 1);// return +-minsubnormal
t = x * x;
if(abs(t - x) <= epsilon<float>())
return t;
else
return x; // raise underflow flag
}
if(hx >= 0)
{ // x > 0
if(hx > hy) // x > y, x -= ulp
hx -= 1;
else // x < y, x += ulp
hx += 1;
}
else
{ // x < 0
if(hy >= 0 || hx > hy) // x < y, x -= ulp
hx -= 1;
else // x > y, x += ulp
hx += 1;
}
hy = hx & 0x7f800000;
if(hy >= 0x7f800000)
return x + x; // overflow
if(hy < 0x00800000) // underflow
{
t = x * x;
if(abs(t - x) > epsilon<float>())
{ // raise underflow flag
GLM_SET_FLOAT_WORD(y, hx);
return y;
}
}
GLM_SET_FLOAT_WORD(x, hx);
return x;
}
GLM_FUNC_QUALIFIER double nextafter(double x, double y)
{
volatile double t;
int hx, hy, ix, iy;
unsigned int lx, ly;
GLM_EXTRACT_WORDS(hx, lx, x);
GLM_EXTRACT_WORDS(hy, ly, y);
ix = hx & 0x7fffffff; // |x|
iy = hy & 0x7fffffff; // |y|
if(((ix >= 0x7ff00000) && ((ix - 0x7ff00000) | lx) != 0) || // x is nan
((iy >= 0x7ff00000) && ((iy - 0x7ff00000) | ly) != 0)) // y is nan
return x + y;
if(abs(y - x) <= epsilon<double>())
return y; // x=y, return y
if((ix | lx) == 0)
{ // x == 0
GLM_INSERT_WORDS(x, hy & 0x80000000, 1); // return +-minsubnormal
t = x * x;
if(abs(t - x) <= epsilon<double>())
return t;
else
return x; // raise underflow flag
}
if(hx >= 0) { // x > 0
if(hx > hy || ((hx == hy) && (lx > ly))) { // x > y, x -= ulp
if(lx == 0) hx -= 1;
lx -= 1;
}
else { // x < y, x += ulp
lx += 1;
if(lx == 0) hx += 1;
}
}
else { // x < 0
if(hy >= 0 || hx > hy || ((hx == hy) && (lx > ly))){// x < y, x -= ulp
if(lx == 0) hx -= 1;
lx -= 1;
}
else { // x > y, x += ulp
lx += 1;
if(lx == 0) hx += 1;
}
}
hy = hx & 0x7ff00000;
if(hy >= 0x7ff00000)
return x + x; // overflow
if(hy < 0x00100000)
{ // underflow
t = x * x;
if(abs(t - x) > epsilon<double>())
{ // raise underflow flag
GLM_INSERT_WORDS(y, hx, lx);
return y;
}
}
GLM_INSERT_WORDS(x, hx, lx);
return x;
}
}//namespace detail
}//namespace glm
#if(GLM_COMPILER & GLM_COMPILER_VC)
# pragma warning(pop)
#endif
namespace glm
{
template<>
GLM_FUNC_QUALIFIER float next_float(float x)
{
# if GLM_HAS_CXX11_STL
return std::nextafter(x, std::numeric_limits<float>::max());
# elif((GLM_COMPILER & GLM_COMPILER_VC) || ((GLM_COMPILER & GLM_COMPILER_INTEL) && (GLM_PLATFORM & GLM_PLATFORM_WINDOWS)))
return detail::nextafterf(x, FLT_MAX);
# elif(GLM_PLATFORM & GLM_PLATFORM_ANDROID)
return __builtin_nextafterf(x, FLT_MAX);
# else
return nextafterf(x, FLT_MAX);
# endif
}
template<>
GLM_FUNC_QUALIFIER double next_float(double x)
{
# if GLM_HAS_CXX11_STL
return std::nextafter(x, std::numeric_limits<double>::max());
# elif((GLM_COMPILER & GLM_COMPILER_VC) || ((GLM_COMPILER & GLM_COMPILER_INTEL) && (GLM_PLATFORM & GLM_PLATFORM_WINDOWS)))
return detail::nextafter(x, std::numeric_limits<double>::max());
# elif(GLM_PLATFORM & GLM_PLATFORM_ANDROID)
return __builtin_nextafter(x, FLT_MAX);
# else
return nextafter(x, DBL_MAX);
# endif
}
template<typename T>
GLM_FUNC_QUALIFIER T next_float(T x, int ULPs)
{
GLM_STATIC_ASSERT(std::numeric_limits<T>::is_iec559, "'next_float' only accept floating-point input");
assert(ULPs >= 0);
T temp = x;
for(int i = 0; i < ULPs; ++i)
temp = next_float(temp);
return temp;
}
GLM_FUNC_QUALIFIER float prev_float(float x)
{
# if GLM_HAS_CXX11_STL
return std::nextafter(x, std::numeric_limits<float>::min());
# elif((GLM_COMPILER & GLM_COMPILER_VC) || ((GLM_COMPILER & GLM_COMPILER_INTEL) && (GLM_PLATFORM & GLM_PLATFORM_WINDOWS)))
return detail::nextafterf(x, FLT_MIN);
# elif(GLM_PLATFORM & GLM_PLATFORM_ANDROID)
return __builtin_nextafterf(x, FLT_MIN);
# else
return nextafterf(x, FLT_MIN);
# endif
}
GLM_FUNC_QUALIFIER double prev_float(double x)
{
# if GLM_HAS_CXX11_STL
return std::nextafter(x, std::numeric_limits<double>::min());
# elif((GLM_COMPILER & GLM_COMPILER_VC) || ((GLM_COMPILER & GLM_COMPILER_INTEL) && (GLM_PLATFORM & GLM_PLATFORM_WINDOWS)))
return _nextafter(x, DBL_MIN);
# elif(GLM_PLATFORM & GLM_PLATFORM_ANDROID)
return __builtin_nextafter(x, DBL_MIN);
# else
return nextafter(x, DBL_MIN);
# endif
}
template<typename T>
GLM_FUNC_QUALIFIER T prev_float(T x, int ULPs)
{
GLM_STATIC_ASSERT(std::numeric_limits<T>::is_iec559, "'prev_float' only accept floating-point input");
assert(ULPs >= 0);
T temp = x;
for(int i = 0; i < ULPs; ++i)
temp = prev_float(temp);
return temp;
}
GLM_FUNC_QUALIFIER int float_distance(float x, float y)
{
detail::float_t<float> const a(x);
detail::float_t<float> const b(y);
return abs(a.i - b.i);
}
GLM_FUNC_QUALIFIER int64 float_distance(double x, double y)
{
detail::float_t<double> const a(x);
detail::float_t<double> const b(y);
return abs(a.i - b.i);
}
}//namespace glm

91
glm/ext/vector_ulp.hpp Normal file
View File

@ -0,0 +1,91 @@
/// @ref ext_vector_ulp
/// @file glm/ext/vector_ulp.hpp
///
/// @defgroup ext_vector_ulp GLM_EXT_vector_ulp
/// @ingroup ext
///
/// Allow the measurement of the accuracy of a function against a reference
/// implementation. This extension works on floating-point data and provide results
/// in ULP.
///
/// Include <glm/ext/vector_ulp.hpp> to use the features of this extension.
///
/// @see ext_scalar_ulp
/// @see ext_scalar_relational
/// @see ext_vector_relational
#pragma once
// Dependencies
#include "../ext/scalar_ulp.hpp"
#if GLM_MESSAGES == GLM_ENABLE && !defined(GLM_EXT_INCLUDED)
# pragma message("GLM: GLM_EXT_vector_ulp extension included")
#endif
namespace glm
{
/// Return the next ULP value(s) after the input value(s).
///
/// @tparam genType A floating-point scalar type.
///
/// @see ext_scalar_ulp
template<length_t L, typename T, qualifier Q>
GLM_FUNC_DECL vec<L, T, Q> next_float(vec<L, T, Q> const& x);
/// Return the value(s) ULP distance after the input value(s).
///
/// @tparam genType A floating-point scalar type.
///
/// @see ext_scalar_ulp
template<length_t L, typename T, qualifier Q>
GLM_FUNC_DECL vec<L, T, Q> next_float(vec<L, T, Q> const& x, int ULPs);
/// Return the value(s) ULP distance after the input value(s).
///
/// @tparam genType A floating-point scalar type.
///
/// @see ext_scalar_ulp
template<length_t L, typename T, qualifier Q>
GLM_FUNC_DECL vec<L, T, Q> next_float(vec<L, T, Q> const& x, vec<L, int, Q> const& ULPs);
/// Return the previous ULP value(s) before the input value(s).
///
/// @tparam genType A floating-point scalar type.
///
/// @see ext_scalar_ulp
template<length_t L, typename T, qualifier Q>
GLM_FUNC_DECL vec<L, T, Q> prev_float(vec<L, T, Q> const& x);
/// Return the value(s) ULP distance before the input value(s).
///
/// @tparam genType A floating-point scalar type.
///
/// @see ext_scalar_ulp
template<length_t L, typename T, qualifier Q>
GLM_FUNC_DECL vec<L, T, Q> prev_float(vec<L, T, Q> const& x, int ULPs);
/// Return the value(s) ULP distance before the input value(s).
///
/// @tparam genType A floating-point scalar type.
///
/// @see ext_scalar_ulp
template<length_t L, typename T, qualifier Q>
GLM_FUNC_DECL vec<L, T, Q> prev_float(vec<L, T, Q> const& x, vec<L, int, Q> const& ULPs);
/// Return the distance in the number of ULP between 2 single-precision floating-point scalars.
///
/// @see ext_scalar_ulp
template<length_t L, typename T, qualifier Q>
GLM_FUNC_DECL vec<L, int, Q> float_distance(vec<L, float, Q> const& x, vec<L, float, Q> const& y);
/// Return the distance in the number of ULP between 2 double-precision floating-point scalars.
///
/// @see ext_scalar_ulp
template<length_t L, typename T, qualifier Q>
GLM_FUNC_DECL vec<L, int64, Q> float_distance(vec<L, double, Q> const& x, vec<L, double, Q> const& y);
/// @}
}//namespace glm
#include "vector_ulp.inl"

74
glm/ext/vector_ulp.inl Normal file
View File

@ -0,0 +1,74 @@
namespace glm
{
template<length_t L, typename T, qualifier Q>
GLM_FUNC_QUALIFIER vec<L, T, Q> next_float(vec<L, T, Q> const& x)
{
vec<L, T, Q> Result;
for(length_t i = 0, n = Result.length(); i < n; ++i)
Result[i] = next_float(x[i]);
return Result;
}
template<length_t L, typename T, qualifier Q>
GLM_FUNC_QUALIFIER vec<L, T, Q> next_float(vec<L, T, Q> const& x, int ULPs)
{
vec<L, T, Q> Result;
for(length_t i = 0, n = Result.length(); i < n; ++i)
Result[i] = next_float(x[i], ULPs);
return Result;
}
template<length_t L, typename T, qualifier Q>
GLM_FUNC_QUALIFIER vec<L, T, Q> next_float(vec<L, T, Q> const& x, vec<L, int, Q> const& ULPs)
{
vec<L, T, Q> Result;
for(length_t i = 0, n = Result.length(); i < n; ++i)
Result[i] = next_float(x[i], ULPs[i]);
return Result;
}
template<length_t L, typename T, qualifier Q>
GLM_FUNC_QUALIFIER vec<L, T, Q> prev_float(vec<L, T, Q> const& x)
{
vec<L, T, Q> Result;
for(length_t i = 0, n = Result.length(); i < n; ++i)
Result[i] = prev_float(x[i]);
return Result;
}
template<length_t L, typename T, qualifier Q>
GLM_FUNC_QUALIFIER vec<L, T, Q> prev_float(vec<L, T, Q> const& x, int ULPs)
{
vec<L, T, Q> Result;
for(length_t i = 0, n = Result.length(); i < n; ++i)
Result[i] = prev_float(x[i], ULPs);
return Result;
}
template<length_t L, typename T, qualifier Q>
GLM_FUNC_QUALIFIER vec<L, T, Q> prev_float(vec<L, T, Q> const& x, vec<L, int, Q> const& ULPs)
{
vec<L, T, Q> Result;
for(length_t i = 0, n = Result.length(); i < n; ++i)
Result[i] = prev_float(x[i], ULPs[i]);
return Result;
}
template<length_t L, qualifier Q>
GLM_FUNC_QUALIFIER vec<L, int, Q> float_distance(vec<L, float, Q> const& x, vec<L, float, Q> const& y)
{
vec<L, int, Q> Result;
for(length_t i = 0, n = Result.length(); i < n; ++i)
Result[i] = float_distance(x[i], y[i]);
return Result;
}
template<length_t L, qualifier Q>
GLM_FUNC_QUALIFIER vec<L, int64, Q> float_distance(vec<L, double, Q> const& x, vec<L, double, Q> const& y)
{
vec<L, int64, Q> Result;
for(length_t i = 0, n = Result.length(); i < n; ++i)
Result[i] = float_distance(x[i], y[i]);
return Result;
}
}//namespace glm

View File

@ -15,60 +15,10 @@
#pragma once #pragma once
// Dependencies // Dependencies
#include "../gtc/constants.hpp" #include "../ext/scalar_ulp.hpp"
#include "../ext/vector_relational.hpp" #include "../ext/vector_ulp.hpp"
#include "../ext/scalar_int_sized.hpp"
#if GLM_MESSAGES == GLM_ENABLE && !defined(GLM_EXT_INCLUDED) #if GLM_MESSAGES == GLM_ENABLE && !defined(GLM_EXT_INCLUDED)
# pragma message("GLM: GLM_GTC_ulp extension included") # pragma message("GLM: GLM_GTC_ulp extension included")
#endif #endif
namespace glm
{
/// @addtogroup gtc_ulp
/// @{
/// Return the next ULP value(s) after the input value(s).
/// @see gtc_ulp
template<typename genType>
GLM_FUNC_DECL genType next_float(genType const& x);
/// Return the previous ULP value(s) before the input value(s).
/// @see gtc_ulp
template<typename genType>
GLM_FUNC_DECL genType prev_float(genType const& x);
/// Return the value(s) ULP distance after the input value(s).
/// @see gtc_ulp
template<typename genType>
GLM_FUNC_DECL genType next_float(genType const& x, int DistanceULPs);
/// Return the value(s) ULP distance before the input value(s).
/// @see gtc_ulp
template<typename genType>
GLM_FUNC_DECL genType prev_float(genType const& x, int DistanceULPs);
/// Return the distance in the number of ULP between 2 single-precision floating-point scalars.
/// @see gtc_ulp
template<typename T>
GLM_FUNC_DECL int float_distance(float x, float y);
/// Return the distance in the number of ULP between 2 double-precision floating-point scalars.
/// @see gtc_ulp
template<typename T>
GLM_FUNC_DECL int64 float_distance(double x, double y);
/// Return the distance in the number of ULP between single-precision floating-point 2 vectors.
/// @see gtc_ulp
template<length_t L, qualifier Q>
GLM_FUNC_DECL vec<L, int, Q> float_distance(vec<L, float, Q> const& x, vec<L, float, Q> const& y);
/// Return the distance in the number of ULP between double-precision floating-point 2 vectors.
/// @see gtc_ulp
template<length_t L, qualifier Q>
GLM_FUNC_DECL vec<L, int64, Q> float_distance(vec<L, double, Q> const& x, vec<L, double, Q> const& y);
/// @}
}// namespace glm
#include "ulp.inl"

View File

@ -1,334 +1,3 @@
/// @ref gtc_ulp /// @ref gtc_ulp
/// ///
/// Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
///
/// Developed at SunPro, a Sun Microsystems, Inc. business.
/// Permission to use, copy, modify, and distribute this
/// software is freely granted, provided that this notice
/// is preserved.
#include "epsilon.hpp"
#include <cmath>
#include <cfloat>
#include <limits>
#include "../detail/type_float.hpp"
#if(GLM_COMPILER & GLM_COMPILER_VC)
# pragma warning(push)
# pragma warning(disable : 4127)
#endif
typedef union
{
float value;
/* FIXME: Assumes 32 bit int. */
unsigned int word;
} ieee_float_shape_type;
typedef union
{
double value;
struct
{
int lsw;
int msw;
} parts;
} ieee_double_shape_type;
#define GLM_EXTRACT_WORDS(ix0,ix1,d) \
do { \
ieee_double_shape_type ew_u; \
ew_u.value = (d); \
(ix0) = ew_u.parts.msw; \
(ix1) = ew_u.parts.lsw; \
} while (0)
#define GLM_GET_FLOAT_WORD(i,d) \
do { \
ieee_float_shape_type gf_u; \
gf_u.value = (d); \
(i) = gf_u.word; \
} while (0)
#define GLM_SET_FLOAT_WORD(d,i) \
do { \
ieee_float_shape_type sf_u; \
sf_u.word = (i); \
(d) = sf_u.value; \
} while (0)
#define GLM_INSERT_WORDS(d,ix0,ix1) \
do { \
ieee_double_shape_type iw_u; \
iw_u.parts.msw = (ix0); \
iw_u.parts.lsw = (ix1); \
(d) = iw_u.value; \
} while (0)
namespace glm{
namespace detail
{
GLM_FUNC_QUALIFIER float nextafterf(float x, float y)
{
volatile float t;
int hx, hy, ix, iy;
GLM_GET_FLOAT_WORD(hx, x);
GLM_GET_FLOAT_WORD(hy, y);
ix = hx&0x7fffffff; // |x|
iy = hy&0x7fffffff; // |y|
if((ix>0x7f800000) || // x is nan
(iy>0x7f800000)) // y is nan
return x+y;
if(abs(y - x) <= epsilon<float>())
return y; // x=y, return y
if(ix==0)
{ // x == 0
GLM_SET_FLOAT_WORD(x,(hy&0x80000000)|1);// return +-minsubnormal
t = x*x;
if(abs(t - x) <= epsilon<float>())
return t;
else
return x; // raise underflow flag
}
if(hx>=0)
{ // x > 0
if(hx>hy) // x > y, x -= ulp
hx -= 1;
else // x < y, x += ulp
hx += 1;
}
else
{ // x < 0
if(hy>=0||hx>hy) // x < y, x -= ulp
hx -= 1;
else // x > y, x += ulp
hx += 1;
}
hy = hx&0x7f800000;
if(hy>=0x7f800000)
return x+x; // overflow
if(hy<0x00800000) // underflow
{
t = x*x;
if(abs(t - x) > epsilon<float>())
{ // raise underflow flag
GLM_SET_FLOAT_WORD(y,hx);
return y;
}
}
GLM_SET_FLOAT_WORD(x,hx);
return x;
}
GLM_FUNC_QUALIFIER double nextafter(double x, double y)
{
volatile double t;
int hx, hy, ix, iy;
unsigned int lx, ly;
GLM_EXTRACT_WORDS(hx, lx, x);
GLM_EXTRACT_WORDS(hy, ly, y);
ix = hx & 0x7fffffff; // |x|
iy = hy & 0x7fffffff; // |y|
if(((ix>=0x7ff00000)&&((ix-0x7ff00000)|lx)!=0) || // x is nan
((iy>=0x7ff00000)&&((iy-0x7ff00000)|ly)!=0)) // y is nan
return x+y;
if(abs(y - x) <= epsilon<double>())
return y; // x=y, return y
if((ix|lx)==0)
{ // x == 0
GLM_INSERT_WORDS(x, hy & 0x80000000, 1); // return +-minsubnormal
t = x*x;
if(abs(t - x) <= epsilon<double>())
return t;
else
return x; // raise underflow flag
}
if(hx>=0) { // x > 0
if(hx>hy||((hx==hy)&&(lx>ly))) { // x > y, x -= ulp
if(lx==0) hx -= 1;
lx -= 1;
} else { // x < y, x += ulp
lx += 1;
if(lx==0) hx += 1;
}
} else { // x < 0
if(hy>=0||hx>hy||((hx==hy)&&(lx>ly))){// x < y, x -= ulp
if(lx==0) hx -= 1;
lx -= 1;
} else { // x > y, x += ulp
lx += 1;
if(lx==0) hx += 1;
}
}
hy = hx&0x7ff00000;
if(hy>=0x7ff00000)
return x+x; // overflow
if(hy<0x00100000)
{ // underflow
t = x*x;
if(abs(t - x) > epsilon<double>())
{ // raise underflow flag
GLM_INSERT_WORDS(y,hx,lx);
return y;
}
}
GLM_INSERT_WORDS(x,hx,lx);
return x;
}
}//namespace detail
}//namespace glm
#if(GLM_COMPILER & GLM_COMPILER_VC)
# pragma warning(pop)
#endif
namespace glm
{
template<>
GLM_FUNC_QUALIFIER float next_float(float const& x)
{
# if GLM_HAS_CXX11_STL
return std::nextafter(x, std::numeric_limits<float>::max());
# elif((GLM_COMPILER & GLM_COMPILER_VC) || ((GLM_COMPILER & GLM_COMPILER_INTEL) && (GLM_PLATFORM & GLM_PLATFORM_WINDOWS)))
return detail::nextafterf(x, FLT_MAX);
# elif(GLM_PLATFORM & GLM_PLATFORM_ANDROID)
return __builtin_nextafterf(x, FLT_MAX);
# else
return nextafterf(x, FLT_MAX);
# endif
}
template<>
GLM_FUNC_QUALIFIER double next_float(double const& x)
{
# if GLM_HAS_CXX11_STL
return std::nextafter(x, std::numeric_limits<double>::max());
# elif((GLM_COMPILER & GLM_COMPILER_VC) || ((GLM_COMPILER & GLM_COMPILER_INTEL) && (GLM_PLATFORM & GLM_PLATFORM_WINDOWS)))
return detail::nextafter(x, std::numeric_limits<double>::max());
# elif(GLM_PLATFORM & GLM_PLATFORM_ANDROID)
return __builtin_nextafter(x, FLT_MAX);
# else
return nextafter(x, DBL_MAX);
# endif
}
template<length_t L, typename T, qualifier Q>
GLM_FUNC_QUALIFIER vec<L, T, Q> next_float(vec<L, T, Q> const& x)
{
vec<L, T, Q> Result;
for(length_t i = 0, n = Result.length(); i < n; ++i)
Result[i] = next_float(x[i]);
return Result;
}
GLM_FUNC_QUALIFIER float prev_float(float const& x)
{
# if GLM_HAS_CXX11_STL
return std::nextafter(x, std::numeric_limits<float>::min());
# elif((GLM_COMPILER & GLM_COMPILER_VC) || ((GLM_COMPILER & GLM_COMPILER_INTEL) && (GLM_PLATFORM & GLM_PLATFORM_WINDOWS)))
return detail::nextafterf(x, FLT_MIN);
# elif(GLM_PLATFORM & GLM_PLATFORM_ANDROID)
return __builtin_nextafterf(x, FLT_MIN);
# else
return nextafterf(x, FLT_MIN);
# endif
}
GLM_FUNC_QUALIFIER double prev_float(double const& x)
{
# if GLM_HAS_CXX11_STL
return std::nextafter(x, std::numeric_limits<double>::min());
# elif((GLM_COMPILER & GLM_COMPILER_VC) || ((GLM_COMPILER & GLM_COMPILER_INTEL) && (GLM_PLATFORM & GLM_PLATFORM_WINDOWS)))
return _nextafter(x, DBL_MIN);
# elif(GLM_PLATFORM & GLM_PLATFORM_ANDROID)
return __builtin_nextafter(x, DBL_MIN);
# else
return nextafter(x, DBL_MIN);
# endif
}
template<length_t L, typename T, qualifier Q>
GLM_FUNC_QUALIFIER vec<L, T, Q> prev_float(vec<L, T, Q> const& x)
{
vec<L, T, Q> Result;
for(length_t i = 0, n = Result.length(); i < n; ++i)
Result[i] = prev_float(x[i]);
return Result;
}
template<typename T>
GLM_FUNC_QUALIFIER T next_float(T const& x, int ulps)
{
T temp = x;
for(int i = 0; i < ulps; ++i)
temp = next_float(temp);
return temp;
}
template<length_t L, typename T, qualifier Q>
GLM_FUNC_QUALIFIER vec<L, T, Q> next_float(vec<L, T, Q> const& x, vec<L, int, Q> const& ulps)
{
vec<L, T, Q> Result;
for(length_t i = 0, n = Result.length(); i < n; ++i)
Result[i] = next_float(x[i], ulps[i]);
return Result;
}
template<typename T>
GLM_FUNC_QUALIFIER T prev_float(T const& x, int ulps)
{
assert(ulps >= 0);
T temp = x;
for(int i = 0; i < ulps; ++i)
temp = prev_float(temp);
return temp;
}
template<length_t L, typename T, qualifier Q>
GLM_FUNC_QUALIFIER vec<L, T, Q> prev_float(vec<L, T, Q> const& x, vec<L, int, Q> const& ulps)
{
vec<L, T, Q> Result;
for(length_t i = 0, n = Result.length(); i < n; ++i)
Result[i] = prev_float(x[i], ulps[i]);
return Result;
}
GLM_FUNC_QUALIFIER int float_distance(float x, float y)
{
detail::float_t<float> const a(x);
detail::float_t<float> const b(y);
return abs(a.i - b.i);
}
GLM_FUNC_QUALIFIER int64 float_distance(double x, double y)
{
detail::float_t<double> const a(x);
detail::float_t<double> const b(y);
return abs(a.i - b.i);
}
template<length_t L, qualifier Q>
GLM_FUNC_QUALIFIER vec<L, int, Q> float_distance(vec<L, float, Q> const& x, vec<L, float, Q> const& y)
{
vec<L, int, Q> Result;
for(length_t i = 0, n = Result.length(); i < n; ++i)
Result[i] = float_distance(x[i], y[i]);
return Result;
}
template<length_t L, qualifier Q>
GLM_FUNC_QUALIFIER vec<L, int64, Q> float_distance(vec<L, double, Q> const& x, vec<L, double, Q> const& y)
{
vec<L, int64, Q> Result;
for(length_t i = 0, n = Result.length(); i < n; ++i)
Result[i] = float_distance(x[i], y[i]);
return Result;
}
}//namespace glm

View File

@ -792,6 +792,22 @@ bool equalULP1(float const a, float const b)
Include `<glm/ext/scalar_constants.hpp>` to use these features. Include `<glm/ext/scalar_constants.hpp>` to use these features.
#### 3.2.4. GLM_EXT_scalar_ulp
This extension exposes function that measure of accuracy in numeric calculations.
```cpp
#include <glm/ext/scalar_ulp.hpp>
bool test_ulp(float x)
{
float const a = glm::next_float(x); // return a float a ULP away from the float argument.
return float_distance(a, x) == 1; // check both float are a single ULP away.
}
```
Include `<glm/ext/scalar_ulp.hpp>` to use these features.
### <a name="section3_3"></a> 3.3. Vector types ### <a name="section3_3"></a> 3.3. Vector types
#### 3.3.1. GLM_EXT_vector_float1 #### 3.3.1. GLM_EXT_vector_float1
@ -999,6 +1015,24 @@ bool epsilonEqual(glm::vec2 const& A, glm::vec2 const& B)
Include `<glm/ext/vector_relational.hpp>` to use these features. Include `<glm/ext/vector_relational.hpp>` to use these features.
#### 3.5.3. GLM_EXT_vector_ulp
This extension exposes function that measure of accuracy in numeric calculations.
```cpp
#include <glm/ext/vector_ulp.hpp>
#include <glm/ext/vector_float4.hpp>
#include <glm/ext/vector_int4.hpp>
bool test_ulp(glm::vec4 const& x)
{
glm::vec4 const a = glm::next_float(x); // return a float a ULP away from the float argument.
return glm::all(float_distance(a, x) == glm::ivec4(1)); // check both float are a single ULP away.
}
```
Include `<glm/ext/vector_ulp.hpp>` to use these features.
### <a name="section3_6"></a> 3.6. Matrix types ### <a name="section3_6"></a> 3.6. Matrix types
#### 3.6.1. GLM_EXT_matrix_float2x2 #### 3.6.1. GLM_EXT_matrix_float2x2

View File

@ -1,31 +1,31 @@
#include <glm/common.hpp>
#include <glm/exponential.hpp>
#include <glm/gtc/constants.hpp> #include <glm/gtc/constants.hpp>
#include <glm/gtc/ulp.hpp> #include <glm/ext/scalar_relational.hpp>
#include <glm/ext/vector_relational.hpp> #include <glm/ext/vector_relational.hpp>
#include <glm/ext/vector_float1.hpp> #include <glm/ext/vector_float1.hpp>
#include <glm/ext/vector_float2.hpp> #include <glm/ext/vector_float2.hpp>
#include <glm/ext/vector_float3.hpp> #include <glm/ext/vector_float3.hpp>
#include <glm/ext/vector_float4.hpp> #include <glm/ext/vector_float4.hpp>
#include <glm/common.hpp>
#include <glm/exponential.hpp>
static int test_pow() static int test_pow()
{ {
int Error(0); int Error(0);
float A = glm::pow(2.f, 2.f); float A = glm::pow(2.f, 2.f);
Error += glm::epsilonEqual(A, 4.f, 0.01f) ? 0 : 1; Error += glm::equal(A, 4.f, 0.01f) ? 0 : 1;
glm::vec1 B = glm::pow(glm::vec1(2.f), glm::vec1(2.f)); glm::vec1 B = glm::pow(glm::vec1(2.f), glm::vec1(2.f));
Error += glm::all(glm::epsilonEqual(B, glm::vec1(4.f), 0.01f)) ? 0 : 1; Error += glm::all(glm::equal(B, glm::vec1(4.f), 0.01f)) ? 0 : 1;
glm::vec2 C = glm::pow(glm::vec2(2.f), glm::vec2(2.f)); glm::vec2 C = glm::pow(glm::vec2(2.f), glm::vec2(2.f));
Error += glm::all(glm::epsilonEqual(C, glm::vec2(4.f), 0.01f)) ? 0 : 1; Error += glm::all(glm::equal(C, glm::vec2(4.f), 0.01f)) ? 0 : 1;
glm::vec3 D = glm::pow(glm::vec3(2.f), glm::vec3(2.f)); glm::vec3 D = glm::pow(glm::vec3(2.f), glm::vec3(2.f));
Error += glm::all(glm::epsilonEqual(D, glm::vec3(4.f), 0.01f)) ? 0 : 1; Error += glm::all(glm::equal(D, glm::vec3(4.f), 0.01f)) ? 0 : 1;
glm::vec4 E = glm::pow(glm::vec4(2.f), glm::vec4(2.f)); glm::vec4 E = glm::pow(glm::vec4(2.f), glm::vec4(2.f));
Error += glm::all(glm::epsilonEqual(E, glm::vec4(4.f), 0.01f)) ? 0 : 1; Error += glm::all(glm::equal(E, glm::vec4(4.f), 0.01f)) ? 0 : 1;
return Error; return Error;
} }
@ -35,42 +35,41 @@ static int test_sqrt()
int Error = 0; int Error = 0;
float A = glm::sqrt(4.f); float A = glm::sqrt(4.f);
Error += glm::epsilonEqual(A, 2.f, 0.01f) ? 0 : 1; Error += glm::equal(A, 2.f, 0.01f) ? 0 : 1;
glm::vec1 B = glm::sqrt(glm::vec1(4.f)); glm::vec1 B = glm::sqrt(glm::vec1(4.f));
Error += glm::all(glm::epsilonEqual(B, glm::vec1(2.f), 0.01f)) ? 0 : 1; Error += glm::all(glm::equal(B, glm::vec1(2.f), 0.01f)) ? 0 : 1;
glm::vec2 C = glm::sqrt(glm::vec2(4.f)); glm::vec2 C = glm::sqrt(glm::vec2(4.f));
Error += glm::all(glm::epsilonEqual(C, glm::vec2(2.f), 0.01f)) ? 0 : 1; Error += glm::all(glm::equal(C, glm::vec2(2.f), 0.01f)) ? 0 : 1;
glm::vec3 D = glm::sqrt(glm::vec3(4.f)); glm::vec3 D = glm::sqrt(glm::vec3(4.f));
Error += glm::all(glm::epsilonEqual(D, glm::vec3(2.f), 0.01f)) ? 0 : 1; Error += glm::all(glm::equal(D, glm::vec3(2.f), 0.01f)) ? 0 : 1;
glm::vec4 E = glm::sqrt(glm::vec4(4.f)); glm::vec4 E = glm::sqrt(glm::vec4(4.f));
Error += glm::all(glm::epsilonEqual(E, glm::vec4(2.f), 0.01f)) ? 0 : 1; Error += glm::all(glm::equal(E, glm::vec4(2.f), 0.01f)) ? 0 : 1;
return Error; return Error;
} }
static int test_exp() static int test_exp()
{ {
int Error = 0; int Error = 0;
float A = glm::exp(1.f); float A = glm::exp(1.f);
Error += glm::epsilonEqual(A, glm::e<float>(), 0.01f) ? 0 : 1; Error += glm::equal(A, glm::e<float>(), 0.01f) ? 0 : 1;
glm::vec1 B = glm::exp(glm::vec1(1.f)); glm::vec1 B = glm::exp(glm::vec1(1.f));
Error += glm::all(glm::epsilonEqual(B, glm::vec1(glm::e<float>()), 0.01f)) ? 0 : 1; Error += glm::all(glm::equal(B, glm::vec1(glm::e<float>()), 0.01f)) ? 0 : 1;
glm::vec2 C = glm::exp(glm::vec2(1.f)); glm::vec2 C = glm::exp(glm::vec2(1.f));
Error += glm::all(glm::epsilonEqual(C, glm::vec2(glm::e<float>()), 0.01f)) ? 0 : 1; Error += glm::all(glm::equal(C, glm::vec2(glm::e<float>()), 0.01f)) ? 0 : 1;
glm::vec3 D = glm::exp(glm::vec3(1.f)); glm::vec3 D = glm::exp(glm::vec3(1.f));
Error += glm::all(glm::epsilonEqual(D, glm::vec3(glm::e<float>()), 0.01f)) ? 0 : 1; Error += glm::all(glm::equal(D, glm::vec3(glm::e<float>()), 0.01f)) ? 0 : 1;
glm::vec4 E = glm::exp(glm::vec4(1.f)); glm::vec4 E = glm::exp(glm::vec4(1.f));
Error += glm::all(glm::epsilonEqual(E, glm::vec4(glm::e<float>()), 0.01f)) ? 0 : 1; Error += glm::all(glm::equal(E, glm::vec4(glm::e<float>()), 0.01f)) ? 0 : 1;
return Error; return Error;
} }
@ -80,19 +79,19 @@ static int test_log()
int Error = 0; int Error = 0;
float const A = glm::log(glm::e<float>()); float const A = glm::log(glm::e<float>());
Error += glm::epsilonEqual(A, 1.f, 0.01f) ? 0 : 1; Error += glm::equal(A, 1.f, 0.01f) ? 0 : 1;
glm::vec1 const B = glm::log(glm::vec1(glm::e<float>())); glm::vec1 const B = glm::log(glm::vec1(glm::e<float>()));
Error += glm::all(glm::epsilonEqual(B, glm::vec1(1.f), 0.01f)) ? 0 : 1; Error += glm::all(glm::equal(B, glm::vec1(1.f), 0.01f)) ? 0 : 1;
glm::vec2 const C = glm::log(glm::vec2(glm::e<float>())); glm::vec2 const C = glm::log(glm::vec2(glm::e<float>()));
Error += glm::all(glm::epsilonEqual(C, glm::vec2(1.f), 0.01f)) ? 0 : 1; Error += glm::all(glm::equal(C, glm::vec2(1.f), 0.01f)) ? 0 : 1;
glm::vec3 const D = glm::log(glm::vec3(glm::e<float>())); glm::vec3 const D = glm::log(glm::vec3(glm::e<float>()));
Error += glm::all(glm::epsilonEqual(D, glm::vec3(1.f), 0.01f)) ? 0 : 1; Error += glm::all(glm::equal(D, glm::vec3(1.f), 0.01f)) ? 0 : 1;
glm::vec4 const E = glm::log(glm::vec4(glm::e<float>())); glm::vec4 const E = glm::log(glm::vec4(glm::e<float>()));
Error += glm::all(glm::epsilonEqual(E, glm::vec4(1.f), 0.01f)) ? 0 : 1; Error += glm::all(glm::equal(E, glm::vec4(1.f), 0.01f)) ? 0 : 1;
return Error; return Error;
} }
@ -102,24 +101,24 @@ static int test_exp2()
int Error = 0; int Error = 0;
float A = glm::exp2(4.f); float A = glm::exp2(4.f);
Error += glm::epsilonEqual(A, 16.f, 0.01f) ? 0 : 1; Error += glm::equal(A, 16.f, 0.01f) ? 0 : 1;
glm::vec1 B = glm::exp2(glm::vec1(4.f)); glm::vec1 B = glm::exp2(glm::vec1(4.f));
Error += glm::all(glm::epsilonEqual(B, glm::vec1(16.f), 0.01f)) ? 0 : 1; Error += glm::all(glm::equal(B, glm::vec1(16.f), 0.01f)) ? 0 : 1;
glm::vec2 C = glm::exp2(glm::vec2(4.f, 3.f)); glm::vec2 C = glm::exp2(glm::vec2(4.f, 3.f));
Error += glm::all(glm::epsilonEqual(C, glm::vec2(16.f, 8.f), 0.01f)) ? 0 : 1; Error += glm::all(glm::equal(C, glm::vec2(16.f, 8.f), 0.01f)) ? 0 : 1;
glm::vec3 D = glm::exp2(glm::vec3(4.f, 3.f, 2.f)); glm::vec3 D = glm::exp2(glm::vec3(4.f, 3.f, 2.f));
Error += glm::all(glm::epsilonEqual(D, glm::vec3(16.f, 8.f, 4.f), 0.01f)) ? 0 : 1; Error += glm::all(glm::equal(D, glm::vec3(16.f, 8.f, 4.f), 0.01f)) ? 0 : 1;
glm::vec4 E = glm::exp2(glm::vec4(4.f, 3.f, 2.f, 1.f)); glm::vec4 E = glm::exp2(glm::vec4(4.f, 3.f, 2.f, 1.f));
Error += glm::all(glm::epsilonEqual(E, glm::vec4(16.f, 8.f, 4.f, 2.f), 0.01f)) ? 0 : 1; Error += glm::all(glm::equal(E, glm::vec4(16.f, 8.f, 4.f, 2.f), 0.01f)) ? 0 : 1;
# if GLM_HAS_CXX11_STL # if GLM_HAS_CXX11_STL
//large exponent //large exponent
float F = glm::exp2(23.f); float F = glm::exp2(23.f);
Error += glm::epsilonEqual(F, 8388608.f, 0.01f) ? 0 : 1; Error += glm::equal(F, 8388608.f, 0.01f) ? 0 : 1;
# endif # endif
return Error; return Error;
@ -130,19 +129,19 @@ static int test_log2()
int Error = 0; int Error = 0;
float A = glm::log2(16.f); float A = glm::log2(16.f);
Error += glm::epsilonEqual(A, 4.f, 0.01f) ? 0 : 1; Error += glm::equal(A, 4.f, 0.01f) ? 0 : 1;
glm::vec1 B = glm::log2(glm::vec1(16.f)); glm::vec1 B = glm::log2(glm::vec1(16.f));
Error += glm::all(glm::epsilonEqual(B, glm::vec1(4.f), 0.01f)) ? 0 : 1; Error += glm::all(glm::equal(B, glm::vec1(4.f), 0.01f)) ? 0 : 1;
glm::vec2 C = glm::log2(glm::vec2(16.f, 8.f)); glm::vec2 C = glm::log2(glm::vec2(16.f, 8.f));
Error += glm::all(glm::epsilonEqual(C, glm::vec2(4.f, 3.f), 0.01f)) ? 0 : 1; Error += glm::all(glm::equal(C, glm::vec2(4.f, 3.f), 0.01f)) ? 0 : 1;
glm::vec3 D = glm::log2(glm::vec3(16.f, 8.f, 4.f)); glm::vec3 D = glm::log2(glm::vec3(16.f, 8.f, 4.f));
Error += glm::all(glm::epsilonEqual(D, glm::vec3(4.f, 3.f, 2.f), 0.01f)) ? 0 : 1; Error += glm::all(glm::equal(D, glm::vec3(4.f, 3.f, 2.f), 0.01f)) ? 0 : 1;
glm::vec4 E = glm::log2(glm::vec4(16.f, 8.f, 4.f, 2.f)); glm::vec4 E = glm::log2(glm::vec4(16.f, 8.f, 4.f, 2.f));
Error += glm::all(glm::epsilonEqual(E, glm::vec4(4.f, 3.f, 2.f, 1.f), 0.01f)) ? 0 : 1; Error += glm::all(glm::equal(E, glm::vec4(4.f, 3.f, 2.f, 1.f), 0.01f)) ? 0 : 1;
return Error; return Error;
} }
@ -152,19 +151,19 @@ static int test_inversesqrt()
int Error = 0; int Error = 0;
float A = glm::inversesqrt(16.f) * glm::sqrt(16.f); float A = glm::inversesqrt(16.f) * glm::sqrt(16.f);
Error += glm::epsilonEqual(A, 1.f, 0.01f) ? 0 : 1; Error += glm::equal(A, 1.f, 0.01f) ? 0 : 1;
glm::vec1 B = glm::inversesqrt(glm::vec1(16.f)) * glm::sqrt(16.f);; glm::vec1 B = glm::inversesqrt(glm::vec1(16.f)) * glm::sqrt(16.f);;
Error += glm::all(glm::epsilonEqual(B, glm::vec1(1.f), 0.01f)) ? 0 : 1; Error += glm::all(glm::equal(B, glm::vec1(1.f), 0.01f)) ? 0 : 1;
glm::vec2 C = glm::inversesqrt(glm::vec2(16.f)) * glm::sqrt(16.f);; glm::vec2 C = glm::inversesqrt(glm::vec2(16.f)) * glm::sqrt(16.f);;
Error += glm::all(glm::epsilonEqual(C, glm::vec2(1.f), 0.01f)) ? 0 : 1; Error += glm::all(glm::equal(C, glm::vec2(1.f), 0.01f)) ? 0 : 1;
glm::vec3 D = glm::inversesqrt(glm::vec3(16.f)) * glm::sqrt(16.f);; glm::vec3 D = glm::inversesqrt(glm::vec3(16.f)) * glm::sqrt(16.f);;
Error += glm::all(glm::epsilonEqual(D, glm::vec3(1.f), 0.01f)) ? 0 : 1; Error += glm::all(glm::equal(D, glm::vec3(1.f), 0.01f)) ? 0 : 1;
glm::vec4 E = glm::inversesqrt(glm::vec4(16.f)) * glm::sqrt(16.f); glm::vec4 E = glm::inversesqrt(glm::vec4(16.f)) * glm::sqrt(16.f);
Error += glm::all(glm::epsilonEqual(E, glm::vec4(1.f), 0.01f)) ? 0 : 1; Error += glm::all(glm::equal(E, glm::vec4(1.f), 0.01f)) ? 0 : 1;
return Error; return Error;
} }

View File

@ -11,6 +11,7 @@ glmCreateTestGTC(ext_scalar_common)
glmCreateTestGTC(ext_scalar_constants) glmCreateTestGTC(ext_scalar_constants)
glmCreateTestGTC(ext_scalar_int_sized) glmCreateTestGTC(ext_scalar_int_sized)
glmCreateTestGTC(ext_scalar_uint_sized) glmCreateTestGTC(ext_scalar_uint_sized)
glmCreateTestGTC(ext_scalar_ulp)
glmCreateTestGTC(ext_scalar_relational) glmCreateTestGTC(ext_scalar_relational)
glmCreateTestGTC(ext_vec1) glmCreateTestGTC(ext_vec1)
glmCreateTestGTC(ext_vector_bool1) glmCreateTestGTC(ext_vector_bool1)
@ -18,3 +19,5 @@ glmCreateTestGTC(ext_vector_common)
glmCreateTestGTC(ext_vector_iec559) glmCreateTestGTC(ext_vector_iec559)
glmCreateTestGTC(ext_vector_integer) glmCreateTestGTC(ext_vector_integer)
glmCreateTestGTC(ext_vector_relational) glmCreateTestGTC(ext_vector_relational)
glmCreateTestGTC(ext_vector_ulp)

View File

@ -0,0 +1,96 @@
#include <glm/ext/scalar_ulp.hpp>
#include <glm/ext/scalar_relational.hpp>
static int test_ulp_float_dist()
{
int Error = 0;
float A = 1.0f;
float B = glm::next_float(A);
Error += glm::notEqual(A, B, 0) ? 0 : 1;
float C = glm::prev_float(B);
Error += glm::equal(A, C, 0) ? 0 : 1;
int D = glm::float_distance(A, B);
Error += D == 1 ? 0 : 1;
int E = glm::float_distance(A, C);
Error += E == 0 ? 0 : 1;
return Error;
}
static int test_ulp_float_step()
{
int Error = 0;
float A = 1.0f;
for(int i = 10; i < 1000; i *= 10)
{
float B = glm::next_float(A, i);
Error += glm::notEqual(A, B, 0) ? 0 : 1;
float C = glm::prev_float(B, i);
Error += glm::equal(A, C, 0) ? 0 : 1;
int D = glm::float_distance(A, B);
Error += D == i ? 0 : 1;
int E = glm::float_distance(A, C);
Error += E == 0 ? 0 : 1;
}
return Error;
}
static int test_ulp_double_dist()
{
int Error = 0;
double A = 1.0;
double B = glm::next_float(A);
Error += glm::notEqual(A, B, 0) ? 0 : 1;
double C = glm::prev_float(B);
Error += glm::equal(A, C, 0) ? 0 : 1;
glm::int64 const D = glm::float_distance(A, B);
Error += D == 1 ? 0 : 1;
glm::int64 const E = glm::float_distance(A, C);
Error += E == 0 ? 0 : 1;
return Error;
}
static int test_ulp_double_step()
{
int Error = 0;
double A = 1.0;
for(int i = 10; i < 1000; i *= 10)
{
double B = glm::next_float(A, i);
Error += glm::notEqual(A, B, 0) ? 0 : 1;
double C = glm::prev_float(B, i);
Error += glm::equal(A, C, 0) ? 0 : 1;
glm::int64 const D = glm::float_distance(A, B);
Error += D == i ? 0 : 1;
glm::int64 const E = glm::float_distance(A, C);
Error += E == 0 ? 0 : 1;
}
return Error;
}
int main()
{
int Error = 0;
Error += test_ulp_float_dist();
Error += test_ulp_float_step();
Error += test_ulp_double_dist();
Error += test_ulp_double_step();
return Error;
}

View File

@ -0,0 +1,99 @@
#include <glm/ext/vector_ulp.hpp>
#include <glm/ext/vector_relational.hpp>
#include <glm/ext/vector_float4.hpp>
#include <glm/ext/vector_double4.hpp>
#include <glm/ext/vector_int4.hpp>
static int test_ulp_float_dist()
{
int Error = 0;
glm::vec4 const A(1.0f);
glm::vec4 const B = glm::next_float(A);
Error += glm::any(glm::notEqual(A, B, 0)) ? 0 : 1;
glm::vec4 const C = glm::prev_float(B);
Error += glm::all(glm::equal(A, C, 0)) ? 0 : 1;
glm::ivec4 const D = glm::float_distance(A, B);
Error += D == glm::ivec4(1) ? 0 : 1;
glm::ivec4 const E = glm::float_distance(A, C);
Error += E == glm::ivec4(0) ? 0 : 1;
return Error;
}
static int test_ulp_float_step()
{
int Error = 0;
glm::vec4 const A(1.0f);
for(int i = 10; i < 1000; i *= 10)
{
glm::vec4 const B = glm::next_float(A, i);
Error += glm::any(glm::notEqual(A, B, 0)) ? 0 : 1;
glm::vec4 const C = glm::prev_float(B, i);
Error += glm::all(glm::equal(A, C, 0)) ? 0 : 1;
glm::ivec4 const D = glm::float_distance(A, B);
Error += D == glm::ivec4(i) ? 0 : 1;
glm::ivec4 const E = glm::float_distance(A, C);
Error += E == glm::ivec4(0) ? 0 : 1;
}
return Error;
}
static int test_ulp_double_dist()
{
int Error = 0;
glm::dvec4 const A(1.0);
glm::dvec4 const B = glm::next_float(A);
Error += glm::any(glm::notEqual(A, B, 0)) ? 0 : 1;
glm::dvec4 const C = glm::prev_float(B);
Error += glm::all(glm::equal(A, C, 0)) ? 0 : 1;
glm::ivec4 const D(glm::float_distance(A, B));
Error += D == glm::ivec4(1) ? 0 : 1;
glm::ivec4 const E = glm::float_distance(A, C);
Error += E == glm::ivec4(0) ? 0 : 1;
return Error;
}
static int test_ulp_double_step()
{
int Error = 0;
glm::dvec4 const A(1.0);
for(int i = 10; i < 1000; i *= 10)
{
glm::dvec4 const B = glm::next_float(A, i);
Error += glm::any(glm::notEqual(A, B, 0)) ? 0 : 1;
glm::dvec4 const C = glm::prev_float(B, i);
Error += glm::all(glm::equal(A, C, 0)) ? 0 : 1;
glm::ivec4 const D(glm::float_distance(A, B));
Error += D == glm::ivec4(i) ? 0 : 1;
glm::ivec4 const E(glm::float_distance(A, C));
Error += E == glm::ivec4(0) ? 0 : 1;
}
return Error;
}
int main()
{
int Error = 0;
Error += test_ulp_float_dist();
Error += test_ulp_float_step();
Error += test_ulp_double_dist();
Error += test_ulp_double_step();
return Error;
}