Skip to content

[rtextures] HalfToFloat() and FloatToHalf() dereferencing issues #4728

Closed
@raysan5

Description

@raysan5

@Not-Nik @raysan5 Sorry for raising the PR again, but while compiling raylib with -02 (or -03) HalfToFloat and FloatToHalf will raise dereferencing type-punned pointer will break strict-aliasing rules, which, AFAIK, incurs in undefined behavior, so probably worth looking into. Not sure how to address these ones tho.
EDIT: -fno-strict-aliasing is set, not sure why it's not being applied.

Compiling log:
[...]
gcc -c rtextures.c -Wall -D_GNU_SOURCE -DPLATFORM_DESKTOP_GLFW -DGRAPHICS_API_OPENGL_33 -Wno-missing-braces -Werror=pointer-arith -fno-strict-aliasing -std=c99 -fPIC -O2 -Werror=implicit-function-declaration -D_GLFW_X11  -I.  -Iexternal/glfw/include
rtextures.c: In function ‘HalfToFloat’:
rtextures.c:5396:30: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
 5396 |     const unsigned int v = (*(unsigned int *)&fm) >> 23; // Evil log2 bit hack to count leading zeros in denormalized format
      |                              ^~~~~~~~~~~~~~~~~~~
rtextures.c:5399:15: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
 5399 |     result = *(float *)&r;
      |               ^~~~~~~~~~~
rtextures.c: In function ‘FloatToHalf’:
rtextures.c:5409:30: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
 5409 |     const unsigned int b = (*(unsigned int *) & x) + 0x00001000; // Round-to-nearest-even: add last bit after truncated mantissa
      |                              ^~~~~~~~~~~~~~~~~~~~
rtextures.c: In function ‘HalfToFloat’:
rtextures.c:5396:29: warning: ‘fm’ is used uninitialized [-Wuninitialized]
 5396 |     const unsigned int v = (*(unsigned int *)&fm) >> 23; // Evil log2 bit hack to count leading zeros in denormalized format
      |                            ~^~~~~~~~~~~~~~~~~~~~~
rtextures.c:5395:17: note: ‘fm’ declared here
 5395 |     const float fm = (float)m;
      |                 ^~
[...]

Originally posted by @asdqwe in #3220 (comment)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions