Made some math functions constexpr using gcem.

This commit is contained in:
Patrick 2023-01-12 22:24:35 +01:00
parent cab263a56d
commit 16b7a1d7d4
Signed by: mewin
GPG Key ID: CEDB412C39B5BC47
6 changed files with 69 additions and 13 deletions

View File

@ -7,6 +7,12 @@
#include <cmath> #include <cmath>
#include <cassert> #include <cassert>
// BEGIN @MEWIN - 2023-01-12 - Made some math functions constexpr using gcem.
#if GLM_ENABLE_CONSTEXPR_MATH == GLM_ENABLE
# include <gcem.hpp>
#endif
// END @MEWIN
namespace glm{ namespace glm{
namespace detail namespace detail
{ {
@ -34,10 +40,12 @@ namespace detail
template<length_t L, typename T, qualifier Q, bool Aligned> template<length_t L, typename T, qualifier Q, bool Aligned>
struct compute_sqrt struct compute_sqrt
{ {
GLM_FUNC_QUALIFIER static vec<L, T, Q> call(vec<L, T, Q> const& x) // BEGIN @MEWIN - 2023-01-12 - Made some math functions constexpr using gcem.
GLM_FUNC_QUALIFIER GLM_MATH_CONSTEXPR static vec<L, T, Q> call(vec<L, T, Q> const& x)
{ {
return detail::functor1<vec, L, T, T, Q>::call(std::sqrt, x); return detail::functor1<vec, L, T, T, Q>::call(GLM_MATH_BASE_NS::sqrt, x);
} }
// END @MEWIN
}; };
template<length_t L, typename T, qualifier Q, bool Aligned> template<length_t L, typename T, qualifier Q, bool Aligned>
@ -123,9 +131,13 @@ namespace detail
} }
// sqrt // sqrt
using std::sqrt; // BEGIN @MEWIN - 2023-01-12 - Made some math functions constexpr using gcem.
using GLM_MATH_BASE_NS::sqrt;
// END @MEWIN
template<length_t L, typename T, qualifier Q> template<length_t L, typename T, qualifier Q>
GLM_FUNC_QUALIFIER vec<L, T, Q> sqrt(vec<L, T, Q> const& x) // BEGIN @MEWIN - 2023-01-12 - Made some math functions constexpr using gcem.
GLM_FUNC_QUALIFIER GLM_MATH_CONSTEXPR vec<L, T, Q> sqrt(vec<L, T, Q> const& x)
// END @MEWIN
{ {
GLM_STATIC_ASSERT(std::numeric_limits<T>::is_iec559, "'sqrt' only accept floating-point inputs"); GLM_STATIC_ASSERT(std::numeric_limits<T>::is_iec559, "'sqrt' only accept floating-point inputs");
return detail::compute_sqrt<L, T, Q, detail::is_aligned<Q>::value>::call(x); return detail::compute_sqrt<L, T, Q, detail::is_aligned<Q>::value>::call(x);

View File

@ -7,7 +7,9 @@ namespace detail
template<length_t L, typename T, qualifier Q, bool Aligned> template<length_t L, typename T, qualifier Q, bool Aligned>
struct compute_length struct compute_length
{ {
GLM_FUNC_QUALIFIER static T call(vec<L, T, Q> const& v) // BEGIN @MEWIN - 2023-01-12 - Made some math functions constexpr using gcem.
GLM_FUNC_QUALIFIER GLM_MATH_CONSTEXPR static T call(vec<L, T, Q> const& v)
// END @MEWIN
{ {
return sqrt(dot(v, v)); return sqrt(dot(v, v));
} }
@ -16,7 +18,9 @@ namespace detail
template<length_t L, typename T, qualifier Q, bool Aligned> template<length_t L, typename T, qualifier Q, bool Aligned>
struct compute_distance struct compute_distance
{ {
GLM_FUNC_QUALIFIER static T call(vec<L, T, Q> const& p0, vec<L, T, Q> const& p1) // BEGIN @MEWIN - 2023-01-12 - Made some math functions constexpr using gcem.
GLM_FUNC_QUALIFIER static GLM_MATH_CONSTEXPR T call(vec<L, T, Q> const& p0, vec<L, T, Q> const& p1)
// END @MEWIN
{ {
return length(p1 - p0); return length(p1 - p0);
} }
@ -125,7 +129,9 @@ namespace detail
// length // length
template<typename genType> template<typename genType>
GLM_FUNC_QUALIFIER genType length(genType x) // BEGIN @MEWIN - 2023-01-12 - Made some math functions constexpr using gcem.
GLM_FUNC_QUALIFIER GLM_MATH_CONSTEXPR genType length(genType x)
// END @MEWIN
{ {
GLM_STATIC_ASSERT(std::numeric_limits<genType>::is_iec559, "'length' accepts only floating-point inputs"); GLM_STATIC_ASSERT(std::numeric_limits<genType>::is_iec559, "'length' accepts only floating-point inputs");
@ -133,7 +139,9 @@ namespace detail
} }
template<length_t L, typename T, qualifier Q> template<length_t L, typename T, qualifier Q>
GLM_FUNC_QUALIFIER T length(vec<L, T, Q> const& v) // BEGIN @MEWIN - 2023-01-12 - Made some math functions constexpr using gcem.
GLM_FUNC_QUALIFIER GLM_MATH_CONSTEXPR T length(vec<L, T, Q> const& v)
// END @MEWIN
{ {
GLM_STATIC_ASSERT(std::numeric_limits<T>::is_iec559, "'length' accepts only floating-point inputs"); GLM_STATIC_ASSERT(std::numeric_limits<T>::is_iec559, "'length' accepts only floating-point inputs");
@ -142,7 +150,9 @@ namespace detail
// distance // distance
template<typename genType> template<typename genType>
GLM_FUNC_QUALIFIER genType distance(genType const& p0, genType const& p1) // BEGIN @MEWIN - 2023-01-12 - Made some math functions constexpr using gcem.
GLM_FUNC_QUALIFIER GLM_MATH_CONSTEXPR genType distance(genType const& p0, genType const& p1)
// END @MEWIN
{ {
GLM_STATIC_ASSERT(std::numeric_limits<genType>::is_iec559, "'distance' accepts only floating-point inputs"); GLM_STATIC_ASSERT(std::numeric_limits<genType>::is_iec559, "'distance' accepts only floating-point inputs");
@ -150,7 +160,9 @@ namespace detail
} }
template<length_t L, typename T, qualifier Q> template<length_t L, typename T, qualifier Q>
GLM_FUNC_QUALIFIER T distance(vec<L, T, Q> const& p0, vec<L, T, Q> const& p1) // BEGIN @MEWIN - 2023-01-12 - Made some math functions constexpr using gcem.
GLM_FUNC_QUALIFIER GLM_MATH_CONSTEXPR T distance(vec<L, T, Q> const& p0, vec<L, T, Q> const& p1)
// END @MEWIN
{ {
return detail::compute_distance<L, T, Q, detail::is_aligned<Q>::value>::call(p0, p1); return detail::compute_distance<L, T, Q, detail::is_aligned<Q>::value>::call(p0, p1);
} }

View File

@ -308,7 +308,26 @@
# define GLM_CONSTEXPR # define GLM_CONSTEXPR
#endif #endif
// BEGIN @MEWIN - 2023-01-12 - Made some math functions constexpr using gcem.
#if !defined(GLM_ENABLE_CONSTEXPR_MATH)
# if (GLM_LANG & GLM_LANG_CXX17_FLAG)
# define GLM_ENABLE_CONSTEXPR_MATH __has_include(<gcem.hpp>)
# else
# define GLM_ENABLE_CONSTEXPR_MATH 0
# endif
#endif
#if GLM_ENABLE_CONSTEXPR_MATH == GLM_ENABLE
# define GLM_MATH_CONSTEXPR GLM_CONSTEXPR
# define GLM_MATH_BASE_NS gcem
#else
# define GLM_MATH_CONSTEXPR GLM_INLINE
# define GLM_MATH_BASE_NS std
#endif
// END @MEWIN
// //
#if GLM_HAS_CONSTEXPR #if GLM_HAS_CONSTEXPR
# if (GLM_COMPILER & GLM_COMPILER_CLANG) # if (GLM_COMPILER & GLM_COMPILER_CLANG)
# if __has_feature(cxx_if_constexpr) # if __has_feature(cxx_if_constexpr)

View File

@ -20,6 +20,12 @@
#include "detail/type_vec4.hpp" #include "detail/type_vec4.hpp"
#include <cmath> #include <cmath>
// BEGIN @MEWIN - 2023-01-12 - Made some math functions constexpr using gcem.
#if GLM_ENABLE_CONSTEXPR_MATH == GLM_ENABLE
# include <gcem.hpp>
#endif
// END @MEWIN
namespace glm namespace glm
{ {
/// @addtogroup core_func_exponential /// @addtogroup core_func_exponential
@ -91,7 +97,9 @@ namespace glm
/// @see <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/sqrt.xml">GLSL sqrt man page</a> /// @see <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/sqrt.xml">GLSL sqrt man page</a>
/// @see <a href="http://www.opengl.org/registry/doc/GLSLangSpec.4.20.8.pdf">GLSL 4.20.8 specification, section 8.2 Exponential Functions</a> /// @see <a href="http://www.opengl.org/registry/doc/GLSLangSpec.4.20.8.pdf">GLSL 4.20.8 specification, section 8.2 Exponential Functions</a>
template<length_t L, typename T, qualifier Q> template<length_t L, typename T, qualifier Q>
GLM_FUNC_DECL vec<L, T, Q> sqrt(vec<L, T, Q> const& v); // BEGIN @MEWIN - 2023-01-12 - Made some math functions constexpr using gcem.
GLM_FUNC_DECL GLM_MATH_CONSTEXPR vec<L, T, Q> sqrt(vec<L, T, Q> const& v);
// END @MEWIN
/// Returns the reciprocal of the positive square root of v. /// Returns the reciprocal of the positive square root of v.
/// ///

View File

@ -27,7 +27,9 @@ namespace glm
/// @see <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/length.xml">GLSL length man page</a> /// @see <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/length.xml">GLSL length man page</a>
/// @see <a href="http://www.opengl.org/registry/doc/GLSLangSpec.4.20.8.pdf">GLSL 4.20.8 specification, section 8.5 Geometric Functions</a> /// @see <a href="http://www.opengl.org/registry/doc/GLSLangSpec.4.20.8.pdf">GLSL 4.20.8 specification, section 8.5 Geometric Functions</a>
template<length_t L, typename T, qualifier Q> template<length_t L, typename T, qualifier Q>
GLM_FUNC_DECL T length(vec<L, T, Q> const& x); // BEGIN @MEWIN - 2023-01-12 - Made some math functions constexpr using gcem.
GLM_FUNC_DECL GLM_MATH_CONSTEXPR T length(vec<L, T, Q> const& x);
// END @MEWIN
/// Returns the distance between p0 and p1, i.e., length(p0 - p1). /// Returns the distance between p0 and p1, i.e., length(p0 - p1).
/// ///
@ -37,7 +39,9 @@ namespace glm
/// @see <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/distance.xml">GLSL distance man page</a> /// @see <a href="http://www.opengl.org/sdk/docs/manglsl/xhtml/distance.xml">GLSL distance man page</a>
/// @see <a href="http://www.opengl.org/registry/doc/GLSLangSpec.4.20.8.pdf">GLSL 4.20.8 specification, section 8.5 Geometric Functions</a> /// @see <a href="http://www.opengl.org/registry/doc/GLSLangSpec.4.20.8.pdf">GLSL 4.20.8 specification, section 8.5 Geometric Functions</a>
template<length_t L, typename T, qualifier Q> template<length_t L, typename T, qualifier Q>
GLM_FUNC_DECL T distance(vec<L, T, Q> const& p0, vec<L, T, Q> const& p1); // BEGIN @MEWIN - 2023-01-12 - Made some math functions constexpr using gcem.
GLM_FUNC_DECL GLM_MATH_CONSTEXPR T distance(vec<L, T, Q> const& p0, vec<L, T, Q> const& p1);
// END @MEWIN
/// Returns the dot product of x and y, i.e., result = x * y. /// Returns the dot product of x and y, i.e., result = x * y.
/// ///

View File

@ -1,3 +1,4 @@
// @MEWIN - 2023-01-02 - Added support for spaceship operator.
#pragma once #pragma once