From fd4ada5843bd63a7c2836e25975d4b59ced1f43b Mon Sep 17 00:00:00 2001 From: Christophe Riccio Date: Thu, 2 Jun 2016 00:33:55 +0200 Subject: [PATCH] Support aligned *vec* even when SIMD isn't enabled --- glm/detail/func_integer_simd.inl | 2 +- glm/detail/type_vec.hpp | 62 +++++++++++++++++++++++++++++++- glm/detail/type_vec4.hpp | 60 ++----------------------------- test/core/core_type_vec4.cpp | 7 ---- test/gtc/gtc_type_aligned.cpp | 60 +++++++++++++++++++++++++++++++ 5 files changed, 124 insertions(+), 67 deletions(-) diff --git a/glm/detail/func_integer_simd.inl b/glm/detail/func_integer_simd.inl index 544a6220..99e0ebbc 100644 --- a/glm/detail/func_integer_simd.inl +++ b/glm/detail/func_integer_simd.inl @@ -57,7 +57,7 @@ namespace detail template <> GLM_FUNC_QUALIFIER int bitCount(uint64 x) { - return _mm_popcnt_u64(x); + return static_cast(_mm_popcnt_u64(x)); } # endif diff --git a/glm/detail/type_vec.hpp b/glm/detail/type_vec.hpp index 433ccb13..2fe33407 100644 --- a/glm/detail/type_vec.hpp +++ b/glm/detail/type_vec.hpp @@ -6,8 +6,68 @@ #include "precision.hpp" #include "type_int.hpp" -namespace glm +namespace glm{ +namespace detail { + template + struct simd_data + { + typedef struct type { + uint8 data[size]; + } type; + }; + + template + struct simd_data + { + typedef GLM_ALIGNED_STRUCT(size) type { + uint8 data[size]; + } type; + }; + +# if GLM_ARCH & GLM_ARCH_SSE2_BIT + template <> + struct simd_data + { + typedef glm_vec4 type; + }; + + template <> + struct simd_data + { + typedef glm_ivec4 type; + }; + + template <> + struct simd_data + { + typedef glm_uvec4 type; + }; +# endif + +# if (GLM_ARCH & GLM_ARCH_AVX_BIT) + template <> + struct simd_data + { + typedef glm_dvec4 type; + }; +# endif + +# if (GLM_ARCH & GLM_ARCH_AVX2_BIT) + template <> + struct simd_data + { + typedef glm_i64vec4 type; + }; + + template <> + struct simd_data + { + typedef glm_u64vec4 type; + }; +# endif +}//namespace detail + template struct tvec1; template struct tvec2; template struct tvec3; diff --git a/glm/detail/type_vec4.hpp b/glm/detail/type_vec4.hpp index d96b64b2..f93d6f73 100644 --- a/glm/detail/type_vec4.hpp +++ b/glm/detail/type_vec4.hpp @@ -14,64 +14,8 @@ #endif //GLM_SWIZZLE #include -namespace glm{ -namespace detail +namespace glm { - template - struct simd_data - { - typedef T type[4]; - }; -/* - template - GLM_ALIGNED_STRUCT(16) struct simd_data - { - typedef T type[4]; - }; -*/ -# if (GLM_ARCH & GLM_ARCH_SSE2_BIT) - template <> - struct simd_data - { - typedef glm_vec4 type; - }; - - template <> - struct simd_data - { - typedef glm_ivec4 type; - }; - - template <> - struct simd_data - { - typedef glm_uvec4 type; - }; -# endif - -# if (GLM_ARCH & GLM_ARCH_AVX_BIT) - template <> - struct simd_data - { - typedef glm_dvec4 type; - }; -# endif - -# if (GLM_ARCH & GLM_ARCH_AVX2_BIT) - template <> - struct simd_data - { - typedef glm_i64vec4 type; - }; - - template <> - struct simd_data - { - typedef glm_u64vec4 type; - }; -# endif -}//namespace detail - template struct tvec4 { @@ -90,7 +34,7 @@ namespace detail struct { T r, g, b, a; }; struct { T s, t, p, q; }; - typename detail::simd_data::value>::type data; + typename detail::simd_data::value>::type data; # ifdef GLM_SWIZZLE _GLM_SWIZZLE4_2_MEMBERS(T, P, glm::tvec2, x, y, z, w) diff --git a/test/core/core_type_vec4.cpp b/test/core/core_type_vec4.cpp index 77c4809a..f8599c61 100644 --- a/test/core/core_type_vec4.cpp +++ b/test/core/core_type_vec4.cpp @@ -347,13 +347,6 @@ int test_vec4_size() Error += glm::vec4().length() == 4 ? 0 : 1; Error += glm::dvec4().length() == 4 ? 0 : 1; - struct my_struct - { - glm::uint32 a; - glm::vec4 b; - }; - GLM_STATIC_ASSERT(sizeof(my_struct) == sizeof(glm::uint32) + sizeof(glm::vec4), "glm::vec4 alignment is not correct"); - return Error; } diff --git a/test/gtc/gtc_type_aligned.cpp b/test/gtc/gtc_type_aligned.cpp index 9df5b6b7..11bf53de 100644 --- a/test/gtc/gtc_type_aligned.cpp +++ b/test/gtc/gtc_type_aligned.cpp @@ -3,9 +3,69 @@ #define GLM_FORCE_ALIGNED #include +struct my_vec4_packed +{ + glm::uint32 a; + glm::vec4 b; +}; +GLM_STATIC_ASSERT(sizeof(my_vec4_packed) == sizeof(glm::uint32) + sizeof(glm::vec4), "glm::vec4 packed is not correct"); + +struct my_vec4_aligned +{ + glm::uint32 a; + glm::aligned_vec4 b; +}; +GLM_STATIC_ASSERT(sizeof(my_vec4_aligned) == sizeof(glm::aligned_vec4) * 2, "glm::vec4 aligned is not correct"); + +struct my_dvec4_packed +{ + glm::uint64 a; + glm::dvec4 b; +}; +GLM_STATIC_ASSERT(sizeof(my_dvec4_packed) == sizeof(glm::uint64) + sizeof(glm::dvec4), "glm::dvec4 packed is not correct"); + +struct my_dvec4_aligned +{ + glm::uint64 a; + glm::aligned_dvec4 b; +}; +GLM_STATIC_ASSERT(sizeof(my_dvec4_aligned) == sizeof(glm::aligned_dvec4) * 2, "glm::dvec4 aligned is not correct"); + +struct my_ivec4_packed +{ + glm::uint32 a; + glm::ivec4 b; +}; +GLM_STATIC_ASSERT(sizeof(my_ivec4_packed) == sizeof(glm::uint32) + sizeof(glm::ivec4), "glm::ivec4 packed is not correct"); + +struct my_ivec4_aligned +{ + glm::uint32 a; + glm::aligned_ivec4 b; +}; +GLM_STATIC_ASSERT(sizeof(my_ivec4_aligned) == sizeof(glm::aligned_ivec4) * 2, "glm::ivec4 aligned is not correct"); + +struct my_u8vec4_packed +{ + glm::uint32 a; + glm::u8vec4 b; +}; +GLM_STATIC_ASSERT(sizeof(my_u8vec4_packed) == sizeof(glm::uint32) + sizeof(glm::u8vec4), "glm::u8vec4 packed is not correct"); + int main() { int Error = 0; + my_vec4_aligned GNA; + my_dvec4_aligned GNI; + + std::size_t A0 = sizeof(my_dvec4_aligned); + std::size_t B0 = sizeof(my_dvec4_packed); + std::size_t C0 = sizeof(glm::aligned_dvec4); + + std::size_t A1 = sizeof(my_vec4_aligned); + std::size_t B1 = sizeof(my_vec4_packed); + std::size_t C1 = sizeof(glm::aligned_vec4); + return Error; }