相关文章推荐
想发财的镜子  ·  微信支付 JSAPI ...·  1 月前    · 
没有腹肌的豆腐  ·  【VSCode - Vetur ...·  1 年前    · 
体贴的拐杖  ·  installation - ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I was wondering on how to use this function, because I get an error when I do this:

#define INT_ADD_OVERFLOW_P(a, b) \
__builtin_add_overflow_p (a, b, (__typeof__ ((a) + (b))) 0);
#include <stdio.h>
#include <assert.h>
__int main()
    int x1 = -1073741826;
    int y1 = -1073741826;
    int z1 = x1+y1; 
    INT_ADD_OVERFLOW_P ( x1,  y1);
    printf("%d\n",z1);
    return 0;

Compile_OUTPUT:

gcc -c -Wall -D DEBUG  tempFile.c
gcc tempFile.o -o tempFile
Makefile:8: recipe for target 'tempFile' failed//new error after update

Compile_ERROR:

tempFile.c: In function ‘main’:
tempFile.c:10:1: warning: implicit declaration of function        ‘__builtin_add_overflow_p’ [-Wimplicit-function-declaration]
tempFile.o: In function `main':
tempFile.c:(.text+0x44): undefined reference to `__builtin_add_overflow_p'
collect2: ld returned 1 exit status
make: *** [tempFile] Error 1

Here is a link to the functions I want to use:

https://gcc.gnu.org/onlinedocs/gcc/Integer-Overflow-Builtins.html

Here is the makefile that i'm using:

compiler=gcc
CFLAGS=-c -Wall -D DEBUG 
programname=tempFile
all: $(programname)
$(programname): $(programname).o
    $(compiler) $(programname).o -o $(programname)
$(programname).o: $(programname).c
    $(compiler) $(CFLAGS) $(programname).c
clean:
    rm *o $(programname)
                There's your problem - those builtins were added in a later version. (5.something I think)
– user253751
                Jul 28, 2016 at 2:21

So I figured out how to use the built-in functions. Instead of using __builtin_add_overflow_p I used __builtin_add_overflow

#include <stdio.h>
#include <assert.h>
int main(void)
  int x1 = -1073741826;
  int y1 = -1073741826;
  int z1 = x1 + y1; 
  int temp;
  printf("%d\n", __builtin_add_overflow(x1, y1, &temp));
  if (__builtin_add_overflow(x1, y1, &temp)) {
    printf("overflow detected");
  printf("%d\n", z1);
  return 0;
                The built-in functions that ends with a p are similar to __builtin_add_overflow, __builtin_sub_overflow and __builtin_mul_overflow. The difference is that the functions that end with a p  don't store the result of the arithmetic operation anywhere and the last argument is not a pointer, but some expression with integral type other than enumerated or Boolean type. Here is a link to integral types msdn.microsoft.com/en-us/library/exx3b86w%28v=vs.80%29.aspx
– Eagle
                Aug 1, 2016 at 10:00
                I don´t see why this would be an answer. Your question asked for how to use one function, and your solution is using an other function which has a different definition? Put it as a comment to your question instead saying "not my problem anymore, but question remains".
– Andreas
                Aug 5, 2016 at 16:30

You can also use __builtin_add_overflow_p to implement something a bit like a saturating addition:

#define sadd(a, b)                                             \
    ({                                                         \
        __auto_type _a = (a);                                  \
        __auto_type _b = (b);                                  \
        __builtin_add_overflow_p(_a, _b, _a) ? _a : (_a + _b); \

Example:

uint8_t foo = 253;
foo = sadd(foo, 1); // foo = 254
foo = sadd(foo, 1); // foo = 255
foo = sadd(foo, 1); // foo = 255

This is using a GCC extension called Statement Expressions, and __auto_type is explained here https://gcc.gnu.org/onlinedocs/gcc/Typeof.html.

The call to __builtin_add_overflow_p is saying, if there would be an overflow from addition of a and b (the first 2 arguments), stored in a (the 3rd argument), return the original value of a, otherwise return the result of the addition between a and b.

You could also probably use __builtin_add_overflow if you added a 3rd variable c, and returned that if the result didn't overflow, otherwise return a.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.