From f6230f88697f63355d00f1453140ede5e8a7aa22 Mon Sep 17 00:00:00 2001 From: Tr1NgleDev Date: Tue, 16 Jul 2024 03:58:48 +0800 Subject: [PATCH 1/4] optimize vector component access --- glm/detail/type_vec2.inl | 18 ++---------------- glm/detail/type_vec3.inl | 22 ++-------------------- glm/detail/type_vec4.inl | 26 ++------------------------ 3 files changed, 6 insertions(+), 60 deletions(-) diff --git a/glm/detail/type_vec2.inl b/glm/detail/type_vec2.inl index e8408997f2..af3c68aa16 100644 --- a/glm/detail/type_vec2.inl +++ b/glm/detail/type_vec2.inl @@ -106,28 +106,14 @@ namespace glm GLM_FUNC_QUALIFIER GLM_CONSTEXPR T & vec<2, T, Q>::operator[](typename vec<2, T, Q>::length_type i) { GLM_ASSERT_LENGTH(i, this->length()); - switch(i) - { - default: - case 0: - return x; - case 1: - return y; - } + return (&x)[i]; } template GLM_FUNC_QUALIFIER GLM_CONSTEXPR T const& vec<2, T, Q>::operator[](typename vec<2, T, Q>::length_type i) const { GLM_ASSERT_LENGTH(i, this->length()); - switch(i) - { - default: - case 0: - return x; - case 1: - return y; - } + return (&x)[i]; } // -- Unary arithmetic operators -- diff --git a/glm/detail/type_vec3.inl b/glm/detail/type_vec3.inl index fed82bf618..ec4151649d 100644 --- a/glm/detail/type_vec3.inl +++ b/glm/detail/type_vec3.inl @@ -171,32 +171,14 @@ namespace glm GLM_FUNC_QUALIFIER GLM_CONSTEXPR T & vec<3, T, Q>::operator[](typename vec<3, T, Q>::length_type i) { GLM_ASSERT_LENGTH(i, this->length()); - switch(i) - { - default: - case 0: - return x; - case 1: - return y; - case 2: - return z; - } + return (&x)[i]; } template GLM_FUNC_QUALIFIER GLM_CONSTEXPR T const& vec<3, T, Q>::operator[](typename vec<3, T, Q>::length_type i) const { GLM_ASSERT_LENGTH(i, this->length()); - switch(i) - { - default: - case 0: - return x; - case 1: - return y; - case 2: - return z; - } + return (&x)[i]; } // -- Unary arithmetic operators -- diff --git a/glm/detail/type_vec4.inl b/glm/detail/type_vec4.inl index 66539e3049..78a9ce6d62 100644 --- a/glm/detail/type_vec4.inl +++ b/glm/detail/type_vec4.inl @@ -362,36 +362,14 @@ namespace detail GLM_FUNC_QUALIFIER GLM_CONSTEXPR T& vec<4, T, Q>::operator[](typename vec<4, T, Q>::length_type i) { GLM_ASSERT_LENGTH(i, this->length()); - switch (i) - { - default: - case 0: - return x; - case 1: - return y; - case 2: - return z; - case 3: - return w; - } + return (&x)[i]; } template GLM_FUNC_QUALIFIER GLM_CONSTEXPR T const& vec<4, T, Q>::operator[](typename vec<4, T, Q>::length_type i) const { GLM_ASSERT_LENGTH(i, this->length()); - switch (i) - { - default: - case 0: - return x; - case 1: - return y; - case 2: - return z; - case 3: - return w; - } + return (&x)[i]; } // -- Unary arithmetic operators -- From 642057b32d546ef90e02495f8595d702494d25da Mon Sep 17 00:00:00 2001 From: Tr1NgleDev Date: Tue, 16 Jul 2024 03:59:21 +0800 Subject: [PATCH 2/4] use GLM_ASSERT_LENGTH in dual_quaternion component access --- glm/gtx/dual_quaternion.inl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/glm/gtx/dual_quaternion.inl b/glm/gtx/dual_quaternion.inl index 3a04160e3a..a5017fb0da 100644 --- a/glm/gtx/dual_quaternion.inl +++ b/glm/gtx/dual_quaternion.inl @@ -10,14 +10,14 @@ namespace glm template GLM_FUNC_QUALIFIER typename tdualquat::part_type & tdualquat::operator[](typename tdualquat::length_type i) { - assert(i >= 0 && i < this->length()); + GLM_ASSERT_LENGTH(i, this->length()); return (&real)[i]; } template GLM_FUNC_QUALIFIER typename tdualquat::part_type const& tdualquat::operator[](typename tdualquat::length_type i) const { - assert(i >= 0 && i < this->length()); + GLM_ASSERT_LENGTH(i, this->length()); return (&real)[i]; } From ce1448a903f3065ab5fb2683fbbb757af3c0dff1 Mon Sep 17 00:00:00 2001 From: Tr1NgleDev <77019520+Tr1NgleDev@users.noreply.github.com> Date: Fri, 1 Nov 2024 20:36:36 +0800 Subject: [PATCH 3/4] fix constexpr ig --- glm/detail/type_vec2.inl | 46 ++++++++++++++++++++++++++++++++++ glm/detail/type_vec3.inl | 49 ++++++++++++++++++++++++++++++++++++ glm/detail/type_vec4.inl | 54 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 149 insertions(+) diff --git a/glm/detail/type_vec2.inl b/glm/detail/type_vec2.inl index af3c68aa16..47709a03d7 100644 --- a/glm/detail/type_vec2.inl +++ b/glm/detail/type_vec2.inl @@ -106,14 +106,60 @@ namespace glm GLM_FUNC_QUALIFIER GLM_CONSTEXPR T & vec<2, T, Q>::operator[](typename vec<2, T, Q>::length_type i) { GLM_ASSERT_LENGTH(i, this->length()); + +# if GLM_LANG & GLM_LANG_CXX14_FLAG || GLM_LANG & GLM_LANG_CXX17_FLAG +# if GLM_LANG & GLM_LANG_CXX20_FLAG + if (std::is_constant_evaluated()) + { +# endif + switch (i) + { + default: + case 0: + return x; + case 1: + return y; + } +# if GLM_LANG & GLM_LANG_CXX20_FLAG + } + else + { + return (&x)[i]; + } +# endif +# else return (&x)[i]; +# endif } template GLM_FUNC_QUALIFIER GLM_CONSTEXPR T const& vec<2, T, Q>::operator[](typename vec<2, T, Q>::length_type i) const { GLM_ASSERT_LENGTH(i, this->length()); + +# if GLM_LANG & GLM_LANG_CXX14_FLAG || GLM_LANG & GLM_LANG_CXX17_FLAG +# if GLM_LANG & GLM_LANG_CXX20_FLAG + if (std::is_constant_evaluated()) + { +# endif + switch (i) + { + default: + case 0: + return x; + case 1: + return y; + } +# if GLM_LANG & GLM_LANG_CXX20_FLAG + } + else + { + return (&x)[i]; + } +# endif +# else return (&x)[i]; +# endif } // -- Unary arithmetic operators -- diff --git a/glm/detail/type_vec3.inl b/glm/detail/type_vec3.inl index ec4151649d..531cd61b51 100644 --- a/glm/detail/type_vec3.inl +++ b/glm/detail/type_vec3.inl @@ -171,14 +171,63 @@ namespace glm GLM_FUNC_QUALIFIER GLM_CONSTEXPR T & vec<3, T, Q>::operator[](typename vec<3, T, Q>::length_type i) { GLM_ASSERT_LENGTH(i, this->length()); +# if GLM_LANG & GLM_LANG_CXX14_FLAG || GLM_LANG & GLM_LANG_CXX17_FLAG +# if GLM_LANG & GLM_LANG_CXX20_FLAG + if (std::is_constant_evaluated()) + { +# endif + switch (i) + { + default: + case 0: + return x; + case 1: + return y; + case 2: + return z; + } +# if GLM_LANG & GLM_LANG_CXX20_FLAG + } + else + { + return (&x)[i]; + } +# endif +# else return (&x)[i]; +# endif } template GLM_FUNC_QUALIFIER GLM_CONSTEXPR T const& vec<3, T, Q>::operator[](typename vec<3, T, Q>::length_type i) const { GLM_ASSERT_LENGTH(i, this->length()); + +# if GLM_LANG & GLM_LANG_CXX14_FLAG || GLM_LANG & GLM_LANG_CXX17_FLAG +# if GLM_LANG & GLM_LANG_CXX20_FLAG + if (std::is_constant_evaluated()) + { +# endif + switch (i) + { + default: + case 0: + return x; + case 1: + return y; + case 2: + return z; + } +# if GLM_LANG & GLM_LANG_CXX20_FLAG + } + else + { + return (&x)[i]; + } +# endif +# else return (&x)[i]; +# endif } // -- Unary arithmetic operators -- diff --git a/glm/detail/type_vec4.inl b/glm/detail/type_vec4.inl index 78a9ce6d62..44f827b9cc 100644 --- a/glm/detail/type_vec4.inl +++ b/glm/detail/type_vec4.inl @@ -362,14 +362,68 @@ namespace detail GLM_FUNC_QUALIFIER GLM_CONSTEXPR T& vec<4, T, Q>::operator[](typename vec<4, T, Q>::length_type i) { GLM_ASSERT_LENGTH(i, this->length()); + +# if GLM_LANG & GLM_LANG_CXX14_FLAG || GLM_LANG & GLM_LANG_CXX17_FLAG +# if GLM_LANG & GLM_LANG_CXX20_FLAG + if (std::is_constant_evaluated()) + { +# endif + switch (i) + { + default: + case 0: + return x; + case 1: + return y; + case 2: + return z; + case 3: + return w; + } +# if GLM_LANG & GLM_LANG_CXX20_FLAG + } + else + { + return (&x)[i]; + } +# endif +# else return (&x)[i]; +# endif } template GLM_FUNC_QUALIFIER GLM_CONSTEXPR T const& vec<4, T, Q>::operator[](typename vec<4, T, Q>::length_type i) const { GLM_ASSERT_LENGTH(i, this->length()); + +# if GLM_LANG & GLM_LANG_CXX14_FLAG || GLM_LANG & GLM_LANG_CXX17_FLAG +# if GLM_LANG & GLM_LANG_CXX20_FLAG + if (std::is_constant_evaluated()) + { +# endif + switch (i) + { + default: + case 0: + return x; + case 1: + return y; + case 2: + return z; + case 3: + return w; + } +# if GLM_LANG & GLM_LANG_CXX20_FLAG + } + else + { + return (&x)[i]; + } +# endif +# else return (&x)[i]; +# endif } // -- Unary arithmetic operators -- From 9097b1469be846d5caf864367e82420d259a9dc1 Mon Sep 17 00:00:00 2001 From: Tr1NgleDev <77019520+Tr1NgleDev@users.noreply.github.com> Date: Sat, 2 Nov 2024 17:38:34 +0800 Subject: [PATCH 4/4] make a bit more readable --- glm/detail/type_vec2.inl | 28 ++++++++++++++-------------- glm/detail/type_vec3.inl | 29 +++++++++++++++-------------- glm/detail/type_vec4.inl | 28 ++++++++++++++-------------- 3 files changed, 43 insertions(+), 42 deletions(-) diff --git a/glm/detail/type_vec2.inl b/glm/detail/type_vec2.inl index 47709a03d7..6444f0dfb7 100644 --- a/glm/detail/type_vec2.inl +++ b/glm/detail/type_vec2.inl @@ -107,11 +107,11 @@ namespace glm { GLM_ASSERT_LENGTH(i, this->length()); -# if GLM_LANG & GLM_LANG_CXX14_FLAG || GLM_LANG & GLM_LANG_CXX17_FLAG -# if GLM_LANG & GLM_LANG_CXX20_FLAG +#if GLM_LANG & GLM_LANG_CXX14_FLAG || GLM_LANG & GLM_LANG_CXX17_FLAG + #if GLM_LANG & GLM_LANG_CXX20_FLAG if (std::is_constant_evaluated()) { -# endif + #endif switch (i) { default: @@ -120,16 +120,16 @@ namespace glm case 1: return y; } -# if GLM_LANG & GLM_LANG_CXX20_FLAG + #if GLM_LANG & GLM_LANG_CXX20_FLAG } else { return (&x)[i]; } -# endif -# else + #endif +#else return (&x)[i]; -# endif +#endif } template @@ -137,11 +137,11 @@ namespace glm { GLM_ASSERT_LENGTH(i, this->length()); -# if GLM_LANG & GLM_LANG_CXX14_FLAG || GLM_LANG & GLM_LANG_CXX17_FLAG -# if GLM_LANG & GLM_LANG_CXX20_FLAG +#if GLM_LANG & GLM_LANG_CXX14_FLAG || GLM_LANG & GLM_LANG_CXX17_FLAG + #if GLM_LANG & GLM_LANG_CXX20_FLAG if (std::is_constant_evaluated()) { -# endif + #endif switch (i) { default: @@ -150,16 +150,16 @@ namespace glm case 1: return y; } -# if GLM_LANG & GLM_LANG_CXX20_FLAG + #if GLM_LANG & GLM_LANG_CXX20_FLAG } else { return (&x)[i]; } -# endif -# else + #endif +#else return (&x)[i]; -# endif +#endif } // -- Unary arithmetic operators -- diff --git a/glm/detail/type_vec3.inl b/glm/detail/type_vec3.inl index 531cd61b51..d5b3cf289f 100644 --- a/glm/detail/type_vec3.inl +++ b/glm/detail/type_vec3.inl @@ -171,11 +171,12 @@ namespace glm GLM_FUNC_QUALIFIER GLM_CONSTEXPR T & vec<3, T, Q>::operator[](typename vec<3, T, Q>::length_type i) { GLM_ASSERT_LENGTH(i, this->length()); -# if GLM_LANG & GLM_LANG_CXX14_FLAG || GLM_LANG & GLM_LANG_CXX17_FLAG -# if GLM_LANG & GLM_LANG_CXX20_FLAG + +#if GLM_LANG & GLM_LANG_CXX14_FLAG || GLM_LANG & GLM_LANG_CXX17_FLAG + #if GLM_LANG & GLM_LANG_CXX20_FLAG if (std::is_constant_evaluated()) { -# endif + #endif switch (i) { default: @@ -186,16 +187,16 @@ namespace glm case 2: return z; } -# if GLM_LANG & GLM_LANG_CXX20_FLAG + #if GLM_LANG & GLM_LANG_CXX20_FLAG } else { return (&x)[i]; } -# endif -# else + #endif +#else return (&x)[i]; -# endif +#endif } template @@ -203,11 +204,11 @@ namespace glm { GLM_ASSERT_LENGTH(i, this->length()); -# if GLM_LANG & GLM_LANG_CXX14_FLAG || GLM_LANG & GLM_LANG_CXX17_FLAG -# if GLM_LANG & GLM_LANG_CXX20_FLAG +#if GLM_LANG & GLM_LANG_CXX14_FLAG || GLM_LANG & GLM_LANG_CXX17_FLAG + #if GLM_LANG & GLM_LANG_CXX20_FLAG if (std::is_constant_evaluated()) { -# endif + #endif switch (i) { default: @@ -218,16 +219,16 @@ namespace glm case 2: return z; } -# if GLM_LANG & GLM_LANG_CXX20_FLAG + #if GLM_LANG & GLM_LANG_CXX20_FLAG } else { return (&x)[i]; } -# endif -# else + #endif +#else return (&x)[i]; -# endif +#endif } // -- Unary arithmetic operators -- diff --git a/glm/detail/type_vec4.inl b/glm/detail/type_vec4.inl index 44f827b9cc..f83cfca2d9 100644 --- a/glm/detail/type_vec4.inl +++ b/glm/detail/type_vec4.inl @@ -363,11 +363,11 @@ namespace detail { GLM_ASSERT_LENGTH(i, this->length()); -# if GLM_LANG & GLM_LANG_CXX14_FLAG || GLM_LANG & GLM_LANG_CXX17_FLAG -# if GLM_LANG & GLM_LANG_CXX20_FLAG +#if GLM_LANG & GLM_LANG_CXX14_FLAG || GLM_LANG & GLM_LANG_CXX17_FLAG + #if GLM_LANG & GLM_LANG_CXX20_FLAG if (std::is_constant_evaluated()) { -# endif + #endif switch (i) { default: @@ -380,16 +380,16 @@ namespace detail case 3: return w; } -# if GLM_LANG & GLM_LANG_CXX20_FLAG + #if GLM_LANG & GLM_LANG_CXX20_FLAG } else { return (&x)[i]; } -# endif -# else + #endif +#else return (&x)[i]; -# endif +#endif } template @@ -397,11 +397,11 @@ namespace detail { GLM_ASSERT_LENGTH(i, this->length()); -# if GLM_LANG & GLM_LANG_CXX14_FLAG || GLM_LANG & GLM_LANG_CXX17_FLAG -# if GLM_LANG & GLM_LANG_CXX20_FLAG +#if GLM_LANG & GLM_LANG_CXX14_FLAG || GLM_LANG & GLM_LANG_CXX17_FLAG + #if GLM_LANG & GLM_LANG_CXX20_FLAG if (std::is_constant_evaluated()) { -# endif + #endif switch (i) { default: @@ -414,16 +414,16 @@ namespace detail case 3: return w; } -# if GLM_LANG & GLM_LANG_CXX20_FLAG + #if GLM_LANG & GLM_LANG_CXX20_FLAG } else { return (&x)[i]; } -# endif -# else + #endif +#else return (&x)[i]; -# endif +#endif } // -- Unary arithmetic operators --