[Bug middle-end/48580] missed optimization: integer overflow checks

glisse at gcc dot gnu.org gcc-bugzilla@gcc.gnu.org
Sun May 19 13:04:00 GMT 2013


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=48580

--- Comment #20 from Marc Glisse <glisse at gcc dot gnu.org> ---
(In reply to Zack Weinberg from comment #5)
> Addendum: what would *you* describe as the correct C idiom for
> ensuring that the product of two signed integers was positive and did
> not overflow the range of a same-sized signed integer, assuming
> nothing about either multiplicand?

The most natural way to do it depends on whether you have access to a wider
type (and may rely on modular casts to signed integer types as guaranteed by
gcc). For instance if you want a non-negative result that fits an int:

  return x * (unsigned long long)(y) <= __INT_MAX__;

or if you only care about signed overflow and not the sign of the result:

  long long l = x * (long long)(y);
  return l == (int) l;

The value of the overflow flags after a multiplication doesn't seem modeled in
i386.md currently (apart from "clobbered"), so it won't be used, but the code
generated is not too horrible.



More information about the Gcc-bugs mailing list