Re: Efficient detection of signed overflow?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



* Mark Dickinson:

> long a, b, x;
> ...
> x = a + b;
> if ((x^a) < 0 && (x^b) < 0)
>     goto deal_with_overflow;
> ...

Would this test be better?  I think it's equivalent, and it saves a
comparison.

  if (((x ^ a) & (x ^ b)) < 0)

> Any suggestions for improvements over this?

Use -fwrapv and your first version.

> Are there any common C code constructs that gcc would compile to a
> jump-on-overflow instruction on x86?

I don't think so.  Without support throughout GCC, anything in this
area (like a builtin) would mostly act as an optimization barrier.

[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux