Why does the following code returns zero, when compiled with clang?
#include <stdint.h>
uint64_t square() {
return ((uint32_t)((double)4294967296.0)) - 10;
}
The assembly it produces is:
push rbp
mov rbp, rsp
xor eax, eax
pop rbp
ret
I would have expected that double to become zero (integer) and that minus would wrap it around. In other words, why doesn't in matter what number there is to subtract, as it always produces zero? Note that gcc does produce different numbers, as expected:
push rbp
mov rbp, rsp
mov eax, 4294967285
pop rbp
ret
I assume casting 4294967296.0 to uint32_t
is undefined behaviour but even then, I would expect to produce different results for different subtrahends.