From a8fd81850ffb6ddf4cf440336de7562501bf1eea Mon Sep 17 00:00:00 2001 From: Christophe Riccio Date: Sat, 8 Feb 2014 14:10:00 +0100 Subject: [PATCH] Tentative fix for strict aliasing warning in GCC 4.8.1 / Android NDK 9c (#152) --- glm/detail/func_exponential.inl | 7 ++--- glm/detail/func_packing.inl | 3 ++- glm/gtc/packing.inl | 47 ++++++++++++++++++++------------- readme.txt | 1 + 4 files changed, 35 insertions(+), 23 deletions(-) diff --git a/glm/detail/func_exponential.inl b/glm/detail/func_exponential.inl index c4dbd681..805ce163 100644 --- a/glm/detail/func_exponential.inl +++ b/glm/detail/func_exponential.inl @@ -67,9 +67,10 @@ namespace detail { vecType tmp(x); vecType xhalf(tmp * 0.5f); - vecType i = *reinterpret_cast*>(const_cast*>(&x)); - i = vecType(0x5f375a86) - (i >> vecType(1)); - tmp = *reinterpret_cast*>(&i); + vecType* p = reinterpret_cast*>(const_cast*>(&x)); + vecType i = vecType(0x5f375a86) - (*p >> vecType(1)); + vecType* ptmp = reinterpret_cast*>(&i); + tmp = *ptmp; tmp = tmp * (1.5f - xhalf * tmp * tmp); return tmp; } diff --git a/glm/detail/func_packing.inl b/glm/detail/func_packing.inl index cd446fed..e5eb0933 100644 --- a/glm/detail/func_packing.inl +++ b/glm/detail/func_packing.inl @@ -100,7 +100,8 @@ namespace glm detail::toFloat16(v.x), detail::toFloat16(v.y)); - return *reinterpret_cast(&Unpack); + uint * Result = reinterpret_cast(&Unpack); + return *Result; } GLM_FUNC_QUALIFIER vec2 unpackHalf2x16(uint const & v) diff --git a/glm/gtc/packing.inl b/glm/gtc/packing.inl index 0d05c37d..537e4648 100644 --- a/glm/gtc/packing.inl +++ b/glm/gtc/packing.inl @@ -231,19 +231,21 @@ namespace detail GLM_FUNC_QUALIFIER uint16 packUnorm2x8(vec2 const & v) { u8vec2 Topack(round(clamp(v, 0.0f, 1.0f) * 255.0f)); - return *reinterpret_cast(&Topack); + uint16* Packed = reinterpret_cast(&Topack); + return *Packed; } GLM_FUNC_QUALIFIER vec2 unpackUnorm2x8(uint16 const & p) { - vec2 Unpack(*reinterpret_cast(const_cast(&p))); - return Unpack * float(0.0039215686274509803921568627451); // 1 / 255 + u8vec2* Unpacked = reinterpret_cast(const_cast(&p)); + return vec2(*Unpacked) * float(0.0039215686274509803921568627451); // 1 / 255 } GLM_FUNC_QUALIFIER uint8 packSnorm1x8(float const & v) { int8 Topack(static_cast(round(clamp(v ,-1.0f, 1.0f) * 127.0f))); - return *reinterpret_cast(&Topack); + uint8* Packed = reinterpret_cast(&Topack); + return *Packed; } GLM_FUNC_QUALIFIER float unpackSnorm1x8(uint8 const & p) @@ -257,14 +259,15 @@ namespace detail GLM_FUNC_QUALIFIER uint16 packSnorm2x8(vec2 const & v) { i8vec2 Topack(round(clamp(v ,-1.0f, 1.0f) * 127.0f)); - return *reinterpret_cast(&Topack); + uint16* Packed = reinterpret_cast(&Topack); + return *Packed; } GLM_FUNC_QUALIFIER vec2 unpackSnorm2x8(uint16 const & p) { - vec2 Unpack(*reinterpret_cast(const_cast(&p))); + i8vec2* Unpack = reinterpret_cast(const_cast(&p)); return clamp( - Unpack * 0.00787401574803149606299212598425f, // 1.0f / 127.0f + vec2(*Unpack) * 0.00787401574803149606299212598425f, // 1.0f / 127.0f -1.0f, 1.0f); } @@ -282,19 +285,21 @@ namespace detail GLM_FUNC_QUALIFIER uint64 packUnorm4x16(vec4 const & v) { u16vec4 Topack(round(clamp(v , 0.0f, 1.0f) * 65535.0f)); - return *reinterpret_cast(&Topack); + uint64* Packed = reinterpret_cast(&Topack); + return *Packed; } GLM_FUNC_QUALIFIER vec4 unpackUnorm4x16(uint64 const & p) { - vec4 Unpack(*reinterpret_cast(const_cast(&p))); - return Unpack * 1.5259021896696421759365224689097e-5f; // 1.0 / 65535.0 + u16vec4* Unpack = reinterpret_cast(const_cast(&p)); + return vec4(*Unpack) * 1.5259021896696421759365224689097e-5f; // 1.0 / 65535.0 } GLM_FUNC_QUALIFIER uint16 packSnorm1x16(float const & v) { int16 Topack = static_cast(round(clamp(v ,-1.0f, 1.0f) * 32767.0f)); - return *reinterpret_cast(&Topack); + uint16* Packed = reinterpret_cast(&Topack); + return *Packed; } GLM_FUNC_QUALIFIER float unpackSnorm1x16(uint16 const & p) @@ -308,27 +313,29 @@ namespace detail GLM_FUNC_QUALIFIER uint64 packSnorm4x16(vec4 const & v) { i16vec4 Topack = static_cast(round(clamp(v ,-1.0f, 1.0f) * 32767.0f)); - return *reinterpret_cast(&Topack); + uint64* Packed = reinterpret_cast(&Topack); + return *Packed; } GLM_FUNC_QUALIFIER vec4 unpackSnorm4x16(uint64 const & p) { - vec4 Unpack(*reinterpret_cast(const_cast(&p))); + i16vec4* Unpack(reinterpret_cast(const_cast(&p))); return clamp( - Unpack * 3.0518509475997192297128208258309e-5f, //1.0f / 32767.0f, + vec4(*Unpack) * 3.0518509475997192297128208258309e-5f, //1.0f / 32767.0f, -1.0f, 1.0f); } GLM_FUNC_QUALIFIER uint16 packHalf1x16(float const & v) { int16 Topack = detail::toFloat16(v); - return *reinterpret_cast(&Topack); + uint16* Packed = reinterpret_cast(&Topack); + return *Packed; } GLM_FUNC_QUALIFIER float unpackHalf1x16(uint16 const & v) { - int16 Unpack(*reinterpret_cast(const_cast(&v))); - return detail::toFloat32(Unpack); + int16* Unpack = reinterpret_cast(const_cast(&v)); + return detail::toFloat32(*Unpack); } GLM_FUNC_QUALIFIER uint64 packHalf4x16(glm::vec4 const & v) @@ -339,12 +346,14 @@ namespace detail detail::toFloat16(v.z), detail::toFloat16(v.w)); - return *reinterpret_cast(&Unpack); + uint64* Packed = reinterpret_cast(&Unpack); + return *Packed; } GLM_FUNC_QUALIFIER glm::vec4 unpackHalf4x16(uint64 const & v) { - i16vec4 Unpack = *reinterpret_cast(const_cast(&v)); + i16vec4* p = reinterpret_cast(const_cast(&v)); + i16vec4 Unpack(*p); return vec4( detail::toFloat32(Unpack.x), diff --git a/readme.txt b/readme.txt index c641a025..f35cbfd5 100644 --- a/readme.txt +++ b/readme.txt @@ -47,6 +47,7 @@ GLM 0.9.5.2: 2014-02-08 - Fixed undefined reference to fastInverseSqrt (#161) - Fixed GLM_FORCE_RADIANS with build error (#165) - Fix dot product clamp range for vector angle functions. (#163) +- Tentative fix for strict aliasing warning in GCC 4.8.1 / Android NDK 9c (#152) ================================================================================ GLM 0.9.5.1: 2014-01-11