I have implemented the following function for detecting whether or not a multiplication overflows:
bool IsSafeMul(uint32_t x, uint32_t y) {
uint32_t z = x * y;
return (z >= x && z >= y);
}
I have verified it empirically, but would like to ensure that:
- Is it 100% guaranteed to work correctly (i.e., no false-positives and no false-negatives)?
- Is it 100% guaranteed to work correctly for any other
unsigned
type?