3 ms·
Botched the editing near the examples. Sizes of intermediates are a big issue. My thinking on this was that it's the compiler's job to prevent overflow in inte
by Animats 7y ago
Botched the editing near the examples.
Sizes of intermediates are a big issue.
My thinking on this was that it's the compiler's job to prevent overflow in intermediate values where the final result will not overflow.
When you write
int_32 a,b,c,n;
...
n = (a*b)*c;
that's legal to compute in 32-bit, but requires overflow checking on the intermediates. If an overflow occurs, there will be an overflow in the result. (Although, the case where some values are zero is an issue. Suppose a * b overflows but c is zero so it doesn't matter. That's probably an error.)
Sometimes you have to use larger sized intermediates. For
int_32 m,n,p;
...
n = (m * n) / p;
how big is each part? Above, you'd have to compute (m * n) as a 64-bit product, do a 64-bit divide, and only then check that the result fit in n.