Bug 61129 - Feature request: integer-overflow-detecting arithmetic intrinsics
Summary: Feature request: integer-overflow-detecting arithmetic intrinsics
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: unknown
: P3 enhancement
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2014-05-09 18:15 UTC by Andy Lutomirski
Modified: 2014-12-03 00:32 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2014-05-18 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Andy Lutomirski 2014-05-09 18:15:52 UTC
Clang has a fairly complete family of intrinsics to do integer arithmetic with overflow detection.  They include function like __builtin_uadd_overflow, and they are described here:

http://clang.llvm.org/docs/LanguageExtensions.html#checked-arithmetic-builtins

Please consider supporting these in GCC as well.
Comment 1 Richard Biener 2014-05-13 12:03:24 UTC
There was a recent proof-of-concept patch on the mailinglist to implement that.
Comment 2 Paul Eggert 2014-05-18 21:16:28 UTC
It'd be nice if this were higher priority. I've been wanting this for years. We jump through a lot of confusing hoops in GNU applications to test for integer overflow, both signed and unsigned. See, for example, INT_MULTIPLY_OVERFLOW in:

http://git.savannah.gnu.org/gitweb/?p=gnulib.git;a=blob;f=lib/intprops.h;h=d0bb7a6f57734e15e535cfc6b287a555dc6ccbb3;hb=HEAD

Also, the need for a fast test for unsigned multiplication overflow recently came up in glibc internals discussions when writing a memory allocator. See:

https://sourceware.org/ml/libc-alpha/2014-05/msg00480.html
Comment 3 Andrew Pinski 2014-05-18 21:51:04 UTC
(In reply to Paul Eggert from comment #2)
> It'd be nice if this were higher priority. I've been wanting this for years.
> We jump through a lot of confusing hoops in GNU applications to test for
> integer overflow, both signed and unsigned. See, for example,
> INT_MULTIPLY_OVERFLOW in:
> 
> http://git.savannah.gnu.org/gitweb/?p=gnulib.git;a=blob;f=lib/intprops.h;
> h=d0bb7a6f57734e15e535cfc6b287a555dc6ccbb3;hb=HEAD
> 
> Also, the need for a fast test for unsigned multiplication overflow recently
> came up in glibc internals discussions when writing a memory allocator. See:
> 
> https://sourceware.org/ml/libc-alpha/2014-05/msg00480.html

You would still need them to compile with older GCCs anyways.  Glibc still needs to compiler with a few year old GCC.
Comment 4 Manuel López-Ibáñez 2014-05-18 21:57:06 UTC
I think this was the last message:

https://gcc.gnu.org/ml/gcc/2014-04/msg00194.html
Comment 5 Andy Lutomirski 2014-12-03 00:32:00 UTC
According to https://gcc.gnu.org/gcc-5/changes.html, this is now implemented.  Thanks!