Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using the builtin function __builtin_add_overflow_p in gcc

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)
like image 647
Eagle Avatar asked Jul 28 '16 01:07

Eagle


People also ask

What is builtin functions in GCC?

Builtin functions of GCC compiler. Bitwise Operators in C/C++ Python Bitwise Operators. Bits manipulation (Important tactics) Little and Big Endian Mystery.

What is __ Builtin_ffs?

int__builtin_ffs (unsigned int x) Returns one plus the index of the least significant 1-bit of x, or if x is zero, returns zero. int__builtin_clz (unsigned int x) Returns the number of leading 0-bits in x, starting at the most significant bit position. If x is 0, the result is undefined.


1 Answers

__builtin_add_overflow_p is supported by GCC 7 that has not been released yet

  • Current development
  • 6.2.0 (Latest releases)
like image 52
Maxim Pestryakov Avatar answered Oct 17 '22 01:10

Maxim Pestryakov