top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Builtin c99 functions, how to use?

+2 votes
70 views

There are many builtin versions of various c99 functions. I find it difficult, however, to use them instead of the glibc equivalent, even when calling __builtin_xx directly. Specifically in the case of fminf, under what conditions will gcc give me nice one-line branch-free assembly vs a call out to the library?

posted Feb 15, 2015 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote

My two cents -

1) Calling __builtin_* directly is generally useless.
2) -ffinite-math-only or when the arguments are known at compile-time, see the source in gcc/builtins.c. -ffast-math is usually a good starting point to see if a math optimization is implemented.

answer Feb 15, 2015 by Deepti Singh
Similar Questions
0 votes

My application builds fine with -flto, but only if I do not also specify -std=c99.

If someone can help me, that would be wonderful. I have created a very simple test, below, to demonstrate the problem.

main.c:

#include "foo.h"
void main(int argc, char** argv) {
 int input = atoi(argv[1]);
 printf("%dn", foo(input));
}

foo.h:

inline int foo(int x);

foo.c:

#include "foo.h"
inline int foo(int x) {
 while (x < 900) {
 x += x;
 }
 return x;
}

Makefile:

CFLAGS += -flto -std=c99
LDFLAGS += -flto -std=c99

main : main.o foo.o
main.o : main.c foo.h
foo.o : foo.c foo.h

.PHONY : clean

clean :
 $(RM) main main.o foo.o

Results of running make:

cc -flto -std=c99 -c -o main.o main.c

In file included from main.c:3:0:

foo.h:1:12: warning: inline function  foo  declared but never defined [enabled by default]
 inline int foo(int x);
 ^
foo.h:1:12: warning: inline function  foo  declared but never defined [enabled by default]
cc -flto -std=c99 -c -o foo.o foo.c
cc -flto -std=c99 main.o foo.o -o main
/tmp/ccTDIBGZ.ltrans0.ltrans.o:ccTDIBGZ.ltrans0.o:function main: error: undefined reference to 'foo'
collect2: error: ld returned 1 exit status
make: *** [main] Error 1

Without the -std=c99 flags, make runs successfully and without warnings.

0 votes

I've been using the settrace function to write a tracer for my program, which is working great except that it doesn't seem to work for built-in functions, like open('filename.txt'). This doesn't seem to be documented, so I'm not sure if I'm doing something wrong or that's the expected behavior.

If settrace's behavior in this regard is fixed, is there any way to trace calls to open()? I don't want to use Linux's strace, as it'll run for whole program (not just the part I want) and won't show my python line numbers/file names, etc. The other option I considered was monkey-patching the open function through a wrapper, like:

def wrapped_open(*arg,**kw):
 print 'open called'
 traceback.print_stack()
 f = __builtin__.open(*arg,**kw)
 return f
open = wrapped_open

but that seemed very brittle to me. Could someone suggest a better way of doing this?

...