diff --git a/glm/detail/qualifier.hpp b/glm/detail/qualifier.hpp index 25ddf8ab..8c31c6dd 100644 --- a/glm/detail/qualifier.hpp +++ b/glm/detail/qualifier.hpp @@ -178,7 +178,7 @@ namespace detail template struct init_gentype { - GLM_FUNC_QUALIFIER static genType identity() + GLM_FUNC_QUALIFIER GLM_CONSTEXPR static genType identity() { return genType(1, 0, 0, 0); } @@ -187,7 +187,7 @@ namespace detail template struct init_gentype { - GLM_FUNC_QUALIFIER static genType identity() + GLM_FUNC_QUALIFIER GLM_CONSTEXPR static genType identity() { return genType(1); } diff --git a/glm/detail/setup.hpp b/glm/detail/setup.hpp index 380b63b7..da130b63 100644 --- a/glm/detail/setup.hpp +++ b/glm/detail/setup.hpp @@ -533,9 +533,11 @@ namespace glm }//namespace glm /////////////////////////////////////////////////////////////////////////////////// -// countof +// constexpr #if GLM_HAS_CONSTEXPR +# define GLM_USE_CONSTEXP GLM_ENABLE + namespace glm { template @@ -546,8 +548,12 @@ namespace glm }//namespace glm # define GLM_COUNTOF(arr) glm::countof(arr) #elif defined(_MSC_VER) +# define GLM_USE_CONSTEXP GLM_DISABLE + # define GLM_COUNTOF(arr) _countof(arr) #else +# define GLM_USE_CONSTEXP GLM_DISABLE + # define GLM_COUNTOF(arr) sizeof(arr) / sizeof(arr[0]) #endif diff --git a/glm/gtc/matrix_transform.hpp b/glm/gtc/matrix_transform.hpp index 4d3b8c83..cc94f634 100644 --- a/glm/gtc/matrix_transform.hpp +++ b/glm/gtc/matrix_transform.hpp @@ -38,7 +38,7 @@ namespace glm /// Builds an identity matrix. template - GLM_FUNC_DECL genType identity(); + GLM_FUNC_DECL GLM_CONSTEXPR genType identity(); /// Builds a translation 4 * 4 matrix created from a vector of 3 components. /// diff --git a/glm/gtc/matrix_transform.inl b/glm/gtc/matrix_transform.inl index 7d0982d6..72349e04 100644 --- a/glm/gtc/matrix_transform.inl +++ b/glm/gtc/matrix_transform.inl @@ -8,7 +8,7 @@ namespace glm { template - GLM_FUNC_QUALIFIER genType identity() + GLM_FUNC_QUALIFIER GLM_CONSTEXPR genType identity() { return detail::init_gentype::GENTYPE>::identity(); } diff --git a/glm/gtc/quaternion.hpp b/glm/gtc/quaternion.hpp index 240f3d12..2d65dea0 100644 --- a/glm/gtc/quaternion.hpp +++ b/glm/gtc/quaternion.hpp @@ -57,7 +57,7 @@ namespace glm GLM_FUNC_DECL static GLM_CONSTEXPR length_type length(){return 4;} GLM_FUNC_DECL T & operator[](length_type i); - GLM_FUNC_DECL T const& operator[](length_type i) const; + GLM_FUNC_DECL GLM_CONSTEXPR T const& operator[](length_type i) const; // -- Implicit basic constructors -- @@ -156,14 +156,14 @@ namespace glm // -- Boolean operators -- template - GLM_FUNC_DECL bool operator==(tquat const& q1, tquat const& q2); + GLM_FUNC_DECL GLM_CONSTEXPR bool operator==(tquat const& q1, tquat const& q2); template - GLM_FUNC_DECL bool operator!=(tquat const& q1, tquat const& q2); + GLM_FUNC_DECL GLM_CONSTEXPR bool operator!=(tquat const& q1, tquat const& q2); /// Builds an identity quaternion. template - GLM_FUNC_DECL genType identity(); + GLM_FUNC_DECL GLM_CONSTEXPR genType identity(); /// Returns the length of the quaternion. /// diff --git a/glm/gtc/quaternion.inl b/glm/gtc/quaternion.inl index f235e173..d7b01a09 100644 --- a/glm/gtc/quaternion.inl +++ b/glm/gtc/quaternion.inl @@ -82,7 +82,7 @@ namespace detail } template - GLM_FUNC_QUALIFIER T const& tquat::operator[](typename tquat::length_type i) const + GLM_FUNC_QUALIFIER GLM_CONSTEXPR T const& tquat::operator[](typename tquat::length_type i) const { assert(i >= 0 && i < this->length()); return (&x)[i]; @@ -377,15 +377,15 @@ namespace detail // -- Boolean operators -- template - GLM_FUNC_QUALIFIER bool operator==(tquat const& q1, tquat const& q2) + GLM_FUNC_QUALIFIER GLM_CONSTEXPR bool operator==(tquat const& q1, tquat const& q2) { - return all(epsilonEqual(q1, q2, epsilon())); + return q1.x == q2.x && q1.y == q2.y && q1.z == q2.z && q1.w == q2.w; } template - GLM_FUNC_QUALIFIER bool operator!=(tquat const& q1, tquat const& q2) + GLM_FUNC_QUALIFIER GLM_CONSTEXPR bool operator!=(tquat const& q1, tquat const& q2) { - return any(epsilonNotEqual(q1, q2, epsilon())); + return q1.x != q2.x || q1.y != q2.y || q1.z != q2.z || q1.w != q2.w; } // -- Operations -- diff --git a/test/core/CMakeLists.txt b/test/core/CMakeLists.txt index 12d76bf3..1990cff1 100644 --- a/test/core/CMakeLists.txt +++ b/test/core/CMakeLists.txt @@ -1,3 +1,4 @@ +glmCreateTestGTC(core_cpp_constexpr) glmCreateTestGTC(core_cpp_defaulted_ctor) glmCreateTestGTC(core_force_aligned_gentypes) glmCreateTestGTC(core_force_ctor_init) diff --git a/test/core/core_cpp_constexpr.cpp b/test/core/core_cpp_constexpr.cpp new file mode 100644 index 00000000..c8d07e6c --- /dev/null +++ b/test/core/core_cpp_constexpr.cpp @@ -0,0 +1,278 @@ +#include + +#if GLM_USE_CONSTEXP == GLM_ENABLE + +#include +#include +#include +#include + +static int test_vec1() +{ + int Error = 0; + + { + constexpr glm::ivec1 O(glm::ivec1(1)); + static_assert(glm::ivec1(1) == O, "GLM: Failed constexpr"); + + constexpr glm::ivec1 P(1); + static_assert(glm::ivec1(1) == P, "GLM: Failed constexpr"); + } + + { + constexpr glm::ivec1 L(glm::ivec2(1, 2)); + static_assert(glm::ivec1(1) == L, "GLM: Failed constexpr"); + + constexpr glm::ivec1 M(glm::ivec3(1, 2, 3)); + static_assert(glm::ivec1(1) == M, "GLM: Failed constexpr"); + + constexpr glm::ivec1 N(glm::ivec4(1, 2, 3, 4)); + static_assert(glm::ivec1(1) == N, "GLM: Failed constexpr"); + } + + { + static_assert(glm::vec1(1.0f).x > 0.0f, "GLM: Failed constexpr"); + static_assert(glm::vec1::length() == 1, "GLM: Failed constexpr"); + } + + { + constexpr glm::bvec1 A1(true); + constexpr glm::bvec1 A2(true); + constexpr glm::bvec1 B1(false); + constexpr glm::bvec1 B2(false); + static_assert(A1 == A2 && B1 == B2, "GLM: Failed constexpr"); + static_assert(A1 == A2 || B1 == B2, "GLM: Failed constexpr"); + } + + return Error; +} + +static int test_vec2() +{ + int Error = 0; + + { + constexpr glm::ivec2 O(glm::ivec1(1)); + static_assert(glm::ivec2(1) == O, "GLM: Failed constexpr"); + + constexpr glm::ivec2 A(1); + static_assert(glm::ivec2(1) == A, "GLM: Failed constexpr"); + } + + { + constexpr glm::ivec2 F(glm::ivec1(1), glm::ivec1(2)); + static_assert(glm::ivec2(1, 2) == F, "GLM: Failed constexpr"); + + constexpr glm::ivec2 G(1, glm::ivec1(2)); + static_assert(glm::ivec2(1, 2) == G, "GLM: Failed constexpr"); + + constexpr glm::ivec2 H(glm::ivec1(1), 2); + static_assert(glm::ivec2(1, 2) == H, "GLM: Failed constexpr"); + + constexpr glm::ivec2 I(1, 2); + static_assert(glm::ivec2(1, 2) == I, "GLM: Failed constexpr"); + } + + { + constexpr glm::ivec2 L(glm::ivec2(1, 2)); + static_assert(glm::ivec2(1, 2) == L, "GLM: Failed constexpr"); + + constexpr glm::ivec2 M(glm::ivec3(1, 2, 3)); + static_assert(glm::ivec2(1, 2) == M, "GLM: Failed constexpr"); + + constexpr glm::ivec2 N(glm::ivec4(1, 2, 3, 4)); + static_assert(glm::ivec2(1, 2) == N, "GLM: Failed constexpr"); + } + + { + static_assert(glm::vec2(1.0f).x > 0.0f, "GLM: Failed constexpr"); + static_assert(glm::vec2(1.0f, -1.0f).x > 0.0f, "GLM: Failed constexpr"); + static_assert(glm::vec2(1.0f, -1.0f).y < 0.0f, "GLM: Failed constexpr"); + static_assert(glm::vec2::length() == 2, "GLM: Failed constexpr"); + } + + { + constexpr glm::bvec2 A1(true); + constexpr glm::bvec2 A2(true); + constexpr glm::bvec2 B1(false); + constexpr glm::bvec2 B2(false); + static_assert(A1 == A2 && B1 == B2, "GLM: Failed constexpr"); + static_assert(A1 == A2 || B1 == B2, "GLM: Failed constexpr"); + } + + return Error; +} + +static int test_vec3() +{ + int Error = 0; + + { + constexpr glm::ivec3 O(glm::ivec1(1)); + static_assert(glm::ivec3(1) == O, "GLM: Failed constexpr"); + + constexpr glm::ivec3 A(1); + static_assert(glm::ivec3(1) == A, "GLM: Failed constexpr"); + } + + { + constexpr glm::ivec3 B(glm::ivec2(1, 2), 3); + static_assert(glm::ivec3(1, 2, 3) == B, "GLM: Failed constexpr"); + + constexpr glm::ivec3 C(1, glm::ivec2(2, 3)); + static_assert(glm::ivec3(1, 2, 3) == C, "GLM: Failed constexpr"); + + constexpr glm::ivec3 D(glm::ivec1(1), glm::ivec2(2, 3)); + static_assert(glm::ivec3(1, 2, 3) == D, "GLM: Failed constexpr"); + + constexpr glm::ivec3 E(glm::ivec2(1, 2), glm::ivec1(3)); + static_assert(glm::ivec3(1, 2, 3) == E, "GLM: Failed constexpr"); + } + + { + constexpr glm::ivec3 F(glm::ivec1(1), glm::ivec1(2), glm::ivec1(3)); + static_assert(glm::ivec3(1, 2, 3) == F, "GLM: Failed constexpr"); + + constexpr glm::ivec3 G(1, glm::ivec1(2), glm::ivec1(3)); + static_assert(glm::ivec3(1, 2, 3) == G, "GLM: Failed constexpr"); + + constexpr glm::ivec3 H(glm::ivec1(1), 2, glm::ivec1(3)); + static_assert(glm::ivec3(1, 2, 3) == H, "GLM: Failed constexpr"); + + constexpr glm::ivec3 I(1, 2, glm::ivec1(3)); + static_assert(glm::ivec3(1, 2, 3) == I, "GLM: Failed constexpr"); + + constexpr glm::ivec3 J(glm::ivec1(1), glm::ivec1(2), 3); + static_assert(glm::ivec3(1, 2, 3) == J, "GLM: Failed constexpr"); + + constexpr glm::ivec3 K(1, glm::ivec1(2), 3); + static_assert(glm::ivec3(1, 2, 3) == K, "GLM: Failed constexpr"); + + constexpr glm::ivec3 L(glm::ivec1(1), 2, 3); + static_assert(glm::ivec3(1, 2, 3) == L, "GLM: Failed constexpr"); + + constexpr glm::ivec3 M(1, 2, 3); + static_assert(glm::ivec3(1, 2, 3) == M, "GLM: Failed constexpr"); + } + + { + constexpr glm::ivec3 N(glm::ivec4(1, 2, 3, 4)); + static_assert(glm::ivec3(1, 2, 3) == N, "GLM: Failed constexpr"); + } + + { + static_assert(glm::vec3(1.0f).x > 0.0f, "GLM: Failed constexpr"); + static_assert(glm::vec3(1.0f, -1.0f, -1.0f).x > 0.0f, "GLM: Failed constexpr"); + static_assert(glm::vec3(1.0f, -1.0f, -1.0f).y < 0.0f, "GLM: Failed constexpr"); + static_assert(glm::vec3::length() == 3, "GLM: Failed constexpr"); + } + + { + constexpr glm::bvec3 A1(true); + constexpr glm::bvec3 A2(true); + constexpr glm::bvec3 B1(false); + constexpr glm::bvec3 B2(false); + static_assert(A1 == A2 && B1 == B2, "GLM: Failed constexpr"); + static_assert(A1 == A2 || B1 == B2, "GLM: Failed constexpr"); + } + + return Error; +} + +static int test_vec4() +{ + int Error = 0; + + { + constexpr glm::ivec4 O(glm::ivec4(1)); + static_assert(glm::ivec4(1) == O, "GLM: Failed constexpr"); + + constexpr glm::ivec4 A(1); + static_assert(glm::ivec4(1) == A, "GLM: Failed constexpr"); + + constexpr glm::ivec4 N(glm::ivec4(1, 2, 3, 4)); + static_assert(glm::ivec4(1, 2, 3, 4) == N, "GLM: Failed constexpr"); + } + + { + constexpr glm::ivec4 A(glm::ivec3(1, 2, 3), 4); + static_assert(glm::ivec4(1, 2, 3, 4) == A, "GLM: Failed constexpr"); + + constexpr glm::ivec4 B(glm::ivec2(1, 2), glm::ivec2(3, 4)); + static_assert(glm::ivec4(1, 2, 3, 4) == B, "GLM: Failed constexpr"); + + constexpr glm::ivec4 C(1, glm::ivec3(2, 3, 4)); + static_assert(glm::ivec4(1, 2, 3, 4) == C, "GLM: Failed constexpr"); + + constexpr glm::ivec4 D(glm::ivec1(1), glm::ivec2(2, 3), glm::ivec1(4)); + static_assert(glm::ivec4(1, 2, 3, 4) == D, "GLM: Failed constexpr"); + + constexpr glm::ivec4 E(glm::ivec2(1, 2), glm::ivec1(3), glm::ivec1(4)); + static_assert(glm::ivec4(1, 2, 3, 4) == E, "GLM: Failed constexpr"); + + constexpr glm::ivec4 F(glm::ivec1(1), glm::ivec1(2), glm::ivec2(3, 4)); + static_assert(glm::ivec4(1, 2, 3, 4) == F, "GLM: Failed constexpr"); + } + + { + static_assert(glm::ivec4(1).x > 0, "GLM: Failed constexpr"); + static_assert(glm::ivec4(1.0f, -1.0f, -1.0f, 1.0f).x > 0, "GLM: Failed constexpr"); + static_assert(glm::ivec4(1.0f, -1.0f, -1.0f, 1.0f).y < 0, "GLM: Failed constexpr"); + static_assert(glm::ivec4::length() == 4, "GLM: Failed constexpr"); + } + + { + constexpr glm::bvec4 A1(true); + constexpr glm::bvec4 A2(true); + constexpr glm::bvec4 B1(false); + constexpr glm::bvec4 B2(false); + static_assert(A1 == A2 && B1 == B2, "GLM: Failed constexpr"); + static_assert(A1 == A2 || B1 == B2, "GLM: Failed constexpr"); + } + + return Error; +} + +static int test_quat() +{ + int Error = 0; + + { + static_assert(glm::quat::length() == 4, "GLM: Failed constexpr"); + static_assert(glm::quat(1.0f, glm::vec3(0.0f)).w > 0.0f, "GLM: Failed constexpr"); + static_assert(glm::quat(1.0f, 0.0f, 0.0f, 0.0f).w > 0.0f, "GLM: Failed constexpr"); + + glm::quat constexpr Q = glm::identity(); + static_assert(Q.x - glm::quat(1.0f, glm::vec3(0.0f)).x <= glm::epsilon(), "GLM: Failed constexpr"); + } + + return Error; +} + +static int test_mat2x2() +{ + int Error = 0; + + static_assert(glm::mat2x2::length() == 2, "GLM: Failed constexpr"); + + return Error; +} + +#endif//GLM_USE_CONSTEXP == GLM_ENABLE + +int main() +{ + int Error = 0; + +# if GLM_USE_CONSTEXP == GLM_ENABLE + Error += test_vec1(); + Error += test_vec2(); + Error += test_vec3(); + Error += test_vec4(); + Error += test_quat(); + Error += test_mat2x2(); +# endif//GLM_USE_CONSTEXP == GLM_ENABLE + + return Error; +} +