Fixed constexpr relational function and added tests

This commit is contained in:
Christophe Riccio 2018-08-07 23:52:57 +02:00
parent 13ca6771ca
commit c1be8bf008
8 changed files with 197 additions and 277 deletions

View File

@ -2,170 +2,68 @@
#include "compute_vector_relational.hpp"
// Bug #782: Warning C4701: potentially uninitialized local variable 'Result' used
#if ((GLM_COMPILER & GLM_COMPILER_VC) && (GLM_COMPILER >= GLM_COMPILER_VC12))
# define GLM_BUG_VC_INIT (false)
#else
# define GLM_BUG_VC_INIT
#endif
namespace glm{
namespace detail
namespace glm
{
enum relational_type
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)
{
EQUAL,
NOT_EQUAL,
LESS,
LESS_EQUAL,
GREATER,
GREATER_EQUAL,
ANY,
ALL,
NOT
};
vec<L, bool, Q> Result(true);
for(length_t i = 0; i < L; ++i)
Result[i] = x[i] < y[i];
return Result;
}
template <relational_type R>
struct relational
template<length_t L, typename T, qualifier Q>
GLM_FUNC_QUALIFIER GLM_CONSTEXPR vec<L, bool, Q> lessThanEqual(vec<L, T, Q> const& x, vec<L, T, Q> const& y)
{
template<typename T>
GLM_FUNC_QUALIFIER GLM_CONSTEXPR static bool call(T Src0, T Src1);
};
vec<L, bool, Q> Result(true);
for(length_t i = 0; i < L; ++i)
Result[i] = x[i] <= y[i];
return Result;
}
template <>
struct relational<EQUAL>
template<length_t L, typename T, qualifier Q>
GLM_FUNC_QUALIFIER GLM_CONSTEXPR vec<L, bool, Q> greaterThan(vec<L, T, Q> const& x, vec<L, T, Q> const& y)
{
template<typename T>
GLM_FUNC_QUALIFIER GLM_CONSTEXPR static bool call(T Src0, T Src1)
{
return Src0 == Src1;
}
};
vec<L, bool, Q> Result(true);
for(length_t i = 0; i < L; ++i)
Result[i] = x[i] > y[i];
return Result;
}
template <>
struct relational<NOT_EQUAL>
template<length_t L, typename T, qualifier Q>
GLM_FUNC_QUALIFIER GLM_CONSTEXPR vec<L, bool, Q> greaterThanEqual(vec<L, T, Q> const& x, vec<L, T, Q> const& y)
{
template<typename T>
GLM_FUNC_QUALIFIER GLM_CONSTEXPR static bool call(T Src0, T Src1)
{
return Src0 != Src1;
}
};
vec<L, bool, Q> Result(true);
for(length_t i = 0; i < L; ++i)
Result[i] = x[i] >= y[i];
return Result;
}
template <>
struct relational<LESS>
template<length_t L, typename T, qualifier Q>
GLM_FUNC_QUALIFIER GLM_CONSTEXPR vec<L, bool, Q> equal(vec<L, T, Q> const& x, vec<L, T, Q> const& y)
{
template<typename T>
GLM_FUNC_QUALIFIER GLM_CONSTEXPR static bool call(T Src0, T Src1)
{
return Src0 < Src1;
}
};
vec<L, bool, Q> Result(true);
for(length_t i = 0; i < L; ++i)
Result[i] = x[i] == y[i];
return Result;
}
template <>
struct relational<LESS_EQUAL>
template<length_t L, typename T, qualifier Q>
GLM_FUNC_QUALIFIER GLM_CONSTEXPR vec<L, bool, Q> notEqual(vec<L, T, Q> const& x, vec<L, T, Q> const& y)
{
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 <>
struct relational<ANY>
{
template<typename T>
GLM_FUNC_QUALIFIER GLM_CONSTEXPR static bool call(T Src0, T Src1)
{
return Src0 || Src1;
}
};
template <>
struct relational<ALL>
{
template<typename T>
GLM_FUNC_QUALIFIER GLM_CONSTEXPR static bool call(T Src0, T Src1)
{
return Src0 && Src1;
}
};
template <>
struct relational<NOT>
{
template<typename T>
GLM_FUNC_QUALIFIER GLM_CONSTEXPR static bool call(T Src0, T)
{
return !Src0;
}
};
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&)
{}
};
template<length_t I, length_t N, relational_type R>
struct reduce_relational
{
template<typename vecType>
GLM_FUNC_QUALIFIER GLM_CONSTEXPR static void call(bool& Dst, vecType const& Src)
{
Dst = relational<R>::call(Dst, Src[I]);
reduce_relational<I + 1, N, R>::call(Dst, Src);
}
};
template <length_t N, relational_type R>
struct reduce_relational<N, N, R>
{
template<typename vecType>
GLM_FUNC_QUALIFIER GLM_CONSTEXPR static void call(bool&, vecType const&)
{}
};
}//namespace detail
vec<L, bool, Q> Result(true);
for(length_t i = 0; i < L; ++i)
Result[i] = x[i] != y[i];
return Result;
}
template<length_t L, qualifier Q>
GLM_FUNC_QUALIFIER GLM_CONSTEXPR bool any(vec<L, bool, Q> const& v)
{
bool Result = false;
detail::reduce_relational<0, L, detail::ANY>::call(Result, v);
for(length_t i = 0; i < L; ++i)
Result = Result || v[i];
return Result;
}
@ -173,63 +71,17 @@ namespace detail
GLM_FUNC_QUALIFIER GLM_CONSTEXPR bool all(vec<L, bool, Q> const& v)
{
bool Result = true;
detail::reduce_relational<0, L, detail::ALL>::call(Result, v);
for(length_t i = 0; i < L; ++i)
Result = Result && v[i];
return Result;
}
template<length_t L, qualifier Q>
GLM_FUNC_QUALIFIER GLM_CONSTEXPR vec<L, bool, Q> not_(vec<L, bool, Q> const& v)
{
vec<L, bool, Q> Result;
detail::loop_relational<0, L, detail::NOT>::call(Result, v, v);
return Result;
}
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;
detail::loop_relational<0, L, detail::LESS>::call(Result, x, y);
return Result;
}
template<length_t L, typename T, qualifier Q>
GLM_FUNC_QUALIFIER GLM_CONSTEXPR vec<L, bool, Q> lessThanEqual(vec<L, T, Q> const& x, vec<L, T, Q> const& y)
{
vec<L, bool, Q> Result GLM_BUG_VC_INIT;
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 GLM_CONSTEXPR vec<L, bool, Q> greaterThan(vec<L, T, Q> const& x, vec<L, T, Q> const& y)
{
vec<L, bool, Q> Result GLM_BUG_VC_INIT;
detail::loop_relational<0, L, detail::GREATER>::call(Result, x, y);
return Result;
}
template<length_t L, typename T, qualifier Q>
GLM_FUNC_QUALIFIER GLM_CONSTEXPR vec<L, bool, Q> greaterThanEqual(vec<L, T, Q> const& x, vec<L, T, Q> const& y)
{
vec<L, bool, Q> Result GLM_BUG_VC_INIT;
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 GLM_CONSTEXPR vec<L, bool, Q> equal(vec<L, T, Q> const& x, vec<L, T, Q> const& y)
{
vec<L, bool, Q> Result GLM_BUG_VC_INIT;
detail::loop_relational<0, L, detail::EQUAL>::call(Result, x, y);
return Result;
}
template<length_t L, typename T, qualifier Q>
GLM_FUNC_QUALIFIER GLM_CONSTEXPR vec<L, bool, Q> notEqual(vec<L, T, Q> const& x, vec<L, T, Q> const& y)
{
vec<L, bool, Q> Result GLM_BUG_VC_INIT;
detail::loop_relational<0, L, detail::NOT_EQUAL>::call(Result, x, y);
vec<L, bool, Q> Result(true);
for(length_t i = 0; i < L; ++i)
Result[i] = !v[i];
return Result;
}
}//namespace glm

View File

@ -65,14 +65,14 @@ namespace glm
GLM_FUNC_QUALIFIER GLM_CONSTEXPR T & vec<1, T, Q>::operator[](typename vec<1, T, Q>::length_type i)
{
assert(i >= 0 && i < this->length());
return (&x)[i];
return x;
}
template<typename T, qualifier Q>
GLM_FUNC_QUALIFIER GLM_CONSTEXPR T const& vec<1, T, Q>::operator[](typename vec<1, T, Q>::length_type i) const
{
assert(i >= 0 && i < this->length());
return (&x)[i];
return x;
}
// -- Unary arithmetic operators --

View File

@ -104,14 +104,28 @@ namespace glm
GLM_FUNC_QUALIFIER GLM_CONSTEXPR T & vec<2, T, Q>::operator[](typename vec<2, T, Q>::length_type i)
{
assert(i >= 0 && i < this->length());
return (&x)[i];
switch(i)
{
default:
case 0:
return x;
case 1:
return y;
}
}
template<typename T, qualifier Q>
GLM_FUNC_QUALIFIER GLM_CONSTEXPR T const& vec<2, T, Q>::operator[](typename vec<2, T, Q>::length_type i) const
{
assert(i >= 0 && i < this->length());
return (&x)[i];
switch(i)
{
default:
case 0:
return x;
case 1:
return y;
}
}
// -- Unary arithmetic operators --

View File

@ -168,14 +168,32 @@ namespace glm
GLM_FUNC_QUALIFIER GLM_CONSTEXPR T & vec<3, T, Q>::operator[](typename vec<3, T, Q>::length_type i)
{
assert(i >= 0 && i < this->length());
return (&x)[i];
switch(i)
{
default:
case 0:
return x;
case 1:
return y;
case 2:
return z;
}
}
template<typename T, qualifier Q>
GLM_FUNC_QUALIFIER GLM_CONSTEXPR T const& vec<3, T, Q>::operator[](typename vec<3, T, Q>::length_type i) const
{
assert(i >= 0 && i < this->length());
return (&x)[i];
switch(i)
{
default:
case 0:
return x;
case 1:
return y;
case 2:
return z;
}
}
// -- Unary arithmetic operators --

View File

@ -507,14 +507,36 @@ namespace detail
GLM_FUNC_QUALIFIER GLM_CONSTEXPR T& vec<4, T, Q>::operator[](typename vec<4, T, Q>::length_type i)
{
assert(i >= 0 && i < this->length());
return (&x)[i];
switch(i)
{
default:
case 0:
return x;
case 1:
return y;
case 2:
return z;
case 3:
return w;
}
}
template<typename T, qualifier Q>
GLM_FUNC_QUALIFIER GLM_CONSTEXPR T const& vec<4, T, Q>::operator[](typename vec<4, T, Q>::length_type i) const
{
assert(i >= 0 && i < this->length());
return (&x)[i];
switch(i)
{
default:
case 0:
return x;
case 1:
return y;
case 2:
return z;
case 3:
return w;
}
}
// -- Unary arithmetic operators --

View File

@ -5,62 +5,8 @@
#include "../ext/vector_relational.hpp"
#include "../common.hpp"
namespace glm{
namespace detail
namespace glm
{
enum matrix_relational_type
{
MAT_EQUAL,
MAT_NOT_EQUAL
};
template <relational_type R>
struct matrix_relational
{
template<typename vecType, typename T>
GLM_FUNC_QUALIFIER GLM_CONSTEXPR static bool call(vecType const& Src0, vecType const& Src1, T Epsilon);
};
template <>
struct matrix_relational<EQUAL>
{
template<typename vecType, typename T>
GLM_FUNC_QUALIFIER GLM_CONSTEXPR static bool call(vecType const& Src0, vecType const& Src1, T Epsilon)
{
return all(equal(Src0, Src1, Epsilon));
}
};
template <>
struct matrix_relational<NOT_EQUAL>
{
template<typename vecType, typename T>
GLM_FUNC_QUALIFIER GLM_CONSTEXPR static bool call(vecType const& Src0, vecType const& Src1, T Epsilon)
{
return any(notEqual(Src0, Src1, Epsilon));
}
};
template<length_t I, length_t N, relational_type R>
struct loop_matrix_relational
{
template<typename vecBType, typename matType, typename vecType>
GLM_FUNC_QUALIFIER GLM_CONSTEXPR static void call(vecBType& Dst, matType const& Src0, matType const& Src1, vecType const& Epsilon)
{
Dst[I] = matrix_relational<R>::call(Src0[I], Src1[I], Epsilon[I]);
loop_matrix_relational<I + 1, N, R>::call(Dst, Src0, Src1, Epsilon);
}
};
template <length_t N, relational_type R>
struct loop_matrix_relational<N, N, R>
{
template<typename vecBType, typename matType, typename vecType>
GLM_FUNC_QUALIFIER GLM_CONSTEXPR static void call(vecBType&, matType const&, matType const&, vecType const&)
{}
};
}//namespace detail
template<length_t C, length_t R, typename T, qualifier Q>
GLM_FUNC_QUALIFIER GLM_CONSTEXPR vec<C, bool, Q> equal(mat<C, R, T, Q> const& a, mat<C, R, T, Q> const& b)
{
@ -68,16 +14,17 @@ namespace detail
}
template<length_t C, length_t R, typename T, qualifier Q>
GLM_FUNC_QUALIFIER GLM_CONSTEXPR vec<C, bool, Q> equal(mat<C, R, T, Q> const& a, mat<C, R, T, Q> const& b, T epsilon)
GLM_FUNC_QUALIFIER GLM_CONSTEXPR vec<C, bool, Q> equal(mat<C, R, T, Q> const& a, mat<C, R, T, Q> const& b, T Epsilon)
{
return equal(a, b, vec<C, T, Q>(epsilon));
return equal(a, b, vec<C, T, Q>(Epsilon));
}
template<length_t C, length_t R, typename T, qualifier Q>
GLM_FUNC_QUALIFIER GLM_CONSTEXPR vec<C, bool, Q> equal(mat<C, R, T, Q> const& a, mat<C, R, T, Q> const& b, vec<C, T, Q> const& epsilon)
GLM_FUNC_QUALIFIER GLM_CONSTEXPR vec<C, bool, Q> equal(mat<C, R, T, Q> const& a, mat<C, R, T, Q> const& b, vec<C, T, Q> const& Epsilon)
{
vec<C, bool, Q> Result;
detail::loop_matrix_relational<0, C, detail::EQUAL>::call(Result, a, b, epsilon);
for(length_t i = 0; i < C; ++i)
Result[i] = all(equal(a[i], b[i], Epsilon[i]));
return Result;
}
@ -88,16 +35,17 @@ namespace detail
}
template<length_t C, length_t R, typename T, qualifier Q>
GLM_FUNC_QUALIFIER GLM_CONSTEXPR vec<C, bool, Q> notEqual(mat<C, R, T, Q> const& x, mat<C, R, T, Q> const& y, T epsilon)
GLM_FUNC_QUALIFIER GLM_CONSTEXPR vec<C, bool, Q> notEqual(mat<C, R, T, Q> const& x, mat<C, R, T, Q> const& y, T Epsilon)
{
return notEqual(x, y, vec<C, T, Q>(epsilon));
return notEqual(x, y, vec<C, T, Q>(Epsilon));
}
template<length_t C, length_t R, typename T, qualifier Q>
GLM_FUNC_QUALIFIER GLM_CONSTEXPR vec<C, bool, Q> notEqual(mat<C, R, T, Q> const& a, mat<C, R, T, Q> const& b, vec<C, T, Q> const& epsilon)
GLM_FUNC_QUALIFIER GLM_CONSTEXPR vec<C, bool, Q> notEqual(mat<C, R, T, Q> const& a, mat<C, R, T, Q> const& b, vec<C, T, Q> const& Epsilon)
{
vec<C, bool, Q> Result;
detail::loop_matrix_relational<0, C, detail::NOT_EQUAL>::call(Result, a, b, epsilon);
for(length_t i = 0; i < C; ++i)
Result[i] = any(notEqual(a[i], b[i], Epsilon[i]));
return Result;
}
}//namespace glm

