From a157d96de87fce84f06e60f4049dd96b8a6993e3 Mon Sep 17 00:00:00 2001 From: "Ryan C. Gordon" Date: Sat, 28 Mar 2026 11:18:03 -0400 Subject: [PATCH] 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. --- src/stdlib/SDL_random.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/stdlib/SDL_random.c b/src/stdlib/SDL_random.c index f1c6f99861..747d85413f 100644 --- a/src/stdlib/SDL_random.c +++ b/src/stdlib/SDL_random.c @@ -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 }