This patch adds __builtin_add_overflow, __builtin_sub_overflow, and __builtin_mul_overflow to clang. These are useful functions and are easier to use than the existing overflow builtins because they can accept any type of integer for the inputs and output. GCC 5 already has these functions in it, and I think they should be added to clang.
The need for these built-ins is discussed here:
https://llvm.org/bugs/show_bug.cgi?id=21716
The repository I used to help develop this patch is here:
https://github.com/DavidEGrayson/builtin_overflow
The patch should apply cleanly to revision 248284 of clang. I added these new builtins to the documentation and added tests for them.
I went through some extra effort to support boolean arguments, even though GCC does not support boolean arguments for these functions.
I am new to clang/LLVM development and I am not really sure if I have submitted this patch in the right way. For example, I never found a way to select "clang" as the project that I am submitting this patch to. Please let me know if I there is a better way I can submit patches. I hope someone can review this and commit it to clang. Please let me know if there are any problems with it. Thanks!
Hmm. It's not necessarily truncation; you could meaningfully use these intrinsics to add two small signed numbers and store the result in an unsigned number. It's the unique representative value modulo 2^n, where n is the bit-width of the result.