View File

@ -8,26 +8,26 @@
namespace glm
{
template<length_t L, typename T, qualifier Q>
GLM_FUNC_QUALIFIER GLM_CONSTEXPR 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));
return equal(x, y, vec<L, T, Q>(Epsilon));
}
template<length_t L, typename T, qualifier Q>
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)
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);
return lessThanEqual(abs(x - y), Epsilon);
}
template<length_t L, typename T, qualifier Q>
GLM_FUNC_QUALIFIER GLM_CONSTEXPR 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));
return notEqual(x, y, vec<L, T, Q>(Epsilon));
}
template<length_t L, typename T, qualifier Q>
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)
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);
return greaterThan(abs(x - y), Epsilon);
}
}//namespace glm

View File

@ -7,12 +7,30 @@
#include <glm/ext/vector_relational.hpp>
#include <glm/ext/vector_int1.hpp>
#include <glm/ext/vector_bool1.hpp>
#include <glm/ext/vector_bool4.hpp>
#include <glm/ext/vector_float1.hpp>
#include <glm/vector_relational.hpp>
static int test_vec1()
{
int Error = 0;
{
constexpr glm::bvec1 B(true);
constexpr bool A = glm::all(B);
static_assert(A, "GLM: Failed constexpr");
constexpr glm::bvec1 D(true);
constexpr bool C = glm::any(D);
static_assert(C, "GLM: Failed constexpr");
}
{
constexpr glm::bvec2 C(true);
constexpr glm::bvec2 B(true);
static_assert(glm::any(glm::equal(C, B)), "GLM: Failed constexpr");
}
{
constexpr glm::ivec1 O(glm::ivec1(1));
static_assert(glm::ivec1(1) == O, "GLM: Failed constexpr");
@ -154,6 +172,22 @@ static int test_vec2()
{
int Error = 0;
{
constexpr glm::bvec2 B(true);
constexpr bool A = glm::all(B);
static_assert(A, "GLM: Failed constexpr");
constexpr glm::bvec2 D(true, false);
constexpr bool C = glm::any(D);
static_assert(C, "GLM: Failed constexpr");
}
{
constexpr glm::bvec2 C(true);
constexpr glm::bvec2 B(true, false);
static_assert(glm::any(glm::equal(C, B)), "GLM: Failed constexpr");
}
{
constexpr glm::ivec2 O(glm::ivec1(1));
static_assert(glm::ivec2(1) == O, "GLM: Failed constexpr");
@ -311,6 +345,22 @@ static int test_vec3()
{
int Error = 0;
{
constexpr glm::bvec3 B(true);
constexpr bool A = glm::all(B);
static_assert(A, "GLM: Failed constexpr");
constexpr glm::bvec3 D(true, false, true);
constexpr bool C = glm::any(D);
static_assert(C, "GLM: Failed constexpr");
}
{
constexpr glm::bvec3 C(true);
constexpr glm::bvec3 B(true, false, true);
static_assert(glm::any(glm::equal(C, B)), "GLM: Failed constexpr");
}
{
constexpr glm::ivec3 O(glm::ivec1(1));
static_assert(glm::ivec3(1) == O, "GLM: Failed constexpr");
@ -487,6 +537,22 @@ static int test_vec3()
static int test_vec4()
{
int Error = 0;
{
constexpr glm::bvec4 B(true);
constexpr bool A = glm::all(B);
static_assert(A, "GLM: Failed constexpr");
constexpr glm::bvec4 D(true, false, true, false);
constexpr bool C = glm::any(D);
static_assert(C, "GLM: Failed constexpr");
}
{
constexpr glm::bvec4 C(true);
constexpr glm::bvec4 B(true, false, true, false);
static_assert(glm::any(glm::equal(C, B)), "GLM: Failed constexpr");
}
{
constexpr glm::ivec4 O(glm::ivec4(1));