Explorar o código

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.
Ryan C. Gordon hai 1 día
pai
achega
a157d96de8
Modificáronse 1 ficheiros con 6 adicións e 0 borrados
  1. 6 0
      src/stdlib/SDL_random.c

+ 6 - 0
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
 }