Added constexpr relational operators

This commit is contained in:
Christophe Riccio 2018-08-06 19:28:42 +02:00
parent f82d23949a
commit 389fb2457d
12 changed files with 121 additions and 64 deletions

View File

@ -1,5 +1,4 @@
/// @ref core
/// @file glm/detail/func_vector_relational.inl
#include "compute_vector_relational.hpp"
@ -10,58 +9,119 @@
# define GLM_BUG_VC_INIT
#endif
namespace glm
namespace glm{
namespace detail
{
template<length_t L, typename T, qualifier Q>
GLM_FUNC_QUALIFIER vec<L, bool, Q> lessThan(vec<L, T, Q> const& x, vec<L, T, Q> const& y)
enum relational_type
{
assert(x.length() == y.length());
LESS,
LESS_EQUAL,
GREATER,
GREATER_EQUAL
};
template <relational_type R>
struct relational
{
template<typename T>
GLM_FUNC_QUALIFIER GLM_CONSTEXPR static bool call(T Src0, T Src1);
};
template <>
struct relational<LESS>
{
template<typename T>
GLM_FUNC_QUALIFIER GLM_CONSTEXPR static bool call(T Src0, T Src1)
{
return Src0 < Src1;
}
};
template <>
struct relational<LESS_EQUAL>
{
template<typename T>
GLM_FUNC_QUALIFIER GLM_CONSTEXPR static bool call(T Src0, T Src1)
{
return Src0 <= Src1;
}
};
template <>
struct relational<GREATER>
{
template<typename T>
GLM_FUNC_QUALIFIER GLM_CONSTEXPR static bool call(T Src0, T Src1)
{
return Src0 > Src1;
}
};
template <>
struct relational<GREATER_EQUAL>
{
template<typename T>
GLM_FUNC_QUALIFIER GLM_CONSTEXPR static bool call(T Src0, T Src1)
{
return Src0 >= Src1;
}
};
template<length_t I, length_t N, relational_type R>
struct loop_relational
{
template<typename vecBType, typename vecType>
GLM_FUNC_QUALIFIER GLM_CONSTEXPR static void call(vecBType& Dst, vecType const& Src0, vecType const& Src1)
{
Dst[I] = relational<R>::call(Src0[I], Src1[I]);
loop_relational<I + 1, N, R>::call(Dst, Src0, Src1);
}
};
template <length_t N, relational_type R>
struct loop_relational<N, N, R>
{
template<typename vecBType, typename vecType>
GLM_FUNC_QUALIFIER GLM_CONSTEXPR static void call(vecBType&, vecType const&, vecType const&)
{}
};
}//namespace detail
template<length_t L, typename T, qualifier Q>
GLM_FUNC_QUALIFIER GLM_CONSTEXPR vec<L, bool, Q> lessThan(vec<L, T, Q> const& x, vec<L, T, Q> const& y)
{
vec<L, bool, Q> Result GLM_BUG_VC_INIT;
for(length_t i = 0; i < x.length(); ++i)
Result[i] = x[i] < y[i];
detail::loop_relational<0, L, detail::LESS>::call(Result, x, y);
return Result;
}
template<length_t L, typename T, qualifier Q>
GLM_FUNC_QUALIFIER vec<L, bool, Q> lessThanEqual(vec<L, T, Q> const& x, vec<L, T, Q> const& y)
GLM_FUNC_QUALIFIER GLM_CONSTEXPR vec<L, bool, Q> lessThanEqual(vec<L, T, Q> const& x, vec<L, T, Q> const& y)
{
assert(x.length() == y.length());
vec<L, bool, Q> Result GLM_BUG_VC_INIT;
for(length_t i = 0; i < x.length(); ++i)
Result[i] = x[i] <= y[i];
detail::loop_relational<0, L, detail::LESS_EQUAL>::call(Result, x, y);
return Result;
}
template<length_t L, typename T, qualifier Q>
GLM_FUNC_QUALIFIER vec<L, bool, Q> greaterThan(vec<L, T, Q> const& x, vec<L, T, Q> const& y)
GLM_FUNC_QUALIFIER GLM_CONSTEXPR vec<L, bool, Q> greaterThan(vec<L, T, Q> const& x, vec<L, T, Q> const& y)
{
assert(x.length() == y.length());
vec<L, bool, Q> Result GLM_BUG_VC_INIT;
for(length_t i = 0; i < x.length(); ++i)
Result[i] = x[i] > y[i];
detail::loop_relational<0, L, detail::GREATER>::call(Result, x, y);
return Result;
}
template<length_t L, typename T, qualifier Q>
GLM_FUNC_QUALIFIER vec<L, bool, Q> greaterThanEqual(vec<L, T, Q> const& x, vec<L, T, Q> const& y)
GLM_FUNC_QUALIFIER GLM_CONSTEXPR vec<L, bool, Q> greaterThanEqual(vec<L, T, Q> const& x, vec<L, T, Q> const& y)
{
assert(x.length() == y.length());
vec<L, bool, Q> Result GLM_BUG_VC_INIT;
for(length_t i = 0; i < x.length(); ++i)
Result[i] = x[i] >= y[i];
detail::loop_relational<0, L, detail::GREATER_EQUAL>::call(Result, x, y);
return Result;
}
template<length_t L, typename T, qualifier Q>
GLM_FUNC_QUALIFIER vec<L, bool, Q> equal(vec<L, T, Q> const& x, vec<L, T, Q> const& y)
{
assert(x.length() == y.length());
vec<L, bool, Q> Result GLM_BUG_VC_INIT;
for(length_t i = 0; i < x.length(); ++i)
Result[i] = detail::compute_equal<T, std::numeric_limits<T>::is_iec559>::call(x[i], y[i]);
@ -71,8 +131,6 @@ namespace glm
template<length_t L, typename T, qualifier Q>
GLM_FUNC_QUALIFIER vec<L, bool, Q> notEqual(vec<L, T, Q> const& x, vec<L, T, Q> const& y)
{
assert(x.length() == y.length());
vec<L, bool, Q> Result GLM_BUG_VC_INIT;
for(length_t i = 0; i < x.length(); ++i)
Result[i] = !detail::compute_equal<T, std::numeric_limits<T>::is_iec559>::call(x[i], y[i]);

View File

@ -29,7 +29,7 @@ namespace glm
///
/// @see ext_vector_relational
template<typename genType>
GLM_FUNC_DECL bool equal(genType const& x, genType const& y, genType const& epsilon);
GLM_FUNC_DECL GLM_CONSTEXPR bool equal(genType const& x, genType const& y, genType const& epsilon);
/// Returns the component-wise comparison of |x - y| >= epsilon.
/// True if this expression is not satisfied.
@ -38,7 +38,7 @@ namespace glm
///
/// @see ext_vector_relational
template<typename genType>
GLM_FUNC_DECL bool notEqual(genType const& x, genType const& y, genType const& epsilon);
GLM_FUNC_DECL GLM_CONSTEXPR bool notEqual(genType const& x, genType const& y, genType const& epsilon);
/// @}
}//namespace glm

View File

@ -7,13 +7,13 @@
namespace glm
{
template<typename genType>
GLM_FUNC_QUALIFIER bool equal(genType const& x, genType const& y, genType const& epsilon)
GLM_FUNC_QUALIFIER GLM_CONSTEXPR bool equal(genType const& x, genType const& y, genType const& epsilon)
{
return abs(x - y) <= epsilon;
}
template<typename genType>
GLM_FUNC_QUALIFIER bool notEqual(genType const& x, genType const& y, genType const& epsilon)
GLM_FUNC_QUALIFIER GLM_CONSTEXPR bool notEqual(genType const& x, genType const& y, genType const& epsilon)
{
return abs(x - y) > epsilon;
}

View File

@ -34,7 +34,7 @@ namespace glm
///
/// @see ext_vector_relational
template<length_t L, typename T, qualifier Q>
GLM_FUNC_DECL vec<L, bool, Q> equal(vec<L, T, Q> const& x, vec<L, T, Q> const& y, T epsilon);
GLM_FUNC_DECL GLM_CONSTEXPR vec<L, bool, Q> equal(vec<L, T, Q> const& x, vec<L, T, Q> const& y, T epsilon);
/// Returns the component-wise comparison of |x - y| < epsilon.
/// True if this expression is satisfied.
@ -45,7 +45,7 @@ namespace glm
///
/// @see ext_vector_relational
template<length_t L, typename T, qualifier Q>
GLM_FUNC_DECL vec<L, bool, Q> equal(vec<L, T, Q> const& x, vec<L, T, Q> const& y, vec<L, T, Q> const& epsilon);
GLM_FUNC_DECL GLM_CONSTEXPR vec<L, bool, Q> equal(vec<L, T, Q> const& x, vec<L, T, Q> const& y, vec<L, T, Q> const& epsilon);
/// Returns the component-wise comparison of |x - y| >= epsilon.
/// True if this expression is not satisfied.
@ -56,7 +56,7 @@ namespace glm
///
/// @see ext_vector_relational
template<length_t L, typename T, qualifier Q>
GLM_FUNC_DECL vec<L, bool, Q> notEqual(vec<L, T, Q> const& x, vec<L, T, Q> const& y, T epsilon);
GLM_FUNC_DECL GLM_CONSTEXPR vec<L, bool, Q> notEqual(vec<L, T, Q> const& x, vec<L, T, Q> const& y, T epsilon);
/// Returns the component-wise comparison of |x - y| >= epsilon.
/// True if this expression is not satisfied.
@ -67,7 +67,7 @@ namespace glm
///
/// @see ext_vector_relational
template<length_t L, typename T, qualifier Q>
GLM_FUNC_DECL vec<L, bool, Q> notEqual(vec<L, T, Q> const& x, vec<L, T, Q> const& y, vec<L, T, Q> const& epsilon);
GLM_FUNC_DECL GLM_CONSTEXPR vec<L, bool, Q> notEqual(vec<L, T, Q> const& x, vec<L, T, Q> const& y, vec<L, T, Q> const& epsilon);
/// @}
}//namespace glm

View File

@ -8,25 +8,25 @@
namespace glm
{
template<length_t L, typename T, qualifier Q>
GLM_FUNC_QUALIFIER vec<L, bool, Q> equal(vec<L, T, Q> const& x, vec<L, T, Q> const& y, T epsilon)
GLM_FUNC_QUALIFIER GLM_CONSTEXPR vec<L, bool, Q> equal(vec<L, T, Q> const& x, vec<L, T, Q> const& y, T epsilon)
{
return equal(x, y, vec<L, T, Q>(epsilon));
}
template<length_t L, typename T, qualifier Q>
GLM_FUNC_QUALIFIER vec<L, bool, Q> equal(vec<L, T, Q> const& x, vec<L, T, Q> const& y, vec<L, T, Q> const& epsilon)
GLM_FUNC_QUALIFIER GLM_CONSTEXPR vec<L, bool, Q> equal(vec<L, T, Q> const& x, vec<L, T, Q> const& y, vec<L, T, Q> const& epsilon)
{
return lessThanEqual(abs(x - y), epsilon);
}
template<length_t L, typename T, qualifier Q>
GLM_FUNC_QUALIFIER vec<L, bool, Q> notEqual(vec<L, T, Q> const& x, vec<L, T, Q> const& y, T epsilon)
GLM_FUNC_QUALIFIER GLM_CONSTEXPR vec<L, bool, Q> notEqual(vec<L, T, Q> const& x, vec<L, T, Q> const& y, T epsilon)
{
return notEqual(x, y, vec<L, T, Q>(epsilon));
}
template<length_t L, typename T, qualifier Q>
GLM_FUNC_QUALIFIER vec<L, bool, Q> notEqual(vec<L, T, Q> const& x, vec<L, T, Q> const& y, vec<L, T, Q> const& epsilon)
GLM_FUNC_QUALIFIER GLM_CONSTEXPR vec<L, bool, Q> notEqual(vec<L, T, Q> const& x, vec<L, T, Q> const& y, vec<L, T, Q> const& epsilon)
{
return greaterThan(abs(x - y), epsilon);
}

View File

@ -45,9 +45,9 @@ namespace glm
union
{
struct { T x, y, z, w;};
};
typename detail::storage<4, T, detail::is_aligned<Q>::value>::type data;
};
# else
T x, y, z, w;
# endif

View File

@ -33,7 +33,7 @@ namespace glm
/// @see <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/lessThan.xml">GLSL lessThan man page</a>
/// @see <a href="http://www.opengl.org/registry/doc/GLSLangSpec.4.20.8.pdf">GLSL 4.20.8 specification, section 8.7 Vector Relational Functions</a>
template<length_t L, typename T, qualifier Q>
GLM_FUNC_DECL vec<L, bool, Q> lessThan(vec<L, T, Q> const& x, vec<L, T, Q> const& y);
GLM_FUNC_DECL GLM_CONSTEXPR vec<L, bool, Q> lessThan(vec<L, T, Q> const& x, vec<L, T, Q> const& y);
/// Returns the component-wise comparison of result x <= y.
///
@ -43,7 +43,7 @@ namespace glm
/// @see <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/lessThanEqual.xml">GLSL lessThanEqual man page</a>
/// @see <a href="http://www.opengl.org/registry/doc/GLSLangSpec.4.20.8.pdf">GLSL 4.20.8 specification, section 8.7 Vector Relational Functions</a>
template<length_t L, typename T, qualifier Q>
GLM_FUNC_DECL vec<L, bool, Q> lessThanEqual(vec<L, T, Q> const& x, vec<L, T, Q> const& y);
GLM_FUNC_DECL GLM_CONSTEXPR vec<L, bool, Q> lessThanEqual(vec<L, T, Q> const& x, vec<L, T, Q> const& y);
/// Returns the component-wise comparison of result x > y.
///
@ -53,7 +53,7 @@ namespace glm
/// @see <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/greaterThan.xml">GLSL greaterThan man page</a>
/// @see <a href="http://www.opengl.org/registry/doc/GLSLangSpec.4.20.8.pdf">GLSL 4.20.8 specification, section 8.7 Vector Relational Functions</a>
template<length_t L, typename T, qualifier Q>
GLM_FUNC_DECL vec<L, bool, Q> greaterThan(vec<L, T, Q> const& x, vec<L, T, Q> const& y);
GLM_FUNC_DECL GLM_CONSTEXPR vec<L, bool, Q> greaterThan(vec<L, T, Q> const& x, vec<L, T, Q> const& y);
/// Returns the component-wise comparison of result x >= y.
///
@ -63,7 +63,7 @@ namespace glm
/// @see <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/greaterThanEqual.xml">GLSL greaterThanEqual man page</a>
/// @see <a href="http://www.opengl.org/registry/doc/GLSLangSpec.4.20.8.pdf">GLSL 4.20.8 specification, section 8.7 Vector Relational Functions</a>
template<length_t L, typename T, qualifier Q>
GLM_FUNC_DECL vec<L, bool, Q> greaterThanEqual(vec<L, T, Q> const& x, vec<L, T, Q> const& y);
GLM_FUNC_DECL GLM_CONSTEXPR vec<L, bool, Q> greaterThanEqual(vec<L, T, Q> const& x, vec<L, T, Q> const& y);
/// Returns the component-wise comparison of result x == y.
///

View File

@ -13,29 +13,29 @@ static int test_operators()
float const Epsilon = 0.001f;
glm::mat4x4 const M(2.0f);
glm::mat4x4 const N(1.0f);
glm::vec4 const U(2.0f);
matType const M(2.0f);
matType const N(1.0f);
vecType const U(2.0f);
{
glm::mat4 const P = N * 2.0f;
matType const P = N * 2.0f;
Error += glm::all(glm::equal(P, M, Epsilon)) ? 0 : 1;
glm::mat4 const Q = M / 2.0f;
matType const Q = M / 2.0f;
Error += glm::all(glm::equal(Q, N, Epsilon)) ? 0 : 1;
}
{
glm::vec4 const V = M * U;
Error += glm::all(glm::equal(V, glm::vec4(4.f), Epsilon)) ? 0 : 1;
vecType const V = M * U;
Error += glm::all(glm::equal(V, vecType(4.f), Epsilon)) ? 0 : 1;
glm::vec4 const W = U / M;
Error += glm::all(glm::equal(V, W, Epsilon)) ? 0 : 1;
vecType const W = U / M;
Error += glm::all(glm::equal(W, vecType(1.f), Epsilon)) ? 0 : 1;
}
{
glm::mat4 const O = M * N;
Error += glm::all(glm::equal(O, glm::mat4(4.f), Epsilon)) ? 0 : 1;
matType const O = M * N;
Error += glm::all(glm::equal(O, matType(2.f), Epsilon)) ? 0 : 1;
}
return Error;

View File

@ -1,4 +1,5 @@
#include <glm/gtc/constants.hpp>
#include <glm/ext/scalar_relational.hpp>
#include <glm/ext/vector_relational.hpp>
#include <glm/ext/vector_dvec1.hpp>
#include <glm/ext/vector_dvec1_precision.hpp>
@ -89,9 +90,6 @@ static int test_constexpr()
{
# if GLM_CONFIG_CONSTEXP == GLM_ENABLE
static_assert(genType::length() == 1, "GLM: Failed constexpr");
static_assert(glm::equal(genType(1)[0], 1.0, glm::epsilon<double>()), "GLM: Failed constexpr");
static_assert(glm::all(glm::equal(genType(1), genType(glm::vec1(1), glm::epsilon<double>()), "GLM: Failed constexpr");
static_assert(glm::all(glm::notEqual(genType(1), genType(0), glm::epsilon<double>())), "GLM: Failed constexpr");
# endif
return 0;

View File

@ -89,9 +89,6 @@ static int test_constexpr()
{
# if GLM_CONFIG_CONSTEXP == GLM_ENABLE
static_assert(genType::length() == 1, "GLM: Failed constexpr");
static_assert(glm::equal(genType(1)[0], 1.0f, glm::epsilon<float>()), "GLM: Failed constexpr");
static_assert(glm::all(glm::equal(genType(1), genType(glm::vec1(1), glm::epsilon<float>()), "GLM: Failed constexpr");
static_assert(glm::all(glm::notEqual(genType(1), genType(0), glm::epsilon<float>())), "GLM: Failed constexpr");
# endif
return 0;

View File

@ -301,8 +301,10 @@ int test_size()
{
int Error = 0;
Error += 16 == sizeof(glm::quat) ? 0 : 1;
Error += 32 == sizeof(glm::dquat) ? 0 : 1;
std::size_t const A = sizeof(glm::quat);
Error += 16 == A ? 0 : 1;
std::size_t const B = sizeof(glm::dquat);
Error += 32 == B ? 0 : 1;
Error += glm::quat().length() == 4 ? 0 : 1;
Error += glm::dquat().length() == 4 ? 0 : 1;
Error += glm::quat::length() == 4 ? 0 : 1;

View File

@ -2,7 +2,9 @@
#if GLM_CONFIG_ALIGNED_GENTYPES == GLM_ENABLE
#include <glm/gtc/type_aligned.hpp>
#include <glm/gtc/type_precision.hpp>
#include <glm/ext/vector_relational.hpp>
#include <glm/ext/matrix_relational.hpp>
GLM_STATIC_ASSERT(glm::detail::is_aligned<glm::aligned_lowp>::value, "aligned_lowp is not aligned");
GLM_STATIC_ASSERT(glm::detail::is_aligned<glm::aligned_mediump>::value, "aligned_mediump is not aligned");