mirror of
https://github.com/libsdl-org/SDL.git
synced 2026-04-19 14:01:01 +00:00
stdlib: Patched SDL_rand_f to compile on pre-C99 compilers.
Visual Studio _still_ doesn't report itself as C99 compatible, afaict, but
does support the syntax as of VS2017 15.6, apparently.
This page mentions the first version of Visual Studio that handles hexidecimal
float notation:
https://stackoverflow.com/questions/18180116/vc-rejecting-hexadecimal-floating-point-constant?utm_source=chatgpt.com
If not Visual Studio, we also take the messier path for things that don't
report themselves as C99. Most things will take the cleaner path, though.
Closes #15276.
(cherry picked from commit a157d96de8)
This commit is contained in:
@@ -110,6 +110,12 @@ Sint32 SDL_rand_r(Uint64 *state, Sint32 n)
|
||||
float SDL_randf_r(Uint64 *state)
|
||||
{
|
||||
// Note: its using 24 bits because float has 23 bits significand + 1 implicit bit
|
||||
#if (defined(_MSC_VER) && (_MSC_VER < 1913)) || (!defined(_MSC_VER) && (!defined(__STDC_VERSION__) || (__STDC_VERSION__ < 199901L)))
|
||||
// no hexidecimal float notation, do it the hard way. MSVC before 15.6 (2017), etc, needs this.
|
||||
const union { Uint32 u32; float f; } float_union = { 0x33800000U };
|
||||
return (SDL_rand_bits_r(state) >> (32 - 24)) * float_union.f;
|
||||
#else
|
||||
return (SDL_rand_bits_r(state) >> (32 - 24)) * 0x1p-24f;
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user