Tentative fix for strict aliasing warning in GCC 4.8.1 / Android NDK 9c (#152)

This commit is contained in:
Christophe Riccio 2014-02-08 14:10:00 +01:00
parent 577ee319e6
commit a8fd81850f
4 changed files with 35 additions and 23 deletions

View File

@ -67,9 +67,10 @@ namespace detail
{ {
vecType<float, lowp> tmp(x); vecType<float, lowp> tmp(x);
vecType<float, lowp> xhalf(tmp * 0.5f); vecType<float, lowp> xhalf(tmp * 0.5f);
vecType<uint, lowp> i = *reinterpret_cast<vecType<uint, lowp>*>(const_cast<vecType<float, lowp>*>(&x)); vecType<uint, lowp>* p = reinterpret_cast<vecType<uint, lowp>*>(const_cast<vecType<float, lowp>*>(&x));
i = vecType<uint, lowp>(0x5f375a86) - (i >> vecType<uint, lowp>(1)); vecType<uint, lowp> i = vecType<uint, lowp>(0x5f375a86) - (*p >> vecType<uint, lowp>(1));
tmp = *reinterpret_cast<vecType<float, lowp>*>(&i); vecType<float, lowp>* ptmp = reinterpret_cast<vecType<float, lowp>*>(&i);
tmp = *ptmp;
tmp = tmp * (1.5f - xhalf * tmp * tmp); tmp = tmp * (1.5f - xhalf * tmp * tmp);
return tmp; return tmp;
} }

View File

@ -100,7 +100,8 @@ namespace glm
detail::toFloat16(v.x), detail::toFloat16(v.x),
detail::toFloat16(v.y)); detail::toFloat16(v.y));
return *reinterpret_cast<uint*>(&Unpack); uint * Result = reinterpret_cast<uint*>(&Unpack);
return *Result;
} }
GLM_FUNC_QUALIFIER vec2 unpackHalf2x16(uint const & v) GLM_FUNC_QUALIFIER vec2 unpackHalf2x16(uint const & v)

View File

