Skip to content

Commit

Permalink
Limit COM_Rand to 24 bit
Browse files Browse the repository at this point in the history
  • Loading branch information
Novum committed Jan 9, 2025
1 parent 4067b2f commit a7f29bd
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 10 deletions.
4 changes: 2 additions & 2 deletions Quake/cl_parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ static void CL_EntitiesDeltaed (void)
if (model->synctype == ST_FRAMETIME)
ent->syncbase = -cl.time;
else if (model->synctype == ST_RAND)
ent->syncbase = (float)(COM_Rand () & 0xffffff) / 0xffffff;
ent->syncbase = (float)COM_Rand () / COM_RAND_MAX;
else
ent->syncbase = 0.0;
}
Expand Down Expand Up @@ -1265,7 +1265,7 @@ static void CL_ParseUpdate (int bits)
if (model)
{
if (model->synctype == ST_RAND)
ent->syncbase = (float)(COM_Rand () & 0xffffff) / 0xffffff;
ent->syncbase = (float)COM_Rand () / COM_RAND_MAX;
else
ent->syncbase = 0.0;
}
Expand Down
2 changes: 1 addition & 1 deletion Quake/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -3205,5 +3205,5 @@ uint32_t COM_Rand ()
s1 ^= s0;
xorshiro_state[0] = rotl (s0, 26) ^ s1 ^ (s1 << 9);
xorshiro_state[1] = rotl (s1, 13);
return result;
return result & COM_RAND_MAX;
}
4 changes: 3 additions & 1 deletion Quake/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,9 @@ size_t LOC_Format (const char *format, const char *(*getarg_fn) (int idx, void

void COM_SeedRand (uint64_t seed);
uint32_t COM_Rand (void);
#define COM_RAND_MAX UINT32_MAX

// 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
6 changes: 3 additions & 3 deletions Quake/pr_ext.c
Original file line number Diff line number Diff line change
Expand Up @@ -3926,9 +3926,9 @@ static void PF_randomvector (void)
vec3_t temp;
do
{
temp[0] = (COM_Rand () & 0xffffff) * (2.0 / (float)0xffffff) - 1.0;
temp[1] = (COM_Rand () & 0xffffff) * (2.0 / (float)0xffffff) - 1.0;
temp[2] = (COM_Rand () & 0xffffff) * (2.0 / (float)0xffffff) - 1.0;
temp[0] = COM_Rand () * (2.0 / (float)COM_RAND_MAX) - 1.0;
temp[1] = COM_Rand () * (2.0 / (float)COM_RAND_MAX) - 1.0;
temp[2] = COM_Rand () * (2.0 / (float)COM_RAND_MAX) - 1.0;
} while (DotProduct (temp, temp) >= 1);
VectorCopy (temp, G_VECTOR (OFS_RETURN));
}
Expand Down
6 changes: 3 additions & 3 deletions Quake/r_part_fte.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ cvar_t r_fteparticles = {"r_fteparticles", "1", CVAR_ARCHIVE};
#define USE_DECALS
#define Con_Printf Con_SafePrintf

#define frandom() ((COM_Rand () & 0xFFFFFF) * (1.0f / (float)0xFFFFFF))
#define crandom() ((COM_Rand () & 0xFFFFFF) * (2.0f / (float)0xFFFFFF) - 1.0f)
#define hrandom() ((COM_Rand () & 0xFFFFFF) * (1.0f / (float)0xFFFFFF) - 0.5f)
#define frandom() (COM_Rand () * (1.0f / (float)COM_RAND_MAX))
#define crandom() (COM_Rand () * (2.0f / (float)COM_RAND_MAX) - 1.0f)
#define hrandom() (COM_Rand () * (1.0f / (float)COM_RAND_MAX) - 0.5f)
#define particle_s fparticle_s
#define particle_t fparticle_t
typedef vec_t vec2_t[2];
Expand Down

0 comments on commit a7f29bd

Please sign in to comment.