user1287161 - 7 months ago 29

C Question

I don't understand how this xxhash function is able to add larger values than uint32 can store. Can someone explain why this wizardry is possible?

`static const U32 PRIME32_1 = 2654435761U;`

static const U32 PRIME32_2 = 2246822519U;

...

U32 v1 = seed + PRIME32_1 + PRIME32_2;

Answer

The standard requires that the standard unsigned integral types handle overflow/underflow by 'wrapping' the value modulo `FOO_MAX + 1`

. (e.g. `UINT32_MAX`

is the macro holding the maximum value of `uint32_t`

)

Assuming `U32`

refers to `uint32_t`

, the additions are performed modulo `4294967296`

.