@ -231,19 +231,21 @@ namespace detail
GLM_FUNC_QUALIFIER uint16 packUnorm2x8(vec2 const & v) GLM_FUNC_QUALIFIER uint16 packUnorm2x8(vec2 const & v)
{ {
u8vec2 Topack(round(clamp(v, 0.0f, 1.0f) * 255.0f)); u8vec2 Topack(round(clamp(v, 0.0f, 1.0f) * 255.0f));
return *reinterpret_cast<uint16*>(&Topack); uint16* Packed = reinterpret_cast<uint16*>(&Topack);
return *Packed;
} }
GLM_FUNC_QUALIFIER vec2 unpackUnorm2x8(uint16 const & p) GLM_FUNC_QUALIFIER vec2 unpackUnorm2x8(uint16 const & p)
{ {
vec2 Unpack(*reinterpret_cast<u8vec2*>(const_cast<uint16*>(&p))); u8vec2* Unpacked = reinterpret_cast<u8vec2*>(const_cast<uint16*>(&p));
return Unpack * float(0.0039215686274509803921568627451); // 1 / 255 return vec2(*Unpacked) * float(0.0039215686274509803921568627451); // 1 / 255
} }
GLM_FUNC_QUALIFIER uint8 packSnorm1x8(float const & v) GLM_FUNC_QUALIFIER uint8 packSnorm1x8(float const & v)
{ {
int8 Topack(static_cast<int8>(round(clamp(v ,-1.0f, 1.0f) * 127.0f))); int8 Topack(static_cast<int8>(round(clamp(v ,-1.0f, 1.0f) * 127.0f)));
return *reinterpret_cast<uint8*>(&Topack); uint8* Packed = reinterpret_cast<uint8*>(&Topack);
return *Packed;
} }
GLM_FUNC_QUALIFIER float unpackSnorm1x8(uint8 const & p) GLM_FUNC_QUALIFIER float unpackSnorm1x8(uint8 const & p)
@ -257,14 +259,15 @@ namespace detail
GLM_FUNC_QUALIFIER uint16 packSnorm2x8(vec2 const & v) GLM_FUNC_QUALIFIER uint16 packSnorm2x8(vec2 const & v)
{ {
i8vec2 Topack(round(clamp(v ,-1.0f, 1.0f) * 127.0f)); i8vec2 Topack(round(clamp(v ,-1.0f, 1.0f) * 127.0f));
return *reinterpret_cast<uint16*>(&Topack); uint16* Packed = reinterpret_cast<uint16*>(&Topack);
return *Packed;
} }
GLM_FUNC_QUALIFIER vec2 unpackSnorm2x8(uint16 const & p) GLM_FUNC_QUALIFIER vec2 unpackSnorm2x8(uint16 const & p)
{ {
vec2 Unpack(*reinterpret_cast<i8vec2*>(const_cast<uint16*>(&p))); i8vec2* Unpack = reinterpret_cast<i8vec2*>(const_cast<uint16*>(&p));
return clamp( return clamp(
Unpack * 0.00787401574803149606299212598425f, // 1.0f / 127.0f vec2(*Unpack) * 0.00787401574803149606299212598425f, // 1.0f / 127.0f
-1.0f, 1.0f); -1.0f, 1.0f);
} }
@ -282,19 +285,21 @@ namespace detail
GLM_FUNC_QUALIFIER uint64 packUnorm4x16(vec4 const & v) GLM_FUNC_QUALIFIER uint64 packUnorm4x16(vec4 const & v)
{ {
u16vec4 Topack(round(clamp(v , 0.0f, 1.0f) * 65535.0f)); u16vec4 Topack(round(clamp(v , 0.0f, 1.0f) * 65535.0f));
return *reinterpret_cast<uint64*>(&Topack); uint64* Packed = reinterpret_cast<uint64*>(&Topack);
return *Packed;
} }
GLM_FUNC_QUALIFIER vec4 unpackUnorm4x16(uint64 const & p) GLM_FUNC_QUALIFIER vec4 unpackUnorm4x16(uint64 const & p)
{ {
vec4 Unpack(*reinterpret_cast<u16vec4*>(const_cast<uint64*>(&p))); u16vec4* Unpack = reinterpret_cast<u16vec4*>(const_cast<uint64*>(&p));
return Unpack * 1.5259021896696421759365224689097e-5f; // 1.0 / 65535.0 return vec4(*Unpack) * 1.5259021896696421759365224689097e-5f; // 1.0 / 65535.0
} }
GLM_FUNC_QUALIFIER uint16 packSnorm1x16(float const & v) GLM_FUNC_QUALIFIER uint16 packSnorm1x16(float const & v)
{ {
int16 Topack = static_cast<int16>(round(clamp(v ,-1.0f, 1.0f) * 32767.0f)); int16 Topack = static_cast<int16>(round(clamp(v ,-1.0f, 1.0f) * 32767.0f));
return *reinterpret_cast<uint16*>(&Topack); uint16* Packed = reinterpret_cast<uint16*>(&Topack);
return *Packed;
} }
GLM_FUNC_QUALIFIER float unpackSnorm1x16(uint16 const & p) GLM_FUNC_QUALIFIER float unpackSnorm1x16(uint16 const & p)
@ -308,27 +313,29 @@ namespace detail
GLM_FUNC_QUALIFIER uint64 packSnorm4x16(vec4 const & v) GLM_FUNC_QUALIFIER uint64 packSnorm4x16(vec4 const & v)
{ {
i16vec4 Topack = static_cast<i16vec4>(round(clamp(v ,-1.0f, 1.0f) * 32767.0f)); i16vec4 Topack = static_cast<i16vec4>(round(clamp(v ,-1.0f, 1.0f) * 32767.0f));
return *reinterpret_cast<uint64*>(&Topack); uint64* Packed = reinterpret_cast<uint64*>(&Topack);
return *Packed;
} }
GLM_FUNC_QUALIFIER vec4 unpackSnorm4x16(uint64 const & p) GLM_FUNC_QUALIFIER vec4 unpackSnorm4x16(uint64 const & p)
{ {
vec4 Unpack(*reinterpret_cast<i16vec4*>(const_cast<uint64*>(&p))); i16vec4* Unpack(reinterpret_cast<i16vec4*>(const_cast<uint64*>(&p)));
return clamp( return clamp(
Unpack * 3.0518509475997192297128208258309e-5f, //1.0f / 32767.0f, vec4(*Unpack) * 3.0518509475997192297128208258309e-5f, //1.0f / 32767.0f,
-1.0f, 1.0f); -1.0f, 1.0f);
} }
GLM_FUNC_QUALIFIER uint16 packHalf1x16(float const & v) GLM_FUNC_QUALIFIER uint16 packHalf1x16(float const & v)
{ {
int16 Topack = detail::toFloat16(v); int16 Topack = detail::toFloat16(v);
return *reinterpret_cast<uint16*>(&Topack); uint16* Packed = reinterpret_cast<uint16*>(&Topack);
return *Packed;
} }
GLM_FUNC_QUALIFIER float unpackHalf1x16(uint16 const & v) GLM_FUNC_QUALIFIER float unpackHalf1x16(uint16 const & v)
{ {
int16 Unpack(*reinterpret_cast<int16*>(const_cast<uint16*>(&v))); int16* Unpack = reinterpret_cast<int16*>(const_cast<uint16*>(&v));
return detail::toFloat32(Unpack); return detail::toFloat32(*Unpack);
} }
GLM_FUNC_QUALIFIER uint64 packHalf4x16(glm::vec4 const & v) GLM_FUNC_QUALIFIER uint64 packHalf4x16(glm::vec4 const & v)
@ -339,12 +346,14 @@ namespace detail
detail::toFloat16(v.z), detail::toFloat16(v.z),
detail::toFloat16(v.w)); detail::toFloat16(v.w));
return *reinterpret_cast<uint64*>(&Unpack); uint64* Packed = reinterpret_cast<uint64*>(&Unpack);
return *Packed;
} }
GLM_FUNC_QUALIFIER glm::vec4 unpackHalf4x16(uint64 const & v) GLM_FUNC_QUALIFIER glm::vec4 unpackHalf4x16(uint64 const & v)
{ {
i16vec4 Unpack = *reinterpret_cast<i16vec4*>(const_cast<uint64*>(&v)); i16vec4* p = reinterpret_cast<i16vec4*>(const_cast<uint64*>(&v));
i16vec4 Unpack(*p);
return vec4( return vec4(
detail::toFloat32(Unpack.x), detail::toFloat32(Unpack.x),

View File

@ -47,6 +47,7 @@ GLM 0.9.5.2: 2014-02-08
- Fixed undefined reference to fastInverseSqrt (#161) - Fixed undefined reference to fastInverseSqrt (#161)
- Fixed GLM_FORCE_RADIANS with <glm/ext.hpp> build error (#165) - Fixed GLM_FORCE_RADIANS with <glm/ext.hpp> build error (#165)
- Fix dot product clamp range for vector angle functions. (#163) - 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 GLM 0.9.5.1: 2014-01-11