Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
raysan5 opened this issue Jan 26, 2025 · 2 comments · Fixed by #4729
Closed

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

raysan5 opened this issue Jan 26, 2025 · 2 comments · Fixed by #4729

Comments

@raysan5
Copy link
Owner

raysan5 commented Jan 26, 2025

@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)

@raysan5
Copy link
Owner Author

raysan5 commented Jan 26, 2025

@asdqwe thanks for pointing this issue, definitely it should be reviewed.

@Not-Nik
Copy link
Contributor

Not-Nik commented Jan 26, 2025

Could possibly be fixed with unions which properly support type punning since C99

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants