Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

xNextInt: unnecessary if branch? #134

Open
arnaud-ma opened this issue Dec 27, 2024 · 1 comment
Open

xNextInt: unnecessary if branch? #134

arnaud-ma opened this issue Dec 27, 2024 · 1 comment

Comments

@arnaud-ma
Copy link

Here:

cubiomes/rng.h

Lines 214 to 225 in e61f905

static inline int xNextInt(Xoroshiro *xr, uint32_t n)
{
uint64_t r = (xNextLong(xr) & 0xFFFFFFFF) * n;
if ((uint32_t)r < n)
{
while ((uint32_t)r < (~n + 1) % n)
{
r = (xNextLong(xr) & 0xFFFFFFFF) * n;
}
}
return r >> 32;
}

The condition ((uint32_t)r < (~n + 1) % n) will never be true because (~n + 1) % n simplifies to 0 for any value of n.

@Nel-S
Copy link
Contributor

Nel-S commented Jan 15, 2025

Since n is unsigned, it is not necessarily true that (~n + 1) % n == 0 for all n. For example, if n = 3,

  • ~n = 4294967292 (due to n being unsigned and 32 bits),
  • ~n + 1 = 4294967293, and
  • (~n + 1) % n = 1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants