Fix modulo by zero done in __external_prng_bound__ and __internal_prng_bound__

This commit is contained in:
NepDisk 2025-08-27 08:28:51 -04:00
parent 28a47037ab
commit ec642a20c5
2 changed files with 38 additions and 13 deletions

View file

@ -1626,7 +1626,14 @@ void F_TitleScreenTicker(boolean run)
return;
}
numstaff = M_RandomKey(numstaff)+1;
if (numstaff)
{
numstaff = M_RandomKey(numstaff)+1;
}
else
{
numstaff++;;
}
// Setup demo name
sprintf(dname, "%s/GHOST_%u", mapheaderinfo[mapnum]->lumpname, numstaff);

View file

@ -45,14 +45,23 @@ ATTRINLINE static UINT32 FUNCINLINE __external_prng__(void)
ATTRINLINE static UINT32 FUNCINLINE __external_prng_bound__(UINT32 bound)
{
// Do rejection sampling to remove the modulo bias.
UINT32 threshold = -bound % bound;
for (;;)
// Handle zero like it would previously.
if (bound == 0)
{
UINT32 r = __external_prng__();
if (r >= threshold)
(void)__external_prng__();
return 0;
}
else
{
// Do rejection sampling to remove the modulo bias.
UINT32 threshold = -bound % bound;
for (;;)
{
return r % bound;
UINT32 r = __external_prng__();
if (r >= threshold)
{
return r % bound;
}
}
}
}
@ -144,14 +153,23 @@ ATTRINLINE static UINT32 FUNCINLINE __internal_prng__(void)
*/
ATTRINLINE static UINT32 FUNCINLINE __internal_prng_bound__(UINT32 bound)
{
// Do rejection sampling to remove the modulo bias.
UINT32 threshold = -bound % bound;
for (;;)
// Handle zero like it would previously.
if (bound == 0)
{
UINT32 r = __internal_prng__();
if (r >= threshold)
(void)__internal_prng__();
return 0;
}
else
{
// Do rejection sampling to remove the modulo bias.
UINT32 threshold = -bound % bound;
for (;;)
{
return r % bound;
UINT32 r = __internal_prng__();
if (r >= threshold)
{
return r % bound;
}
}
}
}