Fix modulo by zero done in __external_prng_bound__ and __internal_prng_bound__
This commit is contained in:
parent
28a47037ab
commit
ec642a20c5
2 changed files with 38 additions and 13 deletions
|
|
@ -1626,7 +1626,14 @@ void F_TitleScreenTicker(boolean run)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
numstaff = M_RandomKey(numstaff)+1;
|
if (numstaff)
|
||||||
|
{
|
||||||
|
numstaff = M_RandomKey(numstaff)+1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
numstaff++;;
|
||||||
|
}
|
||||||
|
|
||||||
// Setup demo name
|
// Setup demo name
|
||||||
sprintf(dname, "%s/GHOST_%u", mapheaderinfo[mapnum]->lumpname, numstaff);
|
sprintf(dname, "%s/GHOST_%u", mapheaderinfo[mapnum]->lumpname, numstaff);
|
||||||
|
|
|
||||||
|
|
@ -45,14 +45,23 @@ ATTRINLINE static UINT32 FUNCINLINE __external_prng__(void)
|
||||||
|
|
||||||
ATTRINLINE static UINT32 FUNCINLINE __external_prng_bound__(UINT32 bound)
|
ATTRINLINE static UINT32 FUNCINLINE __external_prng_bound__(UINT32 bound)
|
||||||
{
|
{
|
||||||
// Do rejection sampling to remove the modulo bias.
|
// Handle zero like it would previously.
|
||||||
UINT32 threshold = -bound % bound;
|
if (bound == 0)
|
||||||
for (;;)
|
|
||||||
{
|
{
|
||||||
UINT32 r = __external_prng__();
|
(void)__external_prng__();
|
||||||
if (r >= threshold)
|
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)
|
ATTRINLINE static UINT32 FUNCINLINE __internal_prng_bound__(UINT32 bound)
|
||||||
{
|
{
|
||||||
// Do rejection sampling to remove the modulo bias.
|
// Handle zero like it would previously.
|
||||||
UINT32 threshold = -bound % bound;
|
if (bound == 0)
|
||||||
for (;;)
|
|
||||||
{
|
{
|
||||||
UINT32 r = __internal_prng__();
|
(void)__internal_prng__();
|
||||||
if (r >= threshold)
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue