Skip to content

Commit

Permalink
COM_Rand() : existing code expects a signed integer (like rand()), so…
Browse files Browse the repository at this point in the history
… do so
  • Loading branch information
vsonnier committed Jan 9, 2025
1 parent a7f29bd commit 6f09255
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
6 changes: 4 additions & 2 deletions Quake/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -3196,7 +3196,8 @@ static inline uint32_t rotl (const uint32_t x, int k)
{
return (x << k) | (x >> (32 - k));
}
uint32_t COM_Rand ()

int32_t COM_Rand ()
{
// Xorshiro64**
const uint32_t s0 = xorshiro_state[0];
Expand All @@ -3205,5 +3206,6 @@ uint32_t COM_Rand ()
s1 ^= s0;
xorshiro_state[0] = rotl (s0, 26) ^ s1 ^ (s1 << 9);
xorshiro_state[1] = rotl (s1, 13);
return result & COM_RAND_MAX;

return (int)(result & COM_RAND_MAX);
}
4 changes: 2 additions & 2 deletions Quake/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -313,8 +313,8 @@ const char *LOC_GetString (const char *key);
qboolean LOC_HasPlaceholders (const char *str);
size_t LOC_Format (const char *format, const char *(*getarg_fn) (int idx, void *userdata), void *userdata, char *out, size_t len);

void COM_SeedRand (uint64_t seed);
uint32_t COM_Rand (void);
void COM_SeedRand (uint64_t seed);
int32_t COM_Rand (void);

// Limit to 24 bits so values fit in float mantissa & don't get negative when casting to ints
#define COM_RAND_MAX 0xFFFFFF
Expand Down

0 comments on commit 6f09255

Please sign in to comment.