cogl/bitmap-conversion: Don't break strict-aliasing for flt_pack/unpack

Simply reinterpreting the bytes differently is a strict-aliasing
violation if the type of the object isn't char or the target type of the
reinterpretation. None of that is the case here, so we have to resort to
a memcpy.

Fixes: 60c082caa ("cogl/bitmap-conversion: Support packing fp16 formats")
Part-of: <https://gitlab.gnome.org/GNOME/mutter/-/merge_requests/3499>
This commit is contained in:
Sebastian Wick 2024-01-08 16:08:37 +01:00 committed by Marge Bot
parent 0cd85e4adf
commit a0a1d102a0

View File

@ -45,16 +45,22 @@ typedef enum
MEDIUM_TYPE_FLOAT, MEDIUM_TYPE_FLOAT,
} MediumType; } MediumType;
G_STATIC_ASSERT (sizeof (uint32_t) == sizeof (GLfloat));
inline static uint32_t inline static uint32_t
pack_flt (GLfloat b) pack_flt (GLfloat b)
{ {
return *(uint32_t *) &b; uint32_t ret;
memcpy (&ret, &b, sizeof (uint32_t));
return ret;
} }
inline static GLfloat inline static GLfloat
unpack_flt (uint32_t b) unpack_flt (uint32_t b)
{ {
return *(GLfloat *) &b; GLfloat ret;
memcpy (&ret, &b, sizeof (GLfloat));
return ret;
} }
#define CLAMP_NORM(b) (MAX (MIN ((b), 1.0), 0.0)) #define CLAMP_NORM(b) (MAX (MIN ((b), 1.0), 0.0))