mirror of
https://github.com/libsdl-org/SDL.git
synced 2026-04-25 00:35:44 +00:00
Simplified SDL random function names and added thread-safe versions
This commit is contained in:
@@ -961,9 +961,11 @@ SDL3_0.0.0 {
|
||||
SDL_powf;
|
||||
SDL_qsort;
|
||||
SDL_qsort_r;
|
||||
SDL_rand_bits;
|
||||
SDL_rand_float;
|
||||
SDL_rand_n;
|
||||
SDL_rand;
|
||||
SDL_rand_bits_r;
|
||||
SDL_rand_r;
|
||||
SDL_randf;
|
||||
SDL_randf_r;
|
||||
SDL_realloc;
|
||||
SDL_round;
|
||||
SDL_roundf;
|
||||
|
||||
@@ -986,9 +986,11 @@
|
||||
#define SDL_powf SDL_powf_REAL
|
||||
#define SDL_qsort SDL_qsort_REAL
|
||||
#define SDL_qsort_r SDL_qsort_r_REAL
|
||||
#define SDL_rand_bits SDL_rand_bits_REAL
|
||||
#define SDL_rand_float SDL_rand_float_REAL
|
||||
#define SDL_rand_n SDL_rand_n_REAL
|
||||
#define SDL_rand SDL_rand_REAL
|
||||
#define SDL_rand_bits_r SDL_rand_bits_r_REAL
|
||||
#define SDL_rand_r SDL_rand_r_REAL
|
||||
#define SDL_randf SDL_randf_REAL
|
||||
#define SDL_randf_r SDL_randf_r_REAL
|
||||
#define SDL_realloc SDL_realloc_REAL
|
||||
#define SDL_round SDL_round_REAL
|
||||
#define SDL_roundf SDL_roundf_REAL
|
||||
|
||||
@@ -995,9 +995,11 @@ SDL_DYNAPI_PROC(double,SDL_pow,(double a, double b),(a,b),return)
|
||||
SDL_DYNAPI_PROC(float,SDL_powf,(float a, float b),(a,b),return)
|
||||
SDL_DYNAPI_PROC(void,SDL_qsort,(void *a, size_t b, size_t c, SDL_CompareCallback d),(a,b,c,d),)
|
||||
SDL_DYNAPI_PROC(void,SDL_qsort_r,(void *a, size_t b, size_t c, SDL_CompareCallback_r d, void *e),(a,b,c,d,e),)
|
||||
SDL_DYNAPI_PROC(Uint32,SDL_rand_bits,(void),(),return)
|
||||
SDL_DYNAPI_PROC(float,SDL_rand_float,(void),(),return)
|
||||
SDL_DYNAPI_PROC(Sint32,SDL_rand_n,(Sint32 a),(a),return)
|
||||
SDL_DYNAPI_PROC(Sint32,SDL_rand,(Sint32 a),(a),return)
|
||||
SDL_DYNAPI_PROC(Uint32,SDL_rand_bits_r,(Uint64 *a),(a),return)
|
||||
SDL_DYNAPI_PROC(Sint32,SDL_rand_r,(Uint64 *a, Sint32 b),(a,b),return)
|
||||
SDL_DYNAPI_PROC(float,SDL_randf,(void),(),return)
|
||||
SDL_DYNAPI_PROC(float,SDL_randf_r,(Uint64 *a),(a),return)
|
||||
SDL_DYNAPI_PROC(void*,SDL_realloc,(void *a, size_t b),(a,b),return)
|
||||
SDL_DYNAPI_PROC(double,SDL_round,(double a),(a),return)
|
||||
SDL_DYNAPI_PROC(float,SDL_roundf,(float a),(a),return)
|
||||
|
||||
@@ -34,12 +34,30 @@ void SDL_srand(Uint64 seed)
|
||||
SDL_rand_initialized = SDL_TRUE;
|
||||
}
|
||||
|
||||
Uint32 SDL_rand_bits(void)
|
||||
Sint32 SDL_rand(Sint32 n)
|
||||
{
|
||||
if (!SDL_rand_initialized) {
|
||||
SDL_srand(0);
|
||||
}
|
||||
|
||||
return SDL_rand_r(&SDL_rand_state, n);
|
||||
}
|
||||
|
||||
float SDL_randf(void)
|
||||
{
|
||||
if (!SDL_rand_initialized) {
|
||||
SDL_srand(0);
|
||||
}
|
||||
|
||||
return SDL_randf_r(&SDL_rand_state);
|
||||
}
|
||||
|
||||
Uint32 SDL_rand_bits_r(Uint64 *state)
|
||||
{
|
||||
if (!state) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// The C and A parameters of this LCG have been chosen based on hundreds
|
||||
// of core-hours of testing with PractRand and TestU01's Crush.
|
||||
// Using a 32-bit A improves performance on 32-bit architectures.
|
||||
@@ -55,13 +73,13 @@ Uint32 SDL_rand_bits(void)
|
||||
// Softw Pract Exper. 2022;52(2):443-458. doi: 10.1002/spe.3030
|
||||
// https://arxiv.org/abs/2001.05304v2
|
||||
|
||||
SDL_rand_state = SDL_rand_state * 0xff1cd035ul + 0x05;
|
||||
*state = *state * 0xff1cd035ul + 0x05;
|
||||
|
||||
// Only return top 32 bits because they have a longer period
|
||||
return (Uint32)(SDL_rand_state >> 32);
|
||||
return (Uint32)(*state >> 32);
|
||||
}
|
||||
|
||||
Sint32 SDL_rand_n(Sint32 n)
|
||||
Sint32 SDL_rand_r(Uint64 *state, Sint32 n)
|
||||
{
|
||||
// Algorithm: get 32 bits from SDL_rand_bits() and treat it as a 0.32 bit
|
||||
// fixed point number. Multiply by the 31.0 bit n to get a 31.32 bit
|
||||
@@ -70,18 +88,19 @@ Sint32 SDL_rand_n(Sint32 n)
|
||||
if (n < 0) {
|
||||
// The algorithm looks like it works for numbers < 0 but it has an
|
||||
// infintesimal chance of returning a value out of range.
|
||||
// Returning -SDL_rand_n(abs(n)) blows up at INT_MIN instead.
|
||||
// Returning -SDL_rand(abs(n)) blows up at INT_MIN instead.
|
||||
// It's easier to just say no.
|
||||
return 0;
|
||||
}
|
||||
|
||||
// On 32-bit arch, the compiler will optimize to a single 32-bit multiply
|
||||
Uint64 val = (Uint64)SDL_rand_bits() * n;
|
||||
Uint64 val = (Uint64)SDL_rand_bits_r(state) * n;
|
||||
return (Sint32)(val >> 32);
|
||||
}
|
||||
|
||||
float SDL_rand_float(void)
|
||||
float SDL_randf_r(Uint64 *state)
|
||||
{
|
||||
// Note: its using 24 bits because float has 23 bits significand + 1 implicit bit
|
||||
return (SDL_rand_bits() >> (32 - 24)) * 0x1p-24f;
|
||||
return (SDL_rand_bits_r(state) >> (32 - 24)) * 0x1p-24f;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user