This is fine. According to the C99 specification, for unsigned long operations, the result is the same but reduced modulo 232 (§6.2.5):
A computation involving unsigned operands can never overflow, because a result
that cannot be represented by the resulting unsigned integer type is reduced
modulo the number that is one greater than the largest value that can be
represented by the resulting type.
So this behaviour isn't actually "overflow", but I'll call it that for simplicity in this answer. Since for modular arithmetic we have
a1 ≡ b1 (mod m)
a2 ≡ b2 (mod m)
implies
a1 + a2 ≡ b1 + b2 (mod m)
We have
Next * a ≡ k (mod 2^32)
where k
is Next * a
with "overflow". So since M = 2^32
,
Next * a + c ≡ k + c (mod M)
The result with "overflow" is equivalent to the one without "overflow" under modular arithmetic, so the formula is fine. Once we reduce modulo M = 2^32
, it will give the same